You are given an array $a$ consisting of $n$ integers. You have to find the length of the smallest (shortest) prefix of elements you need to erase from $a$ to make it a good array. Recall that the prefix of the array $a=[a_1, a_2, \dots, a_n]$ is a subarray consisting several first elements: the prefix of the array $a$ of length $k$ is the array $[a_1, a_2, \dots, a_k]$ ($0 \le k \le n$).

The array $b$ of length $m$ is called good, if you can obtain a non-decreasing array $c$ ($c_1 \le c_2 \le \dots \le c_{m}$) from it, repeating the following operation $m$ times (initially, $c$ is empty):  select either the first or the last element of $b$, remove it from $b$, and append it to the end of the array $c$. 

For example, if we do $4$ operations: take $b_1$, then $b_{m}$, then $b_{m-1}$ and at last $b_2$, then $b$ becomes $[b_3, b_4, \dots, b_{m-3}]$ and $c =[b_1, b_{m}, b_{m-1}, b_2]$.

Consider the following example: $b = [1, 2, 3, 4, 4, 2, 1]$. This array is good because we can obtain non-decreasing array $c$ from it by the following sequence of operations:  take the first element of $b$, so $b = [2, 3, 4, 4, 2, 1]$, $c = [1]$;  take the last element of $b$, so $b = [2, 3, 4, 4, 2]$, $c = [1, 1]$;  take the last element of $b$, so $b = [2, 3, 4, 4]$, $c = [1, 1, 2]$;  take the first element of $b$, so $b = [3, 4, 4]$, $c = [1, 1, 2, 2]$;  take the first element of $b$, so $b = [4, 4]$, $c = [1, 1, 2, 2, 3]$;  take the last element of $b$, so $b = [4]$, $c = [1, 1, 2, 2, 3, 4]$;  take the only element of $b$, so $b = []$, $c = [1, 1, 2, 2, 3, 4, 4]$ — $c$ is non-decreasing. 

Note that the array consisting of one element is good.

Print the length of the shortest prefix of $a$ to delete (erase), to make $a$ to be a good array. Note that the required length can be $0$.

You have to answer $t$ independent test cases.


-----Input-----

The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) — the number of test cases. Then $t$ test cases follow.

The first line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of $a$. The second line of the test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 2 \cdot 10^5$), where $a_i$ is the $i$-th element of $a$.

It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$).


-----Output-----

For each test case, print the answer: the length of the shortest prefix of elements you need to erase from $a$ to make it a good array.


-----Example-----
Input
5
4
1 2 3 4
7
4 3 3 8 4 5 2
3
1 1 1
7
1 3 1 4 5 3 2
5
5 4 3 2 3

Output
0
4
0
2
3



-----Note-----

In the first test case of the example, the array $a$ is already good, so we don't need to erase any prefix.

In the second test case of the example, the initial array $a$ is not good. Let's erase first $4$ elements of $a$, the result is $[4, 5, 2]$. The resulting array is good. You can prove that if you erase fewer number of first elements, the result will not be good.

Difficulty: introductory

Starter code:

Solutions:
for __ in range(int(input())):
    n = int(input())
    ar = list(map(int, input().split()))
    ar.reverse()
    ans = n - 1
    flag = False
    for i in range(1, n):
        if ar[i] < ar[i - 1]:
            flag = True
        if flag:
            if ar[i] > ar[i - 1]:
                break
        ans -= 1
    print(ans)

def solve():
    n = int(input())
    a = list(map(int, input().split()))
    prev = a[-1]
    cnt = 1
    status = 0
    for i in range(n - 2, -1, -1):
        v = a[i]
        if status == 0:
            if v < prev:
                status = 1
            cnt += 1
            prev = v
            continue
        elif status == 1:
            if v > prev:
                break
            cnt += 1
            prev = v
            continue
    print(n - cnt)

t = int(input())
for _ in range(t):
    solve()

for testcase in range(int(input())):
    n = int(input())
    a = list(reversed(list(map(int, input().split()))))
    
    i = 1
    while i < n and a[i - 1] <= a[i]:
        i += 1
    while i < n and a[i - 1] >= a[i]:
        i += 1
    print(n - i)
        


def solve():
    n = int(input())
    b = list(map(int, input().split()))
    key = True
    for i in range(n - 2, -1, -1):
        if key and b[i] < b[i + 1]:
            key = False
        elif not key and b[i] > b[i + 1]:
            print(i + 1)
            return
    print(0)


for _ in range(int(input())):
    solve()


