#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll a[1500];
int main()
{
int t;
scanf("%d",&t);
for(int i=1; i<=t; i++)
{
memset(a,0,sizeof a);
ll ans=0;
int n;
scanf("%d",&n);
for(int j=1; j<=n; j++)
scanf("%lld",&a[j]);
if(n==1)
{
printf("%lld\n",a[1]);
continue;
}
int st=1;
int mark=1;
while(true)
{
ll current=a[st];
ll maxi=a[st];
mark=st;
if(st==n)
{
ans+=a[n];
break;
}
for(int j=st+1; j<=n; j++)
{
current+=a[j];
if((current*((j-st)+1))>=maxi && current>0)
{
mark=j;
maxi=current;
}
}
ans+=(maxi*(mark-st+1));
if(mark==n)
break;
st=mark+1;
}
printf("%lld\n",ans);
}
return 0;
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KI2RlZmluZSBsbCBsb25nIGxvbmcKdXNpbmcgbmFtZXNwYWNlIHN0ZDsKbGwgYVsxNTAwXTsKaW50IG1haW4oKQp7CiAgICBpbnQgdDsKICAgIHNjYW5mKCIlZCIsJnQpOwogICAgZm9yKGludCBpPTE7IGk8PXQ7IGkrKykKICAgIHsKICAgICAgICBtZW1zZXQoYSwwLHNpemVvZiBhKTsKICAgICAgICBsbCBhbnM9MDsKICAgICAgICBpbnQgbjsKICAgICAgICBzY2FuZigiJWQiLCZuKTsKICAgICAgICBmb3IoaW50IGo9MTsgajw9bjsgaisrKQogICAgICAgICAgICBzY2FuZigiJWxsZCIsJmFbal0pOwoKICAgICAgICBpZihuPT0xKQogICAgICAgIHsKICAgICAgICAgICAgcHJpbnRmKCIlbGxkXG4iLGFbMV0pOwogICAgICAgICAgICBjb250aW51ZTsKICAgICAgICB9CgogICAgICAgIGludCBzdD0xOwogICAgICAgIGludCBtYXJrPTE7CiAgICAgICAgd2hpbGUodHJ1ZSkKICAgICAgICB7CgogICAgICAgICAgICBsbCBjdXJyZW50PWFbc3RdOwogICAgICAgICAgICBsbCBtYXhpPWFbc3RdOwogICAgICAgICAgICBtYXJrPXN0OwogICAgICAgICAgICBpZihzdD09bikKICAgICAgICAgICAgewogICAgICAgICAgICAgICAgYW5zKz1hW25dOwogICAgICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgZm9yKGludCBqPXN0KzE7IGo8PW47IGorKykKICAgICAgICAgICAgewogICAgICAgICAgICAgICAgY3VycmVudCs9YVtqXTsKICAgICAgICAgICAgICAgIGlmKChjdXJyZW50Kigoai1zdCkrMSkpPj1tYXhpICYmIGN1cnJlbnQ+MCkKICAgICAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgICAgICBtYXJrPWo7CiAgICAgICAgICAgICAgICAgICAgbWF4aT1jdXJyZW50OwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICB9CgogICAgICAgICAgICBhbnMrPShtYXhpKihtYXJrLXN0KzEpKTsKICAgICAgICAgICAgaWYobWFyaz09bikKICAgICAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgICBzdD1tYXJrKzE7CiAgICAgICAgfQogICAgICAgIHByaW50ZigiJWxsZFxuIixhbnMpOwogICAgfQogICAgcmV0dXJuIDA7Cn0K