#include <bits/stdc++.h>
using namespace std;
int day[100];
int main() {
  int t, k;
  scanf("%d", &t);
  while (t--) {
    scanf("%d", &k);
    int cnt = 0;
    for (int i = 1; i <= 7; i++) {
      scanf("%d", &day[i]);
      day[i + 7] = day[i];
      cnt += day[i];
    }
    int ans = 1e9;
    for (int i = 1; i <= 7; i++) {
      int n = k;
      for (int j = i; j <= i + 7; j++) {
        n -= day[j];
        if (n % cnt == 0) ans = min(ans, j - i + 1 + n / cnt * 7);
      }
    }
    printf("%d\n", ans);
  }
}
