#include <bits/stdc++.h>
using namespace std;
const long long N = 1000000;
const long long INF = (long long)(1e9);
const long long mod = 998244353;
const double eps = 1e-9;
long long n, k, pref[10], suff[10], sum, t, a[10];
pair<long long, long long> p[N];
vector<long long> v;
signed main() {
  ios_base ::sync_with_stdio(false);
  cin.tie(0);
  cin >> t;
  for (long long ii = 1; ii <= t; ++ii) {
    memset(pref, 0, sizeof pref);
    memset(suff, 0, sizeof suff);
    sum = 0;
    cin >> k;
    for (long long i = 1; i <= 7; ++i) {
      cin >> a[i];
      sum += a[i];
      pref[i] = pref[i - 1] + a[i];
    }
    for (long long i = 7; i >= 1; --i) {
      suff[i] = suff[i + 1] + a[i];
    }
    if (k == 1) {
      cout << 1 << "\n";
      continue;
    }
    long long ans = INF * INF;
    for (long long i = 1; i <= 7; ++i) {
      for (long long j = 1; j <= 7; ++j) {
        if (j > i && pref[j] - pref[i - 1] == k) {
          ans = min(ans, j - i + 1);
        }
        long long cur = k - pref[i] - suff[j];
        if (cur < 0 || cur % sum != 0) continue;
        long long val = (cur / sum) * 7 + i + (7 - j + 1);
        ans = min(ans, val);
      }
    }
    cout << ans << "\n";
  }
  return 0;
}
