#include <bits/stdc++.h>
using namespace std;
int main() {
  int t, n, total, ost, need, day;
  long long ans;
  cin >> t;
  while (t--) {
    day = 7;
    cin >> n;
    vector<int> help(7);
    total = 0;
    for (int i = 0; i < 7; i++) {
      cin >> help[i];
      if (help[i] == 1) total++;
    }
    ost = n % total;
    ans = n / total;
    if (ost == 0) {
      need = total;
      ans--;
    } else
      need = ost;
    for (int i = 0; i < 7; i++) {
      if (help[i] == 1) {
        int pos = i;
        int find = 0;
        while (find != need) {
          if (help[pos % 7] == 1) find++;
          pos++;
        }
        day = min(day, pos - i);
      }
    }
    ans = ans * 7 + day;
    cout << ans << endl;
  }
  return 0;
}
