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 X, class Y>
void minimize(X &x, const Y &y) {
  if (x > y) x = y;
}
template <class X, class Y>
void maximize(X &x, const Y &y) {
  if (x < y) x = y;
}
template <class T>
T Abs(const T &x) {
  return (x < 0 ? -x : x);
}
int n, addLen, numLen[1010];
int digits[1010][1050 + 3], toAdd[1050 + 3];
int order[1050 + 3][1010], ___rank[1050 + 3][1010];
int countRank[1050 + 3][11][1010], countDigit[1050 + 3][11];
int postValue[1010][2];
int value[11];
int f[1050 + 3][1010];
char input[1050 + 3];
void init(void) {
  scanf("%s", input);
  addLen = strlen(input);
  reverse(input, input + addLen);
  for (int i = 0, _n = (addLen); i < _n; i = i + 1)
    toAdd[i] = input[i] == '?' ? -1 : input[i] - '0';
  scanf("%d", &n);
  for (int i = (1), _b = (n); i <= _b; i = i + 1) {
    scanf("%s", input);
    numLen[i] = strlen(input);
    reverse(input, input + numLen[i]);
    for (int j = 0, _n = (numLen[i]); j < _n; j = j + 1)
      digits[i][j] = input[j] - '0';
  }
  for (int i = 0, _n = (10); i < _n; i = i + 1) scanf("%d", &value[i]);
}
int calcPostValue(const string &s) {
  int res = 0;
  for (__typeof((s).begin()) it = (s).begin(); it != (s).end(); it++)
    res += value[*it - '0'];
  return res;
}
int curPos;
bool compare(const int &x, const int &y) {
  if (digits[x][curPos] != digits[y][curPos])
    return digits[x][curPos] > digits[y][curPos];
  return curPos == 0 ? false : ___rank[curPos - 1][x] < ___rank[curPos - 1][y];
}
void prepare(void) {
  for (int i = 0, _n = (1050); i < _n; i = i + 1) {
    curPos = i;
    for (int j = (1), _b = (n); j <= _b; j = j + 1) order[i][j] = j;
    sort(order[i] + 1, order[i] + n + 1, compare);
    for (int j = (1), _b = (n); j <= _b; j = j + 1) ___rank[i][order[i][j]] = j;
    for (int j = (1), _b = (n); j <= _b; j = j + 1)
      countRank[i + 1][digits[j][i + 1]][___rank[i][j]]++;
    for (int j = 0, _n = (10); j < _n; j = j + 1)
      for (int k = (1), _b = (n); k <= _b; k = k + 1)
        countRank[i + 1][j][k] += countRank[i + 1][j][k - 1];
    for (int j = (1), _b = (n); j <= _b; j = j + 1)
      countDigit[i][digits[j][i]]++;
  }
  for (int i = (1), _b = (n); i <= _b; i = i + 1) {
    if (numLen[i] <= addLen) {
      postValue[i][0] = 0;
      postValue[i][1] = value[1];
      continue;
    }
    string post;
    for (int j = (addLen), _b = (numLen[i] - 1); j <= _b; j = j + 1)
      post.push_back(digits[i][j] + '0');
    postValue[i][0] = calcPostValue(post);
    int j = 0;
    while (j < post.size() && post[j] == '9') post[j++] = '0';
    if (j == post.size())
      post.push_back('1');
    else
      post[j]++;
    postValue[i][1] = calcPostValue(post);
  }
}
int getSum(int sum[], int l, int r) {
  if (l > r) return 0;
  return l == 0 ? sum[r] : sum[r] - sum[l - 1];
}
void optimize(void) {
  memset(f, -1, sizeof f);
  f[0][0] = 0;
  for (int i = 0, _n = (addLen); i < _n; i = i + 1) {
    int countRem[11];
    memset(countRem, 0, sizeof countRem);
    for (int j = 0, _n = (n + 1); j < _n; j = j + 1) {
      if (j > 0 && i > 0) {
        int pos = order[i - 1][j];
        countRem[digits[pos][i]]++;
      }
      if (f[i][j] < 0) continue;
      for (int k = 0, _n = (10); k < _n; k = k + 1) {
        if (toAdd[i] != -1 && toAdd[i] != k) continue;
        if (i == addLen - 1 && k == 0) continue;
        int gain = 0, newRem = 0;
        for (int l = 0, _n = (10); l < _n; l = l + 1) {
          int tot = countDigit[i][l];
          int haveRem = countRem[l];
          gain += haveRem * value[(k + l + 1) % 10] +
                  (tot - haveRem) * value[(k + l) % 10];
          if (k + l > 9) newRem += tot;
          if (k + l == 9) newRem += getSum(countRank[i][l], 1, j);
        }
        maximize(f[i + 1][newRem], f[i][j] + gain);
      }
    }
  }
  int res = 0;
  for (int j = 0, _n = (n + 1); j < _n; j = j + 1) {
    int tmp = f[addLen][j];
    if (tmp <= 0) continue;
    for (int i = (1), _b = (n); i <= _b; i = i + 1)
      tmp += ___rank[addLen - 1][i] <= j ? postValue[i][1] : postValue[i][0];
    maximize(res, tmp);
  }
  printf("%d\n", res);
}
int main(void) {
  init();
  prepare();
  optimize();
  return 0;
}