Chef just got a box of chocolates as his birthday gift. The box contains $N$ chocolates in a row (numbered $1$ through $N$), where $N$ is even. For each valid $i$, the $i$-th chocolate has a sweetness value $W_i$.
Chef wants to eat all the chocolates in the first half of the box and leave all chocolates in the second half uneaten. Since he does not like chocolates that are too sweet, he will be unhappy if at least one of the chocolates he eats has the maximum sweetness among all the chocolates in the box.
A right cyclic shift by $k$ chocolates ($0 \le k < N$) consists of moving the last $k$ chocolates in the row to the beginning in the same order and moving each of the remaining $N-k$ chocolates $k$ places to the right. Before eating the first half of the chocolates, Chef wants to perform some right cyclic shift in such a way that he will not be unhappy after eating them. Find the number of ways to do this, i.e. the number of valid integers $k$ such that if Chef performs the right cyclic shift by $k$ chocolates and then eats the first half of the chocolates in the box, he does not become unhappy.

-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first line of each test case contains a single integer $N$.
- The second line contains $N$ space-separated integers $W_1, W_2, \ldots, W_N$.

-----Output-----
For each test case, print a single line containing one integer ― the number of shifts for which Chef does not become unhappy.

-----Constraints-----
- $1 \le T \le 5$
- $1 \le N \le 10^5$
- $N$ is even
- $1 \le W_i \le 10^5$ for each valid $i$

-----Example Input-----
2
6
1 1 2 1 1 1
6
1 1 2 1 1 2

-----Example Output-----
3
0

-----Explanation-----
Example case 1: The three valid right shifts and the contents of the box for these shifts are:
- shift by $k = 1$: $(1, 1, 1, 2, 1, 1)$
- shift by $k = 2$: $(1, 1, 1, 1, 2, 1)$
- shift by $k = 3$: $(1, 1, 1, 1, 1, 2)$

Difficulty: interview

Starter code:

Solutions:
from collections import deque
t=int(input())

