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 n;
int N = 1015;
vector<int> A, B[1050];
int dp[1050][1050];
int cost[10];
char inp[1150];
int SA[1050];
int now;
int RA[1050];
int maxlen[1050];
vector<int> getInput2() {
  vector<int> res;
  for (int i = 0; i < 999; i++) res.push_back(0);
  res.push_back(1);
  return res;
}
vector<int> getInput() {
  scanf("%s", &inp);
  int len = strlen(inp);
  vector<int> res;
  for (int i = len - 1; i >= 0; i--) {
    if (inp[i] == '?') {
      res.push_back(-1);
    } else {
      res.push_back(inp[i] - '0');
    }
  }
  return res;
}
bool cmp(int a, int b) { return RA[a] < RA[b]; }
void countingsort() {
  vector<int> cnt[15];
  for (int i = 1; i <= n; i++) {
    int x = (now < B[i].size()) ? B[i][now] : 0;
    cnt[x].push_back(i);
  }
  for (int i = 0; i < 10; i++) sort(cnt[i].begin(), cnt[i].end(), cmp);
  int G = 0;
  for (int i = 9; i >= 0; i--) {
    for (int j = 0; j < cnt[i].size(); j++) SA[++G] = cnt[i][j];
  }
}
int solve() {
  for (int i = 1; i <= n; i++) SA[i] = i;
  int len = A.size();
  for (int i = 1; i <= n; i++) {
    maxlen[i] = max(len, (int)B[i].size());
  }
  for (int i = 1; i <= N; i++) {
    int id = i - 1;
    int lo, hi;
    if (id >= A.size()) {
      lo = hi = 0;
    } else if (id == A.size() - 1 && A[id] == -1) {
      lo = 1;
      hi = 9;
    } else if (A[id] == -1) {
      lo = 0;
      hi = 9;
    } else {
      lo = A[id];
      hi = A[id];
    }
    for (int ex = lo; ex <= hi; ex++) {
      int prefix = 0;
      int sum = 0;
      for (int j = 1; j <= n; j++) {
        int x = (id >= B[j].size()) ? 0 : B[j][id];
        sum += (((x + ex) % 10) != 0) ? cost[(x + ex) % 10]
                                      : ((id >= maxlen[j]) ? 0 : cost[0]);
        if (x + ex >= 10) prefix++;
      }
      for (int j = 0; j <= n; j++) {
        if (j > 0) {
          int k = SA[j];
          int x = (id >= B[k].size()) ? 0 : B[k][id];
          sum -= (((x + ex) % 10) != 0) ? cost[(x + ex) % 10]
                                        : ((id >= maxlen[k]) ? 0 : cost[0]);
          sum += (((x + ex + 1) % 10) != 0) ? cost[(x + ex + 1) % 10]
                                            : ((id >= maxlen[k]) ? 0 : cost[0]);
          if (x + ex + 1 == 10) prefix++;
        }
        if (sum + dp[i - 1][j] >= 0)
          dp[i][prefix] = max(dp[i][prefix], sum + dp[i - 1][j]);
      }
    }
    for (int j = 1; j <= n; j++) RA[SA[j]] = j;
    now = id;
    countingsort();
  }
  return dp[N][0];
}
int main() {
  A = getInput();
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    B[i] = getInput();
  }
  for (int i = 0; i < 10; i++) scanf("%d", &cost[i]);
  memset(dp, -64, sizeof(dp));
  dp[0][0] = 0;
  printf("%d\n", solve());
  return 0;
}