#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
int w[8];
int main() {
  int T;
  for (scanf("%d", &T); T--;) {
    int n, cnt = 0;
    scanf("%d", &n);
    for (int i = 0; i < 7; i++) {
      scanf("%d", &w[i]);
      if (w[i]) cnt++;
    }
    int a = n / cnt, A = a * 7;
    int b = n % cnt, B = 7;
    if (!b) A -= 7, b = cnt;
    for (int i = 0; i < 7; i++) {
      int tmp = 0;
      for (int j = 0; j < 7; j++) {
        if (tmp == b) {
          B = min(B, j);
          break;
        }
        if (w[(i + j) % 7]) tmp++;
      }
    }
    printf("%d\n", A + B);
  }
}
