#include <bits/stdc++.h>
using namespace std;
const int maxN = 1005, N = 1001;
array<array<int, maxN>, maxN> dp;
array<string, maxN> B;
array<int, 10> c;
vector<vector<int>> p(maxN);
array<bool, maxN> beg;
string A;
int n, ax;
int input(string &A) {
  cin >> A;
  int a = A.size();
  reverse(A.begin(), A.end());
  while (A.size() < N) {
    A += '0';
  }
  return a;
}
void update(int i) {
  array<int, maxN> tmp;
  int sum = 0, carry = 0;
  for (int j = 0; j < n; j++) {
    if (B[j][i] > '0' || ax > i) {
      beg[j] = true;
    }
    tmp[j] = (B[j][i] - '0') + (A[i] - '0');
    if (tmp[j] > 9) {
      tmp[j] -= 10;
      carry++;
    }
    if (beg[j] or tmp[j] > 0) {
      sum += c[tmp[j]];
    }
  }
  dp[i][0] = max(dp[i][0], sum + dp[i + 1][carry]);
  for (int j = 1; j <= n; j++) {
    if (tmp[p[i][n - j]] == 9) {
      carry++;
      sum += c[0] - c[9];
    } else {
      sum += c[tmp[p[i][n - j]] + 1];
      if (beg[p[i][n - j]] or tmp[p[i][n - j]] > 0) {
        sum -= c[tmp[p[i][n - j]]];
      }
    }
    dp[i][j] = max(dp[i][j], sum + dp[i + 1][carry]);
  }
}
int32_t main() {
  int a = input(A);
  ax = a;
  cin >> n;
  for (int i = 0; i < n; i++) {
    input(B[i]);
    p[0].push_back(i);
  }
  for (int i = 0; i < 10; i++) {
    cin >> c[i];
  }
  for (int i = 1; i <= N; i++) {
    array<vector<int>, 10> temp = {};
    for (int j : p[i - 1]) {
      temp[(B[j][i - 1] - '0')].push_back(j);
    }
    for (int j = 0; j < 10; j++) {
      p[i].insert(p[i].end(), temp[j].begin(), temp[j].end());
    }
  }
  for (int i = N - 1; i >= 0; i--) {
    if (A[i] == '?') {
      for (char j = '0' + (i == a - 1); j <= '9'; j++) {
        A[i] = j;
        update(i);
      }
    } else {
      update(i);
    }
  }
  cout << dp[0][0];
}
