#include <bits/stdc++.h>
using namespace std;
int a[14];
int main() {
  int T;
  cin >> T;
  while (T--) {
    int n, sum = 0;
    scanf("%d", &n);
    for (int i = 0; i < 7; i++) {
      scanf("%d", &a[i]);
      sum += a[i];
      a[i + 7] = a[i];
    }
    int ans = 1e9;
    for (int i = 0; i < 7; i++) {
      if (a[i] == 0) continue;
      int cnt = (n - 1) / sum * 7;
      int tmpn = n - (n - 1) / sum * sum;
      for (int j = i; j < i + 7 && tmpn; j++, cnt++) tmpn -= a[j];
      ans = min(ans, cnt);
    }
    printf("%d\n", ans);
  }
  return 0;
}
