#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
typedef long long ll;
#define lson l, mid, rt * 2
#define rson mid + 1, r, rt * 2 + 1
const int mx = (1e5 + 10);
using namespace std;
struct node{
int nu, po;
bool operator < (node A)const{
return nu < A.nu;
}
}data[mx];
ll fr[mx], ba[mx], num[mx], sum[mx * 4];
int n, m, wei[mx];
ll query(int L, int R, int l, int r, int rt){
if(L <= l && r <= R) return sum[rt];
int mid = ( l + r) / 2;
ll ans = 0;
if( L <= mid ) ans = query(L, R, lson);
if(mid < R) ans = max(ans, query(L, R, rson));
return ans;
}
void updata(int po, ll num, int l, int r, int rt){ // ll num 写成 int num
if(l == r){
sum[rt] = num;
return;
}
int mid = (l + r) / 2;
if(po <= mid) updata(po, num, lson);
else updata(po, num, rson);
sum[rt] = max(sum[rt * 2], sum[rt * 2 + 1]);
}
void cmp(int x, ll *p){
if(num[x] <= 0){
p[x] = 0;
}
else{
if(wei[x]== m)
p[x] = num[x];
else{
ll ret = query(wei[x] + 1, m, 1, m, 1);
p[x]= num[x] + ret ;
}
}
updata(wei[x], p[x], 1, m ,1);
}
int main(){
int T;
//freopen("1in","r",stdin);
scanf("%d", &T);
while(T--){
scanf("%d", &n);
memset(sum, 0, sizeof(sum));
for(int i = 1; i <= n; i++){
scanf("%d", &data[i].nu);
num[i] = data[i].nu;
data[i].po = i;
}
sort(data + 1, data + n + 1);
m = wei[data[1].po] = 1;
for(int i = 2; i <= n; i++){
if(data[i].nu != data[i - 1].nu) m++;
wei[data[i].po] = m;
}
for(int i = n; i >= 1; i--)
cmp(i, ba);
ba[n + 1] = 0;
for(int i = n; i >= 1; i--)
ba[i] = max(ba[i], ba[i+1]);
memset(sum, 0, sizeof(sum)); //忘了初始化
for(int i = 1; i <= n; i++){
num[i] = -num[i];
cmp(i, fr);
}
fr[0] = 0;
for(int i = 1; i <= n; i++)
fr[n] = max(fr[n - 1], fr[n]);
ll ans = 0;
for(int i = 0; i <= n ; i++) // 注意边界
ans = max(ans, fr[i] + ba[i+1]);
cout<<ans<<endl;
}
return 0;
}