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 maxn = 1005;
const int maxlen = 1005;
char temp[maxlen];
int a[maxlen], b[maxn][maxlen];
int n, c[maxn], f[2][maxn], lena, len[maxn], cur, sz[10], lenmax;
int rk[2][maxn], pos[2][maxn];
int main() {
  int i, j, ans, num, jinwei;
  scanf("%s%d", temp, &n);
  for (i = 1, lenmax = lena = strlen(temp); i <= lena; i++)
    a[i] = (temp[lena - i] == '?') ? -1 : (temp[lena - i] - '0');
  for (i = 1; i <= n; i++) {
    scanf("%s", temp);
    for (j = 1, lenmax = max(lenmax, len[i] = strlen(temp)); j <= len[i]; j++)
      b[i][j] = temp[len[i] - j] - '0';
    for (; j <= 1000; j++) b[i][j] = 0;
  }
  for (i = 0; i < 10; i++) scanf("%d", &c[i]);
  cur = 0;
  memset(f[cur], 0x80, sizeof(f[cur]));
  f[cur][0] = 0;
  for (i = 1; i <= n; i++) rk[cur][i] = i, pos[cur][i] = i;
  for (i = 1; i <= lenmax; i++) {
    cur ^= 1;
    memset(f[cur], 0x80, sizeof(f[cur]));
    for (j = 0; j < 10; j++) sz[j] = 0;
    for (j = 1; j <= n; j++) ++sz[b[j][i]];
    ++sz[9];
    for (j = 8; j >= 0; j--) sz[j] += sz[j + 1];
    for (j = n; j; j--) rk[cur][pos[cur ^ 1][j]] = --sz[b[pos[cur ^ 1][j]][i]];
    for (j = 1; j <= n; j++) pos[cur][rk[cur][j]] = j;
    if (a[i] != -1) {
      num = 0;
      for (j = 1; j <= n; j++)
        if (b[j][i] + a[i] || i <= len[j] || i <= lena)
          num += c[(b[j][i] + a[i]) % 10];
      jinwei = 0;
      for (j = 0; j <= n; j++) {
        if (j) {
          if (b[pos[cur ^ 1][j]][i] + a[i] || i <= len[pos[cur ^ 1][j]] ||
              i <= lena)
            num -= c[(b[pos[cur ^ 1][j]][i] + a[i]) % 10];
          num += c[(b[pos[cur ^ 1][j]][i] + a[i] + 1) % 10];
        }
        while (jinwei < n && b[pos[cur][jinwei + 1]][i] +
                                     (rk[cur ^ 1][pos[cur][jinwei + 1]] <= j) +
                                     a[i] >=
                                 10)
          jinwei++;
        f[cur][jinwei] = max(f[cur][jinwei], f[cur ^ 1][j] + num);
      }
    } else
      for (a[i] = (i == lena) ? 1 : 0; a[i] <= 9; a[i]++) {
        num = 0;
        for (j = 1; j <= n; j++)
          if (b[j][i] + a[i] || i <= len[j] || i <= lena)
            num += c[(b[j][i] + a[i]) % 10];
        jinwei = 0;
        for (j = 0; j <= n; j++) {
          if (j) {
            if (b[pos[cur ^ 1][j]][i] + a[i] || i <= len[pos[cur ^ 1][j]] ||
                i <= lena)
              num -= c[(b[pos[cur ^ 1][j]][i] + a[i]) % 10];
            num += c[(b[pos[cur ^ 1][j]][i] + a[i] + 1) % 10];
          }
          while (jinwei < n &&
                 b[pos[cur][jinwei + 1]][i] +
                         (rk[cur ^ 1][pos[cur][jinwei + 1]] <= j) + a[i] >=
                     10)
            jinwei++;
          f[cur][jinwei] = max(f[cur][jinwei], f[cur ^ 1][j] + num);
        }
      }
  }
  ans = -1;
  for (i = 0; i <= n; i++) ans = max(ans, f[cur][i] + i * c[1]);
  printf("%d\n", ans);
  return 0;
}