#include <bits/stdc++.h>
const unsigned long long int prime = 998244353;
const int char_a = 97;
using namespace std;
int main() {
  unsigned int n;
  cin >> n;
  unsigned long long int k;
  for (unsigned int i = 0; i < n; i++) {
    cin >> k;
    vector<int> lec(7);
    int lec_per_week = 0;
    for (int j = 0; j < 7; j++) {
      cin >> lec[j];
      if (lec[j] == 1) {
        lec_per_week++;
      }
    }
    unsigned long long int complete_week = k / lec_per_week;
    unsigned long long int days = complete_week * 7;
    if (lec_per_week == 1) {
      days -= 6;
    } else if (lec_per_week == 7) {
      days = k;
    } else {
      vector<int> gap(2 * lec_per_week);
      int index = -1;
      for (int j = 0; j < 7; j++) {
        if (index == -1) {
          gap[lec_per_week - 1]++;
        } else {
          gap[index]++;
          gap[index + lec_per_week]++;
        }
        if (lec[j] == 1) {
          index++;
        }
      }
      int remainder = k % lec_per_week;
      int min_gap = 7;
      if (remainder == 0) {
        for (int j = 0; j < lec_per_week; j++) {
          int loc_gap = 0;
          for (int k = 0; k < lec_per_week - 1; k++) {
            loc_gap += gap[j + k];
          }
          if (loc_gap < min_gap) {
            min_gap = loc_gap;
          }
        }
        days += min_gap - 6;
      } else {
        for (int j = 0; j < lec_per_week; j++) {
          int loc_gap = 0;
          for (int k = 0; k < remainder - 1; k++) {
            loc_gap += gap[j + k];
          }
          if (loc_gap < min_gap) {
            min_gap = loc_gap;
          }
        }
        days += min_gap + 1;
      }
    }
    cout << days << endl;
  }
}
