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] DP0 = [0]*n DP0[0], DP0[1], DP0[2] = K[0], K[0], K[0] for i in range(3, n): DP0[i] = max(DP0[i-1], DP0[i-2], DP0[i-3]+K[i]) cand0 = DP0[-3] DP1 = [0]*n DP1[0], DP1[1], DP1[2] = 0, K[1], K[1] if n >3: DP1[3] = K[1] for i in range(4, n): DP1[i] = max(DP1[i-1], DP1[i-2], DP1[i-3]+K[i]) cand1 = DP1[-2] DP2 = [0]*n DP2[0], DP2[1], DP2[2] = 0, 0, K[2] if n > 3: DP2[3] = K[2] if n >4: DP2[4] = K[2] for i in range(5, n): DP2[i] = max(DP2[i-1], DP2[i-2], DP2[i-3]+K[i]) cand2 = DP2[-1] print(max(cand0, cand1, cand2))