#include <bits/stdc++.h>
using namespace std;
int a[7];
int t;
long long k;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> t;
  for (long long kas = (0); kas < (t); ++kas) {
    cin >> k;
    int cnt = 0;
    for (long long i = (0); i < (7); ++i) {
      cin >> a[i];
      if (a[i]) cnt++;
    }
    long long ans = max(k / cnt - 2, 1ll * 0), remain = k - ans * cnt;
    ans *= 7;
    int add = 100;
    for (long long i = (0); i < (7); ++i) {
      if (a[i]) {
        int ok = 1, fans = 1;
        for (int j = (i + 1) % 7; ok < remain; j = (++j) % 7) {
          if (a[j]) ok++;
          fans++;
        }
        add = min(add, fans);
      }
    }
    cout << ans + add << endl;
  }
  return 0;
}
