#include <bits/stdc++.h>
using namespace std ;
typedef long long int ll ;
ll t , n , s , p ;
int flag ;
int main ( ) {
cin >> t ;
for ( ll k = 0 ; k < t ; k ++ ) {
cin >> n ;
ll a[n] ;
for ( ll i = 0 ; i < n ; i ++ ) cin >> a[i] ;
s = 0 ;
sort ( a , a + n ) ;
if ( a[0] >= 0 ) {
for ( auto i : a ) s += i ;
cout << s << endl ;
continue ;
}
p = n - 1 ;
flag = 1 ;
ll b[n] , c[n] ;
b[0] = 0 ;
c[0] = a[0] ;
for ( ll i = 1 ; i < n ; i ++ ) {
b[i] = b[i - 1] + c[i - 1] * flag ;
c[i] = a[i] + b[i] ;
if ( c[i] >= 0 && flag ) {
p = i ;
flag = 0 ;
}
}
for ( ll i = p ; i < n ; i ++ ) s += c[i] ;
cout << s << endl ;
}
return 0 ;
}