#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t;
  long long k, cnt;
  int s[10];
  cin >> t;
  for (int i = 1; i <= t; i++) {
    cnt = 0;
    cin >> k;
    for (int j = 0; j < 7; j++) {
      cin >> s[j];
      if (s[j]) cnt++;
    }
    long long ans = k / cnt;
    if (ans * cnt == k) ans--;
    if (ans < 0) ans = 0;
    long long cnt1 = 0;
    for (int j = 0; j < 7; j++) {
      if (s[j]) {
        cnt1++;
        if (cnt1 + (cnt * ans) == k) {
          cnt1 = j + 1;
          break;
        }
      }
    }
    ans *= 7;
    ans += cnt1;
    long long peans = ans;
    for (int it = 1; it <= 7; it++) {
      int tmp = s[0];
      for (int j = 0; j < 6; j++) {
        s[j] = s[j + 1];
      }
      s[6] = tmp;
      ans = k / cnt;
      if (ans * cnt == k) ans--;
      if (ans < 0) ans = 0;
      long long cnt1 = 0;
      for (int j = 0; j < 7; j++) {
        if (s[j]) {
          cnt1++;
          if (cnt1 + (cnt * ans) == k) {
            cnt1 = j + 1;
            break;
          }
        }
      }
      ans *= 7;
      ans += cnt1;
      peans = min(peans, ans);
    }
    cout << peans << "\n";
  }
  return 0;
}
