#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long q;
  cin >> q;
  while (q--) {
    long long k, finalans = INT_MAX;
    cin >> k;
    long long arr[7], cnt = 0;
    for (int i = 0; i < 7; i++) {
      cin >> arr[i];
      cnt += (arr[i] == 1 ? 1 : 0);
    }
    for (int i = 0; i < 7; i++) {
      if (arr[i] == 0) continue;
      long long perc = (k % cnt == 0) ? cnt : (k % cnt);
      long long quo = (k >= cnt) ? ((k - perc) / cnt) : 0;
      long long ans = quo * 7, j = i;
      while (perc != 0) {
        ans++;
        if (arr[j % 7] == 1) perc--;
        j++;
      }
      finalans = min(finalans, ans);
    }
    cout << finalans << endl;
  }
}
