#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  while (t--) {
    int k;
    cin >> k;
    int days[14];
    int ones = 0;
    for (int i = 0; i < 7; i++) {
      cin >> days[i];
      if (days[i]) ones++;
      days[i + 7] = days[i];
    }
    int ans = 0;
    int left = 0;
    if (k % ones == 0) {
      ans = 7 * (k / ones - 1);
      left = ones;
    } else {
      ans = 7 * (k / ones);
      left = k % ones;
    }
    int c = 0, mini = INT_MAX;
    for (int i = 0; i < 14; i++) {
      if (!days[i]) continue;
      int c = 0, c2 = 0;
      for (int j = i; j < 14; j++) {
        if (days[j] == 1) c++;
        c2++;
        if (c == left) {
          mini = min(mini, c2);
          break;
        }
      }
    }
    cout << mini + ans << endl;
  }
}
