#include <bits/stdc++.h>
using namespace std;
int main() {
  long long t;
  cin >> t;
  while (t--) {
    long long k;
    cin >> k;
    long long mn = INT_MAX;
    long long a[8];
    a[0] = 0;
    map<long long, long long> mp;
    for (long long i = 1; i <= 7; i++) {
      cin >> a[i];
      mp[i] = a[i];
      a[i] += a[i - 1];
    }
    long long m = k;
    long long ans = 0;
    if (m <= a[7]) {
      for (long long i = 1; i <= 7; i++) {
        long long ck = 0;
        ans = 0;
        for (long long j = i; j <= 14; j++) {
          if (ck == m) break;
          if (j != 7 && mp[j % 7] == 1) ck++;
          if (j == 7 && mp[j] == 1) ck++;
          ans++;
        }
        mn = min(mn, ans);
      }
      cout << mn << endl;
      continue;
    }
    for (long long i = 1; i <= 7; i++) {
      m = k;
      ans = 0;
      long long tmp = a[7] - a[i - 1];
      ans += 7 - i + 1;
      m -= tmp;
      long long tk = m / a[7];
      long long rem = m % a[7];
      if (rem == 0) {
        ans += (tk - 1) * 7;
        rem = a[7];
        long long ram = 0;
        for (long long j = 1; j <= 7; j++) {
          if (ram == rem) break;
          if (mp[j] == 1) ram++;
          ans++;
        }
        mn = min(mn, ans);
      }
      if (rem != 0) {
        ans += (tk)*7;
        long long ram = 0;
        for (long long j = 1; j <= 7; j++) {
          if (ram == rem) break;
          if (mp[j] == 1) ram++;
          ans++;
        }
        mn = min(mn, ans);
      }
    }
    cout << mn << endl;
  }
}
