def solve(a): q=[0]*(n+1) for i in range(n): if i-2>=0: if len(set(a[i-2:i+1]))==3: if max(a[i-2:i+1])==a[i-1] or min(a[i-2:i+1])==a[i-1]: q[i]=q[i-3]+a[i-2] q[i]=max(q[i],q[i-1]) return max(q) T=int(input()) for _ in range(T): n=int(input()) a=list(map(int,input().split())) print(max(solve(a),solve(a[1:]+a[:1]),solve(a[2:]+a[:2])))