Polycarp analyzes the prices of the new berPhone. At his disposal are the prices for $n$ last days: $a_1, a_2, \dots, a_n$, where $a_i$ is the price of berPhone on the day $i$.

Polycarp considers the price on the day $i$ to be bad if later (that is, a day with a greater number) berPhone was sold at a lower price. For example, if $n=6$ and $a=[3, 9, 4, 6, 7, 5]$, then the number of days with a bad price is $3$ — these are days $2$ ($a_2=9$), $4$ ($a_4=6$) and $5$ ($a_5=7$).

Print the number of days with a bad price.

You have to answer $t$ independent data sets.


-----Input-----

The first line contains an integer $t$ ($1 \le t \le 10000$) — the number of sets of input data in the test. Input data sets must be processed independently, one after another.

Each input data set consists of two lines. The first line contains an integer $n$ ($1 \le n \le 150000$) — the number of days. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^6$), where $a_i$ is the price on the $i$-th day.

It is guaranteed that the sum of $n$ over all data sets in the test does not exceed $150000$.


-----Output-----

Print $t$ integers, the $j$-th of which should be equal to the number of days with a bad price in the $j$-th input data set.


-----Example-----
Input
5
6
3 9 4 6 7 5
1
1000000
2
2 1
10
31 41 59 26 53 58 97 93 23 84
7
3 2 1 2 3 4 5

Output
3
0
1
8
2

Difficulty: EASY
Tags: Data structures

Implementation
Skill types: Data structures

Starter code:

Solutions:
from collections import deque
t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	d = deque(a)
	m = d.pop()
	count = 0
	for j in range(n - 1):
		temp = d.pop()
		if temp > m:
			count += 1
		m = min(m, temp)
	print(count)


def int_multiple():
	return [int(c) for c in input().split()]

def int_single():
	return int(input())

def str_multiple():
	return [c for c in input().split()]

def str_single():
	return input()
t = int_single()
res = []
for i in range(t):
	n = int_single()
	l = int_multiple()
	cnt = 0
	mn = l[-1]
	for j in reversed(list(range(n))):
		if l[j] < mn:
			mn = l[j]
		if l[j] > mn:
			cnt += 1
	res.append(cnt)
for r in res:
	print(r)


import math
import sys
input = sys.stdin.readline

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

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

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

def invr():
	return map(int, input().split())
for _ in range(inp()):
	n = inp()
	l = inlt()
	cnt = 0
	mn = l[-1]
	for i in range(n - 2, -1, -1):
		if l[i] > mn:
			cnt += 1
		else:
			mn = l[i]
	print(cnt)


t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(i) for i in input().split()]
	minPrice = [0 for _ in range(n)]
	minPrice[n - 1] = a[n - 1]
	for i in range(n - 2, -1, -1):
		minPrice[i] = min(a[i], minPrice[i + 1])
	print(sum((1 for i in range(n - 1) if a[i] > minPrice[i + 1])))


from math import *
from collections import *
from itertools import *
get = lambda : int(input())
getl = lambda : [int(x) for x in input().split()]

def main():
	for i in range(get()):
		query()

def query():
	n = get()
	l = getl()
	l.reverse()
	min_cur = 1000000000.0
	ans = 0
	for x in l:
		if x > min_cur:
			ans += 1
		min_cur = min(min_cur, x)
	print(ans)
main()


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


from math import *
from copy import *
from string import *
from random import *
from sys import stdin
from sys import maxsize
from operator import *
from itertools import *
from collections import Counter
T = int(input())
for _ in range(T):
	N = int(stdin.readline())
	l = list(map(int, stdin.readline().rstrip().split()))
	minn = l[-1]
	ans = 0
	for i in range(N - 1, -1, -1):
		if l[i] > minn:
			ans += 1
		minn = min(minn, l[i])
	print(ans)


for _ in range(int(input())):
	n = int(input())
	lst = list(map(int, input().split()))
	cnt = 0
	x = lst[-1]
	for i in lst[:-1][::-1]:
		x = min((x, i))
		if x < i:
			cnt += 1
	print(cnt)


for _ in range(int(input())):
	n = int(input())
	s = list(map(int, input().split()))
	a = float('inf')
	m = [0] * n
	for i in range(n - 1, -1, -1):
		a = min(a, s[i])
		m[i] = a
	s1 = 0
	for i in range(n):
		if s[i] > m[i]:
			s1 += 1
	print(s1)


t = int(input())
for _ in range(t):
	n = int(input())
	ls = list(map(int, input().split()))
	m = ls[-1]
	ans = 0
	for i in ls[-1::-1]:
		if m < i:
			ans += 1
		m = min(m, i)
	print(ans)


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	cnt = 0
	mini = a[-1]
	maxi = a[-1]
	for i in range(len(a) - 2, -1, -1):
		if a[i] < mini:
			mini = a[i]
		elif mini == a[i]:
			cnt += 0
		elif mini < a[i] and a[i] < maxi:
			cnt += 1
		elif maxi == a[i]:
			cnt += 1
		else:
			cnt += 1
			maxi = a[i]
	print(cnt)


t = int(input())
for j in range(t):
	n = int(input())
	a = list(map(int, input().strip().split()))
	op = 0
	x = 10 ** 6 + 1
	for i in range(n - 1, -1, -1):
		if a[i] > x:
			op += 1
		x = min(x, a[i])
	print(op)


tests = int(input())
num = []
arrays = []
for i in range(0, tests):
	no = int(input())
	num.append(no)
	l = input()
	l = l.split(' ')
	for i in range(0, len(l)):
		l[i] = int(l[i])
	arrays.append(l)
for i in arrays:
	bad_days = 0
	mini = i[len(i) - 1]
	mini_prev = 0
	for j in range(len(i) - 1, -1, -1):
		if j == len(i) - 1:
			mini_prev = mini
		else:
			mini = min(i[j], mini_prev)
			if i[j] > mini:
				bad_days += 1
			mini_prev = mini
	print(bad_days)


for i in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	cnt = 0
	mn = a[-1]
	for k in range(2, n + 1):
		if a[-k] > mn:
			cnt += 1
		mn = min(mn, a[-k])
	print(cnt)


for i in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	m = arr[n - 1]
	count = 0
	for i in range(n - 2, -1, -1):
		if arr[i] > m:
			count += 1
		elif arr[i] < m:
			m = arr[i]
	print(count)


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


for _ in [0] * int(input()):
	input()
	m = 10000000.0
	r = 0
	for x in [*map(int, input().split())][::-1]:
		r += x > m
		m = min(m, x)
	print(r)


q = int(input())
result = []
for _ in range(q):
	(_, ns) = (input(), input())
	bad_days = 0
	prev = None
	l_ns = ns.split()
	i_min = int(l_ns[-1])
	for i in reversed(l_ns):
		i_i = int(i)
		if i_i < i_min:
			i_min = i_i
		elif i_i > i_min:
			bad_days += 1
	result.append(str(bad_days))
print('\n'.join(result))


import sys
T = int(input())
for i in range(T):
	_ = input()
	data = [int(x) for x in input().split(' ')]
	badcount = 0
	currMin = data[-1]
	first = True
	for j in reversed(data):
		if first:
			first = False
		elif j > currMin:
			badcount += 1
		elif j < currMin:
			currMin = j
	print(badcount)


for _ in range(int(input())):
	input()
	arr = list(reversed(list(map(int, input().split()))))
	minNum = arr[0]
	am = 0
	for i in range(1, len(arr)):
		if arr[i] <= minNum:
			minNum = arr[i]
		else:
			am += 1
	print(am)


t = int(input())
for _ in range(t):
	n = int(input())
	days = list(map(int, input().split(' ')))
	min_global = days.pop()
	ans = 0
	while days:
		last = days.pop()
		if min_global >= last:
			min_global = last
		else:
			ans += 1
	print(ans)


from sys import stdin
t = int(stdin.readline().strip())

def solve(prices):
	counter = 0
	found_min = prices[-1]
	for i in range(len(prices) - 1, -1, -1):
		m = prices[i]
		if m > found_min:
			counter += 1
		found_min = min(m, found_min)
	return counter
for _ in range(t):
	n = int(stdin.readline().strip())
	prices = [int(i) for i in stdin.readline().strip().split()]
	print(solve(prices))


t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(x) for x in input().split()]
	if len(a) == 1:
		print(0)
	else:
		a.reverse()
		minm = a[0]
		cnt = 0
		for i in range(n):
			if a[i] > minm:
				cnt += 1
			else:
				minm = a[i]
		print(cnt)


for _ in range(int(input())):
	n = int(input())
	arr = [int(x) for x in input().split()]
	count = 0
	min_ = float('inf')
	for i in range(n - 1, -1, -1):
		if arr[i] > min_:
			count += 1
		min_ = min(min_, arr[i])
	print(count)


for i in range(int(input())):
	length = int(input())
	l = list(map(int, input().split()))
	mini = l[-1]
	counter = 0
	for i in range(length - 2, -1, -1):
		if l[i] > mini:
			counter += 1
		if l[i] < mini:
			mini = l[i]
	print(counter)


import sys
mod = 1000000007

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

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

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

def print_array(a):
	print(' '.join(map(str, a)))

def main():
	for _ in range(int(input())):
		n = int(input())
		a = get_array()
		m = a[n - 1]
		cnt = 0
		for i in range(n - 2, -1, -1):
			if a[i] > m:
				cnt += 1
			if a[i] < m:
				m = a[i]
		print(cnt)
main()


t = int(input())
for i in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	count = [0] * n
	maxi = arr[n - 1]
	for j in range(n):
		if arr[n - j - 1] < maxi:
			maxi = arr[n - j - 1]
		count[n - j - 1] = maxi
	c = 0
	for j in range(n):
		if count[j] < arr[j]:
			c += 1
	print(c)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	mp = min(a)
	s = 0
	for i in range(n - 1, -1, -1):
		if i == n - 1:
			mp = a[i]
		else:
			mp = min(a[i], mp)
		if a[i] > mp:
			s += 1
	print(s)


def bad_days(n, seq):
	min_price = seq[-1]
	bad = 0
	i = -1
	while i >= n * -1:
		if seq[i] > min_price:
			bad += 1
		elif seq[i] < min_price:
			min_price = seq[i]
		i -= 1
	return bad
t = int(input())
for x in range(t):
	n = int(input())
	seq = list(map(int, input().split()))
	print(bad_days(n, seq))


t = int(input())
for i in range(t):
	n = int(input())
	d = list(map(int, input().split()))
	if n == 1:
		print(0)
		continue
	ans = 0
	minprice = float('inf')
	d.reverse()
	for i in d:
		if i > minprice:
			ans += 1
		else:
			minprice = i
	print(ans)


task_count = input()
for i in range(int(task_count)):
	price_count = input()
	prices_string = input()
	prices_string = prices_string + ' '
	price = ''
	prices = []
	for char in prices_string:
		if char == ' ':
			prices.append(int(price))
			price = ''
		else:
			price = price + char
	d = 0
	min_price = 0
	for price in reversed(prices):
		if min_price == 0:
			min_price = price
		elif min_price > price:
			min_price = price
		elif price > min_price:
			d = d + 1
	print(d)


count = int(input())
for i in range(count):
	mini = 10000000
	mai = 0
	chi = 0
	coun = int(input())
	a = [*map(int, input().split())]
	for i in range(coun - 1, -1, -1):
		if a[i] <= mini:
			mini = a[i]
		else:
			chi += 1
	print(chi)


for _ in range(int(input())):
	x = int(input())
	li = list(map(int, input().split()))
	min1 = 10 ** 7
	cnt = 0
	for i in li[::-1]:
		if i > min1:
			cnt += 1
		min1 = min(min1, i)
	print(cnt)


def init():
	t = int(input())
	for i in range(t):
		n = int(input())
		nums = list(map(lambda x: int(x), input().split(' ')))
		nums.reverse()
		bad_prices = 0
		min = 10000000
		for num in nums:
			if num > min:
				bad_prices += 1
			if min > num:
				min = num
		print(bad_prices)
	return
init()


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


t = int(input())
for i in range(t):
	n = int(input())
	s = map(int, input().split())
	s = list(s)
	c = 0
	p = s[-1]
	for a in s[:-1][::-1]:
		if p < a:
			c = c + 1
		else:
			p = a
	print(c)


t = int(input())
while t > 0:
	n = int(input())
	a = list(map(int, input().split(' ')))
	minSoFar = a[n - 1]
	i = n - 2
	ans = 0
	while i >= 0:
		if a[i] > minSoFar:
			ans += 1
		else:
			minSoFar = a[i]
		i -= 1
	print(ans)
	t -= 1


for _ in range(int(input())):
	n = int(input())
	a = [*map(int, input().split())]
	s = [-1] * n
	t = []
	for (i, j) in enumerate(a):
		while len(t) and j < a[t[-1]]:
			s[t.pop()] = j
		t.append(i)
	print(len([*filter(lambda x: x != -1, s)]))


t = int(input())
for i in range(t):
	zas = []
	n = int(input())
	a = list(input().split())
	for i in a:
		num = int(i)
		zas.append(num)
	c = 0
	mini = zas[-1]
	for j in range(len(zas) - 1, -1, -1):
		if mini >= zas[j]:
			mini = zas[j]
		else:
			c += 1
	print(c)


for _ in range(int(input())):
	n = int(input())
	price = list(map(int, input().split()))
	arr = [0] * n
	arr[-1] = price[-1]
	for i in range(n - 2, -1, -1):
		arr[i] = min(price[i], arr[i + 1])
	res = 0
	res = sum([1 for (i, val) in enumerate(price) if val > arr[i]])
	print(res)


for _ in range(int(input())):
	n = int(input())
	l1 = [int(__) for __ in input().split()]
	l1.reverse()
	shit = l1[0]
	ans = 0
	for i in l1[1:]:
		if i <= shit:
			shit = i
		else:
			ans += 1
	print(ans)


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


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	days = 0
	Min = a[-1]
	for i in range(n - 2, -1, -1):
		if a[i] > Min:
			days += 1
		Min = min(a[i], Min)
	print(days)


q = int(input())
for _ in range(q):
	_ = input()
	mas = list(map(int, input().split()))
	mini = mas[-1]
	al = 0
	for i in range(len(mas)):
		i = len(mas) - 1 - i
		if mini < mas[i]:
			al += 1
		else:
			mini = mas[i]
	print(al)


from sys import stdin
input = stdin.readline
for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	l = l[::-1]
	sm = l[0]
	c = 0
	for i in l:
		if i > sm:
			c += 1
		sm = min(sm, i)
	print(c)


t = int(input())
for i in range(0, t):
	n = int(input())
	prices = [int(x) for x in input().split()]
	minimum = prices[n - 1]
	badDays = 0
	for j in range(n - 2, -1, -1):
		if prices[j] <= minimum:
			minimum = prices[j]
		else:
			badDays += 1
	print(badDays)


size1 = int(input())
for x in range(0, size1):
	ar = []
	size2 = int(input())
	for t in input().split(' '):
		ar.append(int(t))
	min = ar[size2 - 1]
	count = 0
	for num in reversed(ar):
		if num > min:
			count = count + 1
		if min > num:
			min = num
	print(count)


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 ceil, floor
from copy import *
from collections import deque, defaultdict
from collections import Counter as counter
from itertools import combinations
from itertools import permutations as permutate
from bisect import bisect_left as bl
from operator import *
from bisect import bisect_right as br
from bisect import bisect
BUFSIZE = 8192
from sys import stderr

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 types import GeneratorType

def iterative(f, stack=[]):

	def wrapped_func(*args, **kwargs):
		if stack:
			return f(*args, **kwargs)
		to = f(*args, **kwargs)
		while True:
			if type(to) is GeneratorType:
				stack.append(to)
				to = next(to)
				continue
			stack.pop()
			if not stack:
				break
			to = stack[-1].send(to)
		return to
	return wrapped_func
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 fsep():
	return map(float, inp().split())

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

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

def pow(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
from functools import reduce

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

def gcd(a, b):
	if a == b:
		return a
	while b > 0:
		(a, b) = (b, a % b)
	return a

def prefix_sum(ar):
	return list(accumulate(ar))

def suffix_sum(ar):
	return list(accumulate(ar[::-1]))[::-1]

def N():
	return int(inp())
from collections import defaultdict

def numberOfSetBits(i):
	i = i - (i >> 1 & 1431655765)
	i = (i & 858993459) + (i >> 2 & 858993459)
	return ((i + (i >> 4) & 252645135) * 16843009 & 4294967295) >> 24

def solve():
	n = N()
	ar = lis()
	c = 0
	mi = 9999999999999999999999999
	for i in range(n - 1, -1, -1):
		if mi < ar[i]:
			c += 1
		mi = min(ar[i], mi)
	print(c)
testcase(int(inp()))


k = int(input())
while k != 0:
	n = int(input())
	ans = 0
	mini = 10000000000
	a = [int(x) for x in input().split()]
	for i in range(n - 1, -1, -1):
		if a[i] <= mini:
			mini = a[i]
		else:
			ans += 1
	print(ans)
	k -= 1


t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(x) for x in input().split()]
	m = 1000000
	r = 0
	for x in reversed(a):
		if x > m:
			r += 1
		m = min(m, x)
	print(r)


n = int(input())
b = []
for i in range(n):
	a = []
	ans = 0
	n1 = int(input())
	a = list(map(int, input().split()))
	s = a[-1]
	for x in reversed(a):
		if x > s:
			ans = ans + 1
		else:
			s = x
	print(ans)


import unittest

def solve(n, a):
	ans = 0
	min = 100000000
	i = n - 1
	while i >= 0:
		if a[i] > min:
			ans += 1
		elif a[i] < min:
			min = a[i]
		i -= 1
	return ans

class TestCase(unittest.TestCase):

	def test1(self):
		n = 10
		x = [31, 41, 59, 26, 53, 58, 97, 93, 23, 84]
		self.assertEqual(solve(n, x), 8)
t = int(input())
for _ in range(0, t):
	n = int(input())
	a = [int(i) for i in input().split()]
	print(solve(n, a))


n = int(input())
out = [0] * n
for i in range(n):
	len = int(input())
	a = input().split(' ')
	a = list(map(int, a))
	count = 0
	minEl = a[-1]
	for j in range(2, len + 1):
		if a[-j] <= minEl:
			minEl = a[-j]
		else:
			count += 1
	out[i] = count
for el in out:
	print(el)


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	s_min = [10 ** 9] * n
	s_min[-1] = a[-1]
	for i in range(n - 2, -1, -1):
		s_min[i] = min(s_min[i + 1], a[i])
	cnt = 0
	for i in range(n - 1):
		cnt += s_min[i + 1] < a[i]
	print(cnt)


test_cases = int(input())
for _ in range(test_cases):
	n = int(input())
	prices = list(map(int, input().split()))[:n]
	minimum_price = prices[n - 1]
	bad_day = 0
	for i in range(n - 2, -1, -1):
		if prices[i] > minimum_price:
			bad_day += 1
		minimum_price = min(prices[i], minimum_price)
	print(bad_day)


n = int(input())
l = []
for i in range(n):
	c = 0
	g = int(input())
	h = list(map(int, input().split()))
	n = h[g - 1]
	for j in range(g - 2, -1, -1):
		if h[j] > n:
			c += 1
		else:
			n = h[j]
	l.append(c)
for k in l:
	print(k)


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	base = arr[-1]
	s = 0
	for i in range(len(arr) - 2, -1, -1):
		if arr[i] > base:
			s += 1
		else:
			base = arr[i]
	print(s)


T = int(input())
for i in range(0, T):
	n = int(input())
	inp = input()
	inp_array = inp.split(' ')
	inp_array_int = []
	for i in inp_array:
		inp_array_int.append(int(i))
	set_re = {inp_array_int[-1]}
	inp_array_int.reverse()
	count = 0
	minimum = inp_array_int[0]
	for i in inp_array_int[1:]:
		if i > minimum:
			count += 1
		else:
			minimum = i
	print(count)


(z, zz) = (input, lambda : list(map(int, z().split())))
zzz = lambda : [int(i) for i in stdin.readline().split()]
(szz, graph, mod, szzz) = (lambda : sorted(zz()), {}, 10 ** 9 + 7, lambda : sorted(zzz()))
from string import *
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
from bisect import bisect as bs
from bisect import bisect_left as bsl
from itertools import accumulate as ac

def lcd(xnum1, xnum2):
	return xnum1 * xnum2 // gcd(xnum1, xnum2)

def prime(x):
	p = ceil(x ** 0.5) + 1
	for i in range(2, p):
		if x % i == 0 and x != 2 or x == 0:
			return 0
	return 1

def dfs(u, visit, graph):
	visit[u] = True
	for i in graph[u]:
		if not visit[i]:
			dfs(i, visit, graph)
num = int(z())
for _ in range(num):
	n = int(z())
	l = zzz()
	ans = 0
	x = l[-1]
	for i in range(2, n + 1):
		if l[-i] > x:
			ans += 1
		x = min(x, l[-i])
	print(ans)


tc = int(input())
ans = []
for _ in range(tc):
	number_of_days = int(input())
	temp = [int(i) for i in input().split()]
	ans_temp = 0
	temp_min = temp[-1]
	if len(temp) == 1:
		ans_temp = 0
	else:
		for j in range(len(temp) - 2, -1, -1):
			if temp[j] > temp_min:
				ans_temp = 1 + ans_temp
			temp_min = min(temp_min, temp[j])
	ans.append(ans_temp)
for i in ans:
	print(i)


for _ in range(int(input())):
	n = int(input())
	s = list(map(int, input().split()))[::-1]
	mn = s[0]
	ans = 0
	for j in range(n):
		if s[j] > mn:
			ans += 1
		else:
			mn = s[j]
	print(ans)


ll = lambda : list(map(int, input().split()))
testcases = 1
[testcases] = ll()
for _ in range(testcases):
	[n] = ll()
	a = ll()
	dp = [0] * n
	ans = 0
	mtn = a[n - 1]
	for i in range(n - 2, -1, -1):
		if a[i] > mtn:
			ans += 1
		mtn = min(mtn, a[i])
	print(ans)


import sys
import math
input = sys.stdin.readline
t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	rev = [0] * n
	mini = l[-1]
	for i in range(n - 1, -1, -1):
		if mini > l[i]:
			mini = l[i]
		rev[i] = mini
	ans = 0
	for i in range(n):
		if i + 1 <= n - 1:
			if l[i] > rev[i + 1]:
				ans += 1
	print(ans)


def getResult(a, n):
	badDays = 0
	stack = []
	stack.append(a[0])
	for i in range(1, n):
		while len(stack) > 0 and a[i] < stack[-1]:
			stack.pop()
			badDays += 1
		stack.append(a[i])
	return badDays
t = int(input())
for i in range(t):
	n = int(input())
	l = [int(x) for x in input().split()]
	print(getResult(l, n))


t = int(input())
for _ in range(t):
	input()
	arr = [int(x) for x in input().split()]
	stack = []
	ans = 0
	for item in arr:
		if len(stack) == 0:
			stack.append(item)
		elif stack[-1] <= item:
			stack.append(item)
		else:
			while True:
				if len(stack) == 0:
					stack.append(item)
					break
				if stack[-1] > item:
					ans += 1
					stack.pop()
				else:
					stack.append(item)
					break
	print(ans)


n = int(input())
while n:
	m = int(input())
	l = list(map(int, input().split()))
	l1 = [0] * m
	stck = list()
	i = 0
	while i < m:
		if len(stck) == 0 or l[stck[-1]] <= l[i]:
			stck.append(i)
		else:
			while len(stck) > 0 and l[stck[-1]] > l[i]:
				l1[stck[-1]] = i
				stck.pop()
			stck.append(i)
		i += 1
	print(m - len(stck))
	n -= 1


t = int(input())
for i in range(t):
	n = int(input())
	lst = list(map(int, input().split()))
	lst = lst[::-1]
	mini = lst[0]
	cnt = 0
	for price in lst:
		if price > mini:
			cnt += 1
		if price < mini:
			mini = price
	print(cnt)


t = int(input())
while t > 0:
	t -= 1
	n = int(input())
	a = list(map(int, input().split()))
	b = [0] * (n + 1)
	b[n - 1] = a[n - 1]
	for i in range(n - 2, -1, -1):
		b[i] = min(a[i], b[i + 1])
	sum = 0
	for i in range(n):
		if a[i] > b[i]:
			sum += 1
	print(sum)


import math
import sys
from collections import defaultdict
rt = lambda : map(int, input().split())
ri = lambda : int(input())
rl = lambda : list(map(int, input().split()))

def main():
	t = ri()
	for _ in range(t):
		n = ri()
		a = rl()[::-1]
		m = math.inf
		res = 0
		for val in a:
			if val > m:
				res += 1
			m = min(m, val)
		print(res)
main()


import sys
input = sys.stdin.buffer.readline
t = int(input())
for _ in range(t):
	n = int(input())
	A = list(map(int, input().split()))
	l = []
	import heapq
	heapq.heapify(l)
	ans = 0
	for i in range(n):
		a = A[i]
		while l:
			x = heapq.heappop(l) * -1
			if A[i] < x:
				ans += 1
			else:
				heapq.heappush(l, x * -1)
				break
		heapq.heappush(l, a * -1)
	print(ans)


for i in range(int(input())):
	n = input()
	days = [int(i) for i in input().split()]
	good_days = [days[0]]
	for day in days[1:]:
		while good_days and good_days[-1] > day:
			good_days.pop()
		good_days.append(day)
	print(len(days) - len(good_days))


for _ in range(int(input())):
	a = int(input())
	count = 0
	lis = [x for x in map(int, input().split())]
	lis = lis[::-1]
	min = lis[0]
	for j in lis:
		if j > min:
			count += 1
		else:
			min = j
	print(count)


N = int(input())
for i in range(N):
	n = int(input())
	lst = [int(i) for i in input().split()]
	if len(lst) == 1:
		print(0)
		continue
	lst = lst[::-1]
	mn = lst[0]
	count = 0
	for it in lst:
		if it > mn:
			count += 1
		elif it < mn:
			mn = it
	print(count)


for _ in range(int(input())):
	n = int(input())
	x = list(map(int, input().split()))
	mn = 10000000.0
	c = 0
	for i in range(n - 1, -1, -1):
		mn = min(mn, x[i])
		if x[i] > mn:
			c = c + 1
	print(c)


import math

def fact(n):
	ans = 1
	for i in range(2, n + 1):
		ans *= i
	return ans

def comb(n, c):
	return fact(n) // (fact(n - c) * c)
for _ in range(int(input())):
	n = int(input())
	nums = list(map(int, input().split()))[::-1]
	ans = 0
	m = nums[0]
	for i in range(1, n):
		if nums[i] > m:
			ans += 1
		m = min(m, nums[i])
	print(ans)


def dprint(*ar):
	pass
T = int(input())
for _ in range(T):
	N = int(input())
	vA = list(map(int, input().split()))
	res = 0
	nowA = vA[-1]
	prevA = vA[-1]
	dprint('\n', vA)
	for a in reversed(vA):
		if a > prevA:
			nowA = min(nowA, prevA)
			res += 1
		elif a > nowA:
			res += 1
		prevA = a
		dprint(a, res, nowA, prevA)
	print(res)


for iiiii in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	mc = a[-1]
	ans = 0
	for x in a[:-1][::-1]:
		if x <= mc:
			mc = x
		else:
			ans += 1
	print(ans)


q = int(input())
for _ in range(q):
	n = int(input())
	arr = list(map(int, input().split()))
	mi = arr[-1]
	ans = 0
	for i in range(n - 2, -1, -1):
		if arr[i] > mi:
			ans += 1
		mi = min(mi, arr[i])
	print(ans)


for tc in range(int(input())):
	n = int(input())
	l = [i for i in map(int, input().split())]
	tep = l[n - 1]
	c = 0
	for i in range(n - 2, -1, -1):
		if l[i] < tep:
			tep = l[i]
		elif l[i] > tep:
			c += 1
	print(c)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	if n == 1:
		print(0)
	elif n == 2:
		if a[0] > a[1]:
			print(1)
		else:
			print(0)
	else:
		a = a[::-1]
		mi = a[0]
		cnt = 0
		for i in a[1:]:
			if i > mi:
				cnt += 1
			else:
				mi = i
		print(cnt)


def find_num_of_bad_prices(arr):
	minn = arr[len(arr) - 1]
	bad_days = 0
	for i in range(len(arr) - 2, -1, -1):
		minn = min(minn, arr[i])
		if arr[i] > minn:
			bad_days += 1
	return bad_days

def read_input(flag=0):
	if flag > 0:
		return int(input())
	return [int(i) for i in input().split()]
t = read_input(1)
for i in range(t):
	n = read_input(1)
	arr = read_input()
	print(find_num_of_bad_prices(arr))


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


t = int(input())
for i in range(t):
	n = int(input())
	a = [int(x) for x in input().split()]
	answer = 0
	smallestsofar = a[-1]
	for i in range(len(a) - 2, -1, -1):
		if a[i] > smallestsofar:
			answer += 1
		elif a[i] < smallestsofar:
			smallestsofar = a[i]
	print(answer)


import math
from decimal import Decimal
import heapq

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 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 Factor(n):
	Ans = []
	d = 2
	while d * d <= n:
		if n % d == 0:
			Ans.append(d)
			n //= d
		else:
			d += 1
	if n > 1:
		Ans.append(n)
	return Ans
