#include <bits/stdc++.h>
using namespace std;
const int N = 999999;
const int M = N / 9;
int k, q;
long long F[6], dp[6][M + 1], dp2[6][N + 1];
vector<pair<int, long long>> dp1;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> k;
  for (int i = 0; i < 6; i++) cin >> F[i];
  cin >> q;
  if (k == 1) {
    while (q--) {
      int n;
      cin >> n;
      long long sol = 0;
      for (int i = 0; i < 6; i++) {
        if (n % 10 == 3)
          sol += F[i];
        else if (n % 10 == 6)
          sol += 2 * F[i];
        else if (n % 10 == 9)
          sol += 3 * F[i];
        n /= 10;
      }
      cout << sol << "\n";
    }
    return 0;
  }
  for (int i = 0; i < 6; i++)
    for (int j = 0; j <= M; j++) dp[i][j] = -1;
  int l = k - 2;
  for (int i = 0; i <= min(l, M); i++) dp[0][i] = i * F[0];
  if (F[1] >= 10 * F[0]) {
    for (int i = 0; i < l; i++)
      for (int j = 0; j < 10 && j <= l && 10 * i + j <= M; j++)
        dp[1][10 * i + j] = i * F[1] + j * F[0];
    for (int j = 0; j <= l && 10 * l + j <= M; j++)
      dp[1][10 * l + j] = l * F[1] + j * F[0];
  } else {
    for (int j = 0; j <= min(l, M); j++) dp[1][j] = j * F[0];
    for (int i = 1; i <= l; i++)
      for (int j = max(0, l - 9); j <= l && 10 * i + j <= M; j++)
        dp[1][10 * i + j] = i * F[1] + j * F[0];
  }
  long long p = 10;
  for (int i = 2; i < 6; i++) {
    p *= 10;
    for (int n = 0; n <= M; n++)
      for (int j = 0; j <= l && p * j <= n; j++)
        if (dp[i - 1][n - p * j] != -1)
          dp[i][n] = max(dp[i][n], j * F[i] + dp[i - 1][n - p * j]);
  }
  for (int n = 0; n <= M; n++)
    if (dp[5][n] != -1) dp1.push_back({9 * n, 3 * dp[5][n]});
  for (int i = 0; i < 6; i++)
    for (int j = 0; j <= N; j++) dp2[i][j] = -1;
  int mult[19];
  for (int j = 0; j < 19; j++) {
    mult[j] = (j % 3 == 0 ? j / 3 : min(j / 3, 3));
    dp2[0][j] = mult[j] * F[0];
  }
  p = 1;
  for (int i = 1; i < 6; i++) {
    p *= 10;
    for (int n = 0; n <= N; n++)
      for (int j = 0; j <= 18 && p * j <= n; j++)
        if (dp2[i - 1][n - p * j] != -1)
          dp2[i][n] = max(dp2[i][n], dp2[i - 1][n - p * j] + mult[j] * F[i]);
  }
  while (q--) {
    int n;
    cin >> n;
    long long sol = 0;
    for (int i = 0; i < dp1.size() && dp1[i].first <= n; i++) {
      int j = n - dp1[i].first;
      if (dp2[5][j] != -1) sol = max(sol, dp1[i].second + dp2[5][j]);
    }
    cout << sol << "\n";
  }
  return 0;
}
