#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  int days[7];
  while (t--) {
    int k;
    cin >> k;
    int sum = 0;
    int ret = 0;
    for (int i = 0; i < 7; i++) {
      cin >> days[i];
      sum += days[i];
    }
    ret += (k / sum) * 7;
    int remain = k % sum;
    if (ret) {
      ret -= 7;
      remain += sum;
    }
    if (!remain) {
      cout << ret << endl;
      continue;
    }
    int ad = 14;
    for (int i = 0; i < 7; i++) {
      int adaux = 0;
      int r = remain;
      int j = i;
      while (r) {
        if (days[j]) {
          r--;
        }
        adaux++;
        j++;
        j %= 7;
      }
      ad = min(ad, adaux);
    }
    cout << ret + ad << endl;
  }
  return 0;
}