t = int(input())
for _ in range(t):
	(n, a) = na()
	b = [0] * (n + 1)
	mn = a[n - 1]
	b[n - 1] = mn
	for i in range(n - 2, -1, -1):
		mn = min(a[i], mn)
		b[i] = mn
	tc = 0
	for i in range(n - 1):
		if a[i] > b[i]:
			tc += 1
	print(tc)


for t in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	m = 10 ** 7
	ans = 0
	for i in range(n - 1, -1, -1):
		if a[i] > m:
			ans += 1
		if a[i] < m:
			m = a[i]
	print(ans)


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


t = int(input())
for u in range(t):
	n = int(input())
	l = [int(x) for x in input().split()]
	mi = l[-1]
	cont = 0
	for i in range(n - 1, -1, -1):
		if l[i] > mi:
			cont += 1
		else:
			mi = l[i]
	print(cont)


queries = int(input())
for x in range(queries):
	badDays = 0
	numDays = int(input())
	days = [int(x) for x in input().split(' ')]
	min = days[numDays - 1]
	for x in range(numDays - 2, -1, -1):
		if days[x] > min:
			badDays += 1
		else:
			min = days[x]
	print(badDays)


for _ in range(int(input())):
	n = int(input())
	a = [int(i) for i in input().split()]
	a.reverse()
	l = a.copy()
	for i in range(1, n):
		l[i] = min(l[i - 1], a[i])
	ans = 0
	for i in range(n):
		if a[i] > l[i]:
			ans += 1
	print(ans)


t = int(input())
for i in range(t):
	n = int(input())
	x = list(map(int, input().split()))
	x_min = x[-1]
	j = -1
	cnt = 0
	while j != -n - 1:
		if x[j] < x_min:
			x_min = x[j]
		elif x[j] > x_min:
			cnt += 1
		j -= 1
	print(cnt)


for i in range(int(input())):
	a = int(input())
	s = list(map(int, input().split()))
	t = 0
	p = s[-1]
	for k in range(a - 1, -1, -1):
		if s[k] > p:
			t += 1
		elif s[k] < p:
			p = s[k]
	print(t)


t = int(input())
for tt in range(t):
	n = int(input())
	a = [int(x) for x in input().strip().split()]
	b = 1000000
	c = 0
	for i in a[::-1]:
		if i > b:
			c += 1
		if i < b:
			b = i
	print(c)


t = int(input())
for q in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	mn = a[n - 1]
	ans = 0
	for i in range(n - 1, -1, -1):
		if a[i] > mn:
			ans += 1
		mn = min(mn, a[i])
	print(ans)


def fun(arr, n):
	m = arr[n - 1]
	count = 0
	for i in range(2, n + 1):
		if arr[-i] > m:
			count += 1
		m = min(m, arr[-i])
	print(count)
for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	fun(arr, n)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	m = a[n - 1]
	k = 0
	for i in range(n - 2, -1, -1):
		if a[i] > m:
			k += 1
		elif a[i] < m:
			m = min(a[i], m)
	print(k)


import sys
tc = int(sys.stdin.readline())
for _ in range(tc):
	n = int(sys.stdin.readline())
	arr = list(map(int, sys.stdin.readline().split()))
	st = []
	ans = 0
	for i in arr:
		if not st:
			st.append(i)
		else:
			while st:
				if st[-1] > i:
					st.pop()
					ans += 1
				else:
					break
			st.append(i)
	print(ans)


queries = int(input())
for q in range(queries):
	days = int(input())
	if days == 1:
		input()
		print('0')
		continue
	array = list(map(int, input().split()))
	bad = 0
	lowest = array[-1]
	for i in range(days - 2, -1, -1):
		if array[i] > lowest:
			bad += 1
		else:
			lowest = array[i]
	print(bad)


def read():
	n = int(input())
	a = list(map(int, input().split()))
	return (n, a)

def solve(n, a):
	c = 0
	m = a[-1]
	for i in range(n - 2, -1, -1):
		if a[i] > m:
			c += 1
		m = min(m, a[i])
	return c
for i in range(int(input())):
	result = solve(*read())
	print(result)


def do_task(array):
	min_val = 99999999
	inv_count = 0
	array = array[::-1]
	for elem in array:
		if elem <= min_val:
			min_val = elem
		else:
			inv_count += 1
	return inv_count
queries = int(input())
for q in range(queries):
	no_days = int(input())
	prices = input().split(' ')
	num_prices = []
	for i in range(no_days):
		num_prices.append(int(prices[i]))
	inversions = 0
	if no_days == 1:
		print(0)
	else:
		inv_count = do_task(num_prices)
		print(inv_count)


for _ in range(int(input())):
	n = int(input())
	x = [*map(int, input().split())][::-1]
	ans = 0
	mini = x[0]
	for i in range(n):
		if x[i] > mini:
			ans += 1
		else:
			mini = x[i]
	print(ans)


t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(i) for i in input().split()]
	bad_count = 0
	min_price = 10 ** 6 + 1
	for i in reversed(range(n)):
		if a[i] > min_price:
			bad_count += 1
		elif a[i] < min_price:
			min_price = a[i]
	print(bad_count)


t = int(input().strip())
for _ in range(t):
	n = int(input().strip())
	a = list(map(int, input().strip().split(' ')))
	aux = [0] * n
	aux[n - 1] = a[n - 1]
	for i in range(n - 1):
		aux[n - 2 - i] = min(a[n - 2 - i], aux[n - 2 - i + 1])
	ans = 0
	for i in range(n):
		if a[i] > aux[i]:
			ans += 1
	print(ans)


import sys
t = int(input())
ans = []
for test in range(t):
	n = int(input())
	arr = [int(x) for x in input().split()]
	minarr = [-1] * (n + 1)
	x = sys.maxsize
	i = n - 1
	count = 0
	while i >= 0:
		minarr[i] = x
		if arr[i] < x:
			x = arr[i]
		i -= 1
	for i in range(n):
		if arr[i] > minarr[i]:
			count += 1
	ans.append(count)
for i in range(t):
	print(ans[i])


t = int(input())
for i in range(t):
	n = int(input())
	days = list(map(int, input().split(' ')))
	minimum = float('inf')
	total = 0
	for j in range(len(days) - 1, -1, -1):
		if days[j] <= minimum:
			minimum = days[j]
		else:
			total += 1
	print(total)


t = int(input())
for _ in range(t):
	n = int(input())
	c = 0
	arr = [int(x) for x in str(input()).split()]
	mini = arr[-1]
	for i in arr[::-1]:
		if mini >= i:
			mini = i
		else:
			c += 1
	print(c)


import math
for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	mini = arr[-1]
	count = 0
	for i in range(n - 2, -1, -1):
		if arr[i] > mini:
			count += 1
		if arr[i] < mini:
			mini = arr[i]
	print(count)


def nhap():
	n = int(input())
	n -= 1
	a = []
	a = [int(x) for x in input().split()]
	a.reverse()
	m = a[0]
	dem = 0
	for x in a:
		if x > m:
			dem += 1
		else:
			m = min(m, x)
	print(dem)

def main():
	test = int(input())
	while test > 0:
		nhap()
		test -= 1
main()


def solve(xs):
	ys = [-1 for x in xs]
	stack = []
	c = 0
	for (i, x) in enumerate(xs):
		while len(stack) > 0 and x < xs[stack[-1]]:
			c += 1
			ys[stack.pop()] = x
		stack.append(i)
	return c
t = int(input())
for j in range(t):
	a = int(input())
	b = list(map(int, input().split()))
	print(solve(b))


n = int(input())
for i in range(n):
	k = int(input())
	a = list(map(int, input().split()))
	m = 10 ** 6
	amount = 0
	for r in range(k):
		if m < a[k - 1 - r]:
			amount += 1
		else:
			m = a[k - 1 - r]
	print(amount)


t = int(input())
output = []
for i in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	c = 0
	j = n - 2
	m = arr[n - 1]
	while j >= 0:
		if arr[j] > m:
			c += 1
		else:
			m = arr[j]
		j -= 1
	output.append(str(c))
print('\n'.join(output))


t = int(input())
while t:
	n = int(input())
	arr = [int(x) for x in input().split()]
	cmp = arr[-1]
	answer = 0
	for i in range(-1, -(len(arr) + 1), -1):
		if arr[i] > cmp:
			answer += 1
		cmp = min(cmp, arr[i])
	print(answer)
	t -= 1


t = int(input())
for i in range(t):
	n = int(input())
	i = list(map(int, input().split()))
	bd_count = 0
	min_num = i[-1]
	for day in i[len(i) - 2::-1]:
		if day > min_num:
			bd_count += 1
		min_num = min(day, min_num)
	print(bd_count)


for _ in range(int(input())):
	n = int(input())
	data = [int(i) for i in input().split()]
	mx = [0] * n
	mx1 = 1 << 50
	for i in range(n - 1, -1, -1):
		mx[i] = mx1
		mx1 = min(data[i], mx1)
	ans = 0
	for i in range(n):
		if data[i] > mx[i]:
			ans += 1
	print(ans)


for _ in range(int(input())):
	input()
	ans = 0
	lowest = 1000001
	for ai in map(int, reversed(input().split())):
		if ai > lowest:
			ans += 1
		else:
			lowest = ai
	print(ans)


for _ in range(int(input())):
	n = int(input())
	l1 = list(map(int, input().split()))
	m = l1[-1]
	ans = 0
	for i in range(-2, -n - 1, -1):
		if l1[i] > m:
			ans += 1
		else:
			m = l1[i]
	print(ans)


Q = int(input())
for q in range(Q):
	n = int(input())
	arr = list(map(int, input().split()))
	minimal = 1000000000.0
	bad = 0
	for i in arr[::-1]:
		if i < minimal:
			minimal = i
		if i > minimal:
			bad += 1
	print(bad)


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


t = int(input())
out = []
for st in range(0, t):
	N = int(input())
	prices = [int(x) for x in input().split()]
	counter = 0
	min = prices[N - 1]
	for i in range(N - 2, -1, -1):
		if prices[i] > min:
			counter += 1
		else:
			min = prices[i]
	out += [counter]
for i in range(0, t):
	print(out[i])


n = int(input())
answers = []
for _ in range(n):
	z = int(input())
	ks = list(map(int, input().split()))
	ans = 0
	z = 0
	i = 0
	leng = len(ks)
	while z < leng - 1:
		z += 1
		i += 1
		if ks[-i] < ks[-(i + 1)]:
			del ks[-(i + 1)]
			i -= 1
			ans += 1
	answers.append(ans)
for a in answers:
	print(a)


t = int(input())
for ii in range(t):
	n = int(input())
	s = input().split(' ')
	A = [int(x) for x in s][::-1]
	res = 0
	x = A[0]
	for (i, a) in enumerate(A[1:]):
		if a > x:
			res += 1
		elif a < x:
			x = a
	print(res)


t = int(input())
while t:
	n = int(input())
	l = list(map(int, input().split()))
	c = 0
	l1 = [min(l[n - 1], l[n - 2])]
	for i in range(n - 3, -1, -1):
		l1.append(min(l[i], l1[-1]))
	for i in range(n - 2, -1, -1):
		if l[i] > l1[n - 1 - i - 1]:
			c += 1
	print(c)
	t -= 1


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


for i in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	res = 0
	mi = 10000000000000000000000000000
	for i in range(n - 1, -1, -1):
		x = a[i]
		if x > mi:
			res += 1
		else:
			mi = x
	print(res)


t = int(input())
for i in range(t):
	j = 0
	min = 100000000
	n = int(input())
	mas = list(map(int, input().split()))
	for el in reversed(mas):
		if el <= min:
			min = el
		else:
			j += 1
	print(j)


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


t = int(input())
for i in range(t):
	n = int(input())
	days = [int(j) for j in input().split()]
	cnt = 0
	stack = []
	for j in range(len(days) - 1, 0, -1):
		if j == len(days) - 1:
			stack.append(days[j])
		elif stack[-1] > days[j]:
			stack.append(days[j])
		else:
			stack.append(stack[-1])
	for j in range(len(days) - 1):
		if days[j] > stack[-1]:
			cnt += 1
		stack.pop()
	print(cnt)


t = int(input())
for i in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	an = 0
	ma = l[n - 1]
	for j in range(n - 1, -1, -1):
		if l[j] < ma:
			ma = l[j]
		if l[j] > ma:
			an += 1
	print(an)


for i in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	m = 10000000
	c = 0
	for i in reversed(l):
		m = min(m, i)
		if m != i:
			c += 1
	print(c)


from math import inf
t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	mi = inf
	ans = 0
	for i1 in a[::-1]:
		if i1 > mi:
			ans += 1
		mi = min(i1, mi)
	print(ans)


m = int(input())
while m:
	size = int(input())
	l = list(map(int, input().split()))
	mini = l[size - 1]
	ans = 0
	for i in range(size - 2, -1, -1):
		if l[i] > mini:
			ans += 1
		mini = min(l[i], mini)
	print(ans)
	m -= 1


for _ in range(int(input())):
	int(input())
	a = list(map(int, input().split()))
	max = 1000000000
	cnt = 0
	for i in reversed(a):
		if i > max:
			cnt += 1
		max = min(max, i)
	print(cnt)


tc = int(input())
for _ in range(tc):
	n = int(input())
	a = list(map(int, input().split()))
	lowest = a[n - 1]
	res = 0
	for i in range(n - 1, -1, -1):
		if a[i] > lowest:
			res += 1
		lowest = min(lowest, a[i])
	print(res)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	(ans, min1) = (0, float('inf'))
	for q in range(n - 1, -1, -1):
		if a[q] > min1:
			ans += 1
		min1 = min(min1, a[q])
	print(ans)


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	curr_min = arr[-1]
	que = [0] * n
	for i in range(n - 1, -1, -1):
		curr_min = min(curr_min, arr[i])
		que[i] = curr_min
	ans = 0
	for i in range(n - 1):
		if arr[i] > que[i]:
			ans += 1
	print(ans)


for _ in range(int(input())):
	n = input()
	mv = 10 ** 9
	ans = 0
	for x in reversed(list(map(int, input().split()))):
		if x > mv:
			ans += 1
		mv = min(mv, x)
	print(ans)


import sys

def main():
	n = int(sys.stdin.readline().strip())
	for _ in range(n):
		k = int(sys.stdin.readline().strip())
		xs = [int(s) for s in sys.stdin.readline().strip().split()]
		xm = []
		prev = xs[-1]
		for x in reversed(xs):
			xm.append(min(x, prev))
			prev = xm[-1]
		res = sum((1 for (a, b) in zip(xs, reversed(xm)) if a > b))
		print(res)
main()


t = int(input())
for w in range(t):
	n = int(input())
	l = [int(i) for i in input().split()]
	m = l[n - 1]
	c = 0
	for i in range(n):
		if l[n - i - 1] > m:
			c += 1
		else:
			m = l[n - i - 1]
	print(c)


t = int(input())
while t:
	n = int(input())
	a = list(map(int, input().strip().split()))
	mn = 10 ** 7
	cnt = 0
	a.reverse()
	for i in range(len(a)):
		if a[i] > mn:
			cnt += 1
		mn = min(mn, a[i])
	print(cnt)
	t -= 1


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


for i in range(int(input())):
	a = int(input())
	b = list(map(int, input().split()))
	c = b[-1]
	ans = 0
	for i in range(2, len(b) + 1):
		if b[-i] > c:
			ans += 1
		c = min(c, b[-i])
	print(ans)


import datetime

class Stack:

	def __init__(self):
		self.items = []

	def push(self, item):
		self.items.append(item)

	def pop(self):
		self.items.pop(-1)

	def is_empty(self):
		return self.items == []

	def peek(self):
		if not self.items == []:
			return self.items[-1]

	def get_stack(self):
		return self.items
t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	count_ = 0
	arr.reverse()
	s = Stack()
	for i in arr:
		if s.is_empty():
			s.push(i)
		elif s.peek() < i:
			count_ += 1
			s.push(i)
		else:
			while s.peek() >= i:
				s.pop()
				if s.is_empty():
					break
			if s.is_empty():
				s.push(i)
			elif s.peek() < i:
				count_ += 1
				s.push(i)
	print(count_)


def fun(arr_inp):
	res = 0
	arr_inp.reverse()
	mn = arr_inp[0]
	for temp in arr_inp:
		if temp > mn:
			res += 1
		else:
			mn = temp
	return res
n = int(input())
for i in range(n):
	n1 = int(input())
	arr = [int(x) for x in input().split()]
	print(fun(arr))


for _ in range(int(input())):
	n = int(input())
	ans = 0
	l = [int(x) for x in input().split()]
	mi = 1000000
	for i in reversed(l):
		if i > mi:
			ans += 1
		mi = min(mi, i)
	print(ans)


from sys import stdin
input = stdin.buffer.readline
t = int(input())
for i in range(t):
	n = int(input())
	arr = [int(x) for x in input().split()]
	cnt = 0
	m = arr[n - 1]
	for i in range(n - 2, -1, -1):
		if arr[i] > m:
			cnt = cnt + 1
		else:
			m = arr[i]
	print(cnt)


res = []
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	m = a[-1]
	r = 0
	for i in a[-2::-1]:
		if i > m:
			r += 1
		m = min(m, i)
	res.append(r)
print(*res, sep='\n')


t = int(input())
baddays = [0 for i in range(t)]
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	m = a[-1]
	for j in range(n - 1, -1, -1):
		if a[j] > m:
			baddays[i] += 1
		else:
			m = a[j]
for i in baddays:
	print(i)


T = int(input())
for i in range(T):
	N = int(input())
	inpt = map(int, input().split())
	days = [(inpt.__next__(), j) for j in range(N)]
	days.sort()
	sum = 0
	p = 0
	for day in days:
		d = day[1]
		if d >= p:
			sum += d - p
			p = d + 1
	print(sum)


def mp():
	return map(int, input().split())
q = int(input())
for qq in range(q):
	n = int(input())
	a = list(mp())
	ans = 0
	m = 10 ** 9
	for i in range(n - 1, -1, -1):
		m = min(a[i], m)
		if a[i] > m:
			ans += 1
	print(ans)


minn = 10 ** 6 + 1

def fun(num):
	global minn
	if minn >= num:
		minn = num
		return 0
	return 1
t = int(input())
for i in range(t):
	minn = 10 ** 6 + 1
	n = int(input())
	ai = list(map(int, input().split()))
	ai = [fun(ai[i]) for i in range(n - 1, -1, -1)]
	print(sum(ai))


print(*[len([1 for q in [[a[q[0]] if a[q[0]][0] != 0 and len(a[q[0]]) == 3 and (a[a[q[0]][0] - 1].append(min(a[q[0]][2], a[q[0]][1])) is None) else a[q[0]] if a[q[0]][0] != 0 and len(a[q[0]]) == 2 and (a[a[q[0]][0] - 1].append(a[q[0]][1]) is None) else a[q[0]] for q in [[q[0], q[1][1]] if a[q[0]].insert(0, q[0]) else [q[0], q[1][1]] for q in enumerate(a)][::-1]][::-1] for (n, a) in [[int(input()), [[q] for q in list(map(int, input().split()))]]]][0] if len(q) != 2 and q[2] < q[1]]) for q in range(int(input()))], sep='\n')


for _ in range(int(input())):
	n = int(input())
	p = list(map(int, input().split()))
	count = 0
	minx = p[n - 1]
	for i in range(n - 2, -1, -1):
		if p[i] > minx:
			count += 1
		minx = min(p[i], minx)
	print(count)


num = int(input())
for i in range(num):
	n = int(input())
	lst = list(map(int, input().split()))
	count = 0
	mini = lst[-1]
	for j in lst[::-1]:
		if j > mini:
			count += 1
		elif j < mini:
			mini = j
	print(count)


for i in range(int(input())):
	c = 0
	n = int(input())
	a = input().split()[::-1]
	mi = int(a[0])
	for i in a:
		if int(i) > mi:
			c += 1
		else:
			mi = int(i)
	print(c)


t = int(input())
for _ in range(t):
	n = int(input())
	arr = [int(x) for x in input().split(' ')]
	small = arr[-1]
	c = 0
	for x in range(n - 1, -1, -1):
		if arr[x] > small:
			c += 1
		else:
			small = arr[x]
	print(c)


from array import array
q = int(input())
for i in range(q):
	n = int(input())
	a = array('L', map(int, input().split()))
	bd = 0
	m = 10 ** 6
	for i in range(n - 1, -1, -1):
		if a[i] > m:
			bd += 1
		else:
			m = min(m, a[i])
	print(bd)


t = int(input())
while t > 0:
	n = int(input())
	a = [int(i) for i in input().split()]
	min = a[n - 1]
	c = 0
	for i in range(n - 1, -1, -1):
		if a[i] > min:
			c += 1
		elif a[i] < min:
			min = a[i]
	print(c)
	t -= 1


t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(x) for x in input().split()]
	(badDays, i) = (0, 0)
	stk = []
	while i < n:
		if len(stk) == 0 or a[i] >= stk[-1]:
			stk.append(a[i])
			i += 1
		elif a[i] < stk[-1]:
			stk.pop()
			badDays += 1
	print(badDays)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = [-1] * n
	b[n - 1] = a[n - 1]
	ans = 0
	for i in range(n - 2, -1, -1):
		b[i] = min(b[i + 1], a[i])
	for i in range(n - 1):
		if a[i] > b[i]:
			ans += 1
	print(ans)


n = int(input())
for _ in range(n):
	good = int(input())
	liste = [int(x) for x in input().split()]
	count = 0
	mini = liste[-1]
	for i in range(good - 1, -1, -1):
		if liste[i] > mini:
			count += 1
		else:
			mini = liste[i]
	print(count)


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	m = l[n - 1]
	ans = 0
	for i in range(n - 2, -1, -1):
		if l[i] > m:
			ans += 1
		m = min(m, l[i])
	print(ans)


from sys import stdin, stdout
input = stdin.readline
t = int(input())
for testcase in range(t):
	n = map(int, input().split())
	a = [int(i) for i in input().split()]
	a.reverse()
	mini = 1000 * 1000 * 1000
	res = 0
	for i in a:
		res += mini < i
		mini = min(mini, i)
	print(res)


tcases = int(input())
datalist = []
for i in range(tcases):
	bakwaas = int(input())
	m = list(map(int, input().split()))
	datalist.append(m)

def badcount(somelist):
	bcount = 0
	n = len(somelist)
	mini = somelist[-1]
	for i in range(1, n):
		j = -(i + 1)
		if somelist[j] > mini:
			bcount += 1
		else:
			mini = somelist[j]
	return bcount
for i in range(len(datalist)):
	print(badcount(datalist[i]))


import math
tc = int(input())
for _ in range(tc):
	n = int(input())
	a = [int(x) for x in input().split()]
	z = []
	m = 999999999
	for i in range(n - 1, -1, -1):
		m = min(a[i], m)
		z.append(m)
	z = z[::-1]
	c = 0
	for i in range(n):
		if a[i] > z[i]:
			c = c + 1
	print(c)


import sys
from collections import defaultdict as dd
from itertools import permutations as pp
from itertools import combinations as cc
from collections import Counter as ccd
from random import randint as rd
from bisect import bisect_left as bl
from heapq import heappush as hpush
from heapq import heappop as hpop
mod = 10 ** 9 + 7

def ri(flag=0):
	if flag == 0:
		return [int(i) for i in sys.stdin.readline().split()]
	else:
		return int(sys.stdin.readline())
for _ in range(ri(1)):
	n = ri(1)
	a = ri()
	suf = []
	b = a[::-1]
	suf.append(b[0])
	for i in range(1, n):
		suf.append(min(suf[-1], b[i]))
	ans = 0
	suf = suf[::-1]
	for i in range(n):
		if a[i] > suf[i]:
			ans += 1
	print(ans)


n = int(input())
m = 0
c = 0
a = []
for i in range(n):
	m = int(input())
	a = list(map(int, input().split()))
	b = 0
	if m == 1:
		print(0)
	else:
		mina = a[-1]
		for j in range(m - 1, 0, -1):
			if a[j] < mina:
				mina = a[j]
			if a[j - 1] > mina:
				b += 1
		print(b)


t = int(input())
for _ in range(t):
	n = int(input())
	arr = [int(i) for i in input().split(' ')]
	small = [1000000] * len(arr)
	for (index, val) in enumerate(reversed(arr)):
		small[index] = min(small[index], val)
		if index < len(arr) - 1:
			small[index + 1] = small[index]
	i = 0
	ans = 0
	while i < len(small):
		j = i + 1
		while j < len(small) and small[j] == small[i]:
			if arr[n - j - 1] != arr[n - i - 1]:
				ans += 1
			j += 1
		i = j
	print(ans)


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


def II():
	return int(SI())

def MI():
	return map(int, SI().split())

def LI():
	return list(MI())

def SI():
	return input()
T = II()
for _ in range(T):
	N = II()
	D = LI()
	min = 9999999
	T = []
	for i in range(N - 1, -1, -1):
		temp = D[i]
		if temp <= min:
			min = temp
		T.append(min)
	T.reverse()
	count = 0
	for i in range(N - 1):
		if D[i] > T[i]:
			count += 1
	print(count)


def solve(arr, n):
	mv = min(arr)
	(i, j) = (0, 0)
	ans = 1
	a = [0] * n
	a[-1] = arr[-1]
	for x in range(n - 2, -1, -1):
		if arr[x] <= a[x + 1]:
			a[x] = arr[x]
			ans += 1
		else:
			a[x] = a[x + 1]
	return n - ans
t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	print(solve(arr, n))


q = int(input())
for qu in range(q):
	n = int(input())
	l = list(map(int, input().split()))
	l.reverse()
	mini = l[0]
	wyn = 0
	for i in range(n):
		if l[i] > mini:
			wyn += 1
		mini = min(mini, l[i])
	print(wyn)


for j in range(int(input())):
	n = int(input())
	a = [int(i) for i in input().split()]
	temp = 0
	minn = max(a)
	for i in range(n - 1, -1, -1):
		if a[i] > minn:
			temp += 1
		minn = min(minn, a[i])
	print(temp)


t = int(input())
while t:
	n = int(input())
	l = list(map(int, input().split()))
	m = 1000000
	r = 0
	for x in reversed(l):
		if x > m:
			r += 1
		m = min(m, x)
	print(r)
	t = t - 1


def solve():
	input()
	a = list(map(int, input().split()))
	mn = 10 ** 9
	cnt = 0
	for aa in reversed(a):
		if aa > mn:
			cnt += 1
		else:
			mn = aa
	print(cnt)

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


def check_bad_days(days, n):
	count = 0
	bad = days[n - 1]
	for i in range(n - 2, -1, -1):
		if bad < days[i]:
			count += 1
		else:
			bad = days[i]
	return count
for _ in range(int(input())):
	n = int(input())
	li = list(map(int, input().strip().split()))
	print(check_bad_days(li, n))


t = int(input())
for i in range(t):
	n = int(input())
	lst = list(map(int, input().split()))
	Min = lst[n - 1]
	ans = 0
	temp = n - 2
	while temp >= 0:
		if lst[temp] > Min:
			ans += 1
		else:
			Min = lst[temp]
		temp -= 1
	print(ans)


t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(x) for x in input().split()]
	k = 0
	m = a[-1]
	for i in range(n - 1):
		if a[n - i - 2] > m:
			k += 1
		if a[n - i - 2] < m:
			m = a[n - i - 2]
	print(k)


read = lambda : [int(i) for i in input().split()]
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(reversed(read()))
	cur_min = a[0]
	ans = 0
	for i in a:
		if i > cur_min:
			ans += 1
		else:
			cur_min = i
	print(ans)


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split(' ')))
	lowest = a[::-1][0]
	count = 0
	days = []
	for j in a[::-1]:
		if j <= lowest:
			lowest = j
		else:
			count += 1
	print(count)


n = int(input().strip())
for i in range(n):
	size_arr = int(input().strip())
	arr = list(map(int, input().strip().split()))
	arr.reverse()
	bad_days = 0
	min = 100000000.0
	for i in arr:
		if i > min:
			bad_days += 1
		else:
			min = i
	print(bad_days)


t = int(input())
while t != 0:
	n = int(input())
	l = list(map(int, input().strip().split()))
	(mini, bad) = (10000000, 0)
	for i in reversed(l):
		if i > mini:
			bad += 1
		if i < mini:
			mini = i
	print(bad)
	t -= 1


n = int(input())
for i in range(n):
	a = int(input())
	li = list(map(int, input().split()))
	pivot = li[-1]
	count = 0
	for i in range(-2, -len(li) - 1, -1):
		if pivot < li[i]:
			count += 1
		else:
			pivot = li[i]
	print(count)


import sys
import math
get_string = lambda : sys.stdin.readline().strip()
get_list = lambda : list(map(int, sys.stdin.readline().strip().split()))
get_int = lambda : int(sys.stdin.readline())
for _ in range(get_int()):
	n = get_int()
	lst = get_list()[::-1]
	m = lst[0]
	count = 0
	for i in range(1, n):
		if lst[i] > m:
			count += 1
		m = min(m, lst[i])
	print(count)


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().strip().split()))
	min_ = arr[-1]
	ans = 0
	for x in arr[0:-1][::-1]:
		if x <= min_:
			min_ = x
		else:
			ans += 1
	print(ans)


