#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ;
  long long int t;
  cin >> t;
  while (t--) {
    long long int k;
    cin >> k;
    long long int a[7];
    long long int week = 0;
    for (long long int i = 0; i < 7; i++) {
      cin >> a[i];
      if (a[i]) week++;
    }
    long long int mn = 100000000000000007, k1 = k;
    ;
    for (long long int i = 0; i < 7; i++) {
      long long int st = i;
      k = k1;
      long long int thisweek = 0;
      for (long long int j = st; j < 7; j++)
        if (a[j]) thisweek++;
      if (k <= thisweek) {
        long long int x = 0;
        for (long long int j = st; j < 7; j++) {
          if (a[j]) x++;
          if (x == k) {
            mn = min(mn, j - st + 1);
            break;
          }
        }
      } else {
        long long int x = 0;
        k -= thisweek;
        x += 7 - st;
        x += 7 * ((k - 1) / week);
        long long int lft = k % week;
        if (lft == 0) lft = week;
        for (long long int j = 0; j < 7; j++) {
          if (a[j]) lft--;
          if (lft == 0) {
            x += j + 1;
            break;
          }
        }
        mn = min(mn, x);
      }
    }
    cout << mn << '\n';
  }
}
