Polycarp was presented with some sequence of integers $a$ of length $n$ ($1 \le a_i \le n$). A sequence can make Polycarp happy only if it consists of different numbers (i.e. distinct numbers).

In order to make his sequence like this, Polycarp is going to make some (possibly zero) number of moves.

In one move, he can:

remove the first (leftmost) element of the sequence.

For example, in one move, the sequence $[3, 1, 4, 3]$ will produce the sequence $[1, 4, 3]$, which consists of different numbers.

Determine the minimum number of moves he needs to make so that in the remaining sequence all elements are different. In other words, find the length of the smallest prefix of the given sequence $a$, after removing which all values in the sequence will be unique.


-----Input-----

The first line of the input contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases.

Each test case consists of two lines.

The first line contains an integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of the given sequence $a$.

The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) — elements of the given sequence $a$.

It is guaranteed that the sum of $n$ values over all test cases does not exceed $2 \cdot 10^5$.


-----Output-----

For each test case print your answer on a separate line — the minimum number of elements that must be removed from the beginning of the sequence so that all remaining elements are different.


-----Examples-----

Input
5
4
3 1 4 3
5
1 1 1 1 1
1
1
6
6 5 4 3 2 1
7
1 2 1 7 1 2 1
Output
1
4
0
0
5


-----Note-----

The following are the sequences that will remain after the removal of prefixes:

$[1, 4, 3]$;

$[1]$;

$[1]$;

$[6, 5, 4, 3, 2, 1]$;

$[2, 1]$.

It is easy to see that all the remaining sequences contain only distinct elements. In each test case, the shortest matching prefix was removed.

Source: codeforces | Difficulty: EASY
Tags: Data structures

Implementation

Greedy algorithms


Solutions:
def Solve():
	(ns, A) = ([], [])
	for t in range(int(input())):
		ns.append(int(input()))
		A.append(list(map(int, input().split())))
	for (n, a) in zip(ns, A):
		s = set()
		for i in a[::-1]:
			if i not in s:
				s.add(i)
			else:
				print(n - len(s))
				break
		else:
			print(0)
Solve()


def unique_breaks(s):
	r = {}
	start = len(s) - 1
	end = -1
	step = end
	for k in range(start, end, step):
		v = s[k]
		if r.get(v) == None:
			r[v] = 1
		else:
			return k + 1
	return 0

def main():
	t = input()
	t = int(t)
	from itertools import repeat
	b = list(repeat(0, t))
	for i in range(t):
		n = input()
		s = input()
		b[i] = unique_breaks(s.split(' '))
	for k in range(t):
		print(b[k])
main()


import sys
input = lambda : sys.stdin.readline().rstrip()
mis = lambda : map(int, input().split())
INF = float('inf')

def solve():
	N = int(input())
	arr = list(mis())
	cnt = [0] * (N + 1)
	ans = N
	for i in range(N - 1, -1, -1):
		if cnt[arr[i]]:
			break
		ans -= 1
		cnt[arr[i]] = 1
	print(ans)
T = int(input())
for _ in range(T):
	solve()


import sys

def main():
	t = int(sys.stdin.readline())
	for _ in range(t):
		n = int(sys.stdin.readline())
		arr = list(map(int, sys.stdin.readline().split()))
		st = set()
		for i in range(n - 1, -1, -1):
			if arr[i] not in st:
				st.add(arr[i])
			else:
				break
		print(len(arr) - len(st))
main()


import sys
k = int(sys.stdin.readline().strip())
for _ in range(k):
	s = set()
	n = int(sys.stdin.readline().strip())
	a = list(map(int, sys.stdin.readline().split()))
	for v in a[::-1]:
		if v in s:
			break
		s.add(v)
	print(n - len(s))


from sys import stdin, stdout

def ans():
	n = int(stdin.readline())
	a = list(map(int, stdin.readline().strip().split()))
	temp = set()
	for x in a[::-1]:
		if x in temp:
			break
		temp.add(x)
	print(n - len(temp))
