import sys readline = sys.stdin.readline def kadomatsu(a, b, c): for i in [-1, 1]: if a * i < b * i and b * i > c * i and a != c: return True return False def calc(A): N = len(A) inf = 10 ** 18 dp = [-inf] * (N + 1) dp[0] = 0 for i in range(N): if i != N - 1: dp[i + 1] = max(dp[i + 1], dp[i]) if i < N - 2: if kadomatsu(A[i], A[i + 1], A[i + 2]): dp[i + 3] = max(dp[i + 3], dp[i] + A[i]) return max(dp) T = int(readline()) for _ in range(T): N = int(readline()) A = list(map(int, readline().split())) ans = calc(A) a = A.pop(0) A.append(a) ans = max(ans, calc(A)) a = A.pop(0) A.append(a) ans = max(ans, calc(A)) print(ans)