#include <bits/stdc++.h>
using namespace std;
namespace solution {
const int N = 1e6 + 19;
const long long inf = 1e17;
long long f[N], k, val[6];
long long pw[6] = {1, 10, 100, 1000, 10000, 100000};
void solve() {
  cin >> k;
  for (int i = 0; i < N; ++i) f[i] = -inf;
  f[0] = 0;
  for (int i = 0; i < 6; ++i) {
    cin >> val[i];
    long long res = 3 * (k - 1);
    while (res) {
      long long now = 1;
      while (now <= res) {
        res -= now;
        long long w = now * pw[i] * 3, v = now * val[i];
        for (int j = N - 1; j >= w; --j)
          if (f[j - w] >= 0) f[j] = max(f[j], f[j - w] + v);
        now *= 2;
      }
    }
  }
  for (int i = 0; i < 6; ++i)
    for (int j = N - 1; j >= pw[i]; --j)
      for (int k = 1; k < 10; ++k) {
        long long w = k * pw[i], v = val[i] * (k / 3) * (k % 3 == 0);
        if (w > j) break;
        if (f[j - w] >= 0) f[j] = max(f[j], f[j - w] + v);
      }
  int q;
  cin >> q;
  for (int i = 1; i <= q; ++i) {
    int x;
    cin >> x;
    cout << f[x] << endl;
  }
}
}  // namespace solution
int main() { solution::solve(); }
