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;
template <typename T>
inline bool chkmin(T &a, const T &b) {
  return a > b ? a = b, 1 : 0;
}
template <typename T>
inline bool chkmax(T &a, const T &b) {
  return a < b ? a = b, 1 : 0;
}
const int oo = 0x3f3f3f3f;
const int maxn = 1010, maxm = 1010;
int an;
int n;
int a[maxm + 5];
int b[maxn + 5][maxm + 5];
int len[maxn + 5];
int dp[maxm + 5][maxn + 5];
int cost[10];
int pos[maxn + 5], ppos[maxn + 5];
int main() {
  static char s[maxm + 5];
  scanf("%s", s);
  an = strlen(s);
  reverse(s, s + an);
  for (int i = (0), i_end_ = (an); i < i_end_; ++i) {
    if (s[i] == '?')
      a[i] = -1;
    else
      a[i] = s[i] - '0';
  }
  scanf("%d", &n);
  for (int i = (0), i_end_ = (n); i < i_end_; ++i) {
    scanf("%s", s);
    len[i] = strlen(s);
    reverse(s, s + len[i]);
    for (int j = (0), j_end_ = (len[i]); j < j_end_; ++j) b[i][j] = s[j] - '0';
  }
  for (int i = (0), i_end_ = (10); i < i_end_; ++i) scanf("%d", cost + i);
  for (int i = (0), i_end_ = (n); i < i_end_; ++i) pos[i] = i;
  memset(dp, -oo, sizeof dp);
  dp[0][0] = 0;
  for (int i = (0), i_end_ = (maxm); i < i_end_; ++i) {
    static int dcnt[11];
    memset(dcnt, 0, sizeof dcnt);
    for (int j = (0), j_end_ = (n); j < j_end_; ++j)
      if (i < an || i < len[j]) ++dcnt[b[j][i]];
    for (int j = (0), j_end_ = (n + 1); j < j_end_; ++j) {
      if (dp[i][j] >= 0) {
        for (int k = (0), k_end_ = (10); k < k_end_; ++k) {
          if (i >= an && k) continue;
          if (i == an - 1 && !k) continue;
          if (i < an && ~a[i] && k != a[i]) continue;
          int delta = 0;
          int carry = 0;
          for (int l = (0), l_end_ = (11); l < l_end_; ++l)
            if (dcnt[l])
              delta += dcnt[l] * cost[(l + k) % 10],
                  carry += (l + k >= 10) * dcnt[l];
          chkmax(dp[i + 1][carry], dp[i][j] + delta);
        }
      }
      if (j < n) {
        if (i < an || i < len[pos[j]]) --dcnt[b[pos[j]][i]];
        ++dcnt[b[pos[j]][i] + 1];
      }
    }
    static int sum[10];
    memset(sum, 0, sizeof sum);
    for (int j = (0), j_end_ = (n); j < j_end_; ++j) ++sum[b[j][i]];
    for (int j = 9; j >= 1; --j) sum[j - 1] += sum[j];
    for (int j = n - 1; j >= 0; --j) ppos[--sum[b[pos[j]][i]]] = pos[j];
    memcpy(pos, ppos, sizeof pos);
  }
  printf("%d\n", dp[maxm][0]);
  return 0;
}