#include <bits/stdc++.h>
using namespace std;
int main() {
  int t, n, m;
  int a[8];
  scanf("%d", &t);
  while (t--) {
    scanf("%d", &m);
    int num = 0, minx = 0x3f3f3f3f, ans;
    for (int i = 1; i <= 7; i++) {
      scanf("%d", &a[i]);
      if (a[i]) num++;
    }
    for (int i = 1; i <= 7; i++) {
      ans = 0;
      n = m;
      for (int j = i; j <= 7; j++) {
        if (a[j]) n--;
        ans++;
        if (n == 0) break;
      }
      if (n == 0) {
        minx = min(minx, ans);
        continue;
      }
      ans += (n / num) * 7;
      if (n % num == 0) {
        for (int j = 7; j >= 1; j--) {
          if (a[j])
            break;
          else
            ans--;
        }
      } else {
        int x = n % num;
        for (int j = 1; j <= 7; j++) {
          if (a[j]) x--;
          ans++;
          if (x == 0) break;
        }
      }
      minx = min(minx, ans);
    }
    printf("%d\n", minx);
  }
  return 0;
}
