#include <bits/stdc++.h>
using namespace std;
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
int lcm(int a, int b) { return a / gcd(a, b) * b; }
long long powmod(long long a, long long b, long long MOD) {
  long long ans = 1;
  while (b) {
    if (b % 2) ans = ans * a % MOD;
    a = a * a % MOD;
    b /= 2;
  }
  return ans;
}
int main() {
  ios::sync_with_stdio(false);
  int t;
  for (cin >> t; t; t--) {
    int k, cnt = 0, a[8];
    cin >> k;
    for (int i = 1; i <= 7; i++) cin >> a[i], cnt += a[i] == 1;
    if (k <= cnt) {
      int ans = 333333;
      for (int j = 1; j <= 7; j++) {
        int s = k;
        int mid = 0;
        for (int k = j; s;) {
          mid++;
          if (a[k]) s--;
          if (k == 7)
            k = 1;
          else
            k++;
        }
        ans = min(ans, mid);
      }
      cout << ans << endl;
      continue;
    }
    if (k % cnt == 0) {
      int ans = 2e9;
      for (int j = 1; j <= 7; j++) {
        int s = cnt;
        int mid = 0;
        for (int k = j; s;) {
          mid++;
          if (a[k]) s--;
          if (k == 7)
            k = 1;
          else
            k++;
        }
        ans = min(ans, (k - cnt) / cnt * 7 + mid);
      }
      cout << ans << endl;
      continue;
    }
    int ans = 2e9, q = k % cnt;
    for (int i = 1; i <= 7; i++) {
      int mid = 0, s = q;
      for (int j = i; s;) {
        mid++;
        if (a[j]) s--;
        if (j == 7)
          j = 1;
        else
          j++;
      }
      ans = min(ans, k / cnt * 7 + mid);
    }
    cout << ans << endl;
  }
  return 0;
}
