#include <bits/stdc++.h>
using namespace std;
int d[8];
int main() {
  int t;
  cin >> t;
  while (t--) {
    int k, fuck = 1000000000;
    cin >> k;
    vector<int> v;
    for (int i = 1; i <= 7; i++) {
      int x;
      cin >> x;
      d[i] = x;
      if (x == 1) v.push_back(i);
    }
    int have = v.size();
    for (int i = 0; i < v.size(); i++) {
      int start = v[i];
      int ans = 0;
      int kk = k;
      for (int j = start; j <= 7 && kk; j++) {
        if (d[j] == 1) kk--;
        ans++;
      }
      if (kk <= have) {
        for (int l = 1; l <= 7 && kk; l++) {
          if (d[l] == 1) kk--;
          ans++;
        }
        fuck = min(fuck, ans);
      } else {
        ans += ((kk / have) * 7);
        if (kk % have == 0) {
          for (int l = 7; l > 0; l--) {
            if (d[l] == 1) break;
            ans--;
          }
        } else {
          int need = kk % have;
          for (int l = 1; l <= 7 && need; l++) {
            if (d[l] == 1) need--;
            ans++;
          }
        }
        fuck = min(fuck, ans);
      }
    }
    cout << fuck << endl;
    v.clear();
  }
}
