#include <bits/stdc++.h>
using namespace std;
const int LIMIT = 1e5 + 7;
int t, k, w[7];
int main() {
  scanf("%d", &t);
  while (t--) {
    scanf("%d", &k);
    int r = 7e8 + 7;
    int weekCount = 0;
    for (int i = 0; i < 7; i++) {
      scanf("%d", &w[i]);
      weekCount += w[i];
    }
    for (int i = 0; i < 7; i++) {
      int localR = 0;
      int localDays = 0;
      for (int j = i; j < 7; j++) {
        localR++;
        if (w[j]) {
          localDays++;
        }
        if (localDays == k) {
          r = min(r, localR);
          break;
        }
      }
      if (localDays == k) {
        r = min(r, localR);
      }
      if ((k - localDays) > (weekCount * 2)) {
        localR += ((k - localDays - weekCount) / weekCount * 7);
        localDays += ((k - localDays - weekCount) / weekCount * weekCount);
      }
      while (localDays < k) {
        for (int j = 0; j < 7; j++) {
          localR++;
          if (w[j]) {
            localDays++;
          }
          if (localDays == k) {
            break;
          }
        }
      }
      r = min(r, localR);
    }
    printf("%d\n", r);
  }
  return EXIT_SUCCESS;
}