t = int(input())
while t:
	n = int(input())
	a = input().split()
	for i in range(n):
		a[i] = int(a[i])
	m = a[n - 1]
	res = 0
	for i in range(n - 1, 0, -1):
		if a[i - 1] > m:
			res += 1
		else:
			m = a[i - 1]
	print(res)
	t -= 1


t = int(input())
for _ in range(t):
	n = int(input())
	tmp = input().split()
	mn = 1000001
	bad_day = 0
	for i in range(n - 1, -1, -1):
		value = int(tmp[i])
		if value > mn:
			bad_day += 1
		else:
			mn = value
	print(bad_day)


from __future__ import division, print_function
import os
import sys
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))
input = lambda : sys.stdin.readline().rstrip('\r\n')
import collections, re, itertools, statistics, math
from collections import *
from itertools import *
from statistics import *
from math import *
from re import *
mod = int(1000000000.0 + 7)
imin = -2147483648
imax = 2147483647
true = True
false = False
none = None
inp = lambda : input()
I = lambda : int(inp())
M = lambda : map(int, inp().split())
L = lambda : list(M())
Yes = lambda : print('Yes')
No = lambda : print('No')
YES = lambda : print('YES')
NO = lambda : print('NO')
chartonum = {'1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9, '0': 0}

def IO(controll):
	if controll == false:
		sys.stdin = open('input.txt', 'r')
		sys.stdout = open('output.txt', 'w')
	else:
		pass

def true_yes(statement):
	return YES() if statement else NO()

def state(statement, one, two):
	print(one if statement else two)

def swap(a, b):
	(a, b) = (b, a)

def debug(something):
	print('Raw = {', end='')
	print(*something, sep=', ', end='')
	print('}')

def prec(a):
	return '{0:.9f}'.format(a)

def binary_search(arr, low, high, x):
	while low <= high:
		mid = low + (high - low) // 2
		if arr[mid] == x:
			return [mid, true]
		elif arr[mid] > x:
			high = mid - 1
		else:
			low = mid - 1
	return [-1, false]

def kabraji_ka_totka():
	IO(1)
	for i in range(I()):
		n = I()
		arr = L()
		res = 0
		comp = imax
		for i in range(n - 1, -1, -1):
			if arr[i] > comp:
				res += 1
			comp = min(comp, arr[i])
		print(res)
kabraji_ka_totka()


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	a.reverse()
	cnt = 0
	min_price = max(a) + 1
	for cur in a:
		min_price = min(min_price, cur)
		if min_price < cur:
			cnt += 1
	print(cnt)


import sys
Q = int(input())
for x in range(Q):
	N = int(input())
	L = list(map(int, input().split()))
	r = 0
	m = L[-1]
	for n in range(1, N + 1):
		if L[-n] > m:
			r = r + 1
		if L[-n] < m:
			m = L[-n]
	print(r)


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


t = int(input())
for j in range(t):
	n = int(input())
	result = 0
	array = list(map(int, input().split(' ')))
	array.reverse()
	minv = array[0]
	for i in range(1, len(array)):
		if minv >= array[i]:
			minv = array[i]
		else:
			result += 1
	print(result)


from math import log2

class Sparse:

	def __init__(self, a):
		self.a = a
		self.n = len(a)
		self.k = int(log2(self.n)) + 1
		self.lookup = [[0] * self.k for x in range(self.n)]
		self.process()

	def process(self):
		for i in range(self.n):
			self.lookup[i][0] = self.a[i]
		j = 1
		while 1 << j <= self.n:
			i = 0
			while i + (1 << j) - 1 < self.n:
				self.lookup[i][j] = min(self.lookup[i][j - 1], self.lookup[i + (1 << j - 1)][j - 1])
				i += 1
			j += 1

	def query(self, a, b):
		x = int(log2(b - a + 1))
		return min(self.lookup[a][x], self.lookup[b - (1 << x) + 1][x])
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	sp = Sparse(a)
	bd = 0
	for i in range(n - 1):
		if a[i] > sp.query(i, n - 1):
			bd += 1
	print(bd)


n = int(input())
while n:
	m = int(input())
	l = list(map(int, input().split()))
	l = l[::-1]
	i = m - 1
	stckk = list()
	l2 = [0] * m
	while i >= 0:
		if len(stckk) == 0 or l[stckk[-1]] <= l[i]:
			stckk.append(i)
		else:
			while len(stckk) > 0 and l[stckk[-1]] > l[i]:
				l2[stckk[-1]] = i
				stckk.pop()
			stckk.append(i)
		i -= 1
	print(m - len(stckk))
	n -= 1


for _ in range(int(input())):
	x = int(input())
	l = [int(i) for i in input().split()]
	bad = 0
	c = l[x - 1]
	for i in range(x - 2, -1, -1):
		if l[i] > c:
			bad += 1
		else:
			c = l[i]
	print(bad)


test = int(input())
for t in range(test):
	n = int(input())
	l = [int(x) for x in input().split()]
	le = len(l)
	bd = 0
	m = 1000001
	for i in range(le - 1, -1, -1):
		if l[i] > m:
			bd += 1
		m = min(l[i], m)
	print(bd)


import sys, os, re, datetime, copy
from collections import *
from bisect import *
from math import *

def mat(v, *dims):

	def dim(i):
		return [copy.copy(v) for _ in range(dims[-1])] if i == len(dims) - 1 else [dim(i + 1) for _ in range(dims[i])]
	return dim(0)
__cin__ = None

def cin():
	global __cin__
	if __cin__ is None:
		__cin__ = iter(input().split(' '))
	try:
		return next(__cin__)
	except StopIteration:
		__cin__ = iter(input().split(' '))
		return next(__cin__)

def iarr(T, n):
	return [T(cin()) for _ in range(n)]

def carr(n):
	return input()

def imat(T, n, m):
	return [iarr(T, m) for _ in range(n)]

def cmat(n, m):
	return [input() for _ in range(n)]
t = int(cin())
for k in range(t):
	n = int(cin())
	a = iarr(int, n)
	p = mat(0, n)
	p[-1] = a[-1]
	for i in range(n - 2, -1, -1):
		p[i] = min(p[i + 1], a[i])
	tot = 0
	for i in range(n):
		if a[i] > p[i]:
			tot += 1
	print(tot)


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	p = l
	cnt = 0
	p.reverse()
	q = []
	for (i, a) in enumerate(p):
		if i == 0:
			q.append(p[i])
		elif a < q[i - 1]:
			q.append(p[i])
		else:
			q.append(q[i - 1])
	for i in range(n):
		if l[i] > q[i]:
			cnt += 1
	print(cnt)


for _ in range(int(input())):
	n = int(input())
	a = [int(o) for o in input().split()]
	s = []
	lis = []
	for i in range(n):
		if not s:
			s.append(a[i])
			continue
		while s and s[-1] > a[i]:
			lis.append(s.pop())
		s.append(a[i])
	print(len(lis))


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

def ln():
	return [int(x) for x in input().split()]

def solve():
	n = inp()
	a = ln()
	mns = [-1] * n
	mns[-1] = a[-1]
	for i in range(n - 2, -1, -1):
		mns[i] = min(a[i], mns[i + 1])
	cnt = 0
	for i in range(n):
		if i + 1 < n and mns[i + 1] < a[i]:
			cnt += 1
	print(cnt)
for _ in range(inp()):
	solve()


n = int(input())
for i in range(n):
	len = int(input())
	arr = list(map(int, input().split()))
	stack = [arr[0]]
	cnt = 0
	for i in range(1, len):
		if stack == []:
			stack.append(arr[i])
		elif arr[i] >= stack[-1]:
			stack.append(arr[i])
		else:
			while arr[i] < stack[-1]:
				stack.pop()
				cnt += 1
				if stack == []:
					break
			stack.append(arr[i])
	print(cnt)


t = int(input())
while t > 0:
	t -= 1
	n = int(input())
	a = [int(x) for x in input().split()]
	d = 0
	a_min = a[-1]
	for current in reversed(a):
		if current > a_min:
			d += 1
		else:
			a_min = current
	print(d)


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().strip().split()))[:n]
	count = 0
	mn = a[n - 1]
	for j in range(n - 1, -1, -1):
		if a[j] > mn:
			count += 1
		mn = min(mn, a[j])
	print(count)


t = int(input())
for i in range(0, t):
	n = int(input())
	a = list(map(int, input().split()))
	minim = a[n - 1]
	bad = 0
	for j in range(n - 1, 0, -1):
		if a[j] > minim:
			bad += 1
		else:
			minim = a[j]
	if a[0] > minim:
		bad += 1
	print(bad)


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()
	stack = [a[0]]
	bad_days = 0
	for i in range(1, n):
		if stack:
			while stack and a[i] < stack[-1]:
				bad_days += 1
				stack.pop()
		stack.append(a[i])
	print(bad_days)


T = int(input())
for _ in range(T):
	N = int(input())
	sk = [int(x) for x in input().split()]
	ans = 0
	maz = int(1000000000.0)
	for i in reversed(sk):
		if i > maz:
			ans += 1
		maz = min(i, maz)
	print(ans)


t = int(input())
for q in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	res = 0
	smallest = [-1 for i in range(n)]
	smallest[-1] = a[-1]
	for i in range(n - 2, -1, -1):
		if smallest[i + 1] < a[i]:
			res += 1
		smallest[i] = min(a[i], smallest[i + 1])
	print(res)


T = int(input())
for _ in range(T):
	n = int(input())
	a = list(map(int, input().split()))
	b = [None] * n
	tmin = a[-1]
	for i in range(n - 1, -1, -1):
		tmin = min(tmin, a[i])
		b[i] = tmin
	ans = 0
	for i in range(n):
		if a[i] > b[i]:
			ans += 1
	print(ans)


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	stack = [arr[0]]
	count = 0
	for i in range(1, n):
		if arr[i] >= stack[-1]:
			stack.append(arr[i])
		else:
			while len(stack) > 0 and arr[i] < stack[-1]:
				stack.pop(-1)
				count += 1
			stack.append(arr[i])
	print(count)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	p = 1000001
	cnt = 0
	for item in reversed(a):
		if item > p:
			cnt += 1
		p = min(item, p)
	print(cnt)


import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
	n = int(input())
	lst = list(map(int, input().split()))
	dp = [0] * n
	cnt = 0
	mn = 2019123291
	for i in range(len(lst) - 1, -1, -1):
		if mn >= lst[i]:
			mn = lst[i]
		else:
			cnt += 1
	print(cnt)


t = int(input())
while t > 0:
	t -= 1
	n = int(input())
	arr = list(map(int, input().split()))
	res = 0
	stack = []
	for i in arr:
		while len(stack) > 0 and stack[-1] > i:
			stack.pop()
		stack.append(i)
	res = n - len(stack)
	print(res)


t = int(input())
for _ in range(t):
	n = int(input())
	A = [int(i) for i in input().split()]
	ans = 0
	cur = A[-1]
	for i in reversed(range(n - 1)):
		if A[i] <= cur:
			cur = A[i]
		else:
			ans += 1
	print(ans)


n = int(input())
for x in range(n):
	a = int(input())
	array = list(map(int, input().split()))
	array = array[::-1]
	answer = 0
	mini = array[0]
	for it in array[1:]:
		if it <= mini:
			mini = it
		else:
			answer += 1
	print(answer)


def bad_price(a):
	right_max = max(a)
	count = 0
	for i in range(len(a) - 1, -1, -1):
		if right_max < a[i]:
			count = count + 1
		right_max = min(right_max, a[i])
	return count
ans = []
n = int(input())
while n != 0:
	temp = int(input())
	a = [int(x) for x in input().split()]
	ans.append(bad_price(a))
	n = n - 1
for a in ans:
	print(a)


user_input = int(input())
for i in range(user_input):
	a = int(input())
	b = list(map(int, input().split()))
	count = 0
	minimum = b[-1]
	for j in range(a - 2, -1, -1):
		if b[j] > minimum:
			count += 1
		minimum = min(b[j], minimum)
	print(count)


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):
	curr = arr[-1]
	c = 0
	for i in arr[-1::-1]:
		if curr < i:
			c += 1
		curr = min(i, curr)
	print(c)
for _ in range(int(input())):
	n = int(input())
	arr = int_arr()
	solve(n, arr)


t = int(input())
for i in range(t):
	n = int(input())
	c = 0
	p = list(map(int, input().split()))
	m = p[n - 1]
	for i in range(n - 1, -1, -1):
		if p[i] > m:
			c += 1
		m = min(p[i], m)
	print(c)


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


import copy
import sys
from collections import defaultdict, Counter

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

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

def maps():
	return map(str, input().split())

def print(arg, *argv, sep='', end=None):
	sys.stdout.write(str(arg))
	for i in argv:
		sys.stdout.write(sep + str(i)) if sep else sys.stdout.write(' ' + str(i))
	sys.stdout.write(end) if end else sys.stdout.write('\n')

def GCD(x, y):
	return GCD(y, x % y) if y else x

def modPow(x, y, p):
	(res, x) = (1, x % p)
	while y > 0:
		if y & 1 == 1:
			res = res * x % p
		(y, x) = (y >> 1, x * x % p)
	return res

def modInv(s, mod):
	return modPow(s, mod - 2, mod)

def solve():
	t = 1
	t = int(input())
	while t:
		t -= 1
		n = int(input())
		a = list(mapi())
		sf = [a[n - 1]]
		for i in range(n - 2, -1, -1):
			if a[i] < sf[-1]:
				sf.append(a[i])
			else:
				sf.append(sf[-1])
		sf = sf[::-1]
		res = 0
		for i in range(n):
			if a[i] > sf[i]:
				res += 1
		print(res)
solve()


from sys import stdin, stdout
T = int(stdin.readline())
for i in range(T):
	num = int(stdin.readline())
	ar = list(map(int, stdin.readline().split()))[::-1]
	st = ar[0]
	count = 0
	for j in range(len(ar)):
		if ar[j] > st:
			count += 1
		else:
			st = ar[j]
	print(count)


t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(i) for i in input().split()]
	b = [0] * n
	if n == 1:
		print(0)
	else:
		c = 0
		m = a[n - 1]
		for i in range(n - 2, -1, -1):
			b[i] = m
			if a[i] < m:
				m = a[i]
		for i in range(n):
			if i == n - 1:
				continue
			elif a[i] > b[i]:
				c += 1
		print(c)


t = int(input())
while t:
	n = int(input())
	lista = [int(x) for x in input().split()]
	ans = 0
	mn = 1000000000
	for x in range(n - 1, -1, -1):
		if lista[x] > mn:
			ans += 1
		mn = min(mn, lista[x])
	print(ans)
	t = t - 1


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


num_set = int(input())
for i in range(num_set):
	num = input()
	data = [int(j) for j in reversed(input().split(' '))]
	min_price = data[0]
	res = 0
	for price in data[1:]:
		if price <= min_price:
			min_price = price
		else:
			res = res + 1
	print(res)


t = int(input())
while t:
	n = int(input())
	a = list(map(int, input().split()))
	a.reverse()
	minn = a[0]
	ans = 0
	for i in range(1, n):
		if a[i] > minn:
			ans += 1
		minn = min(minn, a[i])
	print(ans)
	t -= 1


for _ in range(int(input())):
	n = int(input())
	l = [int(x) for x in input().split()]
	m = [100001] * n
	cnt = 0
	now = l[-1]
	for x in range(n - 2, -1, -1):
		if l[x] < now:
			now = l[x]
		m[x] = now
	for x in range(n - 1):
		if l[x] > m[x]:
			cnt += 1
	print(cnt)


t = int(input())
for s in range(t):
	n = int(input())
	days = list(map(int, input().split()))
	bad_days = 0
	minimum = days[-1]
	for day in days[::-1]:
		if day > minimum:
			bad_days += 1
		elif day < minimum:
			minimum = day
	print(bad_days)


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


t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	minn = arr[-1]
	count = 0
	for i in range(n - 2, -1, -1):
		if arr[i] > minn:
			count += 1
		else:
			minn = min(minn, arr[i])
	print(count)


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


for test in range(int(input())):
	n = int(input())
	arr = [int(x) for x in input().split(' ')]
	temp = float('inf')
	m = []
	for i in range(n - 1, -1, -1):
		temp = min(temp, arr[i])
		m.append(temp)
	bad = 0
	for i in range(n):
		if m[n - 1 - i] < arr[i]:
			bad += 1
	print(bad)


sets_number = int(input())
sets_sizes = []
sets = []
for i in range(sets_number):
	sets_sizes.append(int(input()))
	sets.append(list(map(int, input().split(' '))))
bad_days = []
for i in range(sets_number):
	bad_days_current = 0
	smallest_number_current = sets[i][sets_sizes[i] - 1]
	for j in range(sets_sizes[i] - 1):
		if smallest_number_current < sets[i][sets_sizes[i] - 2 - j]:
			bad_days_current = bad_days_current + 1
		else:
			smallest_number_current = sets[i][sets_sizes[i] - 2 - j]
	bad_days.append(bad_days_current)
for i in bad_days:
	print(i)


t = input()
t = int(t)
while t > 0:
	t = t - 1
	n = input()
	s = input()
	s = s + ' '
	num = []
	temp = 0
	for i in s:
		if i != ' ':
			temp = temp * 10 + int(i)
		else:
			num.append(temp)
			temp = 0
	num.reverse()
	rlt = 0
	mins = num[0]
	for i in num:
		if i > mins:
			rlt += 1
		if i < mins:
			mins = i
	print(rlt)


Q = int(input())
q = 0
while q < Q:
	somma = 0
	N = int(input())
	L = list(map(int, input().split()))
	x = len(L) - 1
	minimo = L[x]
	while x != -1:
		if L[x] < minimo:
			minimo = L[x]
		if L[x] > minimo:
			somma += 1
		x -= 1
	q += 1
	print(somma)


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
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):
	maxint = 10 ** 10
	max_so_far = -maxint - 1
	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 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()
	stack = [0]
	ans = 0
	for i in arr:
		while stack[-1] > i:
			stack.pop()
			ans += 1
		stack.append(i)
	print(ans)
t = 1
t = int(input())
for _ in range(t):
	solve()


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


t = int(input())
for _ in range(t):
	n = int(input())
	data = [0] + list(map(int, input().strip().split()))
	ans = 0
	mn = data[n]
	for i in range(n, 0, -1):
		if data[i] > mn:
			ans += 1
		else:
			mn = data[i]
	print(ans)


t = int(input())
for _ in range(t):
	n = int(input())
	A = [int(x) for x in input().split()]
	if n == 1:
		print(0)
		continue
	A = A[::-1]
	curMin = 10 ** 6 + 1
	cnt = 0
	for i in range(n):
		if A[i] > curMin:
			cnt += 1
		if curMin > A[i]:
			curMin = A[i]
	print(cnt)


t = int(input())
for _ in range(t):
	n = int(input())
	ls = list(map(int, input().split()))
	ds = dict()
	for i in range(len(ls)):
		ds[i] = ls[i]
	import operator
	sorted_d = sorted(ds.items(), key=operator.itemgetter(1))
	res = 0
	mx = 0
	for i in sorted_d:
		if mx > i[0]:
			res += 1
		if mx < i[0]:
			mx = i[0]
	print(res)


for qw in range(int(input())):
	N = int(input())
	A = [int(i) for i in input().split()]
	mn = 10 ** 9
	sm = 0
	for i in range(len(A) - 1, -1, -1):
		mn = min(mn, A[i])
		if A[i] > mn:
			sm += 1
	print(sm)


from collections import defaultdict as DD
from bisect import bisect_left as BL
from bisect import bisect_right as BR
from itertools import combinations as IC
from itertools import permutations as IP
from random import randint as RI
import sys
MOD = pow(10, 9) + 7

def IN(f=0):
	if f == 0:
		return [int(i) for i in sys.stdin.readline().split()]
	else:
		return int(sys.stdin.readline())
tc = int(input())
for _ in range(tc):
	n = IN(1)
	a = IN()
	b = [i for i in a]
	m = -1
	b[-1] = a[-1]
	for i in range(n - 2, -1, -1):
		b[i] = min(a[i], b[i + 1])
	ans = 0
	for i in range(n):
		if a[i] > b[i]:
			ans += 1
	print(ans)


I = input
pr = print
for _ in range(int(I())):
	ar = [0] * int(I())
	ar.append(1000000.0)
	arr = list(map(int, I().split()))
	for i in range(len(arr) - 1, -1, -1):
		ar[i] = min(ar[i + 1], arr[i])
	pr(sum((ar[i] < arr[i] for i in range(len(arr)))))


t = int(input())
for _ in range(t):
	n = int(input())
	A = list(map(int, input().split()))[::-1]
	min = 1000000000.0
	q = 0
	for a in A:
		if a < min:
			min = a
		elif a > min:
			q += 1
	print(q)


t = int(input())
for i in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	mini = l[n - 1]
	count = 0
	for j in range(n - 2, -1, -1):
		if l[j] > mini:
			count += 1
		else:
			mini = l[j]
	print(count)


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	ans = 0
	mn = 1000001
	for i in range(n - 1, -1, -1):
		if arr[i] > mn:
			ans += 1
		mn = min(mn, arr[i])
	print(ans)


def get_bad_days(a):
	if not a:
		return 0
	cntr = 0
	a.reverse()
	cm = a[0]
	for i in a:
		if i < cm:
			cm = i
		elif i > cm:
			cntr += 1
	return cntr
for _ in range(int(input())):
	input()
	print(get_bad_days(list(map(int, input().split()))))


from sys import stdin
from collections import defaultdict

def ip():
	return [int(i) for i in stdin.readline().split()]

def sp():
	return [str(i) for i in stdin.readline().split()]

def pp(A):
	for i in A:
		print(i)

def solve():
	n = int(input())
	A = ip()
	cnt = 0
	d = {}
	d[A[0]] = 1
	for i in range(1, n):
		if A[i - 1] > A[i]:
			k = {}
			for j in d:
				if j > A[i]:
					cnt += d[j]
				else:
					k[j] = d[j]
			d = k
		if A[i] not in d:
			d[A[i]] = 1
		else:
			d[A[i]] += 1
	return cnt
t = int(input())
for _ in range(t):
	print(solve())


import math
tst = int(input())
for i in range(tst):
	n = int(input())
	a = list(map(int, input().split()))
	a.reverse()
	mn = a[0] + 100
	cnt = 0
	for c in a:
		if mn < c:
			cnt += 1
		mn = min(mn, c)
	print(cnt)


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	m = 10 ** 6
	bad_days = 0
	for num in l[::-1]:
		if num > m:
			bad_days += 1
		m = min(m, num)
	print(bad_days)


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


t = int(input())
for i in range(0, t):
	stevila = input()
	lista = list(map(int, input().split()))
	st = 0
	minval = lista[-1]
	for j in range(len(lista) - 1, -1, -1):
		if lista[j] > minval:
			st += 1
		else:
			minval = lista[j]
	print(st)


t = int(input())
for _ in range(t):
	n = int(input())
	A = [int(x) for x in input().split()]
	a = 0
	b = 10 ** 6 + 1
	for i in range(n - 1, -1, -1):
		if b < A[i]:
			a += 1
		b = min([A[i], b])
	print(a)


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	c = 0
	m = float('inf')
	for i in range(n - 1, -1, -1):
		if l[i] > m:
			c += 1
		m = min(m, l[i])
	print(c)


n = int(input())
for i in range(0, n):
	o = int(input())
	T = 0
	p = input().rstrip().split(' ')
	minc = int(p[len(p) - 1])
	for j in range(len(p) - 2, -1, -1):
		if int(p[j]) > minc:
			T += 1
		else:
			minc = min(minc, int(p[j]))
	print(T)


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


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	m = a[-1]
	bad = 0
	for x in a[-1::-1]:
		if m < x:
			bad += 1
		m = min(m, x)
	print(bad)


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


t = int(input())
for i in range(t):
	n = int(input())
	a = [int(x) for x in input().split()]
	sum = 0
	if n == 1:
		print(sum)
	else:
		min = a[n - 1]
		for j in range(n - 2, -1, -1):
			if a[j] > min:
				sum += 1
			else:
				sum += 0
				min = a[j]
		print(sum)


for i in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	m = 9999999
	ans = 0
	i = n - 1
	while i >= 0:
		if a[i] > m:
			ans += 1
		else:
			m = min(a[i], m)
		i -= 1
	print(ans)


t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(i) for i in input().split()]
	a = a[::-1]
	s = []
	ans = 0
	for i in range(n):
		while len(s) > 0 and s[-1] >= a[i]:
			s.pop()
		if s:
			ans += 1
		s.append(a[i])
	print(ans)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	(cnt, mn) = (0, a[-1])
	for i in range(len(a) - 2, -1, -1):
		if a[i] > mn:
			cnt += 1
		else:
			mn = a[i]
	print(cnt)


t = int(input())
for _ in range(t):
	__ = input()
	v = list(map(int, input().split()))
	bad_days = 0
	cur_min = v[-1]
	for i in range(len(v) - 1, -1, -1):
		if v[i] > cur_min:
			bad_days += 1
		else:
			cur_min = v[i]
	print(bad_days)


t = int(input())
while t:
	t -= 1
	n = int(input())
	arr = list(map(int, input().split()))
	menor = 1000000.0 + 5
	ans = 0
	for i in range(n - 1, -1, -1):
		if arr[i] > menor:
			ans += 1
		menor = min(menor, arr[i])
	print(ans)


I0 = lambda : map(int, input().split())
I1 = lambda : int(input())
I2 = lambda : list(map(int, input().split()))
for i in range(I1()):
	n = I1()
	a = I2()
	res = 0
	for j in range(n - 1, 0, -1):
		if a[j] < a[j - 1]:
			(a[j], a[j - 1]) = (a[j - 1], a[j])
			res += 1
	print(res)


import sys
import os
from io import IOBase, BytesIO
import math
import collections
mod = 10 ** 9 + 7
pie = 3.1415926536

def modinv(n, p):
	return pow(n, p - 2, p)

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

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

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

def GCD(x, y):
	while y:
		(x, y) = (y, x % y)
	return x

def read2DIntArray(row, col):
	arr = []
	for i in range(0, row):
		temp = list(map(int, sin().split()))
		arr.append(temp)
	return arr

def read2DCharArray(row, col):
	arr = []
	for i in range(0, row):
		temp = str(sin())
		arr.append(temp)
	return arr

def smallestNumber(n):
	lst = list(str(n))
	lst.sort()
	tmp = ''
	for (i, n) in enumerate(lst):
		if n != '0':
			tmp = lst.pop(i)
			break
	return str(tmp) + ''.join(lst)

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 * 2, n + 1, p):
				prime[i] = False
		p += 1
	prime[0] = False
	prime[1] = False
	lst = []
	for p in range(n + 1):
		if prime[p]:
			lst.append(p)
	return lst

def nCr(n, r):
	a = 1
	b = 1
	c = 1
	for i in range(1, n + 1):
		c *= i
	for i in range(1, r + 1):
		b *= i
	for i in range(1, n - r + 1):
		a *= i
	return c // (a * b)

def sieveOfEratosthenes1(N, s):
	prime = [False] * (N + 1)
	for i in range(2, N + 1, 2):
		s[i] = 2
	for i in range(3, N + 1, 2):
		if prime[i] == False:
			s[i] = i
			for j in range(i, int(N / i) + 1, 2):
				if prime[i * j] == False:
					prime[i * j] = True
					s[i * j] = i

def generatePrimeFactors(N):
	s = [0] * (N + 1)
	sieveOfEratosthenes1(N, s)
	curr = s[N]
	cnt = 1
	factors = []
	power = []
	while N > 1:
		N //= s[N]
		if curr == s[N]:
			cnt += 1
			continue
		factors.append(curr)
		power.append(cnt)
		curr = s[N]
		cnt = 1
	return (factors, power)

def main():
	for _ in range(int(sin())):
		n = int(sin())
		a = ain()
		mn = a[n - 1]
		days = 0
		for i in range(n - 2, -1, -1):
			if a[i] <= mn:
				mn = a[i]
			else:
				days += 1
		print(days)
py2 = round(0.5)
if py2:
	from future_builtins import ascii, filter, hex, map, oct, zip
	range = xrange
BUFSIZE = 8192

class FastIO(BytesIO):
	newlines = 0

	def __init__(self, file):
		self._file = file
		self._fd = file.fileno()
		self.writable = 'x' in file.mode or 'w' in file.mode
		self.write = super(FastIO, self).write if self.writable else None

	def _fill(self):
		s = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
		self.seek((self.tell(), self.seek(0, 2), super(FastIO, self).write(s))[0])
		return s

	def read(self):
		while self._fill():
			pass
		return super(FastIO, self).read()

	def readline(self):
		while self.newlines == 0:
			s = self._fill()
			self.newlines = s.count(b'\n') + (not s)
		self.newlines -= 1
		return super(FastIO, self).readline()

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

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		if py2:
			self.write = self.buffer.write
			self.read = self.buffer.read
			self.readline = self.buffer.readline
		else:
			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()


