#include <bits/stdc++.h>
using namespace std;
const int MAXN = 200005;
const int INF = (long long)1e9 + 5;
int K, a[20], pref[20], suff[20];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int T;
  cin >> T;
  while (T--) {
    cin >> K;
    int num = 0;
    for (int i = 1; i <= 7; i++) {
      cin >> a[i];
      pref[i] = pref[i - 1] + a[i];
      num += a[i];
    }
    for (int i = 7; i >= 1; i--) {
      suff[i] = suff[i + 1] + a[i];
    }
    long long ans = K * 7 + 100;
    for (int i = 1; i <= 7; i++) {
      long long rem = K - suff[i],
                mid = rem % num == 0 ? rem / num - 1 : rem / num;
      long long res = 8 - i + mid * 7;
      rem = rem - num * mid;
      for (int j = i; j <= 7; j++) {
        if (pref[j] - pref[i - 1] >= K) {
          ans = min((long long)j - i + 1, ans);
        }
      }
      for (int j = 1; j <= 7; j++) {
        if (pref[j] >= rem) {
          res += j;
          break;
        }
      }
      ans = min(ans, res);
    }
    cout << ans << "\n";
  }
  return 0;
}
