#include <bits/stdc++.h>
using namespace std;
int a[100];
int main(int argc, char const *argv[]) {
  int t;
  scanf("%d", &t);
  while (t--) {
    int k, sum = 0;
    scanf("%d\n", &k);
    for (int i = 0; i < 7; ++i) {
      scanf("%d", a + i);
      sum += a[i];
    }
    int ans = 1e9;
    for (int i = 0; i < 7; ++i) {
      for (int j = 0; j < 7; ++j) {
        int now = 0;
        for (int p = i; p <= 6; ++p) {
          now += a[p];
        }
        for (int p = 0; p <= j; ++p) {
          now += a[p];
        }
        int prev = k - now;
        int weeks = prev / sum;
        if (weeks * sum < prev) {
          weeks++;
        }
        weeks++;
        int ax = weeks * 7 + 1 - i + j;
        if (ax <= ans) {
          ans = ax;
        }
      }
    }
    printf("%d\n", ans);
  }
  return 0;
}
