#include<bits/stdc++.h>
#define int long long
using namespace std;int n,m,a[200010],sum[200010],tree[200010],b[200010],qwq,z;void add(int x,int y){for(;x<=m;x+=x&-x)tree[x]+=y;}
int query(int x){int res=0;for(;x;x&=x-1)res+=tree[x];return res;}signed main(){cin>>qwq;while(qwq--){cin>>n;
for(int i=1;i<=n;i++) cin>>a[i],sum[i]=sum[i-1]+a[i];z=sum[n];ans=0;if(z<0){cout<<"-1\n";continue;}if(!z){int f=0;for(int i=1;i<=n;i++){if(a[i]){cout<<"-1\n";f=1;break;}}if(!f)cout<<"0\n";continue;}
for(int i=1;i<=n;i++)b[++m]=sum[i],b[++m]=(sum[i]%z+z)%z;sort(b+1,b+m+1);m=unique(b+1,b+m+1)-b-1;memset(tree,0,sizeof(tree));for(int i=1;i<=n;i++)ans-=query(lower_bound(b+1,b+m+1,sum[i])-b-1),add(lower_bound(b+1,b+m+1,sum[i])-b,1);
sort(sum+1,sum+n+1);memset(tree,0,sizeof(tree));for(int i=1;i<=n;i++)ans+=(i*2-1ll-n)*(sum[i]>0?sum[i]/z:(sum[i]-z+1)/z)+query(lower_bound(b+1,b+m+1,(sum[i]%z+z)%z)-b-1),add(lower_bound(b+1,b+m+1,(sum[i]%z+z)%z)-b,1);cout<<ans<<"\n";memset(sum,0,sizeof(sum));m=0;}return 0;}