#include <bits/stdc++.h>
using namespace std;
void sol() {
  int k;
  int a[7], day = 0, b[7];
  cin >> k;
  int ans = 7 * k;
  for (int &i : a) cin >> i, day += i;
  for (int i = 0; i < 7; ++i) {
    if (!a[i]) continue;
    int cnt = 0;
    for (int j = 1; j <= 7; ++j) {
      if (a[(i + j - 1) % 7]) {
        ++cnt;
        b[cnt] = j;
      }
    }
    int tmp = (k - 1) / day;
    ans = min(ans, tmp * 7 + b[k - tmp * day]);
  }
  cout << ans << '\n';
}
int main() {
  int t;
  cin >> t;
  while (t--) {
    sol();
  }
}
