#include <bits/stdc++.h>
using namespace std;
long long a[10], f[10], s;
long long F(int index, int K) {
  if (K <= 1000) {
    long long day = 0;
    int i = index;
    while (K != 0) {
      K = K - a[i];
      day++;
      i++;
      if (i == 8) i = 1;
    }
    return day;
  }
  long long day = 1;
  K--;
  day += (K / s) * 7;
  K = K % s;
  if (K == 0) return day;
  int i = index + 1;
  if (i == 8) i = 1;
  while (K != 0) {
    K = K - a[i];
    day++;
    i++;
    if (i == 8) i = 1;
  }
  return day;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int t;
  cin >> t;
  while (t--) {
    long long k;
    cin >> k;
    s = 0;
    for (int i = 1; i <= 7; i++) {
      cin >> a[i];
      s += a[i];
      if (a[i] == 1)
        f[i] = i;
      else
        f[i] = f[i - 1];
    }
    long long mi = 1e9;
    for (int i = 1; i <= 7; i++) {
      if (a[i] == 1) mi = min(mi, F(i, k));
    }
    cout << mi << endl;
  }
  return 0;
}
