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 = 1005;
char A[N + 7];
int n;
char B[N + 7];
int tab[N + 7][N + 7];
int wei[17];
int st[N + 7];
int dpo[N + 7];
int dp[N + 7];
int kolo[N + 7];
int kolor[N + 7];
int kol[N + 7];
int kolr[N + 7];
int inf = 1e9;
int zabr = 0;
int ile[17];
int ile2[17];
int pmx[17];
int gi;
bool cmp1(int a, int b) {
  if (tab[a][gi] == tab[b][gi]) return kolor[a] < kolor[b];
  return tab[a][gi] > tab[b][gi];
}
int main() {
  ios_base::sync_with_stdio(0);
  cin >> A;
  int ll = 0;
  for (; A[ll]; ++ll)
    ;
  for (int j = 0; j < ll; ++j) {
    if (A[j] == '?')
      tab[0][N - ll + 1 + j] = 10;
    else
      tab[0][N - ll + 1 + j] = A[j] - '0';
  }
  st[0] = N - ll + 1;
  if (A[0] == '?') zabr = N - ll + 1;
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> B;
    int len = 0;
    for (; B[len]; len++)
      ;
    for (int j = 0; j < len; ++j) tab[i][N - len + 1 + j] = B[j] - '0';
    st[i] = N - len + 1;
  }
  for (int i = 0; i < 10; ++i) cin >> wei[i];
  for (int i = 1; i <= n; ++i) {
    dpo[i] = -inf;
    kolo[i] = i;
    kolor[i] = i;
  }
  dpo[0] = 0;
  for (int i = N; i >= 1; --i) {
    gi = i;
    for (int j = 1; j <= n; ++j) kol[j] = j;
    sort(kol + 1, kol + n + 1, cmp1);
    for (int j = 0; j <= n; ++j) dp[j] = -inf;
    for (int j = 1; j <= n; ++j) kolr[kol[j]] = j;
    int le = 0;
    int pr = 9;
    if (tab[0][i] == 10) {
      if (i == zabr) le = 1;
    } else
      le = pr = tab[0][i];
    for (int j = 0; j <= 10; ++j) pmx[j] = ile[j] = ile2[j] = 0;
    for (int j = 1; j <= n; ++j) {
      ile[tab[j][i]]++;
      if (i >= st[j] || i >= st[0]) ile2[tab[j][i]]++;
      pmx[tab[j][i]] = max(pmx[tab[j][i]], kolr[j]);
    }
    for (int j = 0; j <= n; ++j) {
      if (j) {
        ile[tab[kolo[j]][i]]--;
        ile[tab[kolo[j]][i] + 1]++;
        pmx[tab[kolo[j]][i] + 1] = max(pmx[tab[kolo[j]][i] + 1], kolr[kolo[j]]);
        if (i >= st[kolo[j]] || i >= st[0]) {
          ile2[tab[kolo[j]][i]]--;
          ile2[tab[kolo[j]][i] + 1]++;
        }
      }
      for (int x = le; x <= pr; ++x) {
        int il = 0;
        int mxi = 0;
        for (int v = 10 - x; v <= 10; ++v) {
          il += ile[v];
          mxi = max(mxi, pmx[v]);
        }
        int ct = 0;
        for (int v = 0; v <= 10; ++v) {
          int dig = (v + x) % 10;
          if (dig != 0)
            ct += wei[dig] * ile[v];
          else
            ct += wei[dig] * ile2[v];
        }
        if (il == mxi) {
          dp[il] = max(dp[il], dpo[j] + ct);
        }
      }
    }
    for (int j = 0; j <= n; ++j) {
      dpo[j] = dp[j];
      if (j) {
        kolo[j] = kol[j];
        kolor[j] = kolr[j];
      }
    }
  }
  cout << dp[0] << endl;
  return 0;
}