t = int(stdin.readline())
for _ in range(t):
	ans()


import sys
input = sys.stdin.readline
for i in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	x = set()
	for i in arr[::-1]:
		if i in x:
			break
		x.add(i)
	print(n - len(x))


import sys

def solve():
	inp = sys.stdin.readline
	n = int(inp())
	a = list(map(int, inp().split()))
	c = [0] * (n + 1)
	for i in range(n - 1, -1, -1):
		if c[a[i]] > 0:
			print(i + 1)
			return
		c[a[i]] += 1
	print(0)

def main():
	for i in range(int(sys.stdin.readline())):
		solve()
main()


def solve():
	n = int(input())
	tc = []
	for i in range(n):
		tc.append([])
		tc[i].append(int(input()))
		tc[i].append(list(map(int, str(input()).split())))
	for test in tc:
		l = test[0]
		coll = set()
		vals = test[1]
		rv = 0
		for i in range(l - 1, -1, -1):
			if vals[i] in coll:
				rv = i + 1
				break
			else:
				coll.add(vals[i])
		print(rv)
solve()


import sys
input = sys.stdin.readline
I = lambda : list(map(int, input().split()))
(t,) = I()
for _ in range(t):
	(n,) = I()
	l = I()
	vis = [0] * (n + 1)
	rem = flag = 0
	for i in range(n - 1, -1, -1):
		if flag == 0 and vis[l[i]]:
			rem = i + 1
			flag = 1
		else:
			vis[l[i]] = 1
	print(rem)


import sys
input = sys.stdin.readline

def print(*args, end='\n', sep=' ') -> None:
	sys.stdout.write(sep.join(map(str, args)) + end)

def Solve():
	(ns, A) = ([], [])
	for t in range(int(input())):
		ns.append(int(input()))
		A.append(list(map(int, input().split())))
	for (n, a) in zip(ns, A):
		s = dict()
		for i in a[::-1]:
			if i not in s.keys():
				s[i] = None
			else:
				print(n - len(s))
				break
		else:
			print(0)
Solve()


def main():
	len_s = int(input())
	my_nums = set()
	seq = input().split(' ')
	for i in range(len_s - 1, -1, -1):
		if seq[i] not in my_nums:
			my_nums.add(seq[i])
		else:
			print(i + 1)
			return
	print('0')
n = int(input())
for _ in range(n):
	main()


cant_test_cases = int(input())
for i in range(cant_test_cases):
	len_lista = int(input())
	lista = input().split(' ')[::-1]
	letras = set()
	for elemento in lista:
		if elemento in letras:
			break
		else:
			letras.add(elemento)
	print(len_lista - len(letras))


def solve(arr, n):
	vis = [0] * (n + 1)
	ans = n
	for i in range(n - 1, -1, -1):
		if vis[arr[i]] != 1:
			ans -= 1
			vis[arr[i]] = 1
		else:
			break
	return ans
t = int(input())
ans = []
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	x = solve(arr, n)
	ans.append(x)
for i in ans:
	print(i)


def solve():
	n = int(input())
	a = input().split(' ')
	s = set()
	for i in range(n - 1, -1, -1):
		if a[i] in s:
			print(i + 1)
			return
		else:
			s.add(a[i])
	print(0)
T = int(input())
for t in range(T):
	solve()


def solve():
	n = int(input())
	a = [int(i) for i in input().split()]
	count = n
	ans = set()
	for i in range(n - 1, -1, -1):
		if a[i] in ans:
			return str(count)
		count -= 1
		ans.add(a[i])
	return str(count)
t = int(input())
q = []
while t:
	q.append(solve())
	t -= 1
print('\n'.join(q))


t = int(input())
sol = []
for _ in range(t):
	n = int(input())
	a = input().split()[::-1]
	s = set()
	cnt = 0
	for i in range(n):
		if a[i] not in s:
			s.add(a[i])
			cnt += 1
		else:
			break
	sol.append(n - cnt)
for s in sol:
	print(s)


