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