import sys;input = lambda: sys.stdin.readline().rstrip() #import heapq #from collections import deque #import numpy as np #from collections import Counter as cnt #from collections import defaultdict as ddc #from math import factorial as fct #from math import gcd #from bisect import bisect_left as bsl #from bisect import bisect_right as bsr #from itertools import accumulate as acc #from itertools import combinations as cmb #from itertools import permutations as pmt #from itertools import product as prd #from functools import reduce as red #import sys #sys.setrecursionlimit(10**9) #再帰を多く使う(デフォルトは1000) for _ in range(int(input())): n = int(input()) A = list(map(int,input().split())) A += A[:2] K = [0]*n for i in range(n): S = A[i:i+3] if len(set(S)) == 3 and S[1] in {min(S), max(S)}: K[i] = S[0] DP = [0]*n DP[0] = K[0] DP[1] = max(K[:2]) DP[2] = max(K[:3]) for i in range(3, n): DP[i] = max(DP[i-1], DP[i-2], DP[i-3]+K[i]) print(DP[-1])