#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int T, K, A[7];
  for (cin >> T; T--;) {
    cin >> K;
    int total = 0;
    for (int i = 0; i < (7); ++i) cin >> A[i], total += A[i];
    int full = max(K / total - 1, 0);
    int ans = numeric_limits<int>::max();
    for (int i = 0; i < (7); ++i)
      if (A[i] == 1) {
        int curD = full * 7;
        int curK = full * total;
        for (int k = 0; curK < K && k <= 14; ++k) {
          ++curD;
          curK += A[(i + k) % 7];
        }
        ans = min(ans, curD);
      }
    cout << ans << endl;
  }
  return 0;
}