for i in range(t):
 n=int(input())
 N=[i for i in range(1, n+1)]
 w=list(map(int, input().split()))
 max_sweetness=max(w)
 sizes=[]
 cnt=0
 for i in range(n):
  if w[i]!=max_sweetness:
   cnt+= 1 
  else:
   sizes.append(cnt)
   cnt=0
 
 if cnt!=0:
  sizes[0]=(cnt+sizes[0])
 
 res=0
 for i in range(len(sizes)):
  res+=max(sizes[i]-n//2+1, 0)
 
 print(res)

# cook your dish here
t=int(input())
for i in range(t):
 a=int(input())
 cho =list(map(int, input().split()))
 maxc=max(cho)
 start=cho.index(maxc)
 mid =int(len(cho)/2)
 for j in range(len(cho)-1, 0, -1):
  if(cho[j]==maxc):
   end = j
   break
 ans =((len(cho))-end)-(mid-start)
 print(max(0,ans))

# cook your dish here
def answer():
 m=max(w)
 count=[]
 c=0
 for i in w:
  if(i==m):
   count.append(c)
   c=-1
  c+=1

 if(c):
  count.append(c)

 first=(w[0]!=m)
 last=(w[-1]!=m)

 if(first and last):
  count[0]+=count[-1]
  count.pop()

 k=0 
 for i in count:
  if(i >= n//2):
   k+=i-n//2+1
 return k
 
   

for T in range(int(input())):
 n=int(input())
 w=list(map(int,input().split()))

 print(answer())


# cook your dish here
t = int(input())
for _ in range(t):
 n = int(input())
 arr = list(map(int, input().split()))
 maxi = max(arr)
 for i in range(n-1,-1,-1):
  if(arr[i]==maxi):
   r = i
   break
 for i in range(n):
  if(arr[i]==maxi):
   r = r-i
   break
 if((n//2)-r<0):
  print(0)
 else:
  print((n//2)-r)

# cook your dish here
t = int(input())
for _ in range(t):
 n = int(input())
 arr = list(map(int, input().split()))
 maxi = max(arr)
 for i in range(n-1,-1,-1):
  if(arr[i]==maxi):
   r = i
   break
 for i in range(n):
  if(arr[i]==maxi):
   r = r-i
   break
 if((n//2)-r<0):
  print(0)
 else:
  print((n//2)-r)


t=int(input())
while(t>0):
 t-=1
 n=int(input())
 l=list(map(int,input().split()))
 m=max(l)
 a=[]
 for i in range(n):
  if(m==l[i]):
   a.append(i+1)
 b=[]
 for i in range(1,len(a)):
  b.append(a[i]-a[i-1])
 if(len(a)>1):
  b.append(n-a[-1]+a[0])
  k=max(b)
  if(k>=n//2):
   print(k-(n//2))
  else:
   print(0)
 else:
  print((n+1)//2)



from sys import maxsize, stdout, stdin
mod = int(1e9 + 7)
def I(): return int(stdin.readline())
def lint(): return [int(x) for x in stdin.readline().split()]
def S(): return input().strip()
def grid(r, c): return [lint() for i in range(r)]
for _ in range(I()):
 n = I()
 ls = lint()
 cnt =0
 arr =[0]*n
 m = max(ls)
 idx = ls.index(m)
 ideal = n//2
 shift = ideal - idx
 for i in range(n):
  arr[i]= (ls[(i-shift)%n])
 if m in arr[:n//2 ]:
  print(0)
 else:
  for j in range(n-1,n//2 -1 ,-1):
   if arr[j]!=m:
    cnt+=1
   else:break
  print(cnt+1)


# cook your dish here
for _ in range(int(input())):
 q = int(input())
 arr = list(map(int,input().split()))
 t = max(arr)
 for i in range(q-1,-1,-1):
  if(arr[i]==t):
   r = i
   break
 for i in range(q):
  if(arr[i]==t):
   r = r-i
   break
 if((q//(1+1))-r < 0):
  print(0)
 else:
  print((q//(1+1))-r)

# cook your dish here
for _ in range(int(input())):
 n=int(input())
 l=list(map(int,input().split()))
 mid=n//2-1
 max_beg=max(l)
 f_o=l.index(max_beg)
 l.reverse()
 l_o=l.index(max_beg)
 l_o=n-1-l_o
 if l_o-f_o>=mid:
  print(0)
 else:
  if f_o==l_o:
   f_o=mid 
   print(n-1-f_o)
  else:
   gap=l_o-f_o 
   f_o=mid 
   l_o=f_o+gap 
   print(n-1-l_o)
 
  


# cook your dish here
try:
 T = int(input())
 while T>0:
  N = int(input())
  midway = (N//2) - 1
  num = list(map(int, input().split()[:N]))
  max_beg = max(num)
  first_occr = num.index(max_beg)
  num.reverse()
  last_occr = num.index(max_beg)
  last_occr = N-1-last_occr
  
  if last_occr-first_occr >= midway:
   print(0)
  else:
   if first_occr==last_occr:
    first_occr=midway
    print(N-1-first_occr)
   else:
    gap = last_occr-first_occr
    first_occr=midway
    last_occr=first_occr+gap
    print(N-1-last_occr)
  T-=1

except EOFError as e:
 print("")

try:
 T = int(input())
 while T>0:
  N = int(input())
  midway = (N//2) - 1
  num = list(map(int, input().split()[:N]))
  max_beg = max(num)
  first_occr = num.index(max_beg)
  num.reverse()
  last_occr = num.index(max_beg)
  last_occr = N-1-last_occr
  
  if last_occr-first_occr >= midway:
   print(0)
  else:
   if first_occr==last_occr:
    first_occr=midway
    print(N-1-first_occr)
   else:
    gap = last_occr-first_occr
    first_occr=midway
    last_occr=first_occr+gap
    print(N-1-last_occr)
  T-=1

except EOFError as e:
 print("")

for _ in range(int(input())):
 n = int(input())
 w = (*map(int, input().split()),)
 m = 0
 for i in range(n):
  if w[i] > w[m]: m = i
 count = 0
 gap = 0
 for i in range(m+1, m+n+1):
  if w[i%n] == w[m]:
   count += max(0,gap-n//2+1)
   gap = 0
  else: gap += 1
 print(count)

for _ in range(int(input())):
 n=int(input())
 l=list(map(int,input().split()))
 u=[]
 k=0
 t=max(l)
 for i in range(n):
  if l[i]==t:
   break
 y=i+1
 while y!=i:
  if l[y] !=t:
   k+=1
  else:
   u.append(k)
   k=0
  y=(y+1)%n
 if k!=0:
  u.append(k)
 s=0
 for i in range(len(u)):
  s+=max(0,u[i]-n//2+1)
 print(s)

for _ in range(int(input())):
 n=int(input())
 l=list(map(int,input().split()))
 u=[]
 k=0
 t=max(l)
 for i in range(n):
  if l[i]==t:
   break
 y=i+1
 while y!=i:
  if l[y] !=t:
   k+=1
  else:
   u.append(k)
   k=0
  y=(y+1)%n
 if k!=0:
  u.append(k)
 s=0
 for i in range(len(u)):
  s+=max(0,u[i]-n//2+1)
 print(s)

for _ in range(int(input())):
 n=int(input())
 l=list(map(int,input().split()))
 u=[]
 k=0
 t=max(l)
 for i in range(n):
  if l[i]==t:
   break
 y=i+1
 while y!=i:
  if l[y] !=t:
   k+=1
  else:
   u.append(k)
   k=0
  y=(y+1)%n
 if k!=0:
  u.append(k)
 s=0
 for i in range(len(u)):
  s+=max(0,u[i]-n//2+1)
 print(s)

for _ in range(int(input())):
 n=int(input())
 l=list(map(int,input().split()))
 u=[]
 k=0
 t=max(l)
 for i in range(n):
  if l[i]==t:
   break
 y=i+1
 while y!=i:
  if l[y] !=t:
   k+=1
  else:
   u.append(k)
   k=0
  y=(y+1)%n
 if k!=0:
  u.append(k)
 s=0
 for i in range(len(u)):
  s+=max(0,u[i]-n//2+1)
 print(s)


t = int(input())
while t>0:
 t -= 1
 n = int(input())
 a = [int(x) for x in input().split()]
 m = max(a)
 ind = []
 for i in range(n):
  if a[i] == m:
   ind.append(i)
 if len(ind) == 1:
  print(n//2)
  continue
 f = ind[0]
 l = ind[-1]
 width = l-f+1
 if width > n//2:
  print(0)
 else:
  print(n//2-width+1)
# cook your dish here


t = int(input())
while t>0:
 t -= 1
 n = int(input())
 a = [int(x) for x in input().split()]
 m = max(a)
 ind = []
 for i in range(n):
  if a[i] == m:
   ind.append(i)
 if len(ind) == 1:
  print(n//2)
  continue
 f = ind[0]
 l = ind[-1]
 width = l-f+1
 if width > n//2:
  print(0)
 else:
  print(n//2-width+1)










test = int(input())
for _ in range(test):
 n=int(input())
 array = list(map(int, input().split()))
 maxi = max(array)
 index=[]
 for i in range(n):
  if array[i]==maxi:
   index.append(i)
   
 if len(index)==1:
  print(n//2)
 else:
  if index[len(index)-1]-index[0]>=n//2:
   print(0)
  else:
   print(n//2 - (index[len(index)-1]-index[0]))

t = int(input())
#for each test cases
for i in range(t):
 n=int(input())
 s=list(map(int,input().split()))
 m=max(s)
 c=[]
 for i in range(n):
  if(s[i]==m):
   c.append(i)
 if (len(c)==1):
  print(n//2)
 else:
  if (c[-1]-c[0]+1>n//2):
   print(0)
  else:
   x=c[-1]-c[0]
   print(n//2-x)

for _ in range(int(input())):
 N = int(input())
 seq = list(map(int,input().split()))
 maxx = max(seq)
 count = 0
 seq = seq+seq
 max_cont = 0
 for i in range(2*N):
  if seq[i] != maxx:
   count +=1
  else:
   if count > max_cont:
    max_cont = count
   count = 0
 if count > max_cont:
  max_cont = count
 print(max(max_cont-N//2+1,0))

from collections import defaultdict
test=int(input())
for _ in range(test):
 r=int(input())
 s=list(map(int,input().split()))
 m=max(s)
 l=int(r/2)
 step=0
 index=[]
 for i in range(len(s)):
  if s[i]==m:
    index.append(i)
 if len(index)==1:
  print(l)
 else:
  diff=index[-1]-index[0]
  if diff+1 > l:
   print(0)
  else:
   print(l-diff)

class node:
 def __init__(self, size, elements):
  self.size = size
  self.elements = elements


def solve(ele, s):
 m = max(ele)
 i, j = 0, 0
 flag = True
 for k in range(s):
  if ele[k] == m:
   j = k
   if flag:
    flag = False
    i = k
 if i == j:
  return s//2
 elif (j-i) >= s//2:
  return 0
 else:
  i, j = s//2, j + s//2 - i
  return s-j


def __starting_point():
 t = int(input())
 inputs = []
 for i in range(t):
  inputs.append(node(int(input()), list(map(int, input().split()))))
 for i in inputs:
  print(solve(i.elements, i.size))

__starting_point()