#include <bits/stdc++.h>
using namespace std;
long long k;
int a[10];
int main() {
  int tc;
  scanf("%d", &tc);
  while (tc--) {
    scanf("%lld", &k);
    int cake = 0;
    for (int i = 0; i < 7; i++) {
      scanf("%d", a + i);
      cake += a[i];
    }
    long long ans = (max(0LL, k - cake) / cake);
    int rem = k - ans * cake;
    ans *= 7;
    int r = 123;
    for (int s = 0; s < 7; s++) {
      int p = s;
      int need = rem;
      int here = 0;
      while (need != 0) {
        need -= a[p];
        here++;
        p = (p + 1) % 7;
      }
      r = min(r, here);
    }
    printf("%lld\n", ans + r);
  }
  return 0;
}
