#include <bits/stdc++.h>
using namespace std;
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
  cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
  const char* comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
template <typename T1, typename T2>
istream& operator>>(istream& in, pair<T1, T2>& a) {
  in >> a.first >> a.second;
  return in;
}
template <typename T1, typename T2>
ostream& operator<<(ostream& out, pair<T1, T2> a) {
  out << a.first << " " << a.second;
  return out;
}
template <typename T, typename T1>
T amax(T& a, T1 b) {
  if (b > a) a = b;
  return a;
}
template <typename T, typename T1>
T amin(T& a, T1 b) {
  if (b < a) a = b;
  return a;
}
const long long INF = 1e18;
const int32_t M = 1e9 + 7;
const int32_t MM = 998244353;
const long long N = 0;
long long dp[1000000];
long long findp[7][1000000];
void solve() {
  long long k;
  cin >> k;
  long long f[6];
  for (long long i = 0; i < 6; ++i) {
    cin >> f[i];
  }
  vector<pair<long long, long long> > wv;
  long long w = 1;
  for (long long i = 0; i < 6; ++i) {
    long long cnt = 3 * (k - 1);
    for (long long j = 1; j < cnt; j *= 2) {
      cnt -= j;
      wv.push_back({3 * w * j, f[i] * j});
    }
    wv.push_back({3 * w * cnt, f[i] * cnt});
    w *= 10;
  }
  for (long long i = 1; i < 1000000; ++i) {
    dp[i] = -M * 1ll * M;
  }
  for (auto u : wv) {
    long long weight = u.first;
    long long value = u.second;
    __f("weight, value", weight, value);
    for (long long i = 999999; i >= weight; --i) {
      amax(dp[i], dp[i - weight] + value);
    }
  }
  for (long long i = 0; i < 1000000; ++i) {
    for (long long j = 0; j < 7; ++j) {
      findp[j][i] = -1ll * M * M;
    }
  }
  for (long long i = 0; i < 1000000; ++i) {
    findp[0][i] = dp[i];
  }
  w = 1;
  for (long long i = 0; i < 6; ++i) {
    for (long long d = 0; d < 10; ++d) {
      long long val = 0;
      if (d % 3 == 0) val = f[i] * d / 3;
      for (long long x = 0; x < 1000000 - d * w; ++x) {
        amax(findp[i + 1][x + d * w], findp[i][x] + val);
      }
    }
    w *= 10;
  }
  long long q;
  cin >> q;
  for (long long i = 0; i < q; ++i) {
    long long n;
    cin >> n;
    cout << findp[6][n] << "\n";
  }
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long t = 1;
  while (t--) solve();
  return 0;
}
