#include <bits/stdc++.h>
using namespace std;
using TimeWho = pair<int, int>;
using LL = long long;
LL round_up(LL a, LL b) { return (a + b - 1) / b; }
void solve() {
  LL k;
  cin >> k;
  vector<int> arr(7);
  int oneWeek = 0;
  for (int i = 0; i < 7; ++i) {
    cin >> arr[i];
    oneWeek += arr[i];
  }
  LL res = 1e10;
  auto tryUpdate = [&res, k](LL cnt, LL days) {
    if (cnt == k) {
      res = min(res, days);
    }
  };
  for (int i = 0; i < 7; ++i) {
    LL cnt = 0;
    LL days = 0;
    for (int j = i; j < 7; ++j) {
      cnt += arr[j];
      ++days;
      tryUpdate(cnt, days);
    }
    if (cnt < k) {
      LL rem = k - cnt;
      days += (rem - 1) / oneWeek * 7;
      rem -= (rem - 1) / oneWeek * oneWeek;
      tryUpdate(k - rem, days);
      for (int j = 0; j < 7; ++j) {
        rem -= arr[j];
        ++days;
        tryUpdate(k - rem, days);
      }
    }
  }
  cout << res << endl;
}
int main() {
  int t;
  cin >> t;
  while (t--) solve();
}
