import sys def kado(a,b,c): if a==b or b==c or a==c: return False return min(a,b,c)==b or max(a,b,c)==b input = sys.stdin.readline for _ in range(int(input())): N = int(input()) A = list(map(int,input().split())) res = 0 dp = [0 for i in range(N)] dp[2] = kado(A[0],A[1],A[2]) * A[0] for i in range(3,N): dp[i] = max(dp[i-1],kado(A[i-2],A[i-1],A[i])*A[i-1]+dp[i-3]) res = max(res,dp[-1]) A = [A[i+1] for i in range(N-1)] + [A[0]] dp = [0 for i in range(N)] dp[2] = kado(A[0],A[1],A[2]) * A[0] for i in range(3,N): dp[i] = max(dp[i-1],kado(A[i-2],A[i-1],A[i])*A[i-1]+dp[i-3]) res = max(res,dp[-1]) A = [A[i+1] for i in range(N-1)] + [A[0]] dp = [0 for i in range(N)] dp[2] = kado(A[0],A[1],A[2]) * A[0] for i in range(3,N): dp[i] = max(dp[i-1],kado(A[i-2],A[i-1],A[i])*A[i-1]+dp[i-3]) res = max(res,dp[-1]) print(res)