for _ in range(int(input())): n = int(input()) a = list(map(int, input().split())) a.append(a[0]) a.append(a[1]) ans = 0 for k in range(3): dp = [0, 0, 0] for i in range(k, n - 2 + k): if a[i] != a[i+2] and (a[i+1] > max(a[i], a[i+2]) or a[i+1] < min(a[i], a[i+2])): dp = [ max(dp[0], dp[1]), dp[2], dp[0] + a[i] ] else: dp = [ max(dp[0], dp[1]), dp[2], 0 ] ans = max(ans, *dp) print(ans)