import sys import io, os input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline def solve(A): dp = [0]*(n+1) for i in range(2, n): dp[i+1] = max(dp[i], 0) if A[i-2] < A[i-1] > A[i] and A[i-2] != A[i]: dp[i+1] = max(dp[i+1], dp[i-2]+A[i-2]) elif A[i-2] > A[i-1] < A[i] and A[i-2] != A[i]: dp[i+1] = max(dp[i+1], dp[i-2]+A[i-2]) return dp[n] t = int(input()) for _ in range(t): n = int(input()) A = list(map(int, input().split())) ans = solve(A) ans = max(ans, solve(A[1:]+A[0:1])) ans = max(ans, solve(A[2:]+A[0:2])) print(ans)