Problem: Boris really likes numbers and even owns a small shop selling interesting numbers. He has n decimal numbers Bi. Cost of the number in his shop is equal to the sum of costs of its digits. You are given the values cd, where cd is the cost of the digit d. Of course, Boris is interested in that numbers he owns have the maximum cost possible.

Recently Boris got hold of the magical artifact A, which can allow him to increase the cost of his collection. Artifact is a string, consisting of digits and '?' symbols. To use the artifact, Boris must replace all '?' with digits to get a decimal number without leading zeros (it is also not allowed to get number 0). After that, the resulting number is added to all numbers Bi in Boris' collection. He uses the artifact exactly once.

What is the maximum cost of the collection Boris can achieve after using the artifact?

Input

First line contains artifact A, consisting of digits '0'–'9' and '?' symbols (1 ≤ |A| ≤ 1000). Next line contains n — the amount of numbers in Boris' collection (1 ≤ n ≤ 1000). Next n lines contain integers Bi (1 ≤ Bi < 101000). A doesn't start with '0'.

Last line contains ten integers — costs of digits c0, c1, ..., c9 (0 ≤ ci ≤ 1000).

Output

Output one integer — the maximum possible cost of the collection after using the artifact.

Examples

Input

42
3
89
1
958
0 0 1 1 2 2 3 3 4 4


Output

4


Input

?5?
4
2203
5229
276
6243
2 1 6 1 1 2 5 2 2 3


Output

62

Note

In the second sample input, the optimal way is to compose the number 453. After adding this number, Boris will have numbers 2656, 5682, 729 and 6696. The total cost of all digits in them is equal to 18 + 15 + 11 + 18 = 62. 

Solution (in CPP):

Solution:
#include <bits/stdc++.h>
using namespace std;
const int DIM = 1005;
int dp[DIM][DIM], pos[DIM][DIM];
int num[DIM][DIM], ord[DIM][DIM];
int cst[10], dig[DIM];
char str[DIM];
struct Struct {
  int a, b, id;
  Struct(int _a = 0, int _b = 0, int _id = 0) : a(_a), b(_b), id(_id) {}
} aux[DIM];
void process(int v[DIM], char str[DIM]) {
  v[0] = strlen(str + 1);
  for (int i = 1; i <= v[0]; ++i)
    v[i] = (str[v[0] - i + 1] == '?' ? -1 : str[v[0] - i + 1] - '0');
}
inline bool cmp(Struct s1, Struct s2) {
  if (s1.a != s2.a) return s1.a > s2.a;
  return s1.b < s2.b;
}
int main(void) {
  cin >> (str + 1);
  process(num[0], str);
  int n;
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    memset(str, 0, sizeof str);
    cin >> (str + 1);
    process(num[i], str);
  }
  int mx = 0;
  for (int i = 0; i <= n; ++i) mx = max(mx, num[i][0]);
  for (int i = 1; i <= n; ++i) ord[0][i] = i;
  for (int j = 1; j <= mx; ++j) {
    for (int i = 1; i <= n; ++i) aux[i] = Struct(num[i][j], ord[j - 1][i], i);
    sort(aux + 1, aux + n + 1, cmp);
    for (int i = 1; i <= n; ++i) ord[j][aux[i].id] = i;
  }
  for (int j = 0; j <= mx; ++j)
    for (int i = 1; i <= n; ++i) pos[j][ord[j][i]] = i;
  for (int c = 0; c <= 9; ++c) cin >> cst[c];
  memset(dp, -1, sizeof dp);
  dp[0][0] = 0;
  for (int j = 1; j <= mx; ++j) {
    int le, ri;
    if (num[0][0] < j)
      le = ri = 0;
    else if (num[0][j] >= 0)
      le = ri = num[0][j];
    else if (num[0][0] == j)
      le = 1, ri = 9;
    else
      le = 0, ri = 9;
    for (int d = le; d <= ri; ++d) {
      int nr = 0, cs = 0;
      for (int i = 1; i <= n; ++i) {
        dig[i] = num[i][j] + d;
        if (dig[i] >= 10) {
          ++nr;
          cs += cst[dig[i] - 10];
          dig[i] -= 10;
        } else if (!(dig[i] == 0 and j > max(num[0][0], num[i][0])))
          cs += cst[dig[i]];
      }
      for (int msk = 0; msk <= n; ++msk) {
        if (msk) {
          int ps = pos[j - 1][msk];
          if (!(dig[ps] == 0 and j > max(num[0][0], num[ps][0])))
            cs -= cst[dig[ps]];
          dig[ps]++;
          if (dig[ps] >= 10) {
            ++nr;
            cs += cst[dig[ps] - 10];
            dig[ps] -= 10;
          } else if (!(dig[ps] == 0 and j > max(num[0][0], num[ps][0])))
            cs += cst[dig[ps]];
        }
        if (dp[j - 1][msk] < 0) continue;
        dp[j][nr] = max(dp[j][nr], dp[j - 1][msk] + cs);
      }
    }
  }
  int ans = 0;
  for (int msk = 0; msk <= n; ++msk) {
    if (dp[mx][msk] < 0) continue;
    ans = max(ans, dp[mx][msk] + cst[1] * msk);
  }
  cout << ans << endl;
  return 0;
}