#include <bits/stdc++.h>
using namespace std;
template <class T>
inline void rread(T& num) {
  num = 0;
  T f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') num = num * 10 + ch - '0', ch = getchar();
  num *= f;
}
const long long inf = 1e18;
const int maxn = 1e3 + 10, mod = 1e9 + 7;
const double pi = acos(-1);
long long gcd(long long a, long long b) { return (a ? gcd(b % a, a) : b); }
void exgcd(long long a, long long b, long long& d, long long& x, long long& y) {
  if (!b) {
    d = a;
    x = 1;
    y = 0;
  } else {
    exgcd(b, a % b, d, y, x);
    y -= x * (a / b);
  }
}
complex<double> power(complex<double> a, int n) {
  complex<double> p = 1;
  while (n > 0) {
    if (n % 2) {
      p = p * a;
    }
    n >>= 1;
    a *= a;
  }
  return p;
}
long long power(long long a, long long n) {
  long long p = 1;
  while (n > 0) {
    if (n % 2) {
      p = p * a;
    }
    n >>= 1;
    a *= a;
  }
  return p;
}
long long power(long long a, long long n, long long mod) {
  long long p = 1;
  while (n > 0) {
    if (n % 2) {
      p = p * a;
      p %= mod;
    }
    n >>= 1;
    a *= a;
    a %= mod;
  }
  return p % mod;
}
char a[maxn], s[maxn][maxn];
int c[maxn];
int dp[maxn][maxn];
pair<int, pair<int, int> > val[maxn], gg[maxn];
int fin[maxn][maxn];
int flag[maxn];
int main() {
  int aa;
  int n;
  int t;
  while (~scanf("%s", a)) {
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) {
      scanf("%s", s[i]);
    }
    for (int i = 0; i <= 9; i++) scanf("%d", &c[i]);
    for (int i = 1; i <= n; i++) {
      int cur = strlen(s[i]) - 1;
      for (int j = 1000; j >= 0; j--) {
        if (cur < 0)
          s[i][j] = '0';
        else
          s[i][j] = s[i][cur];
        cur--;
      }
    }
    int cur = strlen(a) - 1;
    for (int j = 1000; j >= 0; j--) {
      if (cur < 0)
        a[j] = '0';
      else
        a[j] = a[cur];
      cur--;
    }
    memset(flag, 0, sizeof(flag));
    for (int j = 1; j <= n; j++) s[j][1001] = '0';
    memset(dp, 0, sizeof(dp));
    for (int i = 0; i <= n; i++) {
      dp[0][i] = i * c[1];
    }
    int f = 1;
    memset(fin, 0, sizeof(fin));
    for (int i = 1000; i >= 1; i--) {
      for (int j = 1; j <= n; j++)
        gg[j] = make_pair(s[j][i], make_pair(fin[j][i + 1], j));
      sort(gg + 1, gg + 1 + n);
      int cun = 0, ps = 10000;
      for (int j = 1; j <= n; j++) {
        int id = gg[j].second.second;
        if (j > 0 && gg[j].first != gg[j - 1].first)
          cun = 0, ps += 10000;
        else
          cun++;
        fin[id][i] = ps + cun;
      }
    }
    for (int i = 1; i <= 1000; i++) {
      for (int j = 1; j <= n; j++)
        val[j] = make_pair(s[j][i + 1] - '0', make_pair(fin[j][i + 1], j));
      for (int j = 1; j <= n; j++)
        if (flag[j] == 0)
          if (s[j][i] != '0') flag[j] = 1;
      sort(val + 1, val + 1 + n);
      reverse(val + 1, val + 1 + n);
      int l, r;
      if (a[i] == '?') {
        l = max(f, 0);
        r = 9;
      } else
        r = a[i] - '0', l = a[i] - '0';
      if (l != 0) f = 0;
      for (int g = l; g <= r; g++) {
        int cun = 0, cost = 0;
        for (int j = 1; j <= n; j++) {
          int id = g + s[val[j].second.second][i] - '0';
          if (id >= 10) cun++;
          if (f != 1 || flag[val[j].second.second] == 1) cost += c[id % 10];
        }
        dp[i][0] = max(dp[i][0], dp[i - 1][cun] + cost);
        for (int j = 1; j <= n; j++) {
          int id = g + s[val[j].second.second][i] - '0';
          if (id + 1 == 10) cun++;
          if (f != 1 || flag[val[j].second.second] == 1) cost -= c[id % 10];
          cost += c[(id + 1) % 10];
          dp[i][j] = max(dp[i][j], dp[i - 1][cun] + cost);
        }
      }
    }
    printf("%d\n", dp[1000][0]);
  }
  return 0;
}
