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;
int const M = 1000 + 10, M2 = 1e3 + 10, mod = 1e9 + 7, inf = 1e9 + 10;
string y[M], x;
int dp[M][M], val[M], ran[M][M], now, st[M], f[M][10], ind[M], n, bad,
    ind2[M][M], siz[M], carry[M];
int last[10], cnt[10];
bool cmp(int a, int b) {
  if (y[a][now] != y[b][now]) return (y[a][now] - '0') < (y[b][now] - '0');
  if (now == 0) return a < b;
  return ran[now - 1][a] < ran[now - 1][b];
}
void ch(int i, int j, int k) {
  int ans = 0, sv = n + 1;
  for (int t = 0; t <= 9; t++) {
    if (st[t] == -1) continue;
    int l = st[t], r = last[t] + 1;
    if (f[j][t] != -1 && f[j][t] <= last[t] && f[j][t] >= st[t]) r = f[j][t];
    ans += (r - l) * val[((k + t) % 10)];
    ans += (last[t] + 1 - r) * val[(k + t + 1) % 10];
    if (k + t == 9) {
      if (r != last[t] + 1) sv = r;
    }
  }
  for (int l = 9 - k + 1; l <= 9; l++) {
    if ((sv == n + 1 || sv == -1) && st[l] != -1) sv = st[l];
  }
  ans += dp[i + 1][sv];
  dp[i][j] = max(ans, dp[i][j]);
}
void fdp(int i, int j) {
  if (j == 1) {
    for (int k = 0; k <= 9; k++) last[k] = -1, cnt[k] = 0, st[k] = -1;
    for (int k = 1; k <= n; k++) {
      last[y[k][i] - '0'] = max(ran[i][k], last[y[k][i] - '0']);
      if (st[y[k][i] - '0'] == -1 || ran[i][k] < st[y[k][i] - '0'])
        st[y[k][i] - '0'] = ran[i][k];
    }
    for (int k = 0; k <= 9; k++) {
      for (int l = 1; l <= n + 1; l++) {
        f[l][k] = -1;
      }
      int pnt = st[k];
      for (int l = 1; l <= n + 1; l++) {
        if (last[k] == -1 || i == 0) {
          continue;
        }
        while (ran[i - 1][ind2[i][pnt]] < l && pnt != last[k] + 1) pnt++;
        f[l][k] = pnt;
      }
    }
  }
  if (x[i] == '?') {
    int s = 0;
    if (i == bad) s = 1;
    for (int k = s; k <= 9; k++) ch(i, j, k);
  } else
    ch(i, j, x[i] - '0');
}
int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> x;
  reverse(x.begin(), x.end());
  bad = (int)x.size() - 1;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> y[i];
    siz[i] = y[i].size();
    reverse(y[i].begin(), y[i].end());
    while (y[i].size() < 1001) y[i] += '0';
  }
  for (int i = 0; i <= 9; i++) cin >> val[i];
  for (int i = 0; i <= 1000; i++) {
    now = i;
    for (int j = 1; j <= n; j++) {
      ind[j] = j;
    }
    sort(ind + 1, ind + n + 1, cmp);
    for (int j = 1; j <= n; j++) {
      ran[i][ind[j]] = j;
      ind2[i][j] = ind[j];
    }
  }
  for (int j = n + 1; j >= 1; j--) {
    int t = x.size();
    for (int k = j; k <= n; k++) {
      int id = ind2[t - 1][k];
      dp[t][j] += val[(y[id][t] - '0' + 1) % 10];
      if (y[id][t] - '0' == 9) carry[id] = 1;
    }
    for (int i = 1; i <= j - 1; i++) {
      int id = ind2[t - 1][i];
      if (siz[id] > x.size()) dp[t][j] += val[y[id][t] - '0'];
    }
    for (int k = 1; k <= n; k++) {
      for (int l = t + 1; l < siz[k]; l++) {
        dp[t][j] += val[((y[k][l] - '0' + carry[k]) % 10)];
        if (y[k][l] - '0' == 9 && carry[k])
          carry[k] = 1;
        else
          carry[k] = 0;
      }
      if (carry[k]) dp[t][j] += val[1];
      carry[k] = 0;
    }
  }
  for (int i = (int)x.size() - 1; i >= 0; i--) {
    for (int j = 1; j <= n + 1; j++) {
      fdp(i, j);
    }
  }
  cout << dp[0][n + 1];
}