import sys
input = sys.stdin.readline
for nt in range(int(input())):
	n = int(input())
	a = list(map(int,input().split()))
	if n==1:
		print (0)
		continue
	dec = 0
	ind = -1
	for i in range(n-2,-1,-1):
		if a[i]<a[i+1]:
			dec = 1
		elif a[i]>a[i+1] and dec:
			ind = i
			break
	print (ind+1)

import sys
#q=1
q=int(input())
for i in range(q):
    #n,m=[int(j) for j in sys.stdin.readline().split()]
    n=int(sys.stdin.readline())
    a=[int(j) for j in sys.stdin.readline().split()]
    a=a[::-1]
    swap=0
    t=n
    for i in range(n-1):
        if swap==0:
            if a[i]>a[i+1]:
                swap=1
        elif swap==1:
            if a[i]<a[i+1]:
                swap=2
                t=i+1
                break
    print(n-t)


import sys
INF = 10**20
MOD = 10**9 + 7
I = lambda:list(map(int,input().split()))
from math import gcd
from math import ceil
from collections import defaultdict as dd, Counter
from bisect import bisect_left as bl, bisect_right as br
from itertools import permutations

"""
Facts and Data representation
Constructive? Top bottom up down
"""
def solve():
  n, = I()
  a = I()
  j = n - 1
  while j > 0 and a[j - 1] >= a[j]:
    j -= 1
  mx = a[j]
  while j > 0 and a[j - 1] <= a[j] and a[j - 1] <= mx:
    j -= 1
  print(j)

t, = I()
while t:
  t -= 1
  solve()

from sys import stdin

tt = int(stdin.readline())

for loop in range(tt):

    n = int(stdin.readline())
    a = list(map(int, stdin.readline().split()))

    flag = 0
    for i in range(n-1,0,-1):
        if flag == 0 and a[i-1] < a[i]:
            flag = 1
        elif flag == 1 and a[i-1] > a[i]:
            print (i)
            break
    else:
        print(0)


for _ in range(int(input())):
    n = int(input())
    a = list(map(int, input().split()))
    rising = False
    i = n-1
    for _ in range(2):
        while i > 0:
            if a[i-1] == a[i] or (a[i-1] < a[i]) == rising:
                i -= 1
            else:
                rising = not rising
                break
    print(i)


import sys
input = sys.stdin.readline
for f in range(int(input())):
    n=int(input())
    a=list(map(int,input().split()))
    i=n-2
    poss=True
    dec=True
    while i>=0 and poss:
        if dec:
            if a[i]<a[i+1]:
                dec=False
        else:
            if a[i]>a[i+1]:
                poss=False
                i+=1
        i-=1
    print(i+1)


t=int(input())
for _ in range(t):
  n=int(input())
  a=list(map(int,input().split()))
  t=0
  for i in range(n-1,0,-1):
    if a[i]>a[i-1]:
      t=i
      break
    if i==1:
      t=0
  if t<=1:
    print(0)
    continue
  s=0
  for i in range(t,0,-1):
    if a[i]<a[i-1]:
      s=i
      break
    if i==1:
      s=0
  print(s)

#Pye
import os, sys
def _(): return sys.stdin.readline()
def __(): return sys.stdin.readline().split()
N = 100005
q = int(_())
for id in range(q):
    n = int(_())
    a = list(reversed(list(map(int, __()))))
    ans = 0
    check = 0
    for x in range(1, len(a)):
        if not check and a[x] < a[x-1]: check = 1
        if check and a[x] > a[x-1]:
            ans = len(a)-x
            break
    print(ans)
    






import sys
input = sys.stdin.readline
 
t = int(input())
 
for _ in range(t):
    l = int(input())
    m = list(map(int, input().split()))
    m = m[::-1]
    
    uni = []
    gro = 1
    ok = 1
    ans = 0
    
    for j in range(len(m) - 1):
        if gro == 0:
            if m[j+1] > m[j]:
                ok = 0
                ans = j+1
                break
        
        if m[j+1] < m[j]:
            gro = 0
            
    if ans == 0:
        print(ans)
    else:
        print(l-ans)

for _ in range(int(input())):
    n = int(input())
    a = input().split(' ')
    for i in range(n):
        a[i] = int(a[i])
    i = n-1
    while i > 0:
        if a[i] <= a[i-1]:
            i -= 1
            continue
        else:
            break
    while i > 0:
        if a[i] >= a[i-1]:
            i -= 1
            continue
        else:
            break
    print(i)


