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 = 2e3 + 3;
int a[N], m[N], b[N][N], v[10], id[N * 10], nxt[N * 10], sr[N * 10][10],
    sl[N * 10][10], dp[N][N];
vector<int> vec[N], vec2[N * 10];
int main() {
  static char s[N], t[N];
  int n;
  scanf("%s%d", s, &n);
  int l = strlen(s);
  for (int j = 1; j <= l; ++j) a[j] = s[l - j] == '?' ? -1 : s[l - j] - '0';
  while (l && a[l] == 0) --l;
  int lm = l;
  for (int i = 1; i <= n; ++i) {
    scanf("%s", t);
    m[i] = strlen(t);
    for (int j = 1; j <= m[i]; ++j) b[i][j] = t[m[i] - j] - '0';
    while (m[i] && b[i][m[i]] == 0) --m[i];
    lm = max(lm, m[i]);
  }
  for (int i = 0; i <= 9; ++i) scanf("%d", &v[i]);
  for (int i = 1; i <= n; ++i) vec[1].push_back(i);
  int cnt = 2;
  for (int i = 0; i <= lm + 1; ++i)
    for (int j = 1; j <= n + 1; ++j) dp[i][j] = -1e9;
  dp[0][1] = -1e9, dp[0][2] = 0;
  for (int i = 0; i <= lm; ++i) {
    for (int t = 0; t <= 9; ++t) sr[cnt + 1][t] = 0;
    for (int j = cnt; j >= 1; --j) {
      for (int t = 0; t <= 9; ++t) sr[j][t] = sr[j + 1][t];
      for (int k = 0, kend = vec[j].size(); k < kend; ++k) {
        int p = vec[j][k];
        for (int t = 0; t <= 9; ++t) {
          if (i + 1 > max(l, m[p]) + 1) continue;
          int u = ((b[p][i + 1] + t + 1) >= 10 ? (b[p][i + 1] + t + 1) - 10
                                               : (b[p][i + 1] + t + 1));
          sr[j][t] += i + 1 <= max(l, m[p]) || u ? v[u] : 0;
        }
      }
    }
    for (int j = 1; j <= cnt; ++j) {
      for (int t = 0; t <= 9; ++t) sl[j][t] = sl[j - 1][t];
      for (int k = 0, kend = vec[j].size(); k < kend; ++k) {
        int p = vec[j][k];
        for (int t = 0; t <= 9; ++t) {
          if (i + 1 > max(l, m[p]) + 1) continue;
          int u = ((b[p][i + 1] + t) >= 10 ? (b[p][i + 1] + t) - 10
                                           : (b[p][i + 1] + t));
          sl[j][t] += i + 1 <= max(l, m[p]) || u ? v[u] : 0;
        }
      }
    }
    for (int j = 1; j <= cnt * 10; ++j) vec2[j].clear(), id[j] = 0;
    for (int j = 1; j <= cnt; ++j)
      for (int k = 0, kend = vec[j].size(); k < kend; ++k) {
        int p = vec[j][k];
        vec2[b[p][i + 1] * cnt + j].push_back(p);
      }
    int cnt2 = 0;
    for (int j = 1; j <= cnt * 10; ++j)
      if (vec2[j].size()) id[j] = ++cnt2;
    nxt[cnt * 10 + 1] = cnt2 + 1;
    for (int j = cnt * 10; j >= 1; --j)
      nxt[j] = vec2[j].size() ? id[j] : nxt[j + 1];
    for (int j = 1; j <= cnt; ++j) {
      int b = 0, e = 9;
      if (i + 1 > l)
        b = e = 0;
      else if (~a[i + 1])
        b = e = a[i + 1];
      else if (i + 1 == l)
        b = 1;
      for (int t = b; t <= e; ++t) {
        int &x = dp[i + 1][nxt[cnt * (9 - t) + j]];
        x = max(x, dp[i][j] + sr[j][t] + sl[j - 1][t]);
      }
    }
    for (int j = 1; j <= cnt2 + 1; ++j) vec[j].clear();
    for (int j = 1; j <= cnt * 10; ++j)
      if (id[j]) vec[id[j]] = vec2[j];
    cnt = cnt2 + 1;
  }
  int ans = 0;
  for (int j = 1; j <= cnt; ++j) ans = max(ans, dp[lm + 1][j]);
  printf("%d\n", ans);
  return 0;
}