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 cost[12], N, M, INF = 1e9 + 10, oldDp[1009], dp[1009], cif[1009][1009],
                    cuT[1009], faraT[1009], l[1009], oldPos[1009], ap[1009][11];
char sir[1009], curr[1009];
pair<int, int> ord[1009], old[1009];
bool mtch(int i, int j) {
  if (sir[i] == '?') {
    if (j == 0 && i == M) return 0;
    return 1;
  }
  return (sir[i] - '0' == j);
}
int main() {
  gets(sir + 1), M = strlen(sir + 1);
  reverse(sir + 1, sir + M + 1);
  scanf("%d\n", &N);
  for (int i = 1; i <= N; i++) {
    gets(curr + 1), l[i] = strlen(curr + 1);
    for (int j = 1; j <= l[i]; j++) cif[i][j] = curr[l[i] + 1 - j] - '0';
  }
  for (int i = 0; i < 10; i++) scanf("%d", &cost[i]);
  for (int i = 1; i <= N; i++) {
    faraT[i] = 0;
    for (int j = M + 1; j <= l[i]; j++) faraT[i] += cost[cif[i][j]];
    cuT[i] = 0;
    int t = 1;
    if (l[i] >= M + 1) {
      for (int j = M + 1; j <= l[i]; j++) {
        int curr = (cif[i][j] + t) % 10;
        cuT[i] += cost[curr];
        t = (cif[i][j] + t) / 10;
      }
    }
    if (t) cuT[i] += cost[1];
  }
  for (int j = 1; j <= N; j++) ord[j].first = 0, ord[j].second = j;
  for (int i = 0; i <= N; i++) dp[i] = -INF;
  dp[N] = 0;
  for (int i = 1; i <= M; i++) {
    for (int j = 1; j <= N; j++) old[j] = ord[j];
    int curr_nr = 0;
    for (int j = 0; j < 10; j++)
      for (int k = 1; k <= N; k++)
        if (cif[old[k].second][i] == j)
          ord[++curr_nr] = make_pair(old[k].first + j, old[k].second);
    memcpy(oldDp, dp, sizeof(dp));
    for (int j = 0; j <= N; j++) dp[j] = -INF;
    for (int j = 1; j <= N; j++) {
      oldPos[old[j].second] = j;
      for (int k = 0; k < 10; k++) ap[j][k] = ap[j - 1][k];
      ap[j][cif[old[j].second][i]]++;
    }
    for (int state = 0; state <= N; state++)
      if (oldDp[state] >= 0)
        for (int Cif = 0; Cif < 10; Cif++)
          if (mtch(i, Cif)) {
            int p = 1, u = N, mij, ras = 0;
            while (p <= u) {
              mij = (p + u) >> 1;
              int ind = ord[mij].second;
              if ((oldPos[ind] > state) + Cif + cif[ind][i] < 10)
                ras = mij, p = mij + 1;
              else
                u = mij - 1;
            }
            int curr = oldDp[state];
            for (int k = 0; k < 10; k++) {
              int fara = ap[state][k], cu = ap[N][k] - ap[state][k];
              curr += cost[(k + 1 + Cif) % 10] * cu;
              curr += cost[(k + Cif) % 10] * fara;
              if (dp[ras] < curr) dp[ras] = curr;
            }
          }
  }
  int curr = 0;
  for (int i = 1; i <= N; i++) curr += faraT[i];
  int ans = 0;
  for (int i = N; i >= 0; i--) {
    if (curr + dp[i] > ans) ans = curr + dp[i];
    curr -= faraT[ord[i].second];
    curr += cuT[ord[i].second];
  }
  printf("%d\n", ans);
  return 0;
}