for k in range(int(input())):
	n = int(input())
	w = list(map(int, input().split()))
	mi = [0] * n
	cnt = 0
	for k in range(n - 1, -1, -1):
		if k == n - 1:
			mi[k] = w[k]
		else:
			mi[k] = min(w[k], mi[k + 1])
	for j in range(n - 1):
		if w[j] > mi[j + 1]:
			cnt += 1
	print(cnt)


n = int(input())
for i in range(n):
	bad_day = 0
	loc_min = int(input())
	loc_min = 10 ** 7
	arr = [int(s) for s in input().split()]
	if len(arr) == 1:
		print(0)
		continue
	for i in range(len(arr) - 1, -1, -1):
		if arr[i] > loc_min:
			bad_day += 1
		loc_min = min(loc_min, arr[i])
	print(bad_day)


for _ in range(int(input())):
	n = int(input())
	li = list(map(int, input().split()))
	if n == 1:
		print(0)
	else:
		res = [li[0]]
		ans = 0
		for i in range(1, n):
			if res != []:
				if li[i] >= res[-1]:
					res.append(li[i])
				else:
					while 1:
						if res != []:
							if res[-1] > li[i]:
								res.pop()
								ans += 1
							else:
								res.append(li[i])
								break
						else:
							res.append(li[i])
							break
			else:
				res.append(li[i])
		print(ans)


t = int(input())
for _ in range(t):
	n = int(input())
	days = list(map(int, input().split()))[::-1]
	m = days[0]
	c = 0
	for i in days[1:]:
		c += i > m
		m = min(m, i)
	print(c)


t = int(input())
while t > 0:
	n = int(input())
	a = [int(ele) for ele in input().split()]
	count = 0
	i = n - 1
	while i >= 0:
		if i == n - 1:
			mini = a[n - 1]
		elif a[i] > mini:
			count += 1
		else:
			mini = a[i]
		i -= 1
	print(count)
	t -= 1


t = int(input())
for _ in range(t):
	n = int(input())
	lst = list(map(int, input().split()))
	arr = [0] * n
	c = 0
	mini = lst[n - 1]
	arr[n - 1] = mini
	for i in range(n - 2, -1, -1):
		if mini > lst[i]:
			mini = lst[i]
		arr[i] = mini
	for i in range(n):
		if lst[i] > arr[i]:
			c += 1
	print(c)


import sys

def main():

	def input():
		return sys.stdin.readline()[:-1]
	t = int(input())
	for k in range(t):
		n = int(input())
		a = list(map(int, input().split()))
		a = a[::-1]
		m = a[0]
		ans = 0
		for k in range(1, n):
			if a[k] > m:
				ans += 1
			elif a[k] < m:
				m = a[k]
		print(ans)
main()


import sys
import math
t = int(input())
for i in range(t):
	n = int(input())
	prices = list(map(int, input().split()))
	prices.reverse()
	length = len(prices)
	cnt = 0
	for i in range(1, length, 1):
		if prices[i] > prices[i - 1]:
			cnt += 1
		prices[i] = min(prices[i], prices[i - 1])
	print(cnt)
pass


from collections import defaultdict as df
from sys import stdin, stdout
import heapq
import bisect

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

def inti():
	return int(stdin.readline())
t = inti()
for iii in range(t):
	n = inti()
	a = l()
	b = [0] * n
	b[n - 1] = a[n - 1]
	for i in range(n - 2, -1, -1):
		b[i] = min(a[i], b[i + 1])
	count = 0
	for i in range(n):
		if b[i] < a[i]:
			count += 1
	print(count)


for _ in ' ' * int(input()):
	n = int(input())
	a = list(map(int, input().split()))
	cnt = 0
	ind = n - 1
	for i in range(n - 1, -1, -1):
		if a[ind] < a[i]:
			cnt += 1
		else:
			ind = i
	print(cnt)


import sys
input = sys.stdin.readline
qq = int(input())
for _ in range(qq):
	n = int(input())
	l = list(map(int, input().split()))
	l.reverse()
	bad = 0
	curmin = l[0]
	for x in l[1:]:
		if x > curmin:
			bad += 1
		curmin = min(curmin, x)
	print(bad)


t = int(input())
while t:
	t -= 1
	n = int(input())
	b = 0
	a = [int(i) for i in input().split()]
	for i in range(n - 1, 0, -1):
		if a[i - 1] > a[i]:
			b += 1
			(a[i], a[i - 1]) = (a[i - 1], a[i])
	print(b)


for i in range(int(input())):
	(n, l) = (int(input()), list(map(int, input().split()))[::-1])
	(x, r) = (l[0], 0)
	for i in l:
		r += i > x
		x = min(x, i)
	print(r)


from sys import stdin

def solve(n, arr):
	(k, ctr) = (9999999999, 0)
	for j in range(n):
		k = min(k, arr[n - j - 1])
		if k < arr[n - j - 1]:
			ctr += 1
	return ctr
t = int(stdin.readline().rstrip())
for i in range(t):
	n = int(stdin.readline().rstrip())
	arr = [int(x) for x in stdin.readline().rstrip().split()]
	print(solve(n, arr))


t = int(input())
for _ in range(0, t):
	n = int(input())
	last = -1
	a = list(map(int, input().split()))
	count = 0
	temp = 1
	minn = a[0]
	now = n - 1
	mostl = 0
	for i in range(n - 2, -1, -1):
		if a[now] >= a[i]:
			now = i
		else:
			count += 1
	print(count)


from collections import deque
t = int(input())
for i in range(t):
	n = int(input())
	sp = list(map(int, input().split()))
	indec = [0] * len(sp)
	indec[len(sp) - 1] = len(sp) - 1
	D = deque()
	D.append((sp[len(sp) - 1], len(sp) - 1))
	for i in range(len(sp) - 1, -1, -1):
		a = D.pop()
		k = False
		while a[0] >= sp[i]:
			if len(D) == 0:
				D.append((sp[i], i))
				indec[i] = i
				k = True
				break
			a = D.pop()
		if k != True:
			D.append(a)
			indec[i] = a[1]
	kol = 0
	for i in range(len(indec)):
		if indec[i] != i:
			kol += 1
	print(kol)


t = int(input())
for i in range(t):
	brojlosih = 0
	n = int(input())
	a = list(map(int, input().split()))
	mini = float('inf')
	for j in range(len(a) - 1, -1, -1):
		if a[j] > mini:
			brojlosih += 1
		mini = min(mini, a[j])
	print(brojlosih)


t = int(input())
for i in range(t):
	_ = int(input())
	a = list(map(int, input().split()))
	bad_days_count = 0
	min_next_value = a[-1]
	for (_, ai) in enumerate(a[::-1], start=1):
		if ai > min_next_value:
			bad_days_count += 1
		else:
			min_next_value = ai
	print(bad_days_count)


n = int(input())
for i in range(n):
	sum = 0
	t = int(input())
	l = list(input().split(' '))
	a = int(l[len(l) - 1])
	for i in l[::-1]:
		if a >= int(i):
			a = int(i)
		elif a < int(i):
			sum += 1
	print(sum)


def mi():
	return list(map(int, input().split()))
for _ in range(int(input())):
	n = int(input())
	a = list(mi())[::-1]
	m = a[0]
	ans = 0
	for i in range(n):
		if a[i] > m:
			ans += 1
		elif a[i] < m:
			m = a[i]
	print(ans)


for _ in range(int(input())):
	n = int(input())
	a = [int(i) for i in input().split()]
	cnt = 0
	min = a[n - 1]
	for i in range(n - 2, -1, -1):
		if a[i] <= min:
			min = a[i]
		else:
			cnt += 1
	print(cnt)


from sys import stdin
t = int(stdin.readline().strip())
for j in range(t):
	n = int(stdin.readline().strip())
	s = list(map(int, stdin.readline().strip().split()))
	mn = s[-1]
	ans = 0
	for i in range(n - 2, -1, -1):
		if mn < s[i]:
			ans += 1
		mn = min(mn, s[i])
	print(ans)


t = int(input())
for i in range(t):
	n = int(input())
	preco = list(map(int, input().split()))
	barato = preco[-1]
	cont = 0
	for i in range(n - 1, -1, -1):
		if preco[i] > barato:
			cont += 1
		else:
			barato = preco[i]
	print(cont)


t = int(input())
for i in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	final = [0] * n
	final[-1] = arr[-1]
	minimum = final[-1]
	count = 0
	for i in range(n - 2, -1, -1):
		minimum = min(minimum, arr[i])
		final[i] = minimum
		if arr[i] > final[i]:
			count += 1
	print(count)


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


from sys import stdin

def get_ints():
	return list(map(int, stdin.readline().strip().split()))
for _ in range(int(input())):
	n = int(input())
	ar = get_ints()[::-1]
	if n == 1:
		print(0)
		continue
	minso = ar[0]
	c = 0
	for i in ar[1:]:
		if i > minso:
			c += 1
		if i < minso:
			minso = i
	print(c)


t = int(input())
for i in range(t):
	n = int(input())
	n = [int(x) for x in input().split()]
	i = len(n) - 1
	min = 1000000
	ans = 0
	while i >= 0:
		if n[i] < min:
			min = n[i]
		elif n[i] > min:
			ans += 1
		i -= 1
	print(ans)


T = int(input())
t = 0
while t < T:
	X = 0
	N = int(input())
	L = list(map(int, input().split()))
	x = len(L) - 1
	n = L[x]
	while x != -1:
		if L[x] < n:
			n = L[x]
		if L[x] > n:
			X = X + 1
		x = x - 1
	t = t + 1
	print(X)


def main():
	n = int(input())
	for i in range(n):
		n_prices = int(input())
		prices = list(map(int, input().split()))[::-1]
		min_ = prices[0]
		res = 0
		for i in range(n_prices):
			if prices[i] > min_:
				res += 1
			if min_ > prices[i]:
				min_ = prices[i]
		print(res)
main()


for _ in range(int(input())):
	x = int(input())
	s = list(map(int, input().split()))
	s.reverse()
	res = 0
	cmp = s[0]
	for n in range(1, x):
		if s[n] > cmp:
			res += 1
		else:
			cmp = s[n]
	print(res)


from sys import stdin, stdout
from collections import defaultdict
from collections import deque
import math
import copy
T = int(input())
for i in range(T):
	N = int(input())
	arr = [int(x) for x in stdin.readline().split()]
	mini = [0] * N
	m = arr[-1]
	for i in range(N - 1, -1, -1):
		m = min(m, arr[i])
		mini[i] = m
	res = 0
	for i in range(N - 1):
		if arr[i] > mini[i + 1]:
			res += 1
	print(res)


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

def IS():
	return input()

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

def IM():
	return map(int, input().split())
t = I()
for i in range(t):
	n = I()
	l = IL()
	cnt = 0
	maxleft = [0] * n
	maxleft[n - 1] = l[n - 1]
	for i in range(n - 2, -1, -1):
		maxleft[i] = min(maxleft[i + 1], l[i])
	for i in range(n - 1):
		if maxleft[i] < l[i]:
			cnt += 1
	print(cnt)


R = lambda : map(int, input().split())
for _ in range(int(input())):
	n = int(input())
	L = list(R())
	c = 0
	mi = L[n - 1]
	for i in reversed(range(n - 1)):
		if mi < L[i]:
			c += 1
		else:
			mi = L[i]
	print(c)


import math as mt

def printPrevSmaller(arr, n):
	S = list()
	k = list()
	for i in range(n):
		while len(S) > 0 and S[-1] >= arr[i]:
			S.pop()
		if len(S) == 0:
			a = 1
		else:
			k.append(arr[i])
		S.append(arr[i])
	return len(k)
testcase = int(input())
for i in range(testcase):
	n = int(input())
	arr = list(map(int, input().split()))
	arr.reverse()
	ans = printPrevSmaller(arr, n)
	print(ans)


t = int(input())
for q in range(t):
	n = int(input())
	lis = list(map(int, input().split()))
	mini = lis[len(lis) - 1]
	count = 0
	for i in range(n - 1, -1, -1):
		mini = min(lis[i], mini)
		if lis[i] > mini:
			count += 1
	print(count)


from math import *
import time
t = int(input())
for i in range(t):
	n = int(input())
	l = input().split(' ')
	c = 0
	minima = 100000000
	for j in range(n):
		val = int(l[n - 1 - j])
		if val > minima:
			c = c + 1
		if minima > val:
			minima = val
	print(c)


class B:

	def __init__(self):
		self.arr = []

	def bin_insert(self, v):
		if self.arr:
			e = len(self.arr)
			if self.arr[e - 1] <= v:
				self.arr.append(v)
				return 0
			if self.arr[0] > v:
				self.arr = [v]
				return e
			s = 0
			en = e - 1
			while s + 1 != en:
				m = (s + en) // 2
				if self.arr[m] > v:
					en = m
				else:
					s = m
			self.arr.insert(en, v)
			self.arr = self.arr[:en + 1]
			return e - en
		else:
			self.arr.append(v)
			return 0
for _ in range(int(input())):
	n = int(input())
	a = [int(x) for x in input().split()]
	f = 0
	s = 0
	ans = 0
	b = B()
	for i in range(n):
		tmp = b.bin_insert(a[i])
		ans += tmp
	print(ans)


counter = 0
numoftest = input()
numoftest = int(numoftest)
while counter < numoftest:
	numofnum = input()
	numofnum = int(numofnum)
	numbers = input()
	numbers = numbers.split()
	counter3 = 0
	while numofnum > counter3:
		numbers[counter3] = int(numbers[counter3])
		counter3 = counter3 + 1
	smallestnumber = numbers[numofnum - 1]
	GT = 0
	counter2 = numofnum - 1
	for number in numbers:
		number = numbers[counter2]
		if number <= smallestnumber:
			smallestnumber = number
		else:
			GT = GT + 1
		counter2 = counter2 - 1
	print(GT)
	counter = counter + 1


for i in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	c = 0
	r = []
	r.append(arr[-1])
	k = arr[-1]
	for i in range(n - 2, -1, -1):
		k = min(k, arr[i])
		r.append(k)
	for i in range(n):
		if r[i] < arr[n - 1 - i]:
			c = c + 1
	print(c)


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


for _ in range(int(input())):
	n = int(input())
	li = list(map(int, input().split()))
	ans = 0
	ma = li[-1]
	for i in li[::-1]:
		if i > ma:
			ans += 1
		else:
			ma = min(ma, i)
	print(ans)


author = 'DarshanMohan'
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():
	return list(MAP())

def Yes():
	print('Yes')

def No():
	print('No')

def YES():
	print('YES')

def NO():
	print('NO')
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

def add(a, b, c):
	res = a + b
	if res >= c:
		return res - c
	else:
		return res

def mod(a, b, c):
	res = a * b
	if res >= c:
		return res % c
	else:
		return res

def gcd(a, b):
	while b:
		(a, b) = (b, a % b)
	return a

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

def expo(a, b):
	(x, y) = (1, a)
	while b > 0:
		if b & 1:
			x = x * y
		y = y * y
		b >>= 1
	return x
for _ in range(int(input())):
	n = INT()
	a = LIST()
	count = 0
	mini = a[n - 1]
	for i in range(n - 1, -1, -1):
		if a[i] > mini:
			count += 1
		mini = min(mini, a[i])
	print(count)


import sys
from collections import Counter

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 ** 18
MOD = 10 ** 9 + 7
for _ in range(INT()):
	N = INT()
	A = LIST()
	mn = A[-1]
	cnt = 0
	for a in A[::-1][1:]:
		if a > mn:
			cnt += 1
		elif a < mn:
			mn = a
	print(cnt)


from math import *
from sys import *
from collections import defaultdict
M = 10 ** 9 + 7

def pow(a, b):
	res = 1
	while b > 0:
		if b & 1:
			res *= a
		a *= a
		b >>= 1
	return res

def powmod(a, b, m):
	res = 1
	while b > 0:
		if b & 1:
			res = res * a % m
		a *= a
		b >>= 1
	return res

def inv(a, m):
	return powmod(a, m - 2, m)

def factor(n):
	potentional_p = 3
	itog_list = {}
	if n % 2 == 0:
		itog_list[2] = 0
	while n % 2 == 0:
		n = n // 2
		itog_list[2] += 1
	while n - 1:
		if potentional_p > n ** 0.5:
			if n in itog_list:
				itog_list[n] += 1
			else:
				itog_list[n] = 1
			return itog_list
		while n % potentional_p == 0:
			n = n // potentional_p
			if potentional_p in itog_list:
				itog_list[potentional_p] += 1
			else:
				itog_list[potentional_p] = 1
		potentional_p += 2
	return itog_list
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, stdin.readline().split()))
	mn = a[-1]
	a = a[:-1]
	res = 0
	for x in a[::-1]:
		if mn < x:
			res += 1
		mn = min(mn, x)
	print(res)


import sys
import collections
import functools
t = int(input().strip())
for i in range(t):
	n = int(input().strip())
	data = list(map(int, input().strip().split()))
	(curmin, ans) = (data[n - 1], 0)
	for j in range(n - 2, -1, -1):
		if data[j] > curmin:
			ans += 1
		else:
			curmin = data[j]
	print(str(ans) + '\r')


from sys import stdin, stdout
t = int(stdin.readline().rstrip())
while t > 0:
	n = int(stdin.readline().rstrip())
	l = list(map(int, stdin.readline().split()))
	if n == 1:
		stdout.write(str(0) + '\n')
	else:
		c = 0
		m = l[n - 1]
		for i in range(n - 2, -1, -1):
			if l[i] > m:
				c += 1
			m = min(m, l[i])
		stdout.write(str(c) + '\n')
	t -= 1


N = int(input())
for i in range(N):
	A = int(input())
	B = list(map(int, input().split()))
	B.reverse()
	ans = 0
	Min = B[0]
	for i in range(A):
		if B[i] <= Min:
			Min = B[i]
		if Min < B[i]:
			ans = ans + 1
	print(ans)


k = int(input())
for i in range(k):
	count = 0
	t = int(input())
	l = [int(x) for x in input().split(' ')]
	if len(l) == 1:
		print('0')
	else:
		k = len(l)
		min = l[k - 1]
		i = k - 1
		while i > 0:
			if l[i - 1] > min:
				count = count + 1
			elif l[i - 1] < min:
				min = l[i - 1]
			else:
				pass
			i = i - 1
		print(count)


t = int(input())
answers = []
for ti in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	a.reverse()
	mn = a[0]
	bad = 0
	for i in range(len(a)):
		if a[i] <= mn:
			mn = a[i]
		else:
			bad += 1
	answers.append(bad)
for a in answers:
	print(a)


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	m = a[-1]
	res = 0
	for i in range(n - 1, -1, -1):
		if a[i] > m:
			res += 1
		m = min(m, a[i])
	print(res)


def bad_days():
	min_num = arr[-1]
	ans = 0
	for i in range(n - 2, -1, -1):
		if arr[i] > min_num:
			ans += 1
		if arr[i] < min_num:
			min_num = arr[i]
	return ans
t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	print(bad_days())


n = int(input())
for i in range(n):
	c = 0
	input()
	x = input().split()
	mini = int(x[len(x) - 1])
	for j in range(len(x) - 1, -1, -1):
		if int(x[j]) > mini:
			c = c + 1
		elif int(x[j]) < mini:
			mini = int(x[j])
	print(c)


t = int(input())
while t > 0:
	n = int(input())
	ans = 0
	a = list(map(int, input().split()))
	final = list()
	for i in range(n):
		final.append(0)
	final[-1] = a[-1]
	minimum = a[-1]
	for i in range(n - 2, -1, -1):
		if a[i] < minimum:
			final[i] = a[i]
			minimum = final[i]
		else:
			final[i] = minimum
	for i in range(n):
		if a[i] > final[i]:
			ans += 1
	print(ans)
	t -= 1


for i in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	(c, m) = (0, a[-1])
	for j in range(n - 2, -1, -1):
		if a[j] > m:
			c += 1
		m = min(m, a[j])
	print(c)


def check(n, lst):
	if n == 1:
		return 0
	(count, last) = (0, lst[-1])
	for i in range(n - 2, -1, -1):
		if lst[i] > last:
			count += 1
		else:
			last = lst[i]
	return count
t = int(input())
for j in range(t):
	m = int(input())
	a = [int(x) for x in input().split()]
	print(check(m, a))


def badPrices(n, days):
	smallest = days[-1]
	count = 0
	for i in range(n - 2, -1, -1):
		if days[i] > smallest:
			count += 1
		else:
			smallest = days[i]
	return count
t = int(input())
for i in range(t):
	n = int(input())
	days = list(map(int, input().split()))
	print(badPrices(n, days))


t = int(input())
while t:
	t -= 1
	n = int(input())
	arr = list(map(int, input().split()))
	minArr = [0] * n
	for i in range(n - 1, -1, -1):
		if i == n - 1:
			minArr[i] = arr[i]
		else:
			minArr[i] = min(minArr[i + 1], arr[i])
	count = 0
	for i in range(0, n - 1):
		if arr[i] > minArr[i + 1]:
			count += 1
	print(count)


t = int(input())
for j in range(t):
	n = int(input())
	count = 0
	a = list(map(int, input().split()))
	m = max(a)
	for i in range(len(a) - 1, -1, -1):
		if a[i] <= m:
			m = a[i]
			continue
		else:
			count += 1
	print(count)


t = int(input())
while t > 0:
	n = int(input())
	l = [int(i) for i in input().split()]
	c = 0
	min1 = l[n - 1]
	for i in range(n - 2, -1, -1):
		min1 = min(min1, l[i])
		if l[i] > min1:
			c = c + 1
	print(c)
	t -= 1


t = int(input())
for i in range(t):
	n = input()
	arr = input()
	arr = [int(a) for a in arr.split(' ')]
	min_price = arr[len(arr) - 1]
	count = 0
	if len(arr) > 1:
		for a in arr[::-1][1:]:
			if min_price < a:
				count += 1
			else:
				min_price = a
	print(count)


import sys
T = int(input())
for t in range(T):
	n = int(input())
	l = list(map(int, input().split()))
	ans = 0
	mini = 10 ** 7
	for i in range(n - 1, -1, -1):
		mini = min(mini, l[i])
		if mini != l[i]:
			ans += 1
	print(ans)


for _ in range(int(input())):
	(n, a) = (int(input()), reversed(list(map(int, input().split()))))
	cnt = 0
	mina = next(a)
	for k in a:
		if k > mina:
			cnt += 1
		else:
			mina = k
	print(cnt)


cases = int(input())
for i in range(cases):
	tam = int(input())
	days = list(map(int, input().split()))
	bad = 0
	smallest = days[-1]
	for i in range(tam - 2, -1, -1):
		if days[i] > smallest:
			bad += 1
		else:
			smallest = days[i]
	print(bad)


testCases = int(input())
for i in range(testCases):
	dataLength = int(input())
	prices = list(map(int, input().split()))
	badDays = 0
	futurePrice = prices[-1]
	for i in range(dataLength - 2, -1, -1):
		if prices[i] > futurePrice:
			badDays += 1
			continue
		else:
			futurePrice = prices[i]
	print(badDays)


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


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	m = a[-1]
	i = n - 2
	j = 0
	while i + 1:
		j += a[i] > m
		m = min(m, a[i])
		i -= 1
	print(j)


def solve(arr, n, ans):
	dp = []
	for i in range(n - 1, -1, -1):
		if not dp:
			dp.append(arr[i])
		else:
			dp.append(min(dp[-1], arr[i]))
	dp.reverse()
	bad_days = 0
	for i in range(n - 1):
		if dp[i + 1] < arr[i]:
			bad_days += 1
	ans.append(bad_days)

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


t = int(input())
for iter1 in range(t):
	n = int(input())
	N = list(map(int, input().split()))
	N.reverse()
	mini = 2 ** 31 - 1
	ans = 0
	for i in N:
		if i > mini:
			ans += 1
		mini = min(mini, i)
	print(ans)


t = int(input())
for i in range(t):
	n = int(input())
	a = [int(x) for x in input().split()]
	arr = 0
	counter = 10 ** 10
	for i in range(n - 1, -1, -1):
		if a[i] > counter:
			arr += 1
		counter = min(counter, a[i])
	print(arr)


n = int(input())
for i in range(n):
	tam = int(input())
	numeros = list(map(int, input().split()))
	glo = numeros[tam - 1]
	count = 0
	for j in range(tam - 2, -1, -1):
		if numeros[j] > glo:
			count += 1
		glo = min(glo, numeros[j])
	print(count)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	ans = 0
	m = float('inf')
	for x in reversed(a):
		if x > m:
			ans += 1
		else:
			m = min(x, m)
	print(ans)


t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(i) for i in input().split()]
	min1 = -1
	c = 0
	for i in range(len(a) - 1, -1, -1):
		if min1 == -1:
			min1 = a[i]
		else:
			min1 = min(min1, a[i])
		if min1 < a[i]:
			c += 1
	print(c)


import math
import sys
import collections

def getdict(n):
	d = {}
	if type(n) is list:
		for i in n:
			if i in d:
				d[i] += 1
			else:
				d[i] = 1
	else:
		for i in range(n):
			t = ii()
			if t in d:
				d[t] += 1
			else:
				d[t] = 1
	return d

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

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

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

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

def lcm(a, b):
	return abs(a * b) // math.gcd(a, b)
t = ii()
for i in range(t):
	n = ii()
	d = li()
	cmin = d[n - 1]
	r = 0
	for j in range(n - 2, -1, -1):
		if d[j] > cmin:
			r += 1
		else:
			cmin = d[j]
	print(r)


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


import sys
t = int(sys.stdin.readline())
for _ in range(t):
	n = int(sys.stdin.readline())
	a = list(map(int, sys.stdin.readline().split()))
	minVal = a[-1]
	count = 0
	for i in range(n - 2, -1, -1):
		if a[i] > minVal:
			count += 1
		else:
			minVal = a[i]
	print(count)


t = int(input())
for i in range(t):
	n = int(input())
	prices = reversed(list(map(int, input().split())))
	current_min = 10 ** 6
	ans = 0
	for (i, price) in enumerate(prices):
		if current_min < price:
			ans += 1
		elif price < current_min:
			current_min = price
	print(ans)


t = int(input())
while t > 0:
	n = int(input())
	arr = list(map(int, input().split()))
	arr.reverse()
	mini = 9999999999999999999
	count = 0
	for i in arr:
		if i > mini:
			count += 1
		else:
			mini = i
	print(count)
	t -= 1


for _ in range(int(input())):
	n = int(input())
	s = list(map(int, input().split()))
	ans = 0
	kk = []
	mn = s[n - 1]
	for i in range(len(s) - 1, -1, -1):
		mn = min(mn, s[i])
		kk.append(mn)
	kk.reverse()
	for i in range(len(s) - 1):
		if s[i] > kk[i]:
			ans += 1
	print(ans)


casos = int(input())
for i in range(casos):
	n = int(input())
	lista = [int(x) for x in input().split()]
	menor = lista[-1]
	count = 0
	if len(lista) == 1:
		print(0)
		continue
	elif len(lista) == 2:
		if lista[0] > lista[1]:
			print(1)
		else:
			print(0)
		continue
	for i in range(len(lista) - 2, -1, -1):
		if menor >= lista[i]:
			menor = lista[i]
		else:
			count += 1
	print(count)


def get_bad_prices(arr):
	length = len(arr)
	current_min = arr[-1]
	n_bad_prices = 0
	for i in range(length - 2, -1, -1):
		if arr[i] > current_min:
			n_bad_prices += 1
		if arr[i] < current_min:
			current_min = arr[i]
	return n_bad_prices
t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	print(get_bad_prices(a))


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


for i in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	mini = max(l)
	count = 0
	for i in range(n - 1, -1, -1):
		if l[i] > mini:
			count += 1
		mini = min(mini, l[i])
	print(count)


t = int(input())
for test in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	mn = [a[-1]]
	j = 0
	for i in range(n - 1, 0, -1):
		curmn = mn[j]
		cand = a[i]
		if cand < curmn:
			mn.append(cand)
		else:
			mn.append(curmn)
		j += 1
	days = 0
	for i in range(n):
		if a[n - i - 1] > mn[i]:
			days += 1
	print(days)


for i in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	count = 0
	minn = 9000000000.0
	for i in range(n - 1, -1, -1):
		if arr[i] > minn:
			count += 1
		minn = min(arr[i], minn)
	print(count)


for _ in range(int(input())):
	n = int(input())
	arr = map(int, input().split())
	stack = [-1]
	for x in arr:
		if x < stack[-1]:
			while x < stack[-1]:
				stack.pop()
		stack.append(x)
	print(n - len(stack) + 1)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	a = list(reversed(a))
	i = 0
	count = 0
	try:
		while True:
			if a[i] < a[i + 1]:
				num = a[i]
				while True:
					i += 1
					if a[i] <= num:
						break
					count += 1
			else:
				i += 1
	except IndexError:
		print(count)


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	vis = [-1] * n
	for i in range(n - 1, -1, -1):
		if i == n - 1:
			vis[i] = arr[i]
		elif arr[i] < vis[i + 1]:
			vis[i] = arr[i]
		else:
			vis[i] = vis[i + 1]
	count = 0
	for i in range(n):
		if arr[i] != vis[i]:
			count += 1
	print(count)


T = int(input())
for i in range(T):
	len = int(input())
	fuck = list(map(int, input().split()))
	min = 10000000
	ans = 0
	for i in fuck[::-1]:
		if min >= i:
			min = i
		else:
			ans += 1
	print(ans)


