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;
inline long long rd() {
  long long x = 0;
  int ch = getchar(), f = 1;
  while (!isdigit(ch) && (ch != '-') && (ch != EOF)) ch = getchar();
  if (ch == '-') {
    f = -1;
    ch = getchar();
  }
  while (isdigit(ch)) {
    x = (x << 1) + (x << 3) + ch - '0';
    ch = getchar();
  }
  return x * f;
}
inline void rt(long long x) {
  if (x < 0) putchar('-'), x = -x;
  if (x >= 10)
    rt(x / 10), putchar(x % 10 + '0');
  else
    putchar(x + '0');
}
const int maxn = 1005;
int n, f[2][maxn], id[maxn], tid[maxn], suf[maxn];
int cnt[11], val[11];
char B[maxn][maxn], A[maxn];
void radix_sort(int dig) {
  memset(cnt, 0, sizeof cnt);
  for (int i = int(1); i <= (int)(n); i++) cnt[B[i][dig] - '0']++;
  for (int i = int(8); i >= (int)(0); i--) cnt[i] += cnt[i + 1];
  for (int i = int(n); i >= (int)(1); i--)
    tid[cnt[B[id[i]][dig] - '0']--] = id[i];
}
int ml = 0, Al, l, jw[maxn];
void upd(int &x, int y) {
  if (y > x) x = y;
}
int calc(int i) {
  int lst = ml + 1, ret = 0;
  while (B[i][lst] == '0') lst--;
  for (int j = int(Al + 1); j <= (int)(lst); j++) ret += val[B[i][j] - '0'];
  return ret;
}
int main() {
  scanf("%s", A + 1);
  Al = strlen(A + 1);
  ml = max(ml, Al);
  reverse(A + 1, A + Al + 1);
  n = rd();
  for (int i = int(1); i <= (int)(n); i++) id[i] = i;
  for (int i = int(1); i <= (int)(n); i++) {
    scanf("%s", B[i] + 1);
    l = strlen(B[i] + 1);
    ml = max(ml, l);
    reverse(B[i] + 1, B[i] + l + 1);
  }
  for (int i = int(0); i <= (int)(9); i++) val[i] = rd();
  for (int j = int(1); j <= (int)(ml + 1); j++)
    if (!A[j]) A[j] = '0';
  for (int i = int(1); i <= (int)(n); i++)
    for (int j = int(1); j <= (int)(ml + 1); j++)
      if (!B[i][j]) B[i][j] = '0';
  int fr = 0, go = 1;
  memset(f[fr], -1, sizeof f[fr]);
  f[fr][0] = 0;
  for (int d = int(1); d <= (int)(Al); d++) {
    radix_sort(d);
    memset(f[go], -1, sizeof f[go]);
    int sum = 0, nxt = 0;
    for (int x = int(0); x <= (int)(9); x++)
      if (A[d] == '?' || A[d] == '0' + x) {
        if (d == Al && x == 0) continue;
        while (nxt < n && B[tid[nxt + 1]][d] - '0' + x >= 10) nxt++;
        memset(jw, 0, sizeof jw);
        sum = 0;
        for (int i = int(1); i <= (int)(n); i++)
          sum += val[(B[i][d] - '0' + x) % 10];
        for (int i = int(0); i <= (int)(n); i++) {
          if (i) {
            jw[id[i]] = 1;
            int yl = (B[id[i]][d] - '0' + x) % 10;
            sum -= val[yl], sum += val[(yl + 1) % 10];
            while (nxt < n &&
                   B[tid[nxt + 1]][d] - '0' + x + jw[tid[nxt + 1]] >= 10)
              nxt++;
          }
          if (~f[fr][i]) upd(f[go][nxt], f[fr][i] + sum);
        }
      }
    fr ^= 1, go ^= 1;
    memcpy(id, tid, sizeof tid);
  }
  int ans = 0, sum = 0;
  for (int i = int(0); i <= (int)(n); i++) sum += calc(i);
  for (int i = int(0); i <= (int)(n); i++) {
    if (i) {
      int _i = id[i];
      sum -= calc(_i);
      B[_i][Al + 1]++;
      for (int j = Al + 1; B[_i][j] - '0' >= 10; j++)
        B[_i][j] -= 10, B[_i][j + 1]++;
      sum += calc(_i);
    }
    if (~f[fr][i]) upd(ans, f[fr][i] + sum);
  }
  rt(ans), putchar('\n');
}