#include <bits/stdc++.h>
using namespace std;
signed main() {
  long long t;
  cin >> t;
  while (t--) {
    long long k;
    long long a[14];
    cin >> k;
    long long one = 0;
    for (long long i = 0; i < 7; i++) {
      cin >> a[i];
      if (a[i]) one++;
      a[i + 7] = a[i];
    }
    if (one == 1) {
      cout << (k - 1) * 7 + 1 << endl;
      continue;
    }
    long long temp[one + 1];
    for (long long i = 1; i <= one; i++) {
      temp[i] = INT_MAX;
      for (long long j = 0; j < 7; j++) {
        long long cnt = i;
        long long c = 0;
        for (long long k = j; k < 14; k++) {
          if (a[k]) cnt--;
          c++;
          if (!cnt) break;
        }
        temp[i] = min(c, temp[i]);
      }
    }
    if (k <= one) {
      cout << temp[k] << endl;
      continue;
    }
    long long res = (k / one) * 7;
    if ((k % one) == 0 and res > 0) res -= 7;
    if (k % one == 0)
      res += temp[one];
    else
      res += temp[k % one];
    cout << res << endl;
  }
  return 0;
}
