#include <bits/stdc++.h>
using namespace std;
long long mod = 1e9 + 7;
long long INF = 1e18;
bool isPrime(long long n) {
  if (n <= 1) return false;
  if (n <= 3) return true;
  if (n % 2 == 0 || n % 3 == 0) return false;
  for (long long i = 5; i * i <= n; i = i + 6)
    if (n % i == 0 || n % (i + 2) == 0) return false;
  return true;
}
long long nCr(long long n, long long r) {
  long long res = 1;
  if (n < r) return -1;
  r = min(r, n - r);
  for (long long i = 0; i < r; i++) {
    res *= (n - i);
    res /= (i + 1);
  }
  return res;
}
long long nPr(long long n, long long r) {
  long long res = 1;
  if (n < r) return -1;
  for (long long i = 0; i < r; i++) res *= (n - i);
  return res;
}
void solve() {
  long long k;
  cin >> k;
  vector<long long> v(7);
  long long t = 0;
  for (long long i = 0; i < 7; i++) {
    cin >> v[i];
    if (v[i] == 1) t++;
  }
  long long ans = INF;
  if (k == 1) {
    cout << 1 << "\n";
    return;
  }
  if (t == 1) {
    long long ans = (k - 1) * 7 + 1;
    cout << ans << "\n";
    return;
  }
  for (long long i = 0; i < 7; i++) {
    long long d, c;
    if (k > t) {
      if (k % t == 0) {
        d = (k / t - 1) * 7;
        c = t;
      } else {
        d = (k / t) * 7;
        c = k % t;
      }
    } else {
      d = 0;
      c = k;
    }
    long long ind = i;
    while (c > 0) {
      if (v[ind] == 0)
        d++;
      else if (v[ind] == 1) {
        d++;
        c--;
      }
      if (ind == 6)
        ind = 0;
      else
        ind++;
    }
    ans = min(ans, d);
  }
  cout << ans << "\n";
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long t = 1;
  cin >> t;
  while (t--) solve();
  return 0;
}
