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 N = 1100;
int n, dp[N][N], m, ord[N][N], sum[15], pos[15], vi[N];
int len[N], c[15];
char a[N], b[N][N];
pair<int, int> sh[N];
vector<int> t[10];
inline int read() {
  int f = 1, x = 0;
  char s = getchar();
  while (s < '0' || s > '9') {
    if (s == '-') f = -1;
    s = getchar();
  }
  while (s >= '0' && s <= '9') {
    x = x * 10 + s - '0';
    s = getchar();
  }
  return x * f;
}
inline bool check(int i, int j) {
  if (i == len[0] && j == 0) return 0;
  return (a[i] == '0' + j || a[i] == '?');
}
inline bool judge(int id, int i, int j) {
  if (i > len[id] && j == 0) return 0;
  return 1;
}
signed main() {
  scanf("%s", a + 1);
  m = len[0] = strlen(a + 1);
  reverse(a + 1, a + 1 + m);
  n = read();
  for (int i = 1; i <= n; i++) {
    scanf("%s", b[i] + 1);
    len[i] = strlen(b[i] + 1);
    m = max(m, len[i]);
    reverse(b[i] + 1, b[i] + 1 + len[i]);
    len[i] = max(len[i], len[0]);
  }
  for (int i = 0; i < 10; i++) c[i] = read();
  int tmp = strlen(a + 1);
  for (int i = tmp + 1; i <= m; i++) a[i] = '0';
  for (int i = 1; i <= n; i++) {
    tmp = strlen(b[i] + 1);
    for (int j = tmp + 1; j <= m; j++) b[i][j] = '0';
  }
  memset(dp, -0x3f, sizeof(dp));
  dp[0][n + 1] = 0;
  for (int i = 1; i <= n; i++) ord[0][i] = i;
  for (int i = 1; i <= m; i++) b[0][i] = '0' + 15;
  for (int i = 1; i <= m; i++) {
    for (int j = 0; j < 10; j++) t[j].clear();
    for (int j = 1; j <= n; j++) {
      int x = ord[i - 1][j];
      t[b[x][i] - '0'].push_back(x);
    }
    int w = 0;
    for (int j = 0; j < 10; j++)
      for (int k = 0; k < (int)t[j].size(); k++) ord[i][++w] = t[j][k];
    for (int j = 0; j < 10; j++) {
      sum[j] = 0;
      pos[j] = n + 1;
      for (int k = n; k >= 1; k--) {
        int x = ord[i][k];
        if (judge(x, i, j)) sum[j] += c[(b[x][i] - '0' + j) % 10];
      }
      while (pos[j] > 0 && b[ord[i][pos[j]]][i] - '0' + j >= 10) pos[j]--;
      pos[j]++;
      if (check(i, j))
        dp[i][pos[j]] = max(dp[i][pos[j]], dp[i - 1][n + 1] + sum[j]);
    }
    for (int j = 1; j <= n; j++) vi[j] = 0;
    vi[0] = 2000;
    for (int j = n; j >= 1; j--) {
      int x = ord[i - 1][j];
      vi[x] = 1;
      for (int k = 0; k < 10; k++) {
        if (judge(x, i, k)) sum[k] -= c[(b[x][i] - '0' + k) % 10];
        sum[k] += c[(b[x][i] - '0' + k + 1) % 10];
        while (pos[k] > 0 &&
               b[ord[i][pos[k]]][i] - '0' + k + vi[ord[i][pos[k]]] >= 10)
          pos[k]--;
        pos[k]++;
        if (check(i, k))
          dp[i][pos[k]] = max(dp[i][pos[k]], dp[i - 1][j] + sum[k]);
      }
    }
  }
  int ans = dp[m][n + 1];
  for (int i = n; i >= 1; i--) ans = max(ans, dp[m][i] + (n - i + 1) * c[1]);
  printf("%d\n", ans);
}