import sys
from collections import deque
inp = sys.stdin.readline
for _ in range(int(inp())):
	n = int(inp())
	a = list(map(int, inp().split()))
	d = [0] * (n + 1)
	cnt = 0
	for i in range(n - 1, -1, -1):
		if not d[a[i]]:
			d[a[i]] = 1
			cnt += 1
		else:
			break
	print(n - cnt)


t = int(input())

def compute():
	n = int(input())
	seq = [int(x) for x in input().split(' ')]
	seq.reverse()
	hashmap = {}
	moves = n
	for val in seq:
		if val in hashmap:
			return moves
		else:
			hashmap[val] = 1
			moves -= 1
	return 0
for _ in range(t):
	print(compute())


for _ in range(int(input())):
	n = int(input())
	a = input().split()[::-1]
	d = set()
	for x in a:
		if x in d:
			break
		d.add(x)
	print(n - len(d))


from collections import defaultdict
_ = int(input())
ans = []
for testcases in range(_):
	n = int(input())
	a = list(reversed(list(map(int, input().split()))))
	hash = defaultdict(int)
	if len(set(a)) == n:
		ans.append(0)
	else:
		for j in a:
			if hash[j] == 0:
				hash[j] += 1
			else:
				ans.append(n - sum(list(hash.values())))
				break
for i in ans:
	print(i)


from sys import stdin

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

def read_int():
	return int(input())

def read_ints():
	return map(int, input().split())
t = read_int()
for case_num in range(t):
	n = read_int()
	a = list(read_ints())
	s = set()
	ans = 0
	for i in range(n - 1, -1, -1):
		if a[i] in s:
			ans = i + 1
			break
		s.add(a[i])
	print(ans)


for _ in range(int(input())):
	n = int(input())
	l = input().split()
	s = set()
	flag = True
	for i in l[::-1]:
		if i not in s:
			s.add(i)
		else:
			print(n - len(s))
			flag = False
			break
	if flag:
		print(0)


for i in range(int(input())):
	n = int(input())
	aLst = list(input().split())
	aLst.reverse()
	newSet = set()
	for j in aLst:
		if j not in newSet:
			newSet.add(j)
		else:
			break
	print(n - len(newSet))


def polycarp():
	n = int(input())
	counter = {}
	duplicate = 0
	seq = list(input().split())
	for s in seq:
		if s in counter:
			if counter[s] == 1:
				duplicate += 1
			counter[s] += 1
		else:
			counter[s] = 1
	if not duplicate:
		return 0
	for (i, num) in enumerate(seq):
		counter[num] -= 1
		if counter[num] == 1:
			duplicate -= 1
		if duplicate == 0:
			return i + 1
	return len(seq) - 1
t = int(input())
for _ in range(t):
	print(polycarp())


t = int(input())
while t > 0:
	n = int(input())
	a = input().split()[::-1]
	s = set()
	for i in a:
		if i in s:
			break
		s.add(i)
	print(n - len(s))
	t -= 1


def print_hi(W):
	for i in range(int(W)):
		n = input()
		a = input()
		list1 = list(a.split())[::-1]
		set1 = set()
		for i in list1:
			if i in set1:
				break
			set1.add(i)
		print(int(n) - len(set1))
t = input()
print_hi(t)


for _ in range(int(input())):
	(n, s) = (int(input()), set())
	a = list(input().split())[::-1]
	for i in a:
		if i in s:
			break
		s.add(i)
	print(n - len(s))


def test():
	n = int(input())
	l = [int(i) for i in input().split()]
	s = set()
	count = 0
	for i in reversed(l):
		if i in s:
			break
		s.add(i)
		count += 1
	print(n - count)
T = int(input())
for i in range(T):
	test()


from sys import stdin, stdout
from math import gcd, sqrt, floor, ceil
from collections import defaultdict
import random
input = stdin.readline

def list_inp(x):
	return list(map(x, input().split()))

def map_inp(x):
	return map(x, input().split())

def lcm(a, b):
	return a * b / gcd(a, b)
