#include <bits/stdc++.h>
using namespace std;
void DBG() { cerr << "]\n"; }
template <typename T, typename... Args>
void DBG(T first, Args... args) {
  cerr << first;
  if (sizeof...(args)) cerr << ", ";
  DBG(args...);
}
long long ans(long long k, long long pos, long long total,
              vector<long long> &s) {
  long long sum = 0;
  long long d = 0;
  for (long long i = pos; i < 7 && sum < k; i++) {
    sum += s[i];
    d++;
  }
  if (sum == k) {
    return d;
  }
  long long remain = k - sum - 1;
  long long days = 7 - pos;
  long long extra = remain / total;
  remain -= extra * total;
  days += extra * 7;
  remain++;
  for (long long i = 0; i < 7 && remain != 0; i++) {
    remain -= (s[i]);
    days++;
  }
  return days;
}
void solve() {
  long long k;
  cin >> k;
  vector<long long> a(7);
  for (long long i = 0; i < 7; i++) cin >> a[i];
  long long total = 0;
  for (long long i = 0; i < 7; i++) {
    total += a[i];
  }
  long long mn = LONG_MAX;
  for (long long i = 0; i < 7; i++) {
    if (a[i] == 1) {
      mn = min(mn, ans(k, i, total, a));
    }
  }
  cout << mn << "\n";
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  long long t = 1;
  cin >> t;
  for (long long tt = 1; tt <= t; tt++) {
    solve();
  }
}
