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 MAXN = 1e3 + 5;
const int MAXM = 1e3 + 5;
const int MAXL = MAXN * MAXM;
const int inf = 0x3f3f3f3f;
namespace SA {
int _rnk1[MAXL], _rnk2[MAXL];
int sa[MAXL], *rnk = _rnk1, *lst_rnk = _rnk2, sa_k;
inline bool sa_same(int x, int y) {
  return lst_rnk[x] == lst_rnk[y] && lst_rnk[x + sa_k] == lst_rnk[y + sa_k];
}
inline void build(char s[], int n) {
  static int t[MAXL];
  int m = 300;
  rnk[0] = lst_rnk[0] = rnk[n + 1] = lst_rnk[n + 1] = -1;
  memset(t, 0, (m + 1) << 2);
  for (int i = 1; i <= n; ++i) ++t[rnk[i] = s[i]];
  for (int i = 1; i <= m; ++i) t[i] += t[i - 1];
  for (int i = n; i >= 1; --i) sa[t[rnk[i]]--] = i;
  if (n == 1) {
    sa[1] = rnk[1] = 1;
    return;
  }
  if (m == n) ++m;
  for (int &k = sa_k = 1; m != n; k <<= 1) {
    static int id[MAXL];
    int cur = 0, *w = lst_rnk;
    for (int i = n - k + 1; i <= n; ++i) id[++cur] = i;
    for (int i = 1; i <= n; ++i)
      if (sa[i] > k) id[++cur] = sa[i] - k;
    memset(t, 0, (m + 1) << 2);
    for (int i = 1; i <= n; ++i) ++t[w[i] = rnk[id[i]]];
    for (int i = 1; i <= m; ++i) t[i] += t[i - 1];
    for (int i = n; i >= 1; --i) sa[t[w[i]]--] = id[i];
    m = 0;
    swap(rnk, lst_rnk);
    for (int i = 1; i <= n; ++i)
      rnk[sa[i]] = sa_same(sa[i], sa[i - 1]) ? m : ++m;
  }
}
}  // namespace SA
using SA::rnk;
using SA::sa;
inline int read_s(int a[]) {
  static char s[MAXN];
  scanf("%s", s + 1);
  int len = strlen(s + 1);
  reverse(s + 1, s + len + 1);
  for (int i = 1; i <= len; ++i) a[i] = s[i] == '?' ? -1 : s[i] - '0';
  return len;
}
int n, m;
int a[MAXM], b[MAXN][MAXM], bl[MAXN];
int c[15];
namespace Subtask1 {
int brnk[MAXN][MAXM], whb[MAXN][MAXM];
int dp[MAXM][MAXN];
int f[MAXN][2];
void solve(void) {
  static char t[MAXL];
  int tot = 0;
  for (int i = 1; i <= n; ++i) {
    for (int j = m; j >= 1; --j) t[++tot] = b[i][j] + '0';
    t[++tot] = '$';
  }
  SA::build(t, tot);
  for (int i = 1; i <= n; ++i) brnk[i][0] = i, whb[0][i] = i;
  static int cur_rnk[MAXM];
  for (int i = 1; i <= tot; ++i) {
    int bid = (sa[i] - 1) / (m + 1) + 1;
    int bpos = m + 1 - sa[i] % (m + 1);
    if (bpos == m + 1) continue;
    brnk[bid][bpos] = ++cur_rnk[bpos];
    whb[bpos][cur_rnk[bpos]] = bid;
  }
  memset(dp, 0xc0, sizeof(dp));
  dp[0][n + 1] = 0;
  for (int i = 1; i <= m; ++i) {
    static int cnt[15];
    memset(cnt, 0, 11 << 2);
    for (int j = 1; j <= n; ++j) ++cnt[(b[j][i] + 1) % 10];
    for (int j = 1; j <= n + 1; ++j) {
      if (dp[i - 1][j] >= 0) {
        int kl, kr;
        if (~a[i])
          kl = kr = a[i];
        else
          kl = i == m ? 1 : 0, kr = 9;
        for (int k = kl; k <= kr; ++k) {
          int cost = 0;
          for (int x = 0; x <= 9; ++x) cost += cnt[x] * c[(x + k) % 10];
          int l = 1, r = n + 1;
          while (l < r) {
            int mid = (l + r) >> 1;
            if (mid > n) {
              r = mid;
              continue;
            }
            int bid = whb[i][mid];
            if (b[bid][i] + k + (brnk[bid][i - 1] >= j) >= 10)
              r = mid;
            else
              l = mid + 1;
          }
          dp[i][l] = max(dp[i][l], dp[i - 1][j] + cost);
        }
      }
      if (j <= n) {
        --cnt[(b[whb[i - 1][j]][i] + 1) % 10];
        ++cnt[b[whb[i - 1][j]][i]];
      }
    }
  }
  for (int i = 1; i <= n; ++i) {
    for (int j = m + 1; j <= bl[i]; ++j) f[i][0] += c[b[i][j]];
    int lst = 1;
    for (int j = m + 1; j <= bl[i]; ++j) {
      int cur = b[i][j] + lst;
      f[i][1] += c[cur % 10];
      lst = (cur >= 10);
    }
    if (lst) f[i][1] += c[1];
  }
  int ans = 0;
  int sum = 0;
  for (int i = 1; i <= n; ++i) sum += f[i][1];
  for (int i = 1; i <= n + 1; ++i) {
    if (dp[m][i] >= 0) ans = max(ans, dp[m][i] + sum);
    if (i <= n) sum = sum - f[whb[m][i]][1] + f[whb[m][i]][0];
  }
  printf("%d", ans);
  exit(0);
}
}  // namespace Subtask1
int main(void) {
  m = read_s(a);
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) bl[i] = read_s(b[i]);
  for (int i = 0; i <= 9; ++i) scanf("%d", &c[i]);
  Subtask1::solve();
  return 0;
}