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(n-2):
if a[i]!=a[i+1]!=a[i+2] and a[i]!=a[i+2] and (a[i]a[i+2] or a[i]>a[i+1]