#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const long long LINF = 0x3f3f3f3f3f3f3f3f;
const int MOD = 1e9 + 7;
const int N = (int)1e6 + 7;
int k, a[20], pre[20];
int num;
void Init() {
  scanf("%d", &k);
  for (int i = (1); i < (8); ++i) {
    scanf("%d", a + i);
    a[i + 7] = a[i];
  }
  num = 0;
  for (int i = (1); i < (8); ++i)
    if (a[i]) num++;
  pre[0] = 0;
  for (int i = (1); i < (15); ++i) pre[i] = pre[i - 1] + a[i];
}
bool check(long long M) {
  long long res = M / 7 * num, tmp = 0, t = M % 7;
  if (t) {
    for (int i = (1); i < (8); ++i)
      tmp = max(tmp, (long long)pre[i + t - 1] - pre[i - 1]);
    res += tmp;
  }
  return res >= k;
}
int Solve() {
  long long L = k, R = LINF;
  while (L < R) {
    long long M = L + (R - L >> 1);
    if (check(M))
      R = M;
    else
      L = M + 1;
  }
  return printf("%lld\n", L);
}
int main() {
  int t;
  scanf("%d", &t);
  while (t--) {
    Init();
    Solve();
  }
  return 0;
}