t = int(input())
for _ in range(t):
	n = int(input())
	arr = list_inp(int)
	if n == 0 or n == 1:
		print(0)
	else:
		rep = [0] * (n + 1)
		ans = n
		for i in range(n - 1, -1, -1):
			if rep[arr[i]] == 1:
				break
			ans -= 1
			rep[arr[i]] = 1
		print(ans)


def makeHappy(n, seq):
	data = {}
	if len(set(seq)) == len(seq):
		return 0
	if len(seq) == 1:
		return 0
	else:
		for i in reversed(range(n)):
			if seq[i] not in data:
				data[seq[i]] = True
			else:
				return i + 1
t = int(input())
for i in range(t):
	n = int(input())
	seq = list(map(int, input().split()))
	print(makeHappy(n, seq))


import sys
input = sys.stdin.readline
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	boo = set()
	moves = 0
	for i in a[::-1]:
		if i in boo:
			break
		boo.add(i)
		moves += 1
	print(n - moves)


for s in [*open(0)][2::2]:
	a = s.split()
	s = {0}
	i = len(a)
	while {a[(i := (i - 1))]} - s:
		s |= {a[i]}
	print(i + 1)


def delete_prefix(a):
	(s, res) = (set(), 0)
	for (i, j) in enumerate(reversed(a)):
		if j in s:
			res = len(a) - i
			break
		else:
			s.add(j)
	print(res)
t = int(input())
while t:
	t -= 1
	n = int(input())
	a = list(map(int, input().split()))
	delete_prefix(a)


import math
t = int(input())
for t1 in range(t):
	n = int(input())
	a = input().split()
	ans = set()
	for i in a[::-1]:
		if i not in ans:
			ans.add(i)
		else:
			break
	print(abs(len(ans) - n))


n = int(input())
while n > 0:
	m = int(input())
	a = input().split()[::-1]
	q = set()
	for k in a:
		if k in q:
			break
		q.add(k)
	print(m - len(q))
	n -= 1


import math

def max_cuts(n, a, b, c):
	if n == 0:
		return 0
	elif n < 0:
		return -1
	result = max(max_cuts(n - a, a, b, c), max_cuts(n - b, a, b, c), max_cuts(n - c, a, b, c))
	if result == -1:
		return -1
	return result + 1

def freq(list1):
	freq_count = {}
	for i in list1:
		if i in freq_count:
			freq_count[i] += 1
		else:
			freq_count[i] = 1
	return freq_count
t = int(input())
for t1 in range(t):
	n = int(input())
	a = list(map(int, input().split(' ')))
	uniq = set()
	for i in a[::-1]:
		if i not in uniq:
			uniq.add(i)
		else:
			break
	print(n - len(uniq))


t = int(input())
ans = []
for _ in range(t):
	n = int(input())
	a = [int(x) for x in input().split()]
	s = set()
	res = 0
	for i in range(n - 1, -1, -1):
		if a[i] in s:
			res = i + 1
			break
		s.add(a[i])
	ans.append(res)
for i in range(t):
	print(ans[i])


for s in [*open(0)][2::2]:
	s = s.split()
	l = set()
	j = len(s) - 1
	while j >= 0 and s[j] not in l:
		l.add(s[j])
		j -= 1
	print(j + 1)


import sys
import math
from collections import Counter
from collections import defaultdict

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

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

def solve(arr):
	visit = set()
	cnt = 0
	for i in range(len(arr) - 1, -1, -1):
		if arr[i] not in visit:
			visit.add(arr[i])
			cnt += 1
		else:
			break
	return len(arr) - cnt
t = int(input())
for _ in range(t):
	n = get_ints()
	arr = get_ints()
	print(solve(arr))


T = int(input())
q = []
for i in range(T):
	n = int(input())
	data = [int(i) for i in input().split()]
	q.append((n, data))

def solve(n, data):
	s = set()
	for i in range(n - 1, -1, -1):
		if data[i] in s:
			print(i + 1)
			return
		s.add(data[i])
	print(0)
for (n, data) in q:
	solve(n, data)


