t=int(input())
ans=[]
for _ in range(t):
n=int(input())
a_=list(map(int,input().split()))
ansi=0
for idx in range(3):# スタート位置
dp=[0]*(n+1)
a=a_[idx:]+a_[:idx]
for i in range(2,n):
if a[i-2]!=a[i-1]!=a[i] and a[i-2]!=a[i] and (a[i-2]a[i] or a[i-2]>a[i-1]