#include <bits/stdc++.h>
using namespace std;
long long ans[1000005], val[10];
inline void mdf(int v, long long w) {
  for (int i = 1e6 - 1; i >= v; --i) ans[i] = max(ans[i], ans[i - v] + w);
  return;
}
int main() {
  int n, t;
  scanf("%d", &n);
  for (int i = 0; i < 6; ++i) scanf("%lld", val + i);
  for (int i = 1, j, pos; i <= 1e6; ++i) {
    j = i, pos = 0;
    while (j)
      ans[i] += (j % 10 % 3 == 0) * (j % 10 / 3) * val[pos], ++pos, j /= 10;
  }
  for (int i = 0, pw = 3, x, bt; i < 6; ++i, pw = (pw << 3) + (pw << 1)) {
    x = min((n - 1) * 3.0, (1e6 - 1) / pw), bt = 1;
    for (; x > bt; x -= bt, bt <<= 1) mdf(pw * bt, val[i] * bt);
    mdf(pw * x, val[i] * x);
  }
  scanf("%d", &t);
  while (t--) scanf("%d", &n), printf("%lld\n", ans[n]);
  return 0;
}
