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 iinf = 1e9 + 7;
const long long linf = 1ll << 60;
const double dinf = 1e60;
template <typename T>
inline void scf(T &x) {
  bool f = 0;
  x = 0;
  char c = getchar();
  while ((c < '0' || c > '9') && c != '-') c = getchar();
  if (c == '-') {
    f = 1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    x = x * 10 + c - '0';
    c = getchar();
  }
  if (f) x = -x;
  return;
}
template <typename T1, typename T2>
void scf(T1 &x, T2 &y) {
  scf(x);
  return scf(y);
}
template <typename T1, typename T2, typename T3>
void scf(T1 &x, T2 &y, T3 &z) {
  scf(x);
  scf(y);
  return scf(z);
}
template <typename T1, typename T2, typename T3, typename T4>
void scf(T1 &x, T2 &y, T3 &z, T4 &w) {
  scf(x);
  scf(y);
  scf(z);
  return scf(w);
}
inline char mygetchar() {
  char c = getchar();
  while (c == ' ' || c == '\n') c = getchar();
  return c;
}
template <typename T>
void chkmax(T &x, const T &y) {
  if (y > x) x = y;
  return;
}
template <typename T>
void chkmin(T &x, const T &y) {
  if (y < x) x = y;
  return;
}
const int N = 1010;
int n, m, ans;
int a[N], c[10];
int num[N][N], len[N];
int dp[N][N];
int rnk[N], id[N], new_rnk[N], new_id[N];
int with_carry[N], without_carry[N];
int carry_cost[N], no_carry_cost[N];
void TZL() {
  static char str[N];
  scanf("%s", str);
  m = strlen(str);
  memset((a), (-1), sizeof((a)));
  for (int i = 0; i < (m); ++i)
    if (str[i] != '?') a[m - i] = str[i] - '0';
  scf(n);
  for (int i = (1); i <= (n); ++i) {
    scanf("%s", str);
    len[i] = strlen(str);
    for (int j = 0; j < (len[i]); ++j) num[i][len[i] - j] = str[j] - '0';
  }
  for (int i = 0; i < (10); ++i) scf(c[i]);
  for (int i = (1); i <= (n); ++i) rnk[i] = i, id[i] = i;
  memset((dp), (-1), sizeof((dp)));
  dp[0][0] = 0;
  return;
}
void radix_sort(int col) {
  vector<int> bucket[10];
  for (int i = 0; i < (10); ++i) bucket[i].clear();
  for (int i = (1); i <= (n); ++i) bucket[num[id[i]][col]].push_back(id[i]);
  int pnt = 0;
  for (int x = (10) - 1; x >= 0; --x)
    for (int i : bucket[x]) new_id[++pnt] = i;
  for (int i = (1); i <= (n); ++i) new_rnk[new_id[i]] = i;
  return;
}
void RANK1() {
  for (int i = 0; i < (m); ++i) {
    radix_sort(i + 1);
    for (int x = 0; x < (10); ++x) {
      if (i == m - 1 && !x) continue;
      if (a[i + 1] != -1 && x != a[i + 1]) continue;
      with_carry[0] = 0;
      carry_cost[0] = 0;
      for (int j = (1); j <= (n); ++j) {
        int y = x + num[id[j]][i + 1] + 1;
        with_carry[j] = with_carry[j - 1];
        carry_cost[j] = carry_cost[j - 1] + c[y % 10];
        if (y > 9) chkmax(with_carry[j], new_rnk[id[j]]);
      }
      without_carry[n + 1] = 0;
      no_carry_cost[n + 1] = 0;
      for (int j = (n); j >= (1); --j) {
        int y = x + num[id[j]][i + 1];
        without_carry[j] = without_carry[j + 1];
        no_carry_cost[j] = no_carry_cost[j + 1] + c[y % 10];
        if (y > 9) chkmax(without_carry[j], new_rnk[id[j]]);
      }
      for (int j = (0); j <= (n); ++j)
        if (dp[i][j] != -1) {
          chkmax(dp[i + 1][max(without_carry[j + 1], with_carry[j])],
                 carry_cost[j] + no_carry_cost[j + 1] + dp[i][j]);
        }
    }
    memcpy((rnk), (new_rnk), sizeof((new_rnk)));
    memcpy((id), (new_id), sizeof((new_id)));
  }
  carry_cost[0] = 0;
  for (int i = (1); i <= (n); ++i) {
    int j = id[i];
    carry_cost[i] = carry_cost[i - 1];
    int k = m + 1;
    int y = num[j][m + 1] + 1;
    while (y > 9) {
      carry_cost[i] += c[y % 10];
      y = num[j][(++k)] + 1;
    }
    carry_cost[i] += c[y];
    ++k;
    while (k <= len[j]) carry_cost[i] += c[num[j][k++]];
  }
  no_carry_cost[n + 1] = 0;
  for (int i = (n); i >= (1); --i) {
    int j = id[i];
    no_carry_cost[i] = no_carry_cost[i + 1];
    for (int k = (m + 1); k <= (len[j]); ++k) no_carry_cost[i] += c[num[j][k]];
  }
  int ans = 0;
  for (int i = (0); i <= (n); ++i)
    if (dp[m][i] != -1) {
      chkmax(ans, dp[m][i] + carry_cost[i] + no_carry_cost[i + 1]);
    }
  printf("%d\n", ans);
  return;
}
int main() {
  TZL();
  RANK1();
  return 0;
}