#include <bits/stdc++.h>
using namespace std;
int test(int a[7], int k, int s) {
  int q = k / s, td = 0;
  int v = q * s, i = 0;
  td = 7 * q;
  while (a[i] == 0) td--, i++;
  if (k <= v) {
    i = 6;
    while (a[i] == 0) td--, i--;
  } else {
    i = 0;
    while (k > v) td++, v += a[i], i++;
  }
  return td;
}
void permut(int a[7]) {
  int aux = a[0];
  for (int i = 0; i < 6; i++) a[i] = a[i + 1];
  a[6] = aux;
}
int metatest() {
  int k, a[7], s = 0;
  scanf("%d", &k);
  for (int i = 0; i < 7; i++) scanf("%d", &a[i]), s += a[i];
  int mintd = 1e9 + 1;
  for (int i = 0; i < 7; i++) {
    mintd = min(mintd, test(a, k, s));
    permut(a);
  }
  return mintd;
}
int main() {
  int T;
  cin >> T;
  for (int i = 0; i < T; i++) printf("%d\n", metatest());
}
