def solve(): n = int(input()) A = list(map(int, input().split())) stack = [] edges = [[] for _ in range(n)] for i, a in enumerate(A): while stack and a < stack[-1][0]: b, j = stack.pop() if stack and stack[-1][0] > a: edges[stack[-1][1]].append(j) else: edges[i].append(j) stack.append((a, i)) while len(stack) >= 2: _, i = stack.pop() edges[stack[-1][1]].append(i) i = stack[0][1] lst = [i] used = [False] * n used[i] = True ans = 1 while lst: pos = lst.pop() for npos in edges[pos]: if npos != 0 and used[npos - 1]: continue if npos != n - 1 and used[npos + 1]: continue used[npos] = True lst.append(npos) ans += 1 ans = 2 * ans + 1 if used[0]: ans -= 1 if used[-1]: ans -= 1 print(ans) for _ in range(int(input())): solve()