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>
int L, n, l[1010], p[1010], c[1010], tot;
int v[1010][11][1010], nine[1010][1010], r[1010][1010], ur[1010][1010],
    f[1010][1010];
char A[1010], B[1010][1010], D[1010], K[1010];
bool o[1010][11][1010], vis[1010];
int cmpd;
namespace bf {
int ans;
void dfs(int u) {
  if (u > tot) {
    int tmp = 0;
    for (int i = 1; i <= n; ++i) {
      for (int j = 1; j <= l[i]; ++j) D[j] = B[i][j];
      D[l[i] + 1] = 0;
      for (int j = 1; j <= l[i]; ++j) {
        if (A[j] == '?') D[j] += K[j];
        if (D[j] > 9) {
          D[j] -= 10;
          ++D[j + 1];
        }
      }
      int t = l[i];
      if (D[t + 1]) ++t;
      for (int j = 1; j <= t; ++j) tmp += c[D[j]];
    }
    ans = (ans > tmp ? ans : tmp);
    return;
  }
  for (int i = p[u] == L ? 1 : 0; i < 10; ++i) {
    K[p[u]] = i;
    dfs(u + 1);
  }
}
void main() {
  bf::dfs(1);
  printf("%d\n", ans);
}
}  // namespace bf
bool cmp(int i, int j) {
  if (nine[i][cmpd] != nine[j][cmpd]) return nine[i][cmpd] > nine[j][cmpd];
  if (B[i][p[cmpd]] != B[j][p[cmpd]]) return B[i][p[cmpd]] > B[j][p[cmpd]];
  return ur[cmpd - 1][i] < ur[cmpd - 1][j];
}
int main() {
  scanf("%s", A + 1);
  L = strlen(A + 1);
  std::reverse(A + 1, A + L + 1);
  for (int i = 1; i <= L; ++i)
    if (A[i] != '?')
      A[i] -= '0';
    else
      p[++tot] = i;
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    scanf("%s", B[i] + 1);
    l[i] = strlen(B[i] + 1);
    std::reverse(B[i] + 1, B[i] + l[i] + 1);
    for (int j = 1; j <= l[i]; ++j) B[i][j] -= '0';
  }
  for (int i = 0; i < 10; ++i) scanf("%d", &c[i]);
  for (int i = 1; i <= n; ++i) {
    l[i] = (l[i] > L ? l[i] : L);
    for (int j = 1; j <= l[i]; ++j) {
      if (A[j] != '?') B[i][j] += A[j];
      if (B[i][j] > 9) {
        B[i][j] -= 10;
        ++B[i][j + 1];
      }
    }
    if (B[i][l[i] + 1]) ++l[i];
  }
  memset(f, 128, sizeof(f));
  f[0][0] = 0;
  for (int i = 1; i <= n; ++i) {
    p[tot + 1] = l[i] + 1;
    for (int u = 0; u <= 10; ++u) {
      for (int j = 1; j <= tot; ++j) {
        for (int k = p[j]; k < p[j + 1]; ++k) D[k] = B[i][k];
        D[p[j]] += u;
        D[p[j + 1]] = 0;
        int s = 0;
        for (int k = p[j]; k < p[j + 1]; ++k) {
          if (D[k] > 9) {
            D[k] -= 10;
            ++D[k + 1];
          }
          s += c[D[k]];
        }
        v[i][u][j] = s;
        o[i][u][j] = D[p[j + 1]];
      }
    }
    for (int j = 1; j < p[1]; ++j) f[0][0] += c[B[i][j]];
    for (int j = 1; j <= tot; ++j)
      for (int k = p[j] + 1; B[i][k] == 9 && k < p[j + 1]; ++k) ++nine[i][j];
  }
  for (int i = 1; i <= tot; ++i) {
    for (int j = 1; j <= n; ++j) r[i][j] = j;
    cmpd = i;
    std::sort(r[i] + 1, r[i] + 1 + n, cmp);
    for (int j = 1; j <= n; ++j) ur[i][r[i][j]] = j;
  }
  for (int i = 1; i <= n; ++i) ur[0][i] = ur[1][i];
  for (int i = 0; i < tot - 1; ++i) {
    for (int k = 0; k < 10; ++k) {
      for (int j = 1; j <= n; ++j) vis[j] = 0;
      int tmp = 0;
      for (int j = 1; j <= n; ++j) {
        tmp += v[j][k][i + 1];
        vis[j] = o[j][k][i + 1];
      }
      int u = 0;
      while (vis[r[i + 1][u + 1]]) ++u;
      if (f[i][0] >= 0)
        f[i + 1][u] =
            (f[i + 1][u] > f[i][0] + tmp ? f[i + 1][u] : f[i][0] + tmp);
      for (int j = 1; j <= n; ++j) {
        int to = r[i][j];
        tmp -= v[to][k][i + 1];
        tmp += v[to][k + 1][i + 1];
        vis[to] = o[to][k + 1][i + 1];
        while (vis[r[i + 1][u + 1]]) ++u;
        if (f[i][j] >= 0)
          f[i + 1][u] =
              (f[i + 1][u] > f[i][j] + tmp ? f[i + 1][u] : f[i][j] + tmp);
      }
    }
  }
  int ans = 0;
  if (tot) {
    for (int k = p[tot] == L ? 1 : 0; k < 10; ++k) {
      for (int j = 1; j <= n; ++j) vis[j] = 0;
      int tmp = 0, u = 0;
      for (int j = 1; j <= n; ++j) {
        tmp += v[j][k][tot];
        vis[j] = o[j][k][tot];
      }
      if (f[tot - 1][0] >= 0) {
        for (int i = 1; i <= n; ++i)
          if (vis[i]) ++u;
        ans = (ans > f[tot - 1][0] + tmp + u * c[1]
                   ? ans
                   : f[tot - 1][0] + tmp + u * c[1]);
      }
      for (int j = 1; j <= n; ++j) {
        int to = r[tot - 1][j];
        tmp -= v[to][k][tot];
        tmp += v[to][k + 1][tot];
        vis[to] = o[to][k + 1][tot];
        u = 0;
        for (int i = 1; i <= n; ++i)
          if (vis[i]) ++u;
        if (f[tot - 1][j] >= 0) {
          ans = (ans > f[tot - 1][j] + tmp + u * c[1]
                     ? ans
                     : f[tot - 1][j] + tmp + u * c[1]);
        }
      }
    }
  } else
    ans = f[0][0];
  printf("%d\n", ans);
  return 0;
}