#include<bits/stdc++.h>
using namespace std;

using ll = long long;

template<class T> 
bool Min(T &a, const T &b) { 
    return a > b ? a = b, 1 : 0; 
}

const int N = 2e5+5;

int n, a[N], b[N];
ll P[N], M[N];

void solve() {
    cin >> n;
    for (int i = 0; i < n; i++) cin >> a[i];

    ll sum = 0, mn = 0;
    int idx = 0;
    for (int i = 0; i < n; i++) {
        sum += a[i];
        if (Min(mn, sum)) idx = i + 1;
    }
    idx %= n;

    for (int i = 0; i < n; i++)
        b[i] = a[(idx + i) % n];

    for (int i = 1; i < n; i++) {
        P[i] = P[i - 1] + max(0, b[i - 1]);
        M[i] = M[i - 1] + min(0, b[i - 1]);
    }

    ll ans = 0; int v = 0;
    for (int u = 0; u < n; u++) {
        while (v < n && -M[v] < P[u]) v++;
        ans += (n - max(u, v));
    }
    cout << ans << '\n';
}

int main() {
    ios_base::sync_with_stdio(false); cin.tie(NULL);

    int tests = 1; cin >> tests;
    while (tests--) solve();

    #ifndef ONLINE_JUDGE
    cerr << "\nTime elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
    #endif

    return 0;
}