for Test in range(int(input())):
	N = int(input())
	X = list(map(int, input().split()))
	(Count, Min) = (0, X[-1])
	for i in range(N - 1, -1, -1):
		if Min < X[i]:
			Count += 1
		else:
			Min = X[i]
	print(Count)


for i in range(int(input())):
	n = int(input())
	a = [int(i) for i in input().split()]
	notbad = [a[0]]
	bad = 0
	for j in range(1, n):
		if a[j] > notbad[-1]:
			notbad.append(a[j])
		else:
			f = len(notbad) - 1
			while f > -1 and a[j] < notbad[f]:
				f -= 1
				bad += 1
			notbad = notbad[:f + 1] + [a[j]]
	print(bad)


t = int(input())
O = [0] * t
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	S = 0
	x = a[n - 1]
	for j in range(n - 1, 0, -1):
		if a[j - 1] > x:
			S += 1
		elif a[j - 1] < x:
			x = a[j - 1]
	O[i] = S
for i in range(t):
	print(O[i])


t = int(input())
for i in range(t):
	n = int(input())
	nums = list(map(int, input().split()))
	min = nums[-1]
	count = 0
	for j in range(1, n):
		if nums[n - 1 - j] <= min:
			min = nums[n - 1 - j]
		else:
			count += 1
	print(count)


def solve(li):
	count = 0
	flag = li[-1]
	for i in range(len(li) - 1, -1, -1):
		if li[i] <= flag:
			flag = li[i]
		else:
			count += 1
	return count
t = int(input())
for _ in range(t):
	n = int(input())
	li = list(map(int, input().split()))
	result = solve(li)
	print(result)


n = int(input())
for x in range(n):
	b = int(input())
	c = list(map(int, input().split()))
	d = c[::-1]
	a = 0
	m = d[0]
	for i in range(b):
		if m < d[i]:
			a += 1
		else:
			m = min(m, d[i])
	print(a)


for t in range(int(input())):
	(n, arr) = (input(), list(map(int, input().split()))[::-1])
	(m, res) = (arr[0], 0)
	for i in arr:
		if m >= i:
			m = i
		else:
			res += 1
	print(res)


t = int(input())
for i in range(t):
	n = int(input())
	A = [int(x) for x in input().strip().split()]
	A.reverse()
	s = 0
	m = A[0]
	for j in range(1, n):
		if A[j] > m:
			s += 1
		m = min(A[j], m)
	print(s)


n = int(input())
for i in range(n):
	a = int(input())
	ls = list(map(int, input().split()))
	mn = ls[len(ls) - 1]
	s = 0
	for j in range(len(ls) - 2, -1, -1):
		if ls[j] < mn:
			mn = ls[j]
		elif ls[j] > mn:
			s = s + 1
	print(s)


def fun(arr, n):
	count = 0
	min = arr[-1]
	for i in range(n - 2, -1, -1):
		if arr[i] > min:
			count += 1
		elif arr[i] < min:
			min = arr[i]
	return count
n = int(input())
ans = []
for i in range(n):
	size = int(input())
	arr = [int(i) for i in input().split()]
	ans.append(fun(arr, size))
for a in ans:
	print(a)


t = int(input())
for i in range(t):
	n = int(input())
	br = list(map(int, input().split()))
	c = 0
	for i in range(n - 2, -1, -1):
		if br[i] > min(br[i], br[i + 1]):
			c += 1
		br[i] = min(br[i], br[i + 1])
	print(c)


import sys

def main():
	t = int(sys.stdin.readline())
	for i in range(t):
		n = int(sys.stdin.readline())
		nums = [int(x) for x in sys.stdin.readline().split(' ')]
		nums.reverse()
		count = 0
		minimum = nums[0]
		for num in nums[1:]:
			if num > minimum:
				count += 1
			minimum = min(num, minimum)
		print(count)
main()


t = int(input())
for I in range(t):
	n = int(input())
	a = [int(i) for i in input().split()]
	suf = [10 ** 9] * n
	suf[-1] = a[-1]
	for i in range(n - 2, -1, -1):
		suf[i] = min(suf[i + 1], a[i])
	ans = 0
	for i in range(len(a) - 1):
		if a[i] > suf[i + 1]:
			ans += 1
	print(ans)


x = int(input())
for t in range(x):
	N = int(input())
	L = list(map(int, input().split()))
	y = L[-1]
	r = 0
	for n in range(1, N + 1):
		if L[-n] > y:
			r = r + 1
		if L[-n] < y:
			y = L[-n]
	print(r)


for case in range(int(input())):
	N = int(input())
	a = input().split()
	l1 = a[::-1]
	i = 0
	j = 1
	fl = True
	res = 0
	if N == 1:
		fl = False
		bad = [1]
	while fl:
		if int(l1[j]) > int(l1[i]):
			res += 1
			j += 1
		else:
			i = j
			j += 1
		if j == len(l1):
			fl = False
	print(res)


for _ in range(int(input())):
	n = int(input())
	L = list(map(int, input().split()))
	min = L[-1]
	bad_days = 0
	for i in range(n - 1, -1, -1):
		if L[i] > min:
			bad_days += 1
		else:
			min = L[i]
	print(bad_days)


for i in range(int(input())):
	input()
	a = [int(i) for i in input().split()]
	(counter, m) = (0, float('inf'))
	for i in a[::-1]:
		counter += i > m
		m = min(i, m)
	print(counter)


def main():
	n = int(input())
	arr = list(map(int, input().split()))
	res = 0
	mn = arr[-1]
	for i in range(len(arr) - 2, -1, -1):
		el = arr[i]
		if el > mn:
			res += 1
		mn = min(mn, el)
	print(res)
for _ in range(int(input())):
	main()


from sys import stdin, stdout
input = stdin.readline
t = int(input())
for _ in range(t):
	n = int(input())
	arr = [int(x) for x in input().split()]
	arr = arr[::-1]
	ans = 1
	m = arr[0]
	ans = 0
	for i in range(1, n):
		if arr[i] > m:
			ans += 1
		else:
			m = arr[i]
	print(ans)


import math
for i in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	a.reverse()
	mini = a[0]
	c = 0
	for x in a:
		if mini < x:
			c += 1
		else:
			mini = x
	print(c)


t = int(input())
for _ in range(t):
	n = int(input())
	y = list(map(int, input().split()))
	mini = y[-1]
	i = n - 2
	count = 0
	while i >= 0:
		if y[i] > mini:
			count += 1
		else:
			mini = y[i]
		i -= 1
	print(count)


test_count = int(input())
for _ in range(test_count):
	n = int(input())
	a = list(map(int, input().split()))
	i = len(a) - 1
	minCost = int(1000000.0 + 2)
	daysCnt = 0
	while i >= 0:
		if a[i] < minCost:
			minCost = a[i]
		if a[i] > minCost:
			daysCnt += 1
		i -= 1
	print(daysCnt)


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	mi = float('inf')
	bad = 0
	for i in range(n - 1, -1, -1):
		if l[i] > mi:
			bad += 1
		else:
			mi = l[i]
	print(bad)


for t in range(int(input())):
	(n, c, v) = (int(input()), 10 ** 6, 0)
	for ai in map(int, reversed(input().split())):
		if ai > c:
			v += 1
		elif ai < c:
			c = ai
	print(v)


t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	ans = 0
	for i in range(n - 1, -1, -1):
		if i == n - 1:
			minprice = l[i]
		elif l[i] > minprice:
			ans += 1
		else:
			minprice = min(minprice, l[i])
	print(ans)


for _ in range(int(input())):
	n = int(input())
	b = 0
	x = 1e+20
	for y in list(map(int, input().split()))[::-1]:
		if y > x:
			b += 1
		else:
			x = y
	print(b)


t = int(input())
for i in range(t):
	n = int(input())
	list1 = list(map(int, input().split()))
	min1 = list1[len(list1) - 1]
	sum1 = 0
	for i in range(1, n):
		if list1[n - 1 - i] < min1:
			min1 = list1[n - 1 - i]
		elif list1[n - 1 - i] > min1:
			sum1 += 1
	print(sum1)


def solve():
	n = int(input())
	arr = [int(x) for x in input().split()]
	mn = 10 ** 10
	ans = 0
	for x in arr[::-1]:
		if x > mn:
			ans += 1
		mn = min(mn, x)
	print(ans)
kk = int(input())
for _ in range(kk):
	solve()


def main():
	n = int(input())
	for i in range(n):
		c = int(input())
		vals = [int(v) for v in input().split()]
		min_v = vals[-1]
		count = 0
		for j in range(len(vals) - 2, -1, -1):
			if vals[j] > min_v:
				count += 1
			min_v = min(min_v, vals[j])
		print(count)
main()


n = int(input())
while n > 0:
	n -= 1
	input()
	prices = [int(x) for x in input().split()]
	minp = 1000005
	bad_count = 0
	for p in reversed(prices):
		if p > minp:
			bad_count += 1
		else:
			minp = p
	print(bad_count)


cases = int(input())
for case in range(cases):
	length = int(input())
	prices = [int(i) for i in reversed(input().split())]
	count = 0
	min_price = prices[0]
	for price in prices:
		if min_price > price:
			min_price = price
		elif min_price < price:
			count += 1
	print(count)


for _ in range(int(input())):
	input()
	x = 10 ** 7
	d = 0
	for a in reversed(list(map(int, input().split()))):
		d += a > x
		x = min(x, a)
	print(d)


def badprices(arr):
	count = 0
	min1 = arr[len(arr) - 1]
	for i in range(len(arr) - 1, -1, -1):
		if arr[i] - min1 > 0:
			count += 1
		min1 = min(arr[i], min1)
	return count
t = int(input())
for _ in range(t):
	n = int(input())
	arr = input()
	arr = [int(val) for val in arr.split()]
	ans = badprices(arr)
	print(ans)


t = int(input())
for _ in range(t):
	n = int(input())
	l = [*map(int, input().split())]
	mini = l[n - 1]
	count_bad = 0
	for i in range(n - 2, -1, -1):
		if l[i] > mini:
			count_bad += 1
		else:
			mini = l[i]
	print(count_bad)


T = int(input())
t = 0
while t < T:
	i = 0
	N = int(input())
	L = list(map(int, input().split()))
	s = len(L) - 1
	m = L[s]
	while s != -1:
		if L[s] < m:
			m = L[s]
		if L[s] > m:
			i += 1
		s -= 1
	t += 1
	print(i)


t = int(input())
while t > 0:
	n = int(input())
	l = [int(x) for x in input().split()]
	m = l[n - 1]
	count = 0
	if n > 1:
		for i in range(n - 2, -1, -1):
			if l[i] > m:
				count += 1
			else:
				m = l[i]
	print(count)
	t -= 1


t = int(input())
for tt in range(t):
	mi = 10000000000
	n = int(input())
	a = [int(e) for e in input().split()]
	i = n - 1
	ct = 0
	while i >= 0:
		if i != n - 1 and a[i] > mi:
			ct += 1
		mi = min(mi, a[i])
		i -= 1
	print(ct)


ts = int(input())
for t in range(ts):
	n = int(input())
	days = [int(elem) for elem in input().split()]
	good_days = []
	ans = 0
	for day in days:
		while len(good_days) > 0 and day < good_days[-1]:
			good_days.pop()
			ans += 1
		good_days.append(day)
	print(ans)


for t in range(int(input())):
	n = int(input())
	a = [int(x) for x in input().split()]
	c = a[n - 1]
	d = 0
	for i in range(n - 2, -1, -1):
		if a[i] > c:
			d += 1
		else:
			c = a[i]
	print(d)


q = int(input())

def count(day):
	idx = len(day) - 1
	current = idx - 1
	min = int(day[idx])
	counter = 0
	while current >= 0:
		if int(day[current]) <= min:
			min = int(day[current])
		else:
			counter += 1
		current -= 1
	return str(counter)
res = ''
for i in range(q):
	l = int(input())
	day = input().split()
	res += count(day) + '\n'
print(res.strip())


t = int(input().strip())
for i in range(t):
	n = int(input().strip())
	a = [int(x) for x in input().strip().split(' ')]
	min = a[n - 1]
	l = []
	l.append(min)
	for i in range(n - 2, -1, -1):
		if a[i] < min:
			l.append(a[i])
			min = a[i]
		else:
			l.append(min)
	c = 0
	for i in range(n):
		if a[i] > l[n - 1 - i]:
			c += 1
	print(c)


def compared_prices(prices):
	min_price = prices[-1]
	result = 0
	for i in range(len(prices) - 2, -1, -1):
		if prices[i] > prices[i + 1] or prices[i] > min_price:
			result += 1
		if min_price >= prices[i]:
			min_price = prices[i]
	return result
num_tests = int(input())
for i_test in range(num_tests):
	n = int(input())
	prices = [int(elem) for elem in input().split(' ')]
	print(compared_prices(prices))


T = int(input())
for t in range(T):
	X = int(input())
	Y = list(map(int, input().split()))
	a = Y[-1]
	b = 0
	for n in range(1, X + 1):
		if Y[-n] > a:
			b = b + 1
		if Y[-n] < a:
			a = Y[-n]
	print(b)


T = int(input())
ans = []
for _ in range(T):
	N = int(input())
	(*A,) = map(int, input().split())
	m = 10 ** 6
	A.reverse()
	r = 0
	for a in A:
		m = min(a, m)
		if m < a:
			r += 1
	ans.append('%d\n' % r)
open(1, 'w').writelines(ans)


def main():
	n1 = int(input())
	for _ in range(n1):
		n = int(input())
		arr = list(map(int, input().rstrip().split()))
		arr = arr[::-1]
		mini = arr[0]
		count = 0
		for i in range(1, len(arr)):
			if mini >= arr[i]:
				mini = arr[i]
			else:
				count += 1
		print(count)
main()


t = int(input())
for case in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	a.reverse()
	(c, min_a) = (0, a[0])
	for num in a:
		if num <= min_a:
			min_a = num
		else:
			c += 1
	print(c)


n = int(input())
for i in range(0, n):
	l = int(input())
	ln = [int(j) for j in input().split(' ')][::-1]
	c = 0
	mx = 1000000000000
	for j in range(0, len(ln)):
		if ln[j] <= mx:
			mx = ln[j]
			c += 1
	print(l - c)


t = int(input())
for _ in range(t):
	n = int(input())
	it = list(map(int, input().split()))
	mi = max(it) + 1
	tot = 0
	for i in it[-1::-1]:
		if i > mi:
			tot += 1
		mi = min(i, mi)
	print(tot)


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	bad = 0
	ini = l[n - 1]
	for i in range(n - 2, -1, -1):
		if l[i] > ini:
			bad += 1
		else:
			ini = l[i]
	print(bad)


t = int(input())
for _ in range(t):
	n = input()
	arr = list(map(int, input().split(' ')))
	count = 0
	cur_min = arr[-1]
	for num in reversed(arr):
		if num > cur_min:
			count += 1
		elif num < cur_min:
			cur_min = num
	print(count)


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


t = int(input())
for i in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	ans = 0
	mn = 100000000000
	for i in range(n - 1, -1, -1):
		if arr[i] > mn:
			ans += 1
		mn = min(mn, arr[i])
	print(ans)


for i in range(int(input())):
	a = int(input())
	b = list(map(int, input().split()))
	min1 = b[a - 1]
	co = 0
	for s in reversed(range(a - 1)):
		if b[s] > min1:
			co += 1
		if b[s] < min1:
			min1 = b[s]
	print(co)


MOD = 10 ** 9 + 7
I = lambda : list(map(int, input().split()))
(t,) = I()
while t:
	t -= 1
	(n,) = I()
	l = I()
	ans = 0
	s = [l[0]]
	for i in l[1:]:
		while s and s[-1] > i:
			ans += 1
			s.pop()
		s.append(i)
	print(ans)


input = __import__('sys').stdin.readline
t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	ans = 0
	cur_min = a[-1]
	for j in range(n - 2, -1, -1):
		if a[j] > cur_min:
			ans += 1
		else:
			cur_min = min(cur_min, a[j])
	print(ans)


T = int(input())
for i in range(T):
	t = int(input())
	l = list(map(int, input().split()))
	stack = []
	depth = [0] * t
	for j in range(t):
		if stack != []:
			while l[j] < l[stack[-1]]:
				depth[stack.pop()] = 1
				if stack == []:
					break
			stack.append(j)
		else:
			stack.append(j)
	print(depth.count(1))


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()))
	mn = float('inf')
	ct = 0
	for i in range(n - 1, -1, -1):
		if a[i] > mn:
			ct += 1
		mn = min(mn, a[i])
	print(ct)


def main():
	k = int(input())
	arr = [int(i) for i in input().split()]
	min_num = arr[-1]
	ans = 0
	for i in range(k - 2, -1, -1):
		i = arr[i]
		if i < min_num:
			min_num = i
		elif i > min_num:
			ans += 1
	return ans
otv = []
for j in range(int(input())):
	otv.append(main())
print(*otv, sep='\n')


t = int(input())
for i in range(0, t):
	n = int(input())
	l = 0
	a = list(map(int, input().split()))
	c = 999999999
	for j in range(n - 1, -1, -1):
		if c >= a[j]:
			c = a[j]
		else:
			l += 1
	print(l)


for case in range(int(input())):
	N = input()
	n = int(N)
	p = input().split()
	for i in range(n):
		p[i] = int(p[i])
	mn = int(2000000000.0)
	cnt = 0
	for i in range(n - 1, -1, -1):
		if p[i] <= mn:
			cnt = cnt + 1
		mn = min(mn, p[i])
	print(n - cnt)


t = int(input())
for e in range(t):
	n = int(input())
	a = [int(s) for s in input().split()]
	cnt = 0
	m = 100000000
	for i in range(n - 1, -1, -1):
		if a[i] < m:
			m = a[i]
		elif a[i] > m:
			cnt += 1
	print(cnt)


import sys
sys.setrecursionlimit(10 ** 5 + 1)
inf = int(10 ** 20)
max_val = inf
min_val = -inf
RW = lambda : sys.stdin.readline().strip()
RI = lambda : int(RW())
RMI = lambda : [int(x) for x in sys.stdin.readline().strip().split()]
RWI = lambda : [x for x in sys.stdin.readline().strip().split()]
nb_test = RI()
for _ in range(nb_test):
	lens = RI()
	vals = RMI()
	max_last = vals[-1]
	cnts = 0
	for i in range(lens - 2, -1, -1):
		if vals[i] > max_last:
			cnts += 1
		else:
			max_last = vals[i]
	print(cnts)


import sys, os
if os.path.exists('input.txt'):
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
for _ in range(int(input())):
	n = int(input())
	price = list(map(int, input().split()))
	mx = 10000000
	ans = 0
	for i in reversed(range(n)):
		if mx > price[i]:
			mx = price[i]
		if price[i] > mx:
			ans += 1
	print(ans)


k = int(input())
c = list()
for j in range(k):
	n = int(input())
	a = list(map(int, input().split()))
	mini = 10 ** 9
	count = 0
	for i in range(n - 1, -1, -1):
		if a[i] < mini:
			mini = a[i]
		elif a[i] > mini:
			count += 1
	c.append(count)
for i in range(k):
	print(c[i])


for _ in range(int(input())):
	input()
	p = list(map(int, input().split()))
	m = p[len(p) - 1]
	c = 0
	for i in range(len(p) - 1, -1, -1):
		if p[i] > m:
			c += 1
		m = min(m, p[i])
	print(c)


t = int(input())
for i in range(t):
	n = int(input())
	mn = 200000
	arr = list(map(int, input().split()))
	mn = arr[n - 1]
	bad = 0
	for i in range(n - 2, -1, -1):
		tmp = arr[i]
		if tmp < mn:
			mn = tmp
		elif tmp > mn:
			bad += 1
	print(bad)


def main():
	n = int(input())
	arr = list(map(int, input().split()))
	b = 0
	mi = arr[-1]
	for i in range(len(arr) - 2, -1, -1):
		if arr[i] > mi:
			b = b + 1
		mi = min(mi, arr[i])
	print(b)
n = int(input())
for i in range(n):
	main()


import sys
Input = sys.stdin.readline
for i in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	(counter, tmp) = (0, a[-1])
	for j in reversed(a):
		if j > tmp:
			counter += 1
		tmp = min(tmp, j)
	print(counter)


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	c = 0
	m = a[-1]
	for j in range(len(a) - 2, -1, -1):
		if a[j] > m:
			c += 1
		if a[j] < m:
			m = a[j]
	print(c)


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	b = list()
	cn = 0
	b.append(a[-1])
	for i in range(n - 2, -1, -1):
		if b[n - i - 2] <= a[i]:
			b.append(b[n - i - 2])
		else:
			b.append(a[i])
	b.reverse()
	for i in range(n):
		if a[i] > b[i]:
			cn += 1
	print(cn)


T = int(input())
for t in range(T):
	N = int(input())
	L = list(map(int, input().split()))
	m = L[-1]
	r = 0
	for n in range(1, N + 1):
		if L[-n] > m:
			r = r + 1
		if L[-n] < m:
			m = L[-n]
	print(r)


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))))
for i in range(I()):
	n = I()
	a = L()
	m = a[-1]
	ans = 0
	for i in range(n - 2, -1, -1):
		if a[i] > m:
			ans += 1
		m = min(m, a[i])
	print(ans)


amount = int(input())

def socks():
	numberofDays = int(input())
	badDays = 0
	days = list(map(int, input().split(' ')))
	min = days[numberofDays - 1]
	for i in reversed(days):
		if i <= min:
			min = i
		else:
			badDays += 1
	print(badDays)
while amount > 0:
	socks()
	amount -= 1


T = int(input())
for _ in range(0, T):
	N = int(input())
	s = [int(x) for x in input().split()]
	s = s[::-1]
	c = 0
	p = s[0]
	for i in range(1, len(s)):
		if s[i] > p:
			c += 1
		p = min(p, s[i])
	print(c)


entries = int(input())
for i in range(entries):
	qtt = int(input())
	numbers = list(map(int, input().split()))
	stack = []
	stack.append(numbers[0])
	baddays = 0
	j = 1
	while j < len(numbers):
		if len(stack) == 0 or numbers[j] >= stack[-1]:
			stack.append(numbers[j])
			j += 1
		else:
			baddays += 1
			stack.pop()
	print(baddays)


for i in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	count = 0
	min = a[-1]
	for i in range(n - 2, -1, -1):
		if a[i] > min:
			count += 1
		else:
			min = a[i]
	print(count)


import sys
for i in range(int(sys.stdin.readline())):
	c = 0
	n = int(sys.stdin.readline())
	a = sys.stdin.readline().split()[::-1]
	mi = int(a[0])
	for i in a:
		if int(i) > mi:
			c += 1
		else:
			mi = int(i)
	print(c)


for t in range(int(input())):
	n = int(input())
	a = [int(x) for x in input().split()]
	a.reverse()
	curr_min = a[0]
	ans = 0
	for x in a:
		if x > curr_min:
			ans += 1
		curr_min = min(x, curr_min)
	print(ans)


t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(x) for x in input().split()]
	count = 0
	mini = a[n - 1]
	for i in range(n - 2, -1, -1):
		if a[i] > mini:
			count += 1
		else:
			mini = a[i]
	print(count)


for _ in range(int(input())):
	n = int(input())
	l = [int(x) for x in input().split()]
	max_l = max(l)
	c = 0
	for i in range(n - 1, -1, -1):
		if l[i] > max_l:
			c += 1
		max_l = min(max_l, l[i])
	print(c)


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	mi = float('+inf')
	ans = 0
	for i in range(n - 1, -1, -1):
		if a[i] > mi:
			ans += 1
		mi = min(a[i], mi)
	print(ans)


def bad(S):
	mn = 254894514
	count = 0
	for i in range(len(S) - 1, -1, -1):
		if S[i] > mn:
			count += 1
		mn = min(mn, S[i])
	return count
for i in range(int(input())):
	n = int(input())
	S = list(map(int, input().strip().split()))
	print(bad(S))


for _ in range(int(input())):
	x = int(input())
	s = list(map(int, input().split()))
	s = s[::-1]
	m = 10 ** 8
	l = 0
	for n in s:
		if n < m:
			m = min(m, n)
		elif n > m:
			l += 1
	print(l)


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


t = int(input())
for z in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	cnt = 0
	mn = arr[-1]
	for i in range(n - 1, -1, -1):
		cnt += arr[i] > mn
		mn = min(arr[i], mn)
	print(cnt)


k = int(input())
answer = []
for _ in range(k):
	n = int(input())
	a = list(map(int, input().split()))
	ans = 0
	m = a[len(a) - 1]
	for i in range(n - 1, -1, -1):
		if a[i] > m:
			ans += 1
		m = min(a[i], m)
	answer.append(ans)
print(*answer, sep='\n')


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split(' ')))
	min1 = a[n - 1]
	j = n - 2
	bad_days = 0
	while j >= 0:
		if a[j] > min1:
			bad_days += 1
		else:
			min1 = a[j]
		j -= 1
	print(bad_days)


t = int(input())
for _ in range(t):
	n = int(input())
	days = [int(x) for x in input().split()]
	stack = [0]
	i = 1
	while i < n:
		while stack != [] and days[i] < days[stack[-1]]:
			stack.pop()
		stack.append(i)
		i += 1
	print(len(days) - len(stack))


p = int(input())
for q in range(p):
	n = int(input())
	l = list(map(int, input().split()))
	count = 0
	min = l[-1]
	for i in range(n - 2, -1, -1):
		if l[i] > min:
			count += 1
		if l[i] < min:
			min = l[i]
	print(count)


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


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	b = [0] * n
	for i in range(n):
		b[i] = a[i]
	x = min(a)
	b.append(10 ** 20)
	for j in range(n - 1, -1, -1):
		b[j] = min(b[j + 1], b[j])
	cnt = 0
	for i in range(0, n):
		if a[i] > b[i + 1]:
			cnt += 1
	print(cnt)


for u in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	l.reverse()
	x = l[0]
	c = 0
	for i in range(1, n):
		y = l[i]
		if y > x:
			c = c + 1
		x = min(y, x)
	print(c)


def readInts():
	return [int(x) for x in input().split()]

def numBadPrice(N, xs):
	minSoFar = 10 ** 7
	result = 0
	for x in xs[::-1]:
		if x > minSoFar:
			result += 1
		else:
			minSoFar = x
	return result
T = int(input())
for t in range(T):
	N = int(input())
	xs = readInts()
	result = numBadPrice(N, xs)
	print(result)


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	price = [-1] * n
	price[-1] = arr[-1]
	mini = arr[-1]
	for i in range(n - 2, -1, -1):
		mini = min(mini, arr[i])
		price[i] = mini
	ans = 0
	for i in range(n):
		if arr[i] > price[i]:
			ans += 1
	print(ans)


t = int(input())
for i in range(t):
	n = int(input())
	s = list(map(int, input().split()))
	s = s[::-1]
	mn = 10 ** 9
	it = 0
	for i in range(n):
		if s[i] > mn:
			it += 1
		else:
			mn = s[i]
	print(it)


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	ma = 0
	mi = 1000000.0
	count = 0
	for num in reversed(a):
		if num > mi:
			count += 1
		if num > ma:
			ma = num
		if num < mi:
			mi = num
	print(count)


t = int(input())
for i in range(t):
	n = int(input())
	a = [int(x) for x in input().split()]
	minimum = a[n - 1]
	count = 0
	for j in range(n - 2, -1, -1):
		if a[j] > minimum:
			count += 1
		minimum = min(minimum, a[j])
	print(count)


t = int(input())
while t > 0:
	t -= 1
	n = int(input())
	a = [int(x) for x in input().split()[:n]]
	d = 0
	a_min = a[-1]
	for i in range(n - 2, -1, -1):
		current = a[i]
		if current > a_min:
			d += 1
		else:
			a_min = current
	print(d)


t = int(input())
for i in range(t):
	n = int(input())
	a = [int(k) for k in input().split()]
	d = 0
	m = a[n - 1]
	for j in range(n - 1, -1, -1):
		if a[j] <= m:
			m = a[j]
		else:
			d += 1
	print(d)


import sys
import copy
ntc = int(sys.stdin.readline().rstrip())
for tcs in range(ntc):
	n = int(sys.stdin.readline().rstrip())
	num = list(map(int, sys.stdin.readline().rstrip().split()))
	h = copy.deepcopy(num)
	visited = dict()
	for i in range(n):
		try:
			visited[num[i]] += 1
		except KeyError:
			visited[num[i]] = 1
	h.sort()
	res = 0
	idx = 0
	for i in range(n):
		while visited[h[idx]] == 0:
			idx += 1
		if num[i] == h[idx]:
			visited[h[idx]] -= 1
			idx += 1
			continue
		else:
			visited[num[i]] -= 1
			res += 1
	print(res)


z = []
y = []
x = int(input())
while x > 0:
	z = int(input())
	y = input().split()
	count = 0
	if z == 1:
		print(0)
		x -= 1
		continue
	cm = int(y[z - 1])
	while z > 1:
		if int(y[z - 2]) > cm:
			count += 1
		elif int(y[z - 2]) < cm:
			cm = int(y[z - 2])
		z -= 1
	print(count)
	x -= 1


