#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
const long long mx = 2e6 + 10;
int posx[] = {1, -1, 0, 0};
int posy[] = {0, 0, 1, -1};
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t = 1, n, k, m, a, b, c, d;
  cin >> t;
  while (t--) {
    cin >> k;
    vector<int> v(7), one;
    for (int i = 0; i < 7; i++) {
      cin >> v[i];
      if (v[i] == 1) one.push_back(i);
    }
    long long ans = INT_MAX;
    for (int i = 0; i < one.size(); i++) {
      long long cur = 0, tmp = k;
      if ((one.size() - i) < k) {
        cur += (7 - one[i]);
        k -= (one.size() - i);
        long long x = (k / one.size());
        if (k % one.size() == 0) x--;
        cur += (x * 7ll);
        x = (k % one.size());
        if (x == 0) x = one.size();
        x--;
        cur += (one[x] + 1);
      } else {
        cur += (one[i + k - 1] + 1 - one[i]);
      }
      k = tmp;
      ans = min(ans, cur);
    }
    cout << ans << '\n';
  }
  return 0;
}
