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 max_n = 1011, inf = 1011111111;
int n, cost[11], k[111], dp[max_n][max_n], pos[max_n], q[max_n][10], all[max_n],
    cntx[12], sum[12];
int last[max_n], is_finish[max_n], sum_finish[max_n];
pair<int, pair<int, int> > p[max_n];
char s[max_n];
string a, x[max_n];
string read() {
  scanf("%s", s);
  return s;
}
void upd(string &s, int len, char c) {
  string q = "";
  while (s.length() + q.length() < len) {
    q += c;
  }
  s = q + s;
}
int proc() {
  int mx = a.length();
  for (int i = 0; i < n; ++i) {
    mx = max(mx, (int)x[i].length());
  }
  ++mx;
  last[n] = mx - a.length();
  upd(a, mx, '0');
  for (int i = 0; i < n; ++i) {
    last[i] = mx - x[i].length();
    upd(x[i], mx, '0');
  }
  return mx;
}
int get(int l, int r, int x) {
  if (l > r) {
    return 0;
  }
  if (l == 0) {
    return q[r][x];
  }
  return q[r][x] - q[l - 1][x];
}
void get_sum() {
  for (int c = 10; c >= 0; --c) {
    sum[c] = sum[c + 1] + cntx[c];
  }
}
int main() {
  a = read();
  int alen = a.length();
  scanf("%d", &n);
  for (int i = 0; i < n; ++i) {
    x[i] = read();
  }
  for (int i = 0; i <= 9; ++i) {
    scanf("%d", &cost[i]);
  }
  int len = proc();
  if (0) {
    cout << a << endl;
    for (int i = 0; i < n; ++i) {
      cout << x[i] << endl;
    }
  }
  memset(dp, -1, sizeof(dp));
  dp[len - 1][0] = 0;
  for (int i = 0; i < n; ++i) {
    pos[i] = i;
    p[i] = make_pair(0, make_pair(0, i));
  }
  for (int i = len - 1; i > 0; --i) {
    memset(cntx, 0, sizeof(cntx));
    for (int j = 0; j < n; ++j) {
      sum_finish[j] = is_finish[p[j].second.second];
      memset(q[j], 0, sizeof(q[j]));
      all[j] = x[p[j].second.second][i] - '0';
      ++cntx[all[j]];
      ++q[j][all[j]];
      if (j) {
        sum_finish[j] += sum_finish[j - 1];
        for (int k = 0; k < 10; ++k) {
          q[j][k] += q[j - 1][k];
        }
      }
    }
    get_sum();
    for (int cnt = 0; cnt <= n; ++cnt) {
      if (dp[i][cnt] != -1) {
        char from = '0', to = '9';
        if (i + alen == len) {
          from = '1';
        }
        if (a[i] != '?') {
          from = to = a[i];
        }
        from -= '0';
        to -= '0';
        for (int c = from; c <= to; ++c) {
          int res = 0;
          for (int x = 0; x <= 9; ++x) {
            if (x) {
              res += get(0, n - cnt - 1, x) * cost[(x + c) % 10];
            } else if (n - cnt - 1 >= 0) {
              res += (get(0, n - cnt - 1, x) - sum_finish[n - cnt - 1]) *
                     cost[(x + c) % 10];
            }
            res += get(n - cnt, n - 1, x) * cost[(x + c + 1) % 10];
          }
          int ncnt = sum[10 - c];
          dp[i - 1][ncnt] = max(dp[i - 1][ncnt], dp[i][cnt] + res);
        }
      }
      --cntx[all[n - 1 - cnt]];
      ++cntx[all[n - 1 - cnt] + 1];
      get_sum();
    }
    for (int j = 0; j < n; ++j) {
      p[j] = make_pair(x[j][i], make_pair(pos[j], j));
    }
    sort(p, p + n);
    for (int j = 0; j < n; ++j) {
      pos[p[j].second.second] = j;
      if (last[j] >= i && last[n] >= i) {
        is_finish[j] = 1;
      }
    }
  }
  int ans = 0;
  for (int i = 0; i <= n; ++i) {
    if (dp[0][i] != -1) {
      ans = max(ans, dp[0][i] + i * cost[1]);
    }
  }
  printf("%d\n", ans);
  return 0;
}