#include <bits/stdc++.h>
using namespace std;
long long int t;
long long int pday(long long int &d) {
  if (d == 0) return 6;
  return d - 1;
}
int main() {
  scanf("%lld", &t);
  while (t--) {
    long long int k, a[7], c = 0, ans = 1e9;
    scanf("%lld", &k);
    for (int i = 0; i < 7; ++i) {
      scanf("%lld", a + i);
      if (a[i]) c++;
    }
    for (int i = 0; i < 7; ++i) {
      long long int w = k / c, r = k % c, j = i, d = 0;
      if (r == 0)
        while (!a[pday(j)]) d--, j = pday(j);
      else {
        r = 0;
        while (w * c + r < k) {
          if (a[j]) r++;
          d++;
          j = pday(j);
        }
      }
      ans = min(ans, w * 7 + d);
    }
    printf("%lld\n", ans);
  }
  return 0;
}
