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;
struct node {
  int y, nex;
} e[1100];
int n, m, a[1100], b[1100][1100], c[10], f[1100][1100], sa1[1100], sa2[1100],
    pos[10], fir[10];
int len, rk[1100], w[1100], g[10], ap, mx[1100];
char s[1100][1100];
void init() {
  scanf("%s", s[0] + 1);
  m = strlen(s[0] + 1);
  int maxd = m;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%s", s[i] + 1);
    int t = strlen(s[i] + 1);
    m = max(m, t);
  }
  m++;
  int t = strlen(s[0] + 1);
  for (int i = 1; i <= t; i++)
    if (s[0][i] != '?')
      a[i + m - t] = s[0][i] - '0';
    else
      a[i + m - t] = -1;
  if (s[0][1] == '?')
    ap = 1 + m - t;
  else
    ap = -1;
  for (int i = 1; i <= n; i++) {
    t = strlen(s[i] + 1);
    mx[i] = max(maxd, t);
    for (int j = 1; j <= t; j++) b[i][j + m - t] = s[i][j] - '0';
  }
  for (int i = 1; i <= m / 2; i++) swap(a[i], a[m - i + 1]);
  if (ap != -1) ap = m - ap + 1;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m / 2; j++) swap(b[i][j], b[i][m - j + 1]);
  for (int i = 0; i <= 9; i++) scanf("%d", &c[i]);
}
void upd(int &x, int y) {
  if (y > x) x = y;
}
void ins(int x, int y) {
  e[++len].y = y;
  e[len].nex = fir[x];
  fir[x] = len;
}
void make_sa(int k) {
  for (int i = 0; i <= 9; i++) fir[i] = 0;
  len = 0;
  for (int i = 1; i <= n; i++) ins(b[sa1[i]][k], sa1[i]);
  int num = n;
  for (int x = 0; x <= 9; x++)
    for (int k = fir[x]; k; k = e[k].nex) {
      int y = e[k].y;
      sa2[num--] = y;
    }
  for (int i = 1; i <= n; i++) rk[sa2[i]] = i;
}
void solve() {
  for (int i = 0; i <= m; i++)
    for (int j = 0; j <= n; j++) f[i][j] = -1;
  for (int i = 1; i <= n; i++) sa1[i] = i;
  f[0][0] = 0;
  for (int i = 0; i < m; i++) {
    make_sa(i + 1);
    for (int j = 1; j <= n; j++) w[j] = b[sa2[j]][i + 1];
    for (int j = 0; j <= 9; j++) {
      g[j] = 0;
      for (int k = 1; k <= n; k++)
        if (mx[sa2[k]] >= i + 1) g[j] += c[(w[k] + j) % 10];
    }
    for (int j = 0; j <= 9; j++) pos[j] = 0;
    int st = 0, ed = 9;
    if (a[i + 1] != -1)
      st = ed = a[i + 1];
    else if (i + 1 == ap)
      st = 1;
    int j1 = 0;
    for (int j = 0; j <= n; j++) {
      if (f[i][j] == -1) continue;
      while (j1 < j) {
        j1++;
        int t = w[rk[sa1[j1]]];
        w[rk[sa1[j1]]]++;
        for (int k = st; k <= ed; k++)
          if (mx[sa1[j1]] >= i + 1) g[k] -= c[(t + k) % 10];
        t++;
        for (int k = st; k <= ed; k++) g[k] += c[(t + k) % 10];
      }
      for (int k = st; k <= ed; k++) {
        while (pos[k] < n && w[pos[k] + 1] + k >= 10) pos[k]++;
        upd(f[i + 1][pos[k]], f[i][j] + g[k]);
      }
    }
    for (int i = 1; i <= n; i++) sa1[i] = sa2[i];
  }
  printf("%d\n", f[m][0]);
}
int main() {
  init();
  solve();
  return 0;
}