//https://codeforces.com/contest/1668/problem/D
#include<bits/stdc++.h>
#include<unordered_map>
//#define int ll
#define ll long long
#define ull unsigned long long
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const double eps = 1e-8;
const ll mod = 1e9 + 7;
const int N = 5e5 + 5;
int n, a[N], pre[N], dp[N], b[N];
map<ll, ll> id;
int lowbit(int x)
{
return x & (-x);
}
void add(int x, int v)
{
while (x < N)
{
b[x] = max(b[x], v);
x += lowbit(x);
}
}
int query(int x)
{
int ans = -inf;
while (x)
{
ans = max(ans, b[x]);
x -= lowbit(x);
}
return ans;
}
void solve()
{
cin >> n;
for (int i = 0; i <= n; i++)
b[i] = dp[i] = -inf;
set<int> st;
st.insert(0);
for (int i = 1; i <= n; i++)
{
cin >> a[i];
pre[i] = pre[i - 1] + a[i];
st.insert(pre[i]);
}
int tot = 0;
for (auto it : st)
id[it] = ++tot;
add(id[0], 0);
dp[0] = 0;
for (int i = 1; i <= n; i++)
{
if (a[i] < 0)
dp[i] = dp[i - 1] - 1;
else if (a[i] == 0)
dp[i] = dp[i - 1];
dp[i] = max(dp[i], query(id[pre[i]] - 1) + i);
add(id[pre[i]], dp[i] - i);
}
cout << dp[n] << '\n';
}
signed main()
{
IOS;
int t = 1;
cin >> t;
while (t--)
solve();
return 0;
}
07-20
187
07-10
329