#include <bits/stdc++.h>
using namespace std;
long long f[6];
const int maxn = 1e6 + 5;
long long dp[maxn];
vector<int> g;
int pw[6] = {1, 10, 100, 1000, 10000, 100000};
int td[10] = {0, 0, 0, 1, 0, 0, 2, 0, 0, 3};
const long long inf = 0x3f3f3f3f3f3f3f3f;
int main() {
  int k;
  scanf("%d", &k);
  g.clear();
  int num = 3 * (k - 1);
  int cur = 0;
  for (int i = (0); i < (23); ++i) {
    int t = (1 << i);
    if (cur + t > num) {
      g.push_back(num - cur);
      break;
    } else {
      g.push_back(t);
      cur += t;
    }
  }
  for (int i = (0); i < (6); ++i) cin >> f[i];
  for (int i = (0); i < (maxn); ++i) dp[i] = -inf;
  dp[0] = 0;
  for (int d = 0; d < 6; ++d) {
    for (int x : g) {
      long long cur = 3 * x * (long long)pw[d];
      long long w = (long long)x * f[d];
      for (int i = maxn - 1; i >= cur; --i) dp[i] = max(dp[i], dp[i - cur] + w);
    }
  }
  int q;
  cin >> q;
  while (q--) {
    int n;
    scanf("%d", &n);
    long long ans = 0;
    for (int i = (0); i < (n + 1); ++i) {
      int cur = i;
      long long w = 0;
      for (int d = (0); d < (6); ++d) {
        int a = cur % 10;
        cur /= 10;
        w += td[a] * f[d];
      }
      ans = max(ans, dp[n - i] + w);
    }
    printf("%lld\n", ans);
  }
}
