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