t = int(input())
for i in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	arr = [(arr[i], i) for i in range(n)]
	arr.sort()
	r1 = 0
	r = arr[0][1]
	ans = 0
	for k in range(1, n):
		if arr[k][0] != arr[k - 1][0]:
			r1 = r
		if arr[k][1] < r1:
			ans += 1
		elif arr[k][1] > r:
			r = arr[k][1]
	print(ans)


T = int(input())
for i in range(T):
	N = int(input())
	l = [int(x) for x in input().split()]
	ans = 0
	s = l[-1]
	for j in reversed(l):
		if j > s:
			ans += 1
		else:
			s = j
	print(ans)


import os
import heapq
import sys
import math
import operator
from collections import defaultdict
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()
		res = 0
		mn = 10 ** 9
		for i in range(n - 1, -1, -1):
			if mn >= arr[i]:
				mn = arr[i]
			else:
				res += 1
		print(res)
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()


def rint():
	return int(input())

def rints():
	return list(map(int, input().split()))
t = rint()
for _ in range(t):
	n = rint()
	a = rints()
	mi = a[-1]
	bad = 0
	for ai in reversed(a[:-1]):
		if ai > mi:
			bad += 1
		mi = min(mi, ai)
	print(bad)


t = int(input())
for i in range(t):
	n = int(input())
	lst = list(map(int, input().strip().split(' ')))
	lst1 = []
	for j in range(n):
		lst1.append(0)
	c = 0
	mp = lst[n - 1]
	j = n - 1
	while j >= 0:
		if lst[j] < mp:
			mp = lst[j]
		lst1[j] = mp
		j -= 1
	for j in range(n):
		if lst[j] > lst1[j]:
			c += 1
	print(c)


from sys import stdin
n = int(stdin.readline())
for _ in range(n):
	stdin.readline()
	nums = list(map(int, stdin.readline().rstrip().split(' ')))
	c = 0
	mi = float('inf')
	for i in range(len(nums) - 1, -1, -1):
		if nums[i] > mi:
			c += 1
		mi = min(mi, nums[i])
	print(c)


import sys
in_file = sys.stdin
t = int(in_file.readline().strip())
for _ in range(t):
	n = int(in_file.readline().strip())
	A = list(map(int, in_file.readline().strip().split()))
	A.reverse()
	t = A[0]
	res = 0
	for i in A:
		if i > t:
			res += 1
		else:
			t = i
	sys.stdout.write(str(res) + '\n')
sys.stdout.flush()


T = int(input())
while T:
	n = int(input())
	A = list(map(int, input().split()))
	S = []
	S.append(A[0])
	m = A[0]
	i = 1
	k = 0
	while i < n:
		if A[i] > m:
			S.append(A[i])
			m = A[i]
		else:
			j = len(S) - 1
			while j != -1 and S[j] > A[i]:
				k = k + 1
				S.pop(j)
				j = j - 1
			m = A[i]
			S.append(m)
		i = i + 1
	print(k)
	T = T - 1


T = int(input())
res = []
count = 0
for t in range(T):
	n = int(input())
	l = list(map(int, input().split(' ')))
	if n == 1:
		res.append(0)
	else:
		j = l[n - 1]
		for i in reversed(range(n - 1)):
			if l[i] > j:
				count += 1
			else:
				j = min(l[i], j)
		res.append(count)
		count = 0
print(*res, sep='\n')


from __future__ import division, print_function
from sys import stdin, stdout
import itertools, os, sys, threading
from collections import deque, Counter, OrderedDict, defaultdict
import heapq
from math import ceil, floor, log, sqrt, factorial, pow, pi, gcd
from fractions import Fraction

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

def si():
	return str(input())

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

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

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

def fsi():
	return str(stdin.readline())

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

def fli():
	return list(fmi())
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
(dx, dy) = ([-1, 1, 0, 0], [0, 0, 1, -1])

def getKey(item):
	return item[0]

def sort2(l):
	return sorted(l, key=getKey)

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 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
graph = defaultdict(list)
visited = [0] * 1000000
col = [-1] * 1000000

def dfs(v, c):
	if visited[v]:
		if col[v] != c:
			print('-1')
			exit()
		return
	col[v] = c
	visited[v] = 1
	for i in graph[v]:
		dfs(i, c ^ 1)

def bfs(d, v):
	q = []
	q.append(v)
	visited[v] = 1
	while len(q) != 0:
		x = q[0]
		q.pop(0)
		for i in d[x]:
			if visited[i] != 1:
				visited[i] = 1
				q.append(i)
		print(x)

def make_graph(e):
	d = {}
	for i in range(e):
		(x, y) = mi()
		if x not in d.keys():
			d[x] = [y]
		else:
			d[x].append(y)
		if y not in d.keys():
			d[y] = [x]
		else:
			d[y].append(x)
	return d

def gr2(n):
	d = {}
	for i in range(n):
		(x, y) = mi()
		if x not in d.keys():
			d[x] = [y]
		else:
			d[x].append(y)
	return d

def connected_components(graph):
	seen = set()

	def dfs(v):
		vs = set([v])
		component = []
		while vs:
			v = vs.pop()
			seen.add(v)
			vs |= set(graph[v]) - seen
			component.append(v)
		return component
	ans = []
	for v in graph:
		if v not in seen:
			d = dfs(v)
			ans.append(d)
	return ans

def primeFactors(n):
	s = set()
	while n % 2 == 0:
		s.add(2)
		n = n // 2
	for i in range(3, int(sqrt(n)) + 1, 2):
		while n % i == 0:
			s.add(i)
			n = n // i
	if n > 2:
		s.add(n)
	return s

def find_all(a_str, sub):
	start = 0
	while True:
		start = a_str.find(sub, start)
		if start == -1:
			return
		yield start
		start += len(sub)
t = ii()
for a0 in range(t):
	n = ii()
	l = li()
	m = []
	mini = sys.maxsize
	for i in range(n - 1, -1, -1):
		mini = min(mini, l[i])
		m.append(mini)
	m = m[::-1]
	s = 0
	for i in range(n):
		if l[i] > m[i]:
			s += 1
	print(s)


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


for i in range(int(input())):
	k = int(input())
	a = list(map(int, input().split()))
	b = a[-1]
	c = 0
	for i in range(len(a) - 2, -1, -1):
		if a[i] > b:
			c += 1
		else:
			b = a[i]
	print(c)


def op():
	input()
	a = [int(x) for x in input().split()]
	if len(a) == 1:
		print(0)
		return
	b = [0] * len(a)
	mx = a[-1]
	s = 0
	for i in range(1, len(a)):
		if a[-i] <= mx:
			b[-i] = a[-i]
			mx = a[-i]
		else:
			b[-i] = mx
	b[0] = min(a[0], b[1])
	for i in range(len(a)):
		if a[i] > b[i]:
			s += 1
	print(s)
c = int(input())
for i in range(c):
	op()


t = int(input())
for k in range(t):
	n = int(input())
	l1 = list(map(int, input().split()))
	count = 0
	i = 0
	if len(l1) > 1:
		while len(l1) > 1:
			if l1[-1] < l1[-2]:
				count = count + 1
				l1.pop(-2)
			else:
				l1.pop(-1)
		print(count)
	else:
		print(0)


for _ in range(int(input())):
	n = int(input())
	a = [*map(int, input().split())]
	b = [None] * n
	bad = 0
	for i in reversed(range(n)):
		if i == n - 1:
			b[i] = a[n - 1]
		elif a[i] > b[i + 1]:
			b[i] = b[i + 1]
			bad += 1
		else:
			b[i] = a[i]
	print(bad)


t = int(input())
Ans = ''
for k in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	ans = 0
	m = [a[0]]
	for i in range(1, n):
		m.append(a[i])
		l = len(m) - 1
		while l > 0 and m[l] < m[l - 1]:
			(m[l], m[l - 1]) = (m[l - 1], m[l])
			m.pop()
			l -= 1
			ans += 1
	Ans += str(ans) + '\n'
print(Ans)


tn = int(input())
for i in range(0, tn):
	n = int(input())
	inp = input().split(' ')
	inp.reverse()
	lowest = 10000000
	cc = 0
	for i in inp:
		cur = int(i)
		if cur > lowest:
			cc += 1
		lowest = min(lowest, cur)
	print(cc)


t = int(input())
for q in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	c = 0
	m = float('INF')
	for i in range(n - 1, -1, -1):
		if a[i] > m:
			c += 1
		m = min(m, a[i])
	print(c)


def arr_inp():
	return [int(x) for x in input().split()]

def arr_enu():
	return [[i, int(x)] for (i, x) in enumerate(input().split())]
t = int(input())
for i in range(t):
	(n, a, out) = (int(input()), arr_enu(), 0)
	a.sort(key=lambda x: x[1])
	ix = a[0][0]
	for j in range(1, n):
		if a[j][0] > ix:
			ix = a[j][0]
		else:
			out += 1
	print(out)


for tc in range(int(input())):
	n = int(input())
	line = input().split()
	arr = []
	for i in range(n):
		arr.append(int(line[i]))
	mini = arr[-1]
	count = 0
	for i in range(n):
		if arr[n - i - 1] > mini:
			count += 1
		else:
			mini = arr[n - i - 1]
	print(count)


t = int(input())
for i in range(0, t):
	n = int(input())
	a = [int(val) for val in input().split()]
	a.reverse()
	mina = a[0]
	ans = 0
	for j in a:
		if j > mina:
			ans += 1
		if j < mina:
			mina = j
	print(ans)


import sys
import math
from collections import defaultdict
from collections import Counter
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	m = a[n - 1]
	c = 0
	for i in range(n - 2, -1, -1):
		if a[i] > m:
			c += 1
		m = min(a[i], m)
	print(c)


def min(a, b):
	if a < b:
		return a
	else:
		return b
t = int(input())
for i in range(t):
	minimum = 10 ** 7
	n = int(input())
	a = input().split(' ')
	baddays = 0
	lenght_a = len(a)
	mins = [0] * lenght_a
	i = lenght_a - 1
	while i > -1:
		minimum = min(minimum, int(a[i]))
		mins[i] = minimum
		i = i - 1
	for i in range(0, lenght_a - 1):
		if int(a[i]) > mins[i + 1]:
			baddays = baddays + 1
	print(baddays)


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()
	a.reverse()
	smallest = 1000001
	ans = 0
	for i in range(n):
		if a[i] > smallest:
			ans += 1
		if a[i] < smallest:
			smallest = a[i]
	print(ans)


t = int(input())
for j in range(t):
	n = int(input())
	a = [int(x) for x in input().split()]
	k = 0
	mi = a[-1]
	for i in range(n - 2, -1, -1):
		if a[i] > mi:
			k += 1
		elif a[i] < mi:
			mi = a[i]
	print(k)


t = int(input())
while t != 0:
	n = int(input())
	lis = list(map(int, input().split()))
	c = 0
	k = lis[-1]
	for i in range(n - 2, -1, -1):
		if lis[i] > k:
			c += 1
		else:
			k = lis[i]
	print(c)
	t = t - 1


import sys
input = sys.stdin.readline

def fastio():
	from io import StringIO
	from atexit import register
	global input
	sys.stdin = StringIO(sys.stdin.read())
	input = lambda : sys.stdin.readline().rstrip('\r\n')
	sys.stdout = StringIO()
	register(lambda : sys.__stdout__.write(sys.stdout.getvalue()))
fastio()
INF = 10 ** 20
MOD = 998244353
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
(t,) = I()
while t:
	t -= 1
	(n,) = I()
	a = I()
	cur = a[-1]
	ans = 0
	for i in range(n - 1, -1, -1):
		if cur < a[i]:
			ans += 1
		cur = min(cur, a[i])
	print(ans)


t = int(input())
for tt in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	i = n - 1
	res = 0
	mn = 100000000
	while i >= 0:
		if a[i] > mn:
			res += 1
		if mn > a[i]:
			mn = a[i]
		i -= 1
	print(res)


t = int(input())
for j in range(t):
	n = int(input())
	a = [int(i) for i in input().split()]
	ans = 0
	mi = a[len(a) - 1]
	for i in range(len(a) - 1, -1, -1):
		if a[i] < mi:
			mi = a[i]
		if a[i] > mi:
			ans += 1
	print(ans)


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


def solution():
	from sys import stdout, stdin
	(_input, _print) = (stdin.readline, stdout.write)
	(_int, _range, _min, _max) = (int, range, min, max)
	for _ in range(int(input())):
		n = int(input())
		arr = [int(i) for i in input().split()]
		min_arr = [0] * n
		c = 0
		for i in range(n - 1, -1, -1):
			if i == n - 1:
				min_arr[i] = arr[i]
			else:
				min_arr[i] = min(min_arr[i + 1], arr[i])
		for i in range(n - 1):
			if arr[i] > min_arr[i + 1]:
				c += 1
		print(c)
solution()


test = int(input())
for _ in range(test):
	n = int(input())
	arr = list(map(int, input().split()))
	minm = arr[-1]
	ans = 0
	for i in range(n - 2, -1, -1):
		if arr[i] > minm:
			ans += 1
		minm = min(minm, arr[i])
	print(ans)


for t in range(int(input())):
	n = int(input())
	a = [int(i) for i in input().strip().split()]
	m = a[n - 1]
	c = 0
	for i in range(n - 1, -1, -1):
		if a[i] <= m:
			m = a[i]
		else:
			c += 1
	print(c)


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


MIN = int(100000000.0) + 2
t = int(input(''))
arr = []
for i in range(t):
	int(input(''))
	arr.append(list(map(int, input().split())))
for i in arr:
	cnt = 0
	mini = MIN
	for j in range(len(i) - 1, -1, -1):
		if i[j] > mini:
			cnt += 1
		mini = min(mini, i[j])
	print(cnt)


U = int(input())
u = 0
while u < U:
	t = 0
	M = int(input())
	K = list(map(int, input().split()))
	w = len(K) - 1
	n = K[w]
	while w != -1:
		if K[w] < n:
			n = K[w]
		if K[w] > n:
			t += 1
		w -= 1
	u += 1
	print(t)


def main():
	n = int(input())
	arr = list(map(int, input().split()))
	min = arr[n - 1]
	co = 0
	for i in range(n - 1, -1, -1):
		if min < arr[i]:
			co += 1
		else:
			min = arr[i]
	print(co)
for _ in range(int(input())):
	main()


t = int(input())
for i in range(t):
	k = 0
	n = int(input())
	arr = list(map(int, input().split()))
	mi = arr[-1]
	for i in range(n - 1, -1, -1):
		if arr[i] > mi:
			k += 1
		mi = min(mi, arr[i])
	print(k)


import time

def solution(cost, start_ind):
	ans = 0
	cur = cost[-1]
	for i in range(len(cost) - 2, -1, -1):
		if cost[i] <= cur:
			cur = cost[i]
		else:
			ans += 1
	return ans

def main():
	n = int(input())
	cost = list(map(int, input().split()))
	print(solution(cost, 0))
t = int(input())
for i in range(t):
	main()


t = int(input())
for i in range(t):
	n = int(input())
	l = [int(k) for k in input().split()]
	if n == 1:
		print(0)
	else:
		l1 = []
		for j in range(n - 1):
			l1.append(0)
		l1[-1] = l[-1]
		minu = l1[-1]
		for j in range(n - 3, -1, -1):
			if l[j + 1] < minu:
				minu = l[j + 1]
			l1[j] = minu
		number = 0
		for j in range(n - 1):
			if l1[j] < l[j]:
				number += 1
		print(number)


t = int(input())
for _ in range(t):
	n = int(input())
	v = list(map(int, input().split()))
	mn = v[-1]
	f = 0
	while len(v) > 0:
		if v[-1] <= mn:
			mn = v[-1]
		else:
			f += 1
		v.pop()
	print(f)


t = int(input())
for i in range(t):
	n = int(input())
	a = [int(x) for x in input().split()]
	ans = 0
	mini = a[n - 1]
	j = n - 1
	while j > 0:
		if a[j - 1] > mini:
			ans += 1
			j -= 1
		else:
			mini = a[j - 1]
			j -= 1
	print(ans)


cases = int(input())
for _ in range(cases):
	qtd = 0
	days = int(input())
	prices = list(map(int, input().split()))
	minor_day = prices[-1]
	for price in prices[-2::-1]:
		if price > minor_day:
			qtd += 1
		else:
			minor_day = price
	print(qtd)


from sys import stdin
inp = stdin.readline
t = int(inp().strip())
for _ in range(t):
	n = int(inp().strip())
	a = [int(x) for x in inp().strip().split()]
	min1 = 10 ** 6 + 1
	ans = 0
	for i in a[::-1]:
		min1 = min(i, min1)
		if i > min1:
			ans += 1
	print(ans)


t = int(input())
for _ in range(t):
	N = int(input())
	line = input().strip()
	nums = list(map(int, line.split()))
	count = 0
	least = nums[-1]
	for i in range(N - 2, -1, -1):
		if nums[i] > least:
			count += 1
		else:
			least = nums[i]
	print(count)


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


t = int(input())
for tt in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	mm = 10000000000000000
	ans = 0
	for i in range(n - 1, -1, -1):
		if mm < arr[i]:
			ans += 1
		mm = min(mm, arr[i])
	print(ans)


MainLoop = int(input())
for i in range(MainLoop):
	n = int(input())
	a = [int(i) for i in input().split()]
	answ = 0
	min = a[len(a) - 1]
	for i in range(len(a) - 1, -1, -1):
		if min < a[i]:
			answ += 1
		else:
			min = a[i]
	print(answ)


t = int(input())
v = []
for i in range(t):
	c = 0
	n = int(input())
	a = [int(s) for s in input().split()]
	m = a[n - 1] + 1
	for k in range(n - 1, -1, -1):
		if m >= a[k]:
			m = a[k]
		else:
			c += 1
	v.append(c)
print(*v, sep='\n')


out = []
for i in range(int(input())):
	(nahui_ne_nado_prigoditsa_tolko_v_plusah, min_n, n) = (int(input()), None, 0)
	for i in reversed([int(i) for i in input().split()]):
		if min_n == None:
			min_n = i
		elif i <= min_n:
			min_n = i
		else:
			n += 1
	out.append(n)
print(*out, sep='\n')


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	low = a[-1]
	mark = [0] * n
	for i in range(n - 2, -1, -1):
		if a[i] > low:
			mark[i] = 1
		else:
			low = a[i]
	print(mark.count(1))


import sys
n = int(input())
for _ in range(n):
	k = int(input())
	l = []
	l = list(map(int, input().split(' ')))
	ans = 0
	max = sys.maxsize
	for j in range(k - 1, -1, -1):
		if max < l[j]:
			ans += 1
		max = min(max, l[j])
	print(ans)


import sys
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, MOD) = (1000000007, 998244353)
vow = ['a', 'e', 'i', 'o', 'u']
(dx, dy) = ([-1, 1, 0, 0], [0, 0, 1, -1])

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()
T = int(input())
while T > 0:
	n = int(input())
	Arr = get_array()
	ans = 0
	mini = Arr.pop()
	while Arr:
		if Arr[-1] > mini:
			ans += 1
			Arr.pop()
		else:
			mini = min(mini, Arr.pop())
	print(ans)
	T -= 1


t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().strip().split()))
	stc = []
	cnt = 0
	for i in range(n):
		while stc and stc[-1] > arr[i]:
			stc.pop()
			cnt += 1
		stc.append(arr[i])
	print(cnt)


def program():
	n = int(input())
	l = list(map(int, input().split()))
	m = l[n - 1]
	count = 0
	for i in range(n - 2, -1, -1):
		if l[i] < m:
			m = l[i]
		elif l[i] > m:
			count += 1
	print(count)
t = int(input())
for i in range(t):
	program()


try:
	t = int(input())
	for __ in range(t):
		n = int(input())
		l = list(map(int, input().split()))
		st = [l[0]]
		c = 0
		for _ in l[1:]:
			if st[-1] < _:
				st.append(_)
			else:
				while len(st) != 0 and st[-1] > _:
					st.pop()
					c += 1
				st.append(_)
		print(c)
except:
	pass


t = int(input())
for j in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	k = 0
	min_price = [a[n - 1]]
	f = 0
	for i in range(n - 1, -1, -1):
		min_price.append(min(min_price[f], a[i]))
		f += 1
	min_price = min_price[::-1]
	for i in range(n - 1):
		if a[i] > min_price[i + 1]:
			k += 1
	print(k)


for _ in range(int(input().strip())):
	n = int(input().strip())
	C = [int(i) for i in input().strip().split(' ')]
	count = 0
	min_ = C[-1]
	for i in range(n - 2, -1, -1):
		if C[i] > min_:
			count += 1
		else:
			min_ = C[i]
	print(count)


t = int(input())
for i in range(t):
	n = int(input())
	c = 0
	data = list(map(int, input().split()))
	stack = []
	for i in data:
		if not stack:
			stack.append(i)
		else:
			while stack and stack[-1] > i:
				c += 1
				stack.pop()
			stack.append(i)
	print(c)


p = int(input())
for i in range(p):
	n = int(input())
	arr = list(map(int, input().split()))
	x = arr[len(arr) - 1]
	c = 0
	for i in range(len(arr) - 2, -1, -1):
		if x < arr[i]:
			c += 1
		else:
			x = arr[i]
	print(c)


t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	if n == 1:
		print(0)
	else:
		st = []
		cnt = 0
		for a in arr:
			while st and st[-1] > a:
				st.pop()
				cnt += 1
			st.append(a)
		print(cnt)


for _ in range(int(input())):
	n = int(input())
	p = list(map(int, input().split()))
	minp = p[-1]
	bad = 0
	for i in range(n - 2, -1, -1):
		if p[i] > minp:
			bad += 1
		else:
			minp = p[i]
	print(bad)


t = int(input())
while t > 0:
	t = t - 1
	n = int(input())
	c = 0
	ar = [int(x) for x in input().split()]
	mi = ar[n - 1]
	for i in range(n - 2, -1, -1):
		if ar[i] > mi:
			c = c + 1
		else:
			mi = ar[i]
	print(c)


t = int(input())
for i in range(t):
	d = int(input())
	prices = [int(x) for x in input().split()]
	c = 0
	stack = []
	for j in prices:
		while stack and j < stack[-1]:
			stack.pop()
			c += 1
		stack.append(j)
	print(c)


import sys

def get_ints():
	return map(int, sys.stdin.readline().strip().split())
for t in range(int(input())):
	n = int(input())
	arr = list(get_ints())
	count = 0
	maxim = arr[-1]
	for i in range(n - 1, -1, -1):
		if arr[i] > maxim:
			count += 1
		maxim = min(arr[i], maxim)
	print(count)


t = int(input())
for i in range(t):
	input()
	l = [int(x) for x in input().split()]
	bad = 0
	lowest = l[-1]
	for price in reversed(l):
		if price > lowest:
			bad += 1
		else:
			lowest = price
	print(bad)


n = int(input())
for i in range(n):
	nn = int(input())
	k = map(lambda x: int(x), input().split())
	k = list(k)
	k.reverse()
	count = 0
	minnum = k[0]
	for c in k:
		if c > minnum:
			count += 1
		minnum = min(minnum, c)
	print(count)


def solve(A):
	B = []
	c = 0
	for (i, a) in enumerate(A):
		if i > 0:
			while B and B[-1] > a:
				B.pop()
				c += 1
		B.append(a)
	return c
assert solve([3, 9, 4, 6, 7, 5]) == 3
assert solve([1000000]) == 0
assert solve([2, 1]) == 1
assert solve([31, 41, 59, 26, 53, 58, 97, 93, 23, 84]) == 8
assert solve([3, 2, 1, 2, 3, 4, 5]) == 2
m = int(input())
for _ in range(m):
	n = int(input())
	A = list(map(int, input().split()))
	ans = solve(A)
	print(ans)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	a = a[::-1]
	bad = 0
	check = a[0]
	for i in a:
		if check < i:
			bad += 1
		check = min(check, i)
	print(bad)


import sys
import random
t = int(sys.stdin.readline().split()[0])
for data in range(t):
	n = int(sys.stdin.readline().split()[0])
	days = sys.stdin.readline().split()
	for i in range(n):
		days[i] = int(days[i])
	bad_days = 0
	minimum = days[n - 1]
	for i in range(n - 1, -1, -1):
		if days[i] > minimum:
			bad_days += 1
		else:
			minimum = days[i]
	sys.stdout.write(str(bad_days) + '\n')


import sys
i = n = 0
res = []
for line in sys.stdin:
	line = line.strip()
	if i:
		if not i % 2:
			arr = []
			cnt = 0
			for num in map(int, line.split()):
				while arr and arr[-1] > num:
					arr.pop()
					cnt += 1
				arr.append(num)
			res.append(cnt)
	i += 1
for num in res:
	print(num)


numOfSets = int(input())
for i in range(numOfSets):
	numOfDays = int(input())
	sells = input().split(' ')
	minimum = int(sells[numOfDays - 1])
	badDays = 0
	if numOfDays == 1:
		print('0')
	else:
		for i in reversed(sells):
			if int(i) > int(minimum):
				badDays += 1
			else:
				minimum = i
		print(badDays)


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


t = int(input())
for i in range(t):
	n = int(input())
	l1 = list(map(int, input().split()))
	l2 = [0] * n
	l2[n - 1] = l1[n - 1]
	for i in range(n - 2, -1, -1):
		if l1[i] < l2[i + 1]:
			l2[i] = l1[i]
		else:
			l2[i] = l2[i + 1]
	days = 0
	for i in range(n):
		if l1[i] != l2[i]:
			days += 1
	print(days)


def problem():
	input()
	min_so_far = float('inf')
	num_bad = 0
	for n in reversed(list(map(int, input().split(' ')))):
		if n > min_so_far:
			num_bad += 1
		min_so_far = min(min_so_far, n)
	print(num_bad)
n = int(input())
for i in range(n):
	problem()


from sys import stdin, stdout
from collections import Counter
import math

def rsingle_int():
	return int(stdin.readline().rstrip())

def rmult_int():
	return [int(x) for x in stdin.readline().rstrip().split()]

def rmult_str():
	return stdin.readline().rstrip().split()

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

def rsingle_char():
	return stdin.read(1)

def sortFirst(val):
	return val[0]

def main():
	t = rsingle_int()
	for i in range(t):
		n = rsingle_int()
		a = rmult_int()
		a_pairs = []
		for j in range(n):
			a_pairs.append((a[j], j))
		a_pairs.sort(key=sortFirst)
		prev_max_pos = -1
		cnt = 0
		for (val, pos) in a_pairs:
			if pos > prev_max_pos:
				prev_max_pos = pos
			else:
				cnt += 1
		print(cnt)
main()


T = int(input())
for i in range(T):
	n = int(input())
	l = list(map(int, input().split()))
	(min, s) = (l[-1], 0)
	for k in range(n - 2, -1, -1):
		if l[k] < min:
			min = l[k]
		if l[k] > min:
			s += 1
	print(s)


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


t = int(input())
while t > 0:
	n = int(input())
	a = list(map(int, input().split()))
	arr = [0] * n
	count = 0
	arr[n - 1] = a[-1]
	for i in range(n - 2, -1, -1):
		arr[i] = min(a[i], arr[i + 1])
	for i in range(n):
		if arr[i] < a[i]:
			count += 1
	print(count)
	t -= 1


def solve(n, ar):
	ans = 0
	right_min = 10000000
	for i in range(n - 1, -1, -1):
		if ar[i] > right_min:
			ans += 1
		right_min = min(right_min, ar[i])
	print(ans)
t = int(input())
for _ in range(t):
	n = int(input())
	ar = list(map(int, input().split()))
	solve(n, ar)


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	c = 0
	r = a.pop()
	while a:
		if a[-1] > r:
			c += 1
			a.pop()
		else:
			r = min(r, a.pop())
	print(c)


t = int(input())
while t:
	n = int(input())
	l = list(map(int, input().rstrip().split(' ')))
	if n == 1:
		print(0)
	else:
		c = 0
		m = l[-1]
		for i in range(n - 2, -1, -1):
			if l[i] > m:
				c += 1
			else:
				m = l[i]
		print(c)
	t -= 1


import sys, math
input = sys.stdin.readline

def getInt():
	return int(input())

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

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

def getStr():
	return input().strip()
t = getInt()
for _ in range(t):
	n = getInt()
	a = getList()
	m = a[n - 1]
	res = 0
	for i in range(n - 2, -1, -1):
		if a[i] <= m:
			m = a[i]
		else:
			res += 1
	print(res)


t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	stack = [arr[-1]]
	for i in range(len(arr) - 2, -1, -1):
		stack.append(min(stack[-1], arr[i]))
	stack.reverse()
	cnt = 0
	for i in range(len(arr)):
		if arr[i] > stack[i]:
			cnt += 1
	print(cnt)


n = int(input())
for i in range(n):
	t = int(input())
	a = list(map(int, input().split()))[::-1]
	d = 0
	c = a[0]
	for i in range(1, t):
		if c < a[i]:
			d += 1
		c = min(c, a[i])
	print(d)


def get_ans(array, n):
	array = array[::-1]
	min_value = array[0]
	counter = 0
	for i in array[1:]:
		if i > min_value:
			counter += 1
		else:
			min_value = i
	return counter
t = int(input())
for i in range(t):
	n = int(input())
	array = list(map(int, input().strip().split()))
	print(get_ans(array, n))


