#include <bits/stdc++.h>
using namespace std;
static const int _ = []() {
  ios::sync_with_stdio(false);
  cin.sync_with_stdio(false);
  cout.sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  return 0;
}();
template <class T>
inline vector<T> rv(long long n) {
  vector<T> v(n);
  for (long long i = 0; i < (n); i++) cin >> v[i];
  return v;
}
template <class T>
inline void pv(const vector<T>& v) {
  for (long long i = 0; i < (v.size()); i++) cout << v[i] << " ";
  cout << endl;
}
template <class T>
inline void amax(T& a, const T& b) {
  if (a < b) a = b;
}
template <class T>
inline void amin(T& a, const T& b) {
  if (a > b) a = b;
}
const long long MOD = 1e9 + 7;
const long long MOD1 = 998244353;
struct custom_hash {
  static uint64_t splitmix64(uint64_t x) {
    x += 0x9e3779b97f4a7c15;
    x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
    x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
    return x ^ (x >> 31);
  }
  size_t operator()(uint64_t x) const {
    static const uint64_t FIXED_RANDOM =
        chrono::steady_clock::now().time_since_epoch().count();
    return splitmix64(x + FIXED_RANDOM);
  }
};
struct Rd {
  template <typename T>
  Rd& operator>>(T& n) {
    cin >> n;
    return *this;
  }
  template <typename T>
  Rd& operator>>(vector<T>& v) {
    for (long long i = 0; i < v.size(); i++) {
      cin >> v[i];
    }
    return *this;
  }
  template <typename T, typename U>
  Rd& operator>>(vector<pair<T, U>>& v) {
    for (long long i = 0; i < v.size(); i++) {
      cin >> v[i].first >> v[i].second;
    }
    return *this;
  }
  template <typename T>
  Rd& operator>>(vector<vector<T>>& v) {
    for (long long i = 0; i < v.size(); i++) {
      for (long long j = 0; j < v[i].size(); j++) {
        cin >> v[i][j];
      }
    }
    return *this;
  }
} rd;
long long GCD(long long a, long long b) {
  while (b) {
    long long r = a % b;
    a = b;
    b = r;
  }
  return a;
}
vector<long long> fac(long long n) {
  set<long long> s{1, n};
  for (long long i = 2; i <= sqrt(n); i++) {
    if ((n % i) == 0) {
      s.insert(i);
      s.insert(n / i);
    }
  }
  vector<long long> v((s).begin(), (s).end());
  return v;
}
long long dp[11][11];
int main(int argc, char** argv) {
  long long t;
  cin >> t;
  for (long long i = 0; i < (t); i++) {
    long long k;
    cin >> k;
    vector<long long> d(7);
    rd >> d;
    long long w = 0;
    for (long long j = 0; j < (7); j++) w += d[j];
    long long weeks = k / w, gain = k % w;
    long long sa = 0, sb = 0, best = INT_MAX;
    vector<long long> sumf(7, 0), sumb(7, 0);
    sumb.back() = d.back();
    sumb.push_back(0);
    for (int i = 5; i >= 0; i--) sumb[i] += sumb[i + 1] + d[i];
    for (long long a = 0; a <= 7; a++) {
      long long days = 7 - a, classes = sumb[a], needed = k - classes;
      long long ww = needed / w - 1;
      classes += ww * w;
      days += ww * 7;
      needed = k - classes;
      for (long long b = 0; b < 14 && classes < k; b++) {
        if (d[b % 7]) {
          classes++;
        }
        days++;
      }
      amin(best, days);
    }
    cout << best << '\n';
  }
  return 0;
}
