#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main(int argc, char *argv[]) {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t;
  ll k, wk, aw;
  int ad, n;
  bool a[7];
  cin >> t;
  while (t--) {
    cin >> k;
    wk = 0;
    for (int i = 0; i < 7; i++) {
      cin >> a[i];
      if (a[i]) wk++;
    }
    aw = ll((k - 1) / wk);
    ad = 0;
    n = int(k - aw * wk);
    if (n > 0) {
      ad = 7;
      for (int f = 0, tad, tn; f < 7; f++) {
        tad = 0;
        tn = 0;
        for (int i = 0; i < 7; i++) {
          if (tn == n) break;
          if (a[(f + i) % 7]) tn++;
          tad++;
        }
        ad = min(ad, tad);
      }
    }
    cout << aw * 7 + ad << "\n";
  }
  return 0;
}
