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(6): dp = [0]*(N) ruimax = 0 for k in range(N-2): if k-3 < 0 and iskadomatu(lsA[k:k+3]): dp[k] += lsA[k] elif iskadomatu(lsA[k:k+3]): ruimax = max(ruimax,dp[k-3]) dp[k] += lsA[k] + ruimax else: dp[k] = ruimax #print(dp) ans = max(ans,max(dp)) lsA = [lsA[-1]] + lsA[:-1] lsans.append(ans) print(*lsans,sep='\n')