#include <bits/stdc++.h>
using namespace std;
void solve() {
  int k, cnt = 0, add = 7, ans = 0;
  cin >> k;
  bool a[7] = {};
  for (int i = 0; i < 7; ++i) cin >> a[i], cnt += a[i];
  --k;
  ans += k / cnt * 7;
  k -= k / cnt * cnt;
  ++k;
  for (int i = 0; i < 7; ++i) {
    int tmp = k, j = 0;
    while (tmp) {
      tmp -= a[(i + j) % 7];
      ++j;
    }
    add = min(add, j);
  }
  cout << ans + add << '\n';
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  ;
  int t = 1;
  cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}
