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;
char a[1100], b[1100][1100];
const int inf = 2000000000;
int c[1100], len[1100], f[1100][1100], sum[1100], ran[2][1100], to[1100],
    d[1100], n, m, fr[1100];
int cal(int i) {
  int j, res = 0;
  for (j = len[i]; j > m; j--) res += c[b[i][j] - '0'];
  return res;
}
void add(int i) {
  if (len[i] <= m)
    len[i] = m + 1, b[i][m + 1] = '1';
  else {
    int pos = m + 1;
    b[i][pos]++;
    while (b[i][pos] > '9') {
      if (len[i] == pos)
        len[i]++, b[i][len[i]] = '1';
      else
        b[i][pos + 1]++;
      b[i][pos] = '0';
      pos++;
    }
  }
}
int main() {
  int i, j, k, nw, pre, l, r, co, p, q, ans;
  scanf("%s", a + 1);
  m = strlen(a + 1);
  reverse(a + 1, a + m + 1);
  scanf("%d", &n);
  for (i = 1; i <= n; i++) {
    scanf("%s", b[i] + 1);
    len[i] = strlen(b[i] + 1);
    reverse(b[i] + 1, b[i] + len[i] + 1);
    for (j = len[i] + 1; j <= m; j++) b[i][++len[i]] = '0';
  }
  for (i = 0; i <= 9; i++) scanf("%d", &c[i]);
  for (i = 1; i <= n; i++) ran[0][i] = i;
  nw = 0;
  pre = 1;
  for (i = 0; i <= m; i++)
    for (j = 1; j <= n + 1; j++) f[i][j] = -inf;
  f[0][n + 1] = 0;
  for (i = 1; i <= m; i++) {
    nw ^= 1;
    pre ^= 1;
    for (j = 0; j <= 9; j++) sum[j] = 0;
    for (j = 1; j <= n; j++) sum[b[j][i] - '0']++;
    for (j = 1; j <= 9; j++) sum[j] += sum[j - 1];
    for (j = n; j; j--) {
      ran[nw][sum[b[ran[pre][j]][i] - '0']] = ran[pre][j];
      to[sum[b[ran[pre][j]][i] - '0']] = j;
      fr[j] = sum[b[ran[pre][j]][i] - '0']--;
    }
    l = 0;
    r = 9;
    if (i == m) l = 1;
    if (a[i] != '?') l = a[i] - '0', r = a[i] - '0';
    for (j = l; j <= r; j++) {
      co = 0;
      for (k = 1; k <= n; k++) co += c[(b[k][i] - '0' + j) % 10];
      for (k = n; ((k) && (b[ran[nw][k]][i] - '0' + j > 9)); k--)
        ;
      for (p = n + 1;
           (((!k) || (p > to[k]) || (b[ran[nw][k]][i] - '0' + j < 9)) && (p));
           p--) {
        if (p != n + 1) {
          co -= c[(b[ran[nw][fr[p]]][i] - '0' + j) % 10];
          co += c[(b[ran[nw][fr[p]]][i] - '0' + j + 1) % 10];
        }
        f[i][k + 1] = max(f[i][k + 1], f[i - 1][p] + co);
      }
      for (; ((k) && (b[ran[nw][k]][i] - '0' + j == 9)); k--) {
        if ((k == 1) || (b[ran[nw][k - 1]][i] - '0' + j != 9))
          q = 1;
        else
          q = to[k - 1] + 1;
        for (; p >= q; p--) {
          co -= c[(b[ran[nw][fr[p]]][i] - '0' + j) % 10];
          co += c[(b[ran[nw][fr[p]]][i] - '0' + j + 1) % 10];
          f[i][k] = max(f[i][k], f[i - 1][p] + co);
        }
      }
    }
  }
  for (i = 1; i <= n; i++) d[i] = ran[nw][i];
  co = 0;
  ans = 0;
  for (i = 1; i <= n; i++) co += cal(i);
  ans = max(ans, co + f[m][n + 1]);
  for (i = n; i; i--) {
    co -= cal(d[i]);
    add(d[i]);
    co += cal(d[i]);
    ans = max(ans, co + f[m][i]);
  }
  printf("%d\n", ans);
  return 0;
}