#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
const int N = 1e6 + 5;
int main() {
  int t;
  cin >> t;
  while (t--) {
    long long k, first_day;
    int a[8];
    cin >> k;
    for (int i = 1; i < 8; i++) {
      cin >> a[i];
    }
    int sum = 0;
    for (int i = 1; i < 8; i++) {
      if (a[i] == 1) {
        sum++;
      }
    }
    vector<long long> v;
    for (int i = 1; i <= 7; i++) {
      long long x = k;
      if (a[i] == 1) {
        first_day = i;
        long long ans = 0;
        for (int i = first_day; i <= 7; i++) {
          if (x == 0) {
            break;
          }
          ans++;
          if (a[i] == 1) {
            x--;
          }
        }
        long long week = 0, remaining = 0;
        if (x <= sum) {
          remaining = x;
        } else if (x % sum != 0) {
          week = (x / sum);
          remaining = x - week * sum;
          ans += (week)*7;
        } else if (x % sum == 0) {
          week = (x / sum) - 1;
          remaining = x - week * sum;
          ans += week * 7;
        }
        for (int i = 1; i <= 7; i++) {
          if (remaining == 0) {
            break;
          }
          if (a[i] == 1) {
            remaining--;
          }
          ans++;
        }
        v.push_back(ans);
      }
    }
    cout << *min_element(v.begin(), v.end()) << endl;
  }
}
