#include <bits/stdc++.h>
using namespace std;
int n, m, f[1005][1005], ans, a[1005], b[1005], c[25], cnt[10], vis[1005], clk;
char st[1005];
struct num {
  int v[1005], len;
  void read() {
    int i;
    scanf("%s", st + 1);
    len = strlen(st + 1);
    for (i = 1; i <= len; i++)
      if (st[len - i + 1] >= '0' && st[len - i + 1] <= '9')
        v[i] = st[len - i + 1] - '0';
      else
        v[i] = -1;
  }
} A, B[1005];
int main() {
  int i, j, k, l, S;
  A.read(), m = A.len;
  scanf("%d", &n);
  for (i = 1; i <= n; i++) B[i].read(), m = max(m, B[i].len);
  for (i = 0; i <= 9; i++) scanf("%d", &c[i]);
  for (i = 1; i <= n; i++) a[i] = i;
  for (i = 0; i <= m; i++)
    for (j = 0; j <= n; j++) f[i][j] = -(1 << 30);
  f[0][0] = 0;
  for (i = 0; i < m; i++) {
    for (j = 0; j < 10; j++) cnt[j] = 0;
    for (j = 1; j <= n; j++) cnt[B[j].v[i + 1]]++;
    for (j = 9; j >= 0; j--) cnt[j] += cnt[j + 1];
    for (j = n; j >= 1; j--) b[cnt[B[a[j]].v[i + 1]]--] = a[j];
    for (j = (i + 1 == A.len); j <= 9; j++) {
      if (A.v[i + 1] != -1 && j != A.v[i + 1]) continue;
      ++clk, S = 0;
      for (k = 1; k <= n; k++)
        if (i + 1 <= A.len || i + 1 <= B[k].len)
          S += c[(B[k].v[i + 1] + j) % 10];
      for (k = 0, l = 0; k <= n; k++) {
        if (k) {
          vis[a[k]] = clk;
          S += c[(B[a[k]].v[i + 1] + j + 1) % 10];
          if (i + 1 <= B[a[k]].len || i + 1 <= A.len)
            S -= c[(B[a[k]].v[i + 1] + j) % 10];
        }
        while (l < n && B[b[l + 1]].v[i + 1] + j + (vis[b[l + 1]] == clk) >= 10)
          l++;
        f[i + 1][l] = max(f[i + 1][l], f[i][k] + S);
      }
    }
    for (j = n; j >= 1; j--) a[j] = b[j];
  }
  ans = -(1 << 30);
  for (i = 0; i <= n; i++) ans = max(ans, f[m][i] + i * c[1]);
  cout << ans;
  return 0;
}
