#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  while (t--) {
    long long int k;
    cin >> k;
    vector<int> a(7, 0);
    for (int i = 0; i < 7; i++) cin >> a[i];
    long long int total = accumulate(a.begin(), a.end(), 0);
    long long int full_week = k / total;
    if (k % total == 0) full_week -= 1;
    full_week = max(0ll, full_week);
    k -= full_week * total;
    long long int ans = full_week * 7;
    long long int val = 7;
    for (int i = 0; i < 7; i++) {
      long long int days = 0, ones = 0;
      for (int j = i, cnt = 0; cnt < 7; j = (j + 1) % 7, cnt++) {
        ones += a[j];
        days++;
        if (ones == k) {
          val = min(days, val);
          break;
        }
      }
    }
    cout << ans + val << endl;
  }
}
