#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N=1000001;
char *p1,*p2,buf[100010];
#define gc() (p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++)
int read(){
int x=0;
char c=gc();
while(c<48)c=gc();
while(c>47)x=(x<<3)+(x<<1)+(c&15),c=gc();
return x;
}
void chmax(ll&x,ll y){
if(x<y)x=y;
}
int a[200010];
ll f[1000110],g[1000100];
int main(){
freopen("color.in","r",stdin);
freopen("color.out","w",stdout);
int T=read(),n;
ll x,y,mx1,mx2,lz;
a[0]=N;
while(T--){
n=read();
for(int i=1;i<=n;++i)a[i]=read();
memset(f,-0x3f,sizeof f);
memset(g,-0x3f,sizeof g);
mx1=mx2=f[N]=g[N]=lz=0;
for(int i=1;i<=n;++i){
if(a[i-1]==a[i]){
lz+=a[i];
x=max(mx1,mx2);
chmax(f[a[i]],max(mx1,mx2)-lz);
chmax(g[a[i]],max(mx1,mx2)-lz);
chmax(mx1,f[a[i]]);
chmax(mx2,g[a[i]]);
}else{
x=f[a[i]];
y=g[a[i]];
chmax(f[a[i-1]],max(mx1,y+a[i]));
chmax(g[a[i-1]],max(mx2,x+a[i]));
chmax(f[a[i]],x);
chmax(g[a[i]],y);
chmax(mx1,max(f[a[i-1]],f[a[i]]));
chmax(mx2,max(g[a[i-1]],g[a[i]]));
}
}
printf("%lld\n",max(mx1,mx2)+lz);
}
return 0;
}