#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int test;
  scanf("%lld", &test);
  while (test--) {
    long long int val[8];
    long long int arr[8];
    long long int day;
    scanf("%lld", &day);
    long long int tot = 0;
    for (long long int i = 1; i <= 7; i++) {
      scanf("%lld", &arr[i]);
      tot += arr[i];
    }
    long long int ans = 7 * (day / tot);
    ans -= 7;
    long long int baki = day % tot;
    baki += tot;
    long long int ex = INT_MAX;
    long long int tem = baki;
    for (long long int j = 1; j <= 7; j++) {
      long long int cnt = 0;
      for (long long int i = j; i <= 7 && baki > 0; i++) {
        cnt++;
        baki -= arr[i];
      }
      for (long long int i = 1; i < j && baki > 0; i++) {
        cnt++;
        baki -= arr[i];
      }
      for (long long int i = j; i <= 7 && baki > 0; i++) {
        cnt++;
        baki -= arr[i];
      }
      for (long long int i = 1; i < j && baki > 0; i++) {
        cnt++;
        baki -= arr[i];
      }
      ex = min(cnt, ex);
      baki = tem;
    }
    printf("%lld\n", ans + ex);
  }
  return 0;
}
