t = int(input()) for _ in range(t): n = int(input()) A = list(map(int, input().split())) A += A ans = 0 for f in range(3): DP = [0 for _ in range(n + 1)] for i in range(3, n + 1): if len(set(A[f + i - 3:f + i])) < 3: DP[i] = DP[i - 1] else: if A[f + i - 3] < A[f + i - 2] > A[f + i - 1] or A[f + i - 3] > A[f + i - 2] < A[f + i - 1]: DP[i] = max(DP[i - 1], DP[i - 3] + A[f + i - 3]) else: DP[i] = DP[i - 1] ans = max(ans, DP[n]) print(ans)