def k(x): y=sorted(x) if len(set(x))!=3 or y.index(x[1])==1: return 0 return 1 def solve(): N=int(input()) A=list(map(int,input().split())) for i in range(3): A.append(A[i]) INF=10**12 F=[-INF]*N for i in range(N): if k([A[i],A[i+1],A[i+2]]): F[i]=A[i] for i in range(N): F.append(F[i]) DP=[0]*(N+4) for i in range(N+1): DP[i]=max(DP[i],DP[i-1]) DP[i+3]=max(DP[i+3],DP[i]+F[i]) P=max(DP[N-1],DP[N+1],DP[N]) DP=[0]*(N+4) for i in range(N+1): DP[i]=max(DP[i],DP[i-1]) DP[i+3]=max(DP[i+3],DP[i]+F[i-3]) print(max(P,max(DP[N-1],DP[N+1],DP[N]))) for i in range(int(input())): solve()