#include <bits/stdc++.h>
using namespace std;
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
  cout << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
  const char* comma = strchr(names + 1, ',');
  cout.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cout << setprecision(25);
  long long t;
  cin >> t;
  while (t--) {
    long long a[7], k, s = 0;
    cin >> k;
    for (long long i = 0; i < 7; i++) cin >> a[i], s += a[i];
    long long ans = 1e18;
    for (long long i = 0; i < 7; i++) {
      long long st = 0, en = 1e10, poss = -1;
      while (st <= en) {
        long long m = st + en >> 1;
        long long x = m % 7, can = s * (m / 7);
        for (long long j = 0; j < x; j++) can += a[(i + j) % 7];
        if (can >= k)
          poss = m, en = m - 1;
        else
          st = m + 1;
      }
      ans = min(ans, poss);
    }
    cout << ans << "\n";
  }
  return 0;
}
