#include <bits/stdc++.h>
using namespace std;
int T;
int main() {
  cin >> T;
  while (T--) {
    int k, a[9], sum = 0, num;
    cin >> k;
    for (int i = 1; i <= 7; i++) {
      cin >> a[i];
      sum += a[i];
    }
    int ans = 0x3f3f3f;
    for (int i = 1; i <= 7; i++) {
      num = k % sum + sum;
      int tot = 0x3f3f3f;
      int j = i;
      for (tot = 1;; tot++) {
        if (a[j]) num--;
        j = j % 7 + 1;
        if (num == 0) break;
      }
      ans = min(ans, tot);
    }
    cout << ans + (k / sum - 1) * 7 << endl;
  }
}
