#include <bits/stdc++.h>
using namespace std;
int arr[20];
int main() {
  int test;
  cin >> test;
  int ans, rest, mn, sm, k;
  while (test--) {
    cin >> k;
    for (int i = 1; i <= 7; i++) {
      cin >> arr[i];
      arr[i + 7] = arr[i];
    }
    for (int i = 1; i <= 14; i++) arr[i] += arr[i - 1];
    if (k % arr[7] == 0) {
      ans = ((k - arr[7]) / arr[7]) * 7;
      rest = arr[7];
    } else {
      ans = (k / arr[7]) * 7;
      rest = k % arr[7];
    }
    mn = INT_MAX;
    for (int i = 1; i <= 7; i++) {
      for (int j = i; j < i + 7; j++) {
        sm = arr[j] - arr[i - 1];
        if (sm == rest) mn = min(mn, j - i + 1);
      }
    }
    cout << ans + mn << endl;
  }
}
