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 N = 1000 + 7;
const int INF = (int)1e9;
void place(string &s, int len) {
  while ((int)s.size() < len) {
    s += "0";
  }
  assert((int)s.size() == len);
}
int len, n, cls[N][N], dp[N][N], ini, cost[11], kek[N];
string a, b[N];
vector<int> nums[N];
int what[N];
int fr[22];
int fr2[22];
string num[N];
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> a >> n;
  reverse(a.begin(), a.end());
  len = (int)a.size() + 1;
  ini = (int)a.size();
  for (int i = 1; i <= n; i++) {
    cin >> b[i];
    kek[i] = (int)b[i].size();
    reverse(b[i].begin(), b[i].end());
    len = max(len, (int)b[i].size());
  }
  for (int x = 0; x <= 9; x++) {
    cin >> cost[x];
  }
  place(a, len);
  for (int i = 1; i <= n; i++) {
    place(b[i], len);
  }
  for (int i = 1; i <= n; i++) {
    nums[0].push_back(i);
  }
  for (int pos = 1; pos <= len; pos++) {
    vector<pair<pair<int, int>, int>> v;
    for (int i = 1; i <= n; i++) {
      v.push_back({{b[i][pos - 1] - '0', cls[pos - 1][i]}, i});
    }
    sort(v.begin(), v.end());
    int y = 1;
    cls[pos][v[0].second] = y;
    for (int i = 1; i < n; i++) {
      y += (v[i - 1].first != v[i].first);
      cls[pos][v[i].second] = y;
    }
    for (auto &it : v) {
      nums[pos].push_back(it.second);
    }
    reverse(nums[pos].begin(), nums[pos].end());
  }
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      dp[i][j] = -INF;
    }
  }
  dp[0][0] = 0;
  for (int pos = 0; pos < len; pos++) {
    int lo, hi;
    if (a[pos] == '?') {
      lo = (pos == ini - 1), hi = 9;
    } else {
      lo = hi = a[pos] - '0';
    }
    for (int x = 0; x <= 20; x++) {
      fr[x] = 0;
      fr2[x] = 0;
    }
    vector<int> carry;
    for (int i = 1; i <= n; i++) {
      what[i] = b[i][pos] - '0';
      fr[what[i]]++;
      if (pos < max(ini, kek[i])) {
        fr2[what[i]]++;
      }
    }
    for (int i = 0; i <= n; i++) {
      if (i) {
        fr[what[nums[pos][i - 1]]]--;
        if (pos < max(ini, kek[nums[pos][i - 1]])) {
          fr2[what[nums[pos][i - 1]]]--;
        }
        what[nums[pos][i - 1]]++;
        carry.push_back(nums[pos][i - 1]);
        fr[what[nums[pos][i - 1]]]++;
        if (pos < max(ini, kek[nums[pos][i - 1]])) {
          fr2[what[nums[pos][i - 1]]]++;
        }
      }
      if (dp[pos][i] != -INF) {
        for (int d = lo; d <= hi; d++) {
          int relax = dp[pos][i];
          int cnt = 0, cnt2 = 0;
          int zu = 0;
          for (int w = 0; w <= 20; w++) {
            int cf = 0;
            if ((w + d) % 10 == 0)
              cf += fr2[w];
            else
              cf += fr[w];
            zu += cf;
            cnt2 += cf * ((w + d) >= 10);
            relax += cf * cost[(w + d) % 10];
          }
          int ke = 0;
          for (int j = 1; j <= n && 0; j++) {
            if ((what[j] + d) % 10 == 0 && pos >= max(ini, kek[j])) {
              continue;
            }
          }
          cnt = cnt2;
          dp[pos + 1][cnt] = max(dp[pos + 1][cnt], relax);
        }
      }
    }
  }
  int sol = -INF;
  for (int i = 0; i <= n; i++) {
    sol = max(sol, dp[len][i]);
  }
  cout << sol << "\n";
  return 0;
}