#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
bool a[10];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t;
  cin >> t;
  while (t--) {
    int k, Ans = 1e9, cnt = 0, s = 0;
    cin >> k;
    for (int i = 0; i < 7; i++) cin >> a[i], s += a[i];
    int y = (k - 1) / s * 7;
    int c = (k - 1) % s;
    Ans = y;
    y = 1e9;
    for (int i = 0; i < 7; i++) {
      int g = i;
      cnt = 0;
      c = (k - 1) % s + 1;
      while (c) {
        c -= a[g];
        g++;
        g %= 7;
        cnt++;
      }
      y = min(y, cnt);
    }
    cout << Ans + y << "\n";
  }
}
