#include <bits/stdc++.h>
using namespace std;
long long p1 = 1000000007;
long long mod = 998244353;
long long power(long long x, long long y) {
  long long ans = 1;
  x = x % p1;
  while (y > 0) {
    if (y & 1) ans = (ans * x) % p1;
    y = y >> 1;
    x = (x * x) % p1;
  }
  return ans;
}
int main() {
  long long t;
  cin >> t;
  while (t--) {
    long long k;
    cin >> k;
    long long a[7], tot = 0;
    for (long long i = 0; i < 7; i++) {
      cin >> a[i];
      tot += a[i];
    }
    long long ans = LONG_LONG_MAX, day = 0, sum = 0;
    for (long long i = 0; i < 7; i++) {
      sum = 0, day = 0;
      if (a[i] == 1) {
        for (long long j = i; j < 7; j++) {
          sum++;
          day += a[j];
          if (day == k) {
            ans = min(ans, sum);
            break;
          }
        }
        if (day == k) continue;
        if ((k - day) % tot > 0) {
          long long wet = (k - day) / tot;
          sum += 7 * wet;
          day += tot * wet;
        } else if ((k - day) / tot > 0) {
          long long wet = (k - day) / tot;
          sum += 7 * (wet - 1);
          day += tot * (wet - 1);
        }
        for (long long j = 0; j < 7; j++) {
          sum++;
          day += a[j];
          if (day == k) {
            ans = min(ans, sum);
            break;
          }
        }
        if (k == day) continue;
      }
    }
    cout << ans << endl;
  }
  return 0;
}
