#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
inline int read() {
  int x = 0, w = 1;
  char ch = 0;
  while (ch < '0' || ch > '9') {
    ch = getchar();
    if (ch == '-') w = -1;
  }
  while (ch <= '9' && ch >= '0') {
    x = (x << 1) + (x << 3) + ch - '0';
    ch = getchar();
  }
  return x * w;
}
int n, a[10], s[10];
int main() {
  int T = read();
  while (T--) {
    n = read();
    for (int i = 1; i <= 7; ++i) s[i] = s[i - 1] + (a[i] = read());
    int ans = 2e9;
    for (int i = 0; i <= 7; ++i) {
      for (int j = 0; j <= 7; ++j) {
        int k = n - s[j] - (s[7] - s[i]);
        int c = (k / s[7]) + (k % s[7] != 0);
        c *= 7;
        c += 7 - i + j;
        ans = min(ans, c);
      }
    }
    printf("%d\n", ans);
  }
  return 0;
}
