#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
const int maxn = 1e5 + 5;
int main() {
  std::ios::sync_with_stdio(false);
  cin.tie(0), cout.tie(0);
  int t;
  cin >> t;
  while (t--) {
    int k;
    cin >> k;
    int a[7];
    int num = 0, b = 0;
    for (int i = 0; i < 7; i++) {
      cin >> a[i];
      if (a[i] == 1) num++;
    }
    for (int i = 6; i >= 0; i--) {
      if (a[i] == 1) break;
      b++;
    }
    int ans = 0x3f3f3f3f;
    for (int i = 0; i < 7; i++) {
      int tmp = 0;
      int n = k;
      for (int j = i; j < 7; j++) {
        if (n == 0) break;
        tmp++;
        if (a[j] == 1) n--;
      }
      if (n == 0) {
        ans = min(ans, tmp);
        continue;
      }
      tmp += n / num * 7;
      n %= num;
      if (n == 0) {
        tmp -= b;
        ans = min(ans, tmp);
        continue;
      }
      for (int j = 0; j < 7; j++) {
        if (n == 0) break;
        tmp++;
        if (a[j] == 1) n--;
      }
      ans = min(ans, tmp);
    }
    cout << ans << endl;
  }
  return 0;
}
