#include <bits/stdc++.h>
using namespace std;
int a[156], T, n, k, ans, lw, que[100];
int main() {
  int T;
  cin >> T;
  while (T--) {
    cin >> k;
    lw = 0;
    for (int i = 1; i <= 7; i++) {
      cin >> a[i];
      a[i + 7] = a[i];
      lw += a[i];
    }
    int n = k / lw;
    if (k % lw == 0) {
      int maxx = 0, ll = 0;
      for (int i = 1; i <= 14; i++) {
        if (a[i] == 0) {
          ll++;
          maxx = max(maxx, ll);
        } else
          ll = 0;
      }
      ans = n * 7 - maxx;
    } else {
      int yu = k % lw, front = 0, ll = 0, minn = 1000;
      queue<int> q;
      while (!q.empty()) q.pop();
      for (int i = 1; i <= 14; i++) {
        if (a[i] == 0) continue;
        if (a[i] == 1) {
          ll++;
          q.push(i);
          if (ll >= yu) {
            front = q.front();
            q.pop();
            minn = min(i - front + 1, minn);
          }
        }
      }
      ans = n * 7 + minn;
    }
    cout << ans << endl;
  }
}
