#include <bits/stdc++.h>
using namespace std;
const int N = 1015;
int b[N][N];
char c[N];
int n;
char a[N];
int w[15];
int len = 0;
int sa[N], rk[N];
int tsa[N], trk[N];
int sum[15];
int f[2][N];
int cost[N][12][2];
int dj[11][N];
int lp[N];
int dp() {
  for (int i = (int)1; i <= (int)n; i++) sa[i] = i;
  for (int i = (int)1; i <= (int)n; i++) rk[sa[i]] = i;
  for (int i = (int)0; i <= (int)n; i++) f[0][i] = f[1][i] = -1000000000;
  int now = 0;
  f[0][0] = 0;
  for (int bit = (int)1; bit <= (int)1005; bit++) {
    for (int i = (int)0; i <= (int)n; i++) f[now ^ 1][i] = -1000000000;
    memset(cost, 0, sizeof cost);
    for (int i = (int)1; i <= (int)n; i++) {
      for (int j = (int)0; j <= (int)9; j++)
        for (int k = (int)0; k <= (int)1; k++) {
          if ((bit <= max(lp[sa[i]], len)) ||
              (bit == max(lp[sa[i]], len) + 1 &&
               ((b[sa[i]][bit] + j + k) % 10 != 0)))
            cost[i][j][k] = cost[i - 1][j][k] + w[(b[sa[i]][bit] + j + k) % 10];
          else
            cost[i][j][k] = cost[i - 1][j][k];
        }
    }
    memset(sum, 0, sizeof sum);
    for (int i = (int)1; i <= (int)n; i++) sum[b[i][bit]]++;
    for (int i = (int)8; i >= (int)0; i--) sum[i] += sum[i + 1];
    for (int i = (int)n; i >= (int)1; i--) tsa[sum[b[sa[i]][bit]]--] = sa[i];
    for (int i = (int)1; i <= (int)n; i++) trk[tsa[i]] = i;
    for (int i = (int)0; i <= (int)9; i++) {
      int dc = 0;
      while (dc < n && b[tsa[dc + 1]][bit] > i) ++dc;
      for (int j = (int)0; j <= (int)n; j++) {
        while (dc < n && rk[tsa[dc + 1]] <= j && b[tsa[dc + 1]][bit] == i) ++dc;
        dj[i][j] = dc;
      }
    }
    for (int i = (int)0; i <= (int)n; i++)
      if (f[now][i] >= 0) {
        for (int v = (int)0; v <= (int)9; v++) {
          if (v > 0 && bit > len) continue;
          if (bit <= len && a[bit] != '?' && (a[bit] - '0' != v)) continue;
          if (bit == len && v == 0) continue;
          f[now ^ 1][dj[9 - v][i]] =
              max(f[now ^ 1][dj[9 - v][i]],
                  f[now][i] + cost[i][v][1] + cost[n][v][0] - cost[i][v][0]);
        }
      }
    now ^= 1;
    for (int i = (int)1; i <= (int)n; i++) sa[i] = tsa[i], rk[i] = trk[i];
  }
  return f[now][0];
}
int main() {
  scanf("%s", a + 1);
  n = strlen(a + 1);
  len = n;
  for (int i = (int)1; i <= (int)n; i++)
    if (i < n + 1 - i) swap(a[i], a[n + 1 - i]);
  scanf("%d", &n);
  for (int i = (int)1; i <= (int)n; i++) {
    scanf("%s", c + 1);
    int m = strlen(c + 1);
    lp[i] = m;
    for (int j = (int)1; j <= (int)m; j++) b[i][j] = c[m + 1 - j] - '0';
  }
  for (int i = (int)0; i <= (int)9; i++) scanf("%d", &w[i]);
  printf("%d\n", dp());
  return 0;
}
