#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }
const int MAXN = 7;
int n = MAXN;
int need;
int a[MAXN];
int b[2 * MAXN], nb;
int solve() {
  nb = 0;
  for (int i = (0); i < (n); ++i)
    if (a[i] == 1) b[nb++] = i;
  for (int i = (0); i < (nb); ++i) b[nb + i] = b[i] + 7;
  int ret = INT_MAX;
  for (int i = (0); i < (nb); ++i) {
    int whole = (need - 1) / nb;
    int rem = (need - 1) % nb;
    int cur = 1 + 7 * whole + (b[i + rem] - b[i]);
    ret = min(ret, cur);
  }
  return ret;
}
void run() {
  scanf("%d", &need);
  for (int i = (0); i < (n); ++i) scanf("%d", &a[i]);
  printf("%d\n", solve());
}
int main() {
  int n;
  scanf("%d", &n);
  for (int i = (1); i <= (n); ++i) run();
  return 0;
}