t = int(input())
ans = []
for x in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	s = set()
	for y in range(len(a) - 1, -1, -1):
		if a[y] in s:
			ans.append(y + 1)
			break
		s.add(a[y])
	if len(s) == len(a):
		ans.append(0)
for el in ans:
	print(el)


import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)
t = int(input())
for _ in range(t):
	n = int(input())
	I = list(map(int, input().split()))
	Ans = [0] * (n + 1)
	check = True
	for i in range(1, n + 1):
		if Ans[I[-i]] == 0:
			Ans[I[-i]] = 1
		else:
			check = False
			break
	if not check:
		print(n - i + 1)
	else:
		print(0)


for _ in range(int(input())):
	n = int(input())
	s = set()
	if 438434 == '7t8ht4h':
		print('Tanmay')
	a = [int(x) for x in input().split()]
	c = 0
	if n == len(set(a)):
		print(0)
		continue
	for i in a[-1::-1]:
		if i in s:
			c = n - c
			break
		c += 1
		s.add(i)
	print(c)


t = int(input())
res = []
for i in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	(d, maxx) = (set(), 0)
	for j in range(len(arr) - 1, -1, -1):
		if arr[j] in d:
			res.append(j + 1)
			maxx = 1
			break
		else:
			d.add(arr[j])
	if not maxx:
		res.append(0)
for i in res:
	print(i)


t = int(input())

def solve():
	n = int(input())
	lst = list(map(int, input().split()))
	hash_map = {}
	for i in range(n - 1, -1, -1):
		if lst[i] in hash_map:
			print(i + 1)
			return
		else:
			hash_map[lst[i]] = 1
	print(0)
while t:
	solve()
	t -= 1


import sys

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

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

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

def get_num():
	return int(sys.stdin.readline())
tests = get_num()
for test in range(tests):
	n = get_num()
	a = get_array()
	freq = dict()
	i = n - 1
	rep = False
	while i >= 0:
		if a[i] in freq:
			rep = True
			break
		else:
			freq[a[i]] = 1
		i -= 1
	if rep:
		print(i + 1)
	else:
		print(0)


for i in range(int(input())):
	n = int(input())
	s = input()
	s = s[::-1]
	dic = {}
	c = n
	d = 0
	for j in s.split():
		if dic.get(j) == None:
			dic[j] = 1
		else:
			d = c
			break
		c -= 1
	print(d)


t = int(input())
for _in in range(t):
	n = int(input())
	a = list(map(int, input().split(' ')))
	ma = {}
	ans = 0
	for i in a[::-1]:
		if i in ma:
			break
		ma[i] = 1
		ans += 1
	print(n - ans)


for k in [*open(0)][2::2]:
	b = k.split()
	k = {0}
	i = len(b)
	while {b[(i := (i - 1))]} - k:
		k |= {b[i]}
	print(i + 1)


t = int(input())
for i in range(0, t):
	le = int(input())
	arr = input().split()
	arr.reverse()
	arr1 = {}
	for i in range(0, len(arr)):
		if arr1.get(arr[i]) == None:
			arr1[arr[i]] = 1
		else:
			print(le - len(arr1))
			break
	else:
		print(0)


