#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vl = vector<long long>;
using vll = vector<vector<long long>>;
using vi = vector<int>;
using vii = vector<vector<int>>;
using vpii = vector<pair<int, int>>;
bool comp(pair<int, int> a, pair<int, int> b) {
  if (a.first < b.first)
    return true;
  else if (a.second < b.second)
    return true;
  return false;
}
int main() {
  cin.sync_with_stdio(false);
  cout.sync_with_stdio(false);
  int t, q, n, k, temp, c, m, x;
  cin >> t;
  for (int i = 0; i < int(t); i++) {
    cin >> k;
    int days[7][7], count = 0;
    vii dd(7);
    vi v;
    for (int j = 0; j < int(7); j++) {
      cin >> days[0][j];
      if (days[0][j] == 1) {
        count++;
        v.push_back(j);
        dd[0].push_back(j);
      }
    }
    int step = 1;
    while (step <= 6) {
      for (int j = int(0); j < int(7); j++) {
        days[step][j] =
            (j + step) <= 6 ? days[0][j + step] : days[0][j + step - 7];
        if (days[step][j] == 1) dd[step].push_back(j);
      }
      step++;
    }
    int div = 7 * (k / count), mod = k % count;
    int res = 2 * 1000 * 1000 * 1000;
    for (int j = 0; j < int(7); j++) {
      int tmp = div - dd[j][0];
      if (mod == 0)
        tmp -= (6 - dd[j][dd[j].size() - 1]);
      else
        tmp += dd[j][mod - 1] + 1;
      res = min(res, tmp);
    }
    cout << res << endl;
  }
  return 0;
}