q = int(input())
for i in range(q):
	n = int(input())
	a = list(map(int, input().split()))
	a.reverse()
	count = 0
	m = a[0]
	for i in range(1, n):
		if a[i] > m:
			count += 1
		else:
			m = a[i]
	print(count)


t = int(input())
for _ in range(t):
	n = int(input())
	lis = list(map(int, input().split()))
	stack = []
	bad = 0
	for i in range(len(lis)):
		if len(stack) == 0 or stack[-1] <= lis[i]:
			stack.append(lis[i])
		else:
			while len(stack) > 0 and stack[-1] > lis[i]:
				stack.pop()
				bad += 1
			stack.append(lis[i])
	print(bad)


for _ in range(int(input())):
	n = int(input())
	A = list(map(int, input().split()))
	ans = 0
	m = A[-1]
	for a in A[::-1]:
		ans += a > m
		m = min(a, m)
	print(ans)


a = int(input())
for i in range(a):
	b = int(input())
	queue = list(map(int, input().split()))
	queue = queue[::-1]
	minimum = 1000001
	count = 0
	for item in queue:
		if item <= minimum:
			minimum = item
		else:
			count += 1
	print(str(count))


for _ in range(int(input())):
	n = int(input())
	list1 = list(map(int, input().split()))
	min1 = list1[n - 1]
	c = 0
	for i in range(n - 1, -1, -1):
		if list1[i] <= min1:
			min1 = min(min1, list1[i])
		else:
			c = c + 1
	print(c)


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


for tests in range(int(input())):
	n = int(input())
	l = [int(x) for x in input().split(' ')]
	l = l[::-1]
	mn = l[0]
	ans = 0
	for i in l:
		if i > mn:
			ans += 1
		else:
			mn = i
	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()]
	ans = 0
	min_pr = a[-1]
	for i in range(n - 2, -1, -1):
		if a[i] > min_pr:
			ans += 1
		else:
			min_pr = a[i]
	print(ans)


for _ in range(int(input())):
	n = int(input())
	A = list(map(int, input().split()))
	m = 10 ** 9
	c = 0
	for i in range(n - 1, -1, -1):
		if A[i] <= m:
			m = A[i]
		else:
			c += 1
	print(c)


tc = int(input())
for _ in range(tc):
	n = int(input())
	l = [int(x) for x in input().split()]
	cnt = 0
	d = 1000010
	for i in range(n - 1, -1, -1):
		if d < l[i]:
			cnt += 1
		elif l[i] < d:
			d = l[i]
	print(cnt)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	mn = 100000000
	ct = 0
	for i in range(n - 1, -1, -1):
		if a[i] > mn:
			ct += 1
		else:
			mn = a[i]
	print(ct)


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

def unique(sequence):
	seen = set()
	return [x for x in sequence if not (x in seen or seen.add(x))]
for _ in range(int(I1())):
	n = int(I1())
	l = list(I())
	(mi, ans) = (1000006, 0)
	for i in range(n - 1, -1, -1):
		if l[i] > mi:
			ans += 1
		mi = min(mi, l[i])
	print(ans)


t = int(input())
for _ in range(t):
	n = int(input())
	ar = list(map(int, input().split()))
	mn = 1000000000
	count = 0
	for i in range(0, n):
		el = ar[n - i - 1]
		if el > mn:
			count += 1
		mn = min(mn, el)
	print(count)


t = int(input())
for nt in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	m = l[-1]
	count = 0
	for i in range(n - 2, -1, -1):
		if l[i] > m:
			count += 1
		else:
			m = l[i]
	print(count)


for _ in range(int(input())):
	count = 0
	n = int(input())
	a = list(map(int, input().split()))
	x = max(a)
	for i in range(n - 1, -1, -1):
		if a[i] > x:
			count += 1
		x = min(x, a[i])
	print(count)


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	dp = [None] * n
	dp[n - 1] = l[n - 1]
	for i in range(n - 2, -1, -1):
		dp[i] = min(dp[i + 1], l[i])
	c = 0
	for i in range(n):
		if l[i] > dp[i]:
			c += 1
	print(c)


from sys import stdin

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

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

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

def si():
	return stdin.readline()
for _ in range(ii()):
	n = ii()
	s = [i for i in range(n)]
	a = li()
	a = list(zip(a, s))
	a.sort()
	(a, s) = zip(*a)
	x = y = p = 0
	for i in range(n):
		if s[i] > x:
			p += s[i] - x - y
			x = s[i]
			if x == n - 1:
				break
		y = 1
	print(p)


def main():
	t = int(input())
	for _ in range(t):
		n = int(input())
		prices = [int(elem) for elem in input().split()]
		counter = 0
		minimum = 10000001
		for price in reversed(prices):
			if price <= minimum:
				minimum = price
			else:
				counter += 1
		print(counter)
main()


def answer():
	n = int(input())
	a_list = [int(elm) for elm in input().split(' ')]
	count = 0
	min_ = a_list[n - 1]
	for i in range(n - 1, -1, -1):
		if a_list[i] > min_:
			count += 1
		min_ = min(a_list[i], min_)
	return count
nn = int(input())
for _ in range(nn):
	out = answer()
	print(out)


t = int(input())
all = []
ans = []
for i in range(t):
	n = int(input())
	a = [int(i) for i in input().split()]
	all.append(a)
for i in range(t):
	m = 0
	all[i].reverse()
	minprice = all[i][0]
	for j in all[i]:
		if minprice > j:
			minprice = j
		if j > minprice:
			m += 1
	ans.append(m)
[print(i) for i in ans]


import math
y = int(input())
for x in range(y):
	contador = 0
	n = int(input())
	row = input().split(' ')
	if n == 1:
		print('0')
	else:
		minimo = int(row[n - 1])
		for y in reversed(row):
			if int(y) > minimo:
				contador = contador + 1
			else:
				minimo = int(y)
		print(contador)


import os
ans = list()
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	m = a[n - 1]
	count = 0
	for i in range(2, n + 1):
		if a[-i] > m:
			count += 1
		m = min(m, a[-i])
	ans.append(count)
for i in range(len(ans)):
	print(ans[i])


t = int(input())
for _ in range(t):
	n = int(input())
	nos = list(map(int, input().split(' ')))
	bad = 0
	if n == 1:
		print(0)
		continue
	mini = nos[-1]
	for num in reversed(nos):
		if mini < num:
			bad += 1
		else:
			mini = num
	print(bad)


t = int(input())
for _ in range(t):
	days = int(input())
	price = list(map(int, input().split()))
	price.reverse()
	min = price[0]
	bad = 0
	for i in range(days):
		if price[i] <= min:
			min = price[i]
		else:
			bad += 1
	print(bad)


t = input()
for p in range(int(t)):
	n = int(input())
	arr = list(map(int, input().split()))
	minafter = [0] * n
	mn = -1
	for i in range(len(arr) - 1, -1, -1):
		minafter[i] = mn
		if i == len(arr) - 1:
			mn = arr[i]
		else:
			mn = min(mn, arr[i])
	unusabledays = 0
	for i in range(len(arr) - 1):
		if minafter[i] < arr[i]:
			unusabledays += 1
	print(unusabledays)


t = int(input())
while t > 0:
	n = int(input())
	arr = [int(x) for x in input().split()]
	low = arr[n - 1]
	bad = 0
	for i in range(n - 2, -1, -1):
		if arr[i] > low:
			bad += 1
		low = min(low, arr[i])
	print(bad)
	t -= 1


t = int(input())
for i in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	max_l = max(l)
	c = 0
	for i in range(n - 1, -1, -1):
		if l[i] > max_l:
			c += 1
		max_l = min(l[i], max_l)
	print(c)


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

def output_list(v):
	print(' '.join((str(x) for x in v)))

def main():
	q = int(input())
	for _ in range(q):
		n = int(input())
		a = input_ints()
		ans = 0
		mi = 10 ** 9
		for x in a[::-1]:
			ans += x > mi
			mi = min(mi, x)
		print(ans)
main()


t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(x) for x in input().split()]
	a.reverse()
	mi = [n - 1]
	for i in range(1, len(a)):
		if a[i] <= a[n - 1 - mi[i - 1]]:
			mi.append(n - 1 - i)
		else:
			mi.append(mi[i - 1])
	mi.reverse()
	a.reverse()
	sum = 0
	p = 0
	while True:
		j = mi[p]
		if j == n - 1:
			sum += j - p
			break
		sum += j - p
		p = j + 1
	print(sum)


for _ in range(int(input())):
	n = int(input())
	a = [int(i) for i in input().split()]
	gre = a[-1]
	tot = 0
	for i in range(n - 1, -1, -1):
		if a[i] > gre:
			tot += 1
		gre = min(gre, a[i])
	print(tot)


for i in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	m = a[n - 1]
	b = 0
	for i in range(2, len(a) + 1):
		if a[-i] > m:
			b += 1
		m = min(m, a[-i])
	print(b)


test = int(input())
for i in range(test):
	day = int(input())
	prices = [int(i) for i in input().split()]
	min = 1000001
	bad = 0
	for j in range(day - 1, -1, -1):
		if prices[j] <= min:
			min = prices[j]
		else:
			bad += 1
	print(bad)


sets = int(input())
numbers = []
seqs = []
for x in range(sets):
	numbers.append(int(input()))
	seqs.append([int(y) for y in input().split()])
mini = 10 ** 10
mo = [0] * sets
for x in range(sets):
	for y in range(len(seqs[x])):
		if mini > seqs[x][len(seqs[x]) - 1 - y]:
			mini = seqs[x][len(seqs[x]) - 1 - y]
		elif mini < seqs[x][len(seqs[x]) - 1 - y]:
			mo[x] += 1
	mini = 10 ** 10
for x in range(len(mo)):
	print(mo[x])


import math
T = int(input())
while T:
	n = int(input())
	arr = list(map(int, input().split()))
	min_ = math.inf
	cnt = 0
	for i in range(n - 1, -1, -1):
		if arr[i] > min_:
			cnt += 1
		min_ = min(min_, arr[i])
	print(cnt)
	T -= 1


e = int(input())
for i in range(e):
	q = int(input())
	w = list(map(int, input().split()))
	w = w[::-1]
	k = w[0]
	l = 0
	for j in range(len(w)):
		if w[j] < k:
			k = w[j]
		elif w[j] > k:
			l += 1
	print(l)


import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
	n = int(input())
	A = list(map(int, input().split()))
	minArr = [1000007] * (n + 7)
	minSoFar = 1000007
	for i in range(n - 1, -1, -1):
		minSoFar = min(minSoFar, A[i])
		minArr[i] = minSoFar
	count = 0
	for i in range(n):
		if A[i] > minArr[i]:
			count += 1
	print(count)


import bisect

def solve(arr):
	cnt = 0
	mini = arr[-1]
	for i in range(len(arr) - 2, -1, -1):
		if arr[i] > mini:
			cnt += 1
		mini = min(arr[i], mini)
	return cnt
for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	print(solve(arr))


t = int(input())
for _ in range(t):
	n = int(input())
	lst = list(map(int, input().split(' ')))
	cnt = 0
	lst.reverse()
	MIN = lst[0]
	for i in lst:
		if MIN < i:
			cnt += 1
		else:
			MIN = i
	print(cnt)


t = int(input())
for j in range(t):
	n = int(input())
	A = [int(s) for s in input().split(' ')]
	num_bad_days = 0
	argsort = [i for (v, i) in sorted(((v, i) for (i, v) in enumerate(A)))]
	index = 0
	for i in argsort:
		if i >= index:
			num_bad_days += i - index
			index = i + 1
	print(num_bad_days)


import sys
input = lambda : sys.stdin.readline().strip()
t = int(input())
for i in range(t):
	n = int(input())
	ls = list(map(int, input().split()))
	res = []
	m = 10000000
	for i in range(n - 1, -1, -1):
		m = min(m, ls[i])
		res.append(m)
	res.reverse()
	cnt = 0
	for i in range(n):
		if res[i] != ls[i]:
			cnt += 1
	print(cnt)


import sys
allData = int(sys.stdin.readline())
n = []
arr = []
for i in range(allData):
	n.append(int(sys.stdin.readline()))
	arr.append([int(price) for price in sys.stdin.readline().split()])
answer = []
for sample in arr:
	minPrice = sample[-1]
	ans = 0
	for price in sample[-2::-1]:
		if price <= minPrice:
			minPrice = price
		else:
			ans += 1
	answer.append(ans)
for i in answer:
	print(i)


import sys
t = int(input())
for i in range(t):
	n = int(input())
	an = list(map(int, sys.stdin.readline().split()))
	m = an[-1]
	ans = 0
	for j in range(n - 1, -1, -1):
		if an[j] > m:
			ans += 1
		else:
			m = an[j]
	print(ans)


q = int(input())
for i in range(q):
	n = int(input())
	array = [int(x) for x in input().split()]
	min = array[-1]
	count = 0
	for i in reversed(array):
		if i <= min:
			min = i
		else:
			count += 1
	print(count)


for _ in range(int(input())):
	n = int(input())
	A = [int(i) for i in input().split()]
	maxi = [0] * n
	maxi[n - 1] = A[n - 1]
	for i in range(n - 2, -1, -1):
		maxi[i] = min(maxi[i + 1], A[i])
	cnt1 = 0
	for i in range(n - 1):
		if A[i] > maxi[i + 1]:
			cnt1 += 1
	print(cnt1)


for _ in range(int(input())):
	p = 0
	input()
	a = list(map(int, input().split()))[::-1]
	x = a[0]
	for i in a:
		if i > x:
			p += 1
		else:
			x = i
	print(p)


from bisect import bisect_left as lower_bound, bisect_right as upper_bound

def solve(arr, n):
	lo = arr[-1]
	cnt = 0
	for i in range(n - 2, -1, -1):
		if arr[i] > lo:
			cnt += 1
		lo = min(lo, arr[i])
	return cnt

def main():
	t = int(input())
	for _ in range(t):
		n = int(input())
		arr = list(map(int, input().strip().split()))
		print(solve(arr, n))
main()


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	m = a[-1]
	res = 0
	for __ in a[:-1][::-1]:
		if __ <= m:
			m = __
		else:
			res += 1
	print(res)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = a[:]
	a.sort()
	d = {}
	for i in a:
		if i in d:
			d[i] += 1
		else:
			d[i] = 1
	count = 0
	k = 0
	for i in range(len(b) - 1):
		if b[i] > a[k]:
			count += 1
			d[b[i]] -= 1
		elif b[i] == a[k]:
			d[b[i]] -= 1
			k += 1
			while d[a[k]] == 0:
				k += 1
	print(count)


def problem():
	T = int(input())
	while T:
		n = int(input())
		arr = [int(x) for x in input().split(' ')]
		temp_arr = []
		_len = len(arr)
		window_min = arr[_len - 1] + 1
		for i in range(_len):
			if arr[_len - i - 1] < window_min:
				window_min = arr[_len - i - 1]
			temp_arr.append(window_min)
		_count = 0
		for i in range(_len):
			if arr[i] > temp_arr[_len - i - 1]:
				_count += 1
		print(_count)
		T -= 1
problem()


t = int(input())
for _ in range(t):
	size = int(input())
	li = list(map(int, input().split()))
	li = li[::-1]
	ans = 0
	m = 99999999
	for i in li:
		if i > m:
			ans += 1
		m = min(m, i)
	print(ans)


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


t = int(input())
results = []
for i in range(t):
	n = int(input())
	item_list = input().split()
	items = []
	for item in item_list:
		items.append(int(item))
	count = 0
	items = list(reversed(items))
	min = items[0]
	for index in range(len(items) - 1):
		if min < items[index + 1]:
			count += 1
		else:
			min = items[index + 1]
	results.append(count)
for i in results:
	print(i)


import heapq
num_cases = int(input())
for case in range(1, num_cases + 1):
	days = int(input())
	prices = [int(p) for p in input().split(' ')]
	cnt = 0
	temp = [prices[-1]]
	heapq.heapify(temp)
	for i in range(len(prices) - 2, -1, -1):
		heapq.heappush(temp, prices[i])
		if prices[i] > temp[0]:
			cnt += 1
	print(cnt)


N = int(input())
for itr in range(N):
	n = int(input())
	str = input()
	lst = list()
	lst = str.split()
	count = 0
	min = int(lst[-1])
	for i in range(n - 2, -1, -1):
		if int(lst[i]) > int(min):
			count = count + 1
		if int(lst[i]) < int(min):
			min = int(lst[i])
	print(count)


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

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

def num():
	return int(input())
from itertools import repeat, accumulate as acc
t = num()
for _ in repeat(None, t):
	n = num()
	a = list(line())[::-1]
	b = acc(a, min)
	ans = sum((q > w for (q, w) in zip(a[1:], b)))
	print(ans)


t = int(input())
for q in range(t):
	n = int(input())
	p = list(map(int, input().split()))
	prefixes = [0] * n
	prev_min = p[n - 1]
	for i in range(n - 1, -1, -1):
		if p[i] < prev_min:
			prev_min = p[i]
		prefixes[i] = prev_min
	cnt = 0
	for i in range(n):
		if p[i] > prefixes[i]:
			cnt += 1
	print(cnt)


def BadPrices(l, n):
	prices = [[l[i], i] for i in range(0, n)]
	prices = sorted(prices)
	max_i = prices[0][1]
	baddays = 0
	for i in range(1, n):
		if prices[i][1] > max_i:
			max_i = prices[i][1]
		else:
			baddays += 1
	print(baddays)
n = int(input())
for i in range(0, n):
	n_ = int(input())
	BadPrices(list(map(int, input().rstrip().split())), n_)


for _ in range(int(input())):
	n = int(input())
	data = [int(i) for i in input().split()]
	m = data[-1]
	ans = 0
	for i in range(n - 1, -1, -1):
		if data[i] > m:
			ans += 1
		else:
			m = data[i]
	print(ans)


datasets = int(input())
for _ in range(datasets):
	n = int(input())
	prices = [int(x) for x in input().split()]
	min_price = None
	bad_days = 0
	for (i, price) in enumerate(reversed(prices)):
		if min_price is None:
			min_price = price
			continue
		if price > min_price:
			bad_days += 1
		elif price < min_price:
			min_price = price
	print(bad_days)


import sys
t = int(sys.stdin.readline())
ans_arr = []
for e in range(t):
	n = int(sys.stdin.readline())
	a = [int(i) for i in sys.stdin.readline().split()]
	mini = a[-1]
	count = 0
	for g in range(n - 1, -1, -1):
		if mini < a[g]:
			count += 1
		mini = min(a[g], mini)
	ans_arr.append(str(count))
print('\n'.join(ans_arr))


t = int(input())
for j in range(t):
	n = int(input())
	array = [int(x) for x in input().split()]
	min = array[-1]
	count = 0
	for x in reversed(array):
		if x > min:
			count += 1
		elif x < min:
			min = x
	print(count)


for _ in range(int(input())):
	n = int(input())
	prices = list(map(int, input().split()))
	badPrices = 0
	minPrice = float('inf')
	for price in prices[::-1]:
		if price > minPrice:
			badPrices += 1
		else:
			minPrice = price
	print(badPrices)


t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(x) for x in input().split()]
	c = a[-1]
	ans = 0
	for i in range(1, n):
		if a[n - 1 - i] > c:
			ans += 1
		c = min(c, a[n - 1 - i])
	print(ans)


from sys import stdin
l = stdin.read().splitlines()[2::2]
for (i, s) in enumerate(l):
	(p, c) = (10000000.0, 0)
	for q in map(float, s.split()[::-1]):
		if p < q:
			c += 1
		else:
			p = q
	l[i] = str(c)
print('\n'.join(l))


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


t = int(input())
for i in range(t):
	a = int(input())
	b = list(map(int, input().strip().split()))
	c = b[-1]
	e = 0
	for j in range(a - 1, -1, -1):
		if b[j] > c:
			e = e + 1
		else:
			c = min(b[j], c)
	print(e)


t = int(input())
for i in range(t):
	n = int(input())
	s = input().split(' ')
	for j in range(len(s)):
		s[j] = int(s[j])
	minm = 10000000000
	count = 0
	k = len(s) - 1
	while k >= 0:
		if s[k] > minm:
			count += 1
		elif s[k] < minm:
			minm = s[k]
		k -= 1
	print(count)


q = int(input())
for t in range(q):
	n = int(input())
	cost = list(map(int, input().split()))
	lo = cost[-1]
	bad = 0
	for i in cost[::-1]:
		if i < lo:
			lo = i
		if i > lo:
			bad += 1
	print(bad)


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


t = int(input())
for _ in range(t):
	n = int(input())
	array = list(map(int, input().split()))
	low = array[-1]
	count = 0
	for i in range(n - 2, -1, -1):
		if array[i] > low:
			count += 1
		elif array[i] < low:
			low = array[i]
	print(count)


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


for i in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	(r, q) = (0, a[-1])
	for i in range(n - 2, -1, -1):
		if a[i] <= q:
			q = a[i]
		else:
			r += 1
	print(r)


q = int(input())
for _ in range(q):
	n = int(input())
	a = list(map(int, input().split()))
	l = 0
	b = []
	for i in range(n):
		while b != [] and b[-1] > a[i]:
			w = b.pop()
		b.append(a[i])
	print(n - len(b))


t = int(input())
for _ in range(t):
	n = int(input())
	nums = [int(x) for x in input().strip().split()]
	count = 0
	min = nums[-1]
	for x in nums[-1::-1]:
		if x < min:
			min = x
		elif x > min:
			count += 1
	print(count)


t = int(input())
while t != 0:
	n = int(input())
	ara = []
	s1 = input()
	for data in s1.split():
		ara.append(int(data))
	yo = ara[n - 1]
	Count = 0
	for i in range(n - 2, -1, -1):
		if ara[i] > yo:
			Count += 1
		else:
			yo = ara[i]
	print(Count)
	t -= 1


t = int(input())
for j in range(t):
	n = int(input())
	l = list([int(x) for x in input().split()])
	min = l[n - 1]
	i = n - 2
	c = 0
	while i >= 0:
		if l[i] > min:
			c += 1
		else:
			min = l[i]
		i -= 1
	print(c)


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


test = int(input())
for t in range(test):
	n = int(input())
	l = [int(i) for i in input().split(' ')]
	c = 0
	for i in range(len(l) - 2, -1, -1):
		if l[i] > l[i + 1]:
			c += 1
			(l[i], l[i + 1]) = (l[i + 1], l[i])
	print(c)


t = int(input())
while t:
	n = int(input())
	l = list(map(int, input().strip().split()))
	count = 0
	min = l[n - 1]
	for i in range(n - 2, -1, -1):
		if l[i] > min:
			count += 1
		else:
			min = l[i]
	print(count)
	t -= 1


import sys
import copy
sys.setrecursionlimit(1000000)
T = int(input())
for _ in range(T):
	n = int(input())
	a = [int(x) for x in input().split()]
	a.reverse()
	minV = a[0]
	ans = 0
	for i in range(1, n):
		if a[i] > minV:
			ans += 1
		minV = min(minV, a[i])
	print(ans)


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	k = []
	for i in l:
		if k == []:
			k.append(i)
		elif k[-1] <= i:
			k.append(i)
		else:
			while k != []:
				if k[-1] > i:
					k.pop()
				else:
					break
			k.append(i)
	print(n - len(k))


n = int(input())

def BadPrice(arr, n):
	ans = 0
	pause = arr[0]
	index = 0
	for i in range(0, n):
		if pause < arr[i] and index > i:
			ans += 1
			continue
		for j in range(i, n):
			if arr[j] < arr[i]:
				pause = arr[j]
				index = j
				ans += 1
				break
	print(ans)
for i in range(n):
	N = int(input())
	arr = [int(j) for j in input().split()]
	right_min = 99999999999
	ans = 0
	for i in range(N - 1, -1, -1):
		if arr[i] > right_min:
			ans += 1
		right_min = min(right_min, arr[i])
	print(ans)


import sys
import itertools
t = None
for (c, line) in zip(itertools.count(), sys.stdin):
	if c == 0:
		t = int(line)
	elif c - 1 > t * 2:
		break
	elif (c - 1) % 2 == 0:
		continue
	else:
		prices = list(map(int, line.split(' ')))
		count = 0
		l = len(prices)
		minimum = prices[-1]
		for i in reversed(range(l - 1)):
			if prices[i] > minimum:
				count += 1
			else:
				minimum = prices[i]
		print(count)


n = int(input())
for i in range(n):
	t = int(input())
	a = list(map(int, input().split()))
	a.reverse()
	x = a[0]
	cnt = 0
	for j in a[1:]:
		if j > x:
			cnt += 1
		x = min(x, j)
	print(cnt)


t = int(input())
while t:
	n = int(input())
	a = list(map(int, str(input()).split()))
	small = 10000000
	res = 0
	for i in range(n - 1, -1, -1):
		if a[i] <= small:
			small = a[i]
		else:
			res = res + 1
	print(res)
	t = t - 1


import sys
T = int(input().strip())
while T > 0:
	T -= 1
	n = int(input().strip())
	array = list(map(int, input().strip().split()))
	minx = 2147483647
	count = 0
	for i in range(n - 1, -1, -1):
		if array[i] > minx:
			count += 1
		elif array[i] < minx:
			minx = array[i]
	print(count)


import io, os
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
ii = lambda : int(input())
kk = lambda : map(int, input().split())
ll = lambda : list(kk())
for _ in range(ii()):
	(i, ls) = (ii(), reversed(ll()))
	(minv, c) = (10 ** 7, 0)
	for l in ls:
		if l < minv:
			minv = l
		elif l > minv:
			c += 1
	print(c)


r = ''
for i in range(int(input())):
	n = int(input())
	s = list(map(int, input().split()))
	e = 10 ** 9
	c = 0
	for i in range(n - 1, -1, -1):
		if s[i] <= e:
			e = s[i]
		else:
			c += 1
	r += str(c) + '\n'
print(r)


import sys
import random
input = sys.stdin.readline
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	b = []
	for i in range(n):
		b.append((a[i], i))
	b.sort()
	k = b[0][1]
	ans = 0
	for j in range(1, n):
		if b[j][1] > k:
			k = b[j][1]
		else:
			ans += 1
	print(ans)


t = int(input())
for j in range(t):
	n = int(input())
	s = list(map(int, input().split()))
	minx = 1000000000
	(q, i) = (0, n - 1)
	while i >= 0:
		if s[i] <= minx:
			minx = s[i]
		else:
			q += 1
		i -= 1
	print(q)


t = int(input())
for t1 in range(0, t):
	n = int(input())
	l = list(map(int, input().split()))
	mi = pow(10, 9)
	mx = -l[0]
	c = 0
	c1 = 0
	for i in range(n - 1, -1, -1):
		if l[i] > mi:
			c = c + 1
		mi = min(mi, l[i])
	print(c)


for _ in range(0, int(input())):
	n = int(input())
	l = [int(x) for x in input().split()]
	minp = l[n - 1]
	a = 0
	for i in range(n - 1, -1, -1):
		if minp < l[i]:
			a += 1
		minp = min(minp, l[i])
	print(a)


for _ in range(int(input())):
	input()
	c = 0
	l = [int(x) for x in input().split()][::-1]
	m = l[0]
	for i in l:
		if m > i:
			m = i
		elif i > m:
			c += 1
	print(c)


numbers = int(input())
for i in range(numbers):
	bad = int(input())
	lista = input().split(' ')
	menor = int(lista[bad - 1])
	menores = 0
	pos = bad - 2
	while pos >= 0:
		if menor >= int(lista[pos]):
			menor = int(lista[pos])
		else:
			menores = menores + 1
		pos = pos - 1
	print(menores)


n = int(input())
for i in range(n):
	l = int(input())
	s = list(map(int, input().split()))
	ma = 10 ** 6 + 1
	ans = 0
	for j in range(l - 1, -1, -1):
		if s[j] != min(s[j], ma):
			ans += 1
		ma = min(s[j], ma)
	print(ans)


for _ in range(int(input())):
	n = int(input())
	lis = list(map(int, input().split()))
	mi = lis[-1]
	c = 0
	for i in range(n - 2, -1, -1):
		if lis[i] > mi:
			c += 1
		else:
			mi = lis[i]
	print(c)


t = int(input())
while t:
	n = int(input())
	arr = list(map(int, input().rstrip().split()))
	min_arr = [0] * n
	count = 0
	minm = arr[-1]
	for i in range(len(arr) - 2, -1, -1):
		minm = min(minm, arr[i])
		if arr[i] > minm:
			count += 1
			min_arr[i] = count
	print(max(min_arr))
	t = t - 1


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	k = len(a) - 1
	minimum = a[k]
	count = 0
	j = k - 1
	while j >= 0:
		if a[j] < minimum:
			minimum = a[j]
		if a[j] > minimum:
			count += 1
		j -= 1
	print(count)


for _ in range(int(input())):
	x = int(input())
	y = list(map(int, input().split()))
	min = y[-1]
	ans = 0
	for i in range(x - 1, 0, -1):
		if y[i] - y[i - 1] < 0:
			ans = ans + 1
		elif y[i - 1] <= min:
			min = y[i - 1]
		else:
			ans = ans + 1
	print(ans)


