def kado(a, b, c): if len(set((a, b, c))) != 3: return False return b in [max(a, b, c), min(a, b, c)] def solve(): n = int(input()) A = list(map(int, input().split())) ans = 0 for _ in range(3): dp = [0] * (n + 1) for i in range(3, n + 1): a, b, c = A[i - 3:i] dp[i] = dp[i - 1] if kado(a, b, c): dp[i] = max(dp[i], dp[i - 3] + a) ans = max(ans, dp[-1]) A = A[1:] + A[:1] print(ans) for _ in range(int(input())): solve()