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>
#pragma GCC optimize("Ofast")
using namespace std;
const int N = 1005;
int a[N];
int dig[N][N];
bool has[N][N];
int cost[10];
int dp[N + 1][N];
int ord[N + 1][N];
int idx[N + 1][N];
int vl[N];
vector<pair<int, int> > gg[10];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ;
  for (int i = 0; i <= N; i++) {
    for (int j = 0; j < N; j++) {
      dp[i][j] = -(int)1e9;
    }
  }
  string t;
  cin >> t;
  int p = N;
  for (int i = t.size() - 1; i >= 0; i--) {
    p--;
    if (t[i] == '?')
      a[p] = -1;
    else
      a[p] = t[i] - '0';
  }
  int pos = p;
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> t;
    p = N;
    for (int j = (int)t.size() - 1; j >= 0; j--) {
      p--;
      dig[i][p] = t[j] - '0';
      has[i][p] = true;
    }
  }
  for (int j = 0; j < 10; j++) cin >> cost[j];
  for (int i = 0; i < n; i++) {
    ord[N][i] = i;
  }
  int c;
  for (int i = N - 1; i >= 0; i--) {
    for (int j = 0; j < 10; j++) {
      gg[j].clear();
    }
    for (int j = 0; j < n; j++) {
      gg[dig[j][i]].push_back(make_pair(ord[i + 1][j], j));
    }
    for (int j = 0; j < 10; j++) {
      sort(gg[j].begin(), gg[j].end());
    }
    c = 0;
    for (int j = 9; j >= 0; j--) {
      for (auto x : gg[j]) {
        ord[i][x.second] = c;
        c++;
      }
    }
  }
  for (int i = 0; i < N + 1; i++) {
    for (int j = 0; j < n; j++) {
      idx[i][ord[i][j]] = j;
    }
  }
  dp[N][0] = 0;
  int add;
  int qid;
  int id;
  int tot;
  int ss;
  for (int i = N - 1; i >= 0; i--) {
    vector<int> stg;
    if (a[i] == -1)
      for (int j = (i == pos); j <= 9; j++) stg.push_back(j);
    else
      stg.push_back(a[i]);
    for (auto q : stg) {
      add = 0;
      for (int j = 0; j < n; j++) {
        vl[j] = 0;
      }
      for (int j = 0; j < n; j++) {
        ss = (dig[j][i] + q);
        if (ss == 0 && i < pos && !has[j][i]) continue;
        vl[j] = cost[ss % 10];
        add += vl[j];
      }
      qid = 0;
      for (int t = 0; t <= n; t++) {
        while (qid < n) {
          id = idx[i][qid];
          ss = (dig[id][i] + q + (ord[i + 1][id] < t));
          if (ss == 0 && i < pos && !has[id][i]) break;
          add -= vl[id];
          vl[id] = cost[ss % 10];
          add += vl[id];
          if (ss >= 10)
            qid++;
          else
            break;
        }
        dp[i][qid] = max(dp[i][qid], dp[i + 1][t] + add);
        if (t < n) {
          id = idx[i + 1][t];
          ss = (dig[id][i] + q + 1);
          add -= vl[id];
          vl[id] = cost[ss % 10];
          add += vl[id];
        }
      }
    }
  }
  cout << dp[0][0];
  return 0;
}