T=int(input()) for i in range(T): N=int(input()) A=list(map(int, input().split())) A=A+A B=A[1:]+A[:1] C=A[2:]+A[:2] a,b,c=0,0,0 d=0 for j in range(N): d-=1 if A[j]!=A[j+1] and A[j+1]!=A[j+2] and A[j+2]!=A[j] and d<0: if min(A[j],A[j+1],A[j+2])==A[j+1] or max(A[j],A[j+1],A[j+2])==A[j+1]: a+=A[j] d=2 d=0 for j in range(N): d-=1 if B[j]!=B[j+1] and B[j+1]!=B[j+2] and B[j+2]!=B[j] and d<0: if min(B[j],B[j+1],B[j+2])==B[j+1] or max(B[j],B[j+1],B[j+2])==B[j+1]: b+=B[j] d=2 d=0 for j in range(N): d-=1 if C[j]!=C[j+1] and C[j+1]!=C[j+2] and C[j+2]!=C[j] and d<0: if min(C[j],C[j+1],C[j+2])==C[j+1] or max(C[j],C[j+1],C[j+2])==C[j+1]: c+=C[j] d=2 print(max(a,b,c))