from collections import defaultdict t = int(input()) for _ in range(t): n = int(input()) A = list(map(int, input().split())) A += A Counter = defaultdict(int) Counter[0] = 0 for i in range(n): if len(set(A[i:i + 3])) != 3: continue if A[i] > A[i + 1] < A[i + 2] or A[i] < A[i + 1] > A[i + 2]: Counter[A[i]] += A[i] Vals = list(Counter.values()) print(max(Vals))