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 = 1015;
int b[N][N];
char c[N];
int n;
char a[N];
int w[15];
int len = 0;
int sa[N], rk[N];
int tsa[N], trk[N];
int sum[15];
int f[2][N];
int cost[N][12][2];
int dj[11][N];
int lp[N];
int dp() {
  for (int i = (int)1; i <= (int)n; i++) sa[i] = i;
  for (int i = (int)1; i <= (int)n; i++) rk[sa[i]] = i;
  for (int i = (int)0; i <= (int)n; i++) f[0][i] = f[1][i] = -1000000000;
  int now = 0;
  f[0][0] = 0;
  for (int bit = (int)1; bit <= (int)1005; bit++) {
    for (int i = (int)0; i <= (int)n; i++) f[now ^ 1][i] = -1000000000;
    memset(cost, 0, sizeof cost);
    for (int i = (int)1; i <= (int)n; i++) {
      for (int j = (int)0; j <= (int)9; j++)
        for (int k = (int)0; k <= (int)1; k++) {
          if ((bit <= max(lp[sa[i]], len)) ||
              (bit == max(lp[sa[i]], len) + 1 &&
               ((b[sa[i]][bit] + j + k) % 10 != 0)))
            cost[i][j][k] = cost[i - 1][j][k] + w[(b[sa[i]][bit] + j + k) % 10];
          else
            cost[i][j][k] = cost[i - 1][j][k];
        }
    }
    memset(sum, 0, sizeof sum);
    for (int i = (int)1; i <= (int)n; i++) sum[b[i][bit]]++;
    for (int i = (int)8; i >= (int)0; i--) sum[i] += sum[i + 1];
    for (int i = (int)n; i >= (int)1; i--) tsa[sum[b[sa[i]][bit]]--] = sa[i];
    for (int i = (int)1; i <= (int)n; i++) trk[tsa[i]] = i;
    for (int i = (int)0; i <= (int)9; i++) {
      int dc = 0;
      while (dc < n && b[tsa[dc + 1]][bit] > i) ++dc;
      for (int j = (int)0; j <= (int)n; j++) {
        while (dc < n && rk[tsa[dc + 1]] <= j && b[tsa[dc + 1]][bit] == i) ++dc;
        dj[i][j] = dc;
      }
    }
    for (int i = (int)0; i <= (int)n; i++)
      if (f[now][i] >= 0) {
        for (int v = (int)0; v <= (int)9; v++) {
          if (v > 0 && bit > len) continue;
          if (bit <= len && a[bit] != '?' && (a[bit] - '0' != v)) continue;
          if (bit == len && v == 0) continue;
          f[now ^ 1][dj[9 - v][i]] =
              max(f[now ^ 1][dj[9 - v][i]],
                  f[now][i] + cost[i][v][1] + cost[n][v][0] - cost[i][v][0]);
        }
      }
    now ^= 1;
    for (int i = (int)1; i <= (int)n; i++) sa[i] = tsa[i], rk[i] = trk[i];
  }
  return f[now][0];
}
int main() {
  scanf("%s", a + 1);
  n = strlen(a + 1);
  len = n;
  for (int i = (int)1; i <= (int)n; i++)
    if (i < n + 1 - i) swap(a[i], a[n + 1 - i]);
  scanf("%d", &n);
  for (int i = (int)1; i <= (int)n; i++) {
    scanf("%s", c + 1);
    int m = strlen(c + 1);
    lp[i] = m;
    for (int j = (int)1; j <= (int)m; j++) b[i][j] = c[m + 1 - j] - '0';
  }
  for (int i = (int)0; i <= (int)9; i++) scanf("%d", &w[i]);
  printf("%d\n", dp());
  return 0;
}