#include <bits/stdc++.h>
using namespace std;
int main() {
  long long test;
  scanf("%lld", &test);
  while (test--) {
    long long n;
    scanf("%lld", &n);
    long long ara[20];
    long long tot = 0;
    for (long long i = 1; i <= 7; i++) {
      scanf("%lld", &ara[i]);
      ara[i + 7] = ara[i];
      tot += ara[i];
    }
    long long out = 99999999999999999;
    for (long long pos = 1; pos <= 7; pos++) {
      if (ara[pos] == 1) {
        long long day = (n / tot);
        if (n % tot == 0) day--;
        long long ase = day * tot;
        day *= 7;
        for (long long i = pos; i < pos + 7; i++) {
          ase += ara[i];
          day++;
          if (ase == n) break;
        }
        out = min(out, day);
      }
    }
    printf("%lld\n", out);
  }
}