"""
                            pppppppppppppppppppp
                         ppppp  ppppppppppppppppppp
                      ppppppp    ppppppppppppppppppppp
                      pppppppp  pppppppppppppppppppppp
                      pppppppppppppppppppppppppppppppp
                              pppppppppppppppppppppppp
       ppppppppppppppppppppppppppppppppppppppppppppppp  pppppppppppppppppppp
      pppppppppppppppppppppppppppppppppppppppppppppppp  ppppppppppppppppppppp
     ppppppppppppppppppppppppppppppppppppppppppppppppp  pppppppppppppppppppppp
    ppppppppppppppppppppppppppppppppppppppppppppppp    pppppppppppppppppppppppp
   pppppppppppppppppppppppppppppppppppppppppppppp     pppppppppppppppppppppppppp
  ppppppppppppppppppppppppppppppppppppppppppppp      pppppppppppppppppppppppppppp
  pppppppppppppppppppppppppppppppp               pppppppppppppppppppppppppppppppp
  pppppppppppppppppppppppppppp     pppppppppppppppppppppppppppppppppppppppppppppp
  ppppppppppppppppppppppppppp    pppppppppppppppppppppppppppppppppppppppppppppppp
    pppppppppppppppppppppppp  pppppppppppppppppppppppppppppppppppppppppppppppppp
     ppppppppppppppppppppppp  ppppppppppppppppppppppppppppppppppppppppppppppppp
      pppppppppppppppppppppp  ppppppppppppppppppppppppppppppppppppppppppppppp
       ppppppppppppppppppppp  ppppppppppppppppppppppppppppppppppppppppppppp
                              pppppppppppppppppppppppp
                              pppppppppppppppppppppppppppppppp
                              pppppppppppppppppppppp  pppppppp
                              ppppppppppppppppppppp    ppppppp
                                 ppppppppppppppppppp  ppppp
                                    pppppppppppppppppppp
"""


import sys
from functools import lru_cache, cmp_to_key
from heapq import merge, heapify, heappop, heappush, nsmallest
from math import ceil, floor, gcd, fabs, factorial, fmod, sqrt, inf
from collections import defaultdict as dd, deque, Counter as C
from itertools import combinations as comb, permutations as perm
from bisect import bisect_left as bl, bisect_right as br, bisect
from time import perf_counter
from fractions import Fraction
from decimal import Decimal
# sys.setrecursionlimit(pow(10, 6))
# sys.stdin = open("input.txt", "r")
# sys.stdout = open("output.txt", "w")
mod = pow(10, 9) + 7
mod2 = 998244353
def data(): return sys.stdin.readline().strip()
def out(var): sys.stdout.write(str(var)+"\n")
def outa(*var, end="\n"): sys.stdout.write(' '.join(map(str, var)) + end)
def l(): return list(sp())
def sl(): return list(ssp())
def sp(): return list(map(int, data().split()))
def ssp(): return list(map(str, data().split()))
def l1d(n, val=0): return [val for i in range(n)]
def l2d(n, m, val=0): return [l1d(n, val) for j in range(m)]


for _ in range(int(data())):
    n = int(data())
    arr = l()
    low, high = 0, n
    answer = n
    while low <= high:
        mid = (low + high) >> 1
        new = arr[mid:]
        res = []
        i, j = 0, len(new)-1
        while i < j:
            if new[i] > new[j]:
                res.append(new[j])
                j -= 1
            else:
                res.append(new[i])
                i += 1
        if res == sorted(res):
            answer = min(answer, mid)
            high = mid - 1
        else:
            low = mid + 1
    out(answer)


from sys import stdin
                        
t = int(stdin.readline().strip())
for i in range(t):                           
    n = int(stdin.readline().strip())
    A = list(map(int, stdin.readline().strip().split(' ')))
    i = n-2
    while i >= 0 and A[i] >= A[i+1]:
        i -= 1
    while i >= 0 and A[i] <= A[i+1]:
        i -= 1
    print(i+1)


def solve(n, a):
    increasing = True
    prev = float('-inf')
    for i in range(n-1, -1, -1):
        if a[i] < prev and increasing:
            increasing = False
        if a[i] > prev and not increasing:
            return i+1
        prev = a[i]
    return 0

t = int(input())
for _ in range(t):
    n = int(input())
    a = list(map(int, input().split()))
    # x, y, z = map(int, input().split())
    print(solve(n, a))

tests = int(input())
for test in range(tests):
    n = int(input())
    a = [int(i) for i in input().split()]
    x = -1
    for i in range(n-2, -1, -1):
        if a[i] < a[i + 1]:
            x = i
            break
    if x == -1:
        print(0)
        continue
    y = -1
    for i in range(x - 1, -1, -1):
        if a[i] > a[i + 1]:
            y = i
            break
    if y == -1:
        print(0)
        continue
    print(y + 1)