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.

Source: codeforces | Difficulty: EASY
Tags: Greedy algorithms


Solutions:
from math import *
sInt = lambda : int(input())
mInt = lambda : map(int, input().split())
lInt = lambda : list(map(int, input().split()))
t = sInt()
for _ in range(t):
	n = sInt()
	a = lInt()
	x = n - 1
	while a[x - 1] >= a[x] and x > 0:
		x -= 1
	while a[x - 1] <= a[x] and x > 0:
		x -= 1
	print(x)


def main():
	for t in range(int(input())):
		n = int(input())
		a = [int(i) for i in input().split()]
		i = n - 2
		while i >= 0 and a[i] >= a[i + 1]:
			i -= 1
		peak = i + 1
		i = peak - 1
		while i >= 0 and a[i] <= a[i + 1]:
			i -= 1
		deep = i + 1
		print(deep)
main()


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)


import sys

def is_sorted(arr):
	prev = 0
	for i in arr:
		if i < prev:
			return False
		prev = i
	return True
t = int(sys.stdin.readline().rstrip())
for _ in range(t):
	n = int(sys.stdin.readline().rstrip())
	arr = list(map(int, sys.stdin.readline().rstrip().split()))
	if is_sorted(arr):
		print(0)
		continue
	prev = cur = n - 1
	while arr[cur] - arr[prev] >= 0 and cur >= 0:
		prev = cur
		cur -= 1
	if prev == 0:
		print(0)
		continue
	else:
		while arr[cur] - arr[prev] <= 0 and cur >= 0:
			prev = cur
			cur -= 1
		print(prev)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	if n <= 2:
		print(0)
		continue
	flip = 0
	last = 1
	if a[-2] - a[-1] < 0:
		flip = 1
		last = -1
	ans = 0
	for i in range(n - 3, -1, -1):
		if a[i] - a[i + 1] > 0 and last > 0:
			continue
		elif a[i] - a[i + 1] < 0 and last > 0:
			flip += 1
			last = -1
		elif a[i] - a[i + 1] > 0:
			flip += 1
		elif a[i] == a[i + 1]:
			continue
		if flip > 1:
			ans = i + 1
			break
	print(ans)


def tc():
	n = int(input())
	a = [int(x) for x in input().split()]
	if len(a) < 3:
		print(0)
		return
	for i in range(n - 2, -1, -1):
		if a[i] < a[i + 1]:
			break
	for j in range(i, 0, -1):
		if a[j] < a[j - 1]:
			break
	else:
		j = 0
	print(j)
T = int(input())
for _ in range(T):
	tc()


from sys import stdin, stdout
from math import gcd, ceil, sqrt
from collections import Counter, deque
from bisect import bisect_left, bisect_right
ii1 = lambda : int(stdin.readline().strip())
is1 = lambda : stdin.readline().strip()
iia = lambda : list(map(int, stdin.readline().strip().split()))
isa = lambda : stdin.readline().strip().split()
mod = 1000000007
tc = ii1()
for _ in range(tc):
	n = ii1()
	arr = iia()
	check = 0
	for i in range(len(arr) - 1)[::-1]:
		if arr[i] < arr[i + 1]:
			check = i
			break
	final = 0
	if check > 0:
		for i in range(check):
			if arr[i] > arr[i + 1]:
				final = i + 1
	print(final)


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	if n < 3 or sorted(l) == l:
		print(0)
		continue
	elif sorted(l, reverse=True) == l:
		print(0)
		continue
	ind = 0
	for i in range(n - 2, -1, -1):
		if l[i + 1] > l[i]:
			ind = i
			break
	ans = 0
	for i in range(ind - 1, -1, -1):
		if l[i] > l[i + 1]:
			ans = i + 1
			break
	print(ans)


import os
if 'LOCAL_COMPROG' in os.environ:
	filename = __file__.replace('py', 'in')
	if os.path.exists(filename):
		print('[WARN] swapping stdin for test file')
		import sys
		sys.stdin = open(filename, 'r')
for __ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	ans = n - 1
	while ans and a[ans - 1] >= a[ans]:
		ans -= 1
	while ans and a[ans - 1] <= a[ans]:
		ans -= 1
	print(ans)


t = int(input())
for x in range(0, t):
	m = int(input())
	l = list(map(int, input().strip().split()))[:m]
	l2 = []
	if m == 1:
		print('0')
		continue
	for i in range(1, m):
		if l[i] - l[i - 1] > 0:
			l2.append('+')
		elif l[i] - l[i - 1] < 0:
			l2.append('-')
		else:
			l2.append('0')
	if l2[-1] == '0':
		l2[-1] == '-'
	for i in range(m - 3, -1, -1):
		if l2[i] == '0':
			l2[i] = l2[i + 1]
	ct1 = 1
	for i in range(m - 2, -1, -1):
		if l2[i] == '+' and l2[i - 1] == '-':
			break
		ct1 += 1
	ct1 += 1
	z = m - ct1
	if z == -1:
		print('0')
	else:
		print(z)


for i in range(int(input())):
	n = int(input())
	b = list(map(int, input().split()))
	bs = sorted(b)
	pos = len(b) - 1
	if pos == 0 or bs == b:
		print(0)
	else:
		while pos > 0:
			if b[pos - 1] >= b[pos]:
				if pos == 1:
					print(pos - 1)
				pos -= 1
				continue
			else:
				while pos > 0:
					if b[pos] >= b[pos - 1]:
						if pos == 1:
							print(pos - 1)
						pos -= 1
						continue
					else:
						print(pos)
						break
			break


from collections import defaultdict
import math
t = int(input())
for _ in range(t):
	n = int(input())
	inp = input().split(' ')
	inp = list(map(int, inp))
	x = 0
	i = n - 1
	while i >= 1 and inp[i] <= inp[i - 1]:
		x += 1
		i -= 1
	while i >= 1 and inp[i] >= inp[i - 1]:
		x += 1
		i -= 1
	print(n - x - 1)


from math import *
from copy import *
from string import *
from random import *
from sys import stdin, stdout
from sys import maxsize
from operator import *
from itertools import *
from collections import Counter
import math
import math
import time
from queue import Queue

def seive(n):
	l = [True]
	l = l * (n + 1)
	for i in range(2, int(sqrt(n)) + 1):
		if l[i] == True:
			val = i * i
			while val < len(l):
				l[val] = False
				val += i
	prime = []
	for i in range(2, len(l)):
		if l[i] == True:
			prime.append(i)
	return prime

def dp(l, i, n, val, ans):
	if i >= n or val < 0:
		return 1000000
	elif val == 0:
		return 0
	else:
		if ans[i] == 1000000:
			ans[i] = min(min(1 + dp(l, i + 1, n, val - l[i], ans), 1 + dp(l, i, n, val - l[i], ans)), dp(l, i + 1, n, val, ans))
		return ans[i]
from queue import Queue

def factors(n):
	l = []
	for i in range(1, int(sqrt(n)) + 1):
		if n % i == 0:
			if n // i == i:
				l.append(i)
			else:
				l.append(i)
				l.append(n // i)
	return l

def check1(s, v, x):
	while True:
		if v not in s:
			return v
		else:
			v -= 1

def check2(s, v, x):
	while True:
		if v not in s:
			return v
		else:
			v += 1

def up(n):
	j = n - 1
	while j > 0:
		if n % j == 0:
			break
		j -= 1
	return j

def fac(n):
	c = 1
	i = 1
	while i <= n:
		c *= i
		i += 1
	return c

def check_happy(n):
	ss = set()
	print(n)
	while n != 1:
		val = n
		s = 0
		print(val)
		while val != 0:
			s += (val % 10) ** 2
			val = val // 10
		print(val)
		print('hi')
		if val in ss:
			return False
		else:
			n = val
			ss.add(n)
	return True

def update(a, b):
	c = 1
	val = a
	while a < b:
		print(c, a)
		c += c
		a += a
	print(1)
	while a > b:
		print()
		a -= val
		c -= 1
	return c
import random as rand

def gcd(a, b):
	if a == 0:
		return b
	return gcd(b % a, a)
from itertools import permutations
t = int(stdin.readline())
for i in range(t):
	n = int(stdin.readline())
	l = list(map(int, stdin.readline().split(' ')))
	l = l[::-1]
	dec = -1
	c = 0
	for i in range(n - 1):
		if l[i] == l[i + 1]:
			c += 1
		elif l[i] < l[i + 1]:
			if dec != -1:
				break
			else:
				c += 1
		else:
			dec = 1
			c += 1
	stdout.write(str(n - (c + 1)) + '\n')


T = int(input())
for _ in range(T):
	n = int(input())
	a = list(map(int, input().split()))
	ap = []
	for i in range(n - 1):
		if a[i + 1] - a[i] > 0:
			ap.append(1)
		elif a[i + 1] - a[i] < 0:
			ap.append(-1)
		else:
			ap.append(0)
	ap = [999999] + ap
	aa = 0
	an = 0
	one = False
	for i in range(n - 1, 0, -1):
		an += 1
		if ap[i] == 1:
			one = True
		if one and ap[i - 1] == -1:
			break
	print(n - (an + 1))


T = int(input())
for _ in range(T):
	N = int(input())
	a = list(map(int, input().split()))
	i = N - 1
	while i > 0:
		if a[i] > a[i - 1]:
			break
		i -= 1
	while i > 0:
		if a[i] < a[i - 1]:
			break
		i -= 1
	print(i)


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


for i in range(int(input())):
	n = int(input())
	num = list(map(int, input().split()))
	flag = False
	flag2 = True
	i = n - 1
	while i > 0 and num[i] <= num[i - 1]:
		i -= 1
	while i > 0 and num[i] >= num[i - 1]:
		i -= 1
	print(i)


t = int(input().split()[0])
for case in range(t):
	n = int(input().split()[0])
	arr = list(map(int, input().split()))
	decrease = False
	ans = 0
	for i in range(n - 1, 0, -1):
		if arr[i - 1] < arr[i] and (not decrease):
			decrease = True
		if arr[i - 1] > arr[i] and decrease:
			ans = i
			break
	print(ans)


def solve(l):
	start = 0
	for i in range(len(l) - 1):
		if l[-i - 2] < l[-i - 1]:
			start = i + 1
			break
	else:
		return 0
	for i in range(start, len(l) - 1):
		if l[-i - 2] > l[-i - 1]:
			return len(l) - i - 1
	return 0
for _ in range(int(input())):
	n = int(input())
	l = [int(x) for x in input().split()]
	print(solve(l))


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	a.reverse()
	i = 0
	while i < n - 1:
		if a[i] <= a[i + 1]:
			i += 1
		else:
			break
	ass = i
	b = 0
	while i < n - 1:
		if a[i] >= a[i + 1]:
			i += 1
			b += 1
		else:
			break
	c = ass + b
	print(n - c - 1)


t = int(input())
for t in range(t):
	p = int(input())
	lst = list(map(int, input().split()))
	i = p - 1
	if p <= 2:
		print(0)
	else:
		while lst[i] <= lst[i - 1]:
			i -= 1
			if i == 0:
				break
		if i == 0:
			print(0)
		else:
			i -= 1
			if i != 0:
				while lst[i] >= lst[i - 1]:
					i -= 1
					if i == 0:
						break
			if i == 0:
				print(0)
			else:
				print(i)


for z in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	pos = n - 1
	while pos > 0 and a[pos - 1] >= a[pos]:
		pos -= 1
	while pos > 0 and a[pos - 1] <= a[pos]:
		pos -= 1
	print(pos)


numTestCases = int(input())
caseLengths = []
cases = []
for i in range(numTestCases):
	caseLengths.append(int(input()))
	cases.append(list(map(int, input().split())))

def calculateAns(isIncrease, isDecrease, startPos, case):
	if isDecrease:
		for i in range(startPos - 1, -1, -1):
			if i == 0:
				print(str(0))
			elif i > 0 and case[i - 1] > case[i]:
				tempAns = i
				print(str(tempAns))
				return tempAns
	elif isIncrease:
		for i in range(startPos - 1, -1, -1):
			if i == 0:
				print(str(0))
			elif i != 0 and case[i - 1] < case[i]:
				calculateAns(False, True, i, case)
				break
ansList = []
for case in cases:
	if len(case) == 1:
		ans = str(0)
		print(ans)
	else:
		isDecrease = True
		isIncrease = False
		if case[len(case) - 1] == case[len(case) - 2]:
			startPos = 0
			for i in range(len(case) - 2, -1, -1):
				if case[i - 1] > case[i]:
					isDecrease = False
					isIncrease = True
					startPos = i
					break
				elif case[i - 1] < case[i]:
					isIncrease = False
					isDecrease = True
					startPos = i
					break
				else:
					continue
			if startPos == 0:
				print(str(0))
			else:
				ans = calculateAns(isIncrease, isDecrease, startPos, case)
		elif case[len(case) - 1] > case[len(case) - 2]:
			isDecrease = True
			isIncrease = False
			startPos = len(case) - 1
			ans = calculateAns(isIncrease, isDecrease, startPos, case)
		elif case[len(case) - 1] < case[len(case) - 2]:
			isIncrease = True
			isDecrease = False
			startPos = len(case) - 1
			ans = calculateAns(isIncrease, isDecrease, startPos, case)


t = int(input())
while t > 0:
	size = int(input())
	inp = list(map(int, input().split()))
	flag = 0
	temp = size - 1
	ans = 0
	while temp > 0:
		if inp[temp] <= inp[temp - 1] and (flag == 0 or flag == 1):
			flag = 1
			temp -= 1
		elif inp[temp] >= inp[temp - 1] and (flag == 1 or flag == 2):
			flag = 2
			temp -= 1
		elif inp[temp] >= inp[temp - 1] and (flag == 0 or flag == 10):
			flag = 10
			temp -= 1
		elif inp[temp] <= inp[temp - 1] and (flag == 2 or flag == 10):
			flag = 3
			ans = temp
			temp -= 1
			break
	if flag == 3:
		print(ans)
	else:
		print(0)
	t -= 1


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	a = a[::-1]
	up = 0
	if n == 1:
		print(0)
		continue
	res = 0
	f = 0
	for i in range(1, n):
		if a[i] < a[i - 1]:
			f = 1
		elif f and a[i] > a[i - 1]:
			res = i
			break
		else:
			continue
	if res == 0:
		print(0)
	else:
		print(n - res)


numberOfTc = int(input())
for ctr in range(1, numberOfTc + 1):
	n = int(input())
	lis = list(map(int, input().split()))
	pos = n - 1
	while pos > 0 and lis[pos - 1] >= lis[pos]:
		pos -= 1
	while pos > 0 and lis[pos - 1] <= lis[pos]:
		pos -= 1
	print(pos)


from collections import deque
import sys

def inp():
	return sys.stdin.readline().strip()
for _ in range(int(inp())):
	n = int(inp())
	a = list(map(int, inp().split()))
	if n < 2:
		print(0)
		continue
	j = n - 1
	while j > 0 and a[j - 1] >= a[j]:
		j -= 1
	while j > 0 and a[j - 1] <= a[j]:
		j -= 1
	print(j)


def prefix_erasure(a):
	n = len(a)
	if n < 3:
		return 0
	passed_crest = False
	good_size = 1
	for i in range(n - 1, 0, -1):
		if not passed_crest:
			if a[i - 1] >= a[i]:
				good_size += 1
			else:
				passed_crest = True
				good_size += 1
		elif a[i - 1] <= a[i]:
			good_size += 1
		else:
			break
	return n - good_size
t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(x) for x in input().split()]
	print(prefix_erasure(a))


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	s = 1
	i = n - 1
	c = 0
	while i >= 1:
		if l[i] <= l[i - 1] and c == 0:
			s += 1
		elif l[i] >= l[i - 1]:
			c = 1
			s += 1
		else:
			break
		i -= 1
	print(n - s)


t = int(input())
for i in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	last = arr[n - 1]
	left_idx = n - 1
	while left_idx - 1 >= 0 and arr[left_idx - 1] >= last:
		left_idx -= 1
		last = arr[left_idx]
	while left_idx - 1 >= 0 and arr[left_idx - 1] <= last:
		left_idx -= 1
		last = arr[left_idx]
	print(left_idx)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	if n <= 2:
		print(0)
		continue
	ci = 1
	cd = 1
	for i in range(0, n - 1):
		if a[i] <= a[i + 1]:
			ci += 1
		elif a[i] >= a[i + 1]:
			cd += 1
	if ci == n or cd == n:
		print(0)
	else:
		j = n - 1
		ans = 0
		fg = 0
		while a[j] == a[j - 1]:
			j -= 1
		if a[j - 1] < a[j]:
			for i in range(j, 0, -1):
				if a[i] < a[i - 1]:
					ans = i
					break
		elif a[j - 1] > a[j]:
			for i in range(j, 0, -1):
				if a[i - 1] < a[i]:
					fg = 1
				elif a[i - 1] > a[i] and fg == 1:
					ans = i
					break
		print(ans)


def solve(n, d):
	big = float('inf')
	small = float('-inf')
	if n < 3:
		return 0
	peak = -1
	for i in range(n - 1, 0, -1):
		if d[i] > d[i - 1]:
			peak = i
			break
	ans = 0
	if peak > -1:
		for i in range(peak - 1, 0, -1):
			if d[i] < d[i - 1]:
				ans = i
				break
	return ans

def main():
	t = int(input())
	for i in range(t):
		n = int(input())
		d = input()
		d = [int(i) for i in d.split()]
		ans = solve(n, d)
		print(ans)
main()


try:
	for _ in range(int(input())):
		n = int(input())
		arr = [int(i) for i in input().split()]
		ans = n - 1
		while ans > 0 and arr[ans - 1] >= arr[ans]:
			ans -= 1
		while ans > 0 and arr[ans - 1] <= arr[ans]:
			ans -= 1
		print(ans)
except:
	pass


for _ in range(int(input())):
	n = int(input())
	arr = [int(x) for x in input().split()]
	ans = -1
	temp = 1
	for i in range(n - 1, 0, -1):
		if arr[i] <= arr[i - 1]:
			temp += 1
		else:
			break
	ans = max(ans, temp)
	temp = 1
	for i in range(n - 1, 0, -1):
		if arr[i] >= arr[i - 1]:
			temp += 1
		else:
			break
	ans = max(ans, temp)
	temp = 1
	i = n - 1
	while i > 0:
		if arr[i] <= arr[i - 1]:
			temp += 1
			i -= 1
		else:
			break
	while i > 0:
		if arr[i] >= arr[i - 1]:
			temp += 1
			i -= 1
		else:
			break
	ans = max(ans, temp)
	print(n - ans)


from fractions import Fraction
from collections import defaultdict
import math
import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))

def input():
	return sys.stdin.readline().rstrip('\r\n')
t = int(input())
for _ in range(t):
	n = int(input())
	l = list([int(x) for x in input().split()])
	s = len(l) - 1
	while s >= 0 and l[s] <= l[s - 1]:
		s -= 1
	while s >= 0 and l[s] >= l[s - 1]:
		s -= 1
	if s < 0:
		print('0')
	else:
		print(s)


for s in [*open(0)][2::2]:
	a = (*map(int, s.split()),)
	s = ''.join((str((x > y) - (x < y) + 1) for (x, y) in zip(a, a[1:])))
	i = s.rfind('0')
	i = (s.rfind('2', 0, i), i)[i < 0]
	print(i + 1)


import math
t = int(input())
for i in range(t):
	string = ''
	n = int(input())
	list1 = input().split()
	for i in range(len(list1)):
		list1[i] = int(list1[i])
	pos = n - 1
	notmax = True
	if pos == 0 or list1[pos] > list1[pos - 1]:
		notmax = False
	while notmax:
		pos -= 1
		if pos == 0 or list1[pos] > list1[pos - 1]:
			notmax = False
	notmin = True
	if pos == 0 or list1[pos] < list1[pos - 1]:
		notmin = False
	while notmin:
		pos -= 1
		if pos == 0 or list1[pos] < list1[pos - 1]:
			notmin = False
	print(pos)


from sys import stdin
input = stdin.readline
T = int(input())
for case in range(T):
	N = int(input())
	Nums = [*map(int, input().split())]
	flip = False
	Ans = 0
	for i in range(N - 1, 0, -1):
		if not flip and Nums[i] > Nums[i - 1]:
			flip = True
		elif flip and Nums[i] < Nums[i - 1]:
			flip = False
			Ans = i
			break
	print(Ans)


for _ in range(int(input())):
	x = int(input())
	(*li,) = map(int, input().split())
	a = 0
	cnt = 0
	inc = True
	for i in li[::-1]:
		if i >= a and inc:
			cnt += 1
		elif i <= a:
			inc = False
			cnt += 1
		else:
			break
		a = i
	print(x - cnt)


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	i = 0
	flag = False
	naverBreak = True
	for i in range(n - 2, -1, -1):
		if flag == False:
			if arr[i] < arr[i + 1]:
				flag = True
		elif arr[i] > arr[i + 1]:
			naverBreak = False
			break
	if naverBreak == True:
		print(0)
	else:
		print(i + 1)


t = int(input())
for _ in range(t):
	n = int(input())
	ar = list(map(int, input().split()))
	dec = 0
	for j in range(n - 1, -1, -1):
		if ar[j] > ar[j - 1] and dec == 0:
			dec = 1
		elif ar[j] < ar[j - 1] and dec == 1:
			break
	print(j)


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split(' ')))
	if n <= 2:
		print(0)
	else:
		p = n - 2
		while p != -1 and a[p] >= a[p + 1]:
			p = p - 1
		while p != -1 and a[p] <= a[p + 1]:
			p = p - 1
		print(p + 1)


import sys
q = int(input())
for i in range(q):
	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
input = sys.stdin.readline
t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(x) for x in input().split()]
	seen_up = False
	seen_down2 = False
	for i in range(n - 1, 0, -1):
		if a[i] - a[i - 1] > 0:
			seen_up = True
		if a[i] - a[i - 1] < 0 and seen_up:
			seen_down2 = True
			print(i)
			break
	if not seen_down2:
		print(0)


for _ in range(int(input())):
	n = int(input())
	a = [int(i) for i in input().split()]
	ind = 0
	bad_elements = 0
	state = 0
	border = 0
	seq = 0
	while ind < n:
		if state == 0:
			if ind == 0:
				ind += 1
				continue
			if a[ind - 1] <= a[ind]:
				ind += 1
			else:
				ind -= 1
				state = 1
		if state == 1:
			if ind == n - 1:
				break
			if a[ind] > a[ind + 1]:
				ind += 1
				seq = 0
			elif a[ind] == a[ind + 1]:
				ind += 1
				seq += 1
			else:
				state = 0
				bad_elements = ind - seq
				ind += 1
	print(bad_elements)


for _ in range(int(input())):
	N = int(input())
	a = list(map(int, input().split()))
	if len(set(a)) == 1 or a == sorted(a):
		print(0)
		continue
	a.reverse()
	a = [0] + a
	if a == sorted(a):
		print(0)
	else:
		Dec = False
		ans = 0
		for i in range(1, N + 1):
			if Dec:
				if a[i] <= a[i - 1]:
					ans += 1
				elif a[i] > a[i - 1]:
					break
			elif a[i] >= a[i - 1]:
				ans += 1
			elif a[i] < a[i - 1]:
				ans += 1
				Dec = True
		print(N - ans)


import sys

def solve(a):
	a = a[::-1]
	n = len(a)
	if a[1] < a[0]:
		b = [a[i - 1] - a[i] for i in range(1, n)]
		ndown = n
		for i in range(1, n):
			if a[i] > a[i - 1]:
				ndown = i
				break
		return n - ndown
	else:
		ngood = n
		for i in range(1, n):
			if a[i] < a[i - 1]:
				ngood = i
				break
		if ngood < n:
			ndown = n
			for i in range(ngood + 1, n):
				if a[i] > a[i - 1]:
					ndown = i
					break
			ngood = ndown
		return n - ngood
IN = [x.strip() for x in sys.stdin.readlines()]
T = int(IN[0])
cur = 1
for ti in range(T):
	n = int(IN[cur])
	a = [int(x) for x in IN[cur + 1].split(' ')]
	if n == 1:
		print(0)
	else:
		print(solve(a))
	cur += 2


n = int(input())
while n:
	m = int(input())
	arr = list(map(int, input().split()))
	i = m - 1
	while i > 0 and arr[i] <= arr[i - 1]:
		i -= 1
	i -= 1
	while i > 0 and arr[i] >= arr[i - 1]:
		i -= 1
	print(max(i, 0))
	n -= 1


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	q = n - 1
	while a[q] <= a[q - 1] and q != 0:
		q -= 1
	while a[q] >= a[q - 1] and q != 0:
		q -= 1
	print(q)


T = int(input())
while T > 0:
	n = int(input())
	arr = list(map(int, input().split()))
	up = True
	prev = -1
	index = -1
	for i in range(n - 1, -1, -1):
		if arr[i] > prev:
			if up is False:
				index = i
				break
		elif arr[i] < prev:
			up = False
		prev = arr[i]
	print(index + 1)
	T -= 1


t = int(input())
for i in range(t):
	n = int(input())
	a = list((int(num) for num in input().split()))
	k = n - 1
	while k > 0:
		if a[k] <= a[k - 1]:
			k -= 1
		else:
			break
	while k > 0:
		if a[k] >= a[k - 1]:
			k -= 1
		else:
			break
	print(k)


import sys
from collections import defaultdict as dd
from collections import Counter as cc
from queue import Queue
import math
import itertools
try:
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
except:
	pass
input = lambda : sys.stdin.buffer.readline().rstrip()
for _ in range(int(input())):
	w = int(input())
	a = list(map(int, input().split()))
	l = 0
	k = a[-1]
	q = w
	for i in range(w - 1, -1, -1):
		if l == 0 and a[i] >= k:
			k = a[i]
			pass
		elif l == 0:
			l = 1
			k = a[i]
		elif l == 1 and a[i] <= k:
			k = a[i]
		else:
			q = i
			break
	if q == w:
		print(0)
	else:
		print(q + 1)


t = int(input())
while t > 0:
	n = int(input())
	res = [int(i) for i in input().split()]
	list = res[::-1]
	i = 0
	while i < n - 1 and list[i] <= list[i + 1]:
		i += 1
	while i < n - 1 and list[i] >= list[i + 1]:
		i += 1
	print(n - i - 1)
	t -= 1


import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	a.reverse()
	flag = False
	flag2 = False
	for i in range(n - 1):
		if not flag:
			if a[i] > a[i + 1]:
				flag = True
		elif a[i] < a[i + 1]:
			print(n - i - 1)
			flag2 = True
			break
	if not flag2:
		print(0)


for _ in range(int(input())):
	n = int(input())
	arr = [int(c) for c in input().split()]
	arr.reverse()
	if n == 1:
		print(0)
		continue
	ind = n - 1
	for i in range(1, n):
		if arr[i - 1] > arr[i]:
			ind = i
			break
	for i in range(ind + 1, n):
		if arr[i] <= arr[i - 1]:
			ind += 1
			continue
		else:
			break
	print(n - 1 - ind)


from sys import stdin

def input():
	return stdin.readline()
for _ in range(int(input())):
	n = int(input())
	lst = [int(i) for i in input().split()]
	lst = lst[::-1]
	max1 = 0
	nk = 0
	nd = 0
	for i in range(0, n - 1):
		if nk == 0 and lst[i] <= lst[i + 1]:
			max1 = lst[i + 1]
			nd += 1
		elif nk == 0 and lst[i] > lst[i + 1]:
			max1 = lst[i]
			nk = 1
			nd += 1
		elif max1 >= lst[i] and lst[i] >= lst[i + 1]:
			nd += 1
		else:
			break
	print(n - (nd + 1))


t = int(input())
f = []
for i in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	(ct, pos) = (0, 0)
	for j in range(n - 1, 1, -1):
		if l[j] > l[j - 1]:
			pos = j - 1
			break
	for j in range(pos):
		if l[j] > l[j + 1]:
			ct = j + 1
	f.append(ct)
print(*f, sep='\n')


from sys import stdin

def inp():
	return stdin.buffer.readline().rstrip().decode('utf8')

def itg():
	return int(stdin.buffer.readline())

def mpint():
	return map(int, stdin.buffer.readline().split())
for case in range(itg()):
	n = itg()
	arr = list(mpint())
	arr.reverse()
	idx = idx2 = 0
	for i in range(1, n):
		if arr[i - 1] > arr[i]:
			idx = i
			break
	else:
		print(0)
		continue
	ans = 0
	for i in range(idx + 1, n):
		if arr[i - 1] < arr[i]:
			ans = n - i
			break
	print(ans)


import math

def dtb(n):
	return bin(n).replace('0b', '')

def btd(n):
	return int(n, 2)
t = int(input())
for k in range(t):
	n = int(input())
	a = list(map(int, input().split()))[:n]
	c = 1
	c2 = 1
	for i in range(n - 1):
		if a[i] <= a[i + 1]:
			c += 1
	for i in range(n - 1):
		if a[i] >= a[i + 1]:
			c2 += 1
	if c == n or c2 == n:
		print(0)
		continue
	for i in range(n - 1, 0, -1):
		if a[i] > a[i - 1]:
			x = i
			break
	for i in range(x, 0, -1):
		if i == 1:
			if a[i] < a[i - 1]:
				x = i
				break
			else:
				x = 0
		if a[i] < a[i - 1]:
			x = i
			break
	print(x)


t = int(input())
for i in range(t):
	n = int(input())
	nums = list(map(int, input().split()))
	pos = n - 1
	while pos > 0 and nums[pos - 1] >= nums[pos]:
		pos -= 1
	while pos > 0 and nums[pos - 1] <= nums[pos]:
		pos -= 1
	print(pos)


def main():
	n = int(input())
	for i in range(n):
		solve()

def solve():
	n = int(input())
	arr = list(map(int, input().split()))
	ascending = True
	index = len(arr) - 1
	prev = arr[len(arr) - 1]
	while ascending == True and index > 0:
		if prev <= arr[index - 1]:
			prev = arr[index - 1]
			index -= 1
		else:
			ascending = False
	descending = True
	while descending == True and index > 0:
		if prev >= arr[index - 1]:
			prev = arr[index - 1]
			index -= 1
		else:
			descending = False
	print(index)
main()


from sys import *
input = stdin.readline
t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	p = []
	i = 0
	j = n - 1
	k = 0
	while i < n and j >= i:
		x = min(l[i], l[j])
		if p == []:
			p.append(x)
			if x == l[i]:
				i += 1
			else:
				j -= 1
		else:
			y = p[-1]
			if y <= x:
				p.append(x)
				if x == l[i]:
					i += 1
				else:
					j -= 1
			else:
				p = []
				j = n - 1
				if x != l[i]:
					i += 1
	print(n - len(p))


def MakeItGood(arr, n):
	pos = n - 1
	while pos > 0 and arr[pos - 1] >= arr[pos]:
		pos -= 1
	while pos > 0 and arr[pos - 1] <= arr[pos]:
		pos -= 1
	print(pos)
t = int(input())
for _ in range(t):
	num = int(input())
	ar = list(map(int, input().split()))
	MakeItGood(ar, num)


from math import log2, ceil, floor
for t in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	if a == sorted(a):
		print(0)
	elif a[n - 1] > a[n - 2]:
		c = 1
		for i in range(n - 1, 0, -1):
			if a[i] >= a[i - 1]:
				c += 1
			else:
				break
		print(n - c)
	else:
		c = 1
		index = 0
		for i in range(n - 1, 0, -1):
			if a[i] <= a[i - 1]:
				c += 1
			else:
				index = i
				break
		for i in range(index, 0, -1):
			if a[i] >= a[i - 1]:
				c += 1
			else:
				break
		print(n - c)


def get_inc(arr, n):
	count = 0
	for i in range(n - 2, -1, -1):
		if arr[i] > arr[i + 1]:
			break
		count += 1
	return count + 1

def get_dec(arr, n):
	count = 0
	for i in range(n - 2, -1, -1):
		if arr[i] < arr[i + 1]:
			break
		count += 1
	return count + 1
t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().strip().split()))[:n]
	a = get_inc(arr, n)
	b = get_dec(arr, n)
	count = 0
	for i in range(n - b - 1, -1, -1):
		if arr[i] > arr[i + 1]:
			break
		count += 1
	print(n - max(a, b + count))


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	if n == 1:
		print(0)
	else:
		i = n - 1
		while l[i] <= l[i - 1] and i > 0:
			i -= 1
		while l[i] >= l[i - 1] and i:
			i -= 1
		print(i)


def good_array(arr):
	j = len(arr) - 1
	while j > 0 and arr[j] <= arr[j - 1]:
		j -= 1
	while j > 0 and arr[j] >= arr[j - 1]:
		j -= 1
	return j
t = int(input())
while t > 0:
	n = int(input())
	x = [int(i) for i in input().split()]
	print(good_array(x))
	t -= 1


t = int(input())
final = []
for j in range(t):
	n = int(input())
	l = [int(x) for x in input().split()]
	hot = 0
	for i in range(len(l) - 1):
		h = min(l[len(l) - 1 - i], l[len(l) - 2 - i])
		if h != l[len(l) - 1 - i]:
			hot = len(l) - 2 - i
			break
	for i in range(hot):
		h = min(l[hot - i], l[hot - 1 - i])
		if h != l[hot - i - 1]:
			final.append(hot - i)
			break
	if len(final) == j:
		final.append(0)
for i in range(len(final)):
	print(final[i])


t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	if n == 1:
		print(0)
	else:
		found = False
		up = False
		for (i, v) in enumerate(l[::-1]):
			if i == 0:
				last = v
				if l[::-1][1] >= v:
					up = True
			elif up:
				if v >= last:
					last = v
				else:
					last = v
					up = False
			elif v <= last:
				last = v
			else:
				found = True
				print(n - i)
				break
		if not found:
			print(0)


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	peak = False
	for (c, (v, pv)) in enumerate(zip(a[-2::-1], a[:0:-1])):
		if pv > v:
			peak = True
		elif v > pv and peak:
			print(len(a) - c - 1)
			break
	else:
		print(0)


for test in range(int(input())):
	n = int(input())
	a = input().split()
	ch = 'i'
	l = 1
	f = 0
	for i in range(n - 2, -1, -1):
		if int(a[i]) == int(a[i + 1]):
			l += 1
		elif int(a[i]) > int(a[i + 1]):
			if ch == 'i':
				l += 1
			else:
				f = i + 1
				break
		else:
			l += 1
			ch = 'd'
	print(f)


from sys import *
t = int(stdin.readline())
while t > 0:
	n = int(stdin.readline())
	arr = list(map(int, stdin.readline().split()))
	if arr == sorted(arr):
		ans = 0
	elif arr == sorted(arr, reverse=True):
		ans = 0
	else:
		count = 0
		for i in range(n - 1, 0, -1):
			if arr[i] <= arr[i - 1]:
				count += 1
			else:
				j = i
				break
		for i in range(i, 0, -1):
			if arr[i] >= arr[i - 1]:
				count += 1
			else:
				break
		ans = n - (count + 1)
	print(ans)
	t -= 1


for i in range(int(input())):
	n = int(input())
	line = input().split()
	up = True
	down = False
	broken = -1
	for i in range(n - 1):
		if up:
			if int(line[n - i - 2]) < int(line[n - i - 1]):
				up = False
				down = True
		elif down:
			if int(line[n - i - 2]) > int(line[n - i - 1]):
				down = False
				broken = n - 1 - i
	if broken == -1:
		print(0)
	else:
		print(broken)


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	x = []
	y = []
	x += l
	y += l
	x.sort()
	y.sort(reverse=True)
	if x == l or y == l or n == 1 or (n == 2):
		print(0)
	else:
		ans = 0
		while n > 1 and l[-1] == l[-2]:
			ans += 1
			n -= 1
			l = l[:-1]
		if l[-1] > l[-2]:
			ans = 1
			for i in range(n - 2, -1, -1):
				if l[i + 1] >= l[i]:
					ans += 1
				else:
					break
		elif l[-1] < l[-2]:
			ans = 1
			for i in range(n - 2, -1, -1):
				if l[i] >= l[i + 1]:
					ans += 1
				else:
					for j in range(i, -1, -1):
						if l[j] <= l[j + 1]:
							ans += 1
						else:
							break
					break
		print(n - ans)


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


from time import time
from random import randint

def isGood(arr):
	l = 0
	r = len(arr) - 1
	while l < r:
		lowest = min(arr[l], arr[r])
		if arr[l + 1] <= arr[r - 1] and arr[l + 1] >= lowest:
			l += 1
		elif arr[r - 1] >= lowest:
			r -= 1
		else:
			return False
	return True

def printSolution(arr):
	clicked = False
	for i in range(2, len(arr)):
		if arr[-i] < arr[-i + 1]:
			clicked = True
		if arr[-i] < arr[-i - 1] and clicked == True:
			print(len(arr) - i)
			return
	print(0)

def printSolutionNew(arr):
	pos = len(arr) - 1
	while pos > 0 and arr[pos - 1] >= arr[pos]:
		pos -= 1
	while pos > 0 and arr[pos - 1] <= arr[pos]:
		pos -= 1
	print(pos)

def printSolutionTutorial(arr):
	pos = len(arr) - 1
	while pos > 1 and arr[pos - 1] >= arr[pos]:
		pos -= 1
	while pos > 1 and arr[pos - 1] <= arr[pos]:
		pos -= 1

def submit():
	start_time = time()
	for case in range(int(input())):
		input()
		array = list(map(int, input().split()))
		printSolution(array)

def submit2():
	start_time = time()
	for case in range(int(input())):
		input()
		array = list(map(int, input().split()))
		printSolution(array)
submit2()


t = int(input())
while t:
	n = int(input())
	i = n - 1
	list1 = list(map(int, input().split()))
	while i > 0 and list1[i] <= list1[i - 1]:
		i = i - 1
	while i > 0 and list1[i] >= list1[i - 1]:
		i = i - 1
	print(i)
	t = t - 1


import collections
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	arr = sorted(a, reverse=True)
	ans = 0
	freq = collections.Counter(arr)
	if a == arr or a == arr[::-1]:
		print(0)
	else:
		(i, j) = (0, n - 1)
		while i < j:
			if min(a[i], a[j]) == arr[-1]:
				freq[arr.pop()] -= 1
				if a[j] <= a[i]:
					j -= 1
				else:
					i += 1
			elif freq[arr[-1]] > 0:
				freq[a[i]] -= 1
				i += 1
				ans = i
			else:
				arr.pop()
		print(ans)


for i in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	ans = 0
	j = n - 2
	while j >= 0 and arr[j] >= arr[j + 1]:
		j -= 1
	while j >= 0 and arr[j] <= arr[j + 1]:
		j -= 1
	print(j + 1)


import sys
input = sys.stdin.readline
import heapq
import copy
import math
import decimal

def inp():
	return int(input())

def inlt():
	return list(map(int, input().split()))

def insr():
	s = input()
	return list(s[:len(s) - 1])

def insr2():
	s = input()
	return s[:len(s) - 1]

def invr():
	return map(int, input().split())

def pr_list(a):
	print(*a, sep=' ')

def main():
	tests = inp()
	mod = 1000000007
	limit = 10 ** 18
	ans = 0
	stack = []
	hashm = {}
	arr = []
	heapq.heapify(arr)
	for test in range(tests):
		hashm = {}
		n = inp()
		a = inlt()
		ans = []
		i = n - 2
		inc = 1
		while i >= 0:
			if a[i] < a[i + 1] and inc == 1:
				inc = 0
			elif a[i] > a[i + 1] and inc == 0:
				break
			i = i - 1
		print(i + 1)
main()


for _ in range(int(input())):
	n = int(input())
	s = list(map(int, input().split()))
	ans = n - 1
	while ans and s[ans] <= s[ans - 1]:
		ans -= 1
	while ans and s[ans] >= s[ans - 1]:
		ans -= 1
	print(ans)


for _ in range(int(input())):
	n = int(input())
	p = list(map(int, input().split()))
	flag = True
	p = p[::-1]
	k = n - 1
	for i in range(1, n):
		if flag:
			if p[i] < p[i - 1]:
				flag = False
		if flag == False:
			if p[i] > p[i - 1]:
				break
		k -= 1
	print(k)


import sys
import math
import collections
import heapq
import queue
import itertools
import functools
import operator
import time
readline = sys.stdin.readline
IPS = lambda : readline().rstrip()
IP = lambda : int(readline().rstrip())
MP = lambda : map(int, readline().split())
LS = lambda : list(map(int, readline().split()))

def solve():
	for _ in range(IP()):
		n = IP()
		arr = LS()
		end = n - 1
		while end > 0 and arr[end - 1] >= arr[end]:
			end -= 1
		while end > 0 and arr[end - 1] <= arr[end]:
			end -= 1
		print(end)
solve()


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	ans = 0
	l = []
	i = n - 1
	while i > 0 and a[i] <= a[i - 1]:
		i -= 1
	while i > 0 and a[i] >= a[i - 1]:
		i -= 1
	print(i)


for i in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	flag = 0
	index = 0
	for j in range(n - 1, 0, -1):
		if flag == 0:
			if l[j] > l[j - 1]:
				flag = 1
		elif l[j] < l[j - 1]:
			index = j
			break
	print(index)


import sys
input = sys.stdin.buffer.readline

def main():
	n = int(input())
	l = list(map(int, input().split()))
	if n <= 2:
		return 0
	else:
		flag = 0
		index = 0
		for i in range(n - 1, 0, -1):
			if l[i] <= l[i - 1] and flag == 0:
				continue
			if l[i] >= l[i - 1]:
				flag = 1
			else:
				index = i
				break
		return index
t = int(input())
while t:
	t -= 1
	print(main())


import sys
input = sys.stdin.readline
t = int(input())

def inc(arr):
	ans1 = 0
	n = len(arr)
	for i in range(n - 1, 0, -1):
		if arr[i - 1] > arr[i]:
			ans1 = i
			break
	return ans1

def dec(arr):
	ans1 = 0
	n = len(arr)
	for i in range(n - 1, 0, -1):
		if arr[i - 1] < arr[i]:
			ans1 = i
			break
	return ans1
for t1 in range(t):
	n = int(input())
	f = 0
	ans = 0
	arr = list(map(int, input().split(' ')))
	z1 = dec(arr)
	z2 = inc(arr[:z1])
	z3 = inc(arr)
	f = min(z1, z2)
	f = min(f, z3)
	print(f)


t = int(input())

def is_good(lista, n):
	i = n - 1
	good = 0
	while i > 0 and lista[i - 1] == lista[i]:
		i -= 1
	if lista[i - 1] > lista[i]:
		while i > 0 and lista[i - 1] >= lista[i]:
			i -= 1
		if i > 0 and lista[i - 1] < lista[i]:
			while i > 0 and lista[i - 1] <= lista[i]:
				i -= 1
	if lista[i - 1] < lista[i]:
		while i > 0 and lista[i - 1] <= lista[i]:
			i -= 1
	return i
for i in range(t):
	n = int(input())
	lista = input().split()
	for i in range(n):
		lista[i] = int(lista[i])
	print(is_good(lista, n))


t = int(input())
while t > 0:
	t -= 1
	n = int(input())
	a = list(map(int, input().split()))
	i = n - 1
	l = 1
	while i > 0:
		if a[i] <= a[i - 1]:
			i -= 1
			l += 1
		else:
			break
	while i > 0:
		if a[i] >= a[i - 1]:
			i -= 1
			l += 1
		else:
			break
	print(n - l)


t = int(input())
for _ in range(t):
	n = int(input())
	lst = input().split()
	lst = [int(i) for i in lst]
	dah = False
	for i in range(n - 1, 0, -1):
		if lst[i] > lst[i - 1]:
			dah = True
		elif dah and lst[i] < lst[i - 1]:
			print(i)
			break
	else:
		print(0)


from sys import stdin, stdout
for query in range(int(stdin.readline())):
	n = int(stdin.readline())
	a = [int(x) for x in stdin.readline().split()]
	if n == 1:
		stdout.write('0\n')
		continue
	b = -1
	for x in range(n - 2, -1, -1):
		if a[x] < a[x + 1]:
			b = x
			break
	if b == -1:
		stdout.write('0\n')
		continue
	c = -1
	for x in range(b - 1, -1, -1):
		if a[x] > a[x + 1]:
			c = x
			break
	if c == -1:
		stdout.write('0\n')
	else:
		stdout.write(str(c + 1) + '\n')


import bisect
import collections
import copy
import fractions
import functools
import heapq
import math
import random
import sys

def _p(str):
	sys.stdout.write(str + '\n')

def _i():
	return sys.stdin.readline()
T = int(_i())
for t in range(T):
	N = int(_i())
	A = list(map(int, _i().split()))
	(last, increasing) = (A[-1], True)
	i = N - 2
	while i >= 0:
		if increasing:
			if A[i] < last:
				increasing = False
		elif A[i] > last:
			break
		last = A[i]
		i -= 1
	_p(str(i + 1))
sys.stdout.flush()


for _ in range(int(input())):
	n = int(input())
	arr = [int(x) for x in input().split()]
	ans = 0
	cur = n - 1
	while cur != 0 and arr[cur] <= arr[cur - 1]:
		cur -= 1
	while cur != 0 and arr[cur] >= arr[cur - 1]:
		cur -= 1
	print(cur)


t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	j = n - 1
	while j > 0:
		if l[j - 1] >= l[j]:
			j -= 1
			continue
		break
	while j > 0:
		if l[j] >= l[j - 1]:
			j -= 1
			continue
		break
	print(j)


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


t = int(input())
while t:
	t = t - 1
	n = int(input())
	l = list(map(int, input().split()))
	ans = -1
	i = n - 1
	for w in range(n - 1, 0, -1):
		if l[w] <= l[w - 1]:
			i = w - 1
			continue
		else:
			break
	j = i
	for w in range(i, 0, -1):
		if l[w] >= l[w - 1]:
			j = w - 1
			continue
		else:
			break
	print(j)


for x in range(int(input())):
	n = int(input())
	m = list(map(int, input().split()))
	m = m[::-1]
	k = n
	t = 0
	for i in range(1, n - 1):
		if m[i - 1] > m[i]:
			t = 1
		elif m[i - 1] < m[i]:
			t = 0
		if m[i + 1] > m[i] and t == 1:
			k = i + 1
			break
	print(n - k)


def make(n, a):
	decreasing = False
	increasing = False
	equal = False
	last_index = 0
	could_be = 0
	for i in range(1, n):
		if a[i] > a[i - 1]:
			if decreasing == False:
				increasing = True
				equal = False
			else:
				last_index = i - 1
				decreasing = False
				increasing = True
				if equal:
					last_index = could_be
					equal = False
		elif a[i] < a[i - 1]:
			if increasing == True:
				increasing = False
				decreasing = True
				equal = False
			else:
				decreasing = True
				if equal:
					equal = False
		elif increasing:
			continue
		elif equal:
			continue
		else:
			could_be = i - 1
			equal = True
	return last_index
t = int(input())
array = []
for i in range(t):
	a = int(input())
	b = list(map(int, input().split(' ')))
	array.append([a, b])
for x in array:
	print(make(*x))


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	c = {}
	for i in range(n):
		if a[i] in c:
			c[a[i]] += 1
		else:
			c[a[i]] = 1
	s = list(a)
	s.sort()
	j = 0
	ans = 0
	mi = 0
	for i in s:
		if c[i] >= 1:
			if i == a[-1]:
				n = n - 1
				a.pop()
				c[i] -= 1
				continue
			else:
				count = 0
				for k in range(j, n):
					count += 1
					if a[k] == i:
						c[a[k]] -= 1
						break
					else:
						c[a[k]] -= 1
				j = k + 1
				if count == 1:
					ans = ans
				else:
					ans = j - 1
	print(ans)


import sys, os
from io import BytesIO, IOBase
import collections, itertools, bisect, heapq, math, string
from decimal import *
BUFSIZE = 8192
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def main():
	t = int(input())
	for i in range(t):
		n = int(input())
		a = list(map(int, input().split()))
		pos = n - 1
		while pos > 0 and a[pos - 1] >= a[pos]:
			pos = pos - 1
		while pos > 0 and a[pos - 1] <= a[pos]:
			pos = pos - 1
		print(pos)
main()


import sys, math
from sys import stdin, stdout
mod = int(1000000000.0 + 7)
from collections import deque, defaultdict
from copy import deepcopy
from math import ceil, floor, inf, sqrt, factorial, gcd, log2
ii1 = lambda : int(stdin.readline().strip())
is1 = lambda : stdin.readline().strip()
iia = lambda : list(map(int, stdin.readline().strip().split()))
isa = lambda : stdin.readline().strip().split()
for _ in range(ii1()):
	n = ii1()
	arr = iia()
	start = n - 1
	count = 0
	flag = False
	for i in range(n - 1, 0, -1):
		if arr[i - 1] >= arr[i]:
			count += 1
		else:
			start = i
			flag = True
			break
	if flag:
		for j in range(start, 0, -1):
			if arr[j - 1] <= arr[j]:
				count += 1
			else:
				end = 0
				break
	print(n - count - 1)


def do_good(n: int, arr: list):
	max_num = 2 * 10 ** 5
	len_good_arr = 1
	for i in range(n - 2, -1, -1):
		if arr[i] > max_num:
			break
		if arr[i] < arr[i + 1]:
			max_num = arr[i]
		len_good_arr += 1
	return n - len_good_arr
t = int(input())
for i in range(t):
	n = int(input())
	arr = list(map(int, input().split(' ')))
	print(do_good(n, arr))


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	p = [0] * n
	b = []
	i = 0
	j = n - 1
	while j >= 1:
		if a[j] <= a[j - 1]:
			j -= 1
		else:
			break
	ans = n - j
	pp = a[:j]
	b = sorted(pp)
	if b == pp:
		print(0)
	else:
		while j >= 1:
			if a[j] >= a[j - 1]:
				j -= 1
			else:
				break
		print(j)


t = int(input())
for _ in range(t):
	n = int(input())
	arr = [int(p) for p in input().split()]
	a = list(reversed(arr))
	i = len(a) - 1
	if len(arr) <= 2:
		print(0)
		continue
	while i > 0:
		if arr[i] <= arr[i - 1]:
			i -= 1
		else:
			break
	while i > 0:
		if arr[i] >= arr[i - 1]:
			i -= 1
		else:
			break
	print(i)


import sys
import math
import bisect
from sys import stdin, stdout
from math import gcd, floor, sqrt, log
from collections import defaultdict as dd
from bisect import bisect_left as bl, bisect_right as br
from collections import Counter
inp = lambda : int(input())
strng = lambda : input().strip()
jn = lambda x, l: x.join(map(str, l))
strl = lambda : list(input().strip())
mul = lambda : map(int, input().strip().split())
mulf = lambda : map(float, input().strip().split())
seq = lambda : list(map(int, input().strip().split()))
ceil = lambda x: int(x) if x == int(x) else int(x) + 1
ceildiv = lambda x, d: x // d if x % d == 0 else x // d + 1
flush = lambda : stdout.flush()
stdstr = lambda : stdin.readline()
stdint = lambda : int(stdin.readline())
stdpr = lambda x: stdout.write(str(x))
stdarr = lambda : map(int, stdstr().split())
mod = 1000000007
for _ in range(stdint()):
	n = stdint()
	arr = list(stdarr())
	last = n - 1
	while last > 0 and arr[last - 1] >= arr[last]:
		last -= 1
	while last > 0 and arr[last - 1] <= arr[last]:
		last -= 1
	print(last)


from sys import stdin, stdout
import math
input = stdin.readline

def print(x='', y='', end='\n'):
	if y == '':
		stdout.write(str(x) + end)
	else:
		stdout.write(str(x) + ' ' + str(y) + end)

def solve():
	n = int(input())
	l = list(map(int, input().split()))
	idx = n - 1
	while idx > 0 and l[idx - 1] >= l[idx]:
		idx -= 1
	while idx > 0 and l[idx - 1] <= l[idx]:
		idx -= 1
	print(idx)
tt = 1
tt = int(input())
for __ in range(tt):
	solve()


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


from collections import Counter
import math
import sys
from bisect import bisect, bisect_left, bisect_right

def input():
	return sys.stdin.readline().strip()

def INT():
	return int(input())

def MAP():
	return map(int, input().split())

def LIST(N=None):
	return list(MAP()) if N is None else [INT() for i in range(N)]

def mod():
	return 10 ** 9 + 7
for i in range(INT()):
	n = INT()
	arr = LIST()
	flag = 0
	for i in range(1, n):
		if arr[i] < arr[i - 1]:
			flag = 1
	if flag == 0:
		print(0)
	else:
		arr.reverse()
		ind = n - 1
		for i in range(n - 1):
			if arr[i] <= arr[i + 1]:
				continue
			else:
				ind = i
				break
		for i in range(ind + 1, n):
			if arr[i] <= arr[i - 1]:
				ind = i
			else:
				ind2 = i
				break
		ind += 1
		print(n - ind)


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


for t in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	pos = n - 1
	while pos > 0 and l[pos - 1] >= l[pos]:
		pos -= 1
	while pos > 0 and l[pos - 1] <= l[pos]:
		pos -= 1
	print(pos)


for _ in range(int(input())):
	n = int(input())
	(*arr,) = map(int, input().split())
	pivot = n - 1
	while pivot > 0 and arr[pivot] <= arr[pivot - 1]:
		pivot -= 1
	while pivot > 0 and arr[pivot] >= arr[pivot - 1]:
		pivot -= 1
	print(pivot)


tc = int(input())
while tc:
	n = int(input())
	arr = list(map(int, input().split()))
	id = n - 1
	while id > 0 and arr[id - 1] >= arr[id]:
		id -= 1
	while id > 0 and arr[id - 1] <= arr[id]:
		id -= 1
	print(id)
	tc -= 1


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	c1 = 0
	i = n - 1
	while i > 0:
		if a[i] <= a[i - 1]:
			c1 = c1 + 1
			i = i - 1
		else:
			break
	if i == 0:
		c1 += 1
	else:
		j = i
		while j > 0:
			if a[j] >= a[j - 1]:
				c1 = c1 + 1
				j = j - 1
			else:
				break
		if j == 0:
			c1 += 1
	if c1 == n:
		print(0)
	else:
		print(n - c1 - 1)


def main():
	T = int(input())
	for _ in range(T):
		N = int(input())
		A = [int(i) for i in input().split()][::-1]
		flag = False
		pre = A[0]
		cnt = 1
		for a in A[1:]:
			if not flag:
				if a >= pre:
					pass
				else:
					flag = True
			elif a <= pre:
				pass
			else:
				break
			pre = a
			cnt += 1
		print(N - cnt)
main()


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)


t = int(input())

def fun(n, a):
	ans = -1
	i = 0
	j = n
	a.reverse()
	while i + 1 < n and a[i] <= a[i + 1]:
		i += 1
	while i + 1 < n and a[i] >= a[i + 1]:
		i += 1
	print(n - i - 1)
while t:
	t -= 1
	n = int(input())
	a = [int(i) for i in input().split()]
	fun(n, a)


def sol(lis):
	i = len(lis) - 1
	while i > 0 and lis[i - 1] >= lis[i]:
		i -= 1
	while i > 0 and lis[i - 1] <= lis[i]:
		i -= 1
	print(i)
k = int(input())
for i in range(k):
	input()
	lis = list(map(int, input().split(' ')))
	sol(lis)


try:
	for i in range(int(input())):
		size = int(input())
		arr = list(map(int, input().split()))
		j = len(arr) - 1
		while j - 1 >= 0 and arr[j - 1] >= arr[j]:
			j -= 1
		while j - 1 >= 0 and arr[j - 1] <= arr[j]:
			j -= 1
		print(j)
except:
	pass


t = int(input())
while t > 0:
	n = int(input())
	a = list(map(int, input().split()))
	L = a[::-1]
	result = []
	for i in range(len(L) - 1):
		if L[i] <= L[i + 1]:
			result.append(L[i])
		else:
			break
	s = len(result)
	for k in range(s, len(L) - 1):
		if L[k] >= L[k + 1]:
			result.append(L[k])
		else:
			break
	print(len(a) - len(result) - 1)
	t -= 1


for _ in range(int(input())):
	N = int(input())
	seq = list(map(int, input().split()))
	inc = 1
	dec = 1
	index = N - 1
	count = 0
	while True:
		if inc and index >= 1 and (seq[index] > seq[index - 1]):
			inc = 0
		if not inc and dec and (index >= 1) and (seq[index] < seq[index - 1]):
			inc = 0
			dec = 0
		if index == 0 or (not dec and (not inc)):
			break
		count += 1
		index -= 1
	print(N - count - 1)


T = int(input())
for I in range(0, T):
	N = input()
	X = list(map(int, input().split(' ')))
	Mode = 0
	Q = 0
	A = 0
	while A < len(X):
		P = X[len(X) - A - 1]
		if Mode == 0:
			if P < Q:
				Mode = 1
		elif Mode == 1:
			if P > Q:
				break
		Q = P
		A += 1
	print(len(X) - A)


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	pos = n - 1
	while pos > 0 and arr[pos - 1] >= arr[pos]:
		pos -= 1
	while pos > 0 and arr[pos - 1] <= arr[pos]:
		pos -= 1
	print(pos)


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


import os
from io import BytesIO, IOBase
import sys

def main():
	for _ in range(int(input())):
		n = int(input())
		a = list(map(int, input().split()))
		mx = n
		for i in range(n - 2, -1, -1):
			if a[i] < a[i + 1]:
				mx = i + 1
				break
		if mx == n:
			print(0)
			continue
		p = mx
		for i in range(mx - 1, -1, -1):
			if a[i] <= a[i + 1]:
				p = i
			else:
				break
		print(p)
	return
BUFSIZE = 1048576

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))

def input():
	return sys.stdin.readline().rstrip('\r\n')
main()


import os
is_dev = 'vscode' in os.environ
if is_dev:
	inF = open('in.txt', 'r')
	outF = open('out.txt', 'w')

def ins(r):
	return list(map(int, r.split(' ')))

def inputT():
	if is_dev:
		return inF.readline()
	else:
		return input()

def printT(data):
	if is_dev:
		return outF.write(str(data) + '\n')
	else:
		return print(data)
for _ in range(int(inputT())):
	n = int(inputT())
	x = ins(inputT())
	x = list(reversed(x))
	stepper = x[0]
	step_index = 0
	flag = True
	for (idx, i) in enumerate(x):
		if flag:
			if stepper > i:
				flag = False
		elif stepper < i:
			break
		stepper = i
		step_index = idx
	printT(len(x) - step_index - 1)
if is_dev:
	print(open('out.txt', 'r') == open('outactual.txt', 'r'))


from sys import stdin, stdout
import math
import heapq
t = 1

def aint():
	return int(input().strip())

def lint():
	return list(map(int, input().split()))

def fint():
	return list(map(int, stdin.readline().split()))

def main():
	n = aint()
	l = lint()
	if n <= 2:
		print(0)
		return
	prev = 0
	(s, e) = (0, n - 1)
	flag = True
	for i in range(n):
		if l[s] <= l[e] and l[s] >= prev:
			prev = l[s]
			s += 1
		elif l[e] <= l[s] and l[e] >= prev:
			prev = l[e]
			e -= 1
		else:
			flag = False
	if flag:
		print('0')
		return
	start = l[-1]
	prev = l[-1]
	changed = 0
	for i in range(n - 2, -1, -1):
		if changed == 0:
			if l[i] >= prev:
				prev = l[i]
			else:
				prev = l[i]
				changed = 1
		elif changed == 1:
			if prev >= l[i]:
				prev = l[i]
			else:
				print(i + 1)
				return
	return
t = int(input())
for i in range(t):
	main()


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	if n == 1:
		print(0)
	else:
		big = {}
		nax = a[-1]
		big[n - 1] = a[-1]
		for i in range(n - 2, -1, -1):
			if a[i] > nax:
				nax = a[i]
			big[i] = nax
		i = 1
		ans = 0
		while i < n - 1:
			if a[i] >= a[i - 1]:
				i += 1
				continue
			elif big[i + 1] > a[i]:
				ans = i
				i += 1
			else:
				i += 1
		print(ans)


t = int(input())
while t:
	n = int(input())
	a = list(map(int, input().split(' ')))
	if n <= 2:
		print(0)
	else:
		peak = 0
		vall = 0
		ans = 0
		for i in range(n - 1, 0, -1):
			if a[i] > a[i - 1] and peak == 0 and (vall == 0):
				peak = 1
			elif a[i] >= a[i - 1] and peak == 1 and (vall == 0):
				continue
			elif a[i] < a[i - 1] and peak == 0 and (vall == 0):
				vall = 1
			elif a[i] == a[i - 1] and peak == 0 and (vall == 0):
				continue
			elif a[i] < a[i - 1] and peak == 1 and (vall == 0):
				ans = i
				break
			elif a[i] == a[i - 1] and peak == 1 and (vall == 0):
				continue
			elif a[i] > a[i - 1] and peak == 0 and (vall == 1):
				peak = 1
			elif a[i] <= a[i - 1] and peak == 0 and (vall == 1):
				continue
			elif a[i] < a[i - 1] and peak == 1 and (vall == 1):
				ans = i
				break
			elif a[i] >= a[i - 1] and peak == 1 and (vall == 1):
				continue
		print(ans)
	t -= 1


t = int(input())
for _ in range(0, t):
	n = int(input())
	arr = list(map(int, input().strip().split(' ')))
	ans = 0
	c = 0
	for i in range(n - 2, -1, -1):
		if arr[i] >= arr[i + 1] and c == 0:
			ans += 1
		else:
			c = 1
		if c == 1:
			if arr[i] <= arr[i + 1]:
				ans += 1
			else:
				break
	print(n - ans - 1)


for i in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	result = -1
	up = True
	last = a[-1]
	for i in range(n - 2, -1, -1):
		if a[i] > last and (not up):
			result = i
			break
		if up and a[i] < last:
			up = False
		last = a[i]
	print(result + 1)


for _ in range(int(input())):
	n = int(input())
	l = list(reversed(list(map(int, input().split()))))
	a = 0
	for i in range(n - 1):
		if l[i] > l[i + 1] and a == 0:
			a += 1
		elif l[i] < l[i + 1] and a == 1:
			a = n - i - 1
			break
	else:
		a = 0
	print(a)


import sys
input = sys.stdin.buffer.readline
T = int(input())
for _ in range(T):
	n = int(input())
	a = list(map(int, input().split()))
	(cc, state) = (1, 0)
	for i in range(n - 2, -1, -1):
		if state == 0:
			if a[i] >= a[i + 1]:
				pass
			else:
				state = 1
		elif state == 1:
			if a[i] <= a[i + 1]:
				pass
			else:
				break
		cc += 1
	print(n - cc)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	x = sorted(a)
	if x == a or x[::-1] == a:
		print(0)
	else:
		for i in range(n - 1, -1, -1):
			if a[i] > a[i - 1]:
				k = i
				break
		for i in range(k, 0, -1):
			if a[i] < a[i - 1]:
				k = i
				print(k)
				break
		else:
			print(0)


def create_max(arr):
	mx = [arr[0]]
	for i in arr[1:]:
		mx.append(max(mx[-1], i))
	return mx
n = int(input())
for i in range(n):
	x = int(input())
	arr = [int(i) for i in input().split()]
	if len(arr) < 3:
		print(0)
	else:
		ans = 0
		mx1 = [[10 ** 9, -1]]
		mx2 = create_max(arr[::-1])[::-1]
		for i in range(0, len(arr)):
			while mx1[-1][0] < arr[i]:
				mx1.pop()
			if mx1[-1][0] > arr[i] and mx2[i] > arr[i]:
				ans = max(ans, mx1[-1][1] + 1)
			mx1.append([arr[i], i])
		print(ans)


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	a.reverse()
	c = 0
	ans = 0
	for i in range(n - 1):
		if not ans == 0:
			break
		if a[i] < a[i + 1]:
			if c == 1:
				ans = n - i - 1
		elif a[i] > a[i + 1]:
			c = 1
	print(ans)


T = int(input())
for case in range(T):
	n = int(input())
	a = list(map(int, input().split()))
	idx = n - 1
	while idx > 0 and a[idx - 1] >= a[idx]:
		idx -= 1
	while idx > 0 and a[idx - 1] <= a[idx]:
		idx -= 1
	print(idx)


import sys
import math
import heapq
import collections

def inputnum():
	return int(input())

def inputnums():
	return map(int, input().split())

def inputlist():
	return list(map(int, input().split()))

def inputstring():
	return [x for x in input()]

def inputmatrixchar(rows):
	arr2d = [[j for j in input().strip()] for i in range(rows)]
	return arr2d

def inputmatrixint(rows):
	arr2d = []
	for _ in range(rows):
		arr2d.append([int(i) for i in input().split()])
	return arr2d
t = int(input())
for q in range(t):
	n = inputnum()
	a = inputlist()
	inc = True
	ind = 0
	for i in reversed(range(n - 1)):
		if inc and a[i] < a[i + 1]:
			inc = False
			continue
		if not inc and a[i] > a[i + 1]:
			ind = i + 1
			break
	print(ind)


t = input()
t = int(t)
T = []
for i in range(t):
	n = input()
	n = int(n)
	a = map(int, input().split())
	a = list(a)
	j = n - 1
	while j > 0 and a[j] <= a[j - 1]:
		a.pop()
		j -= 1
	while j > 0 and a[j] >= a[j - 1]:
		a.pop()
		j -= 1
	T.append(j)
for i in T:
	print(i)


t = int(input())
while t > 0:
	n = int(input())
	a = list(map(int, input().split()))
	flag = 0
	i = n - 1
	while i > 0:
		if a[i] - a[i - 1] < 0:
			i = i - 1
			while i > 0 and a[i] - a[i - 1] <= 0:
				i = i - 1
			while i > 0 and a[i] - a[i - 1] >= 0:
				i = i - 1
			flag = 1
			print(i)
			break
		elif a[i] - a[i - 1] > 0:
			i = i - 1
			while i > 0 and a[i] - a[i - 1] >= 0:
				i = i - 1
			flag = 1
			print(i)
			break
		i = i - 1
	if flag == 0:
		print(i)
	t = t - 1


def problem1():
	(x, y, z) = sorted(list(map(int, input().split())))
	if y != z:
		print('NO')
		return
	print('YES')
	print(y, x, x)

def problem2():
	n = int(input())
	l = list(map(int, input().split()))
	s = set(l)
	d = {}
	for i in s:
		d[i] = 0
	a = []
	for i in l:
		if d[i] == 0:
			a.append(i)
			d[i] += 1
	print(*a)

def problem3():
	n = int(input())
	l = list(map(int, input().split()))
	l.append(-1)
	i = n - 1
	while i >= 0 and l[i] >= l[i + 1]:
		i -= 1
	while i >= 0 and l[i] <= l[i + 1]:
		i -= 1
	print(i + 1)
cases = int(input())
for _ in range(cases):
	problem3()


nb = int(input())

def pos_del(T, n):
	marque = n - 1
	cible = 0
	while marque > 0:
		if T[marque] <= T[marque - 1]:
			if cible != 0 and T[marque] != T[marque - 1]:
				return cible
			elif cible != 0 and T[marque] == T[marque - 1]:
				cible -= 1
			marque -= 1
		elif cible == 0:
			marque = marque - 1
			cible = marque
		else:
			marque -= 1
			cible -= 1
	return cible
for k in range(nb):
	n = int(input())
	T = list(map(int, input().split(' ')))
	pos = pos_del(T, n)
	print(pos)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	i = 0
	while i < len(a) - 1:
		if a[i] > a[i + 1]:
			break
		i += 1
	if i == len(a) - 1:
		print(0)
	else:
		start = 0
		end = len(a) - 1
		m = -9999999999
		count = 0
		while start != end:
			if a[start] > a[end]:
				if a[end] < m:
					start += 1
					end = len(a) - 1
					count = start
					m = -9999999999
				else:
					m = a[end]
					end -= 1
			else:
				if a[start] < m:
					count = start
					start -= 1
					end = len(a) - 1
					m = -9999999999
				else:
					m = a[start]
				start += 1
		print(count)


k = int(input())
ans = []
for i in range(k):
	n = int(input())
	lst = list(map(int, input().split(' ')))
	me = len(lst) - 1
	while me > 0 and lst[me - 1] >= lst[me]:
		me -= 1
	while me > 0 and lst[me - 1] <= lst[me]:
		me -= 1
	ans.append(me)
for i in ans:
	print(i)


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


for i in range(int(input())):
	n = int(input())
	ar = list(map(int, input().split(' ')))
	(inc, dec, d) = (False, 0, 0)
	for i in range(n - 1, -1, -1):
		if i != 0:
			if ar[i] < ar[i - 1]:
				dec += 1
			elif ar[i] == ar[i - 1]:
				continue
			else:
				inc = True
				d = dec
			if inc == True and dec > d:
				req = i
				break
		else:
			req = 0
	print(req)


I = lambda : list(map(int, input().split()))
J = lambda : int(input())
for _ in range(int(input())):
	n = J()
	A = I()
	c = n - 1
	while c > 0 and A[c - 1] >= A[c]:
		c -= 1
	while c > 0 and A[c - 1] <= A[c]:
		c -= 1
	print(c)


import sys
import math
import collections
import operator as op
from collections import deque
from math import gcd, inf, sqrt, pi, cos, sin, ceil, log2
from bisect import bisect_right, bisect_left
from functools import reduce
from sys import stdin, stdout, setrecursionlimit
setrecursionlimit(2 ** 20)

def factorial(n):
	if n == 0:
		return 1
	return n * factorial(n - 1)

def ncr(n, r):
	r = min(r, n - r)
	numer = reduce(op.mul, range(n, n - r, -1), 1)
	denom = reduce(op.mul, range(1, r + 1), 1)
	return numer // denom

def prime_factors(n):
	i = 2
	factors = []
	while i * i <= n:
		if n % i:
			i += 1
		else:
			n //= i
			factors.append(i)
	if n > 1:
		factors.append(n)
	return len(set(factors))

def isPowerOfTwo(x):
	return x and (not x & x - 1)

def factors(n):
	return list(set(reduce(list.__add__, ([i, n // i] for i in range(1, int(n ** 0.5) + 1) if n % i == 0))))
MOD = 1000000007
PMOD = 998244353
T = 1
T = int(stdin.readline())
for _ in range(T):
	n = int(stdin.readline())
	a = list(map(int, stdin.readline().rstrip().split()))
	ix = 0
	for i in range(n - 1, 0, -1):
		if a[i - 1] < a[i]:
			ix = i
			break
	res = 0
	for i in range(ix, 0, -1):
		if a[i - 1] > a[i]:
			res = i
			break
	print(res)


for tt in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	if l == sorted(l):
		print(0)
	else:
		i = n - 1
		ele = l[i]
		j = i
		lp = 0
		while j - 1 >= 0 and lp == 0 and (l[j - 1] >= l[j]):
			j -= 1
		lp += 1
		while j - 1 >= 0 and lp == 1 and (l[j - 1] <= l[j]):
			j -= 1
		print(len(l[:j]))


import sys
sys.setrecursionlimit(2147483647)
input = sys.stdin.readline

def calc(arr):
	flag = 0
	length = len(arr)
	for i in range(length - 1):
		if not flag:
			if arr[length - i - 2] < arr[length - i - 1]:
				flag = 1
		elif arr[length - i - 2] > arr[length - i - 1]:
			return length - i - 1
	return 0

def main():
	n = int(input())
	arr = []
	for _ in range(n):
		t = int(input())
		arr.append(list(map(int, input().split(' '))))
	for i in range(n):
		print(calc(arr[i]))
main()


T = int(input())
for q in range(T):
	n = int(input())
	s = list(map(int, input().split()))
	(b, a) = ([0] * n, [0] * n)
	(a[0], b[0]) = (1, 1)
	for i in range(n):
		if i > 0:
			if s[i - 1] > s[i]:
				b[i] = 1
			else:
				b[i] = b[i - 1] + 1
			if s[i - 1] < s[i]:
				a[i] = 1
			else:
				a[i] = a[i - 1] + 1
	if a[n - 1] == n or b[n - 1] == n:
		print(0)
		continue
	if b[n - 1] > a[n - 1]:
		print(n - b[n - 1])
	else:
		print(n - (a[n - 1] + b[n - a[n - 1] - 1]))


from math import *
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	c = n - 1
	while c > 0:
		if a[c - 1] >= a[c]:
			c = c - 1
		else:
			break
	while c > 0:
		if a[c - 1] <= a[c]:
			c = c - 1
		else:
			break
	print(c)


import sys
import math
import itertools
import functools
import collections
import operator
import fileinput
import copy
ORDA = 97

def ii():
	return int(input())

def mi():
	return map(int, input().split())

def li():
	return [int(i) for i in input().split()]

def lcm(a, b):
	return abs(a * b) // math.gcd(a, b)

def revn(n):
	return str(n)[::-1]

def dd():
	return collections.defaultdict(int)

def ddl():
	return collections.defaultdict(list)

def sieve(n):
	if n < 2:
		return list()
	prime = [True for _ in range(n + 1)]
	p = 3
	while p * p <= n:
		if prime[p]:
			for i in range(p * 2, n + 1, p):
				prime[i] = False
		p += 2
	r = [2]
	for p in range(3, n + 1, 2):
		if prime[p]:
			r.append(p)
	return r

def divs(n, start=2):
	r = []
	for i in range(start, int(math.sqrt(n) + 1)):
		if n % i == 0:
			if n / i == i:
				r.append(i)
			else:
				r.extend([i, n // i])
	return r

def divn(n, primes):
	divs_number = 1
	for i in primes:
		if n == 1:
			return divs_number
		t = 1
		while n % i == 0:
			t += 1
			n //= i
		divs_number *= t

def prime(n):
	if n == 2:
		return True
	if n % 2 == 0 or n <= 1:
		return False
	sqr = int(math.sqrt(n)) + 1
	for d in range(3, sqr, 2):
		if n % d == 0:
			return False
	return True

def convn(number, base):
	new_number = 0
	while number > 0:
		new_number += number % base
		number //= base
	return new_number

def cdiv(n, k):
	return n // k + (n % k != 0)

def ispal(s):
	for i in range(len(s) // 2 + 1):
		if s[i] != s[-i - 1]:
			return False
	return True

def main():
	for _ in range(ii()):
		n = ii()
		arr = li()
		ans = 0
		if arr[n - 2] >= arr[n - 1]:
			i = n - 2
			while i >= 0:
				if arr[i] < arr[i + 1]:
					break
				i -= 1
			j = i
			while j >= 0:
				if arr[j] > arr[j + 1]:
					break
				j -= 1
			ans = j + 1
		else:
			i = n - 2
			while i >= 0:
				if arr[i] > arr[i + 1]:
					break
				i -= 1
			ans = i + 1
		print(ans)
main()


from sys import stdin
inp = lambda : stdin.readline().strip()
t = int(inp())
for _ in range(t):
	n = int(inp())
	a = [int(x) for x in inp().split()]
	i = 1
	ans = 0
	while i < len(a) - 1:
		if a[i - 1] > a[i] and a[i] < a[i + 1]:
			ans = max(i, ans)
		elif a[i - 1] > a[i] and a[i] == a[i + 1]:
			x = i
			while i < len(a) - 1 and a[i] == a[i + 1]:
				i += 1
			if i < len(a) - 1 and a[x] < a[i + 1]:
				ans = max(x, ans)
			continue
		i += 1
	print(ans)


import collections

def solve(n, arr):
	pos = n - 1
	while pos > 0 and arr[pos] <= arr[pos - 1]:
		pos -= 1
	while pos >= 0 and arr[pos] >= arr[pos - 1]:
		pos -= 1
	if pos < 0:
		pos = 0
	print(pos)

def main():
	t = int(input())
	while t > 0:
		n = int(input())
		arr = [int(x) for x in input().split()]
		solve(n, arr)
		t -= 1
main()


for _ in range(int(input())):
	n = int(input())
	r = [int(i) for i in input().split()]
	res = r[::-1]
	i = 0
	while i < n - 1 and res[i] <= res[i + 1]:
		i += 1
	while i < n - 1 and res[i] >= res[i + 1]:
		i += 1
	print(n - i - 1)


def good(arr):
	flag = 0
	index = 0
	for i in range(len(arr) - 1, 0, -1):
		if arr[i] > arr[i - 1]:
			flag = 1
		if flag == 1:
			if arr[i] < arr[i - 1]:
				index = i
				break
	return index
t = int(input())
for i in range(0, t):
	n = int(input())
	arr = list(map(int, input().strip().split()))
	print(good(arr))


t = int(input())
for _ in range(t):
	n = int(input())
	ar = list(map(int, input().split()))
	bo = False
	c = 0
	b = True
	i = 1
	for i in range(n - 1, 0, -1):
		if ar[i] > ar[i - 1]:
			c += 1
			bo = True
		elif ar[i] < ar[i - 1]:
			c += 1
			if bo:
				b = False
				break
		else:
			c += 1
	if b and i == 1:
		c += 1
	print(n - c)


t = int(input())
for ex in range(t):
	n = int(input())
	a = [int(i) for i in input().split()]
	ind = len(a) - 2
	while ind >= 0:
		if a[ind] >= a[ind + 1]:
			ind -= 1
		else:
			good_suf = ind + 1
			break
	if ind == -1:
		print(0)
	else:
		good_pref = 0
		ind -= 1
		while ind >= 0:
			if a[ind] <= a[ind + 1]:
				ind -= 1
			else:
				good_pref = ind + 1
				break
		print(good_pref)


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	j = n - 1
	while j > 0 and l[j - 1] >= l[j]:
		j -= 1
	while j > 0 and l[j - 1] <= l[j]:
		j -= 1
	print(j)


def main():
	t = int(input())
	for _ in range(t):
		n = int(input())
		A = list(map(int, input().split()))
		pre = A[-1]
		f = -1
		s = 0
		for i in range(n - 2, -1, -1):
			if A[i] == pre:
				pre = A[i]
				continue
			elif A[i] < pre:
				f = 1
				s = i
				break
			else:
				s = i
				break
		if s == 0:
			print(0)
			continue
		pre = A[s]
		if f == 1:
			for i in range(s - 1, -1, -1):
				if A[i] <= pre:
					pre = A[i]
					if i == 0:
						print(0)
				else:
					print(i + 1)
					break
		else:
			peak = False
			for i in range(s - 1, -1, -1):
				if peak:
					if A[i] <= pre:
						pre = A[i]
						if i == 0:
							print(0)
					else:
						print(i + 1)
						break
				else:
					if i == 0:
						print(0)
					if A[i] >= pre:
						pre = A[i]
					else:
						peak = True
						pre = A[i]
main()


import sys
inputlist = sys.stdin.readlines()
number_of_testcases = int(inputlist[0].strip())
for i in range(number_of_testcases):
	n = int(inputlist[2 * i + 1].strip())
	numbers_list = list(map(int, inputlist[2 * i + 2].strip().split(' ')))
	pointer = n - 1
	while pointer > 0:
		if numbers_list[pointer - 1] < numbers_list[pointer]:
			break
		pointer -= 1
	while pointer > 0:
		if numbers_list[pointer - 1] > numbers_list[pointer]:
			break
		pointer -= 1
	print(pointer)


from sys import stdin
for _ in range(int(stdin.readline())):
	n = int(stdin.readline())
	a = list(map(int, stdin.readline().split()))[::-1]
	increasing = True
	res = 0
	for i in range(1, n):
		if increasing and a[i] < a[i - 1]:
			increasing = False
		elif not increasing and a[i] > a[i - 1]:
			res = n - i
			break
	print(res)


from sys import stdin
for _ in range(int(input())):
	a = int(stdin.readline().rstrip())
	l = list(map(int, stdin.readline().rstrip().split(' ')))
	if a <= 2:
		print('0')
	else:
		g_d = False
		c = 0
		for i in range(a - 1, 0, -1):
			if l[i] <= l[i - 1]:
				c += 1
			else:
				break
		for i in range(a - 1 - c, 0, -1):
			if l[i] >= l[i - 1]:
				c += 1
			else:
				break
		print(a - c - 1)


import sys
input = sys.stdin.readline
INF = float('inf')
tc = int(input())
for _ in range(tc):
	n = int(input())
	arr = list(map(int, input().split()))
	pos = n - 1
	while 0 < pos:
		if arr[pos] > arr[pos - 1]:
			break
		pos -= 1
	while 0 < pos:
		if arr[pos] < arr[pos - 1]:
			break
		pos -= 1
	print(pos)


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


x = int(input())
for z in range(x):
	a = int(input())
	l = list(map(int, input().split()))
	c = l.copy()
	c.sort()
	if l == c or l == c[::-1]:
		print(0)
	else:
		l = l[::-1]
		b = []
		for i in range(a - 1):
			b.append(l[i] - l[i + 1])
		k = q = 0
		if b[0] == 0:
			i = 0
			while b[i] == 0:
				i += 1
			k = i
			b = b[k:]
			q = k
		if b[0] < 0:
			y = 0
			for i in range(1, len(b)):
				if y == 1 and b[i] < 0:
					k = k + i
					break
				elif y == 0 and b[i] > 0:
					y = 1
			if k == q:
				k = k + len(b)
			print(a - k - 1)
		elif b[0] > 0:
			i = 0
			while b[i] >= 0:
				i += 1
			k = k + i
			print(a - k - 1)


def get_primeFactor(n):
	res = [1]
	x = 2
	while x * x <= n:
		while n % x == 0:
			res.append(x)
			n //= x
		x += 1
	if n > 1:
		res.append(n)
	return res

def getFactor(n):
	res = {1}
	x = 2
	while x * x <= n:
		if n % x == 0:
			res.add(x)
			res.add(n // x)
		x += 1
	return res
for _ in range(int(input())):
	n = int(input())
	ar = list(map(int, input().split()))
	pos = n - 1
	while pos > 0 and ar[pos - 1] >= ar[pos]:
		pos -= 1
	while pos > 0 and ar[pos - 1] <= ar[pos]:
		pos -= 1
	print(pos)


from sys import stdin

def iinput():
	return int(stdin.readline())

def minput():
	return map(int, stdin.readline().split())

def linput():
	return list(map(int, stdin.readline().split()))
t = iinput()
while t:
	t -= 1
	n = iinput()
	a = linput()
	if a == list(sorted(a)) or a == list(reversed(sorted(a))):
		print(0)
	else:
		j = n - 1
		while j > 0:
			if a[j - 1] >= a[j]:
				j -= 1
			else:
				break
		i = j - 1
		while i > 0:
			if a[i - 1] <= a[i]:
				i -= 1
			else:
				break
		print(i)


for _ in range(int(input())):
	n = int(input())
	b = list(map(int, input().split()))
	a = []
	c = 0
	for i in range(n):
		a.append(b[n - i - 1])
	i = 0
	while i != n - 1 and a[i + 1] >= a[i]:
		c = c + 1
		i = i + 1
	while i != n - 1 and a[i + 1] <= a[i]:
		c = c + 1
		i = i + 1
	print(n - c - 1)


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	ans = 0
	fl = 0
	a.reverse()
	for j in range(n - 1):
		if a[j + 1] < a[j]:
			fl = 1
		if fl == 1 and a[j + 1] > a[j]:
			ans = n - (j + 1)
			break
	print(ans)


import math
t = int(input())

def isGood(arr, a, b):
	while a < b - 1:
		sm = a if arr[a] <= arr[b] else b
		if sm == a:
			if arr[a] <= arr[a + 1]:
				a += 1
			else:
				return False
		elif arr[b] <= arr[b - 1]:
			b -= 1
		else:
			return False
		if a == b - 1:
			return True
	return True
for i in range(t):
	n = int(input())
	arr = [int(x) for x in input().split()]
	start = 0
	end = n - 1
	max_res = n - 2
	(l, r) = (0, max_res)
	while l <= r:
		m = (l + r) // 2
		a = m
		b = end
		if isGood(arr, a, b):
			r = m - 1
		else:
			l = m + 1
	print(l)


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()))
	print(solve(n, a))


def first_good_array(arr, n):
	i = n
	while i >= 1:
		i -= 1
		if arr[i - 1] < arr[i]:
			break
	while i >= 1:
		i -= 1
		if arr[i - 1] > arr[i]:
			break
	return i
tc = int(input())
for _ in range(tc):
	n = int(input())
	a = list(map(int, input().split()))
	print(first_good_array(a, n))


def bitonic(arr, n):
	i = n - 2
	while i >= 0 and arr[i] >= arr[i + 1]:
		i -= 1
	if i == -1:
		return 0
	while i >= 0 and arr[i] <= arr[i + 1]:
		i -= 1
	if i == -1:
		return 0
	else:
		return i + 1
t = int(input())
for cases in range(t):
	n = int(input())
	arr = list(map(int, input().strip().split()))
	if n < 3:
		print(0)
	else:
		print(bitonic(arr, n))


def solve():
	t = int(input().strip())
	for i in range(t):
		n = int(input().strip())
		a = list(map(int, input().split()))
		si = n - 1
		while si >= 1 and a[si - 1] >= a[si]:
			si -= 1
		while si >= 1 and a[si - 1] <= a[si]:
			si -= 1
		print(si)
solve()


import sys

def input():
	return sys.stdin.readline().rstrip()

def input_split():
	return [int(i) for i in input().split()]
testCases = int(input())
answers = []
for _ in range(testCases):
	n = int(input())
	arr = input_split()
	groups = []
	reduced = []
	reduced.append(arr[0])
	indices = [0]
	for i in range(1, n):
		if arr[i] != reduced[-1]:
			reduced.append(arr[i])
			indices.append(i)
	leng = len(reduced)
	reduced = [-1] + reduced + [-1]
	indices = [-1] + indices + [-1]
	last_instance_prob = None
	for i in range(1, leng + 1):
		if reduced[i] < reduced[i - 1] and reduced[i] < reduced[i + 1]:
			last_instance_prob = indices[i]
	if last_instance_prob is None:
		ans = 0
	else:
		ans = last_instance_prob
	answers.append(ans)
print(*answers, sep='\n')


T = int(input())
while T:
	n = int(input())
	A = list(map(int, input().split()))
	i = n - 2
	while i > -1 and A[i] >= A[i + 1]:
		i = i - 1
	while i > -1 and A[i] <= A[i + 1]:
		i = i - 1
	print(i + 1)
	T = T - 1


t = int(input(''))
mat = []
for i in range(t):
	input('')
	mat.append(list(map(int, input().split())))
for i in range(t):
	x = mat[i]
	n = len(x)
	cnt = 0
	while len(x) > 0:
		flag = 0
		ind = x.index(max(x))
		for j in range(ind + 1, len(x) - 1):
			if x[j] < x[j + 1]:
				flag = 1
				break
		if flag:
			del x[0:ind + 1]
			cnt = cnt + ind + 1
		else:
			sign = 0
			for j in range(ind - 1, 0, -1):
				if x[j] < x[j - 1]:
					cnt = cnt + j
					sign = 1
					break
			break
	print(cnt)


def main():
	n = int(input())
	lst = list(map(int, input().split()))
	curr = lst[-1]
	i = len(lst) - 2
	while i >= 0 and lst[i] >= curr:
		curr = lst[i]
		i -= 1
	while i >= 0 and lst[i] <= curr:
		curr = lst[i]
		i -= 1
	print(i + 1)
for _ in range(int(input())):
	main()


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	up = False
	down = False
	ans = 0
	for i in range(n - 1, 0, -1):
		if a[i] == a[i - 1] and up == False and (down == False):
			continue
		if a[i] > a[i - 1] and down == False and (up == False):
			down = True
		elif a[i] < a[i - 1] and up == False and (down == False):
			up = True
		elif a[i] <= a[i - 1] and up == True and (down == False):
			up = True
		elif a[i] > a[i - 1] and up == True:
			down = True
		elif a[i] < a[i - 1] and down == True and (up == True):
			ans = i
			break
		elif a[i] >= a[i - 1] and down == True:
			down = True
		elif a[i] < a[i - 1] and down == True and (up == False):
			ans = i
			break
	print(ans)


import math
import time
from collections import defaultdict, deque
from sys import stdin, stdout
from bisect import bisect_left, bisect_right
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, stdin.readline().split()))
	if n < 3:
		print(0)
	else:
		if a[-1] > a[-2]:
			assend = -1
		elif a[-1] < a[-2]:
			assend = 1
		else:
			assend = 0
		ans = 0
		for j in range(n - 3, -1, -1):
			if assend == 1:
				if a[j] < a[j + 1]:
					assend = -1
			elif assend == -1:
				if a[j] > a[j + 1]:
					ans = j + 1
					break
			elif a[j] > a[j + 1]:
				assend = 1
			elif a[j] < a[j + 1]:
				assend = -1
		print(ans)


def f(arr):
	if len(arr) <= 2:
		return 0
	i = -1
	while i > -len(arr) and arr[i] == arr[i - 1]:
		i -= 1
	if i == -len(arr):
		return 0
	while i > -len(arr) and arr[i] <= arr[i - 1]:
		i -= 1
	if i == -len(arr):
		return 0
	while i > -len(arr) and arr[i] >= arr[i - 1]:
		i -= 1
	if i == -len(arr):
		return 0
	return len(arr) + i
for _ in range(int(input())):
	int(input())
	print(f(list(map(int, input().split()))))


from sys import stdin, stdout
from math import gcd, sqrt
from collections import deque
input = stdin.readline
R = lambda : map(int, input().split())
I = lambda : int(input())
S = lambda : input().rstrip('\n')
L = lambda : list(R())
P = lambda x: stdout.write(x)
lcm = lambda x, y: x * y // gcd(x, y)
hg = lambda x, y: (y + x - 1) // x * x
pw = lambda x: 1 if x == 1 else 1 + pw(x // 2)
chk = lambda x: chk(x // 2) if not x % 2 else True if x == 1 else False
sm = lambda x: (x ** 2 + x) // 2
N = 10 ** 9 + 7
for _ in range(I()):
	n = I() - 1
	flg = True
	a = L()[::-1]
	p = a[0]
	for i in a[1:]:
		if flg:
			if i < p:
				flg = False
			n -= 1
			p = i
		else:
			if i > p:
				break
			n -= 1
			p = i
	print(n)


t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	ans = n
	now = 0
	count = 0
	li = 0
	for i in l[::-1]:
		if i >= now:
			now = i
			count += 1
		else:
			li = count
			break
	if count == n:
		print(0)
		continue
	for i in range(li, n):
		if now >= l[-i - 1]:
			now = l[-i - 1]
			count += 1
		else:
			break
	print(n - count)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	min_ans = n - 1
	j = n - 1
	while a[j] <= a[j - 1] and j > 0:
		j -= 1
	while a[j] >= a[j - 1] and j > 0:
		j -= 1
	print(j)


t = int(input())
for i in range(t):
	n = int(input())
	arr = [int(j) for j in input().split()]
	j = len(arr) - 2
	while j >= 0 and arr[j] >= arr[j + 1]:
		j -= 1
	while j >= 0 and arr[j] <= arr[j + 1]:
		j -= 1
	if j >= 0:
		print(j + 1)
	else:
		print(0)


from __future__ import division, print_function
from fractions import Fraction
import sys
import os
from io import BytesIO, IOBase
from itertools import *
import bisect
from heapq import *
from math import *
from copy import *
from collections import deque
from collections import Counter as counter
from itertools import combinations as comb
from itertools import permutations as permutate
from bisect import bisect_left as bl
from bisect import bisect_right as br
from bisect import bisect
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
mod = 1000000007

def inp():
	return sys.stdin.readline().rstrip('\r\n')

def out(var):
	sys.stdout.write(str(var))

def lis():
	return list(map(int, inp().split()))

def stringlis():
	return list(map(str, inp().split()))

def sep():
	return map(int, inp().split())

def strsep():
	return map(str, inp().split())

def zerolist(n):
	return [0] * n

def nextline():
	out('\n')

def testcase(t):
	for p in range(t):
		solve()

def printlist(a):
	for p in range(0, len(a)):
		out(str(a[p]) + ' ')

def lcm(a, b):
	return a * b // gcd(a, b)

def power(a, b):
	ans = 1
	while b > 0:
		if b % 2 == 1:
			ans *= a
		a *= a
		b //= 2
	return ans

def ncr(n, r):
	return factorial(n) // (factorial(r) * factorial(max(n - r, 1)))

def isPrime(n):
	if n <= 1:
		return False
	if n <= 3:
		return True
	if n % 2 == 0 or n % 3 == 0:
		return False
	i = 5
	while i * i <= n:
		if n % i == 0 or n % (i + 2) == 0:
			return False
		i = i + 6
	return True

def solve():
	n = int(inp())
	a = lis()
	ind = -1
	for i in range(n - 2, -1, -1):
		if a[i] >= a[i + 1]:
			continue
		else:
			ind = i
			break
	if ind == -1 or a[:ind + 1] == sorted(a[:ind + 1]):
		print(0)
	else:
		index = ind
		f = False
		while index >= 1 and a[index - 1] <= a[index]:
			f = True
			index -= 1
		if f:
			print(index)
		else:
			print(ind)
testcase(int(inp()))


R1 = lambda : list(map(int, input().split()))
R2 = lambda : int(input())
for _ in range(R2()):
	n = R2()
	A = R1()
	p = n - 1
	while p > 0 and A[p - 1] >= A[p]:
		p -= 1
	while p > 0 and A[p - 1] <= A[p]:
		p -= 1
	print(p)


t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	ups = []
	temp = 0
	for i in range(n - 1):
		if l[i] < l[i + 1]:
			if temp != 1:
				ups.append(i)
			temp = 1
		elif l[i] > l[i + 1]:
			temp = 0
	if len(ups) == 0:
		print(0)
	else:
		ans = ups[-1]
		count = int(ups[-1])
		for i in range(ans - 1, -1, -1):
			if l[i] == l[ans]:
				count -= 1
			else:
				break
		print(count)


import math as m
i = input()
inp = []
for a in range(0, int(i)):
	input()
	inp.append([int(x) for x in input().split()])

def logic(n, arr):
	if n <= 2:
		return 0
	curr = arr[n - 1]
	nex = arr[n - 2]
	increasing = curr <= nex
	peaks = 0
	for k in range(n - 2, 0, -1):
		curr = nex
		nex = arr[k - 1]
		if increasing:
			if curr > nex:
				increasing = False
		elif curr < nex:
			return k
	return 0
for arr in inp:
	n = len(arr)
	print(logic(n, arr))


t = int(input())
for i in range(t):
	l = int(input())
	p = list(map(int, input().split()))
	p.reverse()
	j = 0
	if l > 1:
		while j < l - 1 and p[j] <= p[j + 1]:
			j += 1
		while j < l - 1 and p[j] >= p[j + 1]:
			j += 1
		j += 1
		print(l - j)
	else:
		print(0)


t = int(input())
for i in range(t):
	n = int(input()) - 1
	a = [*map(int, input().split())]
	while n > 0 and a[n] <= a[n - 1]:
		n -= 1
	while n > 0 and a[n] >= a[n - 1]:
		n -= 1
	print(n)


import sys
import math
import heapq
from collections import defaultdict, deque
from decimal import *

def r():
	return int(input())

def rm():
	return map(int, input().split())

def rl():
	return list(map(int, input().split()))
for _ in range(r()):
	n = r()
	a = rl()
	res = True
	for i in range(1, n):
		if a[i] < a[i - 1]:
			res = False
			break
	if res:
		print(0)
	else:
		visited = defaultdict(int)
		remove = 0
		i = 0
		while i < n:
			res = True
			lo = i
			hi = n - 1
			last = min(a[lo], a[hi])
			while lo < hi:
				if min(a[lo], a[hi]) < last:
					res = False
					break
				elif a[lo] <= a[hi]:
					last = a[lo]
					lo += 1
				else:
					last = a[hi]
					hi -= 1
			if lo == hi:
				if a[hi] < last:
					res = False
			if res:
				print(i)
				break
			elif visited[lo] == 1:
				i = lo + 1
			else:
				i = lo
				visited[lo] = 1


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


import sys
sys.setrecursionlimit(10 ** 5)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep='\n')

def II():
	return int(sys.stdin.readline())

def MI():
	return map(int, sys.stdin.readline().split())

def LI():
	return list(map(int, sys.stdin.readline().split()))

def LLI(rows_number):
	return [LI() for _ in range(rows_number)]

def SI():
	return sys.stdin.readline()[:-1]
for _ in range(II()):
	n = II()
	aa = LI()
	i = n - 1
	while i and aa[i - 1] >= aa[i]:
		i -= 1
	while i and aa[i - 1] <= aa[i]:
		i -= 1
	print(i)


for t in range(int(input())):
	n = int(input())
	arr = [int(x) for x in input().split()]
	ans = n - 1
	while ans > 0 and arr[ans - 1] >= arr[ans]:
		ans -= 1
	while ans > 0 and arr[ans - 1] <= arr[ans]:
		ans -= 1
	print(ans)


for i in range(int(input())):
	n = int(input())
	a = [int(num) for num in input().split()]
	c = 0
	p = 0
	for j in range(1, n):
		if a[n - j] > a[n - j - 1]:
			p = n - j - 1
			break
	if p > 0:
		for k in range(0, p):
			if a[p - k] < a[p - k - 1]:
				c = p - k
				break
	print(c)


t = int(input())

def solve(arr):
	n = len(arr)
	x = sorted(arr)
	if arr == x or arr == x[::-1] or n == 1:
		return 0
	arr = arr[::-1]
	i = 0
	while i < n - 1 and arr[i + 1] >= arr[i]:
		i += 1
	ans1 = i
	i = 0
	while i < n - 1 and arr[i + 1] <= arr[i]:
		i += 1
	ans2 = i
	i = 0
	ascend = True
	descend = False
	while i < n - 1:
		if ascend and arr[i + 1] >= arr[i]:
			i += 1
		else:
			descend = True
			ascend = False
		if descend and arr[i + 1] <= arr[i]:
			i += 1
		else:
			descend = False
		if not ascend and (not descend):
			break
	ans3 = i
	ans = max(ans1, max(ans2, ans3)) + 1
	return n - ans
while t > 0:
	n = int(input())
	arr = list(map(int, input().split()))
	print(solve(arr))
	t -= 1


import sys, io, os, time
from collections import defaultdict
from collections import OrderedDict
from collections import deque
from itertools import combinations
from itertools import permutations
import bisect, math, heapq
alphabet = 'abcdefghijklmnopqrstuvwxyz'
input = sys.stdin.readline
for ii in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	i = n - 1
	while i >= 0:
		if arr[i - 1] >= arr[i]:
			i -= 1
		else:
			break
	while i >= 0:
		if arr[i - 1] <= arr[i]:
			i -= 1
		else:
			break
	print(0 if i < 0 else i)


t = int(input())
for i in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	order = []
	c = []
	flag = [-1 for j in range(n)]
	for j in range(1, n):
		if l[j - 1] < l[j]:
			flag[j - 1] = 0
		elif l[j - 1] > l[j]:
			flag[j - 1] = 1
		else:
			flag[j - 1] = 2
	var = -1
	ze = -1
	k = 0
	for k in range(n - 2, -1, -1):
		if var == -2 and flag[k] == 1:
			k = k + 1
			break
		if flag[k] == 1 and var != -2:
			var = 1
		if flag[k] == 0:
			var = -2
			ze = 1
		if var == -2 and flag[k] == 1:
			break
	print(k)


import sys
import math
from collections import defaultdict

def solve(test):
	n = int(input())
	a = list(map(int, input().split()))
	if n <= 2:
		print(0)
	else:
		j = n - 1
		flag = None
		count = 0
		while j > 0:
			if a[j] > a[j - 1]:
				if flag is None:
					flag = 1
				elif flag == 0:
					flag = 1
					count += 1
			elif a[j] < a[j - 1]:
				if flag is None:
					flag = 0
				elif flag == 1:
					count += 2
			if count >= 2:
				print(j)
				return
			j -= 1
		print(j)
test_cases = int(input())
for t in range(1, test_cases + 1):
	solve(t)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	l = list(reversed(a))
	x = l[0]
	y = True
	end = 0
	for i in range(1, n):
		if y == True:
			if x <= l[i]:
				x = l[i]
			else:
				y = False
		if y == False:
			if x >= l[i]:
				x = l[i]
			else:
				end = n - i
				break
	print(end)


def main():
	t = Iint()
	for _ in range(t):
		n = Iint()
		a = Ilist()
		up = True
		for i in range(n - 2, -1, -1):
			if up:
				if a[i] >= a[i + 1]:
					continue
				else:
					up = False
			elif a[i] <= a[i + 1]:
				continue
			else:
				break
		else:
			print(0)
			continue
		print(i + 1)
FIO = 1

def I():
	return input()

def Iint():
	return int(input())

def Ilist():
	return list(map(int, input().split()))

def Imap():
	return map(int, input().split())

def Plist(li, s=' '):
	print(s.join(map(str, li)))
en = enumerate
if not FIO:
	if __name__ == '__main__':
		main()
		exit()
else:
	from io import BytesIO, IOBase
	import sys
	import os
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))

def input():
	return sys.stdin.readline().rstrip('\r\n')
main()


t = int(input())
ans = {}
while t:
	n = int(input())
	line = input().split()
	ans = n
	pick = False
	last = -1
	for el in line[::-1]:
		if not pick:
			if last > int(el):
				pick = True
		elif last < int(el):
			break
		ans -= 1
		last = int(el)
	print(ans)
	t -= 1


count = int(input())
for i in range(count):
	n = int(input())
	test = [int(j) for j in input().split()]
	answer = n - 1
	elem = test[-1]
	j = n - 2
	while elem <= test[j] and j >= 0:
		answer -= 1
		elem = test[j]
		j -= 1
	while elem >= test[j] and j >= 0:
		answer -= 1
		elem = test[j]
		j -= 1
	print(answer)


t = int(input())
while t > 0:
	n = int(input())
	L = list(map(int, input().split()))
	ptr1 = 0
	ptr2 = n - 1
	count = 0
	if L[ptr1] < L[ptr2]:
		prev = L[ptr1]
		ptr1 += 1
	else:
		prev = L[ptr2]
		ptr2 -= 1
	while ptr1 < ptr2:
		if prev > min(L[ptr1], L[ptr2]):
			if L[ptr1] > L[ptr2]:
				count = ptr1 + 1
				ptr2 = n - 1
				ptr1 += 1
			else:
				count = ptr1
				ptr2 = n - 1
		if L[ptr1] < L[ptr2]:
			prev = L[ptr1]
			ptr1 += 1
		else:
			prev = L[ptr2]
			ptr2 -= 1
	print(count)
	t -= 1


import sys
T = int(sys.stdin.readline())
for _ in range(T):
	n = int(sys.stdin.readline())
	list_num = list(map(int, sys.stdin.readline().split()))
	s = 0
	former = -1
	while True:
		if s == former:
			s += 1
		former = s
		save_s = s
		e = n - 1
		can = True
		now_min = 0
		while s < e:
			if list_num[s] >= list_num[e] >= now_min:
				now_min = list_num[e]
				e -= 1
			elif list_num[e] >= list_num[s] >= now_min:
				now_min = list_num[s]
				s += 1
			else:
				can = False
				break
		if can:
			print(save_s)
			break


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


(T,) = map(int, input().split())
for _ in range(T):
	(N,) = map(int, input().split())
	X = list(map(int, input().split()))
	for i in range(N - 2, -2, -1):
		if i == -1:
			break
		if X[i] >= X[i + 1]:
			continue
		break
	if i == -1:
		print(0)
		continue
	for j in range(i, -2, -1):
		if j == -1:
			break
		if X[j] <= X[j + 1]:
			continue
		break
	if j == -1:
		print(0)
		continue
	print(j + 1)


for _ in range(int(input())):
	n = int(input())
	arr = [int(f) for f in input().split()]
	pos = n - 1
	while pos > 0 and arr[pos - 1] >= arr[pos]:
		pos -= 1
	while pos > 0 and arr[pos - 1] <= arr[pos]:
		pos -= 1
	print(pos)


t = int(input())
for veces in range(t):
	n = int(input())
	vec = list(map(int, input().split()))
	i = n - 1
	while vec[i - 1] >= vec[i] and i >= 1:
		i = i - 1
	while vec[i - 1] <= vec[i] and i >= 1:
		i = i - 1
	rta = i
	if rta == -1:
		rta = 0
	print(rta)


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	val = 0
	j = n - 1
	while j > 0:
		if arr[j] <= arr[j - 1]:
			val += 1
			j -= 1
		else:
			break
	if j == 0:
		val += 1
	else:
		i = j
		while i > 0:
			if arr[i] >= arr[i - 1]:
				val += 1
				i -= 1
			else:
				break
		if j == 0:
			val += 1
	if val == n:
		print(0)
	else:
		x = n - val - 1
		print(x)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = a
	al = sorted(b)
	if al == a:
		print(0)
	else:
		a = a[::-1]
		se = 0
		count = 0
		sl = 0
		for i in range(0, len(a) - 1):
			if se == 0:
				if a[i + 1] < a[i]:
					se = 1
					count += 1
					for j in range(i + 1, len(a) - 1):
						if a[j] >= a[j + 1]:
							count += 1
						elif a[j] < a[j + 1]:
							sl += 1
							break
					count += 1
					if sl == 1:
						break
				elif a[i + 1] >= a[i]:
					if i == n - 2:
						count += 2
					else:
						count += 1
			else:
				break
		print(n - count)


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


T = int(input())
for _ in range(T):
	n = int(input())
	a = list(map(int, input().strip().split()))
	a.reverse()
	i = 0
	while i < n - 1 and a[i + 1] >= a[i]:
		i += 1
	while i < n - 1 and a[i + 1] <= a[i]:
		i += 1
	print(n - i - 1)


t = int(input().strip())
while t > 0:
	t -= 1
	n = int(input())
	a = list(map(int, input().strip().split()))
	p = len(a) - 1
	while p - 1 >= 0 and a[p - 1] >= a[p]:
		p = p - 1
	while p - 1 >= 0 and a[p - 1] <= a[p]:
		p = p - 1
	print(p)


def inp():
	return int(input())

def inlt():
	return list(map(int, input().split()))

def insr():
	s = input()
	return list(s[:len(s)])

def invr():
	return map(int, input().split())
n = inp()
for i in range(n):
	m = inp()
	l = inlt()
	t = m - 1
	top = 0
	while t >= 1:
		if l[t] > l[t - 1] and top == 0:
			top = 1
		if top == 1 and l[t] < l[t - 1]:
			break
		t -= 1
	print(t)


def answer(n, A):
	count = 1
	flag = 0
	for i in range(n - 1, 0, -1):
		if A[i] - A[i - 1] <= 0 and flag == 0:
			count += 1
		elif A[i] - A[i - 1] >= 0:
			flag = 1
			count += 1
		else:
			break
	return n - count
t = int(input())
for i in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	print(answer(n, arr))


T = int(input())
for _ in range(T):
	n = int(input())
	ls = list(map(int, input().split()))[::-1]
	up = True
	down = True
	ans = 0
	if n == 1 or n == 2:
		print(0)
	else:
		down = False
		ans = [ls[0]]
		for i in range(1, n):
			if ls[i] >= ls[i - 1] and down is False:
				ans.append(ls[i])
			elif ls[i] <= ls[i - 1]:
				down = True
				ans.append(ls[i])
			elif ls[i] >= ls[i - 1] and down is True:
				break
		print(n - len(ans))


from sys import stdout
from collections import defaultdict
t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	k = 0
	ans = 0
	for i in range(1, n):
		if l[i] < l[i - 1]:
			k = i
		elif l[i] > l[i - 1] and k > 0:
			ans = k
	stdout.write(str(ans) + '\n')


def solve(lst, n):
	i = 0
	while i < n - 1 and lst[i] <= lst[i + 1]:
		i += 1
	if i == n - 1:
		return 0
	i = 0
	while i < n - 1 and lst[i] >= lst[i + 1]:
		i += 1
	if i == n - 1:
		return 0
	j = n - 1
	while j > 0 and lst[j] <= lst[j - 1]:
		j -= 1
	while j > 0 and lst[j] >= lst[j - 1]:
		j -= 1
	return j
for _ in range(int(input())):
	n = int(input())
	lst = list(map(int, input().split()))
	print(solve(lst, n))


t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	i = n - 2
	while i >= 0 and l[i] >= l[i + 1]:
		i -= 1
	while i >= 0 and l[i] <= l[i + 1]:
		i -= 1
	print(i + 1)


t = int(input())
for i in range(t):
	f = lambda : map(int, input().split(' '))
	n = int(input())
	ar = list(f())
	l = ['i']
	flag = 0
	ds = []
	i = n - 1
	while i >= 1 and ar[i] <= ar[i - 1]:
		i -= 1
	while i >= 1 and ar[i] >= ar[i - 1]:
		i -= 1
	print(i)


tCases = int(input())
for _ in range(tCases):
	n = int(input())
	a = list(map(int, input().split()))
	index = n - 1
	while index > 0:
		if a[index] > a[index - 1]:
			break
		index -= 1
	while index > 0:
		if a[index] < a[index - 1]:
			break
		index -= 1
	print(index)


t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(i) for i in input().split()]
	g = n - 1
	while g > 0 and a[g - 1] >= a[g]:
		g -= 1
	while g > 0 and a[g - 1] <= a[g]:
		g -= 1
	print(g)


def main():
	n = int(input())
	a = list(map(int, input().split()))
	cnt = 0
	flag = True
	start = 0
	for i in range(n - 1):
		if a[i] > a[i + 1]:
			flag = False
		if flag == False and a[i] < a[i + 1]:
			start = i - cnt
			cnt = 0
			flag = True
		if a[i] == a[i + 1]:
			cnt += 1
		else:
			cnt = 0
	print(start)
	return

def test():
	t = int(input())
	while t:
		main()
		t -= 1
test()


import sys
import os
import math
import copy
from math import gcd
from bisect import bisect
from io import BytesIO, IOBase
from math import sqrt, floor, factorial, gcd, log, ceil
from collections import deque, Counter, defaultdict
from itertools import permutations, combinations

def Int():
	return int(sys.stdin.readline())

def Mint():
	return map(int, sys.stdin.readline().split())

def Lstr():
	return list(sys.stdin.readline().strip())

def Str():
	return sys.stdin.readline().strip()

def Mstr():
	return map(str, sys.stdin.readline().strip().split())

def List():
	return list(map(int, sys.stdin.readline().split()))

def Hash():
	return dict()

def Mod():
	return 1000000007

def Ncr(n, r, p):
	return fact[n] * (ifact[r] * ifact[n - r] % p) % p

def Most_frequent(list):
	return max(set(list), key=list.count)

def Mat2x2(n):
	return [List() for _ in range(n)]

def Prime_factors(n):
	i = 2
	factors = set()
	while i * i <= n:
		if n % i:
			i += 1
		else:
			n //= i
			factors.add(i)
	if n > 1:
		factors.add(n)
	print(factors)
	if len(factors) == 2:
		return True
	else:
		return False

def Divisors(n):
	l = []
	for i in range(1, int(math.sqrt(n) + 1)):
		if n % i == 0:
			if n // i == i:
				l.append(i)
			else:
				l.append(i)
				l.append(n // i)
	return l

def Sieve(n):
	prime = [True for i in range(n + 1)]
	lst = []
	p = 2
	while p * p <= n:
		if prime[p]:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p = p + 1
	for i in range(2, n + 1):
		if prime[i]:
			lst.append(i)
	return lst

def minimum_integer_not_in_list(a):
	b = max(a)
	if b < 1:
		return 1
	A = set(a)
	B = set(range(1, b + 1))
	D = B - A
	if len(D) == 0:
		return b + 1
	else:
		return min(D)

def solution():
	for i in range(Int()):
		n = Int()
		a = List()
		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
		if i >= 0:
			print(i + 1)
		else:
			print(0)
solution()


from sys import stdin

def main():
	l = stdin.read().splitlines()[2::2]
	for (i, s) in enumerate(l):
		(*aa, a) = map(int, s.split())
		while aa:
			if aa[-1] < a:
				break
			a = aa.pop()
		while aa:
			if aa[-1] > a:
				break
			a = aa.pop()
		l[i] = str(len(aa))
	print('\n'.join(l))
main()


def solve():
	n = int(input())
	arr = [int(v) for v in input().split()]
	r = n - 1
	while r >= 0:
		if r and arr[r - 1] >= arr[r]:
			r -= 1
		else:
			break
	l = r - 1
	while l >= 0:
		if arr[l] <= arr[l + 1]:
			l -= 1
		else:
			print(l + 1)
			return
	print(0)
t = int(input())
for _ in range(t):
	solve()


num = int(input())
for i in range(num):
	length = int(input())
	quest = input().split()
	numbers = [int(k) for k in quest]
	pos = length - 1
	while pos > 0 and numbers[pos - 1] >= numbers[pos]:
		pos -= 1
	while pos > 0 and numbers[pos - 1] <= numbers[pos]:
		pos -= 1
	print(pos)


import sys
input = sys.stdin.readline
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	a = a[::-1]
	res = [a[0]]
	sign = '+'
	for i in range(1, n):
		if sign == '+':
			if res[-1] <= a[i]:
				res.append(a[i])
			else:
				sign = '-'
				res.append(a[i])
		elif res[-1] >= a[i]:
			res.append(a[i])
		else:
			break
	print(n - len(res))


T = int(input())
for _ in range(T):
	n = int(input())
	s = list(map(int, input().split()))
	count = n - 1
	if sorted(s) == s:
		print(0)
	else:
		while count > 0 and s[count] <= s[count - 1]:
			count -= 1
		while count > 0 and s[count] >= s[count - 1]:
			count -= 1
		print(count)


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)


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


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)


results = []
tests = int(input())
for _x in range(tests):
	n = int(input())
	a = list((int(item) for item in input().split()))[:n]
	peek = n - 1
	for i in range(len(a) - 2, -1, -1):
		if a[i] >= a[i + 1]:
			peek = i
		else:
			break
	goodArray = peek
	for i in range(peek - 1, -1, -1):
		if a[i] <= a[i + 1]:
			goodArray = i
		else:
			break
	results.append(goodArray)
print(*results, sep='\n')


from sys import stdin

def r():
	return stdin.readline().strip()

def r_t(tp):
	return map(tp, r().strip().split())

def r_a(tp):
	return list(r_t(tp))

def solve(A, n):
	(ans, down, idx) = (0, False, 0)
	for i in range(n - 1):
		if i > 0 and A[i] != A[i - 1]:
			idx = i
		if A[i + 1] < A[i] and (not down):
			down = True
		elif A[i + 1] > A[i] and down:
			(ans, down) = (idx, False)
	return ans

def main():
	cases = int(r())
	for case in range(cases):
		n = int(r())
		A = list(r_t(int))
		print(solve(A, n))
main()


t = int(input())
for k in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	flag = -1
	for i in range(n - 1, 0, -1):
		if i and a[i - 1] < a[i]:
			flag = i
			break
	if flag == -1:
		print(0)
	else:
		ans = -1
		for i in range(flag, 0, -1):
			if i and a[i - 1] > a[i]:
				ans = i
				break
		if ans == -1:
			print(0)
		else:
			print(ans)


def search(nums, k):
	pos = k - 1
	while (pos > 0) & (nums[pos - 1] >= nums[pos]):
		pos = pos - 1
	while (pos > 0) & (nums[pos - 1] <= nums[pos]):
		pos = pos - 1
	return pos
MAXS = 2 * 10 * 5
n = int(input())
for _ in range(n):
	k = int(input())
	numbers = list(map(int, input().split()))
	print(search(numbers, k))


t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	ans = 0
	flag = 0
	mend = arr[-1]
	for i in range(len(arr) - 2, 0, -1):
		if arr[i] < mend and arr[i] < arr[i - 1]:
			flag = 1
			print(i)
			break
		else:
			mend = max(arr[i], mend)
	if flag == 0:
		print(0)


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	k = len(l) - 1
	done = False
	while k > 0 and l[k] <= l[k - 1]:
		k -= 1
	while k > 0 and l[k] >= l[k - 1]:
		k -= 1
	print(k)


for _ in range(int(input())):
	N = int(input())
	A = list(map(int, input().split()))
	A.reverse()
	if N == 1:
		print(0)
	elif A[1] >= A[0]:
		i = 2
		while i < N:
			if A[i] >= A[i - 1]:
				i += 1
			else:
				break
		while i < N:
			if A[i] <= A[i - 1]:
				i += 1
			else:
				break
		print(N - i)
	else:
		i = 2
		while i < N:
			if A[i] <= A[i - 1]:
				i += 1
			else:
				break
		print(N - i)


from collections import defaultdict
t = int(input())
for _ in range(t):
	n = int(input())
	arr = [int(i) for i in input().split()]
	val = 0
	desc = asc = 0
	for i in range(n - 1):
		if arr[i] < arr[i + 1]:
			if desc == 1:
				val = temp
				desc = 0
		elif arr[i] > arr[i + 1]:
			desc = 1
			temp = i + 1
	print(val)


import math
t = int(input())
for i in range(t):
	l = int(input())
	arr = [int(i) for i in input().split()]
	hm = False
	i = l - 1
	while i > 0:
		if arr[i] > arr[i - 1]:
			break
		i -= 1
	while i > 0:
		if arr[i] < arr[i - 1]:
			break
		i -= 1
	print(i)


for _ in range(int(input())):
	n = int(input())
	ar = list(map(int, input().split()))
	pt = 0
	j = n - 1
	while j > 0:
		if ar[j - 1] >= ar[j]:
			j -= 1
		else:
			break
	for i in range(j):
		if ar[i + 1] < ar[i]:
			pt = i + 1
	print(pt)


from sys import stdin, stdout

def INPUT():
	return list((int(i) for i in stdin.readline().split()))
import math

def inp():
	return stdin.readline()

def out(x):
	return stdout.write(x)
import math as M
MOD = 10 ** 9 + 7
from random import randint as R
import sys
for _ in range(int(input())):
	n = int(input())
	A = INPUT()
	ans = []
	m = 0
	first = 0
	end = n - 1
	if n == 1:
		print(0)
	else:
		limit = A[end]
		while end > 0 and A[end] == A[end - 1]:
			end -= 1
		if end > 0:
			if A[end] < A[end - 1]:
				while end > 0 and A[end - 1] >= A[end]:
					end -= 1
				if end > 0:
					while end > 0 and A[end] >= A[end - 1]:
						end -= 1
			else:
				while end > 0 and A[end - 1] <= A[end]:
					end -= 1
		print(max(end, 0))


t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	c = False
	ind = 0
	for i in range(n - 1, 0, -1):
		if c == False:
			if arr[i] > arr[i - 1]:
				c = True
		elif arr[i - 1] > arr[i]:
			ind = i
			break
	print(ind)


from math import inf, log2

class SegmentTree:

	def __init__(self, array, func=max):
		self.n = len(array)
		self.size = 2 ** (int(log2(self.n - 1)) + 1) if self.n != 1 else 1
		self.func = func
		self.default = 0 if self.func != min else inf
		self.data = [self.default] * (2 * self.size)
		self.process(array)

	def process(self, array):
		self.data[self.size:self.size + self.n] = array
		for i in range(self.size - 1, -1, -1):
			self.data[i] = self.func(self.data[2 * i], self.data[2 * i + 1])

	def query(self, alpha, omega):
		if alpha == omega:
			return self.data[alpha + self.size]
		res = self.default
		alpha += self.size
		omega += self.size + 1
		while alpha < omega:
			if alpha & 1:
				res = self.func(res, self.data[alpha])
				alpha += 1
			if omega & 1:
				omega -= 1
				res = self.func(res, self.data[omega])
			alpha >>= 1
			omega >>= 1
		return res

	def update(self, index, value):
		index += self.size
		self.data[index] = value
		index >>= 1
		while index:
			self.data[index] = self.func(self.data[2 * index], self.data[2 * index + 1])
			index >>= 1
import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
from math import gcd, ceil

def pre(s):
	n = len(s)
	pi = [0] * n
	for i in range(1, n):
		j = pi[i - 1]
		while j and s[i] != s[j]:
			j = pi[j - 1]
		if s[i] == s[j]:
			j += 1
		pi[i] = j
	return pi

def prod(a):
	ans = 1
	for each in a:
		ans = ans * each
	return ans

def lcm(a, b):
	return a * b // gcd(a, b)

def binary(x, length=16):
	y = bin(x)[2:]
	return y if len(y) >= length else '0' * (length - len(y)) + y
for _ in range(int(input()) if True else 1):
	n = int(input())
	a = list(map(int, input().split()))
	if n == 1:
		print(0)
		continue
	i = n - 2
	while i >= 0 and a[i] >= a[i + 1]:
		i -= 1
	if i == -1:
		print(0)
		continue
	j = i - 1
	while j >= 0 and a[j] <= a[j + 1]:
		j -= 1
	if j == -1:
		print(0)
		continue
	print(j + 1)


for i in range(int(input())):
	t = int(input())
	A = list(map(int, input().split()))
	ans = t
	down = 0
	for k in range(t - 1, -1, -1):
		ans -= 1
		if k == 0 or k == t - 1:
			continue
		else:
			if A[k] < A[k + 1]:
				down = 1
			if A[k - 1] > A[k] and down == 1:
				break
	print(ans)


from collections import deque

def findPrefixLen(n, a):

	def isGood(a, p):
		(left, right) = (p, n - 1)
		last = float('-inf')
		while left <= right:
			if a[left] <= a[right] and a[left] >= last:
				last = a[left]
				left = left + 1
			elif a[right] <= a[left] and a[right] >= last:
				last = a[right]
				right = right - 1
			else:
				return False
		return True
	(left, right) = (0, n)
	while left < right:
		mid = (left + right) // 2
		if isGood(a, mid):
			right = mid
		else:
			left = mid + 1
	return left
t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	print(findPrefixLen(n, a))


t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(x) for x in input().split()]
	f1 = False
	i = n - 1
	while i > 0:
		if f1:
			if a[i] < a[i - 1]:
				break
		elif a[i] > a[i - 1]:
			f1 = True
		i -= 1
	print(i)


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	b = [i for i in a]
	b.sort()
	d = {}
	for i in range(n):
		if a[i] in d.keys():
			d[a[i]].append(i)
		else:
			d[a[i]] = [i]
	for i in d.keys():
		d[i] = d[i][::-1]
	i = 0
	j = n - 1
	k = 0
	an = 0
	while k < n:
		c = d[b[k]].pop()
		if c < i:
			aa = 0
		elif c == j or b[k] == a[j]:
			j -= 1
			d[b[k]].append(c)
		elif c == i:
			i += 1
		else:
			an = max(an, c)
			i = c + 1
		k += 1
	print(an)


def inc(A):
	for i in range(len(A) - 1):
		if A[i] >= A[i + 1]:
			return False
		return True

def incr(A):
	a = 0
	for i in range(len(A) - 1, -1, -1):
		if A[i] > A[i - 1]:
			a = i
			break
	return a
n = int(input())
for i in range(0, n):
	y = int(input())
	x = list(map(int, input().split()))
	a = [0] * y
	for j in range(y):
		a[j] = x[j]
	a.sort()
	count = 0
	for j in range(y):
		if x[j] != a[j]:
			count = 1
			break
	if count == 0:
		print('0')
		continue
	q = 0
	z = incr(x)
	for j in range(z, -1, -1):
		if x[j] < x[j - 1]:
			q = j
			break
	print(q)


def ans(a, n):
	index1 = len(a) + 1
	for i in range(n - 1, 0, -1):
		if a[i - 1] >= a[i]:
			pass
		else:
			index1 = i
			break
	if index1 == len(a) + 1:
		return 0
	index2 = len(a) + 1
	for i in range(index1, 0, -1):
		if a[i - 1] <= a[i]:
			pass
		else:
			index2 = i
			break
	if index2 == len(a) + 1:
		return 0
	return index2
m = int(input())
for i in range(m):
	n = int(input())
	arr = input().split()
	a = []
	for i in arr:
		a.append(int(i))
	print(ans(a, n))


import sys

def solve(n, a):
	cur = -1
	i = n - 1
	while i > 0 and a[i] >= cur:
		cur = a[i]
		i -= 1
	if i == 0:
		return 0
	while i - 1 >= 0 and (a[i - 1] <= a[i] or (a[i - 1] <= a[-1] and a[i] >= a[-1])):
		i -= 1
	return i
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	print(solve(n, a))


t = int(input())
for _ in range(t):
	n = int(input())
	x = list(map(int, input().split()))
	if n == 1:
		print(0)
		continue
	f = 0
	x.reverse()
	for i in range(n - 1):
		if x[i + 1] < x[i]:
			f = 1
			break
	if f == 0:
		print(0)
		continue
	f = 0
	for j in range(i, n - 1):
		if x[j + 1] > x[j]:
			f = 1
			break
	if f == 0:
		print(0)
	else:
		print(n - 1 - j)


t = int(input())
cnt = 0
while cnt < t:
	cnt += 1
	n = int(input())
	a = [int(i) for i in input().split()]
	l = -1
	r = n
	while r - l > 1:
		mid = (r + l) // 2
		base_l = mid - 1
		base_r = n
		res = 0
		flag = True
		while base_r - base_l > 1:
			if res > a[base_r - 1] and res > a[base_l + 1]:
				flag = False
				break
			if a[base_r - 1] > a[base_l + 1]:
				(argmin, val) = (a[base_l + 1], base_l + 1)
			else:
				(argmin, val) = (a[base_r - 1], base_r - 1)
			if res <= argmin:
				res = argmin
				if val == base_l + 1:
					base_l += 1
				else:
					base_r -= 1
			elif val == base_l + 1:
				res = a[base_r - 1]
				base_r -= 1
			else:
				res = a[base_l + 1]
				base_l += 1
		if flag == False:
			l = mid
		else:
			r = mid
	print(r)


for _ in range(int(input())):
	n = int(input())
	a = [int(i) for i in input().split()]
	a.reverse()
	(c, d) = (0, 0)
	(i1, i2) = (0, n - 1)
	bad = 0
	ibad = n - 1
	for i in range(n - 1):
		if a[i] <= a[i + 1]:
			c += 1
		else:
			if i == 0:
				bad = 1
			i1 = i + 1
			break
	if bad != 1 and i1 != 0:
		for i in range(i1, n - 1):
			if a[i] >= a[i + 1]:
				c += 1
			else:
				i2 = i
				break
	if bad == 1:
		for i in range(n - 1):
			if a[i] < a[i + 1]:
				ibad = i
				break
		print(n - 1 - ibad)
	else:
		print(n - 1 - i2)


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	b = list(range(n - 1))
	for j in range(n - 1):
		b[j] = a[j + 1] - a[j]
	c = 0
	d = 0
	ans = 0
	for k in range(n - 2, -1, -1):
		if b[k] > 0:
			d = 0
			c += 1
		if b[k] < 0:
			d += 1
		if c > 0 and d == 1:
			ans = k + 1
			break
	print(ans)


for T in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	(increasing, f) = (True, 0)
	for i in range(n - 1, 0, -1):
		if a[i] > a[i - 1]:
			increasing = False
		if a[i] < a[i - 1] and increasing == False:
			f = 1
			break
	if f:
		print(i)
	else:
		print('0')


import sys
input = sys.stdin.readline

def goodArray(arr, leftmark, rightmark):
	for i in range(leftmark - 1, -1, -1):
		if arr[i] <= arr[i + 1]:
			rightmark = i
		else:
			break
	return rightmark

def solution():
	n = input()
	arr = list(map(int, input().split()))
	leftmark = len(arr) - 1
	for j in range(len(arr) - 2, -1, -1):
		if arr[j] >= arr[j + 1]:
			leftmark -= 1
		else:
			break
	print(goodArray(arr, leftmark, leftmark))
t = int(input())
for i in range(0, t):
	solution()


for item in range(int(input())):
	n = int(input())
	mass = list(map(int, input().split()))
	if n == 1:
		print(0)
		continue
	flag = n - 1
	while mass[flag - 1] >= mass[flag] and flag != 1:
		flag -= 1
	if flag == 1:
		print(0)
		continue
	while mass[flag - 1] <= mass[flag]:
		flag -= 1
		if flag == 0:
			break
	if flag == 0:
		print(0)
		continue
	print(flag)


I = input
for _ in range(int(I())):
	n = int(I())
	a = list(map(int, I().split()))
	ans = 1
	c = 0
	for i in range(n - 2, -1, -1):
		if a[i] >= a[i + 1] and c == 0:
			ans += 1
		elif a[i] <= a[i + 1]:
			c += 1
			ans += 1
		else:
			break
	print(n - ans)


tests = int(input())
for t in range(tests):
	n = int(input())
	ls = list(map(int, input().split()))
	ls = ls[::-1]
	inc = True
	status = True
	prev = ls[0]
	for (idx, item) in enumerate(ls):
		if inc:
			if item < prev:
				inc = False
		if not inc:
			if item > prev:
				status = False
				break
		prev = item
	if status:
		print(0)
	else:
		print(len(ls) - idx)


t = int(input())
while t:
	n = int(input())
	a = list(map(int, input().rstrip().split()))
	if n == 1 or n == 2:
		print(0)
	else:
		index = 0
		x = a[0]
		y = 1
		b = []
		for i in range(1, n):
			if a[i] == x:
				y += 1
			else:
				b.append((x, y))
				y = 1
				x = a[i]
		b.append((x, y))
		index = -1
		for i in range(1, len(b) - 1):
			if b[i - 1][0] > b[i][0] and b[i + 1][0] > b[i][0]:
				index = i
		if index == -1:
			print(0)
		else:
			ans = 0
			for i in range(index):
				ans += b[i][1]
			print(ans)
	t -= 1


for _ in range(int(input())):
	n = int(input())
	list1 = list(map(int, input().split()))
	inc = 0
	dec = 0
	flag = 0
	flag1 = 0
	printflag = 0
	if sorted(list1) == list1:
		print(0)
	elif sorted(list1, reverse=True) == list1:
		print(0)
	else:
		for i in range(n - 2, -1, -1):
			if list1[i] > list1[i + 1]:
				if flag == 0:
					if inc != 1:
						dec = 1
					else:
						printflag = 1
						print(i + 1)
						break
				else:
					printflag = 1
					print(i + 1)
					flag1 = 1
					break
			elif list1[i] < list1[i + 1]:
				if dec == 1 and flag == 0:
					inc = 1
				elif dec == 0:
					flag = 1
		if flag == 1 and flag1 != 1:
			print(0)
		elif printflag == 0:
			print(0)


import math
from collections import defaultdict, deque
from bisect import bisect_left, bisect_right
from sys import stdin

def mp():
	return map(int, stdin.readline().split())

def ml():
	return list(map(int, stdin.readline().split()))
for _ in range(int(input())):
	n = int(input())
	a = ml()
	mx1 = [0] * n
	mx2 = [0] * (n + 1)
	ans = -1
	(num, ti) = ([], [])
	for i in range(n):
		mx1[i] = max(mx1[i - 1], a[i])
	for i in range(n - 1, -1, -1):
		mx2[i] = max(mx2[i + 1], a[i])
		if mx2[i] > a[i] and mx1[i] > a[i]:
			if num == [] or num[-1] > a[i]:
				num.append(a[i])
				ti.append(i)
	i = 0
	ans = -1
	while ti != [] and i < n:
		if a[i] > num[-1]:
			ans = i
		if i >= ti[-1]:
			ti.pop()
			num.pop()
		i += 1
	if ans == -1:
		print(0)
	else:
		print(ans + 1)


t = int(input())
for i in range(t):
	n = int(input())
	ar = [int(i) for i in input().split()]
	a = -1
	boo = True
	co = 0
	for x in range(n - 1):
		if boo == True and ar[a] > ar[a - 1]:
			boo = False
		else:
			pass
		if boo == False and ar[a] < ar[a - 1]:
			co = n + a
			break
		else:
			pass
		a = a - 1
	print(co)


def f():
	t = int(input())
	ns = []
	arr = []
	for _ in range(t):
		ns.append(int(input()))
		arr.append(list(map(int, input().split())))
	for lis in arr:
		i = len(lis) - 1
		while i > 0:
			if lis[i] <= lis[i - 1]:
				i -= 1
			else:
				break
		while i > 0:
			if lis[i] >= lis[i - 1]:
				i -= 1
			else:
				break
		print(i)
f()


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


import bisect
import os
import gc
import sys
from io import BytesIO, IOBase
from collections import Counter
from collections import deque
import heapq
import math
import statistics

def sin():
	return input()

def ain():
	return list(map(int, sin().split()))

def sain():
	return input().split()

def iin():
	return int(sin())
MAX = float('inf')
MIN = float('-inf')
MOD = 1000000007

def readTree(n, m):
	adj = [deque([]) for _ in range(n + 1)]
	for _ in range(m):
		(u, v) = ain()
		adj[u].append(v)
		adj[v].append(u)
	return adj

def main():
	for _ in range(iin()):
		n = iin()
		l = ain()
		ans = 0
		x = n - 1
		for i in range(n - 2, -1, -1):
			if l[i] >= l[i + 1]:
				x = i
			else:
				break
		ans = x
		for i in range(ans - 1, -1, -1):
			if l[i] <= l[i + 1]:
				x = i
			else:
				break
		print(x)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


for i in range(int(input())):
	n = int(input())
	m = list(map(int, input().split()))
	k = n - 1
	while k - 1 >= 0 and m[k - 1] >= m[k]:
		k -= 1
	while k - 1 >= 0 and m[k - 1] <= m[k]:
		k -= 1
	print(k)


from sys import stdin

def readline():
	return stdin.readline()
tests = int(readline())

def solve(n, a):
	f = a[n - 1] > a[n - 2]
	k = 0
	j = 1 if f else 2
	for i in reversed(range(0, n)):
		if f and a[i] < a[i - 1] or (not f and a[i] > a[i - 1]):
			k += 1
			f = not f
			if k == j:
				return i
	return 0
for t in range(0, tests):
	n = int(readline().rstrip('\n'))
	a = list(map(int, readline().split(' ')))
	print(solve(n, a))


import sys

def get_ints():
	return map(int, sys.stdin.readline().strip().split())

def get_ls():
	return list(map(int, sys.stdin.readline().strip().split()))

def main():
	for _ in range(int(input())):
		n = int(input())
		ls = get_ls()
		p = n - 1
		while p > 0 and ls[p] <= ls[p - 1]:
			p -= 1
		while p > 0 and ls[p] >= ls[p - 1]:
			p -= 1
		print(p)
main()


import sys
t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, sys.stdin.readline().split()))
	if n == 1 or n == 2:
		print(0)
		continue
	flagLow = False
	flag2 = 0
	flag = 0
	maxVal = 0
	prev = arr[-1]
	arr.reverse()
	low = False
	for i in range(1, n):
		x = arr[i]
		if x - prev > 0:
			flag2 = 1
		if x - prev < 0:
			flagLow = True
		if low == True:
			if x - prev > 0:
				ans = n - i
				print(ans)
				flag = 1
				break
		if x - prev < 0:
			low = True
		if x - prev > 0:
			high = True
		prev = x
	if flag == 1:
		continue
	if flagLow == True and flag2 == 0:
		print(0)
		continue
	if flagLow == False and flag2 == 1:
		print(0)
		continue
	if flagLow == False and flag2 == 0:
		print(0)
		continue
	print(0)


import sys
import collections

def input():
	return sys.stdin.readline().rstrip()

def split_input():
	return [int(i) for i in input().split()]
tests = int(input())
for _ in range(tests):
	n = int(input())
	a = split_input()
	ind = n - 2
	while ind >= 0 and a[ind] <= a[ind + 1]:
		ind -= 1
	ans1 = ind + 1
	ind = n - 2
	while ind >= 0 and a[ind] >= a[ind + 1]:
		ind -= 1
	while ind >= 0 and a[ind] <= a[ind + 1]:
		ind -= 1
	ans2 = ind + 1
	print(min(ans1, ans2))


t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	if len(l) == 1:
		print(0)
	else:
		c = 0
		x = 0
		for i in range(n - 2, -1, -1):
			if c == 0:
				if l[i] < l[i + 1]:
					c = -1
				elif l[i] > l[i + 1]:
					c = 1
			elif l[i] > l[i + 1] and c == -1:
				x = i + 1
				break
			elif l[i] < l[i + 1]:
				c = -1
		print(x - 0)


import math
t = int(input())
for w in range(t):
	n = int(input())
	l = [int(i) for i in input().split()]
	k = l[n - 1]
	c = -1
	for i in range(n - 1, -1, -1):
		if l[i] != k:
			c = i
			break
	if c == -1:
		print(0)
	elif l[c] < k:
		m = c
		for i in range(c - 1, -1, -1):
			if l[i] > l[m]:
				c1 = i
				break
			else:
				m = i
		if m == 0:
			print(0)
		else:
			print(c1 + 1)
	else:
		c1 = -2
		for i in range(c - 1, -1, -1):
			if l[i] < l[i + 1]:
				c1 = i
				break
		if c1 == -2:
			print(0)
		else:
			m = c1
			c2 = -3
			for i in range(c1 - 1, -1, -1):
				if l[i] > l[m]:
					c2 = i
					break
				else:
					m = i
			if c2 == -3:
				print(0)
			elif m == 0:
				print(0)
			else:
				print(c2 + 1)


try:
	t = int(input())
	for v in range(t):
		n = int(input())
		a = list(map(int, input().split()))
		i = len(a) - 1
		maximum = a[-1]
		while i >= 0:
			if a[i] < maximum:
				break
			maximum = a[i]
			i -= 1
		if i == -1:
			print(0)
		else:
			j = i
			while j >= 0:
				if a[j] > a[j + 1]:
					break
				j -= 1
			if j == -1:
				print(0)
			else:
				print(j + 1)
except:
	pass


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	if n == 1:
		print(0)
		continue
	c = arr[n - 1]
	for i in range(n - 2, -1, -1):
		if arr[i] >= c:
			c = arr[i]
		else:
			break
	d = arr[i]
	j = i
	for j in range(i - 1, -2, -1):
		if j == -1:
			break
		if arr[j] <= d:
			d = arr[j]
		else:
			break
	print(j + 1)


from sys import stdin, stdout
import math
from itertools import permutations, combinations
from collections import defaultdict
from collections import Counter
from bisect import bisect_left
import sys
from queue import PriorityQueue
import operator as op
from functools import reduce
mod = 1000000007

def L():
	return list(map(int, stdin.readline().split()))

def In():
	return map(int, stdin.readline().split())

def I():
	return int(stdin.readline())

def printIn(ob):
	return stdout.write(str(ob) + '\n')

def powerLL(n, p):
	result = 1
	while p:
		if p & 1:
			result = result * n % mod
		p = int(p / 2)
		n = n * n % mod
	return result

def ncr(n, r):
	r = min(r, n - r)
	numer = reduce(op.mul, range(n, n - r, -1), 1)
	denom = reduce(op.mul, range(1, r + 1), 1)
	return numer // denom

def myCode():
	n = int(input())
	a = list(map(int, input().split()))
	j = n - 1
	while a[j] <= a[j - 1] and j > 0:
		j -= 1
	while a[j] >= a[j - 1] and j > 0:
		j -= 1
	print(j)

def main():
	for t in range(I()):
		myCode()
main()


for i in range(int(input())):
	t = int(input())
	l = list(map(int, input().split()))
	p = t - 1
	while p > 0 and l[p] <= l[p - 1]:
		p -= 1
	while p > 0 and l[p] >= l[p - 1]:
		p -= 1
	print(p)


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	if sorted(arr) == arr or sorted(arr)[::-1] == arr:
		print(0)
	else:
		i = n - 1
		while i > 0 and arr[i] <= arr[i - 1]:
			i -= 1
		while i > 0 and arr[i] >= arr[i - 1]:
			i -= 1
		print(i)


from math import *
t = int(input())
while t > 0:
	n = int(input())
	a = [int(x) for x in input().split()]
	decr = 0
	a.reverse()
	val = -1
	for i in range(1, n):
		if a[i] < a[i - 1]:
			decr = 1
		if decr == 1 and a[i] > a[i - 1]:
			val = i
			break
	if val == -1:
		print(0)
	else:
		print(n - i)
	t -= 1


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	E = 1
	direction = 'up'
	a = a[::-1]
	for i in range(n - 1):
		if direction == 'up':
			E += 1
			if a[i] <= a[i + 1]:
				pass
			else:
				direction = 'down'
		elif a[i] >= a[i + 1]:
			E += 1
		else:
			break
	print(n - E)


from sys import stdin, gettrace
if gettrace():
	inputi = input
else:

	def input():
		return next(stdin)[:-1]

	def inputi():
		return stdin.buffer.readline()

def solve():
	n = int(inputi())
	aa = [int(a) for a in inputi().split()]
	i = n - 1
	while i > 0 and aa[i] <= aa[i - 1]:
		i -= 1
	while i > 0 and aa[i] >= aa[i - 1]:
		i -= 1
	print(i)

def main():
	t = int(inputi())
	for _ in range(t):
		solve()
main()


t = int(input())
for _ in range(t):
	n = int(input())
	array = [int(x) for x in input().split()]
	if n < 3:
		print(0)
		continue
	i = len(array) - 2
	while i >= 0:
		if array[i] < array[i + 1]:
			break
		i -= 1
	while i >= 0:
		if array[i] > array[i + 1]:
			break
		i -= 1
	print(i + 1)


for i in range(int(input())):
	n = int(input())
	ar = list(map(int, input().split()))
	c = n
	cv = 0
	f = False
	for j in reversed(ar):
		if cv == 0:
			cv = ar[-1]
			c -= 1
			continue
		elif (j >= cv) & (f == False):
			cv = j
			c -= 1
		elif (j < cv) & (f == False):
			f = True
			cv = j
			c -= 1
		elif (j <= cv) & f:
			cv = j
			c -= 1
		else:
			break
	print(c)


t = int(input())
for _ in range(t):
	n = int(input())
	arr = [int(x) for x in input().split()]
	p = n - 1
	for i in range(n - 1, 0, -1):
		p = i
		if arr[i] > arr[i - 1]:
			break
	pp = -1
	for i in range(p - 1, 0, -1):
		if arr[i] < arr[i - 1]:
			pp = i
			break
	if pp == -1:
		print(0)
	else:
		print(n - (n - pp))


t = int(input())
while t:
	n = int(input())
	a = input().split()
	a = list(map(int, a))
	i = n - 2
	p = a[n - 1]
	while i >= 0 and a[i] >= p:
		p = a[i]
		i -= 1
	while i >= 0 and a[i] <= p:
		p = a[i]
		i -= 1
	print(i + 1)
	t -= 1


from sys import stdin
input = stdin.readline

def isGood(A):
	flag = 0
	cnt = 0
	for i in range(len(A) - 1):
		if flag == 0:
			if A[i] > A[i + 1]:
				flag = 1
		elif flag == 1:
			if A[i] < A[i + 1]:
				flag = 0
				cnt = i
			if A[i] == A[i + 1]:
				flag = 2
				start = i
		elif flag == 2:
			if A[i] < A[i + 1]:
				flag = 0
				cnt = start
				start = -1
			elif A[i] > A[i + 1]:
				flag = 1
	return cnt
T = int(input())
for _ in range(T):
	n = int(input())
	A = list(map(int, input().split()))
	print(isGood(A))


import sys

def rs():
	return sys.stdin.readline().rstrip()

def ri():
	return int(sys.stdin.readline())

def ria():
	return list(map(int, sys.stdin.readline().split()))

def ws(s):
	sys.stdout.write(s + '\n')

def wi(n):
	sys.stdout.write(str(n) + '\n')

def wia(a):
	sys.stdout.write(' '.join([str(x) for x in a]) + '\n')
import math
from collections import defaultdict
for _ in range(ri()):
	n = ri()
	l = ria()
	p = n - 1
	while p > 0 and l[p - 1] >= l[p]:
		p -= 1
	while p > 0 and l[p - 1] <= l[p]:
		p -= 1
	wi(p)


def check(a):
	k = 0
	for i in range(1, len(a) - 1):
		if a[i - 1] > a[i]:
			if a[i + 1] > a[i]:
				k = i
			else:
				c = 0
				while a[i + 1] == a[i] and i < len(a) - 2:
					i += 1
					c += 1
				if a[i + 1] > a[i]:
					k = i - c
	return k
for t in range(int(input())):
	input()
	a = list(map(int, input().split()))
	k = 0
	k = check(a)
	print(k)


import sys, math, cmath, time
start_time = time.time()

def solve():
	n = inp()
	a = list(reversed(inlt()))
	(q, qw, i, j) = (0, 0, 0, 0)
	for i in range(len(a) - 1):
		if a[i] > a[i + 1] and qw == 0:
			qw = 1
		if a[i] < a[i + 1] and qw == 1:
			q = 1
			break
	print(max(0, len(a) - i - 2 + q + j))

def main():
	global tt
	if not ONLINE_JUDGE:
		sys.stdin = open('input.txt', 'r')
		sys.stdout = open('output.txt', 'w')
	t = 1
	t = inp()
	for tt in range(t):
		solve()
	if not ONLINE_JUDGE:
		print('Time Elapsed :', time.time() - start_time, 'seconds')
		sys.stdout.close()

def inp():
	return int(input())

def inlt():
	return list(map(int, input().split()))

def insr():
	return input().strip()

def invr():
	return map(int, input().split())

def counter(a):
	q = [0] * max(a)
	for i in range(len(a)):
		q[a[i] - 1] = q[a[i] - 1] + 1
	return q

def counter_elements(a):
	q = dict()
	for i in range(len(a)):
		if a[i] not in q:
			q[a[i]] = 0
		q[a[i]] = q[a[i]] + 1
	return q

def string_counter(a):
	q = [0] * 26
	for i in range(len(a)):
		q[ord(a[i]) - 97] = (q[ord(a[i]) - 97], 1)
	return q

def factors(n):
	q = []
	for i in range(1, int(n ** 0.5) + 1):
		if n % i == 0:
			q.append(i)
			q.append(n // i)
	return list(sorted(list(set(q))))

def prime_factors(n):
	q = []
	while n % 2 == 0:
		q.append(2)
		n = n // 2
	for i in range(3, int(n ** 0.5) + 1, 2):
		while n % i == 0:
			q.append(i)
			n = n // i
	if n > 2:
		q.append(n)
	return list(sorted(q))

def transpose(a):
	(n, m) = (len(a), len(a[0]))
	b = [[0] * n for i in range(m)]
	for i in range(m):
		for j in range(n):
			b[i][j] = a[j][i]
	return b
ONLINE_JUDGE = __debug__
if ONLINE_JUDGE:
	input = sys.stdin.readline
main()


def fun(n, lst):
	k = n - 1
	while k > 0 and lst[k - 1] >= lst[k]:
		k -= 1
	while k > 0 and lst[k - 1] <= lst[k]:
		k -= 1
	return k
for _ in range(int(input())):
	m = int(input())
	a = [int(i) for i in input().split()]
	print(fun(m, a))


import math

def mi():
	return map(int, input().split())

def ii():
	return int(input())

def li():
	return list(map(int, input().split()))

def si():
	return input().split()
t = ii()
for _ in range(t):
	n = ii()
	a = li()
	i = 0
	j = n - 1
	ma = 0
	ls = 0
	while i < j:
		if a[i] < a[j]:
			if ls <= a[i]:
				ls = a[i]
				i += 1
			else:
				ma = i
				j = n - 1
				ls = 0
		elif ls <= a[j]:
			ls = a[j]
			j -= 1
		else:
			ma = i + 1
			j = n - 1
			i += 1
			ls = 0
	print(ma)


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	if n == 1:
		print('0')
	else:
		ans = 0
		if arr[n - 1] > arr[n - 2]:
			for i in range(n - 1, 0, -1):
				if arr[i] < arr[i - 1]:
					ans = i
					break
			print(ans)
		else:
			pt = 0
			for i in range(n - 1, 0, -1):
				if arr[i] > arr[i - 1]:
					ans = i
					break
			for i in range(ans, 0, -1):
				if arr[i] < arr[i - 1]:
					pt = i
					break
			print(pt)


t = int(input())
while t:
	t -= 1
	n = int(input())
	l = list(map(int, input().split()))
	k = n - 1
	while k > 0 and l[k - 1] >= l[k]:
		k -= 1
	while k > 0 and l[k - 1] <= l[k]:
		k -= 1
	print(k)


import math
ans = ''
for _ in range(int(input())):
	n = int(input())
	p = list(map(int, input().split()))
	val = 0
	flag = 0
	cnt = 0
	mx = -1
	for i in range(n - 1, -1, -1):
		if flag == 1 and p[i - 1] > p[i]:
			val = n - (n - i)
			break
		if p[i] > p[i - 1]:
			flag = 1
			mx = p[i]
	ans += str(val) + '\n'
print(ans)


for cases in range(int(input(''))):
	n = int(input())
	l = list(map(int, input().split()))
	p = n - 1
	while p > 0 and l[p - 1] >= l[p]:
		p = p - 1
	while p > 0 and l[p] >= l[p - 1]:
		p = p - 1
	print(p)


t = int(input())
for i in range(t):
	n = int(input())
	A = list(map(int, input().split()))
	if n == 1:
		print(0)
	else:
		i = n - 1
		while i > 0 and A[i] <= A[i - 1]:
			i -= 1
		while i > 0 and A[i] >= A[i - 1]:
			i -= 1
		print(i)


t = int(input())
for _ in range(t):
	n = int(input())
	ls = list(map(int, input().split()))
	rev = ls[::-1]
	flag = 0
	ind = -1
	for i in range(n - 1):
		if flag == 1 and rev[i] < rev[i + 1]:
			ind = i
			break
		if rev[i] > rev[i + 1]:
			flag = 1
	if ind == -1:
		print(0)
	else:
		print(n - ind - 1)


ll = lambda : list(map(int, input().split()))
st = lambda : input()
v = lambda : map(int, input().split())
ii = lambda : int(input())
from collections import *
from math import *
from datetime import datetime
from sys import stdin, stdout
for _ in range(ii()):
	n = ii()
	a = ll()
	c1 = 0
	i = n - 1
	while i > 0:
		if a[i] <= a[i - 1]:
			c1 = c1 + 1
			i = i - 1
		else:
			break
	if i == 0:
		c1 += 1
	else:
		j = i
		while j > 0:
			if a[j] >= a[j - 1]:
				c1 = c1 + 1
				j = j - 1
			else:
				break
		if j == 0:
			c1 += 1
	if c1 == n:
		print(0)
	else:
		print(n - c1 - 1)


cases = int(input())
for _ in range(cases):
	n = int(input())
	c = list(map(int, input().split()))
	if c == sorted(c):
		print(0)
	else:
		pos = n - 1
		while pos > 0 and c[pos - 1] >= c[pos]:
			pos -= 1
		while pos > 0 and c[pos - 1] <= c[pos]:
			pos -= 1
		print(pos)


y = int(input())
for i in range(y):
	n = int(input())
	al = input().split()
	m = list(map(int, al))
	if n == 1 or n == 2:
		print(0)
		continue
	z = 0
	x = n - 1
	j = n - 1
	while j > 0:
		if m[j] > m[j - 1]:
			break
		j -= 1
	x = j
	if x == 1 or x == 0:
		print(0)
	elif x > 1:
		j = 0
		cut = 0
		inc = 0
		while j < x - 1:
			inc += 1
			if m[j] > m[j + 1]:
				cut += inc
				inc = 0
			j += 1
		print(cut)


f = lambda : map(int, input().split())
for _ in range(int(input())):
	n = int(input())
	a = list(f())
	p = n - 1
	while p and a[p] <= a[p - 1]:
		p -= 1
	while p and a[p] >= a[p - 1]:
		p -= 1
	print(p)


import sys
input = lambda : sys.stdin.readline()
int_arr = lambda : list(map(int, input().split()))
str_arr = lambda : list(map(str, input().split()))
get_str = lambda : map(str, input().split())
get_int = lambda : map(int, input().split())
get_flo = lambda : map(float, input().split())
mod = 1000000007

def solve(n, arr):
	fg = 0
	prev = arr[n - 1]
	c = 1
	for i in range(n - 2, -1, -1):
		if not fg:
			if arr[i] >= prev:
				c += 1
				prev = arr[i]
			else:
				fg = 1
				c += 1
				prev = arr[i]
		elif arr[i] <= prev:
			c += 1
			prev = arr[i]
		else:
			break
	print(n - c)
for _ in range(int(input())):
	n = int(input())
	arr = int_arr()
	if sorted(arr) == arr or sorted(arr)[::-1] == arr:
		print(0)
	else:
		solve(n, arr)


from collections import defaultdict
from collections import Counter
from sys import setrecursionlimit as srl
srl(2 * 10 ** 6)

def solve(n, a):
	peak = n - 1
	for i in range(n - 2, -1, -1):
		if a[i] >= a[i + 1]:
			peak = i
		else:
			break
	ga = peak
	for i in range(peak - 1, -1, -1):
		if a[i] <= a[i + 1]:
			ga = i
		else:
			break
	return ga
t = int(input())
while t:
	n = int(input())
	a = list(map(int, input().split()))
	print(solve(n, a))
	t -= 1


test = int(input())
for k in range(test):
	n = int(input())
	x = list(map(int, input().split()))
	c = 0
	m = 0
	for i in range(n - 1, -1, -1):
		if x[i] > x[i - 1]:
			break
	for j in range(i, -1, -1):
		if x[j] < x[j - 1]:
			break
	print(j)


import math
from collections import deque
from sys import stdin, stdout
input = stdin.readline
for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	mn = 99999999999
	r = n - 1
	while r - 1 >= 0 and arr[r - 1] >= arr[r]:
		r -= 1
	was = False
	while r - 1 >= 0:
		if arr[r - 1] > arr[r]:
			was = True
			break
		r -= 1
	if was:
		print(r)
	else:
		print(0)


for _ in range(0, int(input())):
	n = int(input())
	listt = list(map(int, input().split()))
	i = n - 1
	while i > 0 and listt[i] <= listt[i - 1]:
		i -= 1
	while i > 0 and listt[i] >= listt[i - 1]:
		i -= 1
	print(i)


from sys import stdin, stdout
import math, sys
from itertools import permutations, combinations
from collections import defaultdict, deque, OrderedDict
from os import path
import bisect as bi
import heapq

def yes():
	print('YES')

def no():
	print('NO')
if path.exists('input.txt'):
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')

	def I():
		return int(input())

	def In():
		return map(int, input().split())
else:

	def I():
		return int(stdin.readline())

	def In():
		return map(int, stdin.readline().split())

def dict(a):
	d = {}
	for x in a:
		if d.get(x, -1) != -1:
			d[x] += 1
		else:
			d[x] = 1
	return d

def find_gt(a, x):
	i = bi.bisect_right(a, x)
	if i != len(a):
		return i
	else:
		return -1

def main():
	try:
		n = I()
		l = list(In())
		if n == 1:
			print(0)
			return
		else:
			ans = 0
			ma = l[-1]
			pos = -1
			for i in range(n - 1, -1, -1):
				if ma <= l[i]:
					pos = i
					ma = l[i]
				else:
					break
			ans += n - pos
			for i in range(pos - 1, -1, -1):
				if l[i] - l[i + 1] > 0:
					break
				else:
					ans += 1
			print(n - ans)
	except:
		pass
M = 998244353
P = 1000000007
for _ in range(I()):
	main()


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
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)


def main():
	(n,) = map(int, input().split(' '))
	a = list(map(int, input().split(' ')))
	a.reverse()
	p = a[0]
	i = 0
	while i < n and p <= a[i]:
		p = a[i]
		i += 1
	while i < n and p >= a[i]:
		p = a[i]
		i += 1
	print(n - i)
for _ in range(int(input())):
	main()


import sys
import math, bisect
from itertools import groupby, accumulate
from heapq import heapify, heappop, heappush
from collections import deque, Counter, OrderedDict

def neo():
	return map(int, input().split())

def Neo():
	return list(map(int, input().split()))
for _ in range(int(input())):
	N = int(input())
	A = Neo()
	i = N - 1
	while i >= 1:
		if A[i - 1] >= A[i]:
			i -= 1
		else:
			break
	while i >= 1:
		if A[i - 1] <= A[i]:
			i -= 1
		else:
			break
	print(i)


k = int(input())
for i in range(k):
	n = int(input())
	l = list(map(int, input().split(' ')))
	pos = n - 1
	while pos != 0:
		if l[pos - 1] < l[pos]:
			break
		pos -= 1
	while pos != 0:
		if l[pos - 1] > l[pos]:
			break
		pos -= 1
	print(pos)


import os
import sys
debug = True
if debug and os.path.exists('input.in'):
	input = open('input.in', 'r').readline
else:
	debug = False
	input = sys.stdin.readline

def inp():
	return int(input())

def inlt():
	return list(map(int, input().split()))

def insr():
	s = input()
	return s[:len(s) - 1]

def invr():
	return map(int, input().split())
test_count = inp()
for t in range(test_count):
	if debug:
		print('Test Case #', t + 1)
	n = inp()
	a = inlt()
	decr = False
	ans = 0
	for i in range(n - 2, -1, -1):
		if a[i] == a[i + 1]:
			continue
		elif a[i] > a[i + 1]:
			if decr:
				ans = i + 1
				break
		else:
			decr = True
	print(ans)


import sys
input = sys.stdin.readline
for i in range(int(input())):
	n = int(input())
	m = list(map(int, input().split()))
	i = n - 1
	while i > 0 and m[i - 1] >= m[i]:
		i -= 1
	while i > 0 and m[i - 1] <= m[i]:
		i -= 1
	print(i)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	l = []
	neg = 0
	c = 0
	for i in range(n - 1, 0, -1):
		if a[i - 1] - a[i] == 0:
			if not l:
				pass
			else:
				l.append(l[-1])
		else:
			l.append((a[i - 1] - a[i]) // abs(a[i - 1] - a[i]))
			if l[-1] == 1 and neg != 0:
				c = i
				break
			if l[-1] == -1:
				neg += 1
	print(n - (n - c))


import sys, math
mod = 10 ** 9 + 7
md = 998244353
input = lambda : sys.stdin.readline().strip()
inp = lambda : list(map(int, input().split()))
for _ in range(int(input())):
	(n,) = inp()
	a = list(map(int, input().split()))
	a = a[::-1]
	inc = False
	dec = False
	ans = 0
	for i in range(1, n):
		if a[i - 1] > a[i]:
			inc = True
		if a[i - 1] < a[i]:
			if inc == True:
				ans = n - i
				break
			dec = True
	print(ans)


from math import ceil
from collections import deque
for _ in range(int(input())):
	n = int(input())
	a = [int(i) for i in input().split()]
	f = 0
	ans = 0
	for i in range(n - 1, 0, -1):
		if a[i] > a[i - 1]:
			f = 1
		if f and a[i] < a[i - 1]:
			ans = i
			break
	print(ans)


for _ in range(int(input())):
	(n, l) = (int(input()), list(map(int, input().split()))[::-1])
	k = True
	s = False
	for i in range(n - 1):
		if k == True:
			if l[i] > l[i + 1]:
				k = False
		elif l[i] < l[i + 1]:
			s = True
			break
	if s == True:
		print(n - i - 1)
	else:
		print(0)


for test_case in range(int(input())):
	length = int(input())
	arr = list(map(int, input().split()))
	ans = []
	flag = False
	for x in range(length - 1, 0, -1):
		if not flag:
			if arr[x] > arr[x - 1]:
				flag = True
				ans.append(arr[x])
			else:
				ans.append(arr[x])
		elif arr[x] < arr[x - 1]:
			ans.append(arr[x])
			break
		else:
			ans.append(arr[x])
	else:
		ans.append(arr[0])
	print(length - len(ans))


from bisect import *
from collections import *
from math import gcd, ceil, sqrt, floor, inf
from heapq import *
from itertools import *
from operator import add, mul, sub, xor, truediv, floordiv
from functools import *
import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def RL():
	return map(int, sys.stdin.readline().rstrip().split())

def RLL():
	return list(map(int, sys.stdin.readline().rstrip().split()))

def N():
	return int(input())
from types import GeneratorType

def bootstrap(f, stack=[]):

	def wrappedfunc(*args, **kwargs):
		if stack:
			return f(*args, **kwargs)
		else:
			to = f(*args, **kwargs)
			while True:
				if type(to) is GeneratorType:
					stack.append(to)
					to = next(to)
				else:
					stack.pop()
					if not stack:
						break
					to = stack[-1].send(to)
			return to
	return wrappedfunc
farr = [1]
ifa = []

def fact(x, mod=0):
	if mod:
		while x >= len(farr):
			farr.append(farr[-1] * len(farr) % mod)
	else:
		while x >= len(farr):
			farr.append(farr[-1] * len(farr))
	return farr[x]

def ifact(x, mod):
	global ifa
	ifa.append(pow(farr[-1], mod - 2, mod))
	for i in range(x, 0, -1):
		ifa.append(ifa[-1] * i % mod)
	ifa = ifa[::-1]

def per(i, j, mod=0):
	if i < j:
		return 0
	if not mod:
		return fact(i) // fact(i - j)
	return farr[i] * ifa[i - j] % mod

def com(i, j, mod=0):
	if i < j:
		return 0
	if not mod:
		return per(i, j) // fact(j)
	return per(i, j, mod) * ifa[j] % mod

def catalan(n):
	return com(2 * n, n) // (n + 1)

def linc(f, t, l, r):
	while l < r:
		mid = (l + r) // 2
		if t > f(mid):
			l = mid + 1
		else:
			r = mid
	return l

def rinc(f, t, l, r):
	while l < r:
		mid = (l + r + 1) // 2
		if t < f(mid):
			r = mid - 1
		else:
			l = mid
	return l

def ldec(f, t, l, r):
	while l < r:
		mid = (l + r) // 2
		if t < f(mid):
			l = mid + 1
		else:
			r = mid
	return l

def rdec(f, t, l, r):
	while l < r:
		mid = (l + r + 1) // 2
		if t > f(mid):
			r = mid - 1
		else:
			l = mid
	return l

def isprime(n):
	for i in range(2, int(n ** 0.5) + 1):
		if n % i == 0:
			return False
	return True

def binfun(x):
	c = 0
	for w in arr:
		c += ceil(w / x)
	return c

def lowbit(n):
	return n & -n

def inverse(a, m):
	a %= m
	if a <= 1:
		return a
	return (1 - inverse(m, a) * m) // a % m

class BIT:

	def __init__(self, arr):
		self.arr = arr
		self.n = len(arr) - 1

	def update(self, x, v):
		while x <= self.n:
			self.arr[x] += v
			x += x & -x

	def query(self, x):
		ans = 0
		while x:
			ans += self.arr[x]
			x &= x - 1
		return ans

class DSU:

	def __init__(self, n):
		self.c = [-1] * n

	def same(self, x, y):
		return self.find(x) == self.find(y)

	def find(self, x):
		if self.c[x] < 0:
			return x
		self.c[x] = self.find(self.c[x])
		return self.c[x]

	def union(self, u, v):
		(u, v) = (self.find(u), self.find(v))
		if u == v:
			return False
		if self.c[u] < self.c[v]:
			(u, v) = (v, u)
		self.c[u] += self.c[v]
		self.c[v] = u
		return True

	def size(self, x):
		return -self.c[self.find(x)]

class UFS:

	def __init__(self, n):
		self.parent = [i for i in range(n)]
		self.ranks = [0] * n

	def find(self, x):
		if x != self.parent[x]:
			self.parent[x] = self.find(self.parent[x])
		return self.parent[x]

	def union(self, u, v):
		(pu, pv) = (self.find(u), self.find(v))
		if pu == pv:
			return False
		if self.ranks[pu] >= self.ranks[pv]:
			self.parent[pv] = pu
			if self.ranks[pv] == self.ranks[pu]:
				self.ranks[pu] += 1
		else:
			self.parent[pu] = pv

def Prime(n):
	c = 0
	prime = []
	flag = [0] * (n + 1)
	for i in range(2, n + 1):
		if not flag[i]:
			prime.append(i)
			c += 1
		for j in range(c):
			if i * prime[j] > n:
				break
			flag[i * prime[j]] = prime[j]
			if i % prime[j] == 0:
				break
	return prime

def dij(s, graph):
	d = {}
	d[s] = 0
	heap = [(0, s)]
	seen = set()
	while heap:
		(dis, u) = heappop(heap)
		if u in seen:
			continue
		for v in graph[u]:
			if v not in d or d[v] > d[u] + graph[u][v]:
				d[v] = d[u] + graph[u][v]
				heappush(heap, (d[v], v))
	return d

def GP(it):
	return [[ch, len(list(g))] for (ch, g) in groupby(it)]

class DLN:

	def __init__(self, val):
		self.val = val
		self.pre = None
		self.next = None
t = N()
for i in range(t):
	n = N()
	a = RLL()
	i = n - 2
	while i >= 0 and a[i] >= a[i + 1]:
		i -= 1
	if i <= 0:
		ans = n
	else:
		j = i - 1
		while j >= 0 and a[j] <= a[j + 1]:
			j -= 1
		if j < 0:
			ans = n
		else:
			ans = n - j - 1
	print(n - ans)


def ld(l, n):
	i = n - 1
	c = 1
	f = 0
	while i >= 0 and l[i - 1] >= l[i]:
		i = i - 1
		c = c + 1
	while i >= 0 and l[i - 1] <= l[i]:
		i = i - 1
		c = c + 1
	return c
for t in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	if ld(l, n) > n:
		print(0)
	else:
		print(n - ld(l, n))


for _ in range(int(input())):
	n = int(input())
	s = list(map(int, input().split()))
	i = 1
	t = 0
	if n > 2:
		while i != n - 1:
			if s[n - i - 1] < s[n - i - 2] and s[n - i - 1] < s[n - i]:
				break
			if s[n - i - 1] <= s[n - i - 2] and s[n - i - 1] < s[n - i]:
				t = 1
			if s[n - i - 1] < s[n - i - 2] and s[n - i - 1] <= s[n - i] and (t == 1):
				break
			i += 1
	else:
		i = n - 1
	print(n - i - 1)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split(' ')))
	reversed_a = list(reversed(a))
	cur = 1
	good_count = 0
	while cur < n and reversed_a[cur] >= reversed_a[cur - 1]:
		good_count += 1
		cur += 1
	while cur < n and reversed_a[cur] <= reversed_a[cur - 1]:
		good_count += 1
		cur += 1
	print(n - good_count - 1)


for t in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	pos = n - 1
	while True:
		if pos == 0:
			break
		if arr[pos] > arr[pos - 1]:
			break
		pos -= 1
	while True:
		if pos == 0:
			break
		if arr[pos] < arr[pos - 1]:
			break
		pos -= 1
	print(pos)


for _ in range(int(input())):
	n = int(input())
	li = list(map(int, input().split()))
	if len(li) == 1:
		print(0)
	else:
		index1 = -1
		index2 = -1
		for i in range(len(li) - 1, 0, -1):
			if li[i - 1] < li[i]:
				index1 = i
				break
		i -= 1
		while i > 0:
			if li[i - 1] > li[i]:
				index2 = i - 1
				break
			i -= 1
		if index2 == -1 and index1 == -1:
			print(0)
		else:
			print(i)


def jisuan(list1):
	for i in range(n - 1):
		if list1[i] > list1[i + 1]:
			break
	else:
		return n
	for x in range(i + 1, n - 1):
		if list1[x] < list1[x + 1]:
			break
	else:
		return n
	return x + 1
list_ans = []
for _ in range(int(input())):
	n = int(input())
	list1 = list(map(int, input().split()))
	list1.reverse()
	list_ans.append(n - jisuan(list1))
for item in list_ans:
	print(item)


from sys import stdin, stdout
from sys import maxsize
input = stdin.readline

def solve():
	pass
test = 1
test = int(input().strip())
for t in range(0, test):
	n = int(input().strip())
	a = [int(x) for x in input().strip().split()]
	i = n - 1
	while i > 0 and a[i - 1] >= a[i]:
		i -= 1
	while i > 0 and a[i - 1] <= a[i]:
		i -= 1
	print(i)
	ans = solve()


for t in range(int(input())):
	_ = int(input())
	l = list(map(int, input().split()))[::-1]
	i = 1
	m = 1
	while i < len(l):
		if l[i] - l[i - 1] >= 0 and m > 0:
			i += 1
		elif l[i] - l[i - 1] < 0 and m > 0:
			i += 1
			m = -1
		elif l[i] - l[i - 1] <= 0 and m < 0:
			i += 1
		else:
			break
	print(len(l) - i)


q = int(input())
for o in range(q):
	n = int(input())
	a = list(map(int, input().split()))
	pos = n - 1
	while pos > 0 and a[pos] <= a[pos - 1]:
		pos -= 1
	while pos > 0 and a[pos] >= a[pos - 1]:
		pos -= 1
	print(pos)


def solve():
	n = int(input())
	end = n - 1
	l = list(map(int, input().split()))
	while end > 0 and l[end - 1] >= l[end]:
		end -= 1
	while end > 0 and l[end - 1] <= l[end]:
		end -= 1
	print(end)

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


import io
import os
from collections import Counter, defaultdict, deque

def solve(N, A):
	temp = [A.pop()]
	while A and A[-1] >= temp[-1]:
		temp.append(A.pop())
	while A and A[-1] <= temp[-1]:
		temp.append(A.pop())
	return N - len(temp)
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
T = int(input())
for t in range(T):
	(N,) = [int(x) for x in input().split()]
	A = [int(x) for x in input().split()]
	ans = solve(N, A)
	print(ans)


import sys
input = lambda : sys.stdin.readline().strip()
char = [chr(i) for i in range(97, 123)]
CHAR = [chr(i) for i in range(65, 91)]
mp = lambda : list(map(int, input().split()))
INT = lambda : int(input())
rn = lambda : range(INT())
from math import ceil, sqrt, factorial, gcd
for _ in rn():
	n = INT()
	l = mp()
	l = l[::-1]
	flag = 0
	c = 1
	for i in range(1, n):
		if l[i] > l[i - 1]:
			c += 1
			if flag == 1:
				c -= 1
				break
		elif l[i] == l[i - 1]:
			c += 1
		else:
			if flag == 0:
				flag += 1
			c += 1
	print(n - c)


import sys
input = sys.stdin.readline
from collections import deque
t = int(input())
for tests in range(t):
	n = int(input())
	A = list(map(int, input().split()))
	OK = n - 1
	NG = -1
	while OK > NG + 1:
		mid = (OK + NG) // 2
		X = deque(A[mid:])
		MIN = -1
		while X:
			if X[0] <= X[-1] and X[0] >= MIN:
				MIN = X[0]
				X.popleft()
			elif X[0] >= X[-1] and X[-1] >= MIN:
				MIN = X[-1]
				X.pop()
			else:
				break
		if X:
			NG = mid
		else:
			OK = mid
	print(OK)


import bisect as bi
import math as mt
import collections as cc
I = lambda : list(map(int, input().split()))
for tc in range(int(input())):
	(n,) = I()
	l = I()
	i = n - 2
	while i >= 0:
		if l[i] >= l[i + 1]:
			i -= 1
		else:
			break
	while i >= 0:
		if l[i] <= l[i + 1]:
			i -= 1
		else:
			break
	print(i + 1)


def solve():
	n = int(input())
	a = list(map(int, input().split()))[::-1]
	i = 0
	while i < n and (i == 0 or (i > 0 and a[i] >= a[i - 1])):
		i += 1
	while i < n and a[i] <= a[i - 1]:
		i += 1
	print(n - i)
	return
t = int(input())
for _ in range(t):
	solve()


for _ in range(int(input())):
	i = int(input())
	l = list(reversed(list(map(int, input().split()))))
	t = c = 0
	p = l[0]
	for x in l:
		if t == 0:
			if x >= p:
				c += 1
			else:
				t = 1
				c += 1
		elif t == 1:
			if x <= p:
				c += 1
			else:
				break
		p = x
	print(i - c)


from bisect import bisect_left as bl
from bisect import bisect_right as br
from heapq import heappush, heappop, heapify
import math
from collections import *
from functools import reduce, cmp_to_key
import sys
input = sys.stdin.readline
M = mod = 998244353

def factors(n):
	return sorted(set(reduce(list.__add__, ([i, n // i] for i in range(1, int(n ** 0.5) + 1) if n % i == 0))))

def inv_mod(n):
	return pow(n, mod - 2, mod)

def li():
	return [int(i) for i in input().rstrip('\n').split()]

def st():
	return input().rstrip('\n')

def val():
	return int(input().rstrip('\n'))

def li2():
	return [i for i in input().rstrip('\n')]

def li3():
	return [int(i) for i in input().rstrip('\n')]
l = []

def check(m):
	l1 = l[-m:]
	l2 = sorted(l1)
	l1 = deque(l1)
	for i in range(len(l1)):
		if l2[i] == l1[0]:
			l1.popleft()
		elif l2[i] == l1[-1]:
			l1.pop()
		else:
			return 0
	return 1
for _ in range(val()):
	n = val()
	l = li()
	ans = low = 1
	high = n
	while low <= high:
		mid = low + high >> 1
		if check(mid):
			ans = mid
			low = mid + 1
		else:
			high = mid - 1
	print(n - ans)


def main():
	t = int(input())
	for i in range(t):
		n = int(input())
		a = list(map(int, input().split()))
		padanje = False
		promjena = 0
		for j in range(n):
			if not padanje:
				if j + 2 <= n:
					if a[-(j + 2)] >= a[-(j + 1)]:
						pass
					else:
						padanje = True
				else:
					break
			elif j + 2 <= n and a[-(j + 2)] <= a[-(j + 1)]:
				pass
			else:
				promjena = n - j - 1
				break
		print(promjena)
main()


t = int(input())
for _ in range(t):
	n = int(input())
	lst = []
	lst = list(map(int, input().split()))
	pos = n - 1
	while pos > 0 and lst[pos - 1] >= lst[pos]:
		pos -= 1
	while pos > 0 and lst[pos - 1] <= lst[pos]:
		pos -= 1
	print(pos)


from collections import *
import sys

def ri():
	return int(input())

def rl():
	return list(map(int, input().split()))
t = ri()
for _ in range(t):
	n = ri()
	a = rl()
	ans = n - 1
	compare = 0
	j = n - 1
	while a[j - 1] >= a[j] and j - 1 >= 0:
		ans -= 1
		j -= 1
	while a[j - 1] <= a[j] and j - 1 >= 0:
		ans -= 1
		j -= 1
	print(ans)


for _ in range(int(input())):
	n = int(input())
	lst = list(map(int, input().split()))
	if sorted(lst) == lst or sorted(lst) == lst[::-1]:
		print(0)
	else:
		for i in range(len(lst) - 1, 0, -1):
			if lst[i - 1] < lst[i]:
				break
		for j in range(i, -1, -1):
			if lst[j - 1] > lst[j]:
				break
		print(j)


GI = lambda : int(input())
GIS = lambda : map(int, input().split())
LGIS = lambda : list(GIS())

def main():
	for t in range(GI()):
		n = GI()
		l = LGIS()
		last = None
		went_down = False
		for i in range(n - 2, -1, -1):
			if l[i] < l[i + 1]:
				went_down = True
			elif l[i] > l[i + 1]:
				if went_down:
					break
		else:
			print(0)
			continue
		print(i + 1)
main()


def check(a, start):
	newArray = []
	(l, r) = (start, len(a) - 1)
	while l <= r:
		minVal = min(a[l], a[r])
		if len(newArray) > 0 and minVal < newArray[-1]:
			return False
		newArray.append(minVal)
		if minVal == a[l]:
			l += 1
		else:
			r -= 1
	return True
t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(x) for x in input().split()]
	(l, r) = (0, n - 1)
	result = n - 1
	while l <= r:
		mid = (l + r) // 2
		if check(a, mid):
			result = mid
			r = mid - 1
		else:
			l = mid + 1
	print(result)


q = int(input())
for _ in range(q):
	n = int(input())
	arr = list(map(int, input().split(' ')))
	if len(arr) <= 2:
		print(0)
		continue
	if arr[-1] > arr[-2]:
		decreasing = True
	else:
		decreasing = False
	if decreasing:
		count = 1
		pos = n - 1
		while pos > 0 and arr[pos] >= arr[pos - 1]:
			count += 1
			pos -= 1
		print(n - count)
	else:
		count = 1
		pos = n - 1
		while pos > 0 and arr[pos] <= arr[pos - 1]:
			count += 1
			pos -= 1
		while pos > 0 and arr[pos] >= arr[pos - 1]:
			count += 1
			pos -= 1
		print(n - count)


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


t = int(input())
for _ in range(t):
	n = int(input())
	seq = [int(x) for x in input().strip().split()]
	while n >= 2:
		if seq[n - 1] <= seq[n - 2]:
			n -= 1
		else:
			break
	while n >= 2:
		if seq[n - 1] >= seq[n - 2]:
			n -= 1
		else:
			break
	print(n - 1)


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


import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(item) for item in input().split()]
	a.reverse()
	prev = 0
	state = 0
	ok = 0
	for item in a:
		if state == 0 and item >= prev:
			ok += 1
		elif state == 0 and item < prev:
			state = 1
			ok += 1
		elif state == 1 and item <= prev:
			ok += 1
		elif state == 1 and item > prev:
			break
		prev = item
	print(n - ok)


from math import *
tests = int(input())
for t in range(tests):
	n = int(input())
	a = list(map(int, input().split()))
	len = 0
	prev = 0
	for i in range(n - 1, -1, -1):
		if a[i] < prev:
			break
		len += 1
		prev = a[i]
	if a[i] < prev and i >= 0:
		len += 1
		prev = a[i]
		for j in range(i - 1, -1, -1):
			if a[j] > prev:
				break
			len += 1
			prev = a[j]
	print(n - len)


test = int(input())
for t in range(test):
	leng = int(input())
	arr = input().split(' ')
	arr = [int(n) for n in arr]
	if leng < 3:
		print(0)
		continue
	declining = False
	found = False
	for n in reversed(range(leng - 1)):
		if arr[n + 1] > arr[n] and (not declining):
			declining = True
		elif arr[n + 1] < arr[n] and declining:
			print(n + 1)
			found = True
			break
	if not found:
		print(0)


t = int(input())
for i in range(t):
	n = int(input())
	arr = list(map(int, input().split(' ')))
	if n <= 2:
		print('0')
		continue
	diff = []
	for j in range(1, n):
		diff.append(arr[j] - arr[j - 1])
	default_ans = False
	itr = len(diff) - 1
	while diff[itr] <= 0:
		itr -= 1
		if itr < 0:
			default_ans = True
			break
	if default_ans:
		print('0')
		continue
	for j in range(itr, -1, -1):
		if diff[j] >= 0:
			continue
		else:
			break
	if diff[j] < 0:
		print(j + 1)
	else:
		print('0')


t = int(input())
for i in range(t):
	length = int(input())
	arr = list(map(int, input().split()))
	flag = True
	printflag = False
	start = arr[-1]
	for i in range(length - 1, -1, -1):
		if flag:
			if arr[i] < start:
				flag = False
				start = arr[i]
			else:
				start = arr[i]
		elif arr[i] > start:
			print(i + 1)
			printflag = True
			break
		else:
			start = arr[i]
	if printflag == False:
		print(0)


import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	prev = a[n - 1]
	idx = n - 1
	flag = False
	for i in range(n - 2, -1, -1):
		if a[i] > prev:
			if flag:
				break
			else:
				idx = i
		elif a[i] == prev:
			idx = i
		elif a[i] < prev:
			flag = True
			idx = i
		prev = a[i]
	print(idx)


t = int(input())
for i in range(t):
	n = int(input())
	b = list(map(int, input().split()))
	j = n - 2
	f = 0
	ans = 0
	while j >= 0:
		if f == 0:
			if b[j] >= b[j + 1]:
				pass
			else:
				f = 1
				max = b[j + 1]
		if f == 1:
			if b[j] > b[j + 1]:
				ans = j + 1
				break
		j += -1
	print(ans)


test_number = int(input())
for test in range(test_number):
	array_length = int(input())
	str_array = input().split()
	if array_length == 1:
		print(0)
	else:
		array = list(map(int, str_array))
		begin = array[-1]
		decreasing = 0
		mark = -1
		for count in range(array_length - 2, -1, -1):
			if array[count] > begin:
				if decreasing == 1:
					mark = count
					break
				else:
					begin = array[count]
			elif array[count] < begin:
				decreasing = 1
				begin = array[count]
			else:
				begin = array[count]
		print(mark + 1)


for i in range(int(input())):
	l = int(input())
	nums = list(map(int, input().split()))
	n = l - 1
	while n > 0 and nums[n] <= nums[n - 1]:
		n -= 1
	while n > 0 and nums[n] >= nums[n - 1]:
		n -= 1
	print(n)


t = int(input())
for _ in range(t):
	n = int(input())
	nums = list(map(int, input().split()))
	dec = [0 for i in range(n)]
	dec[-1] = 0
	for i in reversed(range(n - 1)):
		if nums[i] >= nums[i + 1]:
			dec[i] = dec[i + 1] + 1
		else:
			dec[i] = 0
	inc = [0 for i in range(n)]
	inc[0] = 0
	for i in range(1, n):
		if nums[i] >= nums[i - 1]:
			inc[i] = inc[i - 1] + 1
		else:
			inc[i] = 0
	i = n - 1
	while i > -1 and dec[i] < dec[i - 1]:
		i -= 1
	while i > -1 and inc[i] > inc[i - 1]:
		i -= 1
	print(i)


from sys import stdin, stdout
import functools
import sys, os, math
T = int(input())
for _ in range(T):
	N = int(input())
	A = list(map(int, input().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 go(arr):
	arr.reverse()
	prev = arr[0]
	for i in range(len(arr)):
		if arr[i] < prev:
			return i
		prev = arr[i]
	return len(arr)
n = int(input())
for i in range(n):
	a = []
	m = int(input())
	arr = list(map(int, input().split(' ')))
	r = go(arr)
	arr.reverse()
	prev = arr[0]
	idx = 0
	for i in range(m - r):
		if arr[i] < prev:
			idx = i
		prev = arr[i]
	print(idx)


a = int(input())
for x in range(a):
	b = int(input())
	c = list(map(int, input().split()))
	m = 0
	n = 0
	o = 0
	for y in range(b - 1, 0, -1):
		if m == 0:
			if c[y] <= c[y - 1]:
				o += 1
			else:
				m = 1
				o += 1
		elif m == 1:
			if c[y] >= c[y - 1]:
				o += 1
			else:
				m = -1
		else:
			break
	print(b - (o + 1))


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	if arr == sorted(arr):
		print('0')
	else:
		f = 0
		for i in range(len(arr) - 1, -1, -1):
			if f == 2:
				break
			if f == 0:
				if arr[i - 1] < arr[i]:
					f += 1
			elif arr[i - 1] > arr[i]:
				f += 1
				break
		print(i)


import sys
input = sys.stdin.readline
for _ in range(int(input())):
	n = int(input())
	A = list(map(int, input().split()))
	lo = 0
	hi = n
	while lo < hi:
		m = (lo + hi) // 2
		i = m
		j = n - 1
		prev = 0
		while i <= j:
			if min(A[i], A[j]) < prev:
				lo = m + 1
				break
			prev = min(A[i], A[j])
			if A[i] == prev:
				i += 1
			else:
				j -= 1
		else:
			hi = m
	print(lo)


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


from sys import stdin, stdout
import sys
INF = 100000000000.0
import bisect

def get_int():
	return int(stdin.readline().strip())

def get_ints():
	return map(int, stdin.readline().strip().split())

def get_array():
	return list(map(int, stdin.readline().strip().split()))

def get_string():
	return stdin.readline().strip()

def op(c):
	return stdout.write(c)
from collections import defaultdict
import math
for t in range(int(stdin.readline())):
	n = get_int()
	a = get_array()
	if sorted(a) == a:
		print(0)
		continue
	if sorted(a, reverse=True) == a:
		print(0)
		continue
	ans1 = INF
	ans2 = INF
	for i in range(n - 2, -1, -1):
		if a[i] > a[i + 1]:
			ans1 = i + 1
			break
	ind = n - 2
	while ind > -1 and a[ind] >= a[ind + 1]:
		ind -= 1
	while ind > -1 and a[ind] <= a[ind + 1]:
		ind -= 1
	ans2 = ind + 1
	print(min(ans1, ans2))


for i in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	(flag, flag2) = (0, 0)
	for i in range(n - 1, 0, -1):
		if a[i] < a[i - 1]:
			if flag2 == 1:
				flag = 1
				break
		elif a[i] == a[i - 1]:
			pass
		else:
			flag2 = 1
	if flag:
		print(i)
	else:
		print(0)


for _ in range(int(input())):
	k = int(input())
	a = list(map(int, input().split()))
	n = k
	f = 0
	for j in range(k - 1, 0, -1):
		if a[j - 1] != a[j]:
			f = 1
			break
	if f == 1:
		n = j + 1
		if a[n - 1] >= a[n - 2]:
			f = 0
			for i in range(n - 1, 0, -1):
				if a[i - 1] > a[i]:
					f = 1
					break
			if f == 1:
				print(i)
			else:
				print(0)
		else:
			f = 0
			i = n - 1
			while i > 0:
				if a[i - 1] < a[i]:
					while i > 0:
						if a[i - 1] > a[i]:
							f = 1
							break
						i -= 1
					break
				i -= 1
			if f == 1:
				print(i)
			else:
				print(0)
	else:
		print(0)


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)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	ind = n - 1
	while ind > 0 and a[ind] <= a[ind - 1]:
		ind -= 1
	if ind == n - 1:
		while ind > 0 and a[ind] >= a[ind - 1]:
			ind -= 1
	else:
		while ind > 0 and a[ind] >= a[ind - 1]:
			ind -= 1
	print(ind)


from collections import defaultdict as dd
import sys
input = sys.stdin.readline
t = int(input())
while t:
	n = int(input())
	l = list(map(int, input().split()))
	pre = [-1]
	mx = l[0]
	for i in range(1, n):
		if l[i] >= mx:
			pre.append(-1)
			mx = l[i]
		else:
			pre.append(mx)
	pos = [-1] * n
	mx = l[-1]
	for i in range(n - 2, -1, -1):
		if l[i] >= mx:
			pos[i] = -1
			mx = l[i]
		else:
			pos[i] = mx
	lol = 0
	mx = l[0]
	for i in range(1, n):
		if l[i] < mx and l[i] < pos[i]:
			ans = i
			lol = i
			mx = l[i]
		if l[i] > mx:
			mx = l[i]
	if lol == 0:
		print(0)
	else:
		print(ans)
	t -= 1


t = int(input())
for i in range(0, t):
	n = int(input())
	L = list(map(int, input().split()))
	M = sorted(L)
	if L == M:
		print(0)
	elif L == M.reverse():
		print(0)
	else:
		c = 0
		d = 0
		j = 0
		for i in range(n - 1, 0, -1):
			if L[i] <= L[i - 1]:
				c += 1
			else:
				j = i
				break
		for p in range(j, 0, -1):
			if L[p] >= L[p - 1]:
				d += 1
			else:
				break
		print(j - d)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	i = n - 1
	l1 = 0
	while i > 0 and a[i] <= a[i - 1]:
		l1 += 1
		i = i - 1
	while i > 0 and a[i] >= a[i - 1]:
		l1 += 1
		i = i - 1
	l1 += 1
	i = n - 1
	l2 = 0
	l = max(l1, l2)
	print(n - l)


from collections import defaultdict, Counter
from math import log, ceil, floor

def solve():
	n = int(input())
	a = list(map(int, input().split()))
	a = [float('inf')] + a
	for i in range(n, -1, -1):
		if a[i - 1] < a[i]:
			for j in range(i - 1, -1, -1):
				if a[j - 1] > a[j]:
					print(j - 1)
					return
	print(0)
	return
t = 1
t = int(input())
for _ in range(t):
	solve()


import math

def solve():
	n = int(input())
	l = list(map(int, input().split()))
	pf = [1] * n
	sf = [1] * n
	for i in range(n - 2, -1, -1):
		if l[i] >= l[i + 1]:
			sf[i] += sf[i + 1]
	for i in range(1, n):
		if l[i] >= l[i - 1]:
			pf[i] += pf[i - 1]
	ans = n - 1
	for i in range(n):
		if sf[i] == n - i:
			ans = min(ans, n - (sf[i] + pf[i] - 1))
	print(ans)
t = 1
t = int(input())
while t > 0:
	t -= 1
	solve()


import sys
import math
import collections
import heapq
import queue
import itertools
import functools
import operator
import time
readline = sys.stdin.readline
IPS = lambda : readline().rstrip()
IP = lambda : int(readline().rstrip())
MP = lambda : map(int, readline().split())
LS = lambda : list(map(int, readline().split()))

def solve():
	for _ in range(IP()):
		n = IP()
		arr = LS()
		(fg, ans) = (0, -1)
		for i in range(n - 1, 0, -1):
			if fg:
				if arr[i - 1] > arr[i]:
					ans = i
					break
			elif arr[i - 1] < arr[i]:
				fg = 1
		if ans == -1:
			print(0)
		else:
			print(ans)
solve()


def main():
	n = int(input())
	arr = list(map(int, input().split()))
	for i in range(n - 2, 0, -1):
		c = 1
		while i - c >= 0 and arr[i] == arr[i - c]:
			c += 1
		if arr[i - c] > arr[i] and arr[i + 1] > arr[i]:
			print(i - c + 1)
			return
	print(0)
for _ in range(int(input())):
	main()


t = int(input())
while t:
	t = t - 1
	n = int(input())
	a = list(map(int, input().split()))
	if len(set(a)) == 1:
		print(0)
	else:
		(flag, flag2) = (False, True)
		for i in reversed(range(n)):
			if a[i] < a[i - 1] and flag:
				print(i)
				flag2 = False
				break
			elif a[i] > a[i - 1]:
				flag = True
		if flag2:
			print(0)


import sys
t = int(input())
for _ in range(t):
	n = int(input())
	arr = [int(x) for x in input().split()]
	pivot = {}
	for i in range(n):
		if arr[i] not in pivot:
			pivot[arr[i]] = [i]
		else:
			pivot[arr[i]].append(i)
	temp = sorted(pivot.keys())
	ptr1 = 0
	ptr2 = n - 1
	ans = 0
	for i in temp:
		lis = pivot[i]
		while True:
			if ptr2 in lis:
				lis.remove(ptr2)
				ptr2 -= 1
				continue
			if ptr1 in lis:
				lis.remove(ptr1)
				ptr1 += 1
				continue
			if len(lis) == 0:
				break
			m = -1
			for k in lis:
				if k + 1 > ptr1:
					m = k
					break
			if m == -1:
				break
			if m + 1 > ptr1:
				ptr1 = m + 1
				ans = m
	print(ans)


from functools import cmp_to_key
from collections import deque
from heapq import heappush, heappop
from math import log, ceil
import sys
import os
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()

def inp():
	return sys.stdin.readline().rstrip('\r\n')

def ii():
	return int(inp())

def li(lag=0):
	l = list(map(int, inp().split()))
	if lag != 0:
		for i in range(len(l)):
			l[i] += lag
	return l

def mi(lag=0):
	matrix = list()
	for i in range(n):
		matrix.append(li(lag))
	return matrix

def sli():
	return list(map(str, inp().split()))

def print_list(lista, space=' '):
	print(space.join(map(str, lista)))

class UnionFind:

	def __init__(self, n):
		self.parent = list(range(n))
		self.size = [1] * n
		self.num_sets = n

	def find(self, a):
		to_update = []
		while a != self.parent[a]:
			to_update.append(a)
			a = self.parent[a]
		for b in to_update:
			self.parent[b] = a
		return self.parent[a]

	def merge(self, a, b):
		a = self.find(a)
		b = self.find(b)
		if a == b:
			return
		if self.size[a] < self.size[b]:
			(a, b) = (b, a)
		self.num_sets -= 1
		self.parent[b] = a
		self.size[a] += self.size[b]

	def set_size(self, a):
		return self.size[self.find(a)]

	def __len__(self):
		return self.num_sets

def bisect_left(a, x):
	left = 0
	right = len(a)
	while left < right:
		mid = (left + right) // 2
		if a[mid] < x:
			left = mid + 1
		else:
			right = mid
	return left

def bisect_right(a, x):
	left = 0
	right = len(a)
	while left < right:
		mid = (left + right) // 2
		if a[mid] > x:
			right = mid
		else:
			left = mid + 1
	return left

def bisect_elements(a, x):
	return bisect_right(a, x) - bisect_left(a, x)

def custom_sort(lista):

	def cmp(x, y):
		if x + y > y + x:
			return 1
		else:
			return -1
	return sorted(lista, key=cmp_to_key(cmp))
MOD = 10 ** 9 + 7
maxN = 10 ** 5
FACT = [0] * maxN

def add(x, y):
	return (x + y) % MOD

def multiply(x, y):
	return x * y % MOD

def power(x, y):
	if y == 0:
		return 1
	elif y % 2:
		return multiply(x, power(x, y - 1))
	else:
		a = power(x, y // 2)
		return multiply(a, a)

def inverse(x):
	return power(x, MOD - 2)

def divide(x, y):
	return multiply(x, inverse(y))

def allFactorials():
	FACT[0] = 1
	for i in range(1, maxN):
		FACT[i] = multiply(i, FACT[i - 1])

def coeffBinom(n, k):
	if n < k:
		return 0
	return divide(FACT[n], multiply(FACT[k], FACT[n - k]))

def primes(N):
	smallest_prime = [1] * (N + 1)
	prime = []
	smallest_prime[0] = 0
	smallest_prime[1] = 0
	for i in range(2, N + 1):
		if smallest_prime[i] == 1:
			prime.append(i)
			smallest_prime[i] = i
		j = 0
		while j < len(prime) and i * prime[j] <= N:
			smallest_prime[i * prime[j]] = min(prime[j], smallest_prime[i])
			j += 1
	return (prime, smallest_prime)

def gcd(a, b):
	a = abs(a)
	b = abs(b)
	(s, t, r) = (0, 1, b)
	(old_s, old_t, old_r) = (1, 0, a)
	while r != 0:
		quotient = old_r // r
		(old_r, r) = (r, old_r - quotient * r)
		(old_s, s) = (s, old_s - quotient * s)
		(old_t, t) = (t, old_t - quotient * t)
	return (old_r, old_s, old_t)

def create_graph(n, m, undirected=1, unweighted=1):
	graph = [[] for i in range(n)]
	if unweighted:
		for i in range(m):
			[x, y] = li(lag=-1)
			graph[x].append(y)
			if undirected:
				graph[y].append(x)
	else:
		for i in range(m):
			[x, y, w] = li(lag=-1)
			w += 1
			graph[x].append([y, w])
			if undirected:
				graph[y].append([x, w])
	return graph

def create_tree(n, unweighted=1):
	children = [[] for i in range(n)]
	if unweighted:
		for i in range(n - 1):
			[x, y] = li(lag=-1)
			children[x].append(y)
			children[y].append(x)
	else:
		for i in range(n - 1):
			[x, y, w] = li(lag=-1)
			w += 1
			children[x].append([y, w])
			children[y].append([x, w])
	return children

def create_edges(m, unweighted=0):
	edges = list()
	if unweighted:
		for i in range(m):
			edges.append(li(lag=-1))
	else:
		for i in range(m):
			[x, y, w] = li(lag=-1)
			w += 1
			edges.append([w, x, y])
	return edges

def dist(tree, n, A, B=-1):
	s = [[A, 0]]
	(massimo, massimo_nodo) = (0, 0)
	distanza = -1
	v = [-1] * n
	while s:
		(el, dis) = s.pop()
		if dis > massimo:
			massimo = dis
			massimo_nodo = el
		if el == B:
			distanza = dis
		for child in tree[el]:
			if v[child] == -1:
				v[child] = 1
				s.append([child, dis + 1])
	return (massimo, massimo_nodo, distanza)

def diameter(tree):
	(_, foglia, _) = dist(tree, n, 0)
	(diam, _, _) = dist(tree, n, foglia)
	return diam

def dfs(graph, n, A):
	v = [-1] * n
	s = [[A, 0]]
	v[A] = 0
	while s:
		(el, dis) = s.pop()
		for child in graph[el]:
			if v[child] == -1:
				v[child] = dis + 1
				s.append([child, dis + 1])
	return v

def bfs(graph, n, A):
	v = [-1] * n
	s = deque()
	s.append([A, 0])
	v[A] = 0
	while s:
		(el, dis) = s.popleft()
		for child in graph[el]:
			if v[child] == -1:
				v[child] = dis + 1
				s.append([child, dis + 1])
	return v

def connected(graph, n):
	v = dfs(graph, n, 0)
	for el in v:
		if el == -1:
			return False
	return True

def topological(graph, n):
	indegree = [0] * n
	for el in range(n):
		for child in graph[el]:
			indegree[child] += 1
	s = deque()
	for el in range(n):
		if indegree[el] == 0:
			s.append(el)
	order = []
	while s:
		el = s.popleft()
		order.append(el)
		for child in graph[el]:
			indegree[child] -= 1
			if indegree[child] == 0:
				s.append(child)
	if n == len(order):
		return (False, order)
	else:
		return (True, [])

def bipartite(graph, n):
	color = [-1] * n
	color[0] = 0
	s = [0]
	while s:
		el = s.pop()
		for child in graph[el]:
			if color[child] == color[el]:
				return False
			if color[child] == -1:
				s.append(child)
			color[child] = 1 - color[el]
	return True

def dijkstra(graph, n, A):
	dist = [float('inf') for i in range(n)]
	prev = [-1 for i in range(n)]
	dist[A] = 0
	pq = []
	heappush(pq, [0, A])
	while pq:
		[d_v, v] = heappop(pq)
		if d_v != dist[v]:
			continue
		for (to, w) in graph[v]:
			if dist[v] + w < dist[to]:
				dist[to] = dist[v] + w
				prev[to] = v
				heappush(pq, [dist[to], to])
	return (dist, prev)

def dijkstra_0_1(graph, n, A):
	dist = [float('inf') for i in range(n)]
	dist[A] = 0
	p = deque()
	p.append(A)
	while p:
		v = p.popleft()
		for (to, w) in graph[v]:
			if dist[v] + w < dist[to]:
				dist[to] = dist[v] + w
			if w == 1:
				q.append(to)
			else:
				q.appendleft(to)
	return dist

def floyd_warshall(graph, n):
	dist = [[float('inf') for _ in range(n)] for _ in range(n)]
	for i in range(n):
		dist[i][i] = 0
		for (child, d) in graph[i]:
			dist[i][child] = d
			dist[child][i] = d
	for k in range(n):
		for i in range(n):
			for j in range(j):
				dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j])
	return dist

def minimum_spanning_tree(edges, n):
	edges = sorted(edges)
	union_find = UnionFind(n)
	used_edges = list()
	for (w, x, y) in edges:
		if union_find.find(x) != union_find.find(y):
			union_find.merge(x, y)
			used_edges.append([w, x, y])
	return used_edges

def parents_children_root_unrooted_tree(tree, n, root=0):
	q = deque()
	visited = [0] * n
	parent = [-1] * n
	children = [[] for i in range(n)]
	q.append(root)
	while q:
		all_done = 1
		visited[q[0]] = 1
		for child in tree[q[0]]:
			if not visited[child]:
				all_done = 0
				q.appendleft(child)
		if all_done:
			for child in tree[q[0]]:
				if parent[child] == -1:
					parent[q[0]] = child
					children[child].append(q[0])
			q.popleft()
	return (parent, children)

def all_longest_path_passing_from_node(parent, children, n):
	q = deque()
	visited = [len(children[i]) for i in range(n)]
	downwards = [[0, 0] for i in range(n)]
	upward = [1] * n
	longest_path = [1] * n
	for i in range(n):
		if not visited[i]:
			q.append(i)
			downwards[i] = [1, 0]
	while q:
		node = q.popleft()
		if parent[node] != -1:
			visited[parent[node]] -= 1
			if not visited[parent[node]]:
				q.append(parent[node])
		else:
			root = node
		for child in children[node]:
			downwards[node] = sorted([downwards[node][0], downwards[node][1], downwards[child][0] + 1], reverse=True)[0:2]
	s = [node]
	while s:
		node = s.pop()
		if parent[node] != -1:
			if downwards[parent[node]][0] == downwards[node][0] + 1:
				upward[node] = 1 + max(upward[parent[node]], downwards[parent[node]][1])
			else:
				upward[node] = 1 + max(upward[parent[node]], downwards[parent[node]][0])
		longest_path[node] = downwards[node][0] + downwards[node][1] + upward[node] - min([downwards[node][0], downwards[node][1], upward[node]]) - 1
		for child in children[node]:
			s.append(child)
	return longest_path

def finding_ancestors(parent, queries, n):
	steps = int(ceil(log(n, 2)))
	ancestors = [[-1 for i in range(n)] for j in range(steps)]
	ancestors[0] = parent
	for i in range(1, steps):
		for node in range(n):
			if ancestors[i - 1][node] != -1:
				ancestors[i][node] = ancestors[i - 1][ancestors[i - 1][node]]
	result = []
	for (node, k) in queries:
		ans = node
		if k >= n:
			ans = -1
		i = 0
		while k > 0 and ans != -1:
			if k % 2:
				ans = ancestors[i][ans]
			k = k // 2
			i += 1
		result.append(ans)
	return result

def prefix_sum(arr):
	r = [0] * (len(arr) + 1)
	for (i, el) in enumerate(arr):
		r[i + 1] = r[i] + el
	return r

def nearest_from_the_left_smaller_elements(arr):
	n = len(arr)
	res = [-1] * n
	s = []
	for (i, el) in enumerate(arr):
		while s and s[-1] >= el:
			s.pop()
		if s:
			res[i] = s[-1]
		s.append(el)
	return res

def sliding_window_minimum(arr, k):
	res = []
	q = deque()
	for (i, el) in enumerate(arr):
		while q and arr[q[-1]] >= el:
			q.pop()
		q.append(i)
		while q and q[0] <= i - k:
			q.popleft()
		if i >= k - 1:
			res.append(arr[q[0]])
	return res
for t in range(ii()):
	n = ii()
	l = li()
	i = n - 2
	while i >= 0 and l[i] >= l[i + 1]:
		i -= 1
	while i >= 0 and l[i] <= l[i + 1]:
		i -= 1
	print(i + 1)


t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(i) for i in input().split()]
	res = 0
	i = len(a) - 2
	flag = 0
	while i >= 0:
		if flag == 0 and a[i] >= a[i + 1]:
			res += 1
		elif flag == 1 and a[i] <= a[i + 1]:
			res += 1
		else:
			flag += 1
		if flag == 2:
			break
		i -= 1
	print(i + 1)


t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	if n <= 2:
		print(0)
	else:
		diffs = []
		for i in range(1, n):
			if l[i] > l[i - 1]:
				diffs.append(1)
			elif l[i] == l[i - 1]:
				diffs.append(0)
			else:
				diffs.append(-1)
		i = len(diffs) - 1
		while diffs[i] != 1 and i >= 0:
			i -= 1
		while diffs[i] != -1 and i >= 0:
			i -= 1
		print(i + 1)


for i in range(int(input())):
	n = int(input())
	a = [int(x) for x in input().split()]
	c = None
	d = None
	if n == 1:
		c = 0
		d = 0
	for j in range(n - 1, 0, -1):
		if a[j] <= a[j - 1]:
			c = j - 1
		else:
			break
	if c == None:
		k = n - 1
	else:
		k = c
	for j in range(k, 0, -1):
		if a[j] >= a[j - 1]:
			d = j - 1
		else:
			break
	if d == None:
		print(0)
	elif c == None and d != None:
		print(d)
	else:
		print(d)


from sys import stdin

def main():
	l = stdin.read().splitlines()[2::2]
	for (i, s) in enumerate(l):
		xx = list(map(int, s.split()))
		le = len(xx) - 1
		if le < 2:
			l[i] = '0'
			continue
		while le:
			if xx[le - 1] < xx[le]:
				break
			le -= 1
		while le:
			if xx[le - 1] > xx[le]:
				break
			le -= 1
		l[i] = str(le)
	print('\n'.join(l))
main()


def test_case():
	n = int(input())
	a = [int(x) for x in input().strip().split(' ')]
	pos = n - 1
	while pos > 0 and a[pos - 1] >= a[pos]:
		pos -= 1
	while pos > 0 and a[pos - 1] <= a[pos]:
		pos -= 1
	print(pos)

def main():
	t = int(input())
	for x in range(t):
		test_case()
main()


def main(n, lst):
	ind1 = -1
	for i in range(n - 1, 0, -1):
		if lst[i] > lst[i - 1]:
			ind1 = i
			break
	ind2 = 0
	for i in range(ind1, 0, -1):
		if lst[i] < lst[i - 1]:
			ind2 = i
			break
	return ind2
for _ in range(int(input())):
	n = int(input())
	lst = list(map(int, input().split()))
	print(main(n, lst))


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	cn = 0
	cp = 0
	ans = 0
	counp = 0
	counn = 0
	for i in range(n - 1, 0, -1):
		if a[i] > a[i - 1]:
			cp = 1
		if a[i] < a[i - 1]:
			if cp == 1:
				ans = i
				break
			cn = 1
	print(ans)


z = input
mod = 10 ** 9 + 7
from collections import *
from queue import *
from sys import *
from collections import *
from math import *
from heapq import *
from itertools import *
from bisect import *
from collections import Counter as cc
from math import factorial as f

def lcd(xnum1, xnum2):
	return xnum1 * xnum2 // gcd(xnum1, xnum2)
for _ in range(int(z())):
	n = int(z())
	l = list(map(int, z().split()))[::-1]
	c = 0
	x = l[0]
	for i in range(n):
		if x <= l[i]:
			c += 1
			x = l[i]
		else:
			break
	x = l[i]
	if c >= n:
		print(0)
		continue
	for j in range(i, n):
		if x >= l[j]:
			c += 1
			x = l[j]
		else:
			break
	print(n - c)


t = int(input())
while t > 0:
	n = input()
	arr = list(map(int, input().split(' ')))
	if n == '0':
		print(0)
	pre = arr[0]
	last_idx = 0
	flag = False
	for i in range(len(arr) - 1):
		if arr[i] > arr[i + 1]:
			flag = True
		if flag and arr[i] < arr[i + 1]:
			last_idx = i
			temp = i
			while temp > 0 and arr[temp] == arr[temp - 1]:
				last_idx = temp - 1
				temp -= 1
			flag = False
	print(last_idx)
	t -= 1


def solve(n, a):
	if n == 1:
		return 0
	i = n - 1
	cnt = False
	while i > 0:
		if a[i - 1] < a[i]:
			cnt = True
		if cnt and a[i - 1] > a[i]:
			break
		i -= 1
	return i
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	print(solve(n, a))


import sys
input = sys.stdin.readline
I = lambda : list(map(int, input().split()))
(t,) = I()
for _ in range(t):
	(n,) = I()
	l = I()
	l += [0]
	an = 0
	c = 0
	for i in range(n - 1, -1, -1):
		if l[i] > l[i + 1]:
			if c:
				break
		elif l[i] < l[i + 1]:
			c = 1
		an += 1
	print(n - an)


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	ans = n - 1
	while ans and a[ans] <= a[ans - 1]:
		ans -= 1
	while ans and a[ans] >= a[ans - 1]:
		ans -= 1
	print(ans)


from sys import stdin, setrecursionlimit, stdout
from collections import deque
from math import sqrt, floor, ceil, log, log2, log10, pi, gcd, sin, cos, asin
from heapq import heapify, heappop, heappush, heapreplace, heappushpop

def ii():
	return int(stdin.readline())

def fi():
	return float(stdin.readline())

def mi():
	return map(int, stdin.readline().split())

def fmi():
	return map(float, stdin.readline().split())

def li():
	return list(mi())

def si():
	return stdin.readline().rstrip()

def lsi():
	return list(si())
mod = 1000000007
res = ['YES', 'NO']
test = 1
test_case = ii()
while test <= test_case:
	test += 1
	n = ii()
	a = li()
	x = n - 1
	while x > 0:
		if a[x] > a[x - 1]:
			break
		x -= 1
	while x > 0:
		if a[x] < a[x - 1]:
			break
		x -= 1
	print(x)


for i in range(int(input())):
	n = int(input())
	x = list(map(int, input().split()))
	pos = len(x) - 1
	while pos > 0 and x[pos - 1] >= x[pos]:
		pos = pos - 1
	while pos > 0 and x[pos - 1] <= x[pos]:
		pos = pos - 1
	print(pos)


def solve(n, nums):
	if n == 1:
		return 0
	pos = n - 1
	while nums[pos] <= nums[pos - 1] and pos > 0:
		pos -= 1
	while nums[pos] >= nums[pos - 1] and pos > 0:
		pos -= 1
	return pos
T = int(input())
for t in range(T):
	n = int(input())
	nums = list(map(int, input().split()))
	print(solve(n, nums))


import io
import os
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline

def solve():
	n = int(input())
	a = list(map(int, input().split()))
	temp = 0
	i = 0
	j = n - 1
	l = 0
	while i < j:
		if a[i] < a[j]:
			if a[i] < temp:
				temp = 0
				j = n - 1
				i -= 1
				l = 0
			else:
				temp = a[i]
				l += 1
			i += 1
		else:
			if a[j] < temp:
				temp = 0
				i += 1
				j = n
				l = 0
			else:
				temp = a[j]
				l += 1
			j -= 1
	l += 1
	print(n - l)

def main():
	for t in range(int(input())):
		solve()
main()


t = 1
t = int(input())
while t:
	t -= 1
	n = int(input())
	a = list(map(int, input().split()))
	increasing = True
	i = n - 2
	while i >= 0:
		if increasing:
			if a[i] >= a[i + 1]:
				i -= 1
			else:
				i -= 1
				increasing = False
		elif a[i] <= a[i + 1]:
			i -= 1
		else:
			break
	print(i + 1)


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	if sorted(l) == l:
		print(0)
		continue
	for i in range(n - 2, -1, -1):
		if l[i] < l[i + 1]:
			break
	for j in range(i, -1, -1):
		if l[j] < l[j - 1]:
			break
	if j == -1:
		print(0)
	else:
		print(j)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	x = 0
	while a and a[-1] >= x:
		x = a.pop()
	while a and a[-1] <= x:
		x = a.pop()
	print(len(a))


from sys import stdin, stdout
t = int(stdin.readline())
for q in range(t):
	n = int(stdin.readline())
	a = [int(x) for x in stdin.readline().split()]
	pos = 0
	ans = -1
	for i in range(1, len(a)):
		if a[i] > a[i - 1]:
			pos = i
	for i in range(pos, 0, -1):
		if a[i] < a[i - 1]:
			ans = i - 1
			break
	print(ans + 1)


T = int(input())

def checkD(arr):
	for i in range(0, len(arr) - 1):
		if arr[i] < arr[i + 1]:
			return False
	return True

def checkI(arr):
	for i in range(0, len(arr) - 1):
		if arr[i] > arr[i + 1]:
			return False
	return True

def hasMin(arr):
	d = []
	flag = 0
	for i in range(0, len(arr) - 1):
		d.append(arr[i + 1] - arr[i])
	for i in range(0, len(d)):
		if d[i] < 0:
			flag = 1
		if flag == 1 and d[i] > 0:
			return True
	return False

def solve2(arr):
	fl = 0
	for i in range(len(arr) - 1, 0, -1):
		if arr[i - 1] < arr[i]:
			fl = 1
		if arr[i - 1] > arr[i] and fl == 1:
			return i
	return 0

def solve(arr):
	for i in range(0, len(arr) - 1):
		if hasMin(arr[i:]) == False:
			return i
	return len(arr) - 1
while T > 0:
	n = int(input())
	arr = [int(el) for el in input().split(' ')]
	print(solve2(arr))
	T -= 1


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


t = int(input())
for tt in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	pos = n - 1
	while pos > 0 and arr[pos - 1] >= arr[pos]:
		pos -= 1
	while pos > 0 and arr[pos - 1] <= arr[pos]:
		pos -= 1
	print(pos)


n = int(input())
seqs = [(input(), map(int, input().split()))[1] for _ in range(n)]
for seq in seqs:
	seq = list(reversed(list(seq)))
	was_first_peak = False
	for (i, prev, el) in zip(range(1, len(seq)), seq, seq[1:]):
		if el < prev:
			was_first_peak = True
		if was_first_peak and el > prev:
			print(len(seq) - i)
			break
	else:
		print(0)


t = int(input())
for _ in range(t):
	n = int(input())
	arr = [int(x) for x in input().split()]
	inc = True
	brk = False
	ans = 0
	for i in range(n - 1):
		if brk == False:
			if inc == True:
				if arr[n - i - 2] < arr[n - i - 1]:
					inc = False
			elif arr[n - i - 2] > arr[n - i - 1]:
				ans = n - i - 1
				brk = True
	print(ans)


def solve(n, a):
	pos = n - 1
	index = n - 1
	while pos > 0:
		if a[pos] > a[pos - 1]:
			index = pos
			break
		pos -= 1
	while pos > 0:
		if a[pos] < a[pos - 1]:
			return pos
		pos -= 1
	return 0
t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	print(solve(n, a))


t = int(input())
while t > 0:
	t = t - 1
	n = int(input())
	a = input()
	A = list(map(int, list(a.split())))
	if n <= 2:
		print(0)
	else:
		p = A[n - 1] - A[n - 2]
		j = 0
		for i in range(n - 2, 0, -1):
			if A[i] - A[i - 1] < 0 and p > 0:
				j = i
				break
			elif A[i] - A[i - 1] != 0:
				p = A[i] - A[i - 1]
		if j == 0:
			print(0)
		else:
			print(j)


t = int(input())
for _ in range(t):
	_ = int(input())
	v = list(map(int, input().split()))
	ad = True
	dd = True
	s = 0
	for i in range(len(v) - 1, 0, -1):
		if not v[i] <= v[i - 1]:
			dd = False
		if not v[i] >= v[i - 1] and (not dd):
			ad = False
		if not dd and (not ad):
			s = i
			break
	print(s)


import sys, os, io

def rs():
	return sys.stdin.readline().rstrip()

def ri():
	return int(sys.stdin.readline())

def ria():
	return list(map(int, sys.stdin.readline().split()))

def ws(s):
	sys.stdout.write(s + '\n')

def wi(n):
	sys.stdout.write(str(n) + '\n')

def wia(a):
	sys.stdout.write(' '.join([str(x) for x in a]) + '\n')
import math, datetime, functools, itertools
from collections import deque, defaultdict, OrderedDict
import collections

def main():
	starttime = datetime.datetime.now()
	if os.path.exists('input.txt'):
		sys.stdin = open('input.txt', 'r')
		sys.stdout = open('output.txt', 'w')
	for _ in range(ri()):
		n = ri()
		a = ria()
		pos = n - 1
		while pos > 0 and a[pos - 1] >= a[pos]:
			pos -= 1
		while pos > 0 and a[pos - 1] <= a[pos]:
			pos -= 1
		print(pos)
	endtime = datetime.datetime.now()
	time = (endtime - starttime).total_seconds() * 1000
	if os.path.exists('input.txt'):
		print('Time:', time, 'ms')

class FastReader(io.IOBase):
	newlines = 0

	def __init__(self, fd, chunk_size=1024 * 8):
		self._fd = fd
		self._chunk_size = chunk_size
		self.buffer = io.BytesIO()

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, self._chunk_size))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self, size=-1):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, self._chunk_size if size == -1 else size))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

class FastWriter(io.IOBase):

	def __init__(self, fd):
		self._fd = fd
		self.buffer = io.BytesIO()
		self.write = self.buffer.write

	def flush(self):
		os.write(self._fd, self.buffer.getvalue())
		(self.buffer.truncate(0), self.buffer.seek(0))

class FastStdin(io.IOBase):

	def __init__(self, fd=0):
		self.buffer = FastReader(fd)
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

class FastStdout(io.IOBase):

	def __init__(self, fd=1):
		self.buffer = FastWriter(fd)
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.flush = self.buffer.flush
sys.stdin = FastStdin()
sys.stdout = FastStdout()
main()


from os import path
import sys
from heapq import heappush, heappop
from functools import cmp_to_key as ctk
from collections import deque, defaultdict as dd
from bisect import bisect, bisect_left, bisect_right, insort, insort_left, insort_right
from itertools import permutations
from datetime import datetime
from math import ceil, sqrt, log, gcd

def ii():
	return int(input())

def si():
	return input().rstrip()

def mi():
	return map(int, input().split())

def li():
	return list(mi())
abc = 'abcdefghijklmnopqrstuvwxyz'
mod = 1000000007
inf = float('inf')
vow = ['a', 'e', 'i', 'o', 'u']
(dx, dy) = ([-1, 1, 0, 0], [0, 0, 1, -1])

def bo(i):
	return ord(i) - ord('a')
file = 1

def func(a, b):
	if a[0] == b[0]:
		if a[1] > b[1]:
			return -1
		else:
			return 1
	if a[0] <= b[0]:
		return -1
	else:
		return 1

def solve():
	for _ in range(ii()):
		n = ii()
		a = li()
		i = 0
		ans = 0
		for i in range(1, n - 1):
			if a[i] < a[i - 1]:
				j = i + 1
				while j < n - 1 and a[j] == a[i]:
					j += 1
				if a[j] > a[i]:
					ans = i
				i = j - 1
		print(ans)
if file:
	if path.exists('input.txt'):
		sys.stdin = open('input.txt', 'r')
		sys.stdout = open('output.txt', 'w')
	else:
		input = sys.stdin.readline
solve()


t = int(input())
for _ in range(t):
	n = int(input())
	list1 = list(map(int, input().strip().split()))
	if n <= 2:
		print(0)
		continue
	index = -1
	minima = 0
	for i in range(n - 2, 0 - 1, -1):
		if list1[i] < list1[i + 1]:
			minima = 1
		if list1[i] > list1[i + 1] and minima == 1:
			index = i
			break
	if index == -1:
		print(0)
	else:
		print(index + 1)


for __ in range(int(input())):
	n = int(input())
	A = list(reversed([int(_) for _ in input().split()]))
	x = 0
	for _ in range(n - 1):
		if x == 1:
			if A[_ + 1] > A[_]:
				print(n - _ - 1)
				break
		if A[_ + 1] < A[_]:
			x = 1
	else:
		print(0)


from collections import defaultdict
t = int(input())
for _ in range(t):
	n = int(input())
	flag = 0
	a = list(map(int, input().split()))
	i = n - 2
	cnt = 0
	while i >= 0:
		if a[i] >= a[i + 1] and flag == 0:
			i -= 1
		elif a[i] > a[i + 1] and flag == 1:
			break
		elif a[i] < a[i + 1] and flag == 0:
			flag = 1
			i -= 1
		else:
			i -= 1
	print(i + 1)


t = int(input())
while t > 0:
	siz = int(input())
	arr = list(map(int, input().split()))
	cnt = siz - 1
	while arr[cnt - 1] >= arr[cnt] and cnt > 0:
		cnt -= 1
	while arr[cnt - 1] <= arr[cnt] and cnt > 0:
		cnt -= 1
	print(cnt)
	t -= 1


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

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()


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def RL():
	return map(int, sys.stdin.readline().rstrip().split())

def RLL():
	return list(map(int, sys.stdin.readline().rstrip().split()))

def N():
	return int(input())

def comb(n, m):
	return factorial(n) / (factorial(m) * factorial(n - m)) if n >= m else 0

def perm(n, m):
	return factorial(n) // factorial(n - m) if n >= m else 0

def mdis(x1, y1, x2, y2):
	return abs(x1 - x2) + abs(y1 - y2)
mod = 998244353
INF = float('inf')
from math import factorial
from collections import Counter, defaultdict, deque
from heapq import heapify, heappop, heappush

def main():
	for _ in range(N()):
		n = N()
		arr = RLL()
		ind = n - 1
		for i in range(n - 1, -1, -1):
			if arr[i] > arr[i - 1]:
				ind = i
				break
		else:
			ind = 0
		res = ind
		for i in range(ind, -1, -1):
			if arr[i] < arr[i - 1]:
				res = i
				break
		else:
			res = 0
		print(res)
main()


t = int(input())
while t:
	t -= 1
	n = int(input())
	arr = list(map(int, input().split()))
	pos = n - 1
	while pos > 0 and arr[pos - 1] >= arr[pos]:
		pos -= 1
	while pos > 0 and arr[pos - 1] <= arr[pos]:
		pos -= 1
	print(pos)


from sys import stdin
input = stdin.buffer.readline
for _ in range(int(input())):
	n = int(input())
	(*a,) = map(int, input().split())
	a = a[::-1] + [0]
	c = 1
	for i in range(n):
		if c and a[i] > a[i + 1]:
			c ^= 1
		elif not c and a[i] < a[i + 1]:
			break
	print(n - i - 1)


def main():
	n = int(input())
	for _ in range(n):
		t = int(input())
		a = list(map(int, input().split()))
		count = 0
		flag = -1
		i = t - 1
		while i >= 0 and flag <= a[i]:
			flag = a[i]
			i -= 1
		while i >= 0 and flag >= a[i]:
			flag = a[i]
			i -= 1
		print(i + 1)
main()


from collections import deque

def is_good(array):
	dq = deque(array)
	tmp = []
	while dq:
		if dq[0] <= dq[-1]:
			tmp.append(dq.popleft())
		else:
			tmp.append(dq.pop())
	return is_non_decreasing(tmp)

def is_non_decreasing(array):
	for i in range(len(array) - 1):
		if array[i] > array[i + 1]:
			return False
	return True

def make_it_good_wa(array):
	prefix = 0
	n = len(array)
	if n > 2:
		for i in range(1, n - 1):
			if array[i] < array[i - 1] and array[i] < array[i + 1]:
				prefix = i
	else:
		dq = deque(array)
		while not is_good(dq):
			dq.popleft()
			prefix += 1
	return prefix

def make_it_good(array):
	p = len(array) - 1
	while p > 0 and a[p - 1] >= a[p]:
		p -= 1
	while p > 0 and a[p - 1] <= a[p]:
		p -= 1
	return p
t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(x) for x in input().split()]
	shortest_prefix = make_it_good(a)
	print(shortest_prefix)


a = input()
for x in range(int(a)):
	b = input()
	c = input().split(' ')
	list1 = [int(i) for i in c]
	ans = 1
	tag = list1[-1]
	for i in range(len(list1) - 2, -1, -1):
		if tag >= list1[i]:
			tag = list1[i]
			ans += 1
		else:
			break
	ans1 = 1
	tag = list1[-1]
	buffer = 0
	for i in range(len(list1) - 2, -1, -1):
		if tag <= list1[i] and buffer == 0:
			ans1 += 1
			tag = list1[i]
		elif buffer == 1:
			if tag >= list1[i]:
				ans1 += 1
				tag = list1[i]
			else:
				break
		else:
			ans1 += 1
			tag = list1[i]
			buffer = 1
	print(len(list1) - max(ans, ans1))


def d(a, n):
	i = n - 1
	count = 0
	while i > 0 and a[i - 1] >= a[i]:
		count += 1
		i -= 1
	while i > 0 and a[i] >= a[i - 1]:
		count += 1
		i -= 1
	return n - count - 1
ans = ''
for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	ans += str(d(arr, n)) + '\n'
print(ans)


t = int(input())
for t1 in range(t):
	x = int(input())
	arr = list(map(int, input().split()))
	a = 0
	b = 0
	p = a
	while b < x - 1:
		p = a
		while a < x - 1 and arr[a + 1] >= arr[a]:
			a += 1
		b = a
		while b < x - 1 and arr[b + 1] <= arr[b]:
			b += 1
		a = b
		while a > 0 and arr[a - 1] == arr[a]:
			a -= 1
	print(p)


for t in range(0, int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	if n <= 2:
		print(0)
		continue
	ans = 0
	passedMax = False
	for i in range(n - 1, 0, -1):
		if passedMax is False and arr[i] > arr[i - 1]:
			passedMax = True
		if passedMax is True and arr[i] < arr[i - 1]:
			ans = i
			break
	print(ans)


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	dec = 0
	ans = 0
	for j in range(n - 1, -1, -1):
		if j == 1:
			if dec == 1:
				if a[j] < a[j - 1]:
					ans = j
					break
				else:
					ans = 0
					break
			else:
				ans = 0
				break
		if dec == 1:
			if a[j] < a[j - 1]:
				ans = j
				break
		elif a[j] <= a[j - 1]:
			continue
		else:
			dec = 1
	print(ans)


t = int(input())
import math
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	if sorted(a) == a:
		print(0)
		continue
	c = 0
	if len(a) <= 2:
		print(0)
		continue
	k = n - 1
	while k > 0 and a[k - 1] >= a[k]:
		k -= 1
	while k > 0 and a[k - 1] <= a[k]:
		k -= 1
	print(k)


__MULTITEST = True

def solve():
	n = int(input())
	a = list(map(int, input().split()))
	ans = 0
	p2 = n - 1
	while 0 < p2 and a[p2 - 1] >= a[p2]:
		p2 -= 1
	p1 = p2
	while 0 < p1 and a[p1 - 1] <= a[p1]:
		p1 -= 1
	print(p1)
t = int(input()) if __MULTITEST else 1
for tt in range(t):
	solve()


import math
num_cases = int(input())
for _ in range(num_cases):
	n = int(input())
	arr = list(map(int, input().strip().split()))
	idx = n - 1
	while idx >= 1 and arr[idx - 1] >= arr[idx]:
		idx -= 1
	maxi = arr[idx]
	if idx == 0:
		print('0')
	else:
		idx -= 1
		while idx >= 0 and arr[idx] <= maxi and (arr[idx] <= arr[idx + 1]):
			idx -= 1
		print(idx + 1)


import sys
t = int(sys.stdin.readline())
for _ in range(t):
	n = int(sys.stdin.readline())
	a = list(map(int, sys.stdin.readline().split()))
	pos = n - 1
	while pos > 0 and a[pos] <= a[pos - 1]:
		pos -= 1
	while pos > 0 and a[pos] >= a[pos - 1]:
		pos -= 1
	print(pos)


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def list2d(a, b, c):
	return [[c] * b for i in range(a)]

def list3d(a, b, c, d):
	return [[[d] * c for j in range(b)] for i in range(a)]

def list4d(a, b, c, d, e):
	return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]

def ceil(x, y=1):
	return int(-(-x // y))

def Yes():
	print('Yes')

def No():
	print('No')

def YES():
	print('YES')

def NO():
	print('NO')
INF = 10 ** 18
MOD = 10 ** 9 + 7
Ri = lambda : [int(x) for x in sys.stdin.readline().split()]
ri = lambda : sys.stdin.readline().strip()
for _ in range(int(ri())):
	n = int(ri())
	a = Ri()
	ans = 1
	maxx = a[-1]
	flag = True
	for i in range(len(a) - 2, -1, -1):
		if flag:
			if a[i] >= maxx:
				ans += 1
				maxx = a[i]
			else:
				flag = False
				ans += 1
				maxx = a[i]
		elif a[i] <= maxx:
			ans += 1
			maxx = a[i]
		else:
			break
	print(n - ans)


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

	def solve():
		n = int(input())
		a = list(map(int, input().split()))
		ans = 0
		ma = a[0]
		if n <= 2:
			print(0)
			return
		else:
			for i in range(n - 2, -1, -1):
				if a[i] < a[i + 1]:
					break
			else:
				print(0)
				return
			for j in range(i, -1, -1):
				if a[j] > a[j + 1]:
					print(j + 1)
					break
			else:
				print(0)
	solve()


for _ in range(int(input())):
	n = int(input())
	arr = [int(i) for i in input().split()]
	last = arr[-1]
	result = n - 1
	flag = True
	for i in range(n - 1, -1, -1):
		result = i
		if flag:
			if arr[i] < last:
				flag = False
		elif arr[i] > last:
			result += 1
			break
		last = arr[i]
	print(result)


for t in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	if n < 3:
		print('0')
	else:
		b = 0
		for i in range(n - 2, 0, -1):
			if a[i] < a[i + 1]:
				b = 1
				if a[i] < a[i - 1]:
					print(i)
					break
			elif b == 1 and a[i - 1] > a[i]:
				print(i)
				break
		else:
			print('0')


test = int(input())
for i in range(test):
	size = int(input())
	element = input()
	lists = [int(x) for x in element.split()]
	lists.reverse()
	keys = 0
	key = 0
	for j in range(len(lists) - 1):
		if lists[j + 1] < lists[j]:
			key = j + 1
			break
	if key == 0:
		print(0)
	else:
		for k in range(len(lists) - key - 1):
			if lists[key + 1] > lists[key]:
				keys = key + 1
				break
			else:
				key = key + 1
		if keys == 0:
			print(0)
		else:
			print(len(lists) - keys)


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


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	mi = 0
	prev = min(a[0], a[-1])
	i = 0
	j = n - 1
	while i < j:
		if a[i] <= a[j] and a[i] >= prev:
			prev = a[i]
			i += 1
		elif a[i] >= a[j] and a[j] >= prev:
			prev = a[j]
			j -= 1
		elif a[i] < prev:
			mi = i
			j = n - 1
			prev = min(a[i], a[j])
		else:
			i += 1
			mi = i
			j = n - 1
			prev = min(a[i], a[j])
	print(mi)


TC = int(input())
for tc in range(TC):
	N = int(input())
	A = list(map(int, input().split()))
	i = N - 2
	while i >= 0 and A[i] >= A[i + 1]:
		i -= 1
	if i == -1:
		print(0)
	else:
		while i >= 0 and A[i] <= A[i + 1]:
			i -= 1
		if i < 0:
			print(0)
		else:
			print(i + 1)


from collections import Counter
t = int(input())
for _ in range(t):
	leng = int(input())
	l1 = list(map(int, input().split()))
	count = 0
	temp = []
	count = len(l1) - 1
	i = len(l1) - 1
	j = 0
	while l1:
		if l1[i] <= l1[i - 1] and i > 0:
			count -= 1
			l1.pop()
			i -= 1
		else:
			break
	while l1:
		if l1[i] >= l1[i - 1] and i > 0:
			count -= 1
			l1.pop()
			i -= 1
		else:
			break
	print(count)


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	if n == 1:
		print(0)
	else:
		yb = 0
		v = 0
		r = 0
		i = n - 1
		while 0 <= i < n and a[i] == a[i - 1]:
			i -= 1
		while 0 <= i < n and a[i] <= a[i - 1]:
			i -= 1
		while 0 <= i < n and a[i] >= a[i - 1]:
			i -= 1
		if i == -1:
			i = 0
		print(i)


import os
import sys
from io import BytesIO, IOBase
from collections import Counter
from collections import deque
from collections import OrderedDict
import heapq
import math
import statistics
MAX = float('inf')
MIN = float('-inf')
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
for i in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	j = n - 1
	while j > 0 and a[j - 1] >= a[j]:
		j -= 1
	while j > 0 and a[j - 1] <= a[j]:
		j -= 1
	print(j)


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def main():
	for t in range(int(input())):
		n = int(input())
		l = [int(j) for j in input().split()]
		mx = []
		rmx = []
		tmp = -1
		for i in range(n - 1, -1, -1):
			rmx.append(tmp)
			tmp = max(tmp, l[i])
		ans = 0
		tmp = -1
		rmx = rmx[::-1]
		for i in range(n):
			if l[i] < tmp and l[i] < rmx[i]:
				ans = i
				tmp = l[i]
			tmp = max(l[i], tmp)
		print(ans)
main()


total = int(input())
for _ in range(total):
	length_a = input()
	a = list(map(int, input().strip().split(' ')))
	b = a[::-1]
	increasing = True
	res = []
	res.append(b[0])
	index_stopped = len(b)
	for i in range(1, len(b)):
		if increasing:
			if b[i] >= b[i - 1]:
				res.append(b[i])
			else:
				increasing = False
				res.append(b[i])
		elif b[i] <= b[i - 1]:
			res.append(b[i])
		else:
			index_stopped = i
			break
	print(len(b) - index_stopped)


for _ in range(int(input())):
	n = int(input())
	x = list(map(int, input().split()))
	state = 0
	for (i, v) in enumerate(x[::-1]):
		if state == 0:
			state += 1
		elif state == 1:
			if v < last:
				state += 1
		elif state == 2:
			if v > last:
				print(n - i)
				break
		last = v
	else:
		print(0)


for t in range(int(input())):
	n = int(input())
	arr = list(map(int, input().strip().split()))
	arr.reverse()
	c = 0
	once = False
	i = 0
	while i < n - 1 and (not once):
		if arr[i] <= arr[i + 1]:
			i += 1
		else:
			once = True
			i += 1
	if i == n - 1:
		print(0)
	else:
		while i < n - 1 and arr[i + 1] <= arr[i]:
			i += 1
		print(n - i - 1)


for i in range(int(input())):
	n = int(input())
	y = [int(x) for x in input().split()]
	b = []
	p = 99
	k = 0
	b = False
	for j in range(n - 1, 0, -1):
		if y[j - 1] < y[j]:
			if p == 99:
				p = 1
			if p == 98:
				p = 2
		elif y[j - 1] > y[j]:
			if p == 99:
				p = 98
			if p == 2:
				print(j)
				b = True
				break
			if p == 1:
				print(j)
				b = True
				break
	if b == False:
		print(0)


for tc in range(int(input())):
	n = int(input())
	A = input().split(' ')
	for i in range(n):
		A[i] = int(A[i])
	x = 1
	while x < n and A[n - 1 - x] >= A[n - x]:
		x += 1
	while x < n and A[n - 1 - x] <= A[n - x]:
		x += 1
	print(n - x)


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	if n < 3 or sorted(l) == l:
		print(0)
		continue
	indx = ans = 0
	for i in range(n - 2, -1, -1):
		if l[i + 1] > l[i]:
			indx = i
			break
	for i in range(indx - 1, -1, -1):
		if l[i] > l[i + 1]:
			ans = i + 1
			break
	print(ans)


for t in range(int(input())):
	n = int(input())
	x = list(map(int, input().split()))
	found = 0
	nax = x[-1]
	new_min = 1000000.0
	index = -999
	for i in range(n - 1, -1, -1):
		if x[i] > new_min:
			found = index
			break
		if x[i] >= nax:
			nax = x[i]
		else:
			new_min = x[i]
			index = i
	if n < 3:
		found = 0
	print(found)


def isGood(A, start, n):
	i = start
	while i < n - 1 and A[i] <= A[i + 1]:
		i += 1
	if i == n - 1:
		return False
	j = n - 1
	while j > i + 1 and A[j] <= A[j - 1]:
		j -= 1
	if j == i + 1:
		return False
	return True

def binarySearch(a, lo=0, hi=None):
	if lo < 0:
		raise ValueError('lo must be non-negative')
	if hi is None:
		hi = len(a)
	while True:
		if lo == hi:
			return lo
		mid = (lo + hi) // 2
		if isGood(a, mid, len(a)):
			lo = mid + 1
		else:
			hi = mid
t = int(input())
for _ in range(t):
	n = int(input())
	A = list(map(int, input().split()))
	print(binarySearch(A))


for t in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	ans = [0]
	prefix = n - 1
	while prefix > 0 and a[prefix - 1] >= a[prefix]:
		prefix -= 1
	while prefix > 0 and a[prefix - 1] <= a[prefix]:
		prefix -= 1
	print(prefix)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	maxpos = -1
	maxval = float('-inf')
	for i in range(n - 1, -1, -1):
		if a[i] >= maxval:
			maxval = a[i]
			maxpos = i
		else:
			break
	minpos = -1
	minval = maxval
	for i in range(maxpos - 1, -1, -1):
		if a[i] <= minval:
			minval = a[i]
			minpos = i
		else:
			break
	if minpos >= 0:
		print(minpos)
	else:
		print(maxpos)


t = int(input())
for i in range(t):
	n = int(input())
	val = list(map(int, input().split()))
	c = 1
	p1 = 0
	p2 = 0
	if val[n - 2] >= val[n - 1] and n > 1:
		for j in range(n - 1, 0, -1):
			if val[j - 1] >= val[j] and p1 == 0:
				c = c + 1
			elif val[j - 1] <= val[j]:
				c = c + 1
				p1 = 1
			else:
				break
	elif val[n - 1] > val[n - 2] and n > 1:
		for j in range(n - 1, 0, -1):
			if val[j] >= val[j - 1]:
				c = c + 1
			else:
				break
	print(n - c)


from sys import stdin
input = stdin.readline
for _ in range(int(input())):
	n = int(input())
	lis = list(map(int, input().split()))
	i = 0
	j = n - 1
	g = []
	while i <= j:
		if lis[i] < lis[j]:
			if len(g) != 0:
				if g[-1] <= lis[i]:
					g.append(lis[i])
					i += 1
				else:
					g = []
					j = n - 1
			else:
				g.append(lis[i])
				i += 1
		elif len(g) != 0:
			if g[-1] <= lis[j]:
				g.append(lis[j])
				j -= 1
			else:
				g = []
				j = n - 1
				i += 1
		else:
			g.append(lis[j])
			j -= 1
	print(n - len(g))


t = int(input())
while t:
	t -= 1
	n = int(input())
	arr = list(map(int, input().split()))
	j = n - 2
	inc = True
	while j > -1:
		while j > -1 and inc:
			if arr[j] >= arr[j + 1]:
				j -= 1
			else:
				inc = False
				break
		else:
			break
		while j > -1 and (not inc):
			if arr[j] <= arr[j + 1]:
				j -= 1
			else:
				break
	print(j + 1)


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	i = len(a) - 1
	flag = 0
	max = a[i]
	while i >= 0:
		if a[i] < max:
			break
		max = a[i]
		i = i - 1
	if i != -1:
		while i >= 0:
			if a[i] > a[i + 1]:
				break
			i = i - 1
		if i != -1:
			print(i + 1)
		else:
			print(0)
	else:
		print(0)


def FindFirst(arr, n):
	right = left = False
	for i in range(n - 2, 0, -1):
		if arr[i] < arr[i + 1] and arr[i] < arr[i - 1]:
			return i
		elif arr[i] < arr[i + 1]:
			right = True
		elif arr[i] < arr[i - 1] and right:
			left = True
		if left and right:
			return i
	return 0
t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(x) for x in input().split()]
	print(FindFirst(a, n))


import sys
input = lambda : sys.stdin.readline().strip()
t = int(input())
while t:
	t -= 1
	n = int(input())
	a = list(map(int, input().split()))
	ans = n - 1
	inc = True
	for i in range(n - 2, -1, -1):
		if inc:
			if a[i] < a[i + 1]:
				if inc == False:
					break
				else:
					inc = False
			ans -= 1
		else:
			if a[i] > a[i + 1]:
				break
			ans -= 1
	print(ans)


import sys
input = sys.stdin.readline

def ceil(x):
	if x != int(x):
		x = int(x) + 1
	return x

def swaparr(arr, a, b):
	temp = arr[a]
	arr[a] = arr[b]
	arr[b] = temp

def gcd(a, b):
	if b == 0:
		return a
	return gcd(b, a % b)

def nCr(n, k):
	if k > n - k:
		k = n - k
	res = 1
	for i in range(k):
		res = res * (n - i)
		res = res / (i + 1)
	return int(res)

def upper_bound(a, x, lo=0, hi=None):
	if hi == None:
		hi = len(a)
	while lo < hi:
		mid = (lo + hi) // 2
		if a[mid] < x:
			lo = mid + 1
		else:
			hi = mid
	return lo

def primefs(n):
	primes = {}
	while n % 2 == 0 and n > 0:
		primes[2] = primes.get(2, 0) + 1
		n = n // 2
	for i in range(3, int(n ** 0.5) + 2, 2):
		while n % i == 0 and n > 0:
			primes[i] = primes.get(i, 0) + 1
			n = n // i
	if n > 2:
		primes[n] = primes.get(n, 0) + 1
	return primes

def power(x, y, p):
	res = 1
	x = x % p
	if x == 0:
		return 0
	while y > 0:
		if y & 1 == 1:
			res = res * x % p
		y = y >> 1
		x = x * x % p
	return res

def swap(a, b):
	temp = a
	a = b
	b = temp
	return (a, b)

def find(x, link):
	p = x
	while p != link[p]:
		p = link[p]
	while x != p:
		nex = link[x]
		link[x] = p
		x = nex
	return p

def union(x, y, link, size):
	x = find(x, link)
	y = find(y, link)
	if size[x] < size[y]:
		(x, y) = swap(x, y)
	if x != y:
		size[x] += size[y]
		link[y] = x

def sieve(n):
	prime = [True for i in range(n + 1)]
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p += 1
	return prime
MAXN = int(100000.0 + 5)

def spf_sieve():
	spf[1] = 1
	for i in range(2, MAXN):
		spf[i] = i
	for i in range(4, MAXN, 2):
		spf[i] = 2
	for i in range(3, ceil(MAXN ** 0.5), 2):
		if spf[i] == i:
			for j in range(i * i, MAXN, i):
				if spf[j] == j:
					spf[j] = i

def factoriazation(x):
	ret = {}
	while x != 1:
		ret[spf[x]] = ret.get(spf[x], 0) + 1
		x = x // spf[x]
	return ret

def int_array():
	return list(map(int, input().strip().split()))

def str_array():
	return input().strip().split()
MOD = int(1000000000.0) + 7
CMOD = 998244353
INF = float('inf')
NINF = -float('inf')
for _ in range(int(input())):
	n = int(input())
	a = int_array()
	x = n - 1
	while x > 0:
		if a[x] <= a[x - 1]:
			x -= 1
		else:
			break
	while x > 0:
		if a[x] >= a[x - 1]:
			x -= 1
		else:
			break
	print(x)


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	goodlen = 1
	isgrowing = True
	isdecr = False
	it = n - 1
	while it > 0:
		if isgrowing:
			if arr[it] <= arr[it - 1]:
				it -= 1
				goodlen += 1
			else:
				isgrowing = False
				isdecr = True
		if isdecr:
			if arr[it] >= arr[it - 1]:
				it -= 1
				goodlen += 1
			else:
				break
	print(n - goodlen)


t = int(input())
for i in range(t):
	n = int(input())
	a = [int(x) for x in input().split()]
	prev = n - 1
	curr = n - 2
	count1 = 0
	while curr >= 0:
		if a[curr] >= a[prev]:
			count1 = count1 + 1
			curr = curr - 1
			prev = prev - 1
		else:
			break
	count2 = 0
	while curr >= 0:
		if a[curr] <= a[prev]:
			count2 = count2 + 1
			curr = curr - 1
			prev = prev - 1
		else:
			break
	print(n - (count1 + count2 + 1))


t = int(input())
while t:
	n = int(input())
	a = [int(x) for x in input().split()]
	r = [0] * n
	b = [float('inf')]
	b.extend(a)
	c = 0
	down = 0
	f = 0
	for i in range(len(b) - 1, 0, -1):
		if b[i - 1] < b[i]:
			down = 1
		elif b[i - 1] > b[i] and down:
			print(i - 1)
			f = 1
			break
	if f == 0:
		print(0)
	t -= 1


from __future__ import division, print_function
import itertools, os, sys, threading
from io import BytesIO, IOBase
if sys.version_info[0] < 3:
	from __builtin__ import xrange as range
	from future_builtins import ascii, filter, hex, map, oct, zip
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
from math import sqrt, floor, factorial, gcd, log, ceil
from collections import deque, Counter, defaultdict
from itertools import permutations, combinations
from math import gcd
from bisect import bisect
from random import randrange
input = lambda : sys.stdin.readline().rstrip('\r\n')
read = lambda : list(map(int, input().strip().split(' ')))

def main():
	for _ in range(int(input())):
		n = int(input())
		arr = read()
		arr.reverse()
		if n == 1:
			print(0)
			continue
		i = 0
		while i + 1 < n and arr[i + 1] >= arr[i]:
			i += 1
		while i + 1 < n and arr[i + 1] <= arr[i]:
			i += 1
		print(n - i - 1)
main()


def fact(x):
	if x == 0:
		return 1
	else:
		return x * fact(x - 1)

def lower_bound(li, num):
	answer = -1
	start = 0
	end = len(li) - 1
	while start <= end:
		middle = (end + start) // 2
		if li[middle] >= num:
			answer = middle
			end = middle - 1
		else:
			start = middle + 1
	return answer

def upper_bound(li, num):
	answer = -1
	start = 0
	end = len(li) - 1
	while start <= end:
		middle = (end + start) // 2
		if li[middle] <= num:
			answer = middle
			start = middle + 1
		else:
			end = middle - 1
	return answer

def abs(x):
	return x if x >= 0 else -x

def binary_search(li, val, lb, ub):
	ans = 0
	while lb <= ub:
		mid = (lb + ub) // 2
		if li[mid] > val:
			ub = mid - 1
		elif val > li[mid]:
			lb = mid + 1
		else:
			ans = 1
			break
	return ans

def sieve_of_eratosthenes(n):
	ans = []
	arr = [1] * (n + 1)
	(arr[0], arr[1], i) = (0, 0, 2)
	while i * i <= n:
		if arr[i] == 1:
			j = i + i
			while j <= n:
				arr[j] = 0
				j += i
		i += 1
	for k in range(n):
		if arr[k] == 1:
			ans.append(k)
	return ans

def nc2(x):
	if x == 1:
		return 0
	else:
		return x * (x - 1) // 2

def kadane(x):
	sum_so_far = 0
	current_sum = 0
	for i in x:
		current_sum += i
		if current_sum < 0:
			current_sum = 0
		else:
			sum_so_far = max(sum_so_far, current_sum)
	return sum_so_far

def mex(li):
	check = [0] * 1001
	for i in li:
		check[i] += 1
	for i in range(len(check)):
		if check[i] == 0:
			return i

def sumdigits(n):
	ans = 0
	while n != 0:
		ans += n % 10
		n //= 10
	return ans

def product(li):
	ans = 1
	for i in li:
		ans *= i
	return ans

def maxpower(n, k):
	cnt = 0
	while n > 1:
		cnt += 1
		n //= k
	return cnt
from math import *
from collections import deque
max_steps = 100005
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	i = n - 1
	while i > 0:
		if a[i] <= a[i - 1]:
			i -= 1
		else:
			break
	while i > 0:
		if a[i] >= a[i - 1]:
			i -= 1
		else:
			break
	print(i)


from collections import Counter
t = int(input())
MAXX = 2 * 10 ** 5 + 7
while t:
	t -= 1
	n = int(input())
	arr = [int(x) for x in input().split()]
	counter = Counter(arr)
	sarr = sorted(arr)
	i = 0
	res = 0
	(s, e) = (0, n - 1)
	while i < n:
		if counter[sarr[i]] == 0:
			i += 1
			continue
		if arr[s] == sarr[i]:
			s += 1
			counter[sarr[i]] -= 1
			i += 1
		elif arr[e] == sarr[i]:
			e -= 1
			counter[sarr[i]] -= 1
			i += 1
		else:
			res = max(res, s + 1)
			counter[arr[s]] -= 1
			s += 1
	print(res)


t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	lst = []
	i = 1
	while i < n - 1:
		if arr[i] > arr[i - 1] and arr[i] > arr[i + 1]:
			lst.append([i, 1])
			i += 1
		elif arr[i] < arr[i - 1] and arr[i] < arr[i + 1]:
			lst.append([i, 0])
			i += 1
		elif arr[i] < arr[i - 1] and arr[i] == arr[i + 1]:
			j = i
			i += 1
			while i < n - 1 and arr[j] == arr[i]:
				i += 1
			if arr[j] < arr[i]:
				lst.append([j, 0])
		elif arr[i] > arr[i - 1] and arr[i] == arr[i + 1]:
			j = i
			i += 1
			while i < n - 1 and arr[j] == arr[i]:
				i += 1
			if arr[j] > arr[i]:
				lst.append([j, 1])
		else:
			i += 1
	if len(lst) == 0:
		print(0)
	elif len(lst) == 1:
		if lst[0][1] == 0:
			print(lst[0][0])
		else:
			print(0)
	elif len(lst) >= 2:
		if lst[-1][1] == 0:
			print(lst[-1][0])
		else:
			print(lst[-2][0])


for i in range(int(input())):
	n = int(input())
	a = list(map(int, input().strip().split()))
	b = sorted(a)
	count = {}
	c = -1
	for i in b:
		count[i] = count.get(i, 0) + 1
	i = 0
	ptr1 = 0
	ptr2 = n - 1
	while i < n and ptr1 < n and (ptr2 >= 0) and (ptr1 <= ptr2):
		if count[b[i]] <= 0:
			i += 1
			continue
		if b[i] == a[ptr2]:
			count[a[ptr2]] -= 1
			ptr2 -= 1
			i += 1
		elif b[i] == a[ptr1]:
			count[a[ptr1]] -= 1
			ptr1 += 1
			i += 1
		else:
			c = ptr1
			count[a[ptr1]] -= 1
			ptr1 += 1
	print(c + 1)


from collections import defaultdict as dc
for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	x = dc(int)
	l.insert(0, 0)
	y = n
	for i in range(n, 0, -1):
		if i == n:
			y = n
		elif i == 1:
			y = 1
		else:
			if l[i] != l[i + 1]:
				x[l[i]] = l[i + 1]
			if l[i - 1] > l[i] and x[l[i]] > l[i]:
				y = i
				break
			else:
				y = i
	print(y - 1)


T = int(input())
for _ in range(T):
	n = int(input())
	a = list(map(int, input().split()))
	if n <= 2:
		ans = 0
	else:
		a = a[::-1]
		if a[1] > a[0]:
			d = 1
		elif a[1] < a[0]:
			d = -1
		else:
			d = 0
		l = 1
		for i in range(1, n):
			if d == 0:
				if a[i] > a[i - 1]:
					d = 1
				elif a[i] < a[i - 1]:
					d = -1
				else:
					d = 0
			elif d == 1:
				if a[i] < a[i - 1]:
					d = -1
			elif d == -1 and a[i] > a[i - 1]:
				break
			l += 1
		ans = n - l
	print(ans)


import math
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	pos = n - 1
	while pos > 0 and a[pos - 1] >= a[pos]:
		pos -= 1
	while pos > 0 and a[pos - 1] <= a[pos]:
		pos -= 1
	print(pos)


def f(n, a):
	peak_idx = -1
	for i in range(n - 2, -1, -1):
		if a[i] < a[i + 1]:
			peak_idx = i + 1
			break
	if peak_idx == -1:
		return 0
	for j in range(peak_idx - 1, -1, -1):
		if a[j] > a[j + 1]:
			return j + 1
	return 0

def main():
	t = int(input())
	for _ in range(t):
		n = int(input())
		a = list(map(int, input().split()))
		print(f(n, a))
main()


def main():
	for _ in range(int(input())):
		n = int(input())
		a = list(map(int, input().split()))
		pos = n - 1
		while pos > 0 and a[pos] <= a[pos - 1]:
			pos -= 1
		while pos > 0 and a[pos] >= a[pos - 1]:
			pos -= 1
		print(pos)
main()


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	if a == sorted(a) or a == sorted(a, reverse=True):
		print(0)
		continue
	d = a[::-1]
	f = False
	for i in range(n - 1):
		if d[i + 1] < d[i]:
			f = True
			break
		if d[i + 1] > d[i]:
			f = False
			break
	if f == True:
		for i in range(n - 1):
			if d[i + 1] > d[i]:
				j = i
				break
		print(n - 1 - j)
	else:
		g = 0
		j = n - 1
		for i in range(n - 1):
			if d[i + 1] < d[i]:
				g = 1
			if d[i + 1] > d[i] and g == 1:
				j = i
				break
		print(n - 1 - j)


nCases = int(input())
for _ in range(nCases):
	aLen = int(input())
	a = [int(x) for x in input().split()]
	i = aLen - 1
	mem = 0
	while i >= 0:
		if a[i] < mem:
			break
		mem = a[i]
		i -= 1
	while i >= 0:
		if a[i] > mem:
			break
		mem = a[i]
		i -= 1
	print(i + 1)


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	c = 1
	f = False
	for i in range(n - 1, 0, -1):
		if not f:
			if a[i] <= a[i - 1]:
				c += 1
			else:
				c += 1
				f = True
		elif a[i] >= a[i - 1]:
			c += 1
		else:
			break
	print(n - c)


import sys
input = sys.stdin.readline
f = lambda : list(map(int, input().split()))
for _ in range(int(input())):
	n = int(input())
	a = f()
	p = n - 1
	while p and a[p - 1] >= a[p]:
		p -= 1
	while p and a[p - 1] <= a[p]:
		p -= 1
	print(p)


t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	decreased = False
	c = 0
	for j in range(n - 1, 0, -1):
		if l[j] <= l[j - 1]:
			if l[j] != l[j - 1] and decreased:
				break
			c += 1
		else:
			if not decreased:
				decreased = True
			c += 1
	c += 1
	print(n - c)


import sys
input = sys.stdin.readline

def inp():
	return int(input())

def inlt():
	return list(map(int, input().split()))

def insr():
	s = input()
	return s[:len(s) - 1]

def invr():
	return map(int, input().split())

def solve(t):
	pos = len(t) - 1
	while pos > 0 and t[pos] <= t[pos - 1]:
		pos -= 1
	while pos > 0 and t[pos] >= t[pos - 1]:
		pos -= 1
	print(pos)
T = inp()
tests = []
for _ in range(T):
	inp()
	tests.append(inlt())
for t in tests:
	solve(t)


import atexit
import io
import sys
_INPUT_LINES = sys.stdin.read().splitlines()
input = iter(_INPUT_LINES).__next__
_OUTPUT_BUFFER = io.StringIO()
sys.stdout = _OUTPUT_BUFFER

@atexit.register
def write():
	sys.__stdout__.write(_OUTPUT_BUFFER.getvalue())
for case in range(int(input())):
	n = int(input())
	a = [int(x) for x in input().split()]
	p = a[-1]
	for i in range(len(a) - 1, -1, -1):
		if a[i] < p:
			break
		p = a[i]
	p = a[i]
	for i in range(i, -1, -1):
		if a[i] > p:
			i += 1
			break
		p = a[i]
	print(i)


t = int(input())
for i in range(t):
	dlina = int(input())
	s = list(map(int, input().split()))
	s.reverse()
	i = 0
	p = 0
	while i < len(s) and s[i] >= p:
		p = s[i]
		i += 1
	while i < len(s) and s[i] <= p:
		p = s[i]
		i += 1
	print(len(s) - i)


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)


t = int(input())
for _ in range(t):
	n = int(input())
	arr = [int(i) for i in input().split()]
	if n == 1 or n == 2:
		print(0)
	else:
		i = n - 2
		while True and i >= 0:
			if arr[i] >= arr[i + 1]:
				i -= 1
			else:
				break
		while True and i >= 0:
			if arr[i] <= arr[i + 1]:
				i -= 1
			else:
				break
		print(i + 1)


for r in [*open(0)][2::2]:
	a = (*map(int, r.split()),)
	r = '0' + ''.join((str((x > y) - (x < y) + 1) for (x, y) in zip(a, a[1:])))
	print(max(0, r.rfind('2', 0, r.rfind('0'))))


from sys import stdin, stdout
from itertools import combinations
from collections import defaultdict, Counter
import math

def listIn():
	return list(map(int, stdin.readline().strip().split()))

def stringListIn():
	return [x for x in stdin.readline().split()]

def intIn():
	return int(stdin.readline())

def stringIn():
	return stdin.readline().strip()
t = intIn()
while t > 0:
	t -= 1
	n = intIn()
	a = listIn()
	for i in range(n - 1, -1, -1):
		if a[i] > a[i - 1]:
			break
	for j in range(i, -1, -1):
		if a[j] < a[j - 1]:
			break
	print(j)


import time, math as mt, bisect, sys
from sys import stdin, stdout
from collections import deque
from fractions import Fraction
from collections import Counter
from collections import OrderedDict
pi = 3.141592653589793

def II():
	return int(stdin.readline())

def IO():
	return stdin.readline()

def IP():
	return map(int, stdin.readline().split())

def L():
	return list(map(int, stdin.readline().split()))

def P(x):
	return stdout.write(str(x) + '\n')

def PI(x, y):
	return stdout.write(str(x) + ' ' + str(y) + '\n')

def lcm(a, b):
	return a * b // gcd(a, b)

def gcd(a, b):
	if a == 0:
		return b
	elif b == 0:
		return a
	if a > b:
		return gcd(a % b, b)
	else:
		return gcd(a, b % a)

def readTree():
	v = int(input())
	adj = [set() for i in range(v + 1)]
	for i in range(v - 1):
		(u1, u2) = In()
		adj[u1].add(u2)
		adj[u2].add(u1)
	return (adj, v)

def bfs(adj, v):
	visited = [False] * (v + 1)
	q = deque()
	while q:
		pass

def sieve():
	li = [True] * 1000001
	(li[0], li[1]) = (False, False)
	for i in range(2, len(li), 1):
		if li[i] == True:
			for j in range(i * i, len(li), i):
				li[j] = False
	prime = []
	for i in range(1000001):
		if li[i] == True:
			prime.append(i)
	return prime

def setBit(n):
	count = 0
	while n != 0:
		n = n & n - 1
		count += 1
	return count
mx = 10 ** 7
spf = [mx] * (mx + 1)

def SPF():
	spf[1] = 1
	for i in range(2, mx + 1):
		if spf[i] == mx:
			spf[i] = i
			for j in range(i * i, mx + 1, i):
				if i < spf[j]:
					spf[j] = i
	return
mod = 10 ** 9 + 7

def solve():
	n = II()
	li = L()
	a = [0] * n
	note = -99
	for i in range(n - 1, 0, -1):
		if li[i] > li[i - 1]:
			note = i - 1
			break
	if note == -99:
		print(0)
		return
	final = -99
	for i in range(note, 0, -1):
		if li[i] < li[i - 1]:
			final = i - 1
			break
	if final == -99:
		print(0)
		return
	print(final + 1)
	return
t = II()
for i in range(t):
	solve()


import os
import sys
from io import BytesIO, IOBase
import math

def main():
	pass
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
for xyz in range(0, int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	l.append(-1)
	p = -1
	fr = True
	fs = True
	for i in range(0, n - 1):
		if l[i] < l[i + 1]:
			fr = False
			break
	for i in range(0, n - 1):
		if l[i] > l[i + 1]:
			fs = False
			break
	if fr or fs:
		print(0)
	else:
		dp = n - 1
		ip = n - 1
		dp2 = n - 1
		for i in range(n - 1, 0, -1):
			if l[i] >= l[i - 1]:
				ip = i
			else:
				break
		for i in range(n - 1, 0, -1):
			if l[i] <= l[i - 1]:
				dp = i
			else:
				break
		for i in range(dp - 1, 0, -1):
			if l[i] >= l[i - 1]:
				dp2 = i
			else:
				break
		print(min(dp2, ip, dp) - 1)


for _ in range(int(input())):
	n = int(input())
	lst = list(map(int, input().split()))
	index = n - 1
	while index > 0 and lst[index - 1] >= lst[index]:
		index -= 1
	while index > 0 and lst[index - 1] <= lst[index]:
		index -= 1
	print(index)


import sys
from collections import defaultdict as dd
from collections import deque
from fractions import Fraction as f
from copy import *
from bisect import *
from heapq import *
from math import *
from itertools import permutations

def eprint(*args):
	print(*args, file=sys.stderr)
zz = 1
if zz:
	input = sys.stdin.readline
else:
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('all.txt', 'w')

def li():
	return [int(xx) for xx in input().split()]

def fli():
	return [float(x) for x in input().split()]

def comp(a, b):
	if a > b:
		return 2
	return 2 if a == b else 0

def gi():
	return [xx for x in input().split()]

def fi():
	return int(input())

def swap(a, i, j):
	(a[i], a[j]) = (a[j], a[i])

def si():
	return list(input().rstrip())

def mi():
	return map(int, input().split())

def gh():
	sys.stdout.flush()

def graph(n, m):
	for i in range(m):
		(x, y) = mi()
		a[x].append(y)
		a[y].append(x)

def bo(i):
	return ord(i) - ord('a')
t = fi()
while t > 0:
	t -= 1
	n = fi()
	a = li()
	pre = [0 for i in range(n)]
	suf = [0 for i in range(n)]
	for i in range(n):
		if i == 0:
			pre[i] = a[i]
		else:
			pre[i] = max(pre[i - 1], a[i])
	for i in range(n - 1, -1, -1):
		if i == n - 1:
			suf[i] = a[i]
		else:
			suf[i] = max(suf[i + 1], a[i])
	maxi = 0
	for i in range(n):
		if i == 0:
			continue
		elif i == n - 1:
			continue
		elif pre[i - 1] > a[i] and suf[i + 1] > a[i]:
			maxi = i
			pre[i] = a[i]
		else:
			pre[i] = max(pre[i - 1], a[i])
	print(maxi)


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


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	c = n - 2
	while c >= 0 and a[c] >= a[c + 1]:
		c -= 1
	while c >= 0 and a[c] <= a[c + 1]:
		c -= 1
	print(c + 1)


import sys
import math
import bisect
from sys import stdin, stdout
from math import gcd, floor, sqrt, log
from collections import defaultdict
from bisect import bisect_left as bl, bisect_right as br
from collections import Counter
ip = lambda : int(stdin.readline())
inp = lambda : map(int, stdin.readline().split())
t = ip()
for _ in range(t):
	n = ip()
	arr = list(inp())
	temp = list(arr)
	arr = arr[::-1]
	flag = True
	ch = arr[0]
	i = 0
	for i in range(1, n):
		if arr[i] >= ch:
			ch = arr[i]
		else:
			i -= 1
			break
	if i == n - 1:
		ans = 0
	else:
		ch = arr[i]
		for i in range(i + 1, n):
			if arr[i] <= ch:
				ch = arr[i]
			else:
				i -= 1
				break
		ans = n - i - 1
	print(ans)


t = int(input())
for _ in range(t):
	n = int(input())
	arr = [int(x) for x in input().split()]
	count = 1
	for i in range(len(arr) - 1, 0, -1):
		if arr[i] <= arr[i - 1]:
			count = count + 1
		else:
			break
	if count == len(arr):
		print(0)
		continue
	arr = arr[0:len(arr) - count]
	poit = 0
	for i in range(1, len(arr)):
		if arr[i] < arr[i - 1]:
			poit = i
	print(poit)


t = int(input())
from sys import *

def f(arr, k, x):
	count = 0
	for il in arr:
		count += il // x
	if count >= k:
		return True
	else:
		return False
for ip in range(t):
	n = int(input())
	arr = list(map(int, stdin.readline().split()))
	qr = []
	se = set()
	count = 0
	index = -1
	mins = arr[0]
	for ip in range(1, n - 1):
		if arr[ip] != arr[ip - 1]:
			mins = arr[ip - 1]
			index = ip
			if arr[ip] < mins and arr[ip] < arr[ip + 1]:
				count = ip
		elif arr[ip] < mins and arr[ip] < arr[ip + 1]:
			count = index
	print(count)


for _ in range(int(input())):
	n = int(input())
	a = [int(x) for x in input().split()]
	lastTrough = 0
	for i in range(1, n - 1):
		if a[i] <= a[i - 1] and a[i] < a[i + 1]:
			lastTrough = i
	while lastTrough > 0 and a[lastTrough] >= a[lastTrough - 1]:
		lastTrough -= 1
	print(lastTrough)


for _ in range(int(input())):
	n = int(input())
	array = list(map(int, input().split()))
	answer = n - 1
	while answer > 0 and array[answer] <= array[answer - 1]:
		answer -= 1
	while answer > 0 and array[answer] >= array[answer - 1]:
		answer -= 1
	print(answer)


import sys
input = sys.stdin.readline

def main():
	n = int(input())
	alst = list(map(int, input().split()))
	bef = 0
	flg = False
	ans = n
	for a in alst[::-1]:
		if not flg:
			if a < bef:
				flg = True
			ans -= 1
		else:
			if a > bef:
				break
			ans -= 1
		bef = a
	print(ans)
for _ in range(int(input())):
	main()


import sys

def input():
	return sys.stdin.readline().strip()

def list2d(a, b, c):
	return [[c] * b for i in range(a)]

def list3d(a, b, c, d):
	return [[[d] * c for j in range(b)] for i in range(a)]

def list4d(a, b, c, d, e):
	return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]

def ceil(x, y=1):
	return int(-(-x // y))

def INT():
	return int(input())

def MAP():
	return map(int, input().split())

def LIST(N=None):
	return list(MAP()) if N is None else [INT() for i in range(N)]

def Yes():
	print('Yes')

def No():
	print('No')

def YES():
	print('YES')

def NO():
	print('NO')
INF = 10 ** 19
MOD = 10 ** 9 + 7
for _ in range(INT()):
	N = INT()
	A = LIST()
	up = True
	ans = 0
	for i in range(N - 1, 0, -1):
		if up and A[i - 1] < A[i]:
			up = False
		elif not up and A[i - 1] > A[i]:
			ans = i
			break
	print(ans)


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	ans = [-1 for x in range(n)]
	if n == 1 or n == 2:
		print(0)
	else:
		max1 = l[n - 1]
		count = 1
		i = n - 2
		while i > -1 and max1 == l[i]:
			count += 1
			i -= 1
		if i > -1 and l[i] > max1:
			while i > -1 and l[i] >= max1:
				count += 1
				max1 = l[i]
				i -= 1
			while i > -1 and l[i] <= max1:
				count += 1
				max1 = l[i]
				i -= 1
		elif i > -1 and l[i] < max1:
			while i > -1 and l[i] <= max1:
				count += 1
				max1 = l[i]
				i -= 1
		print(n - count)


def solve():
	n = int(input())
	a = list(map(int, input().split()))
	i = n - 1
	while i > 0 and a[i - 1] >= a[i]:
		i -= 1
	while i > 0 and a[i - 1] <= a[i]:
		i -= 1
	print(i)
t = int(input())
for test in range(t):
	solve()


import sys
input = lambda : sys.stdin.readline()
exec("try:sys.stdin=open('in.txt','r');sys.stdout=open('out.txt','w')\nexcept:pass")
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	a = a[::-1]
	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)


t = int(input())
for i in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	count = 0
	maxi = 0
	for j in range(n - 1, -1, -1):
		if l[j] > l[j - 1] and maxi == 0:
			maxi = 1
		elif maxi == 1 and l[j] < l[j - 1]:
			break
	print(j)


import copy
t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	b = copy.copy(a)
	if b.sort() == a:
		print(0)
	else:
		j = n - 1
		while j - 1 >= 0 and a[j] <= a[j - 1]:
			j = j - 1
		while j - 1 >= 0 and a[j] >= a[j - 1]:
			j = j - 1
		print(j)


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	b = a[:]
	b.sort()
	if b == a:
		print(0)
	elif n == 1 or n == 2:
		print(0)
	else:
		j = 0
		cnt = 0
		flow = 1
		for i in range(1, n):
			if a[i - 1] > a[i]:
				flow = 0
				j = i
			elif a[i - 1] == a[i]:
				if a[i] != a[j]:
					j = i - 1
			elif a[i - 1] < a[i] and flow == 0:
				if a[i - 1] == a[j]:
					cnt = j
				else:
					cnt = i - 1
				flow = 1
		print(cnt)


def prefix(n, a):
	if n == 2 or n == 1:
		return 0
	x = min(a)
	y = max(a)
	if x == y:
		return 0
	var = 0
	for i in range(n - 1, 0, -1):
		if a[i - 1] < a[i]:
			var = 1
			m = i
			break
	if var == 0:
		return 0
	Var = 0
	for i in range(m, 0, -1):
		if a[i - 1] > a[i]:
			e = i
			Var = 1
			break
	if Var == 0:
		return 0
	return e
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	print(prefix(n, a))


for _ in range(int(input())):
	n = int(input())
	s = list(map(int, input().split()))
	k = list(s)
	k.sort()
	if k == s:
		print(0)
	else:
		inc = 1
		dec = 0
		count = 0
		for i in range(n - 1, 0, -1):
			if s[i] == s[i - 1]:
				count += 1
				continue
			if s[i] < s[i - 1] and inc:
				count += 1
				continue
			else:
				dec = 1
				inc = 0
			if s[i] > s[i - 1] and dec:
				count += 1
			else:
				break
		print(n - count - 1)


t = int(input())
for t1 in range(0, t):
	n = int(input())
	l = list(map(int, input().split()))
	i = 0
	j = n - 1
	while j > 0 and l[j] <= l[j - 1]:
		j = j - 1
	while j > 0 and l[j] >= l[j - 1]:
		j = j - 1
	print(j)


import math
from decimal import Decimal
import heapq
import copy
import heapq
from collections import deque

def na():
	n = int(input())
	b = [int(x) for x in input().split()]
	return (n, b)

def nab():
	n = int(input())
	b = [int(x) for x in input().split()]
	c = [int(x) for x in input().split()]
	return (n, b, c)

def dv():
	(n, m) = map(int, input().split())
	return (n, m)

def da():
	(n, m) = map(int, input().split())
	a = list(map(int, input().split()))
	return (n, m, a)

def dva():
	(n, m) = map(int, input().split())
	a = [int(x) for x in input().split()]
	b = [int(x) for x in input().split()]
	return (n, m, b)

def eratosthenes(n):
	sieve = list(range(n + 1))
	for i in sieve:
		if i > 1:
			for j in range(i + i, len(sieve), i):
				sieve[j] = 0
	return sorted(set(sieve))

def lol(lst, k):
	k = k % len(lst)
	ret = [0] * len(lst)
	for i in range(len(lst)):
		if i + k < len(lst) and i + k >= 0:
			ret[i] = lst[i + k]
		if i + k >= len(lst):
			ret[i] = lst[i + k - len(lst)]
		if i + k < 0:
			ret[i] = lst[i + k + len(lst)]
	return ret

def nm():
	n = int(input())
	b = [int(x) for x in input().split()]
	m = int(input())
	c = [int(x) for x in input().split()]
	return (n, b, m, c)

def dvs():
	n = int(input())
	m = int(input())
	return (n, m)

def fact(n):
	tc = []
	ans = {}
	d = 2
	while d * d <= n:
		if n % d == 0:
			tc.append(d)
			n //= d
		else:
			d += 1
	if n > 1:
		tc.append(n)
	for i in tc:
		ans[i] = ans.get(i, 0) + 1
	return ans
for _ in range(int(input())):
	(n, a) = na()
	i = n - 1
	while i > 0 and a[i - 1] >= a[i]:
		i -= 1
	while i > 0 and a[i - 1] <= a[i]:
		i -= 1
	print(i)


def read():
	return [int(xx) for xx in input().split()]
(ca,) = read()
for _ in range(ca):
	(n,) = read()
	x = read()
	n -= 1
	while n > 0 and x[n - 1] >= x[n]:
		n -= 1
	while n > 0 and x[n - 1] <= x[n]:
		n -= 1
	print(n)


for _ in range(int(input())):
	n = int(input())
	a = [*map(int, input().split())]
	if a == sorted(a):
		print(0)
	else:
		ans = 1
		increasing = 0
		decreasing = 0
		state = 1
		i = n - 1
		while i - 1 >= 0 and a[i] == a[i - 1]:
			i -= 1
		if i - 1 >= 0 and a[i] > a[i - 1]:
			while i - 1 >= 0 and a[i] >= a[i - 1]:
				i -= 1
			print(i)
		else:
			while i - 1 >= 0 and a[i] <= a[i - 1]:
				i -= 1
			while i - 1 >= 0 and a[i] >= a[i - 1]:
				i -= 1
			print(i)


t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	pos = n - 1
	while pos >= 1:
		if arr[pos - 1] >= arr[pos]:
			pos -= 1
		else:
			break
	while pos >= 1:
		if arr[pos - 1] <= arr[pos]:
			pos -= 1
		else:
			break
	print(pos)


for u in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	if l == sorted(l):
		print(0)
	else:
		c = 0
		i = n - 1
		while i > 0:
			if l[i] <= l[i - 1]:
				i = i - 1
				c = c + 1
			else:
				break
		x = i
		i = x
		while i > 0:
			if l[i] >= l[i - 1]:
				i = i - 1
				c = c + 1
			else:
				break
		y = i
		print(n - c - 1)


T = int(input())
for _ in range(T):
	n = int(input())
	arr = list(map(int, input().split()))
	if n <= 2:
		print(0)
	else:
		G = [0] * n
		stack = []
		for i in range(n - 1, -1, -1):
			while len(stack) > 0 and arr[stack[-1]] < arr[i]:
				stack.pop(-1)
			if len(stack) == 0:
				G[i] = -1
			else:
				G[i] = stack[-1]
			stack.append(i)
		if arr[-1] > arr[-2]:
			Max = n - 1
		else:
			Max = n - 2
		for i in range(n - 3, -1, -1):
			A = Max - i - 1
			B = G[i] - i - 1 if G[i] != -1 else -1
			if not (A == 0 or B == 0):
				i += 1
				break
			if arr[i] >= arr[Max]:
				Max = i
		print(i)


import heapq as hp
import collections
import bisect

def unpack(func=int):
	return map(func, input().split())

def l_unpack(func=int):
	return list(map(func, input().split()))

def s_unpack(func=int):
	return sorted(map(func, input().split()))

def range_n():
	return range(int(input()))

def getint():
	return int(input())

def main():
	for _ in range_n():
		n = getint()
		a = l_unpack()
		flag = 0
		prev = 0
		i = 0
		while i < n - 1:
			curr = i
			while i < n - 1 and a[i] == a[i + 1]:
				i += 1
			if i < n - 1 and a[prev] > a[curr] < a[i + 1]:
				flag = curr
			prev = curr
			i += 1
		print(flag)
main()


for _ in range(int(input())):
	n = int(input())
	nums = list(map(int, input().split()))
	if n == 1:
		print(0)
	else:
		ct = 0
		last = 0
		if len(nums) >= 2 and nums[-1] <= nums[-2]:
			for i in range(n - 1, -1, -1):
				c = nums[i]
				if c >= last:
					ct += 1
					last = c
				else:
					break
		last = last if last else 9999999
		if last != 9999999 and len(nums) >= 1 and (last >= nums[-1]) or (last == 9999999 and len(nums) >= 2 and (nums[-1] >= nums[-2])):
			for i in range(n - ct - 1, -1, -1):
				c = nums[i]
				if c <= last:
					ct += 1
					last = c
				else:
					break
		print(n - ct)


t = int(input())
while t:
	n = int(input())
	a = [int(i) for i in input().split()]
	last = n - 1
	while last > 0 and a[last] <= a[last - 1]:
		last -= 1
	while last > 0 and a[last] >= a[last - 1]:
		last -= 1
	print(last)
	t -= 1


for t in range(int(input())):
	n = int(input())
	listy = [int(k) for k in input().split()]
	flag = 0
	rng = 0
	for i in range(n - 1):
		if listy[n - 1 - i] != min(listy[n - 1 - i], listy[n - 2 - i]):
			rng = n - 2 - i
			break
	for ii in range(rng):
		if listy[rng - 1 - ii] != min(listy[rng - ii], listy[rng - 1 - ii]):
			flag = 1
			print(rng - ii)
			break
	if flag == 0:
		print(0)


t = int(input())
for i in range(t):
	n = int(input())
	p = list(map(int, input().strip(' ').split()))
	flag = 1
	j = n - 1
	while j >= 1:
		if flag:
			if p[j] > p[j - 1]:
				flag = 0
			j = j - 1
		else:
			if p[j] < p[j - 1]:
				print(j)
				break
			j = j - 1
	else:
		print(0)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	k = []
	l = 0
	c = 0
	if len(set(a)) == 1 or sorted(a) == a or sorted(a) == a[::-1]:
		print(0)
	else:
		r = n - 1
		while r > 0:
			if a[r] <= a[r - 1]:
				r = r - 1
			else:
				break
		c = r - 1
		while c > 0:
			if a[c - 1] <= a[c]:
				c = c - 1
			else:
				c = c
				break
		print(c)


import sys

def minp():
	return sys.stdin.readline().strip()

def mint():
	return int(minp())

def mints():
	return map(int, minp().split())

def solve():
	n = mint()
	a = list(mints())
	l = -1
	r = n - 1
	while r - l > 1:
		c = (l + r) // 2
		p = -1
		ll = c
		rr = n - 1
		ok = True
		while ll <= rr:
			if ll < rr and a[ll] < a[rr]:
				if a[ll] < p:
					ok = False
					break
				p = a[ll]
				ll += 1
			else:
				if a[rr] < p:
					ok = False
					break
				p = a[rr]
				rr -= 1
		if ok:
			r = c
		else:
			l = c
	print(r)
for i in range(mint()):
	solve()


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	q = -1
	try:
		while a[q - 1] >= a[q]:
			q -= 1
		while a[q - 1] <= a[q]:
			q -= 1
		print(n + q)
	except:
		print(0)


import sys

def solve():
	input()
	lst = [int(i) for i in input().split()]
	lst.reverse()
	inc = True
	for i in range(1, len(lst)):
		if lst[i] < lst[i - 1] and inc:
			inc = False
		elif lst[i] > lst[i - 1] and (not inc):
			print(len(lst) - i)
			return
	print(0)
for i in range(int(input())):
	solve()


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	changed = 0
	i = n - 2
	while i >= 0:
		if changed == False and a[i] < a[i + 1]:
			changed = True
		elif changed == True and a[i] > a[i + 1]:
			break
		i -= 1
	print(i + 1)


from sys import stdin, stdout
import math
from collections import Counter, deque
L = lambda : list(map(int, stdin.readline().strip().split()))
M = lambda : map(int, stdin.readline().strip().split())
I = lambda : int(stdin.readline().strip())
IN = lambda : stdin.readline().strip()
C = lambda : stdin.readline().strip().split()
mod = 1000000007

def s(a):
	print(' '.join(list(map(str, a))))

def solve():
	n = I()
	a = L()
	l = len(a)
	while l > 1:
		if a[-1] <= a[-2]:
			a.pop()
			l -= 1
		else:
			break
	while l > 1:
		if a[-1] >= a[-2]:
			a.pop()
			l -= 1
		else:
			break
	print(l - 1)
for _ in range(I()):
	solve()


import sys

def main():
	T = int(sys.stdin.readline())
	while T:
		T -= 1
		N = int(sys.stdin.readline())
		A = [int(i) for i in sys.stdin.readline().rstrip().split()]
		index = N - 1
		state = 0
		while index > 0:
			if state == 0:
				if A[index - 1] >= A[index]:
					index -= 1
				else:
					index -= 1
					state = 1
			if state == 1:
				if A[index - 1] <= A[index]:
					index -= 1
				else:
					break
		print(index if index >= 0 else 0)
main()


def check(a, l, r):
	x = 0
	while l < r:
		if a[l] < a[r]:
			y = a[l]
			l += 1
		else:
			y = a[r]
			r -= 1
		if y < x:
			return False
		x = y
	return True
t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(x) for x in input().split()]
	l = -1
	r = n
	while r - l > 1:
		m = (l + r) // 2
		if check(a, m, n - 1):
			r = m
		else:
			l = m
	print(l + 1)


for t in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	j = n - 1
	count = 0
	while j > 0:
		if arr[j] <= arr[j - 1]:
			count += 1
			j -= 1
		else:
			break
	if j == 0:
		count += 1
	else:
		i = j
		while i > 0:
			if arr[i] >= arr[i - 1]:
				count += 1
				i -= 1
			else:
				break
		if i == 0:
			count += 1
	if count == n:
		print(0)
	else:
		print(n - count - 1)


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	if n > 1:
		a = l[-1]
		b = l[-2]
		while a <= b and len(l) > 1:
			l.pop()
			if len(l) > 1:
				a = l[-1]
				b = l[-2]
	if len(l) > 1:
		while a >= b and len(l) > 1:
			l.pop()
			if len(l) > 1:
				a = l[-1]
				b = l[-2]
	print(len(l) - 1)


from collections import Counter
t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	l.reverse()
	i = 0
	while i < n - 1 and l[i + 1] >= l[i]:
		i += 1
	while i < n - 1 and l[i + 1] <= l[i]:
		i += 1
	print(n - i - 1)


import sys
import math
import collections
from sys import stdin, stdout, setrecursionlimit
import bisect as bs
setrecursionlimit(2 ** 20)
MOD = 998244353
T = int(stdin.readline())
for _ in range(T):
	n = int(stdin.readline())
	a = list(map(int, stdin.readline().split()))
	ind = 0
	for i in range(n - 1, 0, -1):
		if a[i - 1] < a[i]:
			ind = i
			break
	ans = 0
	for i in range(ind, 0, -1):
		if a[i - 1] > a[i]:
			ans = i
			break
	print(ans)


for tc in range(int(input())):
	n = int(input())
	lis = [int(i) for i in input().split()]
	minVal = 0
	p = 0
	q = n - 1
	ans = 0
	init = 0
	while p <= q:
		if lis[p] < lis[q] and lis[p] >= minVal:
			minVal = lis[p]
			p += 1
		elif lis[p] >= lis[q] and lis[q] >= minVal:
			minVal = lis[q]
			q -= 1
		else:
			if p == ans:
				p += 1
			ans = p
			minVal = 0
			q = n - 1
	print(ans)


from collections import deque
from collections import OrderedDict
import math
import sys
import os
import threading
import bisect
import operator
import heapq
from atexit import register
from io import BytesIO
import io
input = lambda : sys.stdin.readline().rstrip('\r\n')
for t in range(int(input())):
	n = int(input())
	ar = list(map(int, input().split()))
	if t == 878988989895:
		for i in range(n):
			print(str(ar).replace(' ', ''))
	if n == 1 or n == 2:
		print(0)
		continue
	answer = 0
	for i in range(n - 2, 0, -1):
		if ar[i] == ar[i - 1] and ar[i] < ar[i + 1]:
			ar[i] = ar[i + 1]
		if ar[i] < ar[i + 1] and ar[i] < ar[i - 1]:
			answer = i
			break
	print(answer)


t = int(input())
while t > 0:
	n = int(input())
	a = list(map(int, input().split()))
	if n > 1:
		if a[-1] > a[-2]:
			i = n - 1
			while i > 0 and a[i] >= a[i - 1]:
				i -= 1
		else:
			seen_peak = False
			i = n - 1
			while i > 0:
				if not seen_peak:
					if a[i] > a[i - 1]:
						seen_peak = True
					i -= 1
				elif a[i] >= a[i - 1]:
					i -= 1
				else:
					break
	else:
		i = 0
	ans = i
	print(ans)
	t -= 1


rr = lambda : input()
rri = lambda : int(input())
rrm = lambda : list(map(int, input().split()))
INF = float('inf')

def solve(N, A):
	end = N - 1
	for i in range(N - 2, -1, -1):
		if A[i] < A[i + 1]:
			break
		end = i
	for i in range(end - 1, -1, -1):
		if A[i] > A[i + 1]:
			return i + 1
	return 0
t = rri()
for _ in range(t):
	n = rri()
	arr = rrm()
	print(solve(n, arr))


testcases = int(input())
for test in range(testcases):
	n = int(input())
	l = list(map(int, input().strip().split(' ')))
	j = len(l) - 1
	if max(l) == min(l):
		print(0)
	elif l[j] > l[j - 1]:
		j = len(l) - 1
		while j > 0:
			if l[j] >= l[j - 1]:
				pass
			else:
				break
			j -= 1
		print(j)
	else:
		j = len(l) - 1
		while j > 0:
			if l[j - 1] >= l[j]:
				pass
			else:
				break
			j -= 1
		while j > 0:
			if l[j] >= l[j - 1]:
				pass
			else:
				break
			j -= 1
		print(j)


from sys import stdin, stdout, setrecursionlimit
t = int(stdin.readline())
for _ in range(t):
	n = int(stdin.readline())
	a = list(map(int, stdin.readline().split()))
	ck = 0
	for lst in range(n - 1, 0, -1):
		if a[lst - 1] < a[lst]:
			ck = lst
			break
	cont = 0
	for x in range(ck, 0, -1):
		if a[x - 1] > a[x]:
			cont = x
			break
	print(cont)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	ra = list(reversed(a))
	c = 0
	f = 0
	t = ra[0]
	for i in ra:
		if i >= t and f == 0:
			c += 1
			t = i
		elif i <= t:
			f = 1
			c += 1
			t = i
		else:
			break
	print(n - c)


t = int(input())
for _ in range(t):
	n = int(input())
	A = list(map(int, input().split()))
	A.reverse()
	B = []
	if n == 1:
		print(0)
		continue
	for i in range(n - 1):
		B.append(A[i + 1] - A[i])
	if B[0] < 0:
		for i in range(n - 1):
			if B[i] > 0:
				j = i
				print(n - 1 - j)
				break
		else:
			print(0)
	else:
		flag = True
		for i in range(n - 1):
			if not flag:
				if B[i] > 0:
					j = i
					print(n - 1 - j)
					break
			if B[i] < 0:
				flag = False
		else:
			print(0)


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	check = False
	index = 0
	for i in range(n - 1, 0, -1):
		if check == False:
			if a[i] > a[i - 1]:
				check = True
		elif a[i - 1] > a[i]:
			index = i
			break
	print(index)


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


for _ in range(int(input())):
	n = int(input())
	a = [int(__) for __ in input().split()]
	a.reverse()
	flag = 0
	j = 0
	shit = n
	for i in range(n - 1):
		if a[i] > a[i + 1]:
			j = i + 1
			flag = 1
			break
	for i in range(j, n - 1):
		if a[i] < a[i + 1] and flag == 1:
			flag = 2
			shit = i + 1
			break
	print(n - shit)


import sys
input = sys.stdin.readline
import math

def inlt():
	return list(map(int, input().split()))
t = inlt()
for i in range(t[0]):
	n = inlt()[0]
	a = inlt()
	b = 0
	c = 0
	e = 0
	for i in range(1, n):
		d = a[i] - a[i - 1]
		if c < 0 and d > 0:
			b = e
		if d != 0:
			c = d
			e = i
	print(b)


from sys import stdin, stdout

def isSorted(a, n):
	return all((a[i] <= a[i + 1] for i in range(n - 1)))

def isgood(a, n):
	if n == 1 or n == 2:
		return True
	c = list()
	j = n - 1
	i = 0
	k = 0
	while i != j:
		if a[i] <= a[j]:
			c.append(a[i])
			k += 1
			i += 1
		else:
			c.append(a[j])
			k += 1
			j -= 1
		if k != 1 and c[k - 1] < c[k - 2]:
			return False
	return True

def isgood2(a, n):
	pos = n - 1
	while pos > 0 and a[pos] <= a[pos - 1]:
		pos -= 1
	while pos > 0 and a[pos] >= a[pos - 1]:
		pos -= 1
	return pos

def main():
	t = int(stdin.readline().strip())
	for i in range(t):
		n = int(stdin.readline().strip())
		a = [int(x) for x in stdin.readline().strip().split()]
		ans = isgood2(a, n)
		stdout.write(str(ans) + '\n')
main()


import math

def gcd(a, b):
	if b == 0:
		return a
	return gcd(b, a % b)

def lcm(a, b):
	return a * b / gcd(a, b)

def bs(arr, l, r, x):
	while l <= r:
		mid = l + (r - l) // 2
		if arr[mid] == x:
			return arr[mid]
		elif arr[mid] < x:
			l = mid + 1
		else:
			r = mid - 1
	return -1

def swap(list, pos1, pos2):
	(list[pos1], list[pos2]) = (list[pos2], list[pos1])
	return list
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	if n == 0 or n == 1:
		print(0)
		continue
	x = a[n - 1]
	f = 0
	ans = n
	for i in range(n - 2, -1, -1):
		if a[i] >= x and f == 0:
			x = a[i]
		elif a[i] <= x:
			x = a[i]
			f = 1
		else:
			ans = i
			break
	if ans == n:
		print(0)
	else:
		print(ans + 1)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	piche = n - 1
	while piche > 0:
		if a[piche] <= a[piche - 1]:
			piche -= 1
		else:
			piche -= 1
			break
	flag = 0
	while piche > 0:
		if a[piche] >= a[piche - 1]:
			piche -= 1
		else:
			flag = 1
			break
	if flag:
		ans = piche
	else:
		ans = 0
	print(ans)


for _ in range(int(input())):
	n = int(input())
	a = [int(x) for x in input().split()]
	c = 1
	if sorted(a) == a:
		print(0)
		continue
	for i in range(n - 2, -1, -1):
		if a[i] >= a[i + 1]:
			c += 1
		else:
			break
	if i >= 0:
		c += 1
	for j in range(i - 1, -1, -1):
		if a[j] <= a[j + 1]:
			c += 1
		else:
			break
	if n - c >= 0:
		print(n - c)
	else:
		print(0)


t = int(input())
for i in range(t):
	n = int(input())
	a = [int(i) for i in input().split()]
	up = n - 1
	down = 0
	for j in range(len(a) - 2, -1, -1):
		if a[j] >= a[j + 1]:
			up -= 1
		else:
			down = j
			break
	for j in range(down - 1, -1, -1):
		if a[j] <= a[j + 1]:
			down -= 1
		else:
			break
	print(down)


for _ in range(int(input())):
	leng = int(input())
	lst = list(map(int, input().split()))[::-1]
	if leng == 1:
		print(0)
		continue
	(prev, idx) = (lst[0], leng)
	asc = lst[0] <= lst[1]
	for i in range(1, leng):
		if asc:
			if prev > lst[i]:
				asc = False
		elif prev < lst[i]:
			idx = i
			break
		prev = lst[i]
	print(leng - idx)


t = int(input())
for i in range(t):
	n = int(input())
	L = map(int, input().split())
	L = list(L)
	L.reverse()
	c = 0
	d = L[0]
	for i in L:
		if i >= d:
			c = c + 1
			d = i
		else:
			c = c + 1
			d = i
			break
	e = c
	for i in L[e:]:
		if i <= d:
			c = c + 1
			d = i
		else:
			break
	print(n - c)


def solution(x):
	k = len(x) - 2
	while k >= 0 and x[k] >= x[k + 1]:
		k -= 1
	while k > 0 and x[k] >= x[k - 1]:
		k -= 1
	return max(0, k)
N = int(input())
for _ in range(N):
	n = int(input())
	x = list(map(int, input().split()))
	print(solution(x))


def is_dicreasing_increasing_or_same(l, index):
	if l[index] > l[index + 1]:
		return 'increasing'
	elif l[index] < l[index + 1]:
		return 'decreasing'
	else:
		return 'same'
T = int(input())
for _ in range(T):
	n = int(input())
	l = [int(x) for x in input().split(' ')]
	prev_status = 'increasing'
	printed = False
	for index in range(n - 2, -1, -1):
		status = is_dicreasing_increasing_or_same(l, index)
		if status == 'increasing' and prev_status == 'decreasing':
			print(index + 1)
			printed = True
			break
		prev_status = status if status != 'same' else prev_status
	if not printed:
		print(0)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	peeks = 0
	up = True
	i = n - 2
	while i >= 0:
		if up:
			if a[i] < a[i + 1]:
				peeks += 1
				up = False
		elif a[i] > a[i + 1]:
			peeks += 1
			up = True
		if peeks > 1:
			break
		i -= 1
	print(i + 1)


t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(s) for s in input().split()]
	flag1 = True
	flag2 = True
	c = 0
	if n == 1:
		print(0)
	else:
		c = 1
		for i in range(n - 2, -1, -1):
			if flag1 and a[i] < a[i + 1]:
				flag1 = False
			if not flag1 and a[i] > a[i + 1]:
				break
			c += 1
		print(n - c)


n = int(input())
for i in range(n):
	x = int(input())
	l = list(map(int, input().split()))

	def solve(l, x):
		for j in range(x - 1, -1, -1):
			if j == 0:
				return -1
			elif l[j] > l[j - 1]:
				for k in range(j, -1, -1):
					if k == 0:
						return -1
					elif l[k] < l[k - 1] and k > 0:
						return k
	v = solve(l, x)
	if v == -1:
		print(0)
	else:
		print(v)


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


for item in range(int(input())):
	l = int(input())
	fin = list(map(int, input().split()))
	count = len(fin) - 1
	while count > 0 and fin[count - 1] >= fin[count]:
		count -= 1
	while count > 0 and fin[count - 1] <= fin[count]:
		count -= 1
	print(count)


for _ in range(int(input())):
	n = int(input())
	a = [int(i) for i in input().split()]
	pos = n - 1
	while pos >= 1 and a[pos - 1] >= a[pos]:
		pos -= 1
	while pos >= 1 and a[pos - 1] <= a[pos]:
		pos -= 1
	print(pos)


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().strip().split(' ')))
	if n == 1:
		print(0)
		continue
	i = n - 1
	ins = False
	while i > 0 and arr[i - 1] >= arr[i]:
		if arr[i - 1] > arr[i]:
			ins = True
		i -= 1
	if ins:
		while i > 0 and arr[i - 1] <= arr[i]:
			i -= 1
		print(i)
	else:
		i = n - 1
		while i > 0 and arr[i - 1] <= arr[i]:
			i -= 1
		print(i)


def function(n, nums):
	if len(nums) == 1:
		return 0
	if nums == sorted(nums) or nums == sorted(nums, reverse=True):
		return 0
	m1 = []
	nums1 = nums[::-1]
	total = 0
	for i in range(0, len(nums1) - 1):
		if nums1[i] <= nums1[i + 1]:
			total += 1
		else:
			total += 1
			break
	m1.append(total)
	total1 = 0
	for j in range(total, len(nums1)):
		if nums1[j] <= nums1[j - 1]:
			total1 += 1
		else:
			break
	m1.append(total1)
	return len(nums) - sum(m1)
tt1 = input()
tt = int(tt1)
l1 = []
for i in range(tt):
	n1 = input()
	n = int(n1)
	nums = list(map(int, input().rstrip().split()))
	print(function(n, nums))


def MakeitGood(arr, n):
	pos = n - 1
	while pos >= 1 and arr[pos] <= arr[pos - 1]:
		pos -= 1
	while pos >= 1 and arr[pos] >= arr[pos - 1]:
		pos -= 1
	return pos
test = int(input())
for i in range(test):
	n = int(input())
	arr = list(map(int, input().split()))
	print(MakeitGood(arr, n))


t = int(input())
teszt = []
hossz = []
for i in range(t):
	n = int(input())
	hossz.append(n)
	eset = [int(i) for i in input().split()]
	teszt.append(eset)
for i in range(t):
	r = 0
	z = 0
	k = 0
	kmax = 0
	h = hossz[i] - 1
	kesz = False
	while h != -1 and (not kesz):
		if z == 0:
			if r <= teszt[i][h]:
				k += 1
				r = teszt[i][h]
			else:
				z += 1
		if z == 1:
			if r >= teszt[i][h]:
				k += 1
				r = teszt[i][h]
			else:
				z += 1
		if z == 2:
			kesz = True
		h -= 1
	print(hossz[i] - k)


t = int(input())
for _ in range(t):
	k = int(input()) - 1
	arr = [int(i) for i in input().split()]
	check = 0
	prev = arr[k]
	k -= 1
	while k >= 0:
		if arr[k] < prev:
			check = 1
		elif arr[k] > prev and check == 1:
			break
		prev = arr[k]
		k -= 1
	print(k + 1)


import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	l = n - 1
	while a[l] <= a[l - 1]:
		if l < 1:
			break
		l -= 1
	while a[l - 1] <= a[l]:
		if l < 1:
			break
		l -= 1
	print(l)


for _ in range(int(input())):
	x = int(input())
	mylist = list(map(int, input().split()))
	pos = x - 1
	while pos > 0 and mylist[pos - 1] >= mylist[pos]:
		pos -= 1
	while pos > 0 and mylist[pos - 1] <= mylist[pos]:
		pos -= 1
	print(pos)


for _ in range(int(input())):
	n = int(input())
	a = [int(x) for x in input().split()]
	pos = n - 1
	while pos != 0 and a[pos - 1] >= a[pos]:
		pos -= 1
	while pos != 0 and a[pos - 1] <= a[pos]:
		pos -= 1
	print(pos)


import sys
input = sys.stdin.readline

def main():
	for t in range(int(input())):
		n = int(input())
		a = list(map(int, input().split()))
		x = 0
		i = n - 1
		y = 0
		while i > 0 and a[i - 1] >= a[i]:
			i -= 1
			x += 1
			if a[i - 1] == a[i]:
				y += 1
			else:
				y = 0
		while i > 0 and a[i - 1] <= a[i]:
			i -= 1
			y += 1
		print(max(0, n - 1 - x - y))
main()


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


import sys

def solve(xs):
	i = len(xs) - 1
	while i > 0 and xs[i - 1] >= xs[i]:
		i -= 1
	while i > 0 and xs[i - 1] <= xs[i]:
		i -= 1
	return i
next(sys.stdin)
while True:
	if next(sys.stdin, None) is None:
		break
	ln = next(sys.stdin)
	xs = list(map(int, ln.split()))
	print(solve(xs))


import sys
import math

def pro(arr):
	n = len(arr)
	if n <= 2:
		print(0)
		return
	i = n - 1
	while i > 0 and arr[i - 1] >= arr[i]:
		i -= 1
	while i > 0 and arr[i - 1] <= arr[i]:
		i -= 1
	ans = i
	print(ans)
t = int(input())
for i in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	pro(arr)


t = int(input())
while t:
	t = t - 1
	n = int(input())
	a = list(map(int, input().split()))
	if n <= 2:
		print(0)
	else:
		j = n - 1
		while j > 0 and a[j - 1] >= a[j]:
			j -= 1
		while j > 0 and a[j - 1] <= a[j]:
			j -= 1
		print(j)


import sys
reader = (s.rstrip() for s in sys.stdin)
input = reader.__next__
t = int(input())

def check(mid, arr):
	l = mid
	r = len(arr) - 1
	c = []
	while l <= r:
		if arr[l] <= arr[r]:
			c.append(arr[l])
			l += 1
		else:
			c.append(arr[r])
			r -= 1
	return c == sorted(c)
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	if n <= 2:
		print(0)
		continue
	(low, high) = (0, n - 1)
	while low < high:
		mid = (low + high) // 2
		if check(mid, arr):
			high = mid
		else:
			low = mid + 1
	print(low)


import math
import datetime
import collections
import statistics
import itertools

def is_prime(num):
	for i in range(2, int(math.sqrt(num)) + 1):
		if num % i == 0:
			return False
	return True

def input_list():
	ll = list(map(int, input().split(' ')))
	return ll
tc = int(input())
for _ in range(tc):
	n = int(input())
	arr = input_list()
	x = arr[::-1]
	c = 0
	l = n
	for i in range(1, n):
		if c == 0:
			if x[i] < x[i - 1]:
				c += 1
		if c == 1:
			if x[i] > x[i - 1]:
				break
		l -= 1
	print(l - 1)


for _ in range(int(input())):
	d = int(input())
	arr = list(map(int, input().split()))
	i = d - 2
	while i > -1 and arr[i] >= arr[i + 1]:
		i = i - 1
	while i > -1 and arr[i] <= arr[i + 1]:
		i = i - 1
	print(i + 1)


for _ in range(int(input())):
	n = int(input())
	arr = [int(x) for x in input().split()]
	flag = 1
	val = arr[-1]
	i = n - 2
	while i >= 0 and arr[i] >= val:
		val = arr[i]
		i -= 1
	if i == -1:
		print(0)
		continue
	while i >= 0 and arr[i] <= val:
		val = arr[i]
		i -= 1
	if i == -1:
		print(0)
		continue
	print(i + 1)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	i = 0
	j = n - 1
	p = 0
	k = []
	while i < n - 1 and j > i:
		if a[i] >= a[j]:
			if len(k) == 0 or a[j] >= k[-1]:
				k.append(a[j])
				j -= 1
			elif a[j] < k[-1]:
				k = []
				i += 1
				p = i
				j = n - 1
				continue
		elif len(k) == 0 or a[i] >= k[-1]:
			k.append(a[i])
			i += 1
		elif a[i] < k[-1]:
			k = []
			p = i
			j = n - 1
			continue
	else:
		print(p)


import math
from collections import *
import sys
from functools import reduce
from itertools import groupby

def get_ints():
	return map(int, input().strip().split())

def get_list():
	return list(get_ints())

def get_string():
	return list(input().strip().split())

def printxsp(*args):
	return print(*args, end='')

def printsp(*args):
	return print(*args, end=' ')
DIRECTIONS = [[0, 1], [0, -1], [1, 0], [1, -1]]
NEIGHBOURS = [(i, j) for i in range(-1, 2) for j in range(-1, 2) if i != 0 or j != 0]
OrdUnicode_a = ord('a')
OrdUnicode_A = ord('A')
CAPS_ALPHABETS = {chr(i + OrdUnicode_A): i for i in range(26)}
SMOL_ALPHABETS = {chr(i + OrdUnicode_a): i for i in range(26)}
MOD_JOHAN = int(1000000000.0) + 7
MOD_LIGHT = 998244353
INFINITY = float('inf')
MAXN_EYEPATCH = int(100000.0) + 1
MAXN_FULLMETAL = 501
for _testcases_ in range(int(input())):
	n = int(input())
	li = get_list()
	r = n - 1
	while r > 0:
		if li[r] > li[r - 1]:
			flag1 = False
			break
		r -= 1
	if r == 0 or r == 1:
		print(0)
	else:
		while r > 0:
			if li[r] < li[r - 1]:
				flag2 = False
				break
			r -= 1
		if r == 0:
			print(0)
		else:
			print(r)


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


from sys import stdin, stdout
input = stdin.readline
import random
t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(x) for x in input().split()]
	a = a[::-1]
	start = a[0]
	x = 0
	ans = 0
	for i in range(1, n):
		if x == 0:
			if a[i] < start:
				x = 1
		elif a[i] > start:
			ans = n - i
			break
		start = a[i]
	print(ans)


for i in range(int(input())):
	n = int(input())
	a = [int(i) for i in input().split()]
	c = 1
	go = True
	for i in range(1, n):
		if a[-(i + 1)] >= a[-i] and go:
			c += 1
			continue
		else:
			go = False
		if a[-(i + 1)] <= a[-i]:
			c += 1
			go = False
		else:
			break
	print(n - c)


t = int(input())
for _ in range(0, t):
	input()
	a = list(map(int, input().split()))
	increase = True
	for i in range(0, len(a)):
		if a[len(a) - i - 1] > a[len(a) - i - 2] and increase:
			increase = False
		if a[len(a) - i - 1] < a[len(a) - i - 2] and (not increase):
			print(len(a) - i - 1)
			break
		if i == len(a) - 1:
			print(0)


import sys, bisect
input = sys.stdin.readline
T = int(input())
for _ in range(T):
	n = int(input())
	A = list(map(int, input().split()))
	flag = 0
	ind = 0
	for i in range(n - 2, -1, -1):
		if A[i] < A[i + 1] and flag == 0:
			flag = 1
		elif A[i] > A[i + 1] and flag == 1:
			ind = i + 1
			break
	print(ind)


for _ in range(int(input())):
	a = int(input())
	b = list(map(int, input().split()))
	b.reverse()
	(v, ans) = (0, 1)
	for i in range(1, a):
		if b[i] < b[i - 1]:
			v = 1
		elif b[i] == b[i - 1]:
			pass
		elif v == 1:
			break
		ans += 1
	print(a - ans)


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	k = n - 1
	if n == 1:
		print('0')
	else:
		while k > 0:
			if arr[k] > arr[k - 1]:
				break
			k -= 1
		while k > 0:
			if arr[k] < arr[k - 1]:
				break
			k -= 1
		print(str(k))


import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(_) for _ in input().split()]
	if n == 1 or n == 2:
		print(0)
		continue
	MODE = 0
	flag = True
	for i in range(n - 1):
		if MODE == 0:
			if a[i] < a[i + 1]:
				MODE = 1
			elif a[i] > a[i + 1]:
				MODE = -1
		elif MODE == 1:
			if a[i] > a[i + 1]:
				flag = False
				break
		elif a[i] < a[i + 1]:
			flag = False
			break
	if flag:
		print(0)
		continue
	ans = 0
	MODE2 = 0
	MODE3 = 0
	for i in range(n - 1, 0, -1):
		if MODE2 == 0:
			if a[i - 1] < a[i]:
				MODE2 = 1
			elif a[i - 1] > a[i]:
				MODE2 = -1
		elif MODE2 == 1:
			if a[i - 1] > a[i]:
				ans = i
				break
		elif MODE3 == 0:
			if a[i - 1] < a[i]:
				MODE3 = 1
		elif a[i - 1] > a[i]:
			ans = i
			break
	print(ans)


testcases = int(input())
for testcase in range(testcases):
	n = int(input())
	temparr = input()
	temparr = temparr.split()
	arr = []
	for i in temparr:
		arr.append(int(i))
	mins = len(arr)
	left = 0
	right = n
	while left <= right:
		mid = (left + right) // 2
		cur = []
		midleft = mid
		midright = n - 1
		flag = 0
		while midleft < midright:
			leftelem = arr[midleft]
			rightelem = arr[midright]
			if len(cur) == 0:
				if leftelem <= rightelem:
					cur.append(leftelem)
					midleft += 1
				else:
					cur.append(rightelem)
					midright -= 1
				continue
			if cur[-1] > leftelem or cur[-1] > rightelem:
				flag = 1
				break
			if cur[-1] <= leftelem and leftelem <= rightelem:
				midleft += 1
				cur.append(leftelem)
			else:
				midright -= 1
				cur.append(rightelem)
		if flag == 0:
			newans = mid
			if mins > newans:
				mins = newans
			right = mid - 1
		else:
			left = mid + 1
	print(mins)


T = int(input())
for _ in range(T):
	N = int(input())
	A = list(map(int, input().split()))
	should_be_increasing = True
	cutoff = 0
	for i in range(N - 1, 0, -1):
		if should_be_increasing:
			if A[i] > A[i - 1]:
				should_be_increasing = False
				continue
		elif A[i] < A[i - 1]:
			cutoff = i
			break
	print(cutoff)


t = int(input())
ans = []
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	li = []
	li.append(0 - a[0])
	b = sorted(a)
	c = sorted(a, reverse=True)
	if b == a or a == c:
		ans.append(0)
		continue
	for j in range(1, len(a)):
		li.append(a[j] - a[j - 1])
	if len(li) != 0:
		while len(li) > 0 and li[-1] <= 0 and 1:
			li.pop()
		while len(li) > 0 and li[-1] >= 0 and 1:
			li.pop()
		ans.append(len(li) - 1)
for an in ans:
	print(an)


import sys
inp = [int(x) for x in sys.stdin.read().split()]
ii = 0
t = inp[0]
ii += 1
for _ in range(t):
	n = inp[ii]
	ii += 1
	a = inp[ii:ii + n]
	ii += n
	(lo, hi) = (1, n)
	while lo < hi:
		mid = (lo + hi + 1) // 2
		check = 1
		(lt, rt) = (n - mid, n - 1)
		prev = -1
		while lt <= rt:
			if a[lt] <= a[rt]:
				if a[lt] >= prev:
					prev = a[lt]
					lt += 1
				else:
					check = 0
					break
			elif a[rt] >= prev:
				prev = a[rt]
				rt -= 1
			else:
				check = 0
				break
		if check == 1:
			lo = mid
		else:
			hi = mid - 1
	print(n - lo)


try:
	t = int(input())
	for _ in range(t):
		n = int(input())
		a = [int(x) for x in input().split()]
		pos = n - 1
		while pos > 0 and a[pos - 1] >= a[pos]:
			pos -= 1
		while pos > 0 and a[pos - 1] <= a[pos]:
			pos -= 1
		print(pos)
except:
	pass


t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(x) for x in input().split()]
	moves = n - 1
	while moves > 0 and a[moves - 1] >= a[moves]:
		moves -= 1
	while moves > 0 and a[moves - 1] <= a[moves]:
		moves -= 1
	print(moves)


import sys
import os
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
import math
import random
import re
import heapq
t = int(input())
while t:
	t -= 1
	n = int(input())
	arr = list(map(int, input().split()))
	if len(arr) < 3:
		print(0)
		continue
	dive = False
	i = len(arr) - 1
	while i > 0:
		if arr[i] > arr[i - 1]:
			dive = True
		elif dive and arr[i] < arr[i - 1]:
			break
		i -= 1
	print(i)


for _ in range(int(input())):
	n = int(input())
	arr = [int(x) for x in input().split()]
	inc = 0
	dec = 0
	temp = -1
	for i in range(n - 1):
		if inc == 0 and dec == 0:
			if arr[n - 1 - i] < arr[n - i - 2]:
				inc = 1
			elif arr[n - 1 - i] > arr[n - i - 2]:
				dec = 1
		elif inc == 1:
			if arr[n - 1 - i] > arr[n - i - 2]:
				dec = 1
				inc = 0
		elif dec == 1:
			if arr[n - 1 - i] < arr[n - i - 2]:
				temp = i
				break
	if temp == -1:
		print(0)
	else:
		print(n - temp - 1)


input = __import__('sys').stdin.readline
t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	if a == sorted(a):
		print(0)
		continue
	dec = False
	inc = True
	yep = 0
	for i in range(len(a) - 1, -1, -1):
		if a[i] > a[i - 1] and (not dec):
			inc = False
			dec = True
		elif a[i] < a[i - 1] and dec:
			yep = i
			break
	print(yep)


for _ in range(int(input())):
	p = int(input()) - 1
	a = list(map(int, input().split()))
	if a == sorted(a):
		print('0')
	elif a == sorted(a)[::-1]:
		print('0')
	else:
		while p >= 1 and a[p - 1] >= a[p]:
			p -= 1
		while p >= 1 and a[p - 1] <= a[p]:
			p -= 1
		print(len(a) - len(a[p:]))


ii = lambda : int(input())
t = ii()
for h in range(t):
	n = ii()
	a = list(map(int, input().split()))
	c1 = 0
	i = n - 1
	while i > 0:
		if a[i] <= a[i - 1]:
			c1 = c1 + 1
			i = i - 1
		else:
			break
	if i == 0:
		c1 += 1
	elif i != 0:
		j = i
		while j > 0:
			if a[j] >= a[j - 1]:
				c1 = c1 + 1
				j = j - 1
			else:
				break
		if j == 0:
			c1 += 1
	if c1 != n:
		print(n - c1 - 1)
	else:
		print(0)


import sys
input = sys.stdin.buffer.readline

def solution():
	for _ in range(int(input())):
		n = int(input())
		a = list(map(int, input().split()))
		ans = 0
		inc = 0
		dec = 0
		i = n - 1
		j = n - 1
		k = 0
		x = 0
		for i in range(n - 1, 0, -1):
			flag = 0
			if a[i] <= a[i - 1]:
				j = i - 1
				continue
			else:
				break
		for i in range(j, 0, -1):
			if a[i] >= a[i - 1]:
				k = i - 1
				continue
			else:
				break
		print(k)
solution()


from sys import stdin, stdout, setrecursionlimit
t = int(stdin.readline())
for _ in range(t):
	n = int(stdin.readline())
	a = list(map(int, stdin.readline().split()))
	chk = 0
	for i in range(n - 1, 0, -1):
		if a[i - 1] < a[i]:
			chk = i
			break
	cnt = 0
	for i in range(chk, 0, -1):
		if a[i - 1] > a[i]:
			cnt = i
			break
	print(cnt)


def main():
	t = int(input())
	for case in range(t):
		n = int(input())
		arr = [int(x) for x in input().split()]
		i = n - 2
		flag = True
		while i > -1:
			if flag:
				if arr[i] >= arr[i + 1]:
					i -= 1
				else:
					flag = False
			elif arr[i] <= arr[i + 1]:
				i -= 1
			else:
				break
		print(i + 1)
main()


for i in range(int(input())):
	m = int(input())
	l = [int(x) for x in input().split()]
	ind = m - 1
	cnt = -1
	while ind != 0:
		if cnt == -1 and l[ind - 1] >= l[ind]:
			ind -= 1
			continue
		elif cnt == -1 and l[ind - 1] < l[ind]:
			cnt = 0
			ind -= 1
			continue
		elif cnt != -1:
			if l[ind] >= l[ind - 1]:
				ind -= 1
				continue
			else:
				cnt = ind
				break
	if cnt == -1:
		print(0)
	else:
		print(cnt)


from sys import *
from bisect import bisect_left
input = lambda : stdin.readline()
int_arr = lambda : list(map(int, stdin.readline().strip().split()))
str_arr = lambda : list(map(str, stdin.readline().split()))
get_str = lambda : map(str, stdin.readline().strip().split())
get_int = lambda : map(int, stdin.readline().strip().split())
get_float = lambda : map(float, stdin.readline().strip().split())
mod = 1000000007
setrecursionlimit(1000)
for _ in range(int(input())):
	n = int(input())
	arr = int_arr()
	arr = arr[::-1]
	prev = 0
	ind = -1
	for i in range(n):
		if arr[i] >= prev:
			prev = arr[i]
			ind = i
		else:
			break
	for i in range(ind, n):
		if arr[i] <= prev:
			prev = arr[i]
			ind = i
		else:
			break
	print(n - 1 - ind)


import os
import sys
from io import BytesIO, IOBase
import threading
from bisect import bisect_right
from math import gcd, log
from collections import Counter

def main():
	for _ in range(int(input())):
		n = int(input())
		arr = list(map(int, input().split()))
		i = n - 2
		while i >= 0 and arr[i] >= arr[i + 1]:
			i -= 1
		while i >= 0 and arr[i] <= arr[i + 1]:
			i -= 1
		print(i + 1)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


from sys import stdin, stdout
import heapq
import cProfile
from collections import Counter, defaultdict, deque
from functools import reduce
import math

def get_int():
	return int(stdin.readline().strip())

def get_tuple():
	return map(int, stdin.readline().split())

def get_list():
	return list(map(int, stdin.readline().split()))

def solve():
	n = int(input())
	ls = get_list()
	if n == 1:
		print('0')
		return
	i = n - 1
	while i > 0:
		if ls[i - 1] < ls[i]:
			break
		i -= 1
	while i > 0:
		if ls[i - 1] > ls[i]:
			break
		i -= 1
	print(i)

def main():
	solve()
TestCases = True
if TestCases:
	for i in range(get_int()):
		main()
else:
	main()


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


for _ in range(int(input())):
	n = int(input())
	a = [int(i) for i in input().split()]
	a.reverse()
	cnt = 1
	cur = a[0]
	inc = 1
	for i in a[1:]:
		if i > cur and inc in [1, 2]:
			cnt += 1
			inc = 2
		elif i < cur and inc in [1, 2, 3]:
			cnt += 1
			inc = 3
		elif i == cur:
			cnt += 1
		else:
			break
		cur = i
	print(n - cnt)


from collections import Counter
testCases = int(input())
for t in range(testCases):
	n = int(input())
	nums = list(map(int, input().split(' ')))
	index = len(nums) - 1
	while index > 0:
		if nums[index] > nums[index - 1]:
			break
		index -= 1
	while index > 0:
		if nums[index] < nums[index - 1]:
			break
		index -= 1
	print(index)


cases = int(input())
for i in range(cases):
	n = int(input())
	arr = [int(x) for x in input().split()]
	ans = n - 1
	while ans > 0 and arr[ans - 1] >= arr[ans]:
		ans -= 1
	while ans > 0 and arr[ans - 1] <= arr[ans]:
		ans -= 1
	print(ans)


from sys import stdin, stdout
from os import path
if path.exists('input.txt'):
	stdin = open('input.txt', 'r')
	stdout = open('output.txt', 'w')

	def I():
		return int(input())

	def In():
		return map(int, input().split())
else:

	def I():
		return int(stdin.readline())

	def In():
		return map(int, stdin.readline().split())

	def S():
		return stdin.readline().rstrip()

	def Sn():
		return stdin.readline().split(' ')

def Out(whatever):
	return stdout.write(whatever)

def func(ar):
	n = len(ar)
	i = n - 1
	while i > 0 and ar[i] <= ar[i - 1]:
		i -= 1
	while i > 0 and ar[i] >= ar[i - 1]:
		i -= 1
	return str(i)
t = I()
answers = ['  '] * t
for i in range(t):
	n = I()
	arr = list(In())
	answers[i] = func(arr)
Out('\n'.join(answers).lstrip())


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	if n == 1 or n == 2:
		print(0)
		continue
	b = False
	c = 1
	for i in range(n - 2, 0, -1):
		while i - c >= 0 and arr[i] == arr[i - c]:
			c += 1
		if arr[i] < arr[i + 1] and arr[i] < arr[i - c]:
			print(i - c + 1)
			b = True
			break
		c = 1
	if not b:
		print(0)


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	t = l[n - 1]
	idx = 0
	f = 0
	for i in range(n - 1, -1, -1):
		if l[i] >= t:
			t = l[i]
		else:
			idx = i + 1
			break
	t = l[idx]
	for i in range(idx, -1, -1):
		if l[i] <= t:
			t = l[i]
			if i == 0:
				f = 1
		else:
			idx = i + 1
			break
	if f == 1:
		print(0)
	else:
		print(idx)


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


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


import math
for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	l.reverse()
	f = 0
	ind = -1
	for i in range(n - 1):
		if l[i] < l[i + 1]:
			if f == 1:
				ind = i
				break
		elif l[i] == l[i + 1]:
			pass
		elif l[i] > l[i + 1]:
			f = 1
	if ind != -1:
		print(n - 1 - ind)
	else:
		print(0)


t = int(input())
for _ in range(t):
	n = int(input())
	S = list(map(int, input().split()))
	ans = 0
	while True:
		x = S.index(max(S))
		S1 = sorted(S[x:])
		S1.reverse()
		if S1 != S[x:]:
			S = S[x + 1:]
			ans += x + 1
		else:
			break
	x = S.index(max(S))
	pos = x
	while pos > 0:
		if S[pos] >= S[pos - 1]:
			pos -= 1
		else:
			print(pos + ans)
			break
	else:
		print(ans)


def esGood(arreglo):
	d = 1
	c = 1
	m = 1
	i = len(arreglo) - 2
	j = len(arreglo) - 2
	while i > -1:
		if arreglo[i] >= arreglo[i + 1]:
			d += 1
			i -= 1
		else:
			break
	while j > -1:
		if arreglo[j] <= arreglo[j + 1]:
			c += 1
			j -= 1
		else:
			break
	k = len(arreglo) - 2
	while k > -1:
		if arreglo[k] >= arreglo[k + 1]:
			m += 1
			k -= 1
		else:
			break
	while k > -1:
		if arreglo[k] <= arreglo[k + 1]:
			m += 1
			k -= 1
		else:
			break
	r = max(d, c, m)
	return len(arreglo) - r

def main():
	t = int(input())
	for i in range(t):
		n = input()
		arreglo = list(map(int, input().split()))
		resultado = esGood(arreglo)
		print(resultado)
main()


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	d = {}
	b = sorted(a)
	for (i, x) in enumerate(b):
		d[x] = d.get(x, [])
		d[x] += (i,)
	i = 0
	j = n - 1
	c = 0
	while i < j:
		while b[c] == -1 and c < n:
			c += 1
		if c == n:
			break
		if a[j] == b[c]:
			j -= 1
			c += 1
		elif a[i] == b[c]:
			i += 1
			c += 1
		else:
			x = a[i]
			a[i] = -1
			i += 1
			b[d[x][-1]] = -1
			d[x].pop()
	ans = 0
	i = 0
	while i < n:
		if a[i] == -1:
			ans = i + 1
		i += 1
	print(ans)


for _ in range(int(input())):
	input()
	v = [int(v) for v in input().split(' ')]
	i = len(v) - 2
	while i >= 0 and v[i] >= v[i + 1]:
		i -= 1
	while i >= 0 and v[i] <= v[i + 1]:
		i -= 1
	print(i + 1)


def check(a):
	k = 0
	for i in range(1, len(a) - 1):
		if a[i - 1] > a[i]:
			f = i
			while a[i + 1] == a[i] and i < len(a) - 2:
				i += 1
			if a[i + 1] > a[i]:
				k = f
	return k
for _ in range(int(input())):
	input()
	print(check(list(map(int, input().split()))))


t = int(input())
for i in range(t):
	n = int(input())
	a = [int(v) for v in input().split()]
	j = n - 1
	dic = 0
	inc = 0
	while j >= 1 and a[j - 1] >= a[j]:
		j = j - 1
	while j >= 1 and a[j - 1] <= a[j]:
		j = j - 1
	print(j)


import sys
import math
from collections import defaultdict, Counter
t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	flag = 0
	ind = 0
	for j in range(n - 2, -1, -1):
		if a[j] <= a[j + 1]:
			if a[j] < a[j + 1]:
				flag = 1
		elif flag == 1:
			ind = j + 1
			break
	print(ind)


from sys import stdin, stdout
int_in = lambda : int(stdin.readline())
arr_in = lambda : [int(x) for x in stdin.readline().split()]
mat_in = lambda rows: [arr_in() for _ in range(rows)]
str_in = lambda : stdin.readline().strip()
out = lambda o: stdout.write('{}\n'.format(o))
arr_out = lambda o: out(' '.join(map(str, o)))
bool_out = lambda o: out('YES' if o else 'NO')
tests = lambda : range(1, int_in() + 1)
case_out = lambda i, o: out('Case #{}: {}'.format(i, o))

def solve(n, a):
	prev = a[-1]
	return_next_flip = False
	for (i, item) in enumerate(reversed(a)):
		if item < prev:
			return_next_flip = True
		elif return_next_flip and item > prev:
			return len(a) - i
		prev = item
	return 0
for _ in tests():
	n = int_in()
	a = arr_in()
	out(solve(n, a))


def main():
	t = int(input())
	for _ in range(t):
		n = int(input())
		a = list(map(int, input().split()))
		i = len(a) - 1
		turn = False
		while i > 0:
			if turn:
				if a[i - 1] > a[i]:
					break
			elif a[i - 1] < a[i]:
				turn = True
			i -= 1
		print(i)
main()


import sys
input = sys.stdin.readline
Q = int(input())
for _ in range(Q):
	N = int(input())
	A = list(map(int, input().split()))
	if N == 1:
		ans = 0
	else:
		pre = A[-2]
		ans = N - 2
		up = A[-1] <= A[-2]
		for a in reversed(A[:-2]):
			if up:
				if a < pre:
					up = False
			elif a > pre:
				break
			pre = a
			ans -= 1
	print(ans)


import sys
import bisect as bi
import math
import string
from collections import defaultdict as dd
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 6)

def cin():
	return map(int, sin().split())

def ain():
	return list(map(int, sin().split()))

def sin():
	return input()

def inin():
	return int(input())
import sys
import bisect as bi
import math
import sys
import bisect as bi
import math
from collections import defaultdict as dd
input = sys.stdin.readline

def cin():
	return map(int, sin().split())

def ain():
	return list(map(int, sin().split()))

def sin():
	return input()

def inin():
	return int(input())
for _ in range(inin()):
	n = inin()
	l1 = ain()
	if n <= 2:
		print(0)
	else:
		cur = l1[0]
		l = [(cur, 0)]
		temp = [0] * n
		c = 0
		for i in range(1, n):
			if l1[i] == cur:
				temp[i] = temp[i - 1] + 1
				continue
			else:
				l += [(l1[i], temp[i - 1])]
				cur = l1[i]
				temp[i] = temp[i - 1]
		ans = 0
		for i in range(1, len(l) - 1):
			if l[i - 1][0] > l[i][0] and l[i + 1][0] > l[i][0]:
				ans = i + l[i][1]
		print(ans)


t = int(input())
for you in range(t):
	n = int(input())
	l = input().split()
	li = [int(i) for i in l]
	copy = list(li)
	copy.sort()
	front = 0
	end = n - 1
	curr = 0
	ans = 0
	hashi = dict()
	while curr != n:
		if copy[curr] in hashi and hashi[copy[curr]] > 0:
			hashi[copy[curr]] -= 1
			curr += 1
			continue
		if li[end] == copy[curr]:
			end -= 1
			curr += 1
		elif li[front] == copy[curr]:
			front += 1
			curr += 1
		else:
			if li[front] not in hashi:
				hashi[li[front]] = 1
			else:
				hashi[li[front]] += 1
			ans = front + 1
			front += 1
	print(ans)


def solve():
	n = int(input())
	arr = [int(i) for i in input().split()]
	i = n - 1
	while i > 0 and arr[i - 1] >= arr[i]:
		i -= 1
	while i > 0 and arr[i - 1] <= arr[i]:
		i -= 1
	print(i)
for _ in range(int(input())):
	solve()


import sys
input = lambda : sys.stdin.readline().strip()
for _ in range(int(input())):
	n = int(input())
	a = list(reversed([int(x) for x in input().split()]))
	mx = a[0]
	ans = 0
	for i in range(1, n - 1):
		if mx > a[i] and a[i] < a[i + 1]:
			ans = n - 1 - i
			break
		mx = max(mx, a[i])
	print(ans)


i = int(input())
for s in range(i):
	q = int(input())
	po = list(map(int, input().split()))
	res = 0
	rrr = 1
	curIndex = q - 1
	for l in range(0, q - 1):
		if po[curIndex - l - 1] < po[-(l + 1)]:
			rrr = 0
		elif po[curIndex - l - 1] > po[-(l + 1)]:
			if rrr == 0:
				res = q - l - 1
				break
			rrr = 1
	print(res)


import sys
input = sys.stdin.readline

def inp():
	return int(input())

def inlt():
	return list(map(int, input().split()))

def insr():
	s = input()
	return list(s[:len(s) - 1])

def invr():
	return map(int, input().split())
N = inp()

def test_case(n, lnp_list):
	if n < 3:
		print(0)
		return
	saved = 0
	problem = None
	for i in range(n):
		index = n - 1 - i
		if index > 1:
			if problem:
				if lnp_list[index - 1] == lnp_list[index - 2]:
					continue
				if lnp_list[problem] > lnp_list[index - 1] < lnp_list[index - 2]:
					saved = index - 1
					break
				else:
					problem = None
			if lnp_list[index - 1] == lnp_list[index - 2]:
				problem = index
			if lnp_list[index] > lnp_list[index - 1] < lnp_list[index - 2]:
				saved = index - 1
				break
	print(saved)
input_arr = []
for _ in range(N):
	n = inp()
	lnp_list = inlt()
	input_arr.append((n, lnp_list))
for (n, lnp_list) in input_arr:
	test_case(n, lnp_list)


for iota in range(int(input())):
	n = int(input())
	b = list(map(int, input().split()))
	c = []
	bp = 0
	bpp = 0
	for i in range(n - 2, -1, -1):
		if b[i + 1] <= b[i]:
			continue
		else:
			bp = i + 1
			break
	if bp == 0:
		print(0)
		continue
	for i in range(bp - 1, -1, -1):
		if b[i] <= b[i + 1]:
			continue
		else:
			bpp = i + 1
			break
	print(bpp)


T = int(input())
i = 1
while i <= T:
	n = int(input())
	a = list(map(int, input().split(' ')))
	(flag, not_exists) = (False, True)
	for j in range(n - 1, 0, -1):
		if flag and a[j] < a[j - 1]:
			print(j)
			not_exists = False
			break
		elif a[j] > a[j - 1]:
			flag = True
	if not_exists:
		print(0)
	i += 1


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	m = l[n - 1]
	safe_idx = n - 1
	flag = False
	for i in range(n - 2, 0, -1):
		if l[i - 1] > l[i] and l[i] < m:
			safe_idx = i
			print(safe_idx)
			break
		else:
			m = max(m, l[i])
	else:
		print(0)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	if n <= 1:
		print(0)
		continue
	x = n - 1
	flag = 1
	while x > 0 and a[x - 1] >= a[x]:
		x -= 1
	while x > 0 and a[x - 1] <= a[x]:
		x -= 1
	print(x)


from sys import stdin
input = lambda : stdin.readline().strip()
from math import ceil, sqrt, factorial, gcd
from collections import deque
from bisect import bisect_left, bisect_right
for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	f = 0
	z = []
	for i in range(n - 1, -1, -1):
		if len(z) == 0:
			z.append(l[i])
		elif l[i] >= z[-1] and f == 0:
			z.append(l[i])
		elif l[i] <= z[-1] and f == 0:
			z.append(l[i])
			f = 1
		elif l[i] <= z[-1] and f == 1:
			z.append(l[i])
		else:
			break
	print(n - len(z))


t = int(input())
c = []
for i in range(t):
	n = int(input())
	a = [int(u) for u in input().split()]
	pos = n - 1
	while pos > 0 and a[pos - 1] >= a[pos]:
		pos -= 1
	while pos > 0 and a[pos - 1] <= a[pos]:
		pos -= 1
	c.append(pos)
for k in c:
	print(k)


for _ in range(int(input())):
	b = 0
	down = False
	n = int(input())
	aa = [int(x) for x in input().split()]
	a = [aa[0]] + aa + [aa[-1]]
	for i in range(1, n + 1):
		if a[n - i + 1] > a[n - i]:
			down = True
		elif a[n - i + 1] < a[n - i] and down:
			b = n - i
			break
	print(b)


from sys import stdin, stdout
for _ in range(int(stdin.readline())):
	n = int(stdin.readline())
	a = list(map(int, stdin.readline().split()))
	if n == 1:
		print(0)
		continue
	p = n - 1
	c = 0
	(inc, dec) = (1, 0)
	while p >= 0:
		if p > 0 and inc and (a[p - 1] < a[p]):
			(dec, inc) = (1, 0)
		elif inc and p > 0 and (a[p - 1] >= a[p]):
			p -= 1
			c += 1
		elif dec and p > 0 and (a[p - 1] <= a[p]):
			c += 1
			p -= 1
		else:
			break
	print(n - c - 1)


import sys

def rs():
	return sys.stdin.readline().rstrip()

def ri():
	return int(sys.stdin.readline())

def ria():
	return list(map(int, sys.stdin.readline().split()))

def ws(s):
	sys.stdout.write(s)
	sys.stdout.write('\n')

def wi(n):
	sys.stdout.write(str(n))
	sys.stdout.write('\n')

def wia(a, sep=' '):
	sys.stdout.write(sep.join([str(x) for x in a]))
	sys.stdout.write('\n')

def good(a):
	n = len(a)
	i = 0
	j = n - 1
	b = []
	while i <= j:
		if a[i] <= a[j]:
			b.append(a[i])
			i += 1
		else:
			b.append(a[j])
			j -= 1
	for i in range(n - 1):
		if b[i] > b[i + 1]:
			return False
	return True

def solve(n, a):
	lo = -1
	hi = n - 1
	while hi > lo + 1:
		mid = (lo + hi) // 2
		if good(a[mid:]):
			hi = mid
		else:
			lo = mid
	return hi

def main():
	for _ in range(ri()):
		n = ri()
		a = ria()
		wi(solve(n, a))
main()


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)


import sys
t = int(input())
while t:
	n = int(input()) - 1
	s = list(map(int, sys.stdin.readline().split()))
	rs = s.copy()
	rs.reverse()
	i = 0
	while i < n:
		if rs[i] <= rs[i + 1]:
			i += 1
		else:
			break
	j = i
	while i < n:
		if rs[i] >= rs[i + 1]:
			i += 1
		else:
			break
	print(n - i)
	t -= 1


def checker(arr, mid, n):
	temp = [-1]
	(l, r) = (mid, n - 1)
	while l < r:
		if arr[l] <= arr[r]:
			if arr[l] >= temp[-1]:
				temp.append(arr[l])
				l += 1
			else:
				return False
		elif arr[r] >= temp[-1]:
			temp.append(arr[r])
			r -= 1
		else:
			return False
	return True
for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	low = 0
	high = n
	while low < high:
		mid = (low + high) // 2
		f = checker(arr, mid, n)
		if f == True:
			high = mid
		else:
			low = mid
		if high - low == 1:
			break
	if checker(arr, low, n) == True:
		print(low)
	else:
		print(high)


import math
t = int(input())
for g in range(t):
	n = int(input())
	A = list(map(int, input().split()))
	ans = 0
	diff = []
	for i in range(1, n):
		diff.append(A[i] - A[i - 1])
	c = -1
	for i in range(len(diff)):
		if diff[i] < 0:
			c = i
		if diff[i] > 0 and c >= 0:
			if i - c > 1:
				ans = c + 1
				c = -1
				continue
			ans = i
			c = -1
	print(ans)


t = int(input())
for x in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	wyn = 1
	if n == 1 or max(l) == min(l):
		print(0)
		continue
	l_rev = l[::-1]
	big = False
	small = False
	for y in range(0, len(l_rev) - 1, +1):
		if l_rev[y + 1] == l_rev[y] and (not big and (not small) or (big and (not small)) or (small and (not big))):
			wyn += 1
			continue
		elif l_rev[y + 1] > l_rev[y]:
			if small:
				break
			wyn += 1
			big = True
			continue
		elif l_rev[y + 1] < l_rev[y]:
			small = True
			wyn += 1
			continue
		else:
			wyn += 1
			continue
	print(n - wyn)


t = int(input())
i = 0
while i < t:
	n = int(input())
	arr = list(map(int, input().split()))
	if n <= 2:
		print(0)
	else:
		j = n - 1
		while j > 0 and arr[j - 1] >= arr[j]:
			j = j - 1
		while j > 0 and arr[j - 1] <= arr[j]:
			j = j - 1
		print(j)
	i = i + 1


for _ in ' ' * int(input()):
	(n, a) = (int(input()), list(map(int, input().split())))
	k = n - 1
	while k > 0 and a[k - 1] >= a[k]:
		k -= 1
	while k > 0 and a[k - 1] <= a[k]:
		k -= 1
	print(k)


for i in range(int(input())):
	n = int(input())
	b = list(map(int, input().split()))
	l = 0
	r = n - 1
	c = [0]
	d = 0
	while l <= r:
		if b[l] <= b[r]:
			if b[l] >= c[len(c) - 1]:
				c.append(b[l])
				l = l + 1
			else:
				c = [0]
				d = l
				r = n - 1
		elif b[r] >= c[len(c) - 1]:
			c.append(b[r])
			r = r - 1
		else:
			c = [0]
			l = l + 1
			r = n - 1
			d = l
	print(d)


t = int(input())
for _ in range(t):
	N = int(input())
	lst = list(map(int, input().split()))
	count = N - 2
	while count >= 0 and lst[count] >= lst[count + 1]:
		count -= 1
	while count >= 0 and lst[count] <= lst[count + 1]:
		count -= 1
	print(count + 1)


t = int(input())
for i in range(t):
	n = int(input())
	array = list(map(int, input().split()))
	array.reverse()
	inc = 0
	prev = array[0]
	for i in range(1, len(array)):
		if array[i] >= prev:
			inc += 1
			prev = array[i]
		else:
			break
	dec = 0
	prev = array[0]
	for i in range(1, len(array)):
		if array[i] <= prev:
			dec += 1
			prev = array[i]
		else:
			break
	inc_dec = 0
	prev = array[0]
	ban = 1
	for i in range(1, len(array)):
		if ban == 1:
			if array[i] >= prev:
				inc_dec += 1
				prev = array[i]
			else:
				ban = 2
		if ban == 2:
			if array[i] <= prev:
				inc_dec += 1
				prev = array[i]
			else:
				ban = 3
		if ban == 3:
			break
	print(len(array) - max([inc, dec, inc_dec]) - 1)


import sys
line = sys.stdin.readline

def answer(arraylen, nums):
	if arraylen == 1:
		print(0)
		return
	change = []
	for i in range(0, arraylen - 1):
		if nums[i + 1] > nums[i]:
			change.append('+')
		elif nums[i + 1] == nums[i]:
			change.append('o')
		else:
			change.append('-')
	j = -1
	while change[j] != '+':
		j -= 1
		if abs(j) >= arraylen:
			print(0)
			return
	while change[j] != '-':
		j -= 1
		if abs(j) >= arraylen:
			print(0)
			return
	print(length + j)
for i in range(int(line())):
	length = int(line())
	integers = [int(num) for num in line().split(' ')]
	answer(length, integers)


for _ in range(int(input())):
	t = int(input())
	li = [int(i) for i in input().split()]
	li.reverse()
	flag = True
	ans = 0
	if t > 2:
		for i in range(1, t):
			if flag:
				if li[i] < li[i - 1]:
					flag = False
			elif li[i] > li[i - 1]:
				ans = t - i
				break
	print(ans)


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	if a == sorted(a) or a == sorted(a, reverse=True):
		print(0)
	else:
		p_ind = n - 1
		ans = 0
		for i in range(n - 2, -1, -1):
			if a[i] < a[i + 1]:
				p_ind = i + 1
				break
		for i in range(p_ind - 1, -1, -1):
			if a[i] > a[i + 1]:
				ans = i + 1
				break
		print(ans)


for _ in range(int(input())):
	n = int(input())
	A = list(map(int, input('').split()))
	ans = 0
	flag = 0
	for i in range(len(A) - 2, -1, -1):
		if flag == 1 and A[i] > A[i + 1]:
			ans = i + 1
			break
		if A[i] < A[i + 1]:
			flag = 1
	print(ans)


from sys import stdin, stdout
from collections import Counter
from itertools import permutations
import bisect
import math
I = lambda : map(int, stdin.readline().split())
I1 = lambda : stdin.readline()
for _ in range(int(I1())):
	n = int(I1())
	a = list(I())
	j = n - 1
	while j >= 0 and a[j - 1] >= a[j]:
		j -= 1
	while j >= 0 and a[j - 1] <= a[j]:
		j -= 1
	if j < 0:
		j = 0
	print(j)


try:
	t = int(input())
	for v in range(t):
		n = int(input())
		a = list(map(int, input().split()))
		s = 0
		e = n - 1
		lis = []
		top = -1
		dele = 0
		while s <= e:
			flag = 0
			if a[s] <= a[e]:
				num = a[s]
				flag = 1
				s += 1
			else:
				num = a[e]
				e -= 1
			if top != -1:
				if lis[top] <= num:
					lis.append(num)
					top += 1
				else:
					if flag == 1:
						s -= 1
					else:
						s += 1
					dele = s
					e = n - 1
					lis = []
					top = -1
			else:
				top += 1
				lis.append(num)
		print(dele)
except:
	pass


import sys

def readlines(type=int):
	return list(map(type, sys.stdin.readline().split()))

def read(type=int):
	return type(sys.stdin.readline().strip())
joint = lambda it, sep=' ': sep.join([str(i) if type(i) != list else sep.join(map(str, i)) for i in it])

def solve(l, nums):
	minimas = []
	for i in range(0, l - 2):
		if nums[i] > nums[i + 1]:
			k = i + 1
			while k < l - 1:
				if nums[k] == nums[k + 1]:
					k += 1
				elif nums[k] < nums[k + 1]:
					minimas.append(i + 1)
					break
				else:
					break
	if len(minimas) == 0:
		return 0
	return minimas[-1]

def main():
	n = read()
	results = []
	for _ in range(n):
		l = read()
		nums = readlines()
		results.append(solve(l, nums))
	print(joint(results, '\n'))
main()


t = int(input())
for _ in range(t):
	i = int(input())
	ar = list(map(int, input().split()))
	l = len(ar)
	i -= 1
	while i > 0 and ar[i] <= ar[i - 1]:
		i -= 1
	while i > 0 and ar[i] >= ar[i - 1]:
		i -= 1
	print(i)


for i in range(int(input())):
	k = int(input())
	lis = list(map(int, input().split()))
	r = 0
	trend = 'ge'
	eq = 0
	while True:
		if len(lis) == 2 or len(lis) == 1:
			print(k - len(lis))
			break
		if r != len(lis) - 1:
			if trend == 'ge' and lis[r] <= lis[r + 1]:
				if lis[r] == lis[r + 1]:
					eq += 1
				else:
					eq = 0
				r += 1
			elif trend == 'ge' and lis[r] > lis[r + 1]:
				eq = 0
				trend = 'le'
				r += 1
			elif trend == 'le' and lis[r] >= lis[r + 1]:
				if lis[r] == lis[r + 1]:
					eq += 1
				else:
					eq = 0
				r += 1
			elif trend == 'le' and lis[r] < lis[r + 1]:
				lis = lis[r - eq:]
				r = eq + 1
				eq = 0
				trend = 'ge'
		else:
			print(k - len(lis))
			break


for h in range(int(input())):
	n = int(input())
	arr = list(map(int, input().strip().split()))
	if len(arr) <= 2:
		print(0)
		continue
	mod = -1
	count = 0
	ans = -1
	for i in range(n - 2, -1, -1):
		if arr[i] > arr[i + 1]:
			if mod == -1:
				count += 1
			if count == 3 or mod == 0:
				ans = i + 1
				break
			mod = 1
		elif arr[i] < arr[i + 1]:
			if mod == -1 or mod == 1:
				count += 1
			if count == 3:
				ans = i + 1
				break
			mod = 0
	if ans == -1:
		print(0)
	else:
		print(ans)


for _ in range(int(input())):
	n = input()
	a = list(map(int, input().split()))
	pos = len(a) - 1
	while pos >= 1 and a[pos] <= a[pos - 1]:
		pos -= 1
	while pos >= 1 and a[pos] >= a[pos - 1]:
		pos -= 1
	print(pos)


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	l.reverse()
	flag = -1
	count = 1
	if n == 1:
		print(0)
		continue
	for i in range(1, n):
		if l[i] == l[i - 1]:
			count += 1
			continue
		elif l[i] > l[i - 1] and flag == -1:
			count += 1
			flag = 1
		elif l[i] < l[i - 1] and flag == -1:
			count += 1
			flag = 0
		elif l[i] > l[i - 1] and flag == 1:
			count += 1
		elif l[i] < l[i - 1] and (flag == 0 or flag == 2):
			count += 1
		elif l[i] < l[i - 1] and flag == 1:
			count += 1
			flag = 2
		else:
			break
	print(n - count)


n = int(input())
ans = []
for _ in range(n):
	l = int(input())
	arr = list(map(int, input().split()))
	idx = l - 1
	while idx > 0:
		if arr[idx - 1] < arr[idx]:
			break
		else:
			idx -= 1
	while idx > 0:
		if arr[idx - 1] > arr[idx]:
			break
		else:
			idx -= 1
	ans.append(str(idx))
print('\n'.join(ans))


for _ in range(int(input())):
	n = int(input())
	a = [int(i) for i in input().split()]
	a.reverse()
	seen_peak = False
	last = n
	for i in range(1, n):
		if seen_peak:
			if a[i] > a[i - 1]:
				last = i
				break
		elif a[i] < a[i - 1]:
			seen_peak = True
	print(n - last)


T = int(input())
for _ in range(T):
	N = int(input())
	A = list(map(int, input().split()))
	(pre, cnt) = (-1, 0)
	for i in range(N - 1, 0, -1):
		if A[i] <= A[i - 1]:
			cnt += 1
		else:
			if i == 1 and A[1] <= A[0]:
				cnt = N
			break
	le = N - cnt
	for i in range(le - 1, 0, -1):
		if A[i] >= A[i - 1]:
			cnt += 1
		else:
			if i == 1 and A[1] >= A[0]:
				cnt = N
			break
	ans = N - cnt - 1
	print(ans)


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	l2 = []
	if len(l) == 1 or len(l) == 2:
		print(0)
	else:
		temp = -1
		for i in range(n - 1, -1, -1):
			if l[i - 1] < l[i]:
				temp = i
				break
		ans = 0
		for i in range(temp, -1, -1):
			if l[i - 1] > l[i]:
				ans = i
				break
		if temp == -1:
			print(0)
		else:
			print(ans)


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	if n == 1:
		print(0)
	else:
		d = 1
		for j in range(2, n + 1):
			if a[-j] >= a[-j + 1]:
				d += 1
			else:
				break
		b = a[:n - d]
		if len(b) == 0:
			inc = 0
		elif len(b) == 1:
			inc = 1
		else:
			inc = 1
			temp = b[0]
			for j in range(1, len(b)):
				if b[j] >= temp:
					inc += 1
					temp = b[j]
				else:
					inc = 1
					temp = b[j]
		print(n - d - inc)


for _ in range(int(input())):
	N = int(input())
	List = [int(x) for x in input().split()]
	List.reverse()
	inc = True
	dec = False
	count = 1
	for i in range(1, N):
		if List[i] >= List[i - 1] and inc:
			count += 1
			continue
		elif List[i] < List[i - 1] and inc:
			dec = True
			inc = False
			count += 1
		elif List[i] <= List[i - 1] and dec:
			count += 1
		elif List[i] > List[i - 1] and dec:
			break
	print(N - count)


t = int(input())
for _ in range(t):
	n = int(input())
	x = list(map(int, input().split()))
	check = x[n - 1]
	index = 0
	for i in range(n - 2, -1, -1):
		if check <= x[i]:
			check = x[i]
		else:
			index = i + 1
			break
	nc = x[index]
	ni = 0
	for i in range(index - 1, -1, -1):
		if x[i] <= nc:
			nc = x[i]
		else:
			ni = i + 1
			break
	print(ni)


from sys import stdin
input = stdin.readline
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	f = 0
	for i in range(n - 3, -1, -1):
		if a[i] > a[i + 1]:
			j = i + 2
			while a[j] == a[i + 1]:
				if j == n - 1:
					break
				j += 1
			if a[i + 1] < a[j]:
				print(i + 1)
				f = 1
				break
	if not f:
		print(0)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	(pos, neg, c) = (0, 0, 0)
	for i in range(n - 1, 0, -1):
		if a[i - 1] - a[i] > 0 and neg != 0:
			c = i
			break
		if a[i - 1] - a[i] > 0:
			pos += 1
		if a[i - 1] - a[i] < 0:
			neg += 1
	print(n - (n - c))


a = int(input(''))
for i in range(0, a):
	b = int(input(''))
	c = list(map(int, input().split()))
	d = len(c)
	i = d - 1
	while i > 0 and c[i] <= c[i - 1]:
		i = i - 1
	while i > 0 and c[i] >= c[i - 1]:
		i = i - 1
	print(i)


from __future__ import division, print_function
import os, sys
import math
import collections
from io import BytesIO, IOBase
if sys.version_info[0] < 3:
	from __builtin__ import xrange as range
	from future_builtins import ascii, filter, hex, map, oct, zip

class my_dictionary(dict):

	def __init__(self):
		self = dict()

	def add(self, key, value):
		self[key] = value

def ii():
	return int(input())

def si():
	return input()

def mi():
	return map(int, input().strip().split(' '))

def msi():
	return map(str, input().strip().split(' '))

def li():
	return list(mi())

def dmain():
	sys.setrecursionlimit(100000000)
	threading.stack_size(40960000)
	thread = threading.Thread(target=main)
	thread.start()
abc = 'abcdefghijklmnopqrstuvwxyz'
abd = {'a': 0, 'b': 1, 'c': 2, 'd': 3, 'e': 4, 'f': 5, 'g': 6, 'h': 7, 'i': 8, 'j': 9, 'k': 10, 'l': 11, 'm': 12, 'n': 13, 'o': 14, 'p': 15, 'q': 16, 'r': 17, 's': 18, 't': 19, 'u': 20, 'v': 21, 'w': 22, 'x': 23, 'y': 24, 'z': 25}
mod = 1000000007
inf = float('inf')
vow = ['a', 'e', 'i', 'o', 'u']
(dx, dy) = ([-1, 1, 0, 0], [0, 0, 1, -1])

def getKey(item):
	return item[1]

def sort2(l):
	return sorted(l, key=getKey, reverse=True)

def d2(n, m, num):
	return [[num for x in range(m)] for y in range(n)]

def isPowerOfTwo(x):
	return x and (not x & x - 1)

def decimalToBinary(n):
	return bin(n).replace('0b', '')

def ntl(n):
	return [int(i) for i in str(n)]

def ncr(n, r):
	return factorial(n) // (factorial(r) * factorial(n - r))

def binary_search(arr, low, high, x):
	if high >= low:
		mid = (high + low) // 2
		if arr[mid] == x:
			return mid
		elif arr[mid] > x:
			return binary_search(arr, low, mid - 1, x)
		else:
			return binary_search(arr, mid + 1, high, x)
	else:
		return -1

def ceil(x, y):
	if x % y == 0:
		return x // y
	else:
		return x // y + 1

def powerMod(x, y, p):
	res = 1
	x %= p
	while y > 0:
		if y & 1:
			res = res * x % p
		y = y >> 1
		x = x * x % p
	return res

def gcd(x, y):
	while y:
		(x, y) = (y, x % y)
	return x

def isPrime(n):
	if n <= 1:
		return False
	if n <= 3:
		return True
	if n % 2 == 0 or n % 3 == 0:
		return False
	i = 5
	while i * i <= n:
		if n % i == 0 or n % (i + 2) == 0:
			return False
		i = i + 6
	return True

def read():
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')

def padded_bin_with_complement(x):
	if x < 0:
		return bin(2 ** 16 - abs(x))[2:].zfill(16)
	else:
		return bin(x)[2:].zfill(16)

def binaryToDecimal(binary):
	binary1 = binary
	(decimal, i, n) = (0, 0, 0)
	while binary != 0:
		dec = binary % 10
		decimal = decimal + dec * pow(2, i)
		binary = binary // 10
		i += 1
	print(decimal)

def CountFrequency(my_list):
	freq = {}
	for item in my_list:
		if item in freq:
			freq[item] += 1
		else:
			freq[item] = 1
	return freq

def pos(a):
	b = [0] * len(a)
	c = sorted(a)
	for i in range(len(a)):
		for j in range(len(a)):
			if c[j] == a[i]:
				b[i] = j
				break
	return b

def smallestDivisor(n):
	if n % 2 == 0:
		return 2
	i = 3
	while i * i <= n:
		if n % i == 0:
			return i
		i += 2
	return n

def commonn(a, b, n):
	c = []
	for i in range(n):
		if a[i] == b[i]:
			c.append('-1')
		else:
			c.append(b[i])
	return c

def primeFactors(n):
	j = []
	while n % 2 == 0:
		j.append(2)
		n = n / 2
	for i in range(3, int(math.sqrt(n)) + 1, 2):
		while n % i == 0:
			j.append(int(n))
			n = n / i
	if n > 2:
		j.append(int(n))
	return j

def sumdigit(n):
	n = str(n)
	k = 0
	for i in range(len(n)):
		k += int(n[i])
	return k

def largeseq1(a, cost, ini, des):
	ind = -1
	mx = -1000000007
	j = 0
	for i in range(des - 1, ini):
		if a[i] > mx:
			mx = a[i]
			j += cost[i]
			ind = i
	if ind != ini - 1:
		return -1
	else:
		return j

def largeseq2(a, cost, ini, des):
	ind = -1
	mx = -1000000007
	j = 0
	for i in range(des - 1, ini - 2, -1):
		if a[i] > mx:
			mx = a[i]
			j += cost[i]
			ind = i
	if ind != ini - 1:
		return -1
	else:
		return j

def main():
	for _ in range(ii()):
		n = ii()
		a = li()
		f1 = 0
		f2 = 0
		if n < 3:
			print('0')
			continue
		for i in range(n - 1, 0, -1):
			if a[i] > a[i - 1]:
				f1 = i
				break
		for i in range(f1, 0, -1):
			if a[i] < a[i - 1]:
				f2 = i
				break
		print(min(f1, f2))
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


from sys import stdin, stdout

def input():
	return stdin.readline().strip()
T = int(input())

def ans(A):
	A += [float('-inf')]
	i = len(A) - 1
	while i > 0 and A[i - 1] >= A[i]:
		i -= 1
	while i > 0 and A[i - 1] <= A[i]:
		i -= 1
	return i
for _ in range(T):
	input()
	A = input().split(' ')
	A = list(map(int, A))
	print(ans(A))


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


class C:

	def __init__(self):
		self.n = int(input())
		self.a = [int(x) for x in input().split()]

	def solve_and_print(self):
		(n, a) = (self.n, self.a)
		if n <= 2 or a == sorted(a) or a == sorted(a, reverse=True):
			print(0)
			return
		prev = 0
		r = n - 1
		while r > 0 and a[r] >= prev:
			prev = a[r]
			r -= 1
		while r >= 0 and a[r] <= prev:
			prev = a[r]
			r -= 1
		print(r + 1 if r >= 0 else 0)
for _ in range(int(input())):
	C().solve_and_print()


t = int(input())
for i in range(t):
	n = int(input())
	z = [int(x) for x in input().split()]
	index = n - 1
	while index > 0 and z[index] <= z[index - 1]:
		index -= 1
	while index > 0 and z[index] >= z[index - 1]:
		index -= 1
	print(index)


from sys import stdin, stdout, setrecursionlimit
from collections import deque, defaultdict, Counter
from heapq import heappush, heappop
import math
rl = lambda : stdin.readline()
rll = lambda : stdin.readline().split()
rli = lambda : map(int, stdin.readline().split())
rlf = lambda : map(float, stdin.readline().split())
(INF, NINF) = (float('inf'), float('-inf'))

def main():
	T = int(rl())
	for _ in range(T):
		n = int(rl())
		A = list(rli())[::-1]
		if n == 1:
			print(0)
			continue
		peak = False
		maxi = A[0]
		cnt = 0
		for i in range(len(A)):
			if A[i] > maxi:
				maxi = max(A[i], maxi)
			if A[i] < maxi:
				peak = True
			if peak and A[i] > A[i - 1]:
				break
			cnt += 1
		print(n - cnt)
	stdout.close()
main()


t = int(input())
for _ in range(0, t):
	n = int(input())
	list1 = [int(num) for num in input().split()]
	if n == 1:
		print(0)
	else:
		k = -1
		for i in range(n - 1, 0, -1):
			if list1[i] > list1[i - 1]:
				k = i - 1
				break
		if k == -1:
			print(0)
		else:
			i = k
			while i >= 1 and list1[i] >= list1[i - 1]:
				i = i - 1
			print(i)


t = int(input())
for v in range(t):
	n = int(input())
	li = list(map(int, input().split()))
	pref = [1] * n
	suff = [1] * n
	if n == 1:
		print(0)
		continue
	for i in range(1, n):
		if li[i] >= li[i - 1]:
			pref[i] = pref[i - 1] + 1
	i = n - 2
	while i >= 0:
		if li[i] >= li[i + 1]:
			suff[i] = suff[i + 1] + 1
		i -= 1
	ans = -1
	for i in range(n - 1):
		if suff[i + 1] == n - 1 - i:
			ans = n - 1 - i + pref[i]
			break
	print(n - ans)


tst = int(input())
t = 1
while t <= tst:
	n = int(input())
	ls = list(map(int, input().split()))
	ind = n - 1
	i = n - 2
	while i >= 0:
		if ls[i] >= ls[i + 1]:
			ind = i
		else:
			break
		i -= 1
	i = ind - 1
	while i >= 0:
		if ls[i] <= ls[i + 1]:
			ind = i
		else:
			break
		i -= 1
	print(ind)
	t += 1


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
		elif a[i] > a[i + 1]:
			poss = False
			i += 1
		i -= 1
	print(i + 1)


def main():
	t = int(input())
	for t1 in range(0, t):
		input()
		d = list(map(int, input().split()))
		index = 0
		index2 = 0
		for i in range(1, len(d)):
			if d[i - 1] > d[i]:
				index2 = i
			if d[i - 1] < d[i]:
				index = index2
		print(index)
main()


def read_int():
	return int(input().strip())

def read_ints():
	return list(map(int, input().strip().split(' ')))

def solve():
	n = read_int()
	a = read_ints()
	i = n - 1
	while i > 0 and a[i - 1] >= a[i]:
		i -= 1
	while i > 0 and a[i - 1] <= a[i]:
		i -= 1
	return i
T = read_int()
for _ in range(T):
	print(solve())


import sys
import math
from functools import reduce
import bisect

def getN():
	return int(input())

def getNM():
	return map(int, input().split())

def getList():
	return list(map(int, input().split()))

def input():
	return sys.stdin.readline().rstrip()

def index(a, x):
	i = bisect.bisect_left(a, x)
	if i != len(a) and a[i] == x:
		return i
	return i
for i in range(int(input())):
	n = getN()
	arr = getList()[::-1]
	if n <= 2:
		print(0)
		continue
	decrease = 0
	increase = 0
	if arr[0] <= arr[1]:
		increase = 1
	else:
		decrease = 1
	ans = 0
	for i in range(2, n):
		if increase:
			if arr[i] < arr[i - 1]:
				decrease = 1
				increase = 0
		if decrease:
			if arr[i] > arr[i - 1]:
				ans = n - i
				break
	arr = arr[:n - ans][::-1]
	m = min(arr)
	if arr[0] == m or arr[-1] == m:
		print(ans)
	else:
		print(ans + arr.index(m))


for _ in range(int(input())):
	num = int(input())
	a = list(map(int, input().split()))
	l = sorted(a)
	if l == a or l == a[::-1]:
		print(0)
		continue
	for i in range(num - 2, -1, -1):
		if a[i] < a[i + 1]:
			break
	index = i + 1
	flag = 1
	for i in range(index - 1, -1, -1):
		if a[i] > a[i + 1]:
			if i == 0:
				flag = 0
			break
	if i <= 0:
		if flag:
			print(0)
		else:
			print(1)
	else:
		print(1 + num - (num - i))


def solve(arr, n, ans):
	if n == 1:
		ans.append(0)
		return
	index = n - 1
	for i in range(n - 1, 0, -1):
		if arr[i] <= arr[i - 1]:
			index = i - 1
		else:
			break
	for i in range(index, 0, -1):
		if arr[i] >= arr[i - 1]:
			index = i - 1
		else:
			break
	ans.append(index)

def main():
	t = int(input())
	ans = []
	for i in range(t):
		n = int(input())
		arr = list(map(int, input().split()))
		solve(arr, n, ans)
	for i in ans:
		print(i)
main()


for _ in range(int(input())):
	n = int(input())
	a = [int(i) for i in input().split()]
	acc = 0
	i = len(a) - 1
	while i > 0 and a[i] <= a[i - 1]:
		i -= 1
	while i > 0 and a[i] >= a[i - 1]:
		i -= 1
	print(i)


for i in range(int(input())):
	n = int(input())
	l = [int(i) for i in input().split()]
	lis = []
	i = n - 1
	while i > 0 and l[i] <= l[i - 1]:
		i -= 1
	j = i
	while j > 0 and l[j] >= l[j - 1]:
		j -= 1
	if n == 0:
		print(0)
	else:
		print(j)


from sys import stdin
g = lambda : stdin.readline().strip()
gl = lambda : g().split()
gil = lambda : [int(var) for var in gl()]
gcl = lambda : list(g())
(t,) = gil()

def isGood(arr, start):
	(l, r) = (start, len(arr) - 1)
	last = -float('inf')
	while l <= r:
		idx = min(l, r, key=lambda x: arr[x])
		if arr[idx] >= last:
			last = arr[idx]
			if min(l, r, key=lambda x: arr[x]) == l:
				l += 1
			else:
				r -= 1
		else:
			return False
	return True
for _ in range(t):
	(n,) = gil()
	arr = gil()
	if isGood(arr, 0):
		print(0)
	else:
		(l, r) = (0, n - 1)
		store = None
		while l <= r:
			mid = (l + r) // 2
			if isGood(arr, mid):
				store = mid
				r = mid - 1
			else:
				l = mid + 1
		print(store)


for _ in range(int(input())):
	n = int(input())
	numbers = list(map(int, input().split()))
	temp = n - 1
	while numbers[temp] <= numbers[temp - 1] and temp > 0:
		temp -= 1
	while numbers[temp] >= numbers[temp - 1] and temp > 0:
		temp -= 1
	print(temp)


import sys, math, itertools
from collections import Counter, deque, defaultdict
from bisect import bisect_left, bisect_right
mod = 10 ** 9 + 7
INF = float('inf')

def inp():
	return int(sys.stdin.readline())

def inpl():
	return list(map(int, sys.stdin.readline().split()))
for _ in range(inp()):
	n = inp()
	a = inpl()
	a = a[::-1]
	dec = False
	for i in range(n - 1):
		if a[i] > a[i + 1]:
			dec = True
		elif dec and a[i] < a[i + 1]:
			print(n - i - 1)
			break
	else:
		print(0)


for _ in range(int(input())):
	n = int(input())
	l = [int(i) for i in input().split()][:n]
	(inc, dec) = (True, False)
	(x, y, prev) = (0, 0, 0)
	for i in range(len(l) - 1, -1, -1):
		if inc:
			if l[i] >= prev:
				x += 1
				prev = l[i]
			else:
				inc = False
				dec = True
		if dec:
			if l[i] <= prev:
				y += 1
				prev = l[i]
			else:
				break
	print(len(l) - (x + y))


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()


t = int(input())
for i in range(t):
	n = int(input())
	a = [int(i) for i in input().split()]
	ind = 0
	for i in range(n - 1, 0, -1):
		if a[i] > a[i - 1]:
			ind = i
			break
	ans = 0
	if ind != 0:
		for i in range(ind - 1, 0, -1):
			if a[i] < a[i - 1]:
				ans = i
				break
	print(ans)


import math
from collections import *
import math

def solve():
	n = int(input())
	a = [int(i) for i in input().split()]
	ans = n - 1
	while ans > 0 and a[ans - 1] >= a[ans]:
		ans -= 1
	while ans > 0 and a[ans - 1] <= a[ans]:
		ans -= 1
	print(ans)
t = int(input())
for _ in range(t):
	solve()


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	last = n - 1
	f = True
	while last >= 1:
		if f:
			if a[last - 1] >= a[last]:
				last -= 1
				continue
			else:
				f = False
				last -= 1
				continue
		if a[last - 1] <= a[last]:
			last -= 1
		else:
			break
	print(last)


for _ in range(int(input())):
	total = int(input())
	array = list(map(int, input().split()))
	array.reverse()
	first = array[0]
	decreasing = True
	result = False
	for i in range(1, len(array)):
		if decreasing:
			if array[i] < array[i - 1]:
				decreasing = False
		elif array[i] > array[i - 1]:
			print(total - i)
			result = True
			break
	if not result:
		print(0)


from collections import defaultdict
import sys, math
f = None
try:
	f = open('q1.input', 'r')
except IOError:
	f = sys.stdin
if 'xrange' in dir(__builtins__):
	range = xrange
sys.setrecursionlimit(10 ** 6)

def print_case_iterable(case_num, iterable):
	print('Case #{}: {}'.format(case_num, ' '.join(map(str, iterable))))

def print_case_number(case_num, iterable):
	print('Case #{}: {}'.format(case_num, iterable))

def print_iterable(A):
	print(' '.join(A))

def read_int():
	return int(f.readline().strip())

def read_int_array():
	return [int(x) for x in f.readline().strip().split(' ')]

def rns():
	a = [x for x in f.readline().split(' ')]
	return (int(a[0]), a[1].strip())

def read_string():
	return list(f.readline().strip())

def ri():
	return int(f.readline().strip())

def ria():
	return [int(x) for x in f.readline().strip().split(' ')]

def rns():
	a = [x for x in f.readline().split(' ')]
	return (int(a[0]), a[1].strip())

def rs():
	return list(f.readline().strip())

def bi(x):
	return bin(x)[2:]
from collections import deque
import math
NUMBER = 10 ** 9 + 7

def factorial(n):
	M = NUMBER
	f = 1
	for i in range(1, n + 1):
		f = f * i % M
	return f

def mult(a, b):
	return a * b % NUMBER

def minus(a, b):
	return (a - b) % NUMBER

def plus(a, b):
	return (a + b) % NUMBER

def egcd(a, b):
	if a == 0:
		return (b, 0, 1)
	else:
		(g, y, x) = egcd(b % a, a)
		return (g, x - b // a * y, y)

def modinv(a):
	m = NUMBER
	(g, x, y) = egcd(a, m)
	if g != 1:
		raise Exception('modular inverse does not exist')
	else:
		return x % m

def choose(n, k):
	if n < k:
		assert false
	return mult(factorial(n), modinv(mult(factorial(k), factorial(n - k)))) % NUMBER
from collections import deque, defaultdict
import heapq
from types import GeneratorType

def bootstrap(f, stack=[]):

	def wrappedfunc(*args, **kwargs):
		if stack:
			return f(*args, **kwargs)
		else:
			to = f(*args, **kwargs)
			while True:
				if type(to) is GeneratorType:
					stack.append(to)
					to = next(to)
				else:
					stack.pop()
					if not stack:
						break
					to = stack[-1].send(to)
			return to
	return wrappedfunc

def dfs(g, timeIn, timeOut, depths, parents):
	cnt = 0
	stack = [[1, 0, 0, 0]]
	while stack:
		(v, neig_i, parent, depth) = stack[-1]
		parents[v] = parent
		depths[v] = depth
		if neig_i == 0:
			timeIn[v] = cnt
			cnt += 1
		while neig_i < len(g[v]):
			u = g[v][neig_i]
			if u == parent:
				neig_i += 1
				continue
			stack[-1][1] = neig_i + 1
			stack.append([u, 0, v, depth + 1])
			break
		if neig_i == len(g[v]):
			stack.pop()
			timeOut[v] = cnt
			cnt += 1
cnt = 0

@bootstrap
def dfs(v, adj, timeIn, timeOut, depths, parents, parent=0, depth=0):
	global cnt
	parents[v] = parent
	depths[v] = depth
	timeIn[v] = cnt
	cnt += 1
	for u in adj[v]:
		if u == parent:
			continue
		yield dfs(u, adj, timeIn, timeOut, depths, parents, v, depth + 1)
	timeOut[v] = cnt
	cnt += 1
	yield

def gcd(a, b):
	if a == 0:
		return b
	return gcd(b % a, a)

def lcm(a, b):
	return a * b / gcd(a, b)

def get_num_2_5(n):
	twos = 0
	fives = 0
	while n > 0 and n % 2 == 0:
		n //= 2
		twos += 1
	while n > 0 and n % 5 == 0:
		n //= 5
		fives += 1
	return (twos, fives)

def shift(a, i, num):
	for _ in range(num):
		(a[i], a[i + 1], a[i + 2]) = (a[i + 2], a[i], a[i + 1])

def equal(x, y):
	return abs(x - y) <= 1e-09

def getAngle(a, b, c):
	ang = math.degrees(math.atan2(c[1] - b[1], c[0] - b[0]) - math.atan2(a[1] - b[1], a[0] - b[0]))
	return ang + 360 if ang < 0 else ang

def getLength(a, b):
	return math.sqrt((a[0] - b[0]) ** 2 + (a[1] - b[1]) ** 2)
from heapq import heapify, heappush, heappop

def bfs(adj):
	s = set()
	d = defaultdict(set)
	q = deque([])
	s.add(0)
	d[0].add(0)
	q.append((0, 0))
	while len(q) > 0:
		(v, depth) = q.popleft()
		for u in adj[v]:
			if u not in s:
				s.add(u)
				d[depth + 1].add(u)
				q.append((u, depth + 1))
	return d

def dfs(u, top, adj, vis):
	vis[u] = 1
	for (v, d) in adj[u]:
		if vis[v] == 2 or d == 0:
			continue
		if vis[v] == 1:
			return 'No'
		if dfs(v, top, adj, vis) == 'No':
			return 'No'
	vis[u] = 2
	top.append(u)

def solution(a, n):
	i = n - 1
	while i > -1 and a[i] <= a[i - 1]:
		i -= 1
	while i > -1 and a[i] >= a[i - 1]:
		i -= 1
	return max(i, 0)

def main():
	T = 1
	T = ri()
	for i in range(T):
		n = ri()
		a = ria()
		x = solution(a, n)
		if 'xrange' not in dir(__builtins__):
			print(x)
		else:
			(print >> output, str(x))
	if 'xrange' in dir(__builtins__):
		print(output.getvalue())
		output.close()
if 'xrange' in dir(__builtins__):
	import cStringIO
	output = cStringIO.StringIO()
main()


production = True
import sys, math, collections

def input(input_format=0, multi=0):
	if multi > 0:
		return [input(input_format) for i in range(multi)]
	else:
		next_line = sys.stdin.readline()[:-1]
		if input_format >= 10:
			use_list = False
			input_format = int(str(input_format)[-1])
		else:
			use_list = True
		if input_format == 0:
			formatted_input = [next_line]
		elif input_format == 1:
			formatted_input = list(map(int, next_line.split()))
		elif input_format == 2:
			formatted_input = list(map(float, next_line.split()))
		elif input_format == 3:
			formatted_input = list(next_line)
		elif input_format == 4:
			formatted_input = list(map(int, list(next_line)))
		elif input_format == 5:
			formatted_input = next_line.split()
		else:
			formatted_input = [next_line]
		return formatted_input if use_list else formatted_input[0]

def out(output_line, output_format=0, newline=True):
	formatted_output = ''
	if output_format == 0:
		formatted_output = str(output_line)
	elif output_format == 1:
		formatted_output = ' '.join(map(str, output_line))
	elif output_format == 2:
		formatted_output = '\n'.join(map(str, output_line))
	print(formatted_output, end='\n' if newline else '')

def log(*args):
	if not production:
		print('$$$', end='')
		print(*args)
enu = enumerate
ter = lambda a, b, c: b if a else c
ceil = lambda a, b: -(-a // b)

def mapl(iterable, format=0):
	if format == 0:
		return list(map(int, iterable))
	elif format == 1:
		return list(map(str, iterable))
	elif format == 2:
		return list(map(list, iterable))

def solve():
	n = input(11)
	a = input(1)
	u = 0
	c = 1
	if n <= 2:
		out(0)
		return
	for i in range(n - 2, -1, -1):
		c += 1
		if not u:
			if a[i] < a[i + 1]:
				u = 1
		elif a[i] > a[i + 1]:
			c -= 1
			log(a[i])
			break
		log(a[i], u)
	log('c', c)
	out(n - c)
	return
for i in range(input(11)):
	solve()


import os
import heapq
import sys
import math
import operator
from collections import defaultdict
from io import BytesIO, IOBase

def inpt():
	return [int(k) for k in input().split()]

def main():
	for _ in range(int(input())):
		n = int(input())
		ar = inpt()
		j = n - 1
		if n == 1:
			print(0)
		else:
			for i in range(n - 2, -1, -1):
				if ar[i] >= ar[j]:
					j -= 1
				else:
					break
			k = j
			for i in range(k - 1, -1, -1):
				if ar[i] <= ar[j]:
					j -= 1
				else:
					break
			print(j)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	if n == 1:
		print(0)
	else:
		cnt = 0
		strt = a[-1]
		ind = -1
		for i in range(n - 2, -1, -1):
			if a[i] > strt and cnt == 1:
				ind = i
				break
			elif a[i] >= strt and cnt == 0:
				strt = a[i]
			elif a[i] <= strt:
				cnt = 1
				strt = a[i]
		print(ind + 1)


t = int(input())
for i in range(1, t + 1):
	n = int(input())
	a = input().split()
	temp = n
	for i in range(0, n):
		if int(a[n - i - 1]) > int(a[n - i - 2]):
			temp = i
			break
	ans = 0
	for i in range(temp, n):
		if int(a[n - i - 1]) < int(a[n - i - 2]):
			ans = n - i - 1
			break
	print(ans)


from sys import stdin, stdout

def read_input():
	lines = []
	for line in stdin:
		if 'exit' == line.lower().rstrip():
			break
		lines.append(line.rstrip())
	return lines

def write(s):
	stdout.write(str(s) + '\n')

def finish():
	stdout.flush()
lines = read_input()
t = int(lines[0])
for i in range(1, 2 * t + 1, 2):
	n = int(lines[i])
	a = [int(x) for x in lines[i + 1].split()]
	if n == 1 or n == 2:
		write('0')
		continue
	increase = True
	for j in range(n - 2, -1, -1):
		if increase:
			if a[j] < a[j + 1]:
				increase = False
		elif a[j] > a[j + 1]:
			break
		d = j
	write(d)
finish()


def solution():
	t = int(input())
	for _ in range(t):
		n = int(input())
		nums = list(map(int, input().split()))
		j = n - 1
		while j > 0 and nums[j - 1] >= nums[j]:
			j -= 1
		while j > 0 and nums[j - 1] <= nums[j]:
			j -= 1
		print(j)
solution()


import sys
t = int(sys.stdin.readline())
for _ in range(t):
	n = int(sys.stdin.readline())
	l = list(map(int, sys.stdin.readline().split()))
	c = l[-1]
	index = n - 1
	u = True
	while True:
		if index < 0:
			break
		if u:
			if l[index] >= c:
				c = l[index]
				index -= 1
			else:
				u = False
		elif l[index] <= c:
			c = l[index]
			index -= 1
		else:
			break
	print(index + 1)


MOD = 1000000007
ii = lambda : int(input())
si = lambda : input()
dgl = lambda : list(map(int, input()))
f = lambda : map(int, input().split())
il = lambda : list(map(int, input().split()))
ls = lambda : list(input())
from collections import *
for _ in range(ii()):
	n = ii()
	a = il()
	a.reverse()
	x = n - 1
	c = 0
	for i in range(1, n):
		if c == 0:
			if a[i] < a[i - 1]:
				c = c + 1
		if c == 1:
			if a[i] > a[i - 1]:
				break
		x = x - 1
	print(x)


from collections import deque
t = int(input())
k = []
for i in range(t):
	ans = 0
	n = int(input())
	a = deque(map(int, input().split()))
	j = len(a) - 1
	while j > 0 and a[j] <= a[j - 1]:
		ans += 1
		j -= 1
	while j > 0 and a[j] >= a[j - 1]:
		ans += 1
		j -= 1
	k.append(len(a) - ans - 1)
for i in k:
	print(i)


t = int(input())
for tt in range(t):
	n = int(input())
	a = [int(x) for x in input().split(' ')]
	a.reverse()
	inc = 1
	b = [a[0]]
	for i in range(1, n):
		if inc:
			if a[i - 1] <= a[i]:
				b.append(a[i])
			else:
				b.append(a[i])
				inc = 0
		elif a[i - 1] >= a[i]:
			b.append(a[i])
		else:
			break
	a.reverse()
	b.reverse()
	print(len(a) - len(b))


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


from collections import Counter, defaultdict, deque
import sys
input = sys.stdin.readline

def solve():
	n = int(input())
	arr = [int(x) for x in input().split()]
	if n == 1:
		print(0)
		return
	t = []
	for i in range(n - 1):
		if arr[i] > arr[i + 1]:
			t.append(1)
		elif arr[i] < arr[i + 1]:
			t.append(0)
		else:
			t.append(2)
	t.reverse()
	c = 0
	zero = False
	for el in t:
		if el == 2:
			c += 1
			continue
		if zero and el:
			break
		if not el:
			zero = True
		c += 1
	print(n - c - 1)
tt = int(input())
for test in range(tt):
	solve()


import math
import heapq, bisect
import sys
from collections import deque, defaultdict
from fractions import Fraction
mod = 10 ** 9 + 7
mod1 = 998244353
import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

class TreeNode:

	def __init__(self, k, v):
		self.key = k
		self.value = v
		self.left = None
		self.right = None
		self.parent = None
		self.height = 1
		self.num_left = 1
		self.num_total = 1

class AvlTree:

	def __init__(self):
		self._tree = None

	def add(self, k, v):
		if not self._tree:
			self._tree = TreeNode(k, v)
			return
		node = self._add(k, v)
		if node:
			self._rebalance(node)

	def _add(self, k, v):
		node = self._tree
		while node:
			if k < node.key:
				if node.left:
					node = node.left
				else:
					node.left = TreeNode(k, v)
					node.left.parent = node
					return node.left
			elif node.key < k:
				if node.right:
					node = node.right
				else:
					node.right = TreeNode(k, v)
					node.right.parent = node
					return node.right
			else:
				node.value = v
				return

	@staticmethod
	def get_height(x):
		return x.height if x else 0

	@staticmethod
	def get_num_total(x):
		return x.num_total if x else 0

	def _rebalance(self, node):
		n = node
		while n:
			lh = self.get_height(n.left)
			rh = self.get_height(n.right)
			n.height = max(lh, rh) + 1
			balance_factor = lh - rh
			n.num_total = 1 + self.get_num_total(n.left) + self.get_num_total(n.right)
			n.num_left = 1 + self.get_num_total(n.left)
			if balance_factor > 1:
				if self.get_height(n.left.left) < self.get_height(n.left.right):
					self._rotate_left(n.left)
				self._rotate_right(n)
			elif balance_factor < -1:
				if self.get_height(n.right.right) < self.get_height(n.right.left):
					self._rotate_right(n.right)
				self._rotate_left(n)
			else:
				n = n.parent

	def _remove_one(self, node):
		replacement = node.left or node.right
		if node.parent:
			if AvlTree._is_left(node):
				node.parent.left = replacement
			else:
				node.parent.right = replacement
			replacement.parent = node.parent
			node.parent = None
		else:
			self._tree = replacement
			replacement.parent = None
		node.left = None
		node.right = None
		node.parent = None
		self._rebalance(replacement)

	def _remove_leaf(self, node):
		if node.parent:
			if AvlTree._is_left(node):
				node.parent.left = None
			else:
				node.parent.right = None
			self._rebalance(node.parent)
		else:
			self._tree = None
		node.parent = None
		node.left = None
		node.right = None

	def remove(self, k):
		node = self._get_node(k)
		if not node:
			return
		if AvlTree._is_leaf(node):
			self._remove_leaf(node)
			return
		if node.left and node.right:
			nxt = AvlTree._get_next(node)
			node.key = nxt.key
			node.value = nxt.value
			if self._is_leaf(nxt):
				self._remove_leaf(nxt)
			else:
				self._remove_one(nxt)
			self._rebalance(node)
		else:
			self._remove_one(node)

	def get(self, k):
		node = self._get_node(k)
		return node.value if node else -1

	def _get_node(self, k):
		if not self._tree:
			return None
		node = self._tree
		while node:
			if k < node.key:
				node = node.left
			elif node.key < k:
				node = node.right
			else:
				return node
		return None

	def get_at(self, pos):
		x = pos + 1
		node = self._tree
		while node:
			if x < node.num_left:
				node = node.left
			elif node.num_left < x:
				x -= node.num_left
				node = node.right
			else:
				return (node.key, node.value)
		raise IndexError('Out of ranges')

	@staticmethod
	def _is_left(node):
		return node.parent.left and node.parent.left == node

	@staticmethod
	def _is_leaf(node):
		return node.left is None and node.right is None

	def _rotate_right(self, node):
		if not node.parent:
			self._tree = node.left
			node.left.parent = None
		elif AvlTree._is_left(node):
			node.parent.left = node.left
			node.left.parent = node.parent
		else:
			node.parent.right = node.left
			node.left.parent = node.parent
		bk = node.left.right
		node.left.right = node
		node.parent = node.left
		node.left = bk
		if bk:
			bk.parent = node
		node.height = max(self.get_height(node.left), self.get_height(node.right)) + 1
		node.num_total = 1 + self.get_num_total(node.left) + self.get_num_total(node.right)
		node.num_left = 1 + self.get_num_total(node.left)

	def _rotate_left(self, node):
		if not node.parent:
			self._tree = node.right
			node.right.parent = None
		elif AvlTree._is_left(node):
			node.parent.left = node.right
			node.right.parent = node.parent
		else:
			node.parent.right = node.right
			node.right.parent = node.parent
		bk = node.right.left
		node.right.left = node
		node.parent = node.right
		node.right = bk
		if bk:
			bk.parent = node
		node.height = max(self.get_height(node.left), self.get_height(node.right)) + 1
		node.num_total = 1 + self.get_num_total(node.left) + self.get_num_total(node.right)
		node.num_left = 1 + self.get_num_total(node.left)

	@staticmethod
	def _get_next(node):
		if not node.right:
			return node.parent
		n = node.right
		while n.left:
			n = n.left
		return n
avl = AvlTree()

class SegmentTree1:

	def __init__(self, data, default='z', func=lambda a, b: min(a, b)):
		self._default = default
		self._func = func
		self._len = len(data)
		self._size = _size = 1 << (self._len - 1).bit_length()
		self.data = [default] * (2 * _size)
		self.data[_size:_size + self._len] = data
		for i in reversed(range(_size)):
			self.data[i] = func(self.data[i + i], self.data[i + i + 1])

	def __delitem__(self, idx):
		self[idx] = self._default

	def __getitem__(self, idx):
		return self.data[idx + self._size]

	def __setitem__(self, idx, value):
		idx += self._size
		self.data[idx] = value
		idx >>= 1
		while idx:
			self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1])
			idx >>= 1

	def __len__(self):
		return self._len

	def query(self, start, stop):
		if start == stop:
			return self.__getitem__(start)
		stop += 1
		start += self._size
		stop += self._size
		res = self._default
		while start < stop:
			if start & 1:
				res = self._func(res, self.data[start])
				start += 1
			if stop & 1:
				stop -= 1
				res = self._func(res, self.data[stop])
			start >>= 1
			stop >>= 1
		return res

	def __repr__(self):
		return 'SegmentTree({0})'.format(self.data)

class SegmentTree:

	def __init__(self, data, default=0, func=lambda a, b: a + b):
		self._default = default
		self._func = func
		self._len = len(data)
		self._size = _size = 1 << (self._len - 1).bit_length()
		self.data = [default] * (2 * _size)
		self.data[_size:_size + self._len] = data
		for i in reversed(range(_size)):
			self.data[i] = func(self.data[i + i], self.data[i + i + 1])

	def __delitem__(self, idx):
		self[idx] = self._default

	def __getitem__(self, idx):
		return self.data[idx + self._size]

	def __setitem__(self, idx, value):
		idx += self._size
		self.data[idx] = value
		idx >>= 1
		while idx:
			self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1])
			idx >>= 1

	def __len__(self):
		return self._len

	def query(self, start, stop):
		if start == stop:
			return self.__getitem__(start)
		stop += 1
		start += self._size
		stop += self._size
		res = self._default
		while start < stop:
			if start & 1:
				res = self._func(res, self.data[start])
				start += 1
			if stop & 1:
				stop -= 1
				res = self._func(res, self.data[stop])
			start >>= 1
			stop >>= 1
		return res

	def __repr__(self):
		return 'SegmentTree({0})'.format(self.data)

class Factorial:

	def __init__(self, MOD):
		self.MOD = MOD
		self.factorials = [1, 1]
		self.invModulos = [0, 1]
		self.invFactorial_ = [1, 1]

	def calc(self, n):
		if n <= -1:
			print('Invalid argument to calculate n!')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		if n < len(self.factorials):
			return self.factorials[n]
		nextArr = [0] * (n + 1 - len(self.factorials))
		initialI = len(self.factorials)
		prev = self.factorials[-1]
		m = self.MOD
		for i in range(initialI, n + 1):
			prev = nextArr[i - initialI] = prev * i % m
		self.factorials += nextArr
		return self.factorials[n]

	def inv(self, n):
		if n <= -1:
			print('Invalid argument to calculate n^(-1)')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		p = self.MOD
		pi = n % p
		if pi < len(self.invModulos):
			return self.invModulos[pi]
		nextArr = [0] * (n + 1 - len(self.invModulos))
		initialI = len(self.invModulos)
		for i in range(initialI, min(p, n + 1)):
			next = -self.invModulos[p % i] * (p // i) % p
			self.invModulos.append(next)
		return self.invModulos[pi]

	def invFactorial(self, n):
		if n <= -1:
			print('Invalid argument to calculate (n^(-1))!')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		if n < len(self.invFactorial_):
			return self.invFactorial_[n]
		self.inv(n)
		nextArr = [0] * (n + 1 - len(self.invFactorial_))
		initialI = len(self.invFactorial_)
		prev = self.invFactorial_[-1]
		p = self.MOD
		for i in range(initialI, n + 1):
			prev = nextArr[i - initialI] = prev * self.invModulos[i % p] % p
		self.invFactorial_ += nextArr
		return self.invFactorial_[n]

class Combination:

	def __init__(self, MOD):
		self.MOD = MOD
		self.factorial = Factorial(MOD)

	def ncr(self, n, k):
		if k < 0 or n < k:
			return 0
		k = min(k, n - k)
		f = self.factorial
		return f.calc(n) * f.invFactorial(max(n - k, k)) * f.invFactorial(min(k, n - k)) % self.MOD

def powm(a, n, m):
	if a == 1 or n == 0:
		return 1
	if n % 2 == 0:
		s = powm(a, n // 2, m)
		return s * s % m
	else:
		return a * powm(a, n - 1, m) % m

def sort_list(list1, list2):
	zipped_pairs = zip(list2, list1)
	z = [x for (_, x) in sorted(zipped_pairs)]
	return z

def product(l):
	por = 1
	for i in range(len(l)):
		por *= l[i]
	return por

def binarySearchCount(arr, n, key):
	left = 0
	right = n - 1
	count = 0
	while left <= right:
		mid = int((right + left) / 2)
		if arr[mid] <= key:
			count = mid + 1
			left = mid + 1
		else:
			right = mid - 1
	return count

def countdig(n):
	c = 0
	while n > 0:
		n //= 10
		c += 1
	return c

def countGreater(arr, n, k):
	l = 0
	r = n - 1
	leftGreater = n
	while l <= r:
		m = int(l + (r - l) / 2)
		if arr[m] >= k:
			leftGreater = m
			r = m - 1
		else:
			l = m + 1
	return n - leftGreater
for ik in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	ans = 0
	t = 0
	r = 0
	i = 1
	while i < n:
		if l[i] < l[i - 1]:
			r = 0
			if t == 0:
				t = 1
			else:
				i += 1
				continue
		elif t == 1 and l[i] > l[i - 1]:
			if r != 0:
				ans = r - 1
			else:
				ans = i - 1
			t = 0
			r = 0
		elif l[i] == l[i - 1]:
			if r == 0:
				r = i
		i += 1
	print(ans)


def t(l):
	k = 0
	for i in range(len(l) - 1, 0, -1):
		if l[i] > l[i - 1]:
			k = i
			break
	k2 = 0
	for i in range(k, 0, -1):
		if l[i] < l[i - 1]:
			k2 = i
			break
	return k2
n = int(input())
for j in range(n):
	m = int(input())
	l = list(map(int, input().split()))
	print(t(l))


def solution(a):
	max_i = len(a) - 1
	priv = a[-1]
	i = len(a) - 2
	while i >= 0:
		if a[i] >= priv:
			priv = a[i]
			max_i = i
		else:
			break
		i -= 1
	lower_j = max_i
	if max_i > 0:
		priv = a[max_i]
		j = max_i - 1
		while j >= 0:
			if priv < a[j]:
				lower_j = j + 1
				break
			priv = a[j]
			j -= 1
		if j == -1:
			lower_j = 0
	return lower_j
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	result = solution(a)
	print(result)


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	ans = []
	decresing = False
	for i in range(n - 1, 0, -1):
		if arr[i] > arr[i - 1]:
			decresing = True
		elif arr[i] < arr[i - 1] and decresing == False:
			pass
		elif arr[i] == arr[i - 1]:
			pass
		else:
			print(i)
			break
	else:
		print(0)


t = int(input())
for i in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	if n == 2 or n == 1:
		print(0)
		continue
	prev = 0
	output = -1
	for j in reversed(range(n - 1)):
		if prev == 0:
			if arr[j] < arr[j + 1]:
				prev = 1
		if prev == 1:
			if arr[j] > arr[j + 1]:
				output = j
				break
	print(output + 1)


for t in range(int(input())):
	n = int(input())
	num = list(map(int, input().split()))
	num1 = num[::-1]
	ind = 0
	for i in range(n - 1):
		if num1[i + 1] >= num1[i]:
			continue
		else:
			ind = i
			break
	else:
		ind = n
	num2 = num[:n - ind]
	num3 = num2[::-1]
	x = len(num3)
	num4 = sorted(num3, reverse=True)
	if num4 == num3:
		print('0')
	else:
		for j in range(x - 1):
			if num3[j + 1] <= num3[j]:
				continue
			else:
				print(x - j - 1)
				break


import sys
tcs = int(input())
for tc in range(tcs):
	n = int(input())
	arr = list(map(int, sys.stdin.readline().strip().split()))
	cnt = 0
	prev = 0
	inc = True
	for i in range(n - 1, -1, -1):
		if arr[i] >= prev and inc:
			cnt += 1
		elif arr[i] > prev and (not inc):
			break
		else:
			inc = False
			cnt += 1
		prev = arr[i]
	print(n - cnt)


t = int(input())
res = []
for i in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	k = n - 1
	while k > 0 and l[k - 1] >= l[k]:
		k -= 1
	while k > 0 and l[k - 1] <= l[k]:
		k -= 1
	res.append(k)
for i in res:
	print(i)


for _ in range(int(input())):
	n = int(input())
	x = list(map(int, input().split()))
	a = []
	b = []
	for i in range(n - 1):
		if x[i] > x[i + 1]:
			a.append(0)
		elif x[i] < x[i + 1]:
			a.append(1)
		else:
			a.append(-1)
	if a.count(0) == 0:
		print(0)
	elif a[-1] == 0 or a[-1] == -1:
		c = False
		while True:
			if a == [] or (c == True and a[-1] == 0):
				break
			if a[-1] == 1:
				c = True
			a.pop()
		print(len(a))
	else:
		k = a[::-1].index(0)
		print(len(a) - k)


inputs_cnt = int(input())
for input_index in range(inputs_cnt):
	arr_length = int(input())
	array = list(map(int, input().split()))
	arr_index = -1
	arr_len = len(array)
	if arr_index + arr_len == 0:
		print(0)
		continue
	while array[arr_index] <= array[arr_index - 1]:
		arr_index -= 1
		if arr_index + arr_len == 0:
			break
	if arr_index + arr_len == 0:
		print(0)
		continue
	while array[arr_index] >= array[arr_index - 1]:
		arr_index -= 1
		if arr_index + arr_len == 0:
			break
	if arr_index + arr_len == 0:
		print(0)
		continue
	else:
		print(arr_index + arr_len)


def solution(arr):
	p = 0
	for i in range(len(arr) - 1, 0, -1):
		if arr[i] > arr[i - 1]:
			p = i
			break
	l = -1
	for j in range(p):
		if arr[j] > arr[j + 1]:
			l = j
	return l + 1
for _ in range(int(input())):
	n = int(input())
	arr = [int(x) for x in input().split()]
	print(solution(arr))


def solve():
	n = int(input())
	a = list(map(int, input().split()))
	id = n - 1
	while id > 0 and a[id - 1] >= a[id]:
		id -= 1
	while id > 0 and a[id - 1] <= a[id]:
		id -= 1
	print(id)
ct = int(input())
while ct > 0:
	solve()
	ct -= 1


def solve(n):
	ans = []
	return ans
t = int(input())
for ___ in range(t):
	ans = 0
	n = int(input())
	inp_str = input()
	a = [int(k) for k in inp_str.split(' ')]
	if n > 2:
		up = True
		p = n - 1
		while up and p > 0:
			p -= 1
			up = a[p] >= a[p + 1]
		if up and p == 0:
			ans = 0
		elif not up and p == 0:
			ans = 0
		else:
			down = True
			while down and p > 0:
				p -= 1
				down = a[p] <= a[p + 1]
			if down:
				ans = 0
			else:
				ans = p + 1
	print(ans)


num_tests = int(input())
for _ in range(num_tests):
	n = int(input())
	a = list(map(int, input().split()))
	pointer = n - 1
	while pointer > 0 and a[pointer - 1] >= a[pointer]:
		pointer -= 1
	while pointer > 0 and a[pointer - 1] <= a[pointer]:
		pointer -= 1
	print(pointer)


t = int(input())
l = []
for i in range(t):
	n = int(input())
	s = list(map(int, input().split()))
	l.append(s)
for j in l:
	if len(j) == 1:
		print(0)
	else:
		l1 = []
		for k in range(len(j) - 1, 0, -1):
			if j[k] <= j[k - 1]:
				l1.append(j[k])
			else:
				l1.append(j[k])
				ind = k
				break
		for m in range(k - 1, -1, -1):
			if j[m] >= j[m - 1]:
				l1.append(j[m])
			else:
				l1.append(j[m])
				ind = m
				break
		print(m)


from collections import defaultdict
t = int(input())
for q in range(t):
	n = int(input())
	li = list(map(int, input().split()))
	i = n - 1
	while i >= 1 and li[i - 1] >= li[i]:
		i -= 1
	while i >= 1 and li[i - 1] <= li[i]:
		i -= 1
	print(i)


def solve(n, a):
	a.reverse()
	up = True
	curr = a[0]
	for i in range(n):
		x = a[i]
		if curr < x:
			if not up:
				return n - i
		elif curr > x:
			up = False
		curr = x
	return 0
for t in range(int(input())):
	n = int(input())
	a = [int(x) for x in input().split()]
	print(solve(n, a))


import math, sys
from sys import stdin, stdout
from collections import Counter, defaultdict, deque
input = stdin.readline
I = lambda : int(input())
li = lambda : list(map(int, input().split()))

def solve():
	n = I()
	a = li()
	if n == 1:
		print(0)
		return 0
	m = 0
	k = n - 1
	c = 1
	for i in range(n - 2, -1, -1):
		if a[i] >= a[i + 1]:
			k = i
			c += 1
		else:
			break
	p = k
	for i in range(k - 1, -1, -1):
		if a[i] <= a[i + 1]:
			p = i
			c += 1
		else:
			break
	print(n - c)
for _ in range(I()):
	solve()


def solve():
	n = int(input())
	a = list(map(int, input().split()))
	while len(a) > 1 and a[-2] >= a[-1]:
		a.pop()
	while len(a) > 1 and a[-2] <= a[-1]:
		a.pop()
	print(len(a) - 1)
	return

def main():
	t = int(input())
	for i in range(t):
		solve()
	return
main()


t = int(input())

def non_decreasing():
	for i in range(1, n):
		if arr[i] >= arr[i - 1]:
			continue
		else:
			return False
	return True
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	if n == 1 or non_decreasing():
		print(0)
	else:
		increasing = False
		idx = None
		prev = arr[n - 1]
		for i in range(n - 2, -1, -1):
			if increasing == False:
				if arr[i] >= prev:
					prev = arr[i]
				else:
					increasing = True
					prev = arr[i]
			elif arr[i] <= prev:
				prev = arr[i]
			else:
				idx = i
				break
		if idx != None:
			print(i + 1)
		else:
			print(0)


def solve(size, arr):
	i = size - 1
	is_down = False
	is_up = False
	while i > 0:
		curr = arr[i]
		prev = arr[i - 1]
		if curr > prev:
			is_up = True
		elif curr < prev:
			is_down = True
		if is_up and is_down and (curr < prev):
			break
		i -= 1
	print(i)

def main():
	n = int(input())
	for _ in range(n):
		size = int(input())
		arr = list(map(int, input().split()))
		solve(size, arr)
main()


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	i = n - 2
	incele = 1
	while i >= 0:
		if a[i] <= a[i + 1]:
			incele += 1
		else:
			break
		i -= 1
	decele = 1
	i = n - 2
	while i >= 0:
		if a[i] >= a[i + 1]:
			decele += 1
		else:
			break
		i -= 1
	hillcnt = 1
	j = n - 2
	while j >= 0:
		if a[j] >= a[j + 1]:
			hillcnt += 1
		else:
			break
		j -= 1
	while j >= 0:
		if a[j] <= a[j + 1]:
			hillcnt += 1
		else:
			break
		j -= 1
	print(n - max(1, decele, incele, hillcnt))


def solve(A, n):
	pos = n - 1
	while pos > 0 and A[pos - 1] >= A[pos]:
		pos -= 1
	while pos > 0 and A[pos - 1] <= A[pos]:
		pos -= 1
	return pos
res = []
t = int(input())
while t:
	n = int(input())
	arr = input().split(' ')
	A = [int(n) for n in arr]
	res.append(solve(A, n))
	t -= 1
for r in res:
	print(r)


from sys import stdin
input = stdin.readline
T = int(input())

def isGood(A, start, end):
	flag = True
	for i in range(start, end - 1):
		if flag:
			if A[i] > A[i + 1]:
				flag = False
		elif A[i] < A[i + 1]:
			return False
	return True
for _ in range(T):
	N = int(input())
	A = list(map(int, input().split()))
	(start, end) = (0, N - 1)
	while start < end:
		mid = (start + end) // 2
		if isGood(A, mid, N):
			end = mid
		else:
			start = mid + 1
	print(start)


for _ in range(int(input())):
	n = int(input())
	A = [int(a) for a in input().split()]
	p = 0
	last = A[n - 1]
	break_flag = -1
	for i in range(n - 1, 0, -1):
		if A[i - 1] >= A[i]:
			continue
		else:
			break_flag = i
			break
	if break_flag != -1:
		for i in range(break_flag - 1, -1, -1):
			if A[i] >= A[i - 1]:
				continue
			else:
				p = i
				break
	print(p)


def num():
	return int(input())

def line():
	return map(int, input().split())

def ans(lst):
	return ' '.join(map(str, lst))
from itertools import repeat
t = num()
for _ in repeat(None, t):
	n = num()
	a = list(line())
	if len(a) < 2:
		print(0)
		continue
	for i in range(n - 2, -1, -1):
		if a[i] < a[i + 1]:
			q = i
			break
	else:
		q = -1
	for i in range(q, -1, -1):
		if a[i] > a[i + 1]:
			q = i + 1
			break
	else:
		q = 0
	print(q)


t = int(input())
for i in range(t):
	n = int(input())
	p = list(map(int, input().split()))
	up = False
	down = False
	if n == 1 or n == 2:
		print(0)
	elif p[-2] >= p[-1]:
		i = -3
		while i * -1 <= n and p[i] >= p[i + 1]:
			i -= 1
		while i * -1 <= n and p[i] <= p[i + 1]:
			i -= 1
		print(n - i * -1 + 1)
	else:
		i = -3
		while i * -1 <= n and p[i] <= p[i + 1]:
			i -= 1
		print(n - i * -1 + 1)


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	i = 0
	j = n - 1
	ans = 0
	sind = 0
	dicts = {}
	sorted_arr = sorted(arr)
	while i < j:
		if arr[i] == sorted_arr[sind]:
			i += 1
			sind += 1
		elif arr[j] == sorted_arr[sind]:
			j -= 1
			sind += 1
		elif dicts.get(sorted_arr[sind], None) != None:
			dicts[sorted_arr[sind]] -= 1
			if dicts[sorted_arr[sind]] == 0:
				dicts.pop(sorted_arr[sind])
			sind += 1
		else:
			if dicts.get(arr[i], None) == None:
				dicts[arr[i]] = 0
			dicts[arr[i]] += 1
			i += 1
			ans = i
	print(ans)


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


from collections import deque
import sys
input = sys.stdin.buffer.readline

def solve(mid):
	q = deque(a[mid:n])
	val = 0
	while q:
		if q[0] < val or q[-1] < val:
			return False
		if q[0] > q[-1]:
			val = q.pop()
		else:
			val = q.popleft()
	return True
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	if solve(0):
		print(0)
		continue
	ok = n - 1
	ng = 0
	while abs(ok - ng) > 1:
		mid = (ok + ng) // 2
		if solve(mid):
			ok = mid
		else:
			ng = mid
	print(ok)


for _ in range(int(input())):
	n = int(input())
	list1 = list(map(int, input().split()))
	f = 0
	x = list1[n - 1]
	z = -1
	for i in range(n - 2, -1, -1):
		if f == 0:
			if list1[i] >= x:
				x = list1[i]
				continue
			else:
				f = 1
				x = list1[i]
		if f == 1:
			if list1[i] <= x:
				x = list1[i]
				continue
			else:
				z = i
				break
	print(z + 1)


def makeitgood(arr):
	is_dec = False
	for i in range(len(arr) - 1, 0, -1):
		if arr[i] > arr[i - 1]:
			is_dec = True
		if is_dec and arr[i] < arr[i - 1]:
			return i
	return 0
t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	print(makeitgood(arr))


def gcd(a, b):
	if b == 0:
		return a
	return gcd(b, a % b)

def lcm(a, b):
	return a * b / gcd(a, b)

def read(func=int):
	return map(func, input().split())
t = int(input())
for _ in range(t):
	n = int(input()) - 1
	nums = list(read())
	peek = False
	for i in range(n, -1, -1):
		if nums[i - 1] < nums[i]:
			peek = True
		elif nums[i - 1] == nums[i]:
			continue
		elif peek:
			break
	print(i)


test = int(input())
for i in range(test):
	n = int(input())
	arr = [int(i) for i in input().split()]
	j = n - 1
	flag = 1
	while j > 0:
		if arr[j] > arr[j - 1]:
			flag = 0
			break
		j -= 1
	while j > 0:
		if arr[j] < arr[j - 1]:
			flag = 0
			break
		j -= 1
	if not flag:
		ans = j
	else:
		ans = 0
	print(ans)


import bisect
import decimal
from decimal import Decimal
import os
from collections import Counter
import bisect
from collections import defaultdict
import math
import random
import heapq
from math import sqrt
import sys
from functools import reduce, cmp_to_key
from collections import deque
import threading
from itertools import combinations
from io import BytesIO, IOBase
from itertools import accumulate
mod = 10 ** 9 + 7
decimal.getcontext().prec = 46

def primeFactors(n):
	prime = set()
	while n % 2 == 0:
		prime.add(2)
		n = n // 2
	for i in range(3, int(math.sqrt(n)) + 1, 2):
		while n % i == 0:
			prime.add(i)
			n = n // i
	if n > 2:
		prime.add(n)
	return list(prime)

def getFactors(n):
	factors = []
	i = 1
	while i <= math.sqrt(n):
		if n % i == 0:
			if n // i == i:
				factors.append(i)
			else:
				factors.append(i)
				factors.append(n // i)
		i = i + 1
	return factors

def modefiedSieve():
	mx = 10 ** 7 + 1
	sieve = [-1] * mx
	for i in range(2, mx):
		if sieve[i] == -1:
			sieve[i] = i
			for j in range(i * i, mx, i):
				if sieve[j] == -1:
					sieve[j] = i
	return sieve

def SieveOfEratosthenes(n):
	prime = [True for i in range(n + 1)]
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p += 1
	num = []
	for p in range(2, n + 1):
		if prime[p]:
			num.append(p)
	return num

def lcm(a, b):
	return a * b // math.gcd(a, b)

def sort_dict(key_value):
	return sorted(key_value.items(), key=lambda kv: (kv[1], kv[0]), reverse=True)

def list_input():
	return list(map(int, input().split()))

def num_input():
	return map(int, input().split())

def string_list():
	return list(input())

def decimalToBinary(n):
	return bin(n).replace('0b', '')

def binaryToDecimal(n):
	return int(n, 2)

def DFS(n, s, adj):
	visited = [False for i in range(n + 1)]
	stack = []
	stack.append(s)
	while len(stack):
		s = stack[-1]
		stack.pop()
		if not visited[s]:
			visited[s] = True
		for node in adj[s]:
			if not visited[node]:
				stack.append(node)

def maxSubArraySum(a, size):
	max_so_far = -sys.maxsize - 1
	max_ending_here = 0
	start = 0
	end = 0
	s = 0
	for i in range(0, size):
		max_ending_here += a[i]
		if max_so_far < max_ending_here:
			max_so_far = max_ending_here
			start = s
			end = i
		if max_ending_here < 0:
			max_ending_here = 0
			s = i + 1
	return (max_so_far, start, end)

def lis(arr):
	n = len(arr)
	lis = [1] * n
	for i in range(1, n):
		for j in range(0, i):
			if arr[i] >= arr[j] and lis[i] < lis[j] + 1:
				lis[i] = lis[j] + 1
	maximum = 0
	for i in range(n):
		maximum = max(maximum, lis[i])
	return maximum

def solve():
	n = int(input())
	arr = list_input()
	end = n - 1
	while end - 1 >= 0 and arr[end - 1] >= arr[end]:
		end -= 1
	while end - 1 >= 0 and arr[end - 1] <= arr[end]:
		end -= 1
	end -= 1
	if end < 0:
		print(0)
	else:
		print(end + 1)
t = 1
t = int(input())
for _ in range(t):
	solve()


import sys

def readStr():
	return sys.stdin.readline().rstrip()

def readInts():
	return list(map(int, readStr().split(' ')))

def main(n, a):
	if n <= 1:
		return 0
	direction = 'increase'
	for i in range(n - 1):
		if direction == 'increase':
			if a[-i - 2] >= a[-i - 1]:
				pass
			else:
				direction = 'decrease'
		elif a[-i - 2] <= a[-i - 1]:
			pass
		else:
			break
	else:
		return 0
	return n - i - 1
(t,) = readInts()
for _ in range(t):
	(n,) = readInts()
	a = readInts()
	k = main(n, a)
	print(k)


for cases in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	i = n - 1
	while i > 0 and arr[i - 1] >= arr[i]:
		i -= 1
	while i > 0 and arr[i - 1] <= arr[i]:
		i -= 1
	print(i)


import sys
from collections import deque
input = sys.stdin.readline
t = int(input())
for _ in range(t):
	k = int(input())
	a = list(map(int, input().split()))
	prefix = []
	suffix = []
	s = -1
	c = 0
	index = float('inf')
	sor = sorted(a)
	if sor == a:
		print(0)
		continue
	elif sor == a[::-1]:
		print(0)
		continue
	for i in range(1, k):
		prefix.append(a[i] - a[i - 1])
	s = 0
	for i in range(-2, -k - 1, -1):
		suffix.append(a[i] - a[i + 1])
	suffix = suffix[::-1]
	switch = False
	ans = 0
	for i in range(-1, -k, -1):
		if suffix[i] >= 0 and switch == False:
			ans += 1
			continue
		else:
			switch = True
		if switch and suffix[i] <= 0:
			ans += 1
		else:
			break
	print(k - ans - 1)


import sys
input = sys.stdin.readline
n = int(input())
for _ in range(n):
	m = int(input())
	vals = list(map(int, input().split()))
	r = len(vals) - 1
	while r > 0 and vals[r - 1] >= vals[r]:
		r -= 1
	while r > 0 and vals[r - 1] <= vals[r]:
		r -= 1
	print(r)


def main():
	t = int(input())
	for _ in range(t):
		n = int(input())
		arr = list(map(int, input().strip().split(' ')))
		i = n - 1
		while i >= 1 and arr[i - 1] >= arr[i]:
			i -= 1
		while i >= 1 and arr[i - 1] <= arr[i]:
			i -= 1
		print(i)
main()


from sys import stdin, stdout
cin = stdin.readline
cout = stdout.write
mp = lambda : list(map(int, cin().split()))
(t,) = mp()
for _ in range(t):
	(n,) = mp()
	a = mp()
	f1 = f2 = 0
	m = 0
	for i in range(n - 1, 0, -1):
		if a[i] <= a[i - 1] and (not f1):
			continue
		elif a[i] >= a[i - 1] and (not f2):
			f1 = 1
		else:
			m = i
			break
	cout(str(m) + '\n')


for i in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	p = n - 1
	flg = 0
	for i in range(n - 1, 0, -1):
		if a[i - 1] < a[i]:
			p = i
			flg = 1
			break
	if flg == 0:
		print(0)
	else:
		d = 0
		for i in range(p, 0, -1):
			if a[i - 1] > a[i]:
				d = i
				break
		print(d)


import math
LI = lambda : list(map(int, input().split()))
MI = lambda : map(int, input().split())
SI = lambda : input()
II = lambda : int(input())
t = II()
for q in range(t):
	n = II()
	a = LI()[::-1]
	i = 1
	while i < n and a[i] >= a[i - 1]:
		i += 1
	while i < n and a[i] <= a[i - 1]:
		i += 1
	ans = i
	print(n - max(ans, i))


import sys, collections

def check(num):
	temp = collections.deque()
	res = []
	for i in range(num, len(arr)):
		temp.append(arr[i])
	while temp:
		first = temp[0]
		last = temp[-1]
		if first < last:
			res.append(temp.popleft())
		else:
			res.append(temp.pop())
	if res == sorted(res):
		return True
	else:
		return False
tc = int(sys.stdin.readline())
for _ in range(tc):
	n = int(sys.stdin.readline())
	arr = list(map(int, sys.stdin.readline().split()))
	if arr == sorted(arr):
		print(0)
		continue
	left = 0
	right = len(arr)
	while left < right:
		mid = (left + right) // 2
		if check(mid):
			right = mid
		else:
			left = mid + 1
	print(right)


t = int(input())
for _ in range(t):
	n = int(input())
	li = list(map(int, input().split()))
	c = 1
	for i in range(len(li) - 1, 0, -1):
		if li[i] <= li[i - 1]:
			c = c + 1
		else:
			break
	if c == len(li):
		print(0)
		continue
	li = li[0:len(li) - c]
	p = 0
	for i in range(1, len(li)):
		if li[i] < li[i - 1]:
			p = i
	print(p)


import sys
input = sys.stdin.readline

def fu(a):
	for i in range(len(a) - 1):
		if a[i] > a[i + 1]:
			return i
	return len(a) - 1

def fur(a):
	for i in range(len(a) - 1):
		if a[i] < a[i + 1]:
			return i
	return len(a) - 1
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	if n < 3:
		print(0)
	else:
		b = a[::-1]
		d = fu(b)
		e = fur(b[d:]) + d
		print(n - e - 1)


for _ in range(int(input())):
	i_liczb = int(input())
	liczby = [0] + [int(x) for x in input().split()]
	poz = i_liczb
	while poz > 1 and liczby[poz - 1] >= liczby[poz]:
		poz -= 1
	while poz > 1 and liczby[poz - 1] <= liczby[poz]:
		poz -= 1
	print(poz - 1)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	le = 0
	flag = True
	for i in range(n - 1, 0, -1):
		if a[i] <= a[i - 1] and flag:
			le = i - 1
		elif flag:
			flag = False
		if a[i] >= a[i - 1] and (not flag):
			le = i - 1
		elif not flag:
			break
	print(le)


n = int(input())
while n:
	n -= 1
	z = int(input())
	count = z - 1
	lst = list(map(int, input().split()))
	while count > 0 and lst[count - 1] >= lst[count]:
		count -= 1
	while count > 0 and lst[count - 1] <= lst[count]:
		count -= 1
	print(count)


i = int(input())
while i > 0:
	i = i - 1
	c = int(input())
	v = list(map(int, input().strip().split()))[:c]
	f = 1
	while f < c and v[c - f - 1] >= v[c - f]:
		f = f + 1
	while f < c and v[c - f - 1] <= v[c - f]:
		f = f + 1
	print(c - f)


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


for k in range(int(input())):
	n = int(input())
	l = [int(p) for p in input().split()]
	c = 0
	k = 0
	for i in range(n - 1, 0, -1):
		if l[i] <= l[i - 1]:
			c += 1
			continue
		else:
			k = i
			break
	for i in range(k, 0, -1):
		if l[i] >= l[i - 1]:
			c += 1
			continue
		else:
			break
	print(n - c - 1)


def read_list():
	return list(map(int, input().split(' ')))

def print_list(l):
	print(' '.join(map(str, l)))
N = int(input())
for _ in range(N):
	n = int(input())
	a = read_list()
	s = [0]
	for i in range(n - 1, -1, -1):
		if a[i] >= s[-1]:
			s.append(a[i])
		else:
			break
	s.pop(0)
	k = n - len(s) - 1
	for i in range(k, -1, -1):
		if a[i] <= s[-1]:
			s.append(a[i])
		else:
			break
	print(n - len(s))


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	r = n
	for r in range(n - 2, -1, -1):
		if a[r] < a[r + 1]:
			r += 1
			break
	l = 0
	for i in range(1, r):
		if a[i] < a[i - 1]:
			l = i
	print(l)


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	x = sorted(a)
	if x == a:
		print(0)
	else:
		i = n - 1
		while i > 0 and a[i - 1] >= a[i]:
			i = i - 1
		while i > 0 and a[i - 1] <= a[i]:
			i = i - 1
		print(i)


for t in range(int(input())):
	n = int(input())
	A = list(map(int, input().split()))
	startB = len(A) - 1
	while startB > 0:
		if A[startB - 1] >= A[startB]:
			startB -= 1
		else:
			break
	startA = startB - 1
	while startA > 0:
		if A[startA - 1] <= A[startA]:
			startA -= 1
		else:
			break
	print(max(startA, 0))


cases = int(input())
for t in range(cases):
	n = int(input())
	a = list(map(int, input().split()))
	a.append(a[-1] - 1)
	top = n - 1
	for i in range(n, 0, -1):
		if a[i - 1] >= a[i]:
			top = i - 1
		else:
			break
	start = top
	for i in range(top, 0, -1):
		if a[i - 1] <= a[i]:
			start = i - 1
		else:
			break
	print(start)


for _ in range(int(input())):
	n = int(input())
	lis = list(map(int, input().split()))
	high = n - 1
	while high - 1 >= 0 and lis[high - 1] >= lis[high]:
		high -= 1
	while high - 1 >= 0 and lis[high - 1] <= lis[high]:
		high -= 1
	print(high)


def solve(n, ar):
	ptr = n - 1
	while ptr > 0 and ar[ptr] <= ar[ptr - 1]:
		ptr -= 1
	while ptr > 0 and ar[ptr] >= ar[ptr - 1]:
		ptr -= 1
	print(ptr)
t = int(input())
for _ in range(t):
	n = int(input())
	ar = list(map(int, input().split()))
	solve(n, ar)


for t in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	(incr, decr) = (None, None)
	if n == 1:
		print('0')
	else:
		index = n - 2
		while index >= 0 and arr[index] >= arr[index + 1]:
			index = index - 1
		while index >= 0 and arr[index] <= arr[index + 1]:
			index = index - 1
		print(index + 1)


import sys
input = sys.stdin.buffer.readline

def print(val):
	sys.stdout.write(str(val) + '\n')

def prog():
	for _ in range(int(input())):
		n = int(input())
		a = [10 ** 9] + list(map(int, input().split()))
		for i in range(n - 1, -1, -1):
			if a[i] < a[i + 1]:
				break
		peak = i
		for i in range(peak, -1, -1):
			if a[i] > a[i + 1]:
				break
		print(i)
prog()


import os
import operator
import sys
from io import BytesIO, IOBase

def inar():
	return [int(k) for k in input().split()]

def main():
	for _ in range(int(input())):
		n = int(input())
		arr = inar()
		arr = arr[::-1]
		ans = 0
		c1 = 0
		c2 = 0
		for i in range(1, n):
			if c1 == 0 and arr[i] >= arr[i - 1]:
				continue
			else:
				c1 = 1
			if c1 == 1 and arr[i] <= arr[i - 1]:
				continue
			else:
				ans = n - (i - 1) - 1
				break
		print(ans)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


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


t = int(input())
for k in range(t):
	n = int(input())
	(*a,) = map(int, input().split())
	ans = 0
	pref_right = [0] * (n + 1)
	pref_left = [a[0]] + [0] * n
	for i in range(n - 1, -1, -1):
		pref_right[i] = max(pref_right[i + 1], a[i])
	for i in range(1, n):
		pref_left[i] = max(pref_left[i - 1], a[i])
	ans = 0
	for i in range(n - 1, 0, -1):
		if a[i] < a[i - 1] and a[i] < pref_right[i]:
			ans = i
			break
	print(ans)


for _ in range(int(input())):
	n = int(input())
	a = [int(x) for x in input().split()]
	if n <= 2:
		print(0)
	else:
		did_drop = a[n - 1] > a[n - 2]
		for i in range(n - 2, 0, -1):
			will_rise = a[i] < a[i - 1]
			if did_drop and will_rise:
				print(i)
				break
			if i == 1:
				print(0)
			if not did_drop and (not will_rise):
				did_drop = a[i] > a[i - 1]


T = int(input())
for t in range(T):
	n = int(input())
	a = [int(x) for x in input().split(' ')]
	if len(a) <= 2:
		print(0)
		continue
	overPeak = False
	prev = a[-1]
	cutoff = 0
	for i in range(len(a) - 1, -1, -1):
		if a[i] > prev:
			if overPeak:
				cutoff = i + 1
				break
		if a[i] < prev and (not overPeak):
			overPeak = True
		prev = a[i]
	print(cutoff)


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	if n <= 2:
		print(0)
		continue
	xf = 0
	f = -1
	p = 0
	for i in range(n - 2, -1, -1):
		if xf == 0 and l[i] - l[i + 1] < 0:
			xf = -1
		elif xf == 0 and l[i] - l[i + 1] > 0:
			xf = 1
		if xf == -1:
			if l[i] > l[i + 1]:
				f = i
				break
		if xf == 1:
			if l[i] < l[i + 1]:
				p = 1
			elif p == 1 and l[i] > l[i + 1]:
				f = i
				break
	print(f + 1)


from collections import deque
from collections import OrderedDict
import math
import sys
import os
import threading
import bisect
import operator
import heapq
from atexit import register
from io import BytesIO
import io
input = lambda : sys.stdin.readline().rstrip('\r\n')
for t in range(int(input())):
	n = int(input())
	ar = list(map(int, input().split()))
	l = 0
	r = n - 1
	answer = 0
	while l <= r:
		c = l + (r - l) // 2
		l1 = c
		r1 = n - 1
		cur = 0
		cur = min(ar[l1], ar[n - 1])
		while l1 <= r1:
			if cur > ar[l1] or cur > ar[r1]:
				l = c + 1
				break
			if ar[l1] <= ar[r1]:
				cur = ar[l1]
				l1 += 1
			else:
				cur = ar[r1]
				r1 -= 1
		else:
			r = c - 1
			answer = c
	print(answer)


t = int(input())
for _ in range(t):
	n = int(input())
	a = list((int(x) for x in input().split()))
	start = 0
	end = n - 1
	while end > 0 and a[end] <= a[end - 1]:
		end -= 1
	start = end
	while start > 0 and a[start] >= a[start - 1]:
		start -= 1
	print(start)


T = int(input())
for t in range(T):
	n = int(input())
	A = [int(i) for i in input().split()]
	if n <= 2:
		print(0)
		continue
	diff = [A[i] - A[i - 1] for i in range(1, n)]
	positive_flag = False
	for i in range(n - 2, -1, -1):
		if diff[i] > 0:
			positive_flag = True
		if positive_flag and diff[i] < 0:
			print(i + 1)
			break
	else:
		print(0)


def process():
	n = int(input())
	arr = list(map(int, input().split()))
	tol = False
	for i in range(n - 1, 0, -1):
		if arr[i - 1] < arr[i]:
			tol = True
		if tol:
			if arr[i - 1] > arr[i]:
				print(i)
				return
	print(0)
for i in range(int(input())):
	process()


t = int(input())
for test in range(t):
	n = int(input())
	s = input().split()
	s[-1] = int(s[-1])
	dec = False
	found = False
	for i in range(n - 2, -1, -1):
		s[i] = int(s[i])
		if dec and s[i] > s[i + 1]:
			print(i + 1)
			found = True
			break
		elif s[i] < s[i + 1]:
			dec = True
	if not found:
		print(0)


for _ in range(int(input())):
	n = int(input())
	A = list(map(int, input().split()))
	d = []
	for i in range(1, n):
		d.append(A[i] - A[i - 1])
	ca = -1
	ans = 0
	for i in range(len(d)):
		if d[i] < 0:
			ca = i
		if d[i] > 0 and ca >= 0:
			if i - ca > 1:
				ans = ca + 1
				ca = -1
				continue
			ans = i
			ca = -1
	print(ans)


t = int(input())
for _ in range(t):
	n = int(input())
	s = [int(i) for i in input().split()]
	l = s[0]
	h = True
	count = 0
	k = True
	for i in range(1, n):
		if s[i] < l and h:
			l = s[i]
			p = i
			h = False
			continue
		if s[i] > l and (not h):
			count = p
			l = s[i]
			h = True
			continue
		if s[i] <= l and (not h):
			if s[i] < l:
				l = s[i]
				p = i
				k = True
			elif k:
				p = i - 1
				k = False
		if s[i] > l and h:
			l = s[i]
	print(count)


t = int(input())
for i in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	v = n - 1
	for j in range(n - 2, -1, -1):
		if l[j] >= l[j + 1]:
			v = j
			continue
		else:
			v = j
			break
	v1 = v
	b = 0
	for j in range(v - 1, -1, -1):
		b = 0
		if l[j] <= l[j + 1]:
			v1 = j
			continue
		else:
			b = 1
			v1 = j
			break
	if b == 0:
		print(0)
	else:
		print(v1 + 1)


import os, io
from sys import stdout
import collections
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
import sys

def primes(n):
	sieve = [True] * n
	for i in range(3, int(n ** 0.5) + 1, 2):
		if sieve[i]:
			sieve[i * i::2 * i] = [False] * ((n - i * i - 1) // (2 * i) + 1)
	return [2] + [i for i in range(3, n, 2) if sieve[i]]

def binomial_coefficient(n, k):
	if 0 <= k <= n:
		ntok = 1
		ktok = 1
		for t in range(1, min(k, n - k) + 1):
			ntok *= n
			ktok *= t
			n -= 1
		return ntok // ktok
	else:
		return 0

def powerOfK(k, max):
	if k == 1:
		return [1]
	if k == -1:
		return [-1, 1]
	result = []
	n = 1
	while n <= max:
		result.append(n)
		n *= k
	return result

def divisors(n):
	i = 1
	result = []
	while i * i <= n:
		if n % i == 0:
			if n / i == i:
				result.append(i)
			else:
				result.append(i)
				result.append(n / i)
		i += 1
	return result

def distance(xa, ya, xb, yb):
	return ((ya - yb) ** 2 + (xa - xb) ** 2) ** 0.5

def digitsSum(n):
	if n == 0:
		return 0
	r = 0
	while n > 0:
		r += n % 10
		n //= 10
	return r

def isValid(i, j, n, m):
	return i >= 0 and i < n and (j >= 0) and (j < m)

def print_grid(grid):
	for line in grid:
		print(' '.join(map(str, line)))

def kadane(a, size):
	max_so_far = 0
	max_ending_here = 0
	for i in range(0, size):
		max_ending_here = max_ending_here + a[i]
		if max_so_far < max_ending_here:
			max_so_far = max_ending_here
		if max_ending_here < 0:
			max_ending_here = 0
	return max_so_far

def prefixSum(arr):
	for i in range(1, len(arr)):
		arr[i] = arr[i] + arr[i - 1]
	return arr

def ceil(n, d):
	if n % d == 0:
		return n // d
	else:
		return n // d + 1

def _abs(a, b):
	return int(abs(a - b))

def evenodd(l):
	even = [e for e in l if e & 1 == 0]
	odd = [e for e in l if e & 1]
	return (even, odd)
t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	r = l[::-1]
	if len(l) == 1:
		print(0)
	else:
		i = len(l) - 1
		while i >= 0 and l[i - 1] <= l[i]:
			i -= 1
		decreasing = i
		i = len(l) - 1
		while i >= 0 and l[i - 1] >= l[i]:
			i -= 1
		while i >= 0 and l[i - 1] <= l[i]:
			i -= 1
		incdec = i
		r = min(decreasing, incdec)
		if r == -1:
			print(0)
		else:
			print(r)


def solve():
	n = int(input())
	a = list(map(int, input().split()))
	ptr = n - 1
	while ptr > 0 and a[ptr] <= a[ptr - 1]:
		ptr -= 1
	while ptr > 0 and a[ptr] >= a[ptr - 1]:
		ptr -= 1
	return ptr
t = int(input())
while t:
	t -= 1
	print(solve())


def readn():
	return int(input())

def readlist():
	return list(map(int, input().split()))

def readnos():
	return map(int, input().split())
for _ in range(int(input())):
	n = int(input())
	a = readlist()
	if n == 1 or n == 2:
		print(0)
	else:
		f1 = 0
		f2 = 0
		for i in range(n - 1):
			if a[i + 1] >= a[i]:
				f1 += 1
		for i in range(n - 1):
			if a[i + 1] <= a[i]:
				f2 += 1
		if max(f1, f2) == n - 1:
			print(0)
		else:
			f = 0
			i = n - 1
			while i > 0:
				if a[i] < a[i - 1]:
					if f == 1:
						break
				elif a[i] > a[i - 1]:
					f = 1
				i -= 1
			print(i)


t = int(input())
for i in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	lis = []
	for j in range(n - 1):
		if arr[j + 1] > arr[j]:
			lis.append(1)
		elif arr[j + 1] < arr[j]:
			lis.append(-1)
		else:
			lis.append(0)
	x = lis.count(1)
	y = lis.count(-1)
	if x == 0 or y == 0:
		print(0)
	else:
		a = 0
		c = 0
		for j in range(1, n):
			if lis[-j] == 1 or lis[-j] == -1:
				a = lis[-j]
				c = j
				break
		if a == 1:
			b = 0
			for j in range(c + 1, n):
				if lis[-j] == -1:
					b = n - j
					break
			print(b)
		else:
			b = 0
			d = 0
			e = 1
			for j in range(c + 1, n):
				if lis[-j] == 1 and e == 1:
					d = 1
				elif lis[-j] == -1 and d == 1:
					b = n - j
					break
			print(b)


a = int(input())
for k in range(a):
	b = int(input())
	c = list(map(int, input().split()))
	pos = b - 1
	while pos > 0 and c[pos - 1] >= c[pos]:
		pos -= 1
	while pos > 0 and c[pos - 1] <= c[pos]:
		pos -= 1
	print(pos)


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)


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


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	if arr == sorted(arr):
		print(0)
		continue
	elif sorted(arr, reverse=True) == arr:
		print(0)
	else:
		j = n - 1
		while j and arr[j - 1] >= arr[j]:
			j -= 1
		while j and arr[j - 1] <= arr[j]:
			j -= 1
		print(j)


import sys

def get_array():
	return list(map(int, sys.stdin.readline().strip().split()))

def get_ints():
	return map(int, sys.stdin.readline().strip().split())

def input():
	return sys.stdin.readline().strip()
for _ in range(int(input())):
	n = int(input())
	a = get_array()
	a.reverse()
	inc = 1
	count = 0
	f = 0
	for i in range(len(a) - 1):
		if a[i] <= a[i + 1] and inc == 1:
			count += 1
			continue
		elif a[i] < a[i + 1] and inc == 0:
			count += 1
			f = 1
			break
		elif a[i] >= a[i + 1] and inc == 0:
			count += 1
			continue
		elif a[i] > a[i + 1] and inc == 1:
			inc = 0
			count += 1
	if f == 0:
		count += 1
	print(n - count)


t = int(input())
for i in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	a = l[:]
	l.reverse()
	x = l[:]
	x.sort()
	if x == l or x == a:
		print(0)
		continue
	inc = True
	dec = False
	tmp = 0
	for i in range(n - 1):
		if l[i] <= l[i + 1] and inc == True:
			continue
		elif l[i] < l[i + 1] and inc == False:
			tmp = 1
			print(n - i - 1)
			break
		else:
			inc = False
			dec = True
	if tmp == 0:
		print(0)


for ii in range(int(input())):
	n = int(input())
	p = list(map(int, input().split()))
	(co, f) = (1, True)
	for jj in range(n - 1, 0, -1):
		if p[jj - 1] >= p[jj] and f:
			co += 1
		elif p[jj - 1] <= p[jj]:
			co += 1
			f = False
		else:
			break
	print(n - co)


t = int(input())
for i in range(t):
	n = int(input())
	arr = [int(x) for x in input().split()]
	flag = False
	if n <= 2:
		print(0)
	else:
		k = 0
		for i in range(n - 2, -1, -1):
			if flag and arr[i] > arr[i + 1]:
				k = i + 1
				break
			if arr[i] < arr[i + 1]:
				flag = True
		print(k)


for _ in range(int(input())):
	n = int(input())
	arr = list((int(i) for i in input().split()))
	cnt = n - 1
	while cnt > 0 and arr[cnt - 1] >= arr[cnt]:
		cnt -= 1
	while cnt > 0 and arr[cnt - 1] <= arr[cnt]:
		cnt -= 1
	print(cnt)


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	if l == sorted(l):
		print(0)
	else:
		num = []
		f = 0
		(x, y) = (0, 0)
		for i in range(n - 1, 0, -1):
			if l[i - 1] < l[i]:
				x = i - 1
				f = 1
				break
		for i in range(x, 0, -1):
			if l[i - 1] > l[i]:
				y = i
				break
		print(y)


t = int(input())
while t:
	t -= 1
	n = int(input())
	ar = list(map(int, input().split()))
	ar = ar[::-1]
	prev = ar[0]
	for i in range(1, n):
		if prev < ar[i]:
			inc = 1
			break
		elif prev > ar[i]:
			inc = 0
			break
	if len(set(ar)) == 1:
		print(0)
	else:
		end = n - 1
		if inc:
			peak = 0
			end = n - 1
			for i in range(n - 1):
				if not peak:
					if ar[i] > ar[i + 1]:
						peak = 1
				elif ar[i] < ar[i + 1]:
					end = i
					break
		else:
			peak = 0
			end = n - 1
			for i in range(n - 1):
				if not peak:
					if ar[i] < ar[i + 1]:
						peak = 1
						end = i
						break
		print(n - end - 1)


def main():
	for __ in [0] * int(input()):
		N = int(input())
		(*A,) = map(int, input().split())
		p = A[-1]
		for i in range(N - 1, -1, -1):
			if A[i] < p:
				break
			p = A[i]
		for i in range(i, -1, -1):
			if A[i] > p:
				print(i + 1)
				break
			p = A[i]
		else:
			print(i)
main()


a = int(input())
for i in range(a):
	c = int(input())
	x = list(map(int, input().split()))
	j = c - 1
	while x[j] <= x[j - 1] and j > 0:
		j -= 1
	while x[j] >= x[j - 1] and j > 0:
		j -= 1
	print(j)


def f(l, n):
	if n == 1:
		return 0
	i = n - 2
	while l[i] >= l[i + 1]:
		i -= 1
		if i == -1:
			return 0
	while l[i] <= l[i + 1]:
		i -= 1
		if i == -1:
			return 0
	return i + 1
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	print(f(a, n))


import io, os
from collections import deque
RL = lambda : list(map(int, input().split(' ')))
T = int(input())
for _ in range(T):
	n = int(input())
	l = RL()
	ma = l[0]
	a1 = 0
	flag = 0
	for i in range(n - 1, 0, -1):
		if l[i - 1] < l[i]:
			flag = 1
		if flag == 1:
			if l[i - 1] > l[i]:
				a1 = i
				break
	print(a1)


t = int(input())
while t:
	n = int(input())
	l = list(map(int, input().split()))
	i = j = n - 1
	while j > 0 and l[j - 1] >= l[j]:
		j -= 1
	while j > 0 and l[j - 1] <= l[j]:
		j -= 1
	if j == -1:
		print(0)
	else:
		print(j)
	t -= 1


for _ in range(int(input())):
	s = input()
	li = list(map(int, input().split()))
	i = len(li) - 1
	prev = li[i]
	check_dec = False
	all_true = True
	while i > 0:
		i -= 1
		curr = li[i]
		if not check_dec:
			if prev <= curr:
				pass
			else:
				check_dec = True
		elif prev >= curr:
			pass
		else:
			all_true = False
			break
		prev = curr
	if all_true:
		print(0)
	else:
		print(i + 1)


def solve(n, a):
	answ = 0
	left = 0
	right = n - 1
	chosen = -1
	move = -1
	while left < right:
		if chosen == -1:
			if a[left] > a[right]:
				chosen = a[right]
				right -= 1
				move = 1
			else:
				chosen = a[left]
				left += 1
				move = 0
		elif a[left] < chosen or a[right] < chosen:
			if move == 1:
				left += 1
			answ = left
			right = n - 1
			chosen = -1
			move = -1
		elif a[left] > a[right]:
			chosen = a[right]
			right -= 1
			move = 1
		else:
			chosen = a[left]
			left += 1
			move = 0
	print(answ)
t = int(input())
for i in range(t):
	n = int(input())
	a = [int(j) for j in input().split()]
	solve(n, a)


import sys
input = lambda : sys.stdin.readline().strip()
a = int(input())
for i in range(a):
	b = int(input())
	c = list(map(int, input().split()))
	count = 1
	if c == sorted(c):
		print(0)
	else:
		for i in range(b - 1, 0, -1):
			if c[i] <= c[i - 1]:
				count += 1
			else:
				break
		for j in range(i, 0, -1):
			if c[j] >= c[j - 1]:
				count += 1
			else:
				break
		print(max(0, b - count))


t = int(input())
while t:
	t += -1
	n = int(input())
	l = list(map(int, input().split()))
	ind1 = n - 1
	for i in range(n - 2, -1, -1):
		if l[i] >= l[i + 1]:
			ind1 = i
		else:
			break
	ind2 = 0
	for i in range(ind1 - 1, -1, -1):
		if l[i] <= l[i + 1]:
			ind2 = i
		else:
			break
	print(ind2)


q = int(input())
for _ in range(q):
	n = int(input())
	dat = list(map(int, input().split()))
	dat.reverse()
	buf = []
	if n == 1:
		print(0)
		continue
	stat = 0
	cnt = 0
	for i in range(1, n):
		if stat == 0:
			if dat[i] >= dat[i - 1]:
				cnt += 1
				continue
			else:
				stat = 1
				cnt += 1
				continue
		if stat == 1:
			if dat[i] > dat[i - 1]:
				break
			else:
				cnt += 1
				continue
	print(n - cnt - 1)


t = int(input())
for _ in range(t):
	n = int(input())
	l = [int(x) for x in input().split()]
	sam = 0
	samu = 0
	for i in range(n - 1, 0, -1):
		if l[i - 1] < l[i]:
			sam = i
			break
	for i in range(sam, 0, -1):
		if l[i - 1] > l[i]:
			samu = i
			break
	print(samu)


from sys import stdin
input = lambda : stdin.readline().rstrip('\r\n')
from collections import deque as que, defaultdict as vector
from heapq import *
inin = lambda : int(input())
inar = lambda : list(map(int, input().split()))
Testcase = inin()
for i in range(Testcase):
	n = inin()
	a = inar()
	a.reverse()
	st = ''
	if n == 1:
		print(0)
		continue
	if a[0] > a[1]:
		st = 'd'
	else:
		st = 'i'
	for i in range(1, n):
		if a[i] > a[i - 1] and st == 'd':
			print(n - i)
			break
			st = 'i'
		if a[i] < a[i - 1] and st == 'i':
			st = 'd'
	else:
		print(0)


ntcs = int(input())
for tc in range(ntcs):
	n = int(input())
	line = input().split()
	arr = []
	for i in range(n):
		arr.append(int(line[i]))
	j = n - 1
	while j > 0:
		if arr[j] <= arr[j - 1]:
			j -= 1
		else:
			break
	while j > 0:
		if arr[j] >= arr[j - 1]:
			j -= 1
		else:
			break
	print(j)


import sys
input = sys.stdin.buffer.readline
t = int(input())
for _ in range(t):
	n = int(input())
	arr = [int(x) for x in input().split()]
	if n == 1:
		print('0')
		continue
	decreasingPortion = True
	for i in range(n - 2, -1, -1):
		if decreasingPortion and arr[i] < arr[i + 1]:
			decreasingPortion = False
		elif decreasingPortion == False and arr[i] > arr[i + 1]:
			break
		if i == 0:
			i -= 1
	print(i + 1)


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	l1 = l.copy()
	l = l[::-1]
	c = 0
	f = 0
	if len(l) in [0, 1, 2]:
		print(0)
		continue
	x = l1.pop()
	while l1 and l1[-1] == x:
		l1.pop()
	if l1 and x < l1[-1]:
		f = 1
	if not f:
		i = 1
		c = 1
		while i < n and l[i] <= l[i - 1]:
			c += 1
			i += 1
	else:
		i = 1
		c = 1
		while i < n and l[i] >= l[i - 1]:
			c += 1
			i += 1
		while i < n and l[i] <= l[i - 1]:
			c += 1
			i += 1
	print(n - c)


def sol():
	n = int(input())
	l = list(map(int, input().split()))
	x = l[::-1]
	v = n
	b = 0
	for i in range(1, n):
		if b == 0:
			if x[i] < x[i - 1]:
				b += 1
		if b == 1:
			if x[i] > x[i - 1]:
				break
		v -= 1
	print(v - 1)
try:
	for _ in range(int(input())):
		sol()
except:
	pass


import math, sys
from collections import defaultdict, Counter, deque
INF = float('inf')
MOD = 1000000007

def gcd(a, b):
	while b:
		(a, b) = (b, a % b)
	return a

def isPrime(n):
	if n <= 1:
		return False
	i = 2
	while i ** 2 <= n:
		if n % i == 0:
			return False
		i += 1
	return True

def primeFactors(n):
	factors = []
	i = 2
	while i ** 2 <= n:
		while n % i == 0:
			factors.append(i)
			n //= i
		i += 1
	if n > 1:
		factors.append(n)
	return factors

def vars():
	return map(int, input().split())

def array():
	return list(map(int, input().split()))
ans = 0

def dfs(visited, node, digits, tree):
	if not visited[node]:
		visited[node] = True
		digits += str(node)
		leafNode = True
		for child in tree[node]:
			leafNode = False
			dfs(visited, child, digits, tree)
		if leafNode:
			global ans
			ans += int(digits)

def main():
	n = int(input())
	arr = array()
	prev = None
	dec = False
	i = n - 1
	while i >= 0:
		if prev == None:
			prev = arr[i]
		elif not dec and prev <= arr[i]:
			prev = arr[i]
		elif not dec and prev > arr[i]:
			prev = arr[i]
			dec = True
		elif dec and prev > arr[i]:
			prev = arr[i]
		elif dec and prev < arr[i]:
			break
		i -= 1
	print(i + 1)
t = int(input())
for _ in range(t):
	main()


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	f = 0
	x = 0
	y = 0
	for i in range(len(l) - 2, 0, -1):
		x = max(x, l[i + 1])
		if l[i] < x and l[i] < l[i - 1]:
			print(i)
			f = 1
			break
	if f == 0:
		print(0)


t = int(input())
for _ in range(t):
	t = int(input())
	x = input().split()
	i = len(x) - 1
	while i > 0:
		if not int(x[i - 1]) >= int(x[i]):
			break
		i -= 1
	while i > 0:
		if not int(x[i - 1]) <= int(x[i]):
			break
		i -= 1
	print(i)


for _ in range(int(input())):
	n = int(input())
	lst = list(map(int, input().split()))
	j = n - 1
	while j > 0:
		if lst[j - 1] >= lst[j]:
			j -= 1
		else:
			break
	while j > 0:
		if lst[j - 1] <= lst[j]:
			j -= 1
		else:
			break
	if j != 0:
		print(j)
	else:
		print(0)


t = int(input())
for case in range(t):
	n = int(input())
	arr = [int(i) for i in input().split()]
	pos = n - 1
	while arr[pos - 1] >= arr[pos] and pos > 0:
		pos -= 1
	while arr[pos - 1] <= arr[pos] and pos > 0:
		pos -= 1
	print(pos)


t = int(input())
while t:
	n = int(input())
	li = [int(x) for x in input().split()]
	i = len(li) - 1
	while i > 0 and li[i - 1] >= li[i]:
		i -= 1
	while i > 0 and li[i - 1] <= li[i]:
		i -= 1
	print(i)
	t -= 1


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	l.reverse()
	t = 0
	v = 1
	for i in range(1, n):
		if l[i] >= l[i - 1] and t == 0:
			v += 1
		elif l[i] <= l[i - 1] and t == 0:
			v += 1
			t = 1
		elif l[i] <= l[i - 1] and t == 1:
			v += 1
		else:
			break
	print(n - v)


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


tests = int(input())
res = []
for _ in range(tests):
	n = int(input())
	a = list(map(int, input().split()))
	if n < 3:
		res.append(0)
	else:
		dir = 0
		for i in range(n - 2, -1, -1):
			if a[i] > a[i + 1]:
				if dir == 0:
					dir = 1
				elif dir == -1:
					break
			elif a[i] < a[i + 1]:
				if dir >= 0:
					dir = -1
		else:
			res.append(0)
			continue
		res.append(i + 1)
for r in res:
	print(r)


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split(' ')))
	a.reverse()
	going_down = False
	answer = 0
	for i in range(n - 1):
		if a[i] > a[i + 1]:
			going_down = True
		if a[i] < a[i + 1] and going_down:
			answer = n - i - 1
			break
	print(answer)


import sys
input = sys.stdin.readline
from collections import *
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	for i in range(n - 1, 0, -1):
		if a[i] - a[i - 1] > 0:
			for j in range(i - 1, 0, -1):
				if a[j] - a[j - 1] < 0:
					print(j)
					break
			else:
				print(0)
			break
	else:
		print(0)


n = int(input())
for _ in range(n):
	ne = int(input())
	arr = list(map(int, input().split(' ')))
	while ne >= 2:
		if arr[ne - 1] <= arr[ne - 2]:
			ne -= 1
		else:
			break
	while ne >= 2:
		if arr[ne - 1] >= arr[ne - 2]:
			ne -= 1
		else:
			break
	print(ne - 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)


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


rr = lambda : input().strip()
rri = lambda : int(rr())
rrm = lambda : [int(x) for x in rr().split()]
from sys import stdin, stdout, setrecursionlimit

def sol():
	n = rri()
	nums = rrm()
	j = n - 1
	while j > 0:
		if nums[j] <= nums[j - 1]:
			j -= 1
		else:
			break
	while j > 0:
		if nums[j] >= nums[j - 1]:
			j -= 1
		else:
			break
	print(j)
	return

def main():
	T = rri()
	for t in range(1, T + 1):
		ans = sol()
main()


t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(i) for i in input().split()]
	mx = a[n - 1]
	asc = True
	flag_answered = False
	for i in range(n - 2, -1, -1):
		if asc:
			if a[i] < a[i + 1]:
				asc = False
		elif a[i] > a[i + 1]:
			print(i + 1)
			flag_answered = True
			break
	if not flag_answered:
		print(0)


def f(List):
	l = len(List)
	for i in range(l - 1):
		if List[i + 1] < List[i]:
			return i + 1
	else:
		return l

def g(List):
	l = len(List)
	for i in range(l - 1):
		if List[i + 1] > List[i]:
			return i + 1
	else:
		return l
t = int(input())
for test in range(t):
	n = int(input())
	array = list(map(int, input().rstrip().split()))
	array.reverse()
	increasing = f(array)
	if increasing >= n - 1:
		print(0)
	else:
		l = g(array[increasing - 1:])
		print(n + 1 - l - increasing)


def func(a):
	a = list(map(int, a.split()))
	if len(a) <= 2:
		return 0
	check = [a[i + 1] - a[i] for i in range(len(a) - 1)]
	check = [i for i in check if i < 0]
	if len(check) == 0:
		return 0
	a_invert = a.copy()
	a_invert = a_invert[::-1]
	check_inverse = [a_invert[i + 1] - a_invert[i] for i in range(len(a_invert) - 1)]
	diff = [i for i in check_inverse if i < 0]
	if len(diff) == 0:
		return 0
	pivot = check_inverse.index(diff[0])
	pivot = len(a) - pivot - 1
	left_end = [a[i + 1] - a[i] for i in range(pivot)]
	left_end = left_end[::-1]
	diff = [i for i in left_end if i < 0]
	if len(diff) == 0:
		return 0
	cut_position = left_end.index(diff[0])
	return len(left_end) - cut_position

def main():
	t = int(input())
	t_input = []
	for i in range(t):
		input()
		t_input.append(input())
	for i in list(map(func, t_input)):
		print(i)
main()


I = lambda : map(int, input().split())
for _ in [0] * int(input()):
	n = int(input())
	a = list(I())
	ff = 1
	for i in range(n - 1, 0, -1):
		if a[i - 1] < a[i]:
			ff = 0
			lmax = i
			break
	if ff == 1:
		print(0)
	else:
		for i in range(lmax, 0, -1):
			if a[i - 1] > a[i]:
				pre = i
				ff = 1
				break
		if ff == 0:
			print(0)
		else:
			print(pre)


from math import *
from collections import *
from bisect import *
import sys
input = sys.stdin.readline
t = int(input())
while t:
	t -= 1
	n = int(input())
	a = list(map(int, input().split()))
	b = sorted(a)
	if b == a or a[-1::-1] == b:
		print(0)
		continue
	a = a[-1::-1]
	i = 0
	r = 1
	pre = a[0]
	i = 1
	while i < n:
		if a[i] >= pre:
			pre = a[i]
			r += 1
			i += 1
			continue
		pre = a[i]
		i += 1
		r += 1
		break
	while i < n:
		if pre >= a[i]:
			pre = a[i]
			r += 1
			i += 1
			continue
		break
		i += 1
	print(n - r)


for _ in range(int(input())):
	n = int(input())
	lis = list(map(int, input().split()))
	i = n - 2
	while i >= 0 and lis[i] >= lis[i + 1]:
		i -= 1
	i = max(-1, i)
	while i >= 0 and lis[i] <= lis[i + 1]:
		i -= 1
	print(i + 1)


import sys
INP = lambda : sys.stdin.readline().strip()
INT = lambda : int(INP())
MAP = lambda : map(int, INP().split())
ARR = lambda : [int(i) for i in INP().split()]

def JOIN(arr, x=' '):
	return x.join([str(i) for i in arr])

def EXIT(x='NO'):
	print(x)
	exit()

def solve(n, arr):
	rm = arr[-1]
	(M, Mi) = (rm, n - 1)
	for i in reversed(range(n - 1)):
		if arr[i] >= M:
			(M, Mi) = (arr[i], i)
		else:
			break
	(lm, lmi) = (M, Mi)
	for i in reversed(range(Mi)):
		if arr[i] <= lm:
			(lm, lmi) = (arr[i], i)
		else:
			break
	return lmi
for _ in range(INT()):
	n = INT()
	arr = ARR()
	print(solve(n, arr))


for _ in range(int(input())):
	n = int(input())
	a = [int(o) for o in input().split()]
	switch = 0
	inc = True
	l = 0
	flag = False
	for i in range(n - 1, 0, -1):
		if inc:
			if a[i - 1] < a[i]:
				switch += 1
				inc = False
		elif not inc:
			if a[i - 1] > a[i]:
				switch += 1
				inc = True
		l += 1
		if switch > 1:
			flag = True
			break
	if flag:
		print(n - l)
	else:
		print(0)


t = int(input())
for _ in range(t):
	n = int(input())
	array = list(map(int, input().split()))[::-1]
	p = [array[0]]
	min_p = float('inf')
	pr = False
	for i in range(1, n):
		if not pr:
			if p[i - 1] > array[i]:
				pr = True
			p.append(array[i])
		elif p[i - 1] < array[i]:
			break
		else:
			p.append(array[i])
	print(n - len(p))


import sys
import math
from sys import stdin
input = sys.stdin.readline
for _ in range(int(input())):
	n = int(input())
	arr = [int(x) for x in input().split()]
	arr.reverse()
	index = n + 1
	for i in range(1, n):
		if arr[i - 1] <= arr[i]:
			continue
		else:
			index = i
			break
	if index == n + 1:
		print(0)
		continue
	count = n + 1
	for i in range(index + 1, n):
		if arr[i - 1] >= arr[i]:
			continue
		else:
			count = i
			break
	if count == n + 1:
		print(0)
		continue
	print(n - count)


import sys
import math
import heapq
from collections import defaultdict, deque
from decimal import *

def r():
	return int(input())

def rm():
	return map(int, input().split())

def rl():
	return list(map(int, input().split()))
for _ in range(r()):
	n = r()
	a = rl()
	res = True
	for i in range(1, n):
		if a[i] < a[i - 1]:
			res = False
			break
	if res:
		print(0)
	else:
		i = n - 1
		while i > 0:
			if a[i] <= a[i - 1]:
				i -= 1
			else:
				break
		while i > 0:
			if a[i] >= a[i - 1]:
				i -= 1
			else:
				break
		print(i)


t = int(input())
for _ in range(t):
	n = int(input())
	list1 = list((int(n) for n in input().split()))
	list1.reverse()
	for i in range(1, len(list1)):
		if list1[i - 1] <= list1[i]:
			pass
		else:
			break
	if n == 1:
		print(0)
	else:
		flag = 0
		if i == len(list1) - 1:
			flag = 1
		s = i
		while s < len(list1):
			if list1[s] <= list1[s - 1]:
				s += 1
			else:
				break
		t = len(list1) - s
		if flag == 1:
			print(0)
		else:
			print(t)


def main():
	for _ in range(int(input())):
		n = int(input())
		a = list(map(int, input().split()))
		ans = -1
		i = n - 1
		for i in range(n - 1, -1, -1):
			if i == n - 1:
				ans = n - 1
			elif a[i] >= a[i + 1]:
				ans = i
			else:
				break
		ans = ans - 1
		for i in range(ans, -1, -1):
			if a[i + 1] >= a[i]:
				ans = i
			else:
				break
		print(ans if ans >= 0 else 0)
main()


def is_good(n, array, index):
	if n == 1:
		return True
	l = index
	r = n - 1
	result = 0
	while True:
		if l != r:
			if array[l] <= array[r]:
				if array[l] >= result:
					result = array[l]
					l += 1
				else:
					return False
			elif array[r] >= result:
				result = array[r]
				r -= 1
			else:
				return False
		elif array[l] >= result:
			return True
		else:
			return False

def get_result(n, a):
	if n == 1:
		return 0
	l = -1
	r = n
	while l + 1 < r:
		m = (l + r) // 2
		if not is_good(n, a, m - 1) and is_good(n, a, m):
			return m
		elif not is_good(n, a, m - 1) and (not is_good(n, a, m)):
			l = m
		elif is_good(n, a, m - 1) and is_good(n, a, m):
			r = m
	return m
t = int(input())
for j in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	print(get_result(n, a))


t = int(input())
i = 0
while i < t:
	n = int(input())
	a = list(map(int, input().split()))
	answer = 0
	while True:
		maximum = 0
		index = 0
		j = 0
		while j < len(a):
			if a[j] > maximum:
				maximum = a[j]
				index = j
			j += 1
		a1 = a[0:index]
		a2 = [maximum]
		a3 = a[index + 1:]
		j = 0
		while j < len(a3) - 1:
			if a3[j] < a3[j + 1]:
				a = a[index + 1:]
				answer += index + 1
				break
			j += 1
		if j < len(a3) - 1:
			continue
		elif len(a1) > 1:
			index = len(a1) - 1
			ma1 = a1[index]
			while index > 0:
				if a1[index] < a1[index - 1]:
					answer += index
					break
				index -= 1
			break
		else:
			break
	print(answer)
	i += 1


t = int(input(''))
while t > 0:
	n = int(input(''))
	l = list(map(int, input().split()))
	x = n - 1
	flag = 0
	condition = 'less'
	ans = 0
	while x > 0:
		if flag == 0:
			if l[x] > l[x - 1]:
				flag = 1
			elif l[x] < l[x - 1]:
				flag = 2
		elif flag == 1:
			if l[x] < l[x - 1]:
				ans = x
				break
		elif flag == 2:
			if condition == 'less':
				if l[x] > l[x - 1]:
					condition = 'greater'
			if condition == 'greater':
				if l[x] < l[x - 1]:
					ans = x
					break
		x -= 1
	print(ans)
	t -= 1


def makeItGood(n, array):
	if n == 1:
		return 0
	flag = 0
	f = 0
	for x in range(n - 1):
		if array[n - x - 2] > array[n - x - 1]:
			if flag == 0:
				flag = 1
			elif flag == 2:
				f = 1
				break
		elif array[n - x - 2] < array[n - x - 1]:
			if flag in (0, 1):
				flag = 2
	if f == 0:
		return n - x - 2
	return n - x - 1
t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	print(makeItGood(n, l))


from __future__ import division, print_function
from sys import stdin, stdout
import bisect
import math
import heapq
i_m = 9223372036854775807

def inin():
	return int(input())

def stin():
	return input()

def spin():
	return map(int, stin().split())

def lin():
	return list(map(int, stin().split()))

def matrix(n):
	return [list(map(int, input().split())) for i in range(n)]

def calculate_sum(a, N):
	m = N / a
	sum = m * (m + 1) / 2
	ans = a * sum
	return ans

def series(N):
	return N * (N + 1) // 2

def count2Dmatrix(i, list):
	return sum((c.count(i) for c in list))

def modinv(n, p):
	return pow(n, p - 2, p)

def GCD(x, y):
	x = abs(x)
	y = abs(y)
	if min(x, y) == 0:
		return max(x, y)
	while y:
		(x, y) = (y, x % y)
	return x

def LCM(x, y):
	return x * y // GCD(x, y)

def Divisors(n):
	l = []
	for i in range(1, int(math.sqrt(n) + 1)):
		if n % i == 0:
			if n // i == i:
				l.append(i)
			else:
				l.append(i)
				l.append(n // i)
	return l

def isprime(n):
	for i in range(2, int(math.sqrt(n)) + 1):
		if n % i == 0:
			return False
	return True
prime = []

def SieveOfEratosthenes(n):
	global prime
	prime = [True for i in range(n + 1)]
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p += 1
	f = []
	for p in range(2, n):
		if prime[p]:
			f.append(p)
	return f
q = []

def dfs(n, d, v, c):
	global q
	v[n] = 1
	x = d[n]
	q.append(n)
	j = c
	for i in x:
		if i not in v:
			f = dfs(i, d, v, c + 1)
			j = max(j, f)
	return j
for _ in range(inin()):
	n = inin()
	a = lin()
	j = n - 1
	while a[j] <= a[j - 1] and j > 0:
		j -= 1
	while a[j] >= a[j - 1] and j > 0:
		j -= 1
	print(j)


def good(n, a):
	if n <= 2:
		return 0
	k = n - 1
	while k > 0:
		if a[k] > a[k - 1]:
			break
		k -= 1
	while k > 0:
		if a[k] < a[k - 1]:
			break
		k -= 1
	return k
t = int(input())
for i in range(t):
	n = int(input())
	a = [int(j) for j in input().split()]
	print(good(n, a))


for _ in range(int(input())):
	n = int(input())
	ar = list(map(int, input().split()))
	fl = 0
	flg = 0
	for i in range(n - 2, -1, -1):
		if fl == 0:
			if ar[i] < ar[i + 1]:
				fl = 1
		elif ar[i] <= ar[i + 1]:
			pass
		else:
			flg = 1
			break
	if flg:
		print(i + 1)
	else:
		print(0)


test_cases = int(input())
for _ in range(test_cases):
	input()
	(count, flag) = (0, True)
	arr = list(map(int, input().split()))
	for i in range(len(arr) - 1, -1, -1):
		if flag:
			if arr[i] > arr[i - 1]:
				flag = False
		elif arr[i] < arr[i - 1]:
			count = i
			break
	print(count)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	remo = 0
	i = 0
	while i < n - 1:
		if a[i] > a[i + 1]:
			check = 0
			p = i
			j = i
			while j < n - 1:
				if a[j] < a[p]:
					p = j
				if a[j] < a[j + 1]:
					check = 1
					break
				j += 1
			i = j
			if check == 1:
				remo = p
		else:
			i += 1
	print(remo)


for i in range(int(input())):
	n = int(input())
	arr = list(map(int, input().strip().split())) + [-1]
	n += 1
	arrow = []
	prev = False
	index = 0
	flag = False
	for i in range(n - 1, 0, -1):
		if arr[i - 1] > arr[i]:
			if flag:
				index = i
				break
			else:
				pass
		elif arr[i - 1] == arr[i]:
			if flag:
				pass
		elif not flag:
			flag = True
	print(index)


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	c = 0
	arr = arr[::-1]
	inc = True
	i = 0
	while i < len(arr) - 1:
		if inc:
			if arr[i + 1] >= arr[i]:
				c += 1
			else:
				inc = False
				c += 1
		elif arr[i + 1] <= arr[i]:
			c += 1
		else:
			c += 1
			inc = True
			break
		i += 1
	if n > 1:
		if i == len(arr) - 1:
			if not inc and arr[-1] <= arr[-2] or (inc and arr[-1] >= arr[-2]):
				c += 1
		print(len(arr) - c)
	else:
		print(0)


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	x = a[::-1]
	count = 1
	flag = 0
	if a == sorted(a) or x == sorted(x):
		flag = 1
	if flag == 0:
		for j in range(len(x) - 1):
			if x[j] > x[j + 1]:
				x = x[j + 1:]
				break
		for k in range(len(x) - 1):
			if x[k] >= x[k + 1]:
				count += 1
			else:
				break
		x = x[count:]
	if flag == 1:
		print(0)
	else:
		print(len(x))


t = int(input())
for temp in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	i = 0
	j = n - 1
	var = 0
	ans = 0
	while j > 0 and arr[j] <= arr[j - 1]:
		j -= 1
	while j > 0 and arr[j] >= arr[j - 1]:
		j -= 1
	print(j)


for z in range(int(input())):
	n = int(input())
	a = [int(i) for i in input().split()]
	i = n - 1
	dec = False
	if len(set(a)) == 1:
		print(0)
		continue
	while i > 0 and a[i] == a[i - 1]:
		i -= 1
	if a[i - 1] < a[i]:
		dec = True
	if dec:
		i = n - 1
		while i > 0 and a[i] >= a[i - 1]:
			i -= 1
		print(i)
	else:
		i = n - 1
		while i > 0 and a[i] <= a[i - 1]:
			i -= 1
		if i == 0:
			print(i)
		else:
			i -= 1
			while i > 0 and a[i] >= a[i - 1]:
				i -= 1
			print(i)


testcase = int(input())
for i in range(testcase):
	n = int(input())
	li = list(map(int, input().split()))
	n = n - 1
	while n >= 0 and li[n] <= li[n - 1]:
		n -= 1
	while n >= 0 and li[n] >= li[n - 1]:
		n -= 1
	if n < 0:
		print(0)
	else:
		print(n)


import sys
input = sys.stdin.readline

def inp():
	return int(input())

def inlt():
	return list(map(int, input().split()))

def insr():
	s = input()
	return list(s[:len(s) - 1])

def invr():
	return list(map(int, input().split()))
t = inp()
for i in range(t):
	n = inp()
	a = inlt()
	pos = n - 1
	while pos > 0 and a[pos] <= a[pos - 1]:
		pos -= 1
	while pos > 0 and a[pos] >= a[pos - 1]:
		pos -= 1
	print(pos)


for _ in range(int(input())):
	n = int(input())
	a = [int(x) for x in input().split()]
	(flag, cflag) = (0, False)
	for i in range(n - 1, 0, -1):
		if flag == 1:
			if a[i] < a[i - 1]:
				print(i)
				cflag = True
				break
		elif flag == -1:
			if a[i] > a[i - 1]:
				flag = 1
		elif a[i] > a[i - 1]:
			flag = 1
		elif a[i] < a[i - 1]:
			flag = -1
	if cflag == False:
		print(0)


for _ in range(int(input())):
	input()
	arr = list(map(int, input().split()))
	am = len(arr)
	if am < 3:
		print(0)
		continue
	fl = False
	for i in range(am - 2, 0, -1):
		if arr[i] < arr[i + 1]:
			fl = True
		if fl:
			if arr[i - 1] > arr[i]:
				print(i)
				break
	else:
		print(0)


def solve(a, n):
	if sorted(a) == a:
		return 0
	peak = []
	i = n - 1
	while i >= 1 and a[i] <= a[i - 1]:
		i -= 1
	while i >= 1 and a[i] >= a[i - 1]:
		i -= 1
	return i
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	print(solve(a, n))


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()


import sys
input = sys.stdin.readline
for _ in range(int(input())):
	n = int(input())
	ar = list(map(int, input().split()))
	if n == 1:
		print(0)
	else:
		ans = 1
		for i in range(2, n + 1):
			xx = ar[-i] - ar[-i + 1]
			if xx >= 0:
				ans += 1
			else:
				ans += 1
				break
		for j in range(i + 1, n + 1):
			xx = ar[-j] - ar[-j + 1]
			if xx <= 0:
				ans += 1
			else:
				break
		print(n - ans)


def solve(a, n):
	if n <= 2:
		print('0')
		return
	inc = False
	dec = False
	dec_2 = False
	check_new = False
	i = 0
	ans = 0
	while i != n - 1:
		if a[i + 1] > a[i]:
			curr_diff = 1
		elif a[i + 1] < a[i]:
			curr_diff = -1
		else:
			curr_diff = 0
		if i == 0 or check_new == True or (inc == False and dec == False and (dec_2 == False)):
			check_new = False
			if curr_diff == -1:
				dec = True
			elif curr_diff == 1:
				inc = True
		if dec:
			if curr_diff == 1:
				ans = i
				check_new = True
				inc = False
				dec = False
				dec_2 = False
				i -= 1
		if inc:
			if dec_2 == False:
				if curr_diff == -1:
					dec_2 = True
			elif curr_diff == 1:
				ans = i
				check_new = True
				inc = False
				dec = False
				dec_2 = False
				i -= 1
		i += 1
	if a[ans] == a[ans - 1] and ans != 0:
		temp = a[ans]
		while temp == a[ans]:
			ans -= 1
		print(ans + 1)
	else:
		print(ans)
t = int(input())
for i in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	solve(l, n)


t = int(input())
while t != 0:
	n = int(input())
	list1 = list(map(int, input().split()))
	i = len(list1) - 1
	while i >= 1 and list1[i - 1] >= list1[i]:
		i -= 1
	while i >= 0 and list1[i] >= list1[i - 1]:
		i -= 1
	if i < 0:
		print(0)
	else:
		print(i)
	t -= 1


t = int(input())
for case in range(t):
	n = int(input())
	t = list(map(int, input().split()))
	i = n - 1
	while i > 0 and t[i - 1] >= t[i]:
		i -= 1
	while i > 0 and t[i - 1] <= t[i]:
		i -= 1
	print(i)


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


t = int(input())
while t:
	t -= 1
	n = int(input())
	a = list(map(int, input().split()))
	ans = 0
	tem = 0
	if n == 1:
		print(0)
		continue
	down = False
	ok = True
	for i in range(n - 1, 0, -1):
		if i == 1:
			ok = False
		if down:
			if a[i] < a[i - 1]:
				print(i)
				ok = True
				break
		elif a[i] <= a[i - 1]:
			continue
		else:
			down = True
	if ok:
		continue
	print(0)


from sys import stdin
input = stdin.readline
INF = 10 ** 9 + 7
MAX = 10 ** 7 + 7
MOD = 10 ** 9 + 7
for Ti in range(int(input().strip())):
	n = int(input().strip())
	a = [int(x) for x in input().strip().split()]
	i = len(a) - 1
	while i > 0 and a[i] <= a[i - 1]:
		i -= 1
	ans = 0
	for j in range(i):
		if a[j] > a[j + 1]:
			ans = max(ans, j + 1)
	print(ans)


from sys import stdin
input = stdin.readline
for _ in range(int(input())):
	n = int(input())
	a = [int(i) for i in input().split()]
	i = n - 2
	while i > -1 and a[i] >= a[i + 1]:
		i -= 1
	if i == -1:
		print(0)
	else:
		while a[i] <= a[i + 1] and i > -1:
			i -= 1
		if i == -1:
			print(0)
		else:
			print(i + 1)


from sys import stdin
input = stdin.readline
t = int(input())
for _ in range(t):
	n = int(input())
	x = list(map(int, input().split()))
	i = n - 1
	while i > 0 and x[i] <= x[i - 1]:
		i -= 1
	while i > 0 and x[i] >= x[i - 1]:
		i -= 1
	print(i)


def check_good(array1, m):
	count = 0
	i = m - 1
	sign1 = 0
	while sign1 == 0 and i >= 0:
		sign1 = array1[i] - array1[i - 1]
		i -= 1
	if sign1 == 0:
		return 0
	else:
		sign1 = sign1 / abs(sign1)
		if sign1 > 0:
			while i >= 0 and sign1 >= 0:
				sign1 = array1[i] - array1[i - 1]
				i -= 1
		else:
			while abs(count) <= 2 and i >= 0:
				sign = array1[i] - array1[i - 1]
				if sign == 0:
					i -= 1
					continue
				sign = sign / abs(sign)
				count = sign1 - sign + sign * count
				sign1 = sign
				i -= 1
	return i + 1
for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	print(check_good(arr, n))


t = int(input())
for i in range(t):
	n = int(input())
	arr = [int(i) for i in input().split()]
	a = sorted(arr)
	if n == 1 or a == arr:
		print(0)
	else:
		x = arr[-1]
		j = 0
		for i in range(n - 1, -1, -1):
			if arr[i] >= x:
				x = arr[i]
			else:
				i += 1
				break
		for o in range(i, -1, -1):
			if arr[o] <= x:
				x = arr[o]
			else:
				o += 1
				break
		print(o)


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	diff = []
	change = {}
	c = 0
	if n < 3:
		print('0')
		continue
	for i in range(1, n):
		diff.append(l[i] - l[i - 1])
	temp = n - 2
	while diff[temp] == 0 and temp >= 0:
		temp -= 1
		c += 1
	if diff[temp] < 0:
		i = temp - 1
		c += 1
		while diff[i] <= 0 and i >= 0:
			c += 1
			i -= 1
		while diff[i] >= 0 and i >= 0:
			c += 1
			i -= 1
	elif diff[temp] > 0:
		i = temp - 1
		c += 1
		while diff[i] >= 0 and i >= 0:
			c += 1
			i -= 1
	print(n - c - 1)


import sys
import os
from collections import Counter, deque, defaultdict
import itertools
import math
import io

def ii():
	return int(input())

def li():
	return list(map(int, input().split()))

def mi():
	return map(int, input().split())

def gcd(x, y):
	while y:
		(x, y) = (y, x % y)
	return x

def check_sorted(a):
	for i in range(len(a) - 1):
		if a[i] > a[i + 1]:
			return False
	return True

def divs(n, start=1):
	divisors = []
	for i in range(start, int(math.sqrt(n) + 1)):
		if n % i == 0:
			if n / i == i:
				divisors.append(i)
			else:
				divisors.extend([i, n // i])
	return divisors

def main():
	for i in range(ii()):
		n = ii()
		a = li()
		if check_sorted(a) or check_sorted(a[::-1]):
			print(0)
			continue
		k = n
		for i in range(n - 1, -1, -1):
			if a[i] <= a[i - 1]:
				k = i
			else:
				k = i
				break
		for i in range(k, -1, -1):
			if a[i] >= a[i - 1]:
				k = i
			else:
				k = i
				break
		print(k)
main()


def good(A):
	n = len(A)
	if n == 1 or n == 2:
		return 0
	end = -1
	prev = A[n - 1]
	trend = True
	for i in range(n - 2, -1, -1):
		if trend:
			if A[i] >= prev:
				prev = A[i]
			else:
				trend = False
				prev = A[i]
		elif A[i] <= prev:
			prev = A[i]
		else:
			end = i
			break
	if end == -1:
		return 0
	else:
		return end + 1
t = int(input())
for i in range(t):
	n = int(input())
	A = [int(i) for i in input().split()][:n]
	print(good(A))


def check(x):
	ll = x
	rr = n - 1
	c = list()
	while ll < rr:
		if a[ll] <= a[rr]:
			c.append(a[ll])
			ll += 1
		else:
			c.append(a[rr])
			rr -= 1
	return c == sorted(c)
for _ in range(int(input())):
	n = int(input())
	a = [int(o) for o in input().split()]
	l = 0
	r = n
	while l < r:
		mid = (l + r) // 2
		if check(mid):
			r = mid
		else:
			l = mid + 1
	print(l)


from sys import stdout
from sys import stdin

def get():
	return stdin.readline().strip()

def getf():
	return [int(i) for i in get().split()]

def put(a, end='\n'):
	stdout.write(str(a) + end)

def putf(a, sep=' ', end='\n'):
	stdout.write(sep.join(map(str, a)) + end)

def solve(n, a):
	if n < 3:
		return 0
	y = 0
	for i in range(n - 1, 0, -1):
		if a[i] > a[i - 1]:
			y = i
			break
	x = 0
	for i in range(y - 1, 0, -1):
		if a[i] < a[i - 1]:
			x = i
			break
	return x

def main():
	t = int(get())
	for i in range(t):
		n = int(get())
		s = getf()
		put(solve(n, s))
main()


for i in range(int(input())):
	N = int(input())
	array = [int(x) for x in input().split()]
	slope = []
	i = N - 1
	j = N - 1
	if all([x == array[0] for x in array]):
		print(0)
		continue
	elif sorted(array) == array or array == sorted(array, reverse=True):
		print(0)
		continue
	if array[i] >= array[i - 1]:
		while array[i] >= array[i - 1]:
			i -= 1
	if array[j] <= array[j - 1]:
		while array[j] <= array[j - 1]:
			j -= 1
		if array[j] >= array[j - 1]:
			while array[j] >= array[j - 1]:
				j -= 1
			if j < 0:
				print(0)
				continue
			else:
				print(min(j, i))
				continue
	print(min(i, j))


a = int(input())
for i in range(a):
	s = int(input())
	z = list(map(int, input().split()))
	flag = 0
	count = 1
	ans = []
	for i in range(1, len(z)):
		if z[i] == z[i - 1]:
			count += 1
		else:
			ans.append([z[i - 1], count])
			count = 1
	ans.append([z[-1], count])
	if len(ans) <= 2:
		print(0)
		continue
	for i in range(len(ans) - 2, 0, -1):
		if ans[i][0] < ans[i + 1][0] and ans[i][0] < ans[i - 1][0]:
			flag = 1
			break
	if flag == 0:
		print(0)
		continue
	else:
		total = 0
		for j in range(i):
			total += ans[j][1]
		print(total)


from sys import stdin
input = stdin.readline
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().rstrip().split(' ')))
	if n == 1 or n == 2:
		print(0)
	else:
		inc = True
		count = 1
		for i in range(n - 1, 0, -1):
			if inc:
				count += 1
				if a[i - 1] < a[i]:
					inc = False
			elif a[i - 1] > a[i]:
				break
			else:
				count += 1
		print(n - count)


t = int(input())
n_list = [0 for i in range(t)]
array_list = [[] for i in range(t)]
for i in range(t):
	n_list[i] = int(input())
	temp = input()
	array_list[i] = temp.split()
	for j in range(len(array_list[i])):
		array_list[i][j] = int(array_list[i][j])
out_list = [0 for i in range(t)]
for i in range(t):
	pos = n_list[i] - 1
	while pos > 0:
		if array_list[i][pos] <= array_list[i][pos - 1]:
			pos -= 1
		else:
			break
	while pos > 0:
		if array_list[i][pos] >= array_list[i][pos - 1]:
			pos -= 1
		else:
			break
	out_list[i] = pos
for i in range(t):
	print(out_list[i])


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	k = -1
	flag = 0
	m = 0
	for i in range(n - 1):
		if a[i + 1] < a[i]:
			k = i
		if a[i + 1] > a[i] and k != -1:
			flag = 1
			m = max(m, k)
	if flag:
		print(m + 1)
	else:
		print(0)


import sys
import math
import heapq
import bisect
from collections import Counter
from collections import defaultdict
from io import BytesIO, IOBase
import string

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		import os
		self.os = os
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None
		self.BUFSIZE = 8192

	def read(self):
		while True:
			b = self.os.read(self._fd, max(self.os.fstat(self._fd).st_size, self.BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = self.os.read(self._fd, max(self.os.fstat(self._fd).st_size, self.BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			self.os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def get_int():
	return int(input())

def get_ints():
	return list(map(int, input().split(' ')))

def get_int_grid(n):
	return [get_ints() for _ in range(n)]

def get_str():
	return input().strip()

def get_strs():
	return get_str().split(' ')

def flat_list(arr):
	return [item for subarr in arr for item in subarr]

def yes_no(b):
	if b:
		return 'YES'
	else:
		return 'NO'

def binary_search(good, left, right, delta=1, right_true=False):
	limits = [left, right]
	while limits[1] - limits[0] > delta:
		if delta == 1:
			mid = sum(limits) // 2
		else:
			mid = sum(limits) / 2
		if good(mid):
			limits[int(right_true)] = mid
		else:
			limits[int(~right_true)] = mid
	if good(limits[int(right_true)]):
		return limits[int(right_true)]
	else:
		return False

def prefix_sums(a):
	p = [0]
	for x in a:
		p.append(p[-1] + x)
	return p

def solve_a():
	X = get_ints()
	if X.count(max(X)) == 1:
		print('NO')
	else:
		print('YES')
		print(min(X), min(X), max(X))

def solve_b():
	n = get_int()
	p = []
	m = get_strs()
	i = 0
	while len(p) < n:
		if m[i] not in p:
			p.append(m[i])
		i += 1
	return ' '.join(p)

def solve_c():
	n = get_int()
	a = get_ints()
	cnt = 0
	inc = True
	curr = -float('inf')
	while a:
		(curr, prev) = (a.pop(), curr)
		if inc and curr < prev:
			inc = False
		elif not inc and curr > prev:
			break
		cnt += 1
	return n - cnt
t = get_int()
for _ in range(t):
	print(solve_c())


for t in range(int(input())):
	n = int(input())
	aa = [int(s) for s in input().split(' ')]
	i = n - 2
	while i >= 0 and aa[i] >= aa[i + 1]:
		i -= 1
	while i >= 0 and aa[i] <= aa[i + 1]:
		i -= 1
	print(i + 1)


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().strip().split()))
	f = g = 0
	idx = -1
	if a[n - 1] > a[n - 2]:
		for i in range(n - 1, 0, -1):
			if a[i] < a[i - 1]:
				g = 1
				idx = i
				break
		if g == 0:
			print(0)
		else:
			print(idx)
	else:
		for i in range(n - 1, 0, -1):
			if f == 0:
				if a[i] <= a[i - 1]:
					pass
				else:
					f = 1
			elif a[i] >= a[i - 1]:
				pass
			else:
				f = 2
				idx = i
				break
		if f == 0 or f == 1:
			print(0)
		else:
			print(idx)


import sys
input = sys.stdin.readline
(inp, ip) = (lambda : int(input()), lambda : [int(w) for w in input().split()])
for _ in range(inp()):
	n = inp()
	x = ip()
	flag = 1
	for i in range(1, n):
		if x[i] < x[i - 1]:
			flag = 0
			break
	if flag:
		print(0)
		continue
	flip = 1
	flag = 0
	for i in range(n - 2, -1, -1):
		if flip:
			if x[i] < x[i + 1]:
				flip = 0
		elif x[i] > x[i + 1]:
			flag = 1
			break
	if flag:
		print(i + 1)
	else:
		print(0)


def fast(arr):
	increasing = True
	for i in range(len(arr) - 1, 0, -1):
		if arr[i - 1] > arr[i] and (not increasing):
			return i
		if arr[i - 1] < arr[i]:
			increasing = False
	return 0

def main():
	t = int(input())
	for _ in range(t):
		n = int(input())
		arr = [int(x) for x in input().split()]
		result = fast(arr)
		print(result)
main()


for t in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	ans = n - 1
	while ans > 0 and arr[ans - 1] >= arr[ans]:
		ans -= 1
	while ans > 0 and arr[ans - 1] <= arr[ans]:
		ans -= 1
	print(ans)


for _ in range(0, int(input())):
	n = int(input())
	l = list(map(int, input().strip().split()))
	l.reverse()
	flag5 = 0
	counter = 0
	while counter < n - 1:
		if flag5 == 0 and l[counter] <= l[counter + 1]:
			counter += 1
		elif flag5 == 1 and l[counter] >= l[counter + 1]:
			counter += 1
		elif flag5 == 0:
			flag5 = 1
			counter += 1
		else:
			break
	print(n - (counter + 1))


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)


import math
from sys import stdin
input = stdin.buffer.readline
t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	j1 = n - 1
	while j1 > 0 and a[j1] <= a[j1 - 1]:
		j1 = j1 - 1
	while j1 > 0 and a[j1] >= a[j1 - 1]:
		j1 = j1 - 1
	print(j1)


import os
import sys
from math import *
from collections import *
from fractions import *
from bisect import *
from heapq import *
from io import BytesIO, IOBase

def vsInput():
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def value():
	return tuple(map(int, input().split()))

def array():
	return [int(i) for i in input().split()]

def Int():
	return int(input())

def Str():
	return input()

def arrayS():
	return [i for i in input().split()]
for _ in range(Int()):
	n = Int()
	a = array()[::-1]
	i = 1
	while i < n and a[i] >= a[i - 1]:
		i += 1
	while i < n and a[i] <= a[i - 1]:
		i += 1
	print(n - i)


import math
import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	(inc, dec, ans, prev) = (0, 0, 0, -1)
	for i in range(n - 1):
		if l[i + 1] > l[i]:
			if prev == 0:
				ans = ind
			prev = 1
		if l[i + 1] < l[i]:
			ind = i + 1
			prev = 0
	print(ans)


t = int(input())
for test in range(t):
	n = int(input())
	arr = [int(x) for x in input().split()]
	inc = True
	dec = False
	done = False
	for i in range(n - 1, 0, -1):
		if inc and arr[i - 1] < arr[i]:
			inc = False
			dec = True
		if dec and arr[i - 1] > arr[i]:
			print(i)
			done = True
			break
	if done:
		continue
	else:
		print(0)


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	temp = l.copy()
	temp.reverse()
	m = 0
	length = 0
	for i in temp:
		if i >= m:
			length += 1
			m = max(m, i)
		else:
			break
	result = length
	length = 0
	m = 0
	for i in l[0:len(l) - result]:
		if i >= m:
			length += 1
			m = max(m, i)
		else:
			m = i
			length = 1
	print(n - (result + length))


for i in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	if n == 1:
		print(0)
		continue
	for i in range(n - 1, 0, -1):
		if arr[i - 1] >= arr[i]:
			pass
		else:
			break
	else:
		print(0)
		continue
	for j in range(i - 1, 0, -1):
		if arr[j - 1] <= arr[j]:
			pass
		else:
			break
	else:
		print(0)
		continue
	print(j)


def answer(a, n):
	s = 0
	e = 0
	pind = n - 1
	ind = n - 2
	while ind >= 0 and a[ind] >= a[pind]:
		e += 1
		ind -= 1
		pind -= 1
	rem = 0
	flag = 0
	for i in range(0, n - e - 1):
		if a[i] > a[i + 1]:
			rem = i
			flag = 1
	if flag == 0:
		print('0')
	else:
		print(rem + 1)
	return
t = int(input())
for i in range(t):
	n = int(input())
	a = [int(x) for x in input().split()]
	answer(a, n)


t = int(input())
for _ in range(t):
	n = int(input())
	arr = [int(x) for x in input().split()]
	if n <= 2:
		print(0)
		continue
	greatLeft = [-1 for i in range(n)]
	greatRight = [-1 for i in range(n)]
	stack = []
	for i in range(n):
		length = len(stack)
		while length > 0 and arr[stack[length - 1]] < arr[i]:
			greatRight[stack.pop()] = i
			length -= 1
		stack.append(i)
	stack = []
	for i in range(n - 1, 0 - 1, -1):
		length = len(stack)
		while length > 0 and arr[stack[length - 1]] < arr[i]:
			greatLeft[stack.pop()] = i
			length -= 1
		stack.append(i)
	res = 0
	for i in range(n - 2, 0, -1):
		if greatLeft[i] != -1 and greatRight[i] != -1:
			res = max([res, greatLeft[i] + 1])
	print(res)


import sys
sys.setrecursionlimit(100000)
from collections import defaultdict
from collections import deque
import heapq
I = lambda : list(map(int, input().split(' ')))
for _ in range(int(input())):
	n = int(input())
	lst = I()
	max_ind = None
	for i in range(n - 2, -1, -1):
		if lst[i] >= lst[i + 1]:
			continue
		else:
			max_ind = i + 1
			break
	if max_ind == None:
		print(0)
	else:
		min_ind = 0
		for i in range(max_ind - 1, -1, -1):
			if lst[i] <= lst[i + 1]:
				continue
			else:
				min_ind = i + 1
				break
		print(min_ind)


import math
import bisect
import copy
import time
from collections import Counter
for _ in range(int(input())):
	n = int(input())
	l = [int(x) for x in input().split()]
	up = 0
	down = 0
	c = n - 2
	for i in range(n - 2, -1, -1):
		if l[i + 1] <= l[i]:
			up += 1
		else:
			c = i
			break
	if up > 0 and c == n - 2:
		c = -1
	elif up == 0:
		c == n - 2
	for i in range(c, -1, -1):
		if l[i + 1] >= l[i]:
			down += 1
		else:
			break
	x = up + down
	print(n - x - 1)


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	turn = 0
	cnt = 0
	j = -1
	for i in range(n - 1, 0, -1):
		if arr[i] != arr[i - 1]:
			j = i
			break
	if j == -1:
		print(0)
	elif arr[j] < arr[j - 1]:
		for i in range(j, 0, -1):
			if arr[i] == arr[i - 1]:
				continue
			elif arr[i] > arr[i - 1]:
				turn = 1
			elif turn == 1:
				cnt = i
				break
		print(cnt)
	else:
		for i in range(n - 1, 0, -1):
			if arr[i] < arr[i - 1]:
				cnt = i
				break
		print(cnt)


import math, string, itertools, fractions, heapq, collections, re, array, bisect, sys, copy, functools
sys.setrecursionlimit(10 ** 7)
inf = 10 ** 20
eps = 1.0 / 10 ** 10
mod = 10 ** 9 + 7
mod2 = 998244353
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1)]

def LI():
	return list(map(int, sys.stdin.readline().split()))

def LLI():
	return [list(map(int, l.split())) for l in sys.stdin.readlines()]

def LI_():
	return [int(x) - 1 for x in sys.stdin.readline().split()]

def LF():
	return [float(x) for x in sys.stdin.readline().split()]

def LS():
	return sys.stdin.readline().split()

def I():
	return int(sys.stdin.readline())

def F():
	return float(sys.stdin.readline())

def S():
	return input()

def pf(s):
	return print(s, flush=True)

def pe(s):
	return print(str(s), file=sys.stderr)

def JA(a, sep):
	return sep.join(map(str, a))

def JAA(a, s, t):
	return s.join((t.join(map(str, b)) for b in a))

def IF(c, t, f):
	return t if c else f

def main():
	t = I()
	rr = []
	for _ in range(t):
		n = I()
		a = LI()
		t = 0
		m = -1
		r = 0
		for i in range(n - 1, -1, -1):
			c = a[i]
			if t == 0:
				if m < c:
					t = 1
			elif t == 1:
				if m > c:
					t = 2
			elif m < c:
				r = i + 1
				break
			m = c
		rr.append(r)
	return JA(rr, '\n')
print(main())


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	l = l[::-1]
	flag = False
	ans = -1
	for i in range(n):
		if flag == False and i != n - 1 and (l[i] > l[i + 1]):
			flag = True
			continue
		if flag and i != n - 1 and (l[i] < l[i + 1]):
			ans = i
			break
	if ans == -1:
		print(0)
	else:
		print(n - 1 - ans)


rd = lambda : map(int, input().split())
(t,) = rd()
for _ in range(t):
	(n,) = rd()
	a = list(rd())
	right = [0] * len(a)
	right[-1] = a[-1]
	for i in range(len(a) - 2, -1, -1):
		right[i] = max(right[i + 1], a[i])
	left = a[0]
	remove = 0
	for i in range(len(a)):
		left = max(left, a[i])
		if left > a[i] and right[i] > a[i]:
			remove = i
			left = a[i]
	print(remove)


NINF = -10 ** 9

def valid(arr):
	prev = NINF
	l = 0
	r = len(arr) - 1
	while l <= r:
		if arr[l] <= arr[r]:
			if arr[l] >= prev:
				prev = arr[l]
				l += 1
			else:
				return False
		elif arr[r] >= prev:
			prev = arr[r]
			r -= 1
		else:
			return False
	return True
for t in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	ans = None
	l = 0
	r = n - 1
	while l <= r:
		mid = l + (r - l) // 2
		if valid(arr[mid:]):
			ans = mid - 1
			r = mid - 1
		else:
			l = mid + 1
	print(ans + 1)


for _ in range(int(input())):
	n = int(input())
	a = [int(x) for x in input().split()]
	(ans, tmp) = ([0], max(a))
	for i in range(2, n):
		if a[i - 2] > a[i - 1] and a[i - 1] <= a[i]:
			if i < n - 1:
				rev = sorted(a[i - 1:], reverse=True)
				fow = sorted(a[i - 2:])
				ans.append(i - 1)
				if a[i - 1:] == rev or a[i - 2:] == fow:
					ans = ans[:-1]
					break
			elif a[i - 1] < a[i]:
				ans.append(i - 1)
	print(ans[-1])


t = int(input())
for tt in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	dec = False
	foo = 0
	s = 0
	for i in range(1, n):
		if l[i] > l[i - 1]:
			if dec == True:
				s = i - 1 - foo
				dec = False
			foo = 0
		elif l[i] < l[i - 1]:
			dec = True
			foo = 0
		else:
			foo += 1
	print(s)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	if len(a) <= 2:
		print(0)
		continue
	edm = a[-1]
	cnm = a[-2]
	lci = 0
	lcd = 0
	mud = 1
	for i in range(n - 1, 0, -1):
		if a[i] <= a[i - 1]:
			lci += 1
		else:
			break
	for i in range(n - 1, 0, -1):
		if a[i] >= a[i - 1]:
			lcd += 1
		else:
			break
	u = 0
	for i in range(n - 1, 0, -1):
		if a[i] <= a[i - 1] and u == 0:
			mud += 1
		elif a[i] > a[i - 1] and u == 0:
			u = 1
			mud += 1
		elif a[i] >= a[i - 1] and u == 1:
			mud += 1
		elif a[i] < a[i - 1] and u == 1:
			break
	print(min(n - mud, n - lci, n - lcd))


def good(arr, lf, rg):
	prev = -1
	while lf < rg:
		if arr[lf] < prev or arr[rg] < prev:
			return False
		if arr[lf] < arr[rg]:
			num = arr[lf]
			prev = num
			lf += 1
		else:
			num = arr[rg]
			prev = num
			rg -= 1
	return True

def solve():
	import sys
	input = sys.stdin.readline
	print = sys.stdout.write
	t = int(input())
	while t > 0:
		n = int(input())
		a = list(map(int, input().split()))
		l = 1
		r = n + 1
		while r - l > 1:
			mid = (r + l) // 2
			if good(a, n - mid, n - 1):
				l = mid
			else:
				r = mid
		result = str(n - l)
		print(result)
		print('\n')
		t -= 1
solve()


import sys
input = sys.stdin.buffer.readline
t = int(input())
while t:
	t -= 1
	n = int(input())
	arr = list(map(int, input().split()))
	val = arr[-1]
	g = -1
	count = 1
	sp = -1
	z = -1
	br = -1
	sppp = -1
	for i in range(n - 2, -1, -1):
		if arr[i] == val:
			count += 0
		elif arr[i] < val:
			if sppp == -1:
				sppp = 0
			if br == 1:
				break
			if sp == 1:
				sp = 0
				br = 0
			else:
				sp = 0
		else:
			if sppp == -1:
				sppp = 1
			elif sppp == 0:
				break
			if br == 0:
				break
			if sp == 0:
				sp = 1
				br = 1
			else:
				sp = 1
		val = arr[i]
		count += 1
	print(n - count)


t = int(input())
while t > 0:
	t -= 1
	n = int(input())
	arr = list(map(int, input().strip().split(' ')))
	bre = -1
	mxe = 0
	for i in range(n - 1, -1, -1):
		if arr[i] < mxe:
			bre = i
			break
		mxe = max(mxe, arr[i])
	if bre == -1:
		print(0)
		continue
	mxs = 0
	brs = -1
	for i in range(0, bre + 1):
		if mxs > arr[i]:
			brs = i
			mxs = arr[i]
		else:
			mxs = max(mxs, arr[i])
	if brs == -1:
		print(0)
	else:
		print(brs)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	a0 = a[-1]
	ans = 0
	up = False
	v = 0
	down = False
	s = False
	for i in range(n - 1, -1, -1):
		if a0 < a[i]:
			if not up:
				up = True
				down = False
				v += 1
		if a0 > a[i]:
			if not down:
				down = True
				up = False
				v += 1
		if up and v >= 2:
			ans = i + 1
			break
		if down and v >= 3:
			ans = i + 1
			break
		a0 = a[i]
	print(ans)


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	c = []
	rem_start = 0
	answer = 0
	fi = 0
	li = len(a) - 1
	while fi < li:
		first = a[fi]
		last = a[li]
		if c != [] and c[-1] > min(first, last):
			if answer == rem_start:
				rem_start += 1
			answer = rem_start
			fi = rem_start
			li = len(a) - 1
			c = []
			continue
		if first < last:
			fi += 1
			c.append(first)
			rem_start += 1
		else:
			li -= 1
			c.append(last)
	print(answer)


tc = int(input())
for m in range(tc):
	n = int(input())
	array = list(map(int, input().split()))
	if n < 3:
		print(0)
		continue
	char = False
	index = n - 1
	for i in range(index, 0, -1):
		if array[i - 1] > array[i]:
			char = True
			break
		if array[i - 1] < array[i]:
			char = False
			break
	if char == False:
		for i in range(index, 0, -1):
			if array[i] < array[i - 1]:
				break
		if i == 1 and array[i] >= array[i - 1]:
			print(0)
		else:
			print(i)
	elif char == True:
		turning = False
		for i in range(index, 0, -1):
			if turning == False and array[i - 1] < array[i]:
				turning = True
			elif turning == True and array[i - 1] > array[i]:
				break
		if turning == False and i == 1:
			print(0)
		elif turning == True and array[i] >= array[i - 1]:
			print(0)
		else:
			print(i)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	ans = 0
	inc = True
	for i in range(n - 1, 0, -1):
		if a[i] > a[i - 1] and inc:
			inc = False
		elif a[i] < a[i - 1] and (not inc):
			ans = i
			break
	print(ans)


from sys import stdin as sin
for _ in range(int(sin.readline())):
	n = int(sin.readline())
	l = list(map(int, sin.readline().split(' ')))
	c = 0
	i = n - 1
	for i in range(n - 1, 0, -1):
		if l[i] > l[i - 1]:
			break
		c += 1
	for j in range(i, 0, -1):
		if l[j] < l[j - 1]:
			break
		c += 1
	if c == n:
		print(0)
	else:
		print(n - c - 1)


import sys
from functools import lru_cache, cmp_to_key
from heapq import merge, heapify, heappop, heappush
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
mod = int(pow(10, 9) + 7)
mod2 = 998244353

def data():
	return sys.stdin.readline().strip()

def out(*var, end='\n'):
	sys.stdout.write(' '.join(map(str, var)) + end)

def L():
	return list(sp())

def sl():
	return list(ssp())

def sp():
	return map(int, data().split())

def ssp():
	return 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)]
try:
	sys.stdin = open('input.txt', 'r')
except:
	pass

def check(arr):
	sol = [-1]
	l = 0
	r = len(arr) - 1
	while l <= r:
		if arr[l] <= arr[r]:
			if sol[-1] > arr[l]:
				return False
			sol.append(arr[l])
			l += 1
		else:
			if sol[-1] > arr[r]:
				return False
			sol.append(arr[r])
			r -= 1
	return True
t = L()[0]
for _ in range(t):
	n = L()[0]
	A = L()
	l = 0
	r = n
	ans = n - 1
	while l <= r:
		m = (l + r) // 2
		if check(A[m:]):
			ans = m
			r = m - 1
		else:
			l = m + 1
	print(ans)


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	ans = 0
	r = list(reversed(l))
	k = r[0]
	d = 1
	f = -1
	for i in range(1, n):
		if d:
			if r[i] > k:
				k = r[i]
			elif r[i] < k:
				k = r[i]
				d = 0
		elif r[i] < k:
			k = r[i]
		elif r[i] > k:
			f = i
			break
	if f == -1:
		print(0)
	else:
		print(n - i)


for _ in range(int(input())):
	n = int(input())
	nums = list(map(int, input().split()))
	if n == 1:
		print(0)
		continue
	minus = 0
	flag = 0
	for i in range(-1, -n, -1):
		if flag == 0:
			if nums[i] <= nums[i - 1]:
				minus += 1
			else:
				flag = 1
		if flag == 1:
			if nums[i] >= nums[i - 1]:
				minus += 1
			else:
				minus += 1
				break
	if i == -(n - 1):
		if flag == 0:
			if nums[0] >= nums[i]:
				minus += 1
		elif nums[0] <= nums[i]:
			minus += 1
	print(n - minus)


t = int(input())
for p in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	c = 1
	i = n - 1
	while i > 0:
		if l[i] <= l[i - 1]:
			c += 1
			i -= 1
		else:
			break
	while i > 0:
		if l[i] >= l[i - 1]:
			c += 1
			i -= 1
		else:
			break
	print(n - c)


t = int(input())
for i in range(t):
	n = int(input())
	arr = list(map(int, input().strip().split()))
	flag = 0
	index = 0
	for i in range(n - 1, 0, -1):
		if arr[i] <= arr[i - 1] and flag == 0:
			continue
		if arr[i] >= arr[i - 1]:
			flag = 1
		else:
			index = i
			break
	print(index)


import sys
reader = (s.rstrip() for s in sys.stdin)
input = reader.__next__

def gift():
	for _ in range(t):
		n = int(input())
		arry = list(map(int, input().split()))
		prev = arry[n - 1]
		increase = True
		res = None
		for i in range(n - 1):
			curr = arry[n - 2 - i]
			if curr > prev:
				if increase:
					prev = curr
					continue
				else:
					res = i
					break
			elif curr == prev:
				prev = curr
				continue
			elif increase:
				prev = curr
				increase = False
			else:
				prev = curr
				continue
		if res:
			yield (n - res - 1)
		else:
			yield 0
t = int(input())
ans = gift()
print(*ans, sep='\n')


from sys import stdin
for _ in range(int(stdin.readline())):
	n = int(stdin.readline())
	a = [int(i) for i in stdin.readline().split(' ')]
	a = a[::-1]
	inc = 0
	flag = 0
	decf = 0
	for i in range(0, n - 1):
		temp = a[i] - a[i + 1]
		if temp < 0:
			inc += 1
			if decf > 0:
				print(n - (i + 1))
				flag = 1
				break
		if temp > 0:
			decf = 1
	if flag == 0:
		print(0)


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


import sys
input = sys.stdin.readline
read = lambda : map(int, input().split())

def ceil(x):
	if x != int(x):
		x = int(x) + 1
	return x

def factorial(x, m):
	val = 1
	while x > 0:
		val = val * x % m
		x -= 1
	return val

def fact(x):
	val = 1
	while x > 0:
		val *= x
		x -= 1
	return val

def swaparr(arr, a, b):
	temp = arr[a]
	arr[a] = arr[b]
	arr[b] = temp

def gcd(a, b):
	if b == 0:
		return a
	return gcd(b, a % b)

def lcm(a, b):
	return a * b // math.gcd(a, b)

def nCr(n, k):
	if k > n:
		return 0
	if k > n - k:
		k = n - k
	res = 1
	for i in range(k):
		res = res * (n - i)
		res = res / (i + 1)
	return int(res)

def primefs(n):
	primes = {}
	while n % 2 == 0 and n > 0:
		primes[2] = primes.get(2, 0) + 1
		n = n // 2
	for i in range(3, int(n ** 0.5) + 2, 2):
		while n % i == 0 and n > 0:
			primes[i] = primes.get(i, 0) + 1
			n = n // i
	if n > 2:
		primes[n] = primes.get(n, 0) + 1
	return primes

def power(x, y, p):
	res = 1
	x = x % p
	if x == 0:
		return 0
	while y > 0:
		if y & 1 == 1:
			res = res * x % p
		y = y >> 1
		x = x * x % p
	return res

def swap(a, b):
	temp = a
	a = b
	b = temp
	return (a, b)

def find(x, link):
	p = x
	while p != link[p]:
		p = link[p]
	while x != p:
		nex = link[x]
		link[x] = p
		x = nex
	return p

def union(x, y, link, size):
	x = find(x, link)
	y = find(y, link)
	if size[x] < size[y]:
		(x, y) = swap(x, y)
	if x != y:
		size[x] += size[y]
		link[y] = x

def sieve(n):
	prime = [True for i in range(n + 1)]
	(prime[0], prime[1]) = (False, False)
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p += 1
	return prime

def phi(n):
	result = n
	p = 2
	while p * p <= n:
		if n % p == 0:
			while n % p == 0:
				n = n // p
			result = result * (1.0 - 1.0 / float(p))
		p = p + 1
	if n > 1:
		result = result * (1.0 - 1.0 / float(n))
	return int(result)

def is_prime(n):
	if n == 0:
		return False
	if n == 1:
		return True
	for i in range(2, int(n ** (1 / 2)) + 1):
		if not n % i:
			return False
	return True
MAXN = int(100000.0 + 5)

def spf_sieve():
	spf[1] = 1
	for i in range(2, MAXN):
		spf[i] = i
	for i in range(4, MAXN, 2):
		spf[i] = 2
	for i in range(3, ceil(MAXN ** 0.5), 2):
		if spf[i] == i:
			for j in range(i * i, MAXN, i):
				if spf[j] == j:
					spf[j] = i
spf = [0 for i in range(MAXN)]

def factoriazation(x):
	res = []
	for i in range(2, int(x ** 0.5) + 1):
		while x % i == 0:
			res.append(i)
			x //= i
	if x != 1:
		res.append(x)
	return res

def factors(n):
	res = []
	for i in range(1, int(n ** 0.5) + 1):
		if n % i == 0:
			res.append(i)
			res.append(n // i)
	return list(set(res))

def int_array():
	return list(map(int, input().strip().split()))

def float_array():
	return list(map(float, input().strip().split()))

def str_array():
	return input().strip().split()

def binary_search(low, high, w, h, n):
	while low < high:
		mid = low + (high - low) // 2
		if check(mid, w, h, n):
			low = mid + 1
		else:
			high = mid
	return low

def check(councils, a, k):
	summ = 0
	for x in a:
		summ += min(x, councils)
	return summ // councils >= k
MOD = int(1000000000.0) + 7
CMOD = 998244353
INF = float('inf')
NINF = -float('inf')
from itertools import permutations
import math
import bisect as bis
import random
import sys

def solve():
	n = int(input())
	a = list(read())
	j = n - 1
	while j > 0 and a[j - 1] >= a[j]:
		j -= 1
	while j > 0 and a[j - 1] <= a[j]:
		j -= 1
	print(j)
for _ in range(int(input())):
	solve()


t = int(input())
for j in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	temp = 0
	idx = 0
	for i in range(n - 2, -1, -1):
		if arr[i] < arr[i + 1]:
			idx = i
			break
	while idx > 0:
		if arr[idx - 1] <= arr[idx]:
			idx -= 1
		else:
			break
	print(idx)


import sys

def answer(n, a):
	ok = 1
	istage = True
	for i in range(n - 2, -1, -1):
		if istage and a[i] >= a[i + 1]:
			ok += 1
		elif istage and a[i] < a[i + 1]:
			ok += 1
			istage = False
		elif not istage and a[i] <= a[i + 1]:
			ok += 1
		else:
			break
	return n - ok

def main():
	t = int(input())
	while t:
		n = int(input())
		a = [int(i) for i in sys.stdin.readline().split()]
		print(answer(n, a))
		t -= 1
	return
main()


t = int(input())
for q in range(t):
	n = input()
	a = [int(x) for x in input().split()]
	count = len(a) - 1
	if count == 0:
		print(0)
		continue
	flag = 1
	pos = 0
	temp = a[count]
	count -= 1
	while flag:
		if pos == 0 and a[count] < temp:
			pos = 1
		if pos == 1 and a[count] > temp:
			print(count + 1)
			flag = 0
			break
		if count == 0:
			print(0)
			flag = 0
			break
		temp = a[count]
		count -= 1


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	counter = -1
	while counter != len(a) * -1:
		if a[counter - 1] >= a[counter]:
			counter -= 1
			continue
		if a[counter - 1] < a[counter]:
			while counter != len(a) * -1:
				if a[counter - 1] <= a[counter]:
					counter -= 1
					continue
				else:
					break
		break
	print(len(a) + counter)


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


from sys import stdin, stdout
t = int(stdin.readline().strip())
for _ in range(t):
	n = int(stdin.readline().strip())
	a = list(map(int, stdin.readline().split()))
	ans = 0
	b = False
	for i in range(n - 2, -1, -1):
		if not b:
			if a[i] < a[i + 1]:
				b = True
		elif a[i] > a[i + 1]:
			ans = i + 1
			break
	stdout.write('{}\n'.format(ans))


t = int(input())
for T in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	if n < 3:
		print(0)
		continue
	dir = 'up'
	if l[-2] < l[-1]:
		dir = 'd'
	itr = -1
	for i in range(n - 1, 0, -1):
		if dir == 'd' and l[i] < l[i - 1]:
			itr = i
			break
		elif dir == 'up' and l[i] > l[i - 1]:
			dir = 'd'
	print(max(itr, 0))


def func(l):
	r = n - 1
	flag = True
	last = -1
	done = False
	while flag:
		il = a[l]
		ir = a[r]
		if il <= ir:
			if il >= last:
				last = il
				l += 1
			elif ir >= last:
				last = ir
				r -= 1
			else:
				flag = False
		elif ir < il:
			if ir >= last:
				last = ir
				r -= 1
			elif il >= last:
				last = il
				l += 1
			else:
				flag = False
		else:
			flag = False
		if done:
			break
		if l == r:
			done = True
	return flag

def bin_search(n, a):
	l = -1
	r = n
	while r - l > 1:
		m = (l + r) // 2
		if func(m):
			r = m
		else:
			l = m
	return r
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	if n == 1:
		print(0)
	else:
		print(bin_search(n, a))


t = int(input())
while t > 0:
	n = int(input())
	arr = [int(i) for i in input().split()][::-1]
	ans = 0
	flag = 1
	for i in range(n - 1):
		if arr[i] <= arr[i + 1] and flag == 1:
			ans += 1
			continue
		else:
			flag = 0
			if arr[i] >= arr[i + 1]:
				ans += 1
				continue
			else:
				ans += 1
				flag = 2
				break
	if flag == 0 or flag == 1:
		ans += 1
	print(n - ans)
	t -= 1


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


from collections import defaultdict as dd
from collections import deque
import bisect
import heapq

def ri():
	return int(input())

def rl():
	return list(map(int, input().split()))

def solve():
	n = ri()
	A = rl()
	dec = False
	inc = False
	for i in range(n - 2, -1, -1):
		if A[i] == A[i + 1]:
			continue
		elif A[i] < A[i + 1]:
			dec = True
		elif A[i] > A[i + 1]:
			if dec:
				print(i + 1)
				return
			else:
				inc = True
	print(0)
mode = 'T'
if mode == 'T':
	t = ri()
	for i in range(t):
		solve()
else:
	solve()


import sys, bisect, string, math, time, functools, random, fractions
from heapq import heappush, heappop, heapify
from collections import deque, defaultdict, Counter
from itertools import permutations, combinations, groupby
rep = range
R = range

def Golf():
	(n, *t) = map(int, open(0).read().split())

def I():
	return int(input())

def S_():
	return input()

def IS():
	return input().split()

def LS():
	return [i for i in input().split()]

def MI():
	return map(int, input().split())

def LI():
	return [int(i) for i in input().split()]

def LI_():
	return [int(i) - 1 for i in input().split()]

def NI(n):
	return [int(input()) for i in range(n)]

def NI_(n):
	return [int(input()) - 1 for i in range(n)]

def StoLI():
	return [ord(i) - 97 for i in input()]

def ItoS(n):
	return chr(n + 97)

def LtoS(ls):
	return ''.join([chr(i + 97) for i in ls])

def RA():
	return map(int, open(0).read().split())

def GI(V, E, ls=None, Directed=False, index=1):
	org_inp = []
	g = [[] for i in range(V)]
	FromStdin = True if ls == None else False
	for i in range(E):
		if FromStdin:
			inp = LI()
			org_inp.append(inp)
		else:
			inp = ls[i]
		if len(inp) == 2:
			(a, b) = inp
			c = 1
		else:
			(a, b, c) = inp
		if index == 1:
			a -= 1
			b -= 1
		aa = (a, c)
		bb = (b, c)
		g[a].append(bb)
		if not Directed:
			g[b].append(aa)
	return (g, org_inp)

def GGI(h, w, search=None, replacement_of_found='.', mp_def={'#': 1, '.': 0}, boundary=1):
	mp = [boundary] * (w + 2)
	found = {}
	for i in R(h):
		s = input()
		for char in search:
			if char in s:
				found[char] = (i + 1) * (w + 2) + s.index(char) + 1
				mp_def[char] = mp_def[replacement_of_found]
		mp += [boundary] + [mp_def[j] for j in s] + [boundary]
	mp += [boundary] * (w + 2)
	return (h + 2, w + 2, mp, found)

def TI(n):
	return GI(n, n - 1)

def accum(ls):
	rt = [0]
	for i in ls:
		rt += [rt[-1] + i]
	return rt

def bit_combination(n, base=2):
	rt = []
	for tb in R(base ** n):
		s = [tb // base ** bt % base for bt in R(n)]
		rt += [s]
	return rt

def gcd(x, y):
	if y == 0:
		return x
	if x % y == 0:
		return y
	while x % y != 0:
		(x, y) = (y, x % y)
	return y

def YN(x):
	print(['NO', 'YES'][x])

def Yn(x):
	print(['No', 'Yes'][x])

def show(*inp, end='\n'):
	if show_flg:
		print(*inp, end=end)
mo = 10 ** 9 + 7
inf = float('inf')
FourNb = [(-1, 0), (1, 0), (0, 1), (0, -1)]
EightNb = [(-1, 0), (1, 0), (0, 1), (0, -1), (1, 1), (-1, -1), (1, -1), (-1, 1)]
compas = dict(zip('WENS', FourNb))
cursol = dict(zip('LRUD', FourNb))
l_alp = string.ascii_lowercase
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
input = lambda : sys.stdin.readline().rstrip()

def primeFactor(N):
	(i, n) = (2, N)
	ret = {}
	(d, sq) = (2, 99)
	while i <= sq:
		k = 0
		while n % i == 0:
			(n, k, ret[i]) = (n // i, k + 1, k + 1)
		if k > 0 or i == 97:
			sq = int(n ** (1 / 2) + 0.5)
		if i < 4:
			i = i * 2 - 1
		else:
			(i, d) = (i + d, d ^ 6)
	if n > 1:
		ret[n] = 1
	return ret

def divisor(n):
	div = [1]
	for (i, j) in primeFactor(n).items():
		div = [i ** k * d for d in div for k in range(j + 1)]
	return div

def PrimeNumSet(N):
	M = int(N ** 0.5)
	seachList = [i for i in range(2, N + 1)]
	primes = []
	while seachList:
		if seachList[0] > M:
			break
		primes.append(seachList[0])
		tmp = seachList[0]
		seachList = [i for i in seachList if i % tmp != 0]
	return primes + seachList

def LCM(b, mo=10 ** 9 + 7):
	prs = PrimeNumSet(max(b))
	M = dict(zip(prs, [0] * len(prs)))
	for i in b:
		dc = primeFactor(i)
		for (j, k) in dc.items():
			M[j] = max(M[j], k)
	r = 1
	for (j, k) in M.items():
		if k != 0:
			r *= pow(j, k, mo)
			r %= mo
	return r
show_flg = False
show_flg = True
ans = 0
for _ in range(I()):
	n = I()
	a = LI()
	ans = []
	v = [0] * n
	p = -1
	t = 0
	for i in range(n)[::-1]:
		if a[i] >= p:
			p = a[i]
		else:
			t = i + 1
			p = a[i + 1]
			break
	s = -1
	if t != 0:
		for i in range(t)[::-1]:
			if a[i] <= p:
				p = a[i]
			else:
				s = i
				p = a[i]
				break
	ans = s + 1
	print(ans)


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	arr = arr[::-1]
	ans2 = 0
	if n == 1 or n == 2:
		print(0)
	else:
		for i in range(n - 1):
			if arr[i] <= arr[i + 1]:
				ans2 += 1
			else:
				break
		for j in range(i, n - 1):
			if arr[j] >= arr[j + 1]:
				ans2 += 1
			else:
				break
		print(max(0, n - ans2 - 1))


l = []
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	a = a[::-1]
	flag1 = 0
	flag2 = 0
	for i in range(1, n):
		if a[i] > a[i - 1]:
			flag1 = 1
			break
	for i in range(1, n):
		if a[i] < a[i - 1]:
			flag2 = 1
			break
	if flag1 == 0 or flag2 == 0:
		l.append(0)
	else:
		item = a[0]
		index = 0
		for i in range(n):
			if item <= a[i]:
				item = a[i]
			else:
				index = i
				item = a[i]
				break
		a = a[index:]
		t = index
		index = len(a)
		for i in range(len(a)):
			if item >= a[i]:
				item = a[i]
			else:
				index = i
				break
		l.append(n - t - index)
for i in l:
	print(i)


import sys
for _ in range(int(sys.stdin.readline().rstrip())):
	n = int(sys.stdin.readline().rstrip())
	a = list(map(int, sys.stdin.readline().rstrip().split()))
	if n == 1:
		print(0)
		continue
	x = n - 2
	inc = True
	if a[n - 2] < a[n - 1]:
		inc = False
	while True:
		if x == 0:
			break
		if inc is False and a[x - 1] > a[x]:
			break
		elif inc and a[x - 1] < a[x]:
			inc = False
		x -= 1
	print(x)


for y in range(int(input())):
	n = int(input())
	lst = list(map(int, input().split()))
	count = n
	flag = 0
	pf = 0
	for i in range(n - 2, -1, -1):
		if lst[i] < lst[i + 1]:
			flag = 1
		if flag == 1 and lst[i] > lst[i + 1]:
			pf = 1
			break
	if pf == 0:
		print(0)
	else:
		print(i + 1)


for i in range(int(input())):
	n = int(input())
	numbers = list(map(int, input().split()))
	res = n - 1
	while res > 0 and numbers[res] <= numbers[res - 1]:
		res -= 1
	while res > 0 and numbers[res] >= numbers[res - 1]:
		res -= 1
	print(res)


def func(x, y):
	if x > 0 and y < 0 or (x < 0 and y > 0):
		return True
	return False
t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	prev = 0
	limit = 0
	cnt = 0
	ans = 0
	for j in reversed(range(n - 1)):
		if a[j] == a[j + 1]:
			continue
		if prev == 0:
			prev = a[j] - a[j + 1]
			if a[j] - a[j + 1] > 0:
				limit += 1
			continue
		if func(a[j] - a[j + 1], prev):
			cnt += 1
			if cnt > limit:
				ans = j + 1
				break
		prev = a[j] - a[j + 1]
	print(ans)


for i in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	cut = 0
	fl = True
	for j in range(n - 1):
		if arr[j + 1] < arr[j]:
			mem = j + 1
			fl = False
		if not fl and arr[j + 1] > arr[j]:
			cut = mem
			fl = True
	print(cut)


from collections import defaultdict
t = int(input())
for i in range(t):
	n = int(input())
	j = n
	a = list(map(int, input().split()))
	c = 0
	n -= 1
	while a[n] <= a[n - 1] and n > 0:
		n -= 1
		c += 1
	while a[n] >= a[n - 1] and n > 0:
		n -= 1
		c += 1
	print(j - c - 1)


for _ in range(int(input())):
	n = int(input())
	lis = list(map(int, input().split()))
	if n == 1 or n == 2:
		print(0)
	elif lis[-1] < lis[-2]:
		co = 0
		for i in range(n - 1, 0, -1):
			if lis[i] > lis[i - 1]:
				break
			co += 1
		i1 = n - co - 1
		c1 = 1
		for i in range(i1, 0, -1):
			if lis[i] < lis[i - 1]:
				break
			c1 += 1
		print(n - co - c1)
	else:
		co = 0
		for i in range(n - 1, 0, -1):
			if lis[i] > lis[i - 1]:
				break
			co += 1
		i1 = n - co - 1
		c1 = 1
		for i in range(i1, 0, -1):
			if lis[i] < lis[i - 1]:
				break
			c1 += 1
		print(n - co - c1)