T = int(input()) for _ in range(T): N = int(input()) A = list(map(int,input().split())) A = A+A K0 = [] for i in range(N-3+1): a,b,c = A[i:i+3] if a!=b and b!=c and c!=a and (max(a,b,c)==b or min(a,b,c)==b): K0.append(i) M0 = len(K0) dp0 = [0]*M0 if M0>0: dp0[0] = A[K0[0]] for j in range(1,M0): dp0[j] = dp0[j-1] i0 = K0[j-1] i1 = K0[j] if i1>=i0+3: dp0[j] += A[i1] else: dp0 = [0] K1 = [] for i in range(1,N-2+1): a,b,c = A[i:i+3] if a!=b and b!=c and c!=a and (max(a,b,c)==b or min(a,b,c)==b): K1.append(i) M1 = len(K1) dp1 = [0]*M1 if M1>0: dp1[0] = A[K1[0]] for j in range(1,M1): dp1[j] = dp1[j-1] i0 = K1[j-1] i1 = K1[j] if i1>=i0+3: dp1[j] += A[i1] else: dp1 = [0] K2 = [] for i in range(2,N-1+1): a,b,c = A[i:i+3] if a!=b and b!=c and c!=a and (max(a,b,c)==b or min(a,b,c)==b): K2.append(i) M2 = len(K2) dp2 = [0]*M2 if M2>0: dp2[0] = A[K2[0]] for j in range(1,M2): dp2[j] = dp2[j-1] i0 = K2[j-1] i1 = K2[j] if i1>=i0+3: dp2[j] += A[i1] else: dp2 = [0] print(max(dp0[-1],dp1[-1],dp2[-1]))