from bisect import bisect_left, bisect_right
import math, sys, os
from io import BytesIO
from collections import defaultdict, Counter
from math import log, ceil, sqrt, floor, gcd, log2
from itertools import chain, combinations
from functools import reduce
input = sys.stdin.readline
printt = lambda x: sys.stdout.write(str(x) + '\n')
printts = lambda x: sys.stdout.write(' '.join(map(str, x)) + '\n')
divisors = lambda n: set(chain.from_iterable(((i, n // i) for i in range(1, int(sqrt(n)) + 1) if not n % i)))
is_prime = lambda n: False if n < 2 else True if n in [2, 3] else False if n % 2 == 0 else not any((n % i == 0 for i in range(3, 1 + floor(sqrt(n)), 2)))
n_is_power_of_p = lambda n, p: not log(n, p) % 1
lcm = lambda a, b: int(a * b / gcd(a, b))

def yes():
	printt('YES')

def no():
	printt('NO')
for case in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	used = set()
	found = False
	for i in range(n - 1, -1, -1):
		if arr[i] in used:
			printt(i + 1)
			found = True
			break
		used.add(arr[i])
	if not found:
		printt(0)


def ans(n, a):
	s = set()
	i = 0
	for j in range(n):
		while a[j] in s:
			s.remove(a[i])
			i += 1
		s.add(a[j])
	return n - len(s)
T = int(input())
l = []
for _ in range(T):
	n = int(input())
	a = list(map(int, input().split()))
	c = ans(n, a)
	l.append(c)
for i in range(T):
	print(l[i])


for so1 in [*open(0)][2::2]:
	a1 = so1.split()
	so1 = {0}
	i = len(a1)
	while {a1[(i := (i - 1))]} - so1:
		so1 |= {a1[i]}
	print(i + 1)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	temp = set()
	f = 1
	for b in a[::-1]:
		if b in temp:
			print(n - len(temp))
			f = 0
			break
		else:
			temp.add(b)
	if f:
		print(0)


def sol(l):
	l.reverse()
	d = dict()
	if len(l) == len(set(l)):
		return 0
	for _ in range(len(l)):
		if l[_] not in d:
			d[l[_]] = 1
		else:
			return len(l) - _
t = int(input())
for _ in range(t):
	n = input()
	l = [int(i) for i in input().split()]
	print(sol(l))


test = int(input())
for _ in range(test):
	leng = int(input())
	linp = list(map(int, input().split()))
	remove = len(linp) - len(set(linp))
	l = []
	if remove == 0:
		print(0)
		continue
	else:
		c = set()
		for i in range(leng - 1, -1, -1):
			if linp[i] in c:
				move = i + 1
				print(i + 1)
				break
			else:
				c.add(linp[i])
		continue


n_cases = int(input())
for i in range(n_cases):
	largo = int(input())
	lista = input().split(' ')[::-1]
	lista_control = set()
	for num in lista:
		if num not in lista_control:
			lista_control.add(num)
		else:
			break
	print(largo - len(lista_control))


import sys
t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(ii) for ii in input().split()]
	if len(set(a)) == n:
		print(0)
		continue
	b = {}
	i = n - 1
	while i >= 0:
		if a[i] in b.keys():
			break
		else:
			b[a[i]] = 1
			i -= 1
	print(i + 1)


import sys
sys.setrecursionlimit(10 ** 6)

def proc():
	n = int(input())
	a = [int(i) for i in input().split()]
	s = set()
	for (i, ai) in enumerate(a[::-1]):
		if ai in s:
			return n - i
		s.add(ai)
	return 0
pass
nofkase = int(input())
for kase in range(nofkase):
	print(proc())


def calc():
	n = int(input())
	arr = [int(el) for el in input().split()]
	st = set()
	for i in range(-1, -n - 1, -1):
		if arr[i] in st:
			print(n + i + 1)
			return
		st.add(arr[i])
	print(0)
for _ in range(int(input())):
	calc()


for i in range(int(input())):
	n = int(input())
	l = input().split(' ')[::-1]
	s = set()
	for num in l:
		if num not in s:
			s.add(num)
		else:
			break
	print(n - len(s))


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

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

def iinp():
	return int(input())

def test():
	return int(input())

def inp():
	return input()

def solve():
	n = iinp()
	arr = linp()
	d = {}
	res = 0
	for i in range(n):
		if arr[i] in d:
			res = max(res, d[arr[i]])
		d[arr[i]] = i + 1
	print(res)

def main():
	for _ in range(test()):
		solve()
main()


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	if len(set(a)) == len(a):
		print(0)
	else:
		d = dict()
		count = 0
		x = -1
		for i in range(len(a) - 1, -1, -1):
			if a[i] not in d:
				d[a[i]] = 1
			else:
				x = i
				break
		print(x + 1)


def ss(l):
	n = len(l)
	d = {}
	i = n - 1
	while i >= 0 and l[i] not in d:
		d[l[i]] = 1
		i -= 1
	return i + 1
for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	print(ss(l))


t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	check = set()
	flag = 0
	for i in range(n - 1, -1, -1):
		if arr[i] in check:
			flag = 1
			break
		else:
			check.add(arr[i])
	if flag:
		print(i + 1)
	else:
		print(i)


import math

def slove():
	a = int(input())
	op = [0] * (a + 1)
	b = list(map(int, input().split()))
	ans = 0
	for i in range(len(b) - 1, -1, -1):
		if op[b[i]] == 0:
			op[b[i]] += 1
			ans += 1
		else:
			break
	print(a - ans)
for i in range(int(input())):
	slove()


def fun():
	t = int(input())
	while t:
		n = int(input())
		arr = [int(i) for i in input().split()]
		dic = {}
		for index in reversed(range(n)):
			if arr[index] not in dic:
				dic[arr[index]] = 1
			else:
				print(index + 1)
				break
		else:
			print(0)
		t -= 1
fun()


import sys
from collections import Counter

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

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

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

def input_char_arr():
	return sys.stdin.readline().rstrip().split()

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

def print_sp(num):
	sys.stdout.write(str(num) + ' ')

def print_only(num):
	sys.stdout.write(str(num))

def solve():
	n = input_int()
	arr = input_char_arr()
	counter_dict = Counter(arr)
	total_move = 0
	recall = 0
	for char in arr:
		if counter_dict[char] > 1:
			counter_dict[char] -= 1
			total_move += 1 + recall
			recall = 0
		else:
			recall += 1
	print_ln(total_move)

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


t = int(input())

def minNum(a):
	(res, d, pad) = (0, {}, 0)
	for num in a:
		d[num] = d.get(num, 0) + 1
	for num in a:
		if d[num] > 1:
			d[num] -= 1
			res += 1 + pad
			pad = 0
		else:
			pad += 1
	return res
while t:
	n = int(input())
	a = list(map(int, input().split()))
	print(minNum(a))
	t -= 1


def solve():
	n = int(input())
	arr = [int(x) for x in input().split()]
	s = set()
	i = len(arr) - 1
	while i >= 0:
		if arr[i] in s:
			print(i + 1)
			return
		s.add(arr[i])
		i -= 1
	print(0)
	return
t = int(input())
for _ in range(t):
	solve()


def main():
	t = int(input())
	for _ in range(t):
		n = int(input())
		a = [int(i) for i in input().split()]
		b = set()
		ans = 0
		for i in a[::-1]:
			if i in b:
				break
			b.add(i)
			ans += 1
		print(n - ans)
main()


t = int(input())
res = []
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	s = set()
	while n > 0:
		if a[n - 1] in s:
			break
		s.add(a[n - 1])
		n -= 1
	res.append(n)
for r in res:
	print(r)


def sol():
	n = int(input())
	a = list(map(int, input().split()))[::-1]
	s = set()
	for (i, v) in enumerate(a):
		if v not in s:
			s.add(v)
		else:
			print(n - i)
			return
	print(0)
for _ in range(int(input())):
	sol()


def solve():
	n = int(input())
	a = list(map(int, input().split()))
	b = {0}
	l = 0
	b.remove(0)
	for i in range(n - 1, -1, -1):
		b.add(a[i])
		length = len(b)
		if l == length:
			break
		else:
			l = length
	print(n - l)
	b.clear()
	a.clear()
t = int(input())
for i in range(t):
	solve()


t = int(input())
while t:
	n = int(input())
	a = [int(i) for i in input().split()]
	b = a[::-1]
	m = set()
	if len(b) == len(set(b)):
		print('0')
	else:
		for i in range(len(b)):
			m.add(b[i])
			if len(m) != i + 1:
				break
		print(n - i)
	t = t - 1


import math
for _ in range(int(input())):
	n = int(input())
	l = [int(i) for i in input().split(' ')]
	l.reverse()
	a = set()
	for i in l:
		if i in a:
			break
		a.add(i)
	print(len(l) - len(a))