#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    int req;
    cin >> req;
    int tm, cnt = 0, pc[8];
    pc[0] = 0;
    for (int j = 0; j < 7; j++) {
      cin >> tm;
      cnt += tm;
      pc[j + 1] = pc[j] + tm;
    }
    int dp[2 * cnt + 1];
    for (int j = 0; j <= 2 * cnt; j++) dp[j] = 1000;
    for (int j = 0; j <= 7; j++) {
      for (int k = 7; k >= 0; k--) {
        int tmp = pc[j] - pc[k] + cnt;
        int hari = j + 7 - k;
        dp[tmp] = min(dp[tmp], hari);
      }
    }
    if (req <= cnt) {
      int ans = 7;
      for (int j = 0; j <= 7; j++) {
        for (int k = j + 1; k <= 7; k++) {
          if (pc[k] - pc[j] == req) ans = min(ans, k - j);
        }
      }
      cout << min(ans, dp[req]) << endl;
    } else if (req < cnt * 2) {
      int day = req % cnt;
      cout << min(dp[req], 7 + dp[day]) << endl;
    } else if (req == cnt * 2)
      cout << min(dp[req], 7 + dp[cnt]) << endl;
    else {
      int week = req / cnt;
      int day = req % cnt;
      if (day == 0) {
        cout << min((week - 2) * 7 + dp[2 * cnt], (week - 1) * 7 + dp[cnt])
             << endl;
      } else {
        cout << min((week - 1) * 7 + dp[cnt + day], week * 7 + dp[day]) << endl;
      }
    }
  }
}