q = int(input())
for i in range(q):
	n = int(input())
	a = list(map(int, input().split()))
	t = a[-1]
	ans = 0
	for j in range(2, n + 1):
		if t >= a[-j]:
			t = a[-j]
		else:
			ans += 1
	print(ans)


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()
		c = a[-1]
		r = 0
		for t in a[:-1][::-1]:
			if t > c:
				r += 1
			else:
				c = t
		rr.append(r)
	return JA(rr, '\n')
print(main())


from bisect import *
from collections import *
from itertools import *
import functools
import sys
import math
from decimal import *
from copy import *
getcontext().prec = 30
MAX = sys.maxsize
MAXN = 10 ** 5 + 10
MOD = 10 ** 9 + 7

def isprime(n):
	n = abs(int(n))
	if n < 2:
		return False
	if n == 2:
		return True
	if not n & 1:
		return False
	for x in range(3, int(n ** 0.5) + 1, 2):
		if n % x == 0:
			return False
	return True

def mhd(a, b):
	return abs(a[0] - b[0]) + abs(b[1] - a[1])

def charIN(x=' '):
	return sys.stdin.readline().strip().split(x)

def arrIN(x=' '):
	return list(map(int, sys.stdin.readline().strip().split(x)))

def eld(x, y):
	a = y[0] - x[0]
	b = x[1] - y[1]
	return (a * a + b * b) ** 0.5

def lgcd(a):
	g = a[0]
	for i in range(1, len(a)):
		g = math.gcd(g, a[i])
	return g

def ms(a):
	msf = -MAX
	meh = 0
	st = en = be = 0
	for i in range(len(a)):
		meh += a[i]
		if msf < meh:
			msf = meh
			st = be
			en = i
		if meh < 0:
			meh = 0
			be = i + 1
	return (msf, st, en)

def ncr(n, r):
	num = den = 1
	for i in range(r):
		num = num * (n - i) % MOD
		den = den * (i + 1) % MOD
	return num * pow(den, MOD - 2, MOD) % MOD

def flush():
	return sys.stdout.flush()

def fac(n):
	ans = 1
	for i in range(1, n + 1):
		ans *= i
		ans %= MOD
	return ans
for _ in range(int(input())):
	n = int(input())
	a = arrIN()
	x = [MAX] * n
	x[-1] = a[-1]
	for i in range(n - 2, -1, -1):
		x[i] = min(x[i + 1], a[i])
	ans = 0
	for i in range(n - 1):
		if x[i + 1] < a[i]:
			ans += 1
	print(ans)


from sys import stdin, stdout
input = stdin.readline
for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	m = arr[-1]
	cnt = 0
	for i in range(n - 2, -1, -1):
		if arr[i] > m:
			cnt += 1
		if arr[i] <= m:
			m = arr[i]
	print(cnt)


import sys
input = sys.stdin.readline
Q = int(input())
Query = []
for _ in range(Q):
	N = int(input())
	A = list(map(int, input().split()))
	Query.append((N, A))
for (N, A) in Query:
	c = 0
	m = 10 ** 16
	for i in reversed(range(N)):
		if A[i] <= m:
			c += 1
		m = min(m, A[i])
	print(N - c)


t = int(input())
for _ in range(t):
	n = int(input())
	array = list(map(int, input().split()))
	count = 0
	array.reverse()
	temp = array[0]
	for i in range(1, len(array)):
		if array[i] > temp:
			count = count + 1
		else:
			temp = array[i]
	print(count)


T = int(input())
ans = []
from itertools import accumulate
for t in range(T):
	n = int(input())
	t_list = list(map(int, input().split()))
	last = list(accumulate(reversed(t_list), min))
	cnt = 0
	for i in range(n):
		if t_list[n - 1 - i] > last[i]:
			cnt += 1
	ans.append(cnt)
for a in ans:
	print(a)


test = int(input())
while test:
	n = int(input())
	arr = list(map(int, input().split()))
	res = 0
	mn = arr[-1]
	for i in range(n - 1, -1, -1):
		if arr[i] > mn:
			res += 1
		else:
			mn = arr[i]
	print(res)
	test -= 1


w = int(input())
for x in range(w):
	c = 0
	l = int(input())
	d = [int(x) for x in input().split(' ')]
	min_p = d[-1]
	i = l - 2
	while i >= 0:
		if d[i] <= min_p:
			min_p = d[i]
		else:
			c += 1
		i -= 1
	print(c)


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


for i in range(int(input())):
	n = int(input())
	lst = list(map(int, input().split()))
	c = 0
	m = lst[-1]
	for j in range(n):
		if lst[~j] > m:
			c += 1
		else:
			m = min(m, lst[~j])
	print(c)


t = int(input())
day = []
cost = []
bad = 0
for i in range(t):
	day = int(input())
	cost = [int(i) for i in input().split()]
	cost.reverse()
	min = cost[0]
	for j in range(len(cost)):
		if min < cost[j]:
			bad += 1
		elif min > cost[j]:
			min = cost[j]
	print(bad)
	bad = 0


t = int(input())
import math
for x in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	tarr = [0 for i in range(n + 1)]
	tarr[n] = math.inf
	for i in range(n - 1, -1, -1):
		tarr[i] = min(arr[i], tarr[i + 1])
	sol = 0
	for i in range(n):
		if arr[i] > tarr[i + 1]:
			sol += 1
	print(sol)


t = int(input())
for ti in range(t):
	input()
	s = 0
	m = 4294967295
	li = [int(i) for i in input().split(' ')]
	li.reverse()
	for i in li:
		if i <= m:
			m = i
		else:
			s += 1
	print(s)


for _ in range(int(input())):
	N = int(input())
	min_p = 10 ** 10
	ans = 0
	for i in list(map(int, input().split()))[::-1]:
		if i < min_p:
			min_p = i
		elif i > min_p:
			ans += 1
	print(ans)


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


import os
import sys
from io import BytesIO, IOBase

def solution(arr, n):
	if n == 1:
		write(0)
		return
	min_arr = [0] * n
	for i in range(n - 1, -1, -1):
		if i == n - 1:
			min_arr[i] = arr[i]
		else:
			min_arr[i] = min(arr[i], min_arr[i + 1])
	write(sum((1 for (a, b) in zip(arr, min_arr) if a > b)))

def main():
	for _ in range(r_int()):
		n = r_int()
		arr = r_array()
		solution(arr, n)
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')

def write(*args, end='\n'):
	for x in args:
		sys.stdout.write(str(x) + ' ')
	sys.stdout.write(end)

def r_array():
	return [int(x) for x in input().split()]

def r_int():
	return int(input())

def w_array(arr):
	for x in arr:
		write(x, end=' ')
	write()
main()


def solve():
	n = int(input())
	a = list(map(int, input().split()))
	coun = 0
	mn = 10 ** 6 + 5
	for i in range(n - 1, -1, -1):
		mn = min(mn, a[i])
		if a[i] > mn:
			coun += 1
	print(coun)
t = int(input())
for i in range(t):
	solve()


T = int(input())
for _ in range(T):
	n = int(input())
	a = list(map(int, input().split()))
	a.reverse()
	ma = a[0]
	ans = 0
	for i in range(n):
		if ma < a[i]:
			ans += 1
		else:
			ma = min(ma, a[i])
	print(ans)


def process(A, n):
	right = 1e+30
	res = 0
	for i in reversed(A):
		if i > right:
			res = res + 1
		right = min(i, right)
	return res
t = int(input())
for i in range(t):
	n = int(input())
	A = [int(x) for x in input().split()]
	print(process(A, n))


t = int(input())
for _ in range(t):
	n = int(input())
	s = [int(i) for i in input().split()]
	s.reverse()
	m = s[0]
	s.pop(0)
	r = []
	for i in s:
		if i > m:
			r.append(i)
		if i < m:
			m = i
	print(len(r))


n = int(input())
for i in range(n):
	k = int(input())
	s = str(input())
	a = [int(i) for i in s.split()]
	min_n = 100000000000
	ans = 0
	for i in range(len(a) - 1, -1, -1):
		if a[i] <= min_n:
			min_n = a[i]
		else:
			ans += 1
	print(ans)


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	m = 10 ** 9
	ans = 0
	for i in range(n):
		if m < a[n - 1 - i]:
			ans += 1
		m = min(m, a[n - 1 - i])
	print(ans)


a = int(input())
for i in range(a):
	b = int(input())
	info = [int(a) for a in input().split()]
	otv = 0
	minim = 10000001
	for i in range(b - 1, -1, -1):
		if info[i] <= minim:
			minim = info[i]
		else:
			otv += 1
	print(otv)


for _ in range(int(input())):
	a = int(input())
	b = list(map(int, input().split()))
	minny = 100000000000000000
	score = 0
	for i in range(1, len(b) + 1):
		if b[-i] <= minny:
			minny = b[-i]
		else:
			score += 1
	print(score)


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


import heapq
q = int(input())
for i in range(q):
	n = int(input())
	l = list(map(int, input().split(' ')))
	cnt = 0
	l = l[::-1]
	mini = l[0]
	for i in l:
		if i < mini:
			mini = i
		if mini != i:
			cnt = cnt + 1
	print(cnt)


for _ in range(int(input())):
	n = int(input())
	x = [int(n) for n in input().split()]
	m = [0] * n
	mx = 10 ** 6 + 1
	c = 0
	for i in range(len(x) - 1, -1, -1):
		mx = min(mx, x[i])
		m[i] = mx
	for i in range(len(x)):
		if x[i] > m[i]:
			c += 1
	print(c)


import sys
import collections
t = int(sys.stdin.readline().split()[0])
for test_data in range(t):
	bad_days = 0
	n = int(sys.stdin.readline().split()[0])
	days = sys.stdin.readline().split()
	for i in range(n):
		days[i] = int(days[i])
	minimum = days[n - 1]
	for i in range(n - 1, -1, -1):
		if minimum < days[i]:
			bad_days += 1
		minimum = min(minimum, days[i])
	sys.stdout.write(str(bad_days) + '\n')


from bisect import *
for _ in range(int(input())):
	conj = []
	qtd = int(input())
	vals = [int(x) for x in input().split()]
	lenConj = 0
	for val in vals:
		insort_left(conj, val)
		lenConj += 1
		while conj[-1] > val:
			conj.pop()
			lenConj -= 1
	print(qtd - lenConj)


for i in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	minprice = a[-1]
	answer = 0
	for i in range(n - 2, -1, -1):
		if a[i] > minprice:
			answer += 1
		else:
			minprice = a[i]
	print(answer)


t = int(input())
for x in range(t):
	a = int(input())
	b = list(map(int, input().split()))
	c = b[a - 1]
	d = 0
	for x in range(a - 2, -1, -1):
		if b[x] > c:
			d = d + 1
		else:
			c = b[x]
	print(d)


for _ in range(int(input())):
	n = int(input())
	arr = [int(i) for i in input().split()]
	check = arr[-1]
	ans = 0
	for i in range(n - 2, -1, -1):
		if arr[i] > check:
			ans += 1
		else:
			check = arr[i]
	print(ans)


import math
t = int(input())
for _ in range(t):
	days = 0
	min = math.inf
	n = int(input())
	arr = [int(x) for x in input().split()]
	for i in range(len(arr)):
		if arr[n - i - 1] > min:
			days += 1
		if arr[n - i - 1] < min:
			min = arr[n - i - 1]
	print(days)


n = int(input())
for i in range(n):
	m = int(input())
	l = list(map(int, input().split()))
	l.reverse()
	min = l[0]
	count = 0
	for a in l:
		if a > min:
			count += 1
		elif a < min:
			min = a
	print(count)


t = int(input())
while t > 0:
	t -= 1
	n = int(input())
	n_arr = list(map(int, input().split()))
	dp = n_arr.copy()
	dp[n - 1] = n_arr[n - 1]
	for i in range(n - 2, -1, -1):
		dp[i] = min(dp[i], dp[i + 1])
	bad_days = 0
	for i in range(n):
		if n_arr[i] > dp[i]:
			bad_days += 1
	print(bad_days)


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


from sys import stdin, stdout
T = int(stdin.readline())
for TT in range(T):
	n = int(stdin.readline())
	x = list(map(int, stdin.readline().split()))
	mi = 10000000
	ans = 0
	for i in range(n - 1, -1, -1):
		if x[i] > mi:
			ans += 1
		mi = min(mi, x[i])
	stdout.write('%d\n' % ans)


t = int(input())
for i in range(t):
	k = int(input())
	a = [*map(int, input().split())]
	mv = 10 ** 9
	count = 0
	for x in reversed(a):
		if x > mv:
			count += 1
		mv = min(mv, x)
	print(count)


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()))
it = lambda : tuple(map(int, input().split()))
ls = lambda : list(input())
t = ii()
for _ in range(t):
	n = ii()
	l = il()
	mn = l[-1]
	c = 0
	for i in range(n - 1, -1, -1):
		if l[i] > mn:
			c += 1
		mn = min(mn, l[i])
	print(c)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	cnt = 0
	l1 = [0] * n
	l1[n - 1] = a[n - 1]
	for i in range(n - 2, -1, -1):
		l1[i] = min(a[i], l1[i + 1])
	for i in range(n):
		if l1[i] != a[i]:
			cnt += 1
	print(cnt)


t = int(input())
while t:
	n = int(input())
	a = input().split()
	b = [0 for i in range(n)]
	min = 10 ** 6 + 1
	for i in range(n):
		a[i] = int(a[i])
	for j in range(n):
		if a[n - 1 - j] < min:
			b[n - 1 - j] = a[n - 1 - j]
			min = a[n - 1 - j]
		else:
			b[n - 1 - j] = min
	count = 0
	for k in range(n):
		if a[k] > b[k]:
			count += 1
	print(count)
	t -= 1


import sys
input = sys.stdin.readline

def main():
	n = int(input())
	for _ in range(n):
		count = 0
		min_ = 10 ** 6 + 1
		d = int(input())
		nums = list(map(int, input().split()))
		for x in reversed(range(len(nums))):
			min_ = min(min_, nums[x])
			if nums[x] > min_:
				count += 1
		print(count)
main()


def main():

	def solve():
		n = int(input())
		aa = list(map(int, input().split()))
		min = 1000000
		bad = 0
		for a in reversed(aa):
			if a < min:
				min = a
			elif a > min:
				bad += 1
		print(bad)
	q = int(input())
	for _ in range(q):
		solve()
main()


for i in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	c = []
	for i in range(n):
		c.append([a[i], i])

	def sortFirst(val):
		return val[0]
	c.sort(key=sortFirst)
	curr_max = -1
	ans = 0
	for i in range(n):
		if c[i][1] > curr_max:
			curr_max = c[i][1]
		else:
			ans += 1
	print(ans)


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	mn = 10 ** 9
	ans = 0
	for x in reversed(a):
		if x > mn:
			ans += 1
		mn = min(mn, x)
	print(ans)


n = int(input())
for _ in range(n):
	a = int(input())
	b = list(map(int, input().split()))[::-1]
	(z, q) = (b[0], 0)
	for i in b:
		if i > z:
			q += 1
		else:
			z = i
	print(q)


t = int(input())
li = []
while t > 0:
	n = int(input())
	m = list(map(int, input().split()))
	w = 0
	l = m[n - 1]
	for i in range(n - 1):
		if l < m[n - i - 2]:
			w = w + 1
		else:
			l = m[n - i - 2]
	li.append(w)
	t = t - 1
for h in range(len(li)):
	print(li[h])


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()))
	a.reverse()
	ans = 0
	m = 10 ** 10
	for i in range(n):
		if m < a[i]:
			ans += 1
		m = min(m, a[i])
	print(ans)


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


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	bad = 0
	pro = [arr[-1]] * n
	for i in range(len(arr) - 2, -1, -1):
		pro[i] = min(arr[i], pro[i + 1])
		if arr[i] > pro[i]:
			bad += 1
	print(bad)


t = int(input())
for j in range(t):
	n = int(input())
	l1 = list(map(int, input().split()))
	m = l1[-1]
	i = len(l1) - 2
	ans = 0
	while i != -1:
		if l1[i] <= m:
			ans += 1
			m = l1[i]
		i -= 1
	print(len(l1) - ans - 1)


import math
import sys
from heapq import heappop
from heapq import heappush
from heapq import heapify
from bisect import insort
from sys import stdin, stdout
from collections import defaultdict
inp = lambda : int(stdin.readline())
sip = lambda : input()
mulip = lambda : map(int, input().split())
lst = lambda : list(map(int, stdin.readline().split()))
slst = lambda : list(sip())
arr2d = lambda x: [[int(j) for j in input().split()] for i in range(x)]
odds = lambda l: len(list(filter(lambda x: x % 2 != 0, l)))
evens = lambda l: len(list(filter(lambda x: x % 2 == 0, l)))
mod = pow(10, 9) + 7
for _ in range(inp()):
	n = inp()
	A = lst()
	cur = A[n - 1]
	cnt = 0
	for i in range(n - 2, -1, -1):
		if A[i] <= cur:
			cur = A[i]
		else:
			cnt += 1
	print(cnt)


t = int(input())
for k in range(t):
	n = int(input())
	arr = [int(x) for x in input().split()]
	(ans, m) = (0, arr[n - 1])
	for i in range(n - 2, -1, -1):
		if arr[i] > m:
			ans += 1
		if arr[i] < m:
			m = arr[i]
	print(ans)


t = int(input())
for i in range(t):
	n = int(input())
	l = input()
	l = list(map(int, l.split()))
	dp = list()
	for i in range(n):
		dp.append(0)
	min = l[n - 1]
	for j in range(n - 2, -1, -1):
		if l[j] > l[j + 1] or l[j] > min:
			dp[j] += 1
		dp[j] += dp[j + 1]
		if l[j] < min:
			min = l[j]
	print(dp[0])


t = int(input())
for _ in range(t):
	n = int(input())
	c = [int(t) for t in input().split(' ')]
	mcost = c[n - 1]
	bdays = 0
	for i in range(n - 2, -1, -1):
		if c[i] > mcost:
			bdays += 1
		mcost = min(mcost, c[i])
	print(bdays)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	suf = [0] * n
	m = a[-1]
	for i in range(len(a) - 1, -1, -1):
		suf[i] = m
		m = min(m, a[i])
	ans = 0
	for i in range(len(a) - 1):
		ans += suf[i] < a[i]
	print(ans)


t = int(input())
for test in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	smallest = 10 ** 7
	bad_days = 0
	for i in range(0, n):
		if l[n - i - 1] < smallest:
			smallest = l[n - i - 1]
		elif l[n - i - 1] > smallest:
			bad_days += 1
	print(bad_days)


T = int(input())
t = 0
while t < T:
	s = 0
	N = int(input())
	L = list(map(int, input().split()))
	x = len(L) - 1
	mn = L[x]
	while x != -1:
		if L[x] < mn:
			mn = L[x]
		if L[x] > mn:
			s += 1
		x -= 1
	t += 1
	print(s)


from sys import stdin
from copy import deepcopy
from collections import deque
t = int(stdin.readline().strip())
for _ in range(t):
	n = int(stdin.readline().strip())
	arr = list(map(int, stdin.readline().split()))
	small = arr[-1]
	ans = 0
	for i in range(n - 2, -1, -1):
		if arr[i] > small:
			ans += 1
		small = min(arr[i], small)
	print(ans)


def ans(l, magi):
	fuck = 0
	i = len(l) - 1
	while i >= 0:
		if l[i] > magi:
			fuck += 1
		else:
			magi = l[i]
		i -= 1
	return fuck
t = int(input())
while t > 0:
	a = int(input())
	l = list(map(int, input().split()))
	bra = len(l)
	magi = l[bra - 1]
	print(ans(l, magi))
	t -= 1


def main():
	q = int(input())
	for x in range(q):
		n = int(input())
		tab = list(map(int, input().split()))
		mu = [0] * n
		i = n - 1
		cmu = 1000 * 1000 + 1
		while i >= 0:
			mu[i] = cmu
			cmu = min(cmu, tab[i])
			i -= 1
		nbb = 0
		for x in range(n):
			if mu[x] < tab[x]:
				nbb += 1
		print(nbb)
main()


t = int(input())
for t1 in range(t):
	n = int(input())
	data = list(map(int, input().split()))
	ans = 0
	min1 = 10 ** 6
	for i in range(n - 1, -1, -1):
		if data[i] > min1:
			ans += 1
		min1 = min(data[i], min1)
	print(ans)


from bisect import bisect_left
import sys
input = sys.stdin.readline
t = int(input())
ans = []
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	min_price = float('inf')
	cnt = 0
	for i in reversed(a):
		if min_price < i:
			cnt += 1
		else:
			min_price = i
	ans.append(cnt)
print(*ans, sep='\n')


for i in range(int(input())):
	input()
	L = list(map(int, input().split()))
	curMin = 1000001
	an = 0
	for i in range(len(L) - 1, -1, -1):
		if L[i] > curMin:
			an += 1
		curMin = min(curMin, L[i])
	print(an)


t = int(input())
for i in range(t):
	n = int(input())
	ans = 0
	a = [int(i) for i in input().split()]
	min_pref = [0] * n
	min_pref[0] = a[-1]
	for i in range(1, n):
		min_pref[i] = min(min_pref[i - 1], a[n - i - 1])
	for i in range(n):
		if a[i] > min_pref[n - i - 1]:
			ans += 1
	print(ans)


import collections, heapq, bisect, math

def gcd(a, b):
	if b == 0:
		return a
	return gcd(b, a % b)

def solve(A):
	curr_low = float('Inf')
	out = 0
	for i in range(len(A) - 1, -1, -1):
		if A[i] < curr_low:
			curr_low = A[i]
		if A[i] > curr_low:
			out += 1
	return out
q = input()
tests = []
for test in range(int(q)):
	n = input()
	tests.append([int(p) for p in input().split(' ')])
for test in tests:
	print(solve(test))


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


for rpt in range(int(input())):
	dcount = int(input())
	days = [int(x) for x in input().split(' ')]
	best = days[-1]
	res = 0
	for i in range(dcount - 1, -1, -1):
		if best < days[i]:
			res += 1
		else:
			best = days[i]
	print(res)


t = int(input())
for x in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	c = 0
	if n == 1:
		print(0)
		continue
	min = 1000001
	for i in reversed(range(n)):
		if a[i] > min:
			c += 1
		if a[i] < min:
			min = a[i]
	print(c)


for i in range(int(input())):
	N = int(input())
	A = [int(_) for _ in input().split()]
	count = 0
	flag = True
	l_i = -1
	j = 0
	S_A = sorted(A)
	dic = {}
	for p in range(N):
		dic[A[p]] = p
	while flag:
		value = S_A[j]
		index = dic[value]
		if index > l_i:
			count += index - l_i - 1
			if l_i == -1:
				count -= A[0:index].count(value)
			else:
				count -= A[l_i:index].count(value)
			l_i = index
		if index == N - 1:
			flag = False
		j += 1
	print(count)


t = int(input())
ans = []
for p in range(t):
	n = int(input())
	arr = [int(i) for i in input().split()]
	sth = arr[-1]
	cnt = 0
	for i in range(2, n + 1):
		if arr[n - i] <= sth:
			sth = arr[n - i]
		else:
			cnt += 1
	ans.append(cnt)
for _ in ans:
	print(_)


def main():
	t = int(input())
	while t > 0:
		t -= 1
		n = int(input())
		v = [int(x) for x in input().split()]
		mn = 1000000000
		ans = 0
		for i in range(n - 1, -1, -1):
			if v[i] > mn:
				ans += 1
			mn = min(mn, v[i])
		print(ans)
main()


def next_smaller(arr):
	res = []
	x = arr[::-1]
	stack = []
	for i in x:
		while stack and stack[-1] >= i:
			stack.pop()
		if stack:
			res.append(stack[-1])
		else:
			res.append(-1)
		stack.append(i)
	return sum((i != -1 for i in res))
tests = int(input())
for _ in range(tests):
	_ = input()
	data = [int(i) for i in input().split()]
	print(next_smaller(data))


import sys
from collections import defaultdict as dc
from collections import Counter
from bisect import bisect_right, bisect_left
import math
for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	p = [i for i in l]
	for i in range(n - 2, -1, -1):
		p[i] = min(p[i], p[i + 1])
	c = 0
	for i in range(n):
		if l[i] != p[i]:
			c += 1
	print(c)


for _ in range(int(input())):
	n = int(input())
	prices = list(map(int, input().split()))
	bad_prices = 0
	min_price = prices[n - 1]
	for i in range(n - 2, -1, -1):
		if prices[i] > min_price:
			bad_prices += 1
		else:
			min_price = min(prices[i], min_price)
	print(bad_prices)


for _ in range(int(input())):
	n = int(input())
	arr = [int(i) for i in input().split()]
	mi = 100000000
	bad = 0
	for j in range(n - 1, -1, -1):
		if arr[j] > mi:
			bad += 1
		mi = min(arr[j], mi)
	print(bad)


from sys import stdin, stdout
for _ in range(int(stdin.readline())):
	n = int(stdin.readline())
	a = list(map(int, stdin.readline().split()))
	mn = a[-1]
	ans = 0
	for i in range(n - 2, -1, -1):
		if a[i] > mn:
			ans += 1
		mn = min(mn, a[i])
	print(ans)


t = int(input())
n = 0
A = []
B = []
s = 0
f = 0
for i in range(t):
	n = int(input())
	B = input().split()
	for j in range(n):
		A.append(int(B[j]))
	A = A[::-1]
	min1 = A[0]
	s = 0
	for j in range(n):
		if A[j] > min1:
			s += 1
		else:
			min1 = A[j]
	A = []
	print(s)


from math import inf
t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(ai) for ai in input().split()]
	minPrice = inf
	badPrices = 0
	for i in reversed(a):
		if i > minPrice:
			badPrices += 1
		elif i < minPrice:
			minPrice = i
	print(badPrices)


for _ in range(int(input())):
	sfd = input()
	l = [int(i) for i in input().split()]
	l = l[::-1]
	m = l[0]
	ans = 0
	for i in range(1, len(l)):
		if l[i] <= m:
			m = l[i]
		else:
			ans += 1
	print(ans)


n = int(input())
for i in range(n):
	n0 = int(input())
	s = list(map(int, input().split()))
	f = -2
	c = 0
	mi = s[-1]
	for j in range(n0 - 1):
		if mi < s[f]:
			c += 1
			f -= 1
		else:
			mi = s[f]
			f -= 1
	print(c)


t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	post = [0] * len(arr)
	post[-1] = arr[-1]
	co = 0
	for i in range(n - 2, -1, -1):
		if post[i + 1] < arr[i]:
			post[i] = post[i + 1]
			co += 1
		else:
			post[i] = arr[i]
	print(co)


n = int(input())
for _ in range(n):
	k = int(input())
	c = list(map(int, input().split()))
	minAtm = c[len(c) - 1]
	ctr = 0
	for num in reversed(c):
		if num > minAtm:
			ctr += 1
		else:
			minAtm = num
	print(ctr)


for _ in range(int(input())):
	n = int(input())
	l = [int(s) for s in input().split()]
	min_r = [l[-1] for i in range(n)]
	for i in range(n - 2, -1, -1):
		min_r[i] = min(min_r[i + 1], l[i])
	bad = 0
	for i in range(n):
		if min_r[i] < l[i]:
			bad += 1
	print(bad)


t = int(input())
ans = []
for i in range(t):
	n = int(input())
	(*a,) = map(int, input().split())
	mn = a[-1]
	s = 0
	for j in range(n - 2, -1, -1):
		if a[j] > mn:
			s += 1
		elif a[j] < mn:
			mn = a[j]
	ans.append(s)
for i in ans:
	print(i)


def bad_prices(n, array):
	s = 0
	new_array = [array[0]]
	for i in range(1, n):
		if array[i] >= array[i - 1]:
			new_array.append(array[i])
		else:
			removal = []
			for x in new_array:
				if x > array[i]:
					removal.append(x)
					s += 1
			for e in removal:
				new_array.remove(e)
			removal.clear()
			new_array.append(array[i])
	return s
t = int(input())
array = []
for j in range(t):
	a = int(input())
	b = list(map(int, input().split(' ')))
	array.append([a, b])
for x in array:
	print(bad_prices(x[0], x[1]))


test = int(input())
for i in range(test):
	num = int(input())
	seq = [int(x) for x in input().split()]
	count = 0
	min_ = 1000000000
	for j in range(num - 1, -1, -1):
		if seq[j] > min_ and j != num - 1:
			count += 1
		min_ = min(min_, seq[j])
	print(count)


t = int(input())
for _ in range(t):
	n = int(input())
	A = list(map(int, input().split()))
	c = 0
	r = 1000000000000000000000000000000000000
	for i in range(n - 1, -1, -1):
		if A[i] > r:
			c += 1
		r = min(r, A[i])
	print(c)


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


t = int(input())
for _ in range(t):
	n = int(input())
	(mi, c) = (10000000.0, 0)
	for i in list(map(int, input().split()))[::-1]:
		if i > mi:
			c += 1
		mi = min(i, mi)
	print(c)


t = int(input())
while t:
	n = int(input())
	d = [int(x) for x in input().split()]
	c = 0
	m = d[n - 1]
	for i in range(n - 2, -1, -1):
		if d[i] > m:
			c += 1
		m = min(d[i], m)
	print(c)
	t -= 1