#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  while (t--) {
    long long int r = 0, n, min, val;
    cin >> n;
    int a[7], d = 0, b[7];
    for (int i = 0; i < 7; ++i) {
      cin >> a[i];
      if (a[i] == 1) {
        b[d] = i;
        d++;
      }
    }
    if (n > d) {
      if (n % d == 0) {
        r = ((n / d) - 1) * 7;
        n = d;
      } else {
        r = (n / d) * 7;
        n = n % d;
      }
    }
    if (n != 0) {
      for (int i = 0; i < 7; i++) {
        int c = 0;
        val = 0;
        for (int j = i;;) {
          val++;
          if (a[j] == 1) {
            c++;
          }
          if (c == n) {
            break;
          }
          j++;
          if (j >= 7) {
            j = 0;
          }
        }
        if (i == 0) {
          min = val;
        } else {
          if (min > val) {
            min = val;
          }
        }
      }
    }
    cout << r + min << endl;
  }
}
