#include <bits/stdc++.h>

using namespace std;

using ll = long long;

vector<int> minShift(vector<int> a) {
  int n = int(a.size());
  auto res = a;
  for (int i = 1; i < n; ++i) {
    auto na = a;
    rotate(na.begin(), na.begin() + i, na.end());
    if (na < res) {
      res = na;
    }
  }
  return res;
}

void solveTest() {
  int n; cin >> n;
  vector<int> a(n);
  for (auto& x : a) {
    cin >> x;
  }
  a = minShift(a);
  if (*min_element(a.begin(), a.end()) == *max_element(a.begin(), a.end())) {
    cout << a[0] << "\n";
    return;
  }
  map<vector<int>, int> dist;
  dist[a] = 0;
  queue<vector<int>> q;
  q.push(a);
  while (!q.empty()) {
    auto v = q.front(); q.pop();
    for (int i = 0; i < n; ++i) {
      if (v[i] > 0 && v[(i + 1) % n] > 1) {
        auto nv = v;
        nv[i] -= 1;
        nv[(i + 1) % n] -= 2;
        nv = minShift(nv);
        if (!dist.count(nv)) {
          dist[nv] = dist[v] + 1;
          q.push(nv);
          if (*min_element(nv.begin(), nv.end()) == *max_element(nv.begin(), nv.end())) {
            ll original_sum = 0;
            for (auto& x : a) {
              original_sum += x;
            }
            cout << (original_sum - 3 * dist[nv]) / n << "\n";
            return;
          }
        }
      }
    }
  }
  cout << 0 << "\n";
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int t; cin >> t;
  while (t--) {
    solveTest();
  }
  return 0;
}
