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 <class T>
inline void rread(T& num) {
  num = 0;
  T f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') num = num * 10 + ch - '0', ch = getchar();
  num *= f;
}
const long long inf = 1e18;
const int maxn = 1e3 + 10, mod = 1e9 + 7;
const double pi = acos(-1);
long long gcd(long long a, long long b) { return (a ? gcd(b % a, a) : b); }
void exgcd(long long a, long long b, long long& d, long long& x, long long& y) {
  if (!b) {
    d = a;
    x = 1;
    y = 0;
  } else {
    exgcd(b, a % b, d, y, x);
    y -= x * (a / b);
  }
}
complex<double> power(complex<double> a, int n) {
  complex<double> p = 1;
  while (n > 0) {
    if (n % 2) {
      p = p * a;
    }
    n >>= 1;
    a *= a;
  }
  return p;
}
long long power(long long a, long long n) {
  long long p = 1;
  while (n > 0) {
    if (n % 2) {
      p = p * a;
    }
    n >>= 1;
    a *= a;
  }
  return p;
}
long long power(long long a, long long n, long long mod) {
  long long p = 1;
  while (n > 0) {
    if (n % 2) {
      p = p * a;
      p %= mod;
    }
    n >>= 1;
    a *= a;
    a %= mod;
  }
  return p % mod;
}
char a[maxn], s[maxn][maxn];
int c[maxn];
int dp[maxn][maxn];
pair<int, pair<int, int> > val[maxn], gg[maxn];
int fin[maxn][maxn];
int flag[maxn];
int main() {
  int aa;
  int n;
  int t;
  while (~scanf("%s", a)) {
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) {
      scanf("%s", s[i]);
    }
    for (int i = 0; i <= 9; i++) scanf("%d", &c[i]);
    for (int i = 1; i <= n; i++) {
      int cur = strlen(s[i]) - 1;
      for (int j = 1000; j >= 0; j--) {
        if (cur < 0)
          s[i][j] = '0';
        else
          s[i][j] = s[i][cur];
        cur--;
      }
    }
    int cur = strlen(a) - 1;
    for (int j = 1000; j >= 0; j--) {
      if (cur < 0)
        a[j] = '0';
      else
        a[j] = a[cur];
      cur--;
    }
    memset(flag, 0, sizeof(flag));
    for (int j = 1; j <= n; j++) s[j][1001] = '0';
    memset(dp, 0, sizeof(dp));
    for (int i = 0; i <= n; i++) {
      dp[0][i] = i * c[1];
    }
    int f = 1;
    memset(fin, 0, sizeof(fin));
    for (int i = 1000; i >= 1; i--) {
      for (int j = 1; j <= n; j++)
        gg[j] = make_pair(s[j][i], make_pair(fin[j][i + 1], j));
      sort(gg + 1, gg + 1 + n);
      int cun = 0, ps = 10000;
      for (int j = 1; j <= n; j++) {
        int id = gg[j].second.second;
        if (j > 0 && gg[j].first != gg[j - 1].first)
          cun = 0, ps += 10000;
        else
          cun++;
        fin[id][i] = ps + cun;
      }
    }
    for (int i = 1; i <= 1000; i++) {
      for (int j = 1; j <= n; j++)
        val[j] = make_pair(s[j][i + 1] - '0', make_pair(fin[j][i + 1], j));
      for (int j = 1; j <= n; j++)
        if (flag[j] == 0)
          if (s[j][i] != '0') flag[j] = 1;
      sort(val + 1, val + 1 + n);
      reverse(val + 1, val + 1 + n);
      int l, r;
      if (a[i] == '?') {
        l = max(f, 0);
        r = 9;
      } else
        r = a[i] - '0', l = a[i] - '0';
      if (l != 0) f = 0;
      for (int g = l; g <= r; g++) {
        int cun = 0, cost = 0;
        for (int j = 1; j <= n; j++) {
          int id = g + s[val[j].second.second][i] - '0';
          if (id >= 10) cun++;
          if (f != 1 || flag[val[j].second.second] == 1) cost += c[id % 10];
        }
        dp[i][0] = max(dp[i][0], dp[i - 1][cun] + cost);
        for (int j = 1; j <= n; j++) {
          int id = g + s[val[j].second.second][i] - '0';
          if (id + 1 == 10) cun++;
          if (f != 1 || flag[val[j].second.second] == 1) cost -= c[id % 10];
          cost += c[(id + 1) % 10];
          dp[i][j] = max(dp[i][j], dp[i - 1][cun] + cost);
        }
      }
    }
    printf("%d\n", dp[1000][0]);
  }
  return 0;
}