#include <bits/stdc++.h>
using namespace std;
signed main() {
  long long t;
  cin >> t;
  while (t--) {
    long long k;
    cin >> k;
    long long a[15];
    a[0] = 0;
    for (long long i = 1; i <= 7; i++) {
      cin >> a[i];
      a[i + 7] = a[i];
    }
    long long sum = accumulate(a + 1, a + 8, 0);
    for (long long i = 1; i <= 14; i++) a[i] += a[i - 1];
    long long ans = 1ll * 1000 * 1000 * 1000 * 1000;
    for (long long i = 0; i <= 6; i++) {
      for (long long j = i + 1; j <= 7; j++) {
        if (a[j] - a[i] == k) {
          ans = min(ans, j - i);
        }
      }
    }
    for (long long i = 0; i <= 6; i++) {
      for (long long j = 7; j <= 14; j++) {
        if (a[j] - a[i] <= k) {
          long long rem = k - (a[j] - a[i]);
          if (rem % sum) continue;
          ans = min(ans, j - i + (rem / sum) * 7);
        }
      }
    }
    cout << ans << endl;
  }
}
