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;
char tmp[1010];
int B[1010][1010], A[1010];
const int L = 1001;
int dp[1010][1010], c[1010], len[1010];
int change(int i, int j, int add) {
  int a0 = (B[i][j] + add) % 10, a1 = (B[i][j] + add + 1) % 10;
  if (j < len[i]) {
    return -c[a0] + c[a1];
  } else {
    return c[a1];
  }
}
bool cmp(const int &i, const int &j) { return B[i][0] > B[j][0]; }
int main() {
  int n, m;
  scanf("%s", tmp);
  m = strlen(tmp);
  for (int i = 0; i < L; i++) {
    if (i < m) {
      if (tmp[i] == '?')
        A[i] = -1;
      else
        A[i] = tmp[i] - '0';
    } else {
      A[i] = 0;
    }
  }
  reverse(A, A + m);
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%s", tmp);
    reverse(tmp, tmp + strlen(tmp));
    len[i] = strlen(tmp);
    for (int j = 0; j < L; j++) {
      if (j < len[i]) {
        B[i][j] = tmp[j] - '0';
      } else {
        B[i][j] = 0;
      }
    }
    len[i] = max(len[i], m);
  }
  for (int i = 0; i < 10; i++) scanf("%d", &c[i]);
  vector<int> cur(n), nxt(n), inxt(n);
  memset(dp, -1, sizeof(dp));
  for (int i = 0; i < n; i++) {
    nxt[i] = i;
  }
  for (int i = -1; i < L - 1; i++) {
    cur = nxt;
    vector<int> v[10];
    for (int j = 0; j < n; j++) {
      v[B[cur[j]][i + 1]].push_back(cur[j]);
    }
    int p = 0;
    for (int j = 9; j >= 0; j--) {
      for (auto u : v[j]) {
        nxt[p++] = u;
        inxt[u] = p - 1;
      }
    }
    for (int num = i + 1 == m - 1 ? 1 : 0; num < 10; num++) {
      if (A[i + 1] != -1 && num != A[i + 1]) continue;
      p = 0;
      int tmp = 0;
      for (int j = 0; j < n; j++) {
        if (B[j][i + 1] + num >= 10) p++;
        if (i + 1 < len[j]) tmp += c[(B[j][i + 1] + num) % 10];
      }
      for (int j = 0; j < n + 1; j++) {
        int d;
        if (j > 0) tmp += change(cur[j - 1], i + 1, num);
        if (j > 0 && B[cur[j - 1]][i + 1] + num + 1 == 10) p++;
        if (i == -1) {
          if (j != 0) continue;
          d = 0;
        } else {
          if (dp[i][j] == -1) continue;
          d = dp[i][j];
        }
        dp[i + 1][p] = max(dp[i + 1][p], d + tmp);
      }
    }
  }
  printf("%d\n", dp[L - 1][0]);
  return 0;
}