#include <bits/stdc++.h>
using namespace std;
const long long N = 1e6 + 5;
long long k, T, q, m = N - 5;
long long ww[6], f[N];
int main() {
  cin >> k;
  for (long long i = 0; i <= 5; i++) cin >> ww[i];
  for (long long i = 0; i < m; i++) {
    long long t = i, tot = 0;
    for (long long j = 0; j <= 5; j++, t /= 10) {
      long long tt = t % 10;
      if (tt % 3 == 0) tot += tt / 3 * ww[j];
    }
    f[i] = tot;
  }
  long long tot = 1;
  for (long long t = 0; t <= 5; t++, tot *= 10) {
    long long w = ww[t], v = tot * 3;
    long long tmp = 3 * k - 3;
    for (long long j = 1;; j *= 2) {
      if (tmp < j) {
        for (long long i = m; i >= v * tmp; i--)
          f[i] = max(f[i], f[i - v * tmp] + w * tmp);
        break;
      }
      for (long long i = m; i >= v * j; i--)
        f[i] = max(f[i], f[i - v * j] + w * j);
      tmp -= j;
    }
  }
  cin >> T;
  while (T--) {
    long long q;
    cin >> q;
    cout << f[q] << endl;
  }
}
