#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1E6;
int n, q, sz = 1;
long long value[6];
long long dp[MAXN];
inline void upData(int v, long long w) {
  for (int i = MAXN - 1; i >= v; --i) dp[i] = max(dp[i], dp[i - v] + w);
}
inline void Divide(int x) {
  int v = 3 * sz, all = min(MAXN / v, n);
  for (int i = 1; i < all; i <<= 1) upData(v * i, 1ll * value[x] * i), all -= i;
  upData(v * all, 1ll * value[x] * all);
}
int main(void) {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n, n = 3 * (n - 1);
  for (int i = 0; i < 6; ++i) cin >> value[i];
  for (int i = 3; i < MAXN; ++i) {
    int num = i, base = 0;
    while (num) {
      if (((num % 10) % 3) == 0) dp[i] += 1ll * value[base] * ((num % 10) / 3);
      num /= 10, ++base;
    }
  }
  for (int i = 0; i < 6; ++i) Divide(i), sz *= 10;
  cin >> q;
  while (q--) cin >> n, cout << dp[n] << '\n';
  return 0;
}
