#include <bits/stdc++.h>
using namespace std;
int A[1002], LA;
int B[1000][1002], LB[1000];
int C[10];
int M, N;
int T[6][1002];
constexpr auto Z = sizeof(T[0]);
void Read(int *D, int &L) {
  static char H[1001];
  scanf("%s", H);
  int m = (int)strlen(H);
  for (int i = 0; i < m; ++i) D[m - i - 1] = H[i] == '?' ? -1 : H[i] & 0x0f;
  if (m > M) M = m;
  L = m;
}
int *F = T[0], *G = T[1], *I = T[2], *J = T[3];
void Sort(int c) {
  static int W[10];
  memset(W, 0, sizeof(W));
  for (int i = 0; i < N; ++i) ++W[B[J[i]][c]];
  for (int i = 1; i < 10; ++i) W[i] += W[i - 1];
  for (int i = N - 1; i >= 0; --i) I[--W[B[J[i]][c]]] = J[i];
}
int main() {
  Read(A, LA);
  scanf("%d", &N);
  for (int i = 0; i < N; ++i) {
    Read(B[i], LB[i]);
    if (LA > LB[i]) LB[i] = LA;
  }
  for (int i = 0; i < 10; ++i) scanf("%d", &C[i]);
  ++M;
  memset(G, -1, Z);
  G[N] = 0;
  for (int i = 0; i < N; ++i) J[i] = i;
  for (int i = 0; i <= M; ++i) {
    memset(F, -1, Z);
    Sort(i);
    int j1 = A[i] != -1 ? A[i] : i == LA - 1 ? 1 : 0;
    int j2 = A[i] != -1 ? A[i] : 9;
    for (int j = j1; j <= j2; ++j) {
      int f = 0, l = N;
      for (int k = 0; k < N; ++k) {
        int v = i >= LB[k] && !j ? -1 : B[k][i] + j;
        if (v != -1) f += C[v > 9 ? (--l, v - 10) : v];
      }
      if (G[N] != -1 && G[N] + f > F[l]) F[l] = G[N] + f;
      for (int k = N - 1; k >= 0; --k) {
        int v1 = i >= LB[J[k]] && !j ? -1 : B[J[k]][i] + j;
        int v2 = B[J[k]][i] + j + 1;
        if (v1 != -1) f -= C[v1 > 9 ? (++l, v1 - 10) : v1];
        f += C[v2 > 9 ? (--l, v2 - 10) : v2];
        if (G[k] != -1 && G[k] + f > F[l]) F[l] = G[k] + f;
      }
    }
    swap(F, G);
    swap(I, J);
  }
  printf("%d\n", G[N]);
  return 0;
}
