#include <bits/stdc++.h>
using namespace std;
int a[10], n;
int main() {
  int T;
  scanf("%d", &T);
  while (T--) {
    scanf("%d", &n);
    int m = 0;
    for (int i = 0; i < 7; ++i) {
      int x;
      scanf("%d", &x);
      if (x) a[m++] = i;
    }
    int ret = 1e9;
    for (int x = 0; x < m; ++x) {
      int nn = n + x;
      int tim = nn / m, lef = nn % m;
      int ans;
      if (!lef)
        ans = (tim - 1) * 7 + a[m - 1];
      else
        ans = tim * 7 + a[lef - 1];
      ans = ans - a[x] + 1;
      ret = min(ans, ret);
    }
    printf("%d\n", ret);
  }
  return 0;
}
