#include <bits/stdc++.h>
using namespace std;
const int INF = (int)(1e9 + 1337);
const double EPS = (double)(1e-7);
int k;
int a[10];
int s[10];
inline int get_sum(int i, int j) {
  if (i > j) {
    return 0;
  }
  return s[j] - s[i - 1];
}
void solve() {
  cin >> k;
  s[0] = 0;
  for (int i = 1; i <= 7; ++i) {
    cin >> a[i];
    s[i] = s[i - 1] + a[i];
  }
  int ans = INF;
  int total = s[7];
  for (int i = 1; i <= 7; ++i) {
    int si = get_sum(i, 7);
    for (int j = 1; j <= 7; ++j) {
      int sij = get_sum(i, j);
      if (sij == k) {
        ans = min(ans, j - i + 1);
      }
      int sj = get_sum(1, j);
      int need = k - si - sj;
      if (need >= 0 && (need % total) == 0) {
        ans = min(ans, 8 - i + j + (need / total) * 7);
      }
    }
  }
  cout << ans << '\n';
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int ts;
  cin >> ts;
  for (int i = 0; i < ts; ++i) {
    solve();
  }
  return 0;
}
