T = int(input()) def iskadomatu(ll): if ll[0] == ll[1] or ll[1] == ll[2] or ll[2] == ll[0]: return False if (ll[0] > ll[1] and ll[1] < ll[2]) or (ll[0] < ll[1] and ll[1] > ll[2]): return True return False lsans = [] for i in range(T): N = int(input()) lsA = list(map(int,input().split())) ans = 0 for j in range(3): dp = [0]*(N) for k in range(2,N): if iskadomatu(lsA[k-2:k+1]): dp[k] = max(dp[k-1],dp[k-3]+lsA[k-2]) else: dp[k] = dp[k-1] ans = max(ans,max(dp)) lsA = [lsA[-1]] + lsA[:-1] lsans.append(ans) print(*lsans,sep='\n')