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 = 5000;
int n, c[20];
char a[N], str[N];
string b[N];
int add(int x, int y) {
  if (x == -1) {
    return y == 0 ? -1 : y;
  } else {
    return x + y;
  }
}
string read() {
  scanf("%s", str);
  return str;
}
int main() {
  scanf("%s %d", a, &n);
  int l = strlen(a);
  int m = l;
  reverse(a, a + l);
  for (int i = 0; i < n; i++) {
    b[i] = read();
    reverse(b[i].begin(), b[i].end());
    while (b[i].size() < l) {
      b[i] += '0';
    }
    m = max(m, (int)b[i].size());
  }
  for (int i = 0; i < 10; i++) {
    scanf("%d", &c[i]);
  }
  vector<int> dp(n + 1, -1);
  vector<int> order(n);
  for (int i = 0; i < n; i++) {
    order[i] = i;
  }
  dp[0] = 0;
  for (int pos = 0; pos <= m; pos++) {
    vector<int> dp2(n + 1, -1);
    vector<int> col(n);
    int lo = -1;
    int hi = -1;
    if (pos >= l) {
      lo = 0;
      hi = 0;
    } else if (a[pos] == '?') {
      lo = (pos == l - 1) ? 1 : 0;
      hi = 9;
    } else {
      lo = a[pos] - '0';
      hi = a[pos] - '0';
    }
    for (int i = 0; i < n; i++) {
      if (pos >= b[i].size()) {
        col[i] = -1;
      } else {
        col[i] = b[i][pos] - '0';
      }
    }
    for (int d = lo; d <= hi; d++) {
      int sum = 0;
      int out = 0;
      for (int i = 0; i < n; i++) {
        int s = add(col[i], d);
        if (s != -1) {
          sum += c[s % 10];
          out += s / 10;
        }
      }
      for (int carry = 0; carry <= n; carry++) {
        if (dp[carry] >= 0) {
          dp2[out] = max(dp2[out], dp[carry] + sum);
        }
        if (carry < n) {
          int idx = order[carry];
          int s = add(col[idx], d);
          if (s != -1) {
            sum -= c[s % 10];
            out -= s / 10;
          }
          s = add(col[idx], d + 1);
          sum += c[s % 10];
          out += s / 10;
        }
      }
    }
    stable_sort(order.begin(), order.end(), [&col](int x, int y) {
      return max(0, col[x]) > max(0, col[y]);
    });
    dp = move(dp2);
  }
  printf("%d\n", dp[0]);
  return 0;
}