#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  int t;
  cin >> t;
  vector<int> a(7);
  while (t--) {
    long long k;
    cin >> k;
    int sum = 0;
    for (int i = 0; i < 7; i++) {
      cin >> a[i];
      sum += a[i];
    }
    long long ans = (k / sum) * 7;
    long long rem = k % sum;
    if (rem == 0) {
      ans -= 7;
      rem = sum;
    }
    int min = 7;
    int count = 0, count_sum = 0;
    for (int i = 0; i < 7; i++) {
      count = 0;
      count_sum = 0;
      for (int j = 0; j < 7; j++) {
        count++;
        if (a[(i + j) % 7]) {
          count_sum++;
        }
        if (count_sum == rem) {
          if (count < min) {
            min = count;
            break;
          }
        }
      }
    }
    cout << ans + min << endl;
  }
  return 0;
}
