#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 70;
const int INF = 2.1e9;
const int MOD = 1e9 + 7;
int n, m, k;
int a[20];
int main() {
  int T;
  cin >> T;
  while (T--) {
    cin >> n;
    int cnt = 0;
    for (int i = 0; i < 7; i++) {
      cin >> a[i];
      if (a[i]) cnt++;
    }
    for (int i = 7; i < 13; i++) a[i] = a[i - 7];
    int ans = 7 * ((n - 1) / cnt);
    n = (n - 1) % cnt + 1;
    for (int l = 1; l <= 7; l++) {
      int mx = 0;
      for (int i = 0; i <= 13 - l + 1; i++) {
        int s = 0;
        for (int j = i; j < i + l; j++)
          if (a[j]) s++;
        mx = max(mx, s);
      }
      if (mx == n) {
        ans += l;
        break;
      }
    }
    cout << ans << endl;
  }
  return 0;
}
