#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 100;
const int n = 999999;
const long long mod = 1e18;
long long a[6], dp[maxn];
;
const long long ten[6] = {1, 10, 100, 1000, 10000, 100000},
                f[10] = {0, 0, 0, 1, 0, 0, 2, 0, 0, 3};
int main() {
  int t = 1;
  while (t--) {
    int k, q;
    cin >> k;
    for (int i = 0; i < 6; i++) cin >> a[i];
    fill(dp, dp + n + 1, -mod);
    dp[0] = 0;
    for (int d = 0; d < 6; d++) {
      long long l = 3 * (k - 1), g = 1;
      while (l > 0) {
        g = min(g, l);
        long long val = g * a[d];
        long long w = g * ten[d] * 3;
        for (int i = n; i >= w; i--) dp[i] = max(dp[i], dp[i - w] + val);
        l -= g;
        g <<= 1;
      }
    }
    for (int d = 0; d < 6; d++)
      for (int i = n; i >= 0; i--)
        for (int b = 1; b < 10; b++) {
          int p = i - ten[d] * b;
          if (p < 0) break;
          dp[i] = max(dp[i], dp[p] + f[b] * a[d]);
        }
    cin >> q;
    while (q--) {
      int x;
      cin >> x;
      cout << dp[x] << endl;
    }
  }
  return 0;
}
