#include <bits/stdc++.h>
using namespace std;
int a[10];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int t;
  cin >> t;
  while (t--) {
    int k;
    cin >> k;
    int inAWeek = 0;
    for (int i = 0; i < 7; ++i) {
      cin >> a[i];
      inAWeek += a[i];
    }
    int res = 0;
    int safeWeeks = k / inAWeek - 1;
    res += safeWeeks * 7;
    k -= safeWeeks * inAWeek;
    int toAdd = 987654321;
    for (int i = 0; i < 7; ++i) {
      int days = 0;
      int curDay = i;
      int curSum = 0;
      while (curSum < k) {
        curSum += a[curDay];
        curDay++;
        curDay %= 7;
        days++;
      }
      toAdd = min(toAdd, days);
    }
    res += toAdd;
    cout << res << "\n";
  }
  return 0;
}
