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;
string A, num[1005];
int n, m, Alen, c[10], f[1005][1005];
int a[1005][1005], b[1005], cnt[10], len[1005], ans;
pair<int, int> g[1005][1005];
bool flag[1005];
string make(string X) { return X + string(m - X.length(), '0'); }
int calc(int x) {
  int tot = 0;
  for (int i = 1; i <= n; i++) {
    int t = i <= x ? 1 : 0;
    int l = len[b[i]];
    for (int j = Alen; j < l; j++) {
      int y = num[b[i]][j] - '0' + t;
      if (y >= 10)
        y %= 10, t = 1;
      else
        t = 0;
      tot += c[y];
    }
    if (t) tot += c[t];
  }
  return tot;
}
void PRINT(int i, int j) {
  if (i == 0 || j == -1) return;
  printf("%d", g[i][j].second);
  PRINT(i - 1, g[i][j].first);
}
void PRINT2(int i, int j) {
  if (i == 0 || j == -1) return;
  printf("%d", j);
  PRINT2(i - 1, g[i][j].first);
}
int main() {
  memset(f, 0xcf, sizeof(f));
  memset(g, -1, sizeof(g));
  cin >> A >> n;
  reverse(A.begin(), A.end());
  m = Alen = A.length();
  for (int i = 1; i <= n; i++) {
    cin >> num[i];
    len[i] = num[i].length();
    m = max(m, (int)num[i].length());
    reverse(num[i].begin(), num[i].end());
  }
  for (int i = 0; i < 10; i++) cin >> c[i];
  for (int i = 1; i <= n; i++) num[i] = make(num[i]);
  A = make(A);
  f[0][0] = 0;
  for (int i = 1; i <= n; i++) a[0][i] = i;
  for (int i = 0; i < Alen; i++) {
    memset(cnt, 0, sizeof(cnt));
    for (int t = 1; t <= n; t++) cnt[num[a[i][t]][i] - '0']++;
    for (int t = 9; t >= 0; t--) cnt[t] += cnt[t + 1];
    for (int t = n; t >= 1; t--) b[cnt[num[a[i][t]][i] - '0']--] = a[i][t];
    for (int t = 1; t <= n; t++) a[i + 1][t] = b[t];
    for (int k = 0; k < 10; k++) {
      if ((A[i] != '?' && k != A[i] - '0') || (i == Alen - 1 && k == 0))
        continue;
      int sum = 0, pos = 0;
      for (int t = 1; t <= n; t++) {
        int x = num[a[i + 1][t]][i] - '0' + k;
        if (x >= 10) x = x % 10, pos++;
        sum += c[x];
      }
      if (f[i][0] + sum > f[i + 1][pos]) g[i + 1][pos] = make_pair(0, k);
      f[i + 1][pos] = max(f[i + 1][pos], f[i][0] + sum);
      pos++;
      memset(flag, false, sizeof(flag));
      for (int j = 1; j <= n; j++) {
        flag[a[i][j]] = true;
        ;
        for (; pos <= n &&
               num[a[i + 1][pos]][i] - '0' + flag[a[i + 1][pos]] + k >= 10;
             pos++)
          ;
        int y = num[a[i][j]][i] - '0' + k;
        sum = sum - c[y % 10] + c[(y + 1) % 10];
        if (f[i][j] + sum > f[i + 1][pos - 1])
          g[i + 1][pos - 1] = make_pair(j, k);
        f[i + 1][pos - 1] = max(f[i + 1][pos - 1], f[i][j] + sum);
      }
    }
  }
  int id;
  for (int i = 0; i <= n; i++)
    if (f[Alen][i] >= 0 && f[Alen][i] + calc(i) > ans)
      ans = max(ans, f[Alen][i] + calc(i)), id = i;
  printf("%d\n", ans);
  return 0;
}