Let's call an array $t$ dominated by value $v$ in the next situation.

At first, array $t$ should have at least $2$ elements. Now, let's calculate number of occurrences of each number $num$ in $t$ and define it as $occ(num)$. Then $t$ is dominated (by $v$) if (and only if) $occ(v) > occ(v')$ for any other number $v'$. For example, arrays $[1, 2, 3, 4, 5, 2]$, $[11, 11]$ and $[3, 2, 3, 2, 3]$ are dominated (by $2$, $11$ and $3$ respectevitely) but arrays $[3]$, $[1, 2]$ and $[3, 3, 2, 2, 1]$ are not.

Small remark: since any array can be dominated only by one number, we can not specify this number and just say that array is either dominated or not.

You are given array $a_1, a_2, \dots, a_n$. Calculate its shortest dominated subarray or say that there are no such subarrays.

The subarray of $a$ is a contiguous part of the array $a$, i. e. the array $a_i, a_{i + 1}, \dots, a_j$ for some $1 \le i \le j \le n$.


-----Input-----

The first line contains single integer $T$ ($1 \le T \le 1000$) — the number of test cases. Each test case consists of two lines.

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

The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) — the corresponding values of the array $a$.

It's guaranteed that the total length of all arrays in one test doesn't exceed $2 \cdot 10^5$.


-----Output-----

Print $T$ integers — one per test case. For each test case print the only integer — the length of the shortest dominated subarray, or $-1$ if there are no such subarrays.


-----Example-----
Input
4
1
1
6
1 2 3 4 5 1
9
4 1 2 4 5 4 3 2 1
4
3 3 3 3

Output
-1
6
3
2



-----Note-----

In the first test case, there are no subarrays of length at least $2$, so the answer is $-1$.

In the second test case, the whole array is dominated (by $1$) and it's the only dominated subarray.

In the third test case, the subarray $a_4, a_5, a_6$ is the shortest dominated subarray.

In the fourth test case, all subarrays of length more than one are dominated.

Source: codeforces | Difficulty: EASY
Tags: String algorithms

Sorting

Amortized analysis

Implementation

Greedy algorithms


Solutions:
T = int(input())
while T > 0:
	N = int(input())
	dict = {}
	arr = [int(x) for x in input().split()]
	min = N + 1
	(i, j) = [0, 0]
	flag = False
	num = 0
	while j < N:
		if flag == False:
			if arr[j] not in dict:
				dict[arr[j]] = 1
			elif dict[arr[j]] < 1:
				dict[arr[j]] += 1
			else:
				dict[arr[j]] += 1
				flag = True
				num = arr[j]
			j += 1
		else:
			if arr[i] == num:
				flag = False
				num = 0
				if min > j - i:
					min = j - i
			dict[arr[i]] -= 1
			i += 1
	while i < N:
		dict[arr[i]] -= 1
		if arr[i] == num:
			if min > j - i:
				min = j - i
			break
		i += 1
	print(min if min < N + 1 else -1)
	T -= 1


I = input
for _ in [0] * int(I()):
	I()
	r = n = 8 ** 6
	d = {}
	i = 0
	for x in map(int, I().split()):
		r = min(r, i - d.get(x, -r) + 1)
		d[x] = i
		i += 1
	print(r % n or -1)


def read_first_line():
	return int(input())

def read_cases(number_of_cases):
	cases = []
	for i in range(number_of_cases):
		line = input()
		if i % 2 == 1:
			case = [int(string) for string in line.strip().split(' ')]
			cases.append(case)
	return cases

def updateHistory(index, number, history):
	if not number in history:
		history[number] = {'latestIndex': index, 'minDifference': None}
		return
	value = history[number]
	latestIndex = value['latestIndex']
	minDifference = value['minDifference']
	minimum = index - latestIndex
	if not minDifference or minimum < minDifference:
		history[number] = {'latestIndex': index, 'minDifference': minimum}
		return
	history[number] = {'latestIndex': index, 'minDifference': minDifference}

def solve(case):
	history = {}
	for (index, number) in enumerate(case):
		updateHistory(index, number, history)
	values = history.values()
	mins = []
	for value in values:
		minDifference = value['minDifference']
		if minDifference:
			mins.append(minDifference)
	if len(mins) == 0:
		return -1
	return min(mins) + 1
test_cases = read_first_line()
lines_per_case = 2
cases = read_cases(test_cases * lines_per_case)
for case in cases:
	solution = solve(case)
	print(solution)


t = int(input())
from collections import defaultdict as dd
while t:
	d = dd(list)
	n = int(input())
	mi = 100000000000000000000
	l = list(map(int, input().split()))
	lol = 0
	for i in range(n):
		if len(d[l[i]]) != 0:
			lol = 1
			if i - d[l[i]][0] < mi:
				mi = i - d[l[i]][0]
			d[l[i]][0] = i
		else:
			d[l[i]].append(i)
	if lol == 0:
		print(-1)
	else:
		print(mi + 1)
	t -= 1


t = int(input())
while t:
	n = int(input())
	l = list(map(int, input().split()))
	pos = [-1] * (n + 1)
	mini = n + 1
	cnt = 0
	flag = 0
	for i in range(n):
		if pos[l[i]] != -1:
			mini = min(mini, i - pos[l[i]])
		pos[l[i]] = i
	if mini == n + 1:
		print(-1)
	else:
		print(mini + 1)
	t -= 1


T = int(input())
for t in range(T):
	ans = 1000000000
	n = int(input())
	a = list(zip(list(map(int, input().split())), list(range(n))))
	a.sort()
	for i in range(1, n):
		if a[i][0] == a[i - 1][0]:
			ans = min(a[i][1] - a[i - 1][1], ans)
	if ans == 1000000000:
		print('-1')
	else:
		print(ans + 1)


from collections import Counter
t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(x) for x in input().split()]
	prev = Counter()
	mn = n + 1
	for (i, el) in enumerate(a):
		if el in prev:
			mn = min(mn, i - prev[el] + 1)
		prev[el] = i
	print(mn if mn < n + 1 else -1)


t = int(input())
for i in range(t):
	n = int(input())
	mas = list(map(int, input().split()))
	ans = []
	mini = n
	for i in range(n):
		ans.append([mas[i], i])
	ans.sort()
	flag = False
	for i in range(n - 1):
		if ans[i][0] == ans[i + 1][0]:
			mini = min(ans[i + 1][1] - ans[i][1] + 1, mini)
			flag = True
	if not flag:
		print(-1)
	else:
		print(mini)


for _ in range(int(input())):
	n = int(input())
	a = [int(i) for i in input().split()]
	s = {}
	b = []
	for i in range(n):
		if a[i] in s:
			b.append(i - s[a[i]] + 1)
			s[a[i]] = i
		else:
			s[a[i]] = i
	if len(b) == 0:
		print(-1)
	elif len(b) == 1:
		if b[0] > 1:
			print(b[0])
		else:
			print(-1)
	else:
		print(min(b))


import sys
input = sys.stdin.readline
for _ in range(int(input())):
	n = int(input())
	cnt = [0] * (n + 1)
	l = [int(i) for i in input().split()]
	st = 0
	mini = n + 4
	end = 0
	while end < n:
		cnt[l[end]] += 1
		while cnt[l[end]] > 1:
			cnt[l[st]] -= 1
			mini = min(mini, end - st + 1)
			st += 1
		end += 1
	print(mini if mini <= n else -1)


import sys
import string
from collections import Counter, defaultdict
from math import fsum, sqrt, gcd, ceil, factorial
from itertools import combinations, permutations
flush = lambda : sys.stdout.flush
comb = lambda x, y: factorial(x) // factorial(y) // factorial(x - y)
ip = lambda : input()
ii = lambda : int(input())
r = lambda : map(int, input().split())
rr = lambda : list(r())

def solve():
	n = ii()
	arr = rr()
	if n == 1:
		print(-1)
		return
	hmap = defaultdict(list)
	for (i, j) in enumerate(arr):
		hmap[j].append(i)
	ans = float('inf')
	for i in hmap.values():
		for j in range(len(i) - 1):
			ans = min(ans, i[j + 1] - i[j] + 1)
	print(ans if ans != float('inf') else -1)
for _ in range(ii()):
	solve()


from __future__ import division, print_function
import os
import sys
from io import BytesIO, IOBase
import datetime, math, functools
if sys.version_info[0] < 3:
	from __builtin__ import xrange as range
	from future_builtins import ascii, filter, hex, map, oct, zip

def main():
	starttime = datetime.datetime.now()
	if os.path.exists('input.txt'):
		sys.stdin = open('input.txt', 'r')
		sys.stdout = open('output.txt', 'w')
	longans = ''
	tc = 0
	for _ in range(int(input())):
		tc += 1
		n = int(input())
		a = [int(x) for x in input().split()]
		d = {}
		for i in range(n):
			if a[i] not in d:
				d[a[i]] = [i]
			else:
				d[a[i]].append(i)
		z = 999999999999999999
		for i in d:
			if len(d[i]) >= 2:
				for j in range(1, len(d[i])):
					z = min(z, d[i][j] - d[i][j - 1])
		if z == 999999999999999999:
			print(-1)
			continue
		print(z + 1)
	endtime = datetime.datetime.now()
	time = (endtime - starttime).total_seconds() * 1000
	if os.path.exists('input.txt'):
		print('Time:', time, 'ms')
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

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

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

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

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

class IOWrapper(IOBase):

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

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


q = int(input())
for _ in range(q):
	n = int(input())
	l = list(map(int, input().split()))
	if n == 1:
		print(-1)
		continue
	minimum = 9999999
	a = 0
	b = 0
	d = {}
	while b != n:
		while b != n:
			if d.get(l[b]) == None:
				d[l[b]] = 1
			elif d[l[b]] == 1:
				d[l[b]] = 2
				break
			else:
				d[l[b]] += 1
			b += 1
		if b != n:
			while b - a > 0:
				if d[l[a]] == 2:
					d[l[a]] = 1
					if b - a + 1 < minimum:
						minimum = b - a + 1
					a += 1
					b += 1
					break
				else:
					d[l[a]] -= 1
				a += 1
	if minimum == 9999999:
		print(-1)
	else:
		print(minimum)


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	last_seen = {}
	min_diff = 10 ** 10
	if n < 2:
		print(-1)
		continue
	else:
		for i in range(n):
			if arr[i] in last_seen:
				min_diff = min(min_diff, i - last_seen[arr[i]] + 1)
				last_seen[arr[i]] = i
			else:
				last_seen[arr[i]] = i
		if min_diff == 10 ** 10:
			print(-1)
		else:
			print(min_diff)


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	if len(set(a)) == len(a):
		print(-1)
	else:
		d = {}
		for j in range(n):
			if a[j] in d:
				if j - d[a[j]][1] < d[a[j]][0]:
					d[a[j]] = [j - d[a[j]][1], j]
				else:
					d[a[j]] = [d[a[j]][0], j]
			else:
				d[a[j]] = [10 ** 8, j]
		ans = min(d.values())
		if ans[0] == 1:
			print(2)
		else:
			print(ans[0] + 1)


import math
t = int(input())

def hello():
	n = int(input())
	lst = list(map(int, input().split()))
	have = set()
	min_len = 0
	length = 0
	indexes = dict()
	for i in range(len(lst)):
		if not lst[i] in indexes:
			indexes[lst[i]] = i
		else:
			if min_len > i - indexes[lst[i]] + 1 or min_len == 0:
				min_len = i - indexes[lst[i]] + 1
			indexes[lst[i]] = i
	if min_len == 0:
		return -1
	else:
		return min_len
for i in range(t):
	print(hello())


from collections import Counter
for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	if n == 1:
		print(-1)
	else:
		f = 0
		x = Counter(l)
		for g in x:
			if x[g] > 1:
				f = 1
				break
		if f:
			s = set(l)
			d = {}
			for i in s:
				d[i] = []
			ans = 2e+18
			for t in range(n):
				d[l[t]].append(t)
			for j in d:
				d[j].sort()
				if len(d[j]) > 1:
					for y in range(1, len(d[j])):
						ans = min(ans, d[j][y] - d[j][y - 1])
			print(ans + 1)
		else:
			print(-1)


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	if len(set(a)) == n:
		print(-1)
		continue
	table = {}
	for i in a:
		table[i] = []
	possible = set()
	for i in range(n):
		e = a[i]
		table[e].append(i)
		if len(table[e]) >= 2:
			possible.add(e)
	ans = 999999999
	for i in possible:
		table[i].sort()
		for j in range(0, len(table[i]) - 1):
			ans = min(ans, table[i][j + 1] - table[i][j] + 1)
	print(ans)


I = input
for _ in [0] * int(I()):
	I()
	r = N = 20000000.0
	d = {}
	i = 0
	for x in map(int, I().split()):
		r = min(r, i - d.get(x, -N))
		d[x] = i
		i += 1
	print((-1, r + 1)[r < N])


q = int(input())
for i in range(q):
	n = int(input())
	l = input()
	l = [int(x) for x in l.split(' ')]
	pred = {x: -1 for x in l}
	ans = n + 1
	for i in range(n):
		if pred[l[i]] != -1:
			ans = min(ans, i - pred[l[i]] + 1)
		pred[l[i]] = i
	if ans != n + 1:
		print(ans)
	else:
		print(-1)


import sys
T = int(sys.stdin.readline())
for i in range(T):
	n = int(sys.stdin.readline())
	array = sys.stdin.readline().split(' ')
	lastSeen = [-10 ** 6 for i in range(n + 1)]
	best = 10 ** 6
	for j in range(len(array)):
		if j - lastSeen[int(array[j])] < best:
			best = j - lastSeen[int(array[j])]
		lastSeen[int(array[j])] = j
	if best == 10 ** 6:
		print(-1)
	else:
		print(best + 1)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = []
	for i in range(n):
		b.append([])
	for i in range(n):
		b[a[i] - 1].append(i)
		ans = 10 ** 15
	for i in range(n):
		for j in range(max(0, len(b[i]) - 1)):
			ans = min(ans, b[i][j + 1] - b[i][j] + 1)
	if ans == 10 ** 15:
		print(-1)
		continue
	print(ans)


for _ in range(int(input())):
	(n, a) = (int(input()), input().split())
	d = n
	seen = {}
	for (i, a) in enumerate(a):
		if a in seen:
			d = min(d, i - seen[a])
		seen[a] = i
	print(d + 1 if d < n else -1)


q = int(input())
for i in range(q):
	n = int(input())
	arr = list(map(int, input().split()))
	memo = {}
	ret = n
	nf = True
	for (index, v) in enumerate(arr):
		if memo.get(v) == None:
			memo[v] = index
		else:
			ret = min(index - memo[v] + 1, ret)
			memo[v] = index
			nf = False
	if nf:
		print(-1)
	else:
		print(ret)


from __future__ import division, print_function
from collections import Counter

def main():
	import sys
	data = iter(map(int, sys.stdin.buffer.read().decode('ascii').split()))
	t = next(data)
	INF = 1000000
	res = [INF] * t
	for i in range(t):
		n = next(data)
		a = tuple((next(data) for _ in range(n)))
		d = dict()
		for (j, aj) in enumerate(a):
			if aj in d:
				last = d[aj]
				res[i] = min(res[i], j + 1 - last)
			d[aj] = j
		if res[i] == INF:
			res[i] = -1
	sys.stdout.buffer.write('\n'.join((str(x) for x in res)).encode('ascii'))
	return 0
main()


import random
import time
from collections import Counter
tcs = input()
for tc in range(int(tcs)):
	a_length = input()
	arr = input().split(' ')
	pre = {}
	shortest = -1
	for (i, a) in enumerate(arr):
		if a in pre:
			length = i - pre[a] + 1
			if shortest == -1:
				shortest = length
			shortest = min(shortest, length)
		pre[a] = i
	print(shortest)


def solve():
	n = int(input())
	a = list(map(int, input().split()))
	d = {}
	ans = float('inf')
	for (i, ai) in enumerate(a):
		if ai in d:
			ans = min(ans, i - d[ai] + 1)
		d[ai] = i
	if ans == float('inf'):
		ans = -1
	print(ans)
t = int(input())
for i in range(t):
	solve()


import os
import sys
from math import *
from collections import *
from fractions import *
from bisect import *
from heapq import *
from io import BytesIO, IOBase

def vsInput():
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

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

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

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

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

class IOWrapper(IOBase):

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

def value():
	return tuple(map(int, input().split()))

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

def Int():
	return int(input())

def Str():
	return input()

def arrayS():
	return [i for i in input().split()]
for _ in range(Int()):
	n = Int()
	a = array()
	if len(set(a)) == n:
		print(-1)
	else:
		freq = defaultdict(int)
		start = 0
		ans = inf
		i = 0
		while i < n:
			freq[a[i]] += 1
			if freq[a[i]] == 2:
				while freq[a[i]] == 2:
					freq[a[start]] -= 1
					start += 1
				ans = min(ans, i - start + 2)
			i += 1
		print(ans)


import sys
from collections import defaultdict
for _ in range(int(input())):
	d = defaultdict(int)
	n = int(input())
	a = list(sys.stdin.readline().split())
	mx = int(200000.0)
	top = 0
	r = 0
	l = -1
	ret = int(1000000.0)
	while r < n:
		cur = a[r]
		d[cur] += 1
		c = 0
		while d.get(cur) > 1:
			ret = min(ret, r - l)
			l += 1
			remove = a[l]
			d[remove] = d.get(remove) - 1
			c += 1
		r += 1
	sys.stdout.write(str(ret) if ret <= mx else '-1')
	sys.stdout.write('\n')


for ttt in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	if len(set(arr)) == n:
		print(-1)
	else:
		d = {}
		mini = 10 ** 9
		for i in range(n):
			if arr[i] not in d:
				d[arr[i]] = i + 1
			else:
				mini = min(mini, i + 1 - d[arr[i]] + 1)
				d.pop(arr[i])
				d[arr[i]] = i + 1
		print(mini)


for _ in range(int(input())):
	n = int(input())
	L = input().split()
	P = {}
	ans = 1e+18
	for i in range(len(L)):
		if L[i] in P.keys():
			ans = min(ans, i - P[L[i]] + 1)
		P[L[i]] = i
	print(-1 if ans == 1e+18 else ans)


def minDist(arr, n):
	mp = {}
	minDis = 100000000000
	for i in range(n):
		if minDis == 1:
			return 2
		if arr[i] not in mp.keys():
			mp[arr[i]] = i
		else:
			minDis = min(minDis, abs(i - mp[arr[i]]))
			mp[arr[i]] = i
	if minDis == 100000000000:
		return -1
	else:
		return minDis + 1
for _ in range(int(input())):
	n = int(input())
	li = list(map(int, input().split()))
	print(minDist(li, n))


import math
t = int(input())
for k in range(0, t):
	n = int(input())
	arr = input()
	arr = arr.split(' ')
	for i in range(0, len(arr)):
		arr[i] = int(arr[i])
	map = {}
	for i in range(0, len(arr)):
		if arr[i] in map:
			map[arr[i]].append(i)
		else:
			map[arr[i]] = [i]
	min_subarray = math.inf
	for value in map.values():
		if len(value) < min_subarray and len(value) >= 2:
			min_subarray = len(value)
	if min_subarray == math.inf:
		print(-1)
	else:
		min_distance = math.inf
		for j in range(2, min_subarray + 1):
			for value in map.values():
				for k in range(0, len(value) - (j - 1)):
					if value[k + (j - 1)] - value[k] < min_distance:
						min_distance = value[k + (j - 1)] - value[k]
					if min_distance == 1:
						break
				if min_distance == 1:
					break
			if min_distance == 1:
				break
		print(min_distance + 1)


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	dic = {}
	if n == 1:
		print(-1)
	else:
		ma = float('inf')
		flag = 0
		for i in arr:
			dic[i] = -1
		for i in range(n):
			if dic[arr[i]] == -1:
				dic[arr[i]] = i
			else:
				ma = min(i - dic[arr[i]] + 1, ma)
				dic[arr[i]] = i
				flag = 1
		if flag == 0:
			print(-1)
		else:
			print(ma)


import sys
input = sys.stdin.readline
ins = lambda : input().rstrip()
ini = lambda : int(input().rstrip())
inm = lambda : map(int, input().rstrip().split())
inl = lambda : list(map(int, input().split()))
out = lambda x, s='\n': print(s.join(map(str, x)))
t = ini()
for _ in range(t):
	n = ini()
	a = inl()
	if n == 1 or len(a) == len(set(a)):
		print(-1)
	else:
		count = {}
		ans = n
		for i in range(n):
			if a[i] not in count:
				count[a[i]] = i
			else:
				ans = min(ans, i - count[a[i]])
				count[a[i]] = i
		print(ans + 1)


for i in range(int(input())):
	d = dict()
	n = int(input())
	ans = n + 1
	l = [int(j) for j in input().split()]
	for i in range(n):
		if l[i] in d:
			ans = min(ans, i - d[l[i]] + 1)
			d[l[i]] = i
		else:
			d[l[i]] = i
	if ans != n + 1:
		print(ans)
	else:
		print(-1)


for _ in range(int(input())):
	n = int(input())
	ara = list(map(int, input().split()))
	di = {}
	lan = 10 ** 8
	for i in range(n):
		if ara[i] not in di:
			di[ara[i]] = i
		else:
			ind = i - di[ara[i]] + 1
			if ind < lan:
				lan = ind
			di[ara[i]] = i
	if lan == 10 ** 8:
		print(-1)
	else:
		print(lan)


t = int(input())
for _ in range(t):
	n = int(input())
	adjacentes = []
	for i in range(n + 1):
		adjacentes.append([])
	entrada = list(map(int, input().split(' ')))
	a = []
	for i in range(n):
		a.append(entrada[i])
		adjacentes[a[i]].append(i + 1)
	total = 200001
	for i in range(n + 1):
		if len(adjacentes[i]) > 1:
			for j in range(len(adjacentes[i]) - 1):
				k = adjacentes[i][j + 1] - adjacentes[i][j] + 1
				total = min(total, k)
	if total == 200001:
		print(-1)
	else:
		print(total)
	print()


T = int(input())
for _ in range(T):
	n = int(input())
	nums = list(map(int, input().split()))
	if len(nums) == 1:
		print(-1)
		continue
	minn = 2 * 10 ** 5 + 1
	d = {}
	for i in range(n):
		if nums[i] not in d.keys():
			d[nums[i]] = i
		else:
			minn = min(minn, abs(i - d[nums[i]]) + 1)
			d[nums[i]] = i
	if len(d) == n:
		print(-1)
	else:
		print(minn)


test = int(input())
for testing in range(test):
	n = int(input())
	t = [int(x) for x in input().split()]
	if n <= 1:
		print(-1)
		continue
	else:
		pointer = [[False, 0] for i in range(max(max(t), len(t)) + 1)]
		mini = 0
		first = True
		mini = -1
		for i in range(len(t)):
			if not pointer[t[i]][0]:
				pointer[t[i]][1] = i
				pointer[t[i]][0] = True
				continue
			if pointer[t[i]][0]:
				if first:
					mini = i - pointer[t[i]][1] + 1
					first = False
				else:
					a = i - pointer[t[i]][1] + 1
					if mini > a:
						mini = a
			pointer[t[i]][1] = i
			pointer[t[i]][0] = True
	print(mini)


for _ in range(int(input())):
	n = int(input())
	l = [int(x) for x in input().split()]
	m = [-1] * (n + 1)
	cnt = 987654321
	for x in range(n):
		if m[l[x]] != -1:
			if cnt > x - m[l[x]]:
				cnt = x - m[l[x]]
		m[l[x]] = x
	if cnt == 987654321:
		print(-1)
	else:
		print(cnt + 1)


t = int(input())
for tt in range(t):
	n = int(input())
	ent = list(map(int, input().split()))
	mn = 1000000000
	arr = [-1] * (n + 1)
	for i in range(n):
		if arr[ent[i]] == -1:
			arr[ent[i]] = i
		else:
			mn = min(mn, i - arr[ent[i]])
			arr[ent[i]] = i
	if mn == 1000000000:
		print(-1)
	else:
		print(mn + 1)


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	ans = []
	d = {}
	for i in range(n):
		if l[i] in d:
			ans.append(i - d[l[i]] + 1)
		d[l[i]] = i
	if ans != []:
		print(min(ans))
	else:
		print(-1)


for _ in range(int(input())):
	n = int(input())
	A = list(map(int, input().split()))
	D = set(A)
	if len(D) == n or n == 1:
		print('-1')
		continue
	ans = 1000000
	pos = [0 for i in range(max(A) + 1)]
	for i in range(n):
		if pos[A[i]] > 0:
			ans = min(ans, i - pos[A[i]] + 2)
		pos[A[i]] = i + 1
	print(ans)


def main():
	n = int(input())
	pos = [-1] * (n + 1)
	val = list(map(int, input().split()))
	ans = -1
	for (idx, v) in enumerate(val):
		if pos[v] is not -1:
			ans = idx - pos[v] + 1 if ans is -1 else min(ans, idx - pos[v] + 1)
		pos[v] = idx
	print(ans)
t = int(input())
for _ in range(t):
	main()


q = int(input())
for i in range(q):
	w = int(input())
	e = list(map(int, input().split()))
	if w > 1:
		r = dict()
		ans = 999999999999999999
		j = 0
		for i in e:
			if i in r:
				ans = min(ans, j - r[i] + 1)
				r[i] = j
			else:
				r[i] = j
			j += 1
		print(ans if ans != 999999999999999999 else -1)
	else:
		print(-1)


from collections import defaultdict
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	count = {i: [] for i in a}
	for (i, v) in enumerate(a):
		count[v].append(i)
	ans = n + 1
	for (uniq_num, occur) in count.items():
		if len(occur) >= 2:
			m = len(occur)
			for i in range(1, m):
				ans = min(ans, occur[i] - occur[i - 1])
	if ans == n + 1:
		print(-1)
	else:
		print(ans + 1)


import sys
for t in range(int(sys.stdin.readline())):
	n = int(sys.stdin.readline())
	d = [int(i) for i in sys.stdin.readline().split()]
	inds = [[] for i in range(n + 1)]
	for i in range(n):
		inds[d[i]].append(i)
	mn = float('inf')
	for i in range(1, n + 1):
		q = len(inds[i])
		kk = [inds[i][j + 1] - inds[i][j] for j in range(q - 1)]
		if len(kk):
			mn = min(mn, min(kk))
	if mn == float('inf'):
		print(-1)
	else:
		print(mn + 1)


I = input
for _ in [0] * int(I()):
	r = n = int(I())
	(a, b) = ({}, {})
	i = 0
	for x in map(int, I().split()):
		b[x] = min(b.get(x, n), i - a.get(x, -n))
		r = min(r, b[x])
		a[x] = i
		i += 1
	print((-1, r + 1)[r < n])


t = int(input())
for T in range(t):
	n = int(input())
	s = [int(i) for i in input().split()]
	length = n + 1
	a = [-1] * (n + 1)
	for i in range(n):
		if a[s[i]] != -1 and i - a[s[i]] + 1 < length:
			length = i - a[s[i]] + 1
		a[s[i]] = i
	if length > n:
		length = -1
	print(length)


T = int(input())
for i in range(T):
	n = int(input())
	lst = [int(w) for w in input().split()]
	if len(lst) < 2:
		print('-1')
	else:
		shortest = -2
		previous = {}
		for i in range(0, n):
			curr = lst[i]
			if curr not in previous:
				previous[curr] = i
			else:
				curr_len = i - previous[curr]
				if shortest > curr_len or shortest == -2:
					shortest = curr_len
				previous[curr] = i
		print(shortest + 1)


for _ in range(int(input())):
	mindist = 1000001
	n = int(input())
	arr = list(map(int, input().split()))
	arr = list(((c, i) for (i, c) in enumerate(arr)))
	arr = sorted(arr)
	if n < 2:
		print(-1)
		continue
	for i in range(n - 1):
		if arr[i][0] == arr[i + 1][0]:
			mindist = min(mindist, arr[i + 1][1] - arr[i][1] + 1)
	if mindist < 1000000:
		print(mindist)
	else:
		print(-1)


import math
t = int(input())
for si in range(t):
	n = int(input())
	a = [int(s) for s in input().split(' ')]
	mstack = [-1] * n
	ans = n + 1
	for i in range(len(a)):
		if mstack[a[i] - 1] != -1:
			ans = min(ans, i - mstack[a[i] - 1] + 1)
		mstack[a[i] - 1] = i
	if ans == n + 1 or ans == 1:
		print('-1')
		continue
	print(ans)


import sys
input = sys.stdin.readline

def main():
	T = int(input())
	for _ in range(T):
		n = int(input())
		a = list(map(int, input().split()))
		d = dict()
		ans = float('inf')
		for (i, x) in enumerate(a):
			if x not in d:
				d[x] = i
			else:
				ans = min(ans, i - d[x] + 1)
				d[x] = i
		if ans == float('inf'):
			print(-1)
		else:
			print(ans)
main()


inf = 10000000.0
for i in range(int(input())):
	minimal = inf
	n = int(input())
	a = list(map(int, input().split()))
	d = dict()
	for (ind, i) in enumerate(a):
		if i in d:
			minimal = min(minimal, ind - d[i] + 1)
		d[i] = ind
	print(minimal if minimal != inf else -1)


import collections

def dominatedSub(arr):
	if len(arr) < 2:
		return -1
	d = collections.defaultdict()
	minlen = float('inf')
	for (i, v) in enumerate(arr):
		if v not in d:
			d[v] = i
		else:
			l = i - d[v] + 1
			if l < minlen:
				minlen = l
			d[v] = i
	if minlen == float('inf'):
		minlen = -1
	return minlen
q = int(input())
for i in range(q):
	n = map(int, input().split())
	arr = list(map(int, input().split()))
	res = dominatedSub(arr)
	print(res)


for t in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	if n == 1:
		print(-1)
	else:
		m = n + 100
		a = [[] for i in range(n + 1)]
		for i in range(n):
			a[l[i]].append(i)
		m = 10 ** 18
		for i in a:
			if len(i) >= 2:
				for j in range(1, len(i)):
					m = min(m, i[j] - i[j - 1] + 1)
		if m > n:
			print(-1)
		else:
			print(m)


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().strip().split(' ')))
	x = [-1 for __ in range(n + 1)]
	ans = n + 90
	f = len(list(set(a)))
	if f == n:
		print(-1)
	elif f == 1 and f != n:
		print(2)
	else:
		for j in range(n):
			if x[a[j]] != -1:
				ans = min(ans, j - x[a[j]] + 1)
			x[a[j]] = j
		print(ans)


for i in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	mini = n
	s = set(l)
	d = {}
	if len(s) == n:
		print(-1)
		continue
	for i in range(n):
		try:
			if mini > i - d[l[i]]:
				mini = i - d[l[i]] + 1
			d[l[i]] = i
		except:
			d[l[i]] = i
	print(mini)


for _ in range(int(input())):
	n = int(input())
	arr = [int(k) for k in input().split()]
	dic = {}
	for i in range(n):
		if arr[i] not in dic:
			dic[arr[i]] = [i]
		else:
			dic[arr[i]].append(i)
	if len(dic) == n:
		print('-1')
	else:
		mini = n + 1
		for (i, j) in dic.items():
			if len(j) > 1:
				j.sort()
				for k in range(len(j) - 1):
					sumi = j[k + 1] - j[k] + 1
					mini = min(sumi, mini)
		print(mini)


t = int(input())
while t != 0:
	n = int(input())
	l = list(map(int, input().strip().split()))
	dic = {}
	for (index, i) in enumerate(l):
		if i not in dic:
			dic[i] = list()
			dic[i].append(index)
			continue
		dic[i].append(index)
	mini = 999999999
	flag = False
	for i in dic:
		lx = list()
		lx = dic[i]
		if len(lx) != 1:
			flag = True
			for x in range(0, len(lx) - 1):
				mini = min(mini, lx[x + 1] - lx[x])
	if flag:
		print(mini + 1)
	else:
		print(-1)
	t -= 1


t = int(input())
for ij in range(0, t):
	ans = []
	d = {}
	n = int(input())
	l = list(map(int, input().split()))
	for i in range(0, n):
		if l[i] in d:
			ans.append(i - d[l[i]] + 1)
		d[l[i]] = i
	if ans != []:
		print(min(ans))
	else:
		print(-1)


def solve(n, a):
	m = n + 1
	d = {}
	for i in range(n):
		if a[i] in d:
			m = min(m, i - d[a[i]] + 1)
		d[a[i]] = i
	return [m, -1][m > n]
r = []
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	r.append(solve(n, a))
print(*r, sep='\n')


t = int(input())
for i in range(t):
	sub = []
	ok = False
	n = int(input())
	a = list(map(int, input().split()))
	d = {}
	res = []
	o = 0
	for e in set(a):
		d[e] = []
	for i in range(len(a)):
		d[a[i]].append(i)
	for k in d:
		if len(d[k]) >= 2:
			for j in range(len(d[k]) - 1):
				res.append(d[k][j + 1] - d[k][j] + 1)
		else:
			o += 1
	if o == len(a):
		print(-1)
	else:
		print(min(res))


t = int(input().strip())
for i in range(t):
	n = int(input().strip())
	A = [int(j) for j in input().strip().split()]
	if n == 1:
		print('-1')
	else:
		result = n + 1
		right_ind = 1
		elems = {A[0]: 0}
		while result != 2 and right_ind < n:
			right = A[right_ind]
			if right in elems:
				dist = right_ind - elems.get(right) + 1
				if dist < result:
					result = dist
			elems.update({right: right_ind})
			right_ind += 1
		if result == n + 1:
			print('-1')
		else:
			print(result)


T = int(input())
results = []
for i in range(T):
	n = int(input())
	vals = input().split()
	a = [int(val) for val in vals]
	if len(a) < 2:
		results.append(-1)
	else:
		d = {}
		minLen = -1
		for i in range(n):
			a_i = a[i]
			if a_i in d:
				if minLen == -1:
					minLen = i - d[a_i] + 1
				else:
					minLen = min(minLen, i - d[a_i] + 1)
			d[a_i] = i
		results.append(minLen)
for result in results:
	print(result)


def main():

	def solve():
		n = int(input())
		aa = [int(a) for a in input().split()]
		pos = [[] for _ in range(n + 1)]
		for (i, a) in enumerate(aa):
			pos[a].append(i)
		res = n
		for pp in pos:
			if len(pp) > 1:
				for i in range(1, len(pp)):
					if pp[i] - pp[i - 1] < res:
						res = pp[i] - pp[i - 1]
		if res == n:
			print(-1)
		else:
			print(res + 1)
	q = int(input())
	for _ in range(q):
		solve()
main()


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
import math

def bin1(l, r, k, t, b, val, ans):
	if l > r:
		return ans
	else:
		mid = (l + r) // 2
		v = k ** mid
		if v == val:
			return v
		elif v > val:
			ans = mid
			return bin1(mid + 1, r, k, t, b, val, ans)
		else:
			return bin1(l, mid - 1, k, t, b, val, ans)

def bin2(l, r, k, t, b, val, ans):
	if l > r:
		return ans
	else:
		mid = (l + r) // 2
		v = t * k ** mid + b * mid
		if v == val:
			return v
		elif v > val:
			ans = mid
			return bin2(l, mid - 1, k, t, b, val, ans)
		else:
			return bin2(mid + 1, r, k, t, b, val, ans)

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

def bin(l, r, ll, val):
	if l > r:
		return -1
	else:
		mid = (l + r) // 2
		if val >= ll[mid][0] and val <= ll[mid][1]:
			return mid
		elif val < ll[mid][0]:
			return bin(l, mid - 1, ll, val)
		else:
			return bin(mid + 1, r, ll, val)

def deci(n):
	s = ''
	while n != 0:
		if n % 2 == 0:
			n = n // 2
			s = '0' + s
		else:
			n = n // 2
			s = '1' + s
	return s

def diff(s1, s2):
	if len(s1) < len(s2):
		v = len(s1)
		while v != len(s2):
			s1 = '0' + s1
			v = v + 1
	else:
		v = len(s2)
		while v != len(s1):
			s2 = '0' + s2
			v = v + 1
	c = 0
	for i in range(len(s1)):
		if s1[i:i + 1] != s2[i:i + 1]:
			c = c + 1
	return c
from sys import stdin, stdout

def fac(a, b):
	v = a
	while a != b:
		v *= a - 1
		a = a - 1
	return v

def bino(l, r, n):
	if l > r:
		return -1
	else:
		mid = (l + r) // 2
		val1 = math.log(n / mid + 1, 2)
		val2 = int(val1)
		if val1 == val2:
			return val1
		elif val1 < 1.0:
			return bino(l, mid - 1, n)
		else:
			return bino(mid + 1, r, n)

def binary(l, r, ll, val, ans):
	if l > r:
		return ans
	else:
		mid = (l + r) // 2
		if val % ll[mid] == 0:
			ans = ll[mid]
			return binary(l, mid - 1, ll, val, ans)
		else:
			return binary(l, mid - 1, ll, val, ans)

def odd(n, ans, s):
	if n % 2 != 0 or n in s:
		return ans
	else:
		s.add(n)
		return odd(n // 2, ans + 1, s)
t = int(input())
for i in range(t):
	n = int(input())
	l = list(map(int, input().split(' ')))
	l1 = []
	val = max(l)
	for i in range(val):
		l1.append([])
	for i in range(len(l)):
		l1[l[i] - 1].append(i + 1)
	c = -1
	mini = 100000000
	for i in range(len(l1)):
		if len(l1[i]) != 0 and len(l1[i]) != 1:
			for j in range(len(l1[i]) - 1):
				if mini > l1[i][j + 1] - l1[i][j] + 1:
					mini = l1[i][j + 1] - l1[i][j] + 1
	if mini == 100000000:
		print(-1)
	else:
		print(mini)


k = int(input())
for _ in range(k):
	n = int(input())
	a = list(map(int, input().split()))
	m = n
	if n == 1 or len(set(a)) == n:
		print(-1)
	else:
		l = dict()
		for i in range(n):
			if a[i] not in l.keys():
				l[a[i]] = [i]
			else:
				l[a[i]].append(i)
		for tab in l.values():
			for v in range(1, len(tab)):
				m = min(m, tab[v] - tab[v - 1])
		print(m + 1)


import sys
readline = sys.stdin.readline
readlines = sys.stdin.readlines
ns = lambda : readline().rstrip()
ni = lambda : int(readline().rstrip())
nm = lambda : map(int, readline().split())
nl = lambda : list(map(int, readline().split()))
prn = lambda x: print(*x, sep='\n')

def solve():
	n = ni()
	a = nl()
	d = dict()
	ans = -1
	for i in range(n):
		if a[i] in d:
			if ans < 0 or ans > i - d[a[i]] + 1:
				ans = i - d[a[i]] + 1
		d[a[i]] = i
	print(ans)
	return
T = ni()
for _ in range(T):
	solve()


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

def si():
	return input()

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

def li():
	return list(mi())
import math
t = ii()
for i in range(t):
	n = ii()
	a = li()
	l = [-1] * (n + 5)
	mn = 10000000.0
	for i in range(n):
		if l[a[i]] == -1:
			l[a[i]] = i
		else:
			mn = min(mn, i - l[a[i]] + 1)
			l[a[i]] = i
	if mn == 10000000.0:
		print(-1)
	else:
		print(mn)


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


from sys import stdin

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

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

def main():
	t = iin()
	while t:
		t -= 1
		n = iin()
		a = lin()
		d = {}
		for i in range(n):
			try:
				d[a[i]].append(i)
			except:
				d[a[i]] = [i]
		ans = -1
		for i in d:
			l = len(d[i])
			for j in range(l - 1):
				if ans == -1:
					ans = d[i][j + 1] - d[i][j] + 1
				else:
					ans = min(d[i][j + 1] - d[i][j] + 1, ans)
		print(ans)
main()


for _ in range(int(input())):
	size = int(input())
	vector = list(map(int, input().split()))
	dicio = {}
	for i in range(size):
		if vector[i] not in dicio:
			dicio[vector[i]] = [i]
		else:
			dicio[vector[i]].append(i)
	resp = 300000000
	for i in dicio:
		if len(dicio[i]) >= 2:
			for j in range(len(dicio[i]) - 1):
				resp = min(resp, dicio[i][j + 1] - dicio[i][j])
	if resp == 300000000:
		print(-1)
	else:
		print(resp + 1)


t = int(input())
for i in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	x = n + 1
	p = [-1] * (n + 1)
	for j in range(n):
		if p[l[j]] != -1:
			x = min(x, j - p[l[j]] + 1)
		p[l[j]] = j
	if x > n:
		x = -1
	print(x)


t = int(input())
for _ in range(t):
	n = int(input())
	idx = {}
	mx = {}
	for (i, a) in enumerate(map(int, input().split())):
		if a not in idx:
			idx[a] = i
			mx[a] = 10 ** 19
		else:
			mx[a] = min(mx[a], i - idx[a] + 1)
			idx[a] = i
	mn = min(mx.values())
	if mn < 10 ** 19:
		print(mn)
	else:
		print(-1)


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

class FastIO(IOBase):
	newlines = 0

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

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

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

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

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
for j in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	d = dict()
	m = 0
	mi = 99999999999999
	f = 0
	for i in range(n):
		if l[i] not in d:
			d.update({l[i]: i})
		else:
			d[l[i]] = i - d[l[i]]
			mi = min(mi, d[l[i]])
			d[l[i]] = i
	if mi == 99999999999999:
		print(-1)
	else:
		print(mi + 1)


from collections import defaultdict
T = int(input())
for _ in range(T):
	n = int(input())
	a = list(map(int, input().split()))
	d = defaultdict(int)
	ans = 10 ** 18
	for i in range(n):
		if a[i] in d:
			ans = min(ans, i - d[a[i]] + 1)
		d[a[i]] = i
	if ans == 10 ** 18:
		print(-1)
	else:
		print(ans)


import sys
input = sys.stdin.buffer.readline
from collections import defaultdict as dd
t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(x) for x in input().split()]
	cnts = dd(lambda : 0)
	l = 0
	r = 0
	ans = float('inf')
	moveRNext = True
	while True:
		if moveRNext:
			if r == n:
				break
			cnts[a[r]] += 1
			if cnts[a[r]] == 2:
				moveRNext = False
			r += 1
		else:
			cnts[a[l]] -= 1
			if cnts[a[l]] == 1:
				ans = min(ans, r - l)
				moveRNext = True
			l += 1
	if ans == float('inf'):
		print(-1)
	else:
		print(ans)


from sys import stdin

def rl():
	return [int(w) for w in stdin.readline().split()]
(t,) = rl()
for _ in range(t):
	(n,) = rl()
	a = rl()
	d = {}
	r = -1
	for (i, x) in enumerate(a):
		if x in d and (r < 0 or r > i - d[x] + 1):
			r = i - d[x] + 1
		d[x] = i
	print(r)


from collections import defaultdict
t = int(input())
while t > 0:
	t -= 1
	n = int(input())
	a = list(map(int, input().split()))
	minn = 1000000
	if n == 1:
		print(-1)
	else:
		d = dict()
		d = defaultdict(lambda : list(), d)
		for i in range(n):
			d[a[i]].append(i)
		q = 1
		for val in d.values():
			if len(val) != 1:
				q = 0
				break
		if q == 0:
			for val in d.values():
				for i in range(len(val) - 1):
					minn = min(minn, val[i + 1] - val[i])
			print(minn + 1)
		elif q == 1:
			print(-1)


for i in range(int(input())):
	n = int(input())
	ar = list(map(int, input().split()))
	if n == 1:
		print(-1)
	else:
		x = 0
		y = 1
		h = [0] * (n + 1)
		h[ar[x]] += 1
		h[ar[y]] += 1
		c = 999999
		while y < n:
			if h[ar[y]] == 2:
				if y - x + 1 < c:
					c = y - x + 1
				h[ar[x]] -= 1
				x += 1
			else:
				y += 1
				if y < n:
					h[ar[y]] += 1
		if c == 999999:
			print(-1)
		else:
			print(c)


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


from collections import defaultdict
T = int(input())
RES_MAX = 2 * 10 ** 5 + 1
for t in range(T):
	n = int(input())
	a = list(map(int, input().split()))
	if n == 1:
		print(-1)
	else:
		d = {}
		res = 2 * 10 ** 5 + 1
		for i in range(n):
			if a[i] in d:
				res = min(res, i - d[a[i]] + 1)
				d[a[i]] = i
			else:
				d[a[i]] = i
		if res < RES_MAX:
			print(res)
		else:
			print(-1)


INF = int(1000000000.0)
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	lst = [-1] * (n + 1)
	ans = INF
	for i in range(n):
		if lst[a[i]] != -1:
			ans = min(ans, i - lst[a[i]] + 1)
		lst[a[i]] = i
	if ans == INF:
		ans = -1
	print(ans)


t = int(input())

def slove():
	n = int(input())
	a = input().split(' ')
	mp = {}
	ans = 1000000000
	for i in range(len(a)):
		if mp.get(a[i]):
			ans = min(int(i + 1 - int(mp[a[i]])), ans)
			mp[a[i]] = i + 1
		else:
			mp[a[i]] = i + 1
	if ans == 1000000000:
		print(-1)
	else:
		print(ans + 1)
while t > 0:
	t -= 1
	slove()


import random
import time
from collections import Counter
tcs = input()
for tc in range(int(tcs)):
	a_length = input()
	arr = input().split(' ')
	shortest = -1
	setver = set(arr)
	enumver = list(enumerate(arr))
	arr_length = len(arr)
	set_length = len(setver)
	sumtime = 0
	if set_length != arr_length:
		counts = Counter(arr)
		indices_list = {}
		lengths_list = {}
		for (ind, x) in enumver:
			if x not in indices_list:
				indices_list[x] = [ind]
			else:
				indices_list[x].append(ind)
		for i in range(len(arr)):
			a = arr[i]
			count = counts[a]
			if count == 1:
				continue
			indices = indices_list[a]
			if len(indices) <= 1:
				continue
			if a not in lengths_list:
				min_length = arr_length
				for index_of_index in range(len(indices) - 1):
					length = indices[index_of_index + 1] - indices[index_of_index] + 1
					if length <= min_length:
						min_length = length
				length = min_length
				lengths_list[a] = length
			else:
				length = lengths_list[a]
			if length < shortest or shortest == -1:
				shortest = length
			if shortest == 2:
				break
	print(shortest)


for T in range(int(input())):
	n = int(input())
	s = input().split()
	if n < 2:
		print(-1)
		continue
	occ = [-1] * (n + 1)
	d_min = 2 * 10 ** 5 + 1
	t = []
	for i in range(n):
		v = int(s[i])
		if occ[v] != -1:
			d = i - occ[v] + 1
			if d < d_min:
				d_min = d
		occ[v] = i
	print(d_min if d_min != 2 * 10 ** 5 + 1 else -1)


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


import sys
input = sys.stdin.readline
from collections import deque
q = int(input())
for _ in range(q):
	n = int(input())
	x = list(map(int, input().split()))
	shortes = 9999999999
	dic = {}
	for i in range(n):
		if x[i] in dic.keys():
			if i - dic[x[i]] < shortes:
				shortes = i - dic[x[i]]
		dic[x[i]] = i
	if shortes == 9999999999:
		print(-1)
	else:
		print(shortes + 1)


T = int(input())
while T > 0:
	n = int(input())
	arr = list(map(int, input().split()))
	dic = {}
	start = 0
	end = 0
	res = n + 1
	while end < n:
		if arr[end] in dic:
			res = min(res, end - dic[arr[end]] + 1)
			while arr[start] != arr[end]:
				dic.pop(arr[start])
				start += 1
			start += 1
		dic[arr[end]] = end
		end += 1
	if res == n + 1:
		res = -1
	print(res)
	T -= 1


import sys
from collections import defaultdict

def get(arr):
	n = len(arr)
	if n == 1:
		return -1
	ans = float('inf')
	dic = defaultdict(int)
	(l, r) = (0, 0)
	rem = -1
	while l < n:
		while r < n and dic[rem] < 2:
			dic[arr[r]] += 1
			if dic[arr[r]] == 2:
				ans = min(ans, r - l + 1)
				rem = arr[r]
				r += 1
				break
			r += 1
		dic[arr[l]] -= 1
		l += 1
		if dic[rem] >= 2:
			ans = min(ans, r - l)
	if ans == float('inf'):
		return -1
	return ans
t = int(sys.stdin.readline())
for _ in range(t):
	n = int(sys.stdin.readline())
	arr = list(map(int, sys.stdin.readline().split()))
	print(get(arr))


import sys, atexit
from io import BytesIO
inp = BytesIO(sys.stdin.buffer.read())
input = lambda : inp.readline().decode('ascii')
buf = BytesIO()
atexit.register(lambda : sys.__stdout__.buffer.write(buf.getvalue()))
for i in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	g = [[] for i in range(n + 1)]
	for i in range(n):
		g[a[i]].append(i)
	ans = 10 ** 7
	f = False
	for i in range(1, n + 1):
		if len(g[i]) > 1:
			f = True
			for j in range(1, len(g[i])):
				ans = min(ans, g[i][j] - g[i][j - 1] + 1)
	if f:
		print(ans)
	else:
		print(-1)


def main():
	answer = ''
	for query in range(int(input())):
		ans = None
		n = int(input())
		array = list(map(int, input().split(' ')))
		ocurrences = dict()
		for index in range(len(array)):
			num = array[index]
			if ocurrences.get(num) == None:
				ocurrences[num] = [index]
			else:
				a = ocurrences[num]
				a.append(index)
				ocurrences[num] = a
		total_min_range = -1
		for key in ocurrences.keys():
			lst = ocurrences[key]
			if len(lst) > 1:
				min_range = abs(lst[1] - lst[0])
				for index in range(len(lst) - 1):
					min_range = min(min_range, abs(lst[index + 1] - lst[index]))
				if total_min_range == -1 or total_min_range >= min_range:
					total_min_range = min_range
		if total_min_range != -1:
			total_min_range += 1
		answer += str(total_min_range) + '\n'
	print(answer)
main()


import sys
input = sys.stdin.readline
t = int(input())
while t:
	t -= 1
	n = int(input())
	a = list(map(int, input().split()))
	ind = {}
	ans = float('inf')
	for (i, el) in enumerate(a):
		if el in ind:
			ans = min(ans, i - ind[el] + 1)
		ind[el] = i
	print(-1 if ans > n else ans)


s = ''
for i in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	if n == 1:
		s += str(-1) + '\n'
	else:
		v = [0] * (n + 2)
		f = [200001] * (n + 2)
		for i in range(n):
			if v[a[i]] > 0:
				f[a[i]] = min(f[a[i]], i - v[a[i]] + 2)
				v[a[i]] = i + 1
			else:
				v[a[i]] = i + 1
				f[a[i]] = 200001
		h = min(f)
		if h == 200001:
			s += str(-1) + '\n'
		else:
			s += str(h) + '\n'
print(s)


for _ in '0' * int(input()):
	n = int(input())
	ns = list(map(int, input().split()))
	(m, d) = (n + 1, {})
	for i in range(n):
		if ns[i] in d.keys():
			m = min(m, i - d[ns[i]] + 1)
		d[ns[i]] = i
	print(m if m != n + 1 else -1)


ma = 10 ** 9
from collections import defaultdict as dd
for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	ans = ma
	d = dd(lambda : -1)
	for i in range(n):
		if d[arr[i]] != -1:
			ans = min(ans, i - d[arr[i]] + 1)
			d[arr[i]] = i
		else:
			d[arr[i]] = i
	print(-1 if ans == ma else ans)


T = int(input())
C = []
for e in range(T):
	n = int(input())
	A = list(map(int, input().split()))
	D = {}
	flag = 0
	x = len(A)
	for i in range(len(A)):
		if A[i] not in D:
			D[A[i]] = i
		else:
			x = min(x, i + 1 - D[A[i]])
			D[A[i]] = i
			flag += 1
	C.append(-1 if flag == 0 else x)
for i in range(len(C)):
	print(C[i])


T = int(input())
answer = ''
inf = float('inf')

def min_delta(Arr: list):
	ret = float('inf')
	for i in range(0, len(Arr) - 1):
		delta = Arr[i + 1] - Arr[i] + 1
		ret = min(ret, delta)
	return ret
for t in range(T):
	n = int(input())
	arr = list(map(int, input().split()))
	indexes = dict()
	for i in range(n):
		if indexes.get(arr[i]):
			indexes[arr[i]].append(i)
		else:
			indexes[arr[i]] = [i]
	result = inf
	for key in indexes:
		if len(indexes[key]) > 1:
			result = min(min_delta(indexes[key]), result)
	if result == inf:
		result = -1
	answer += str(result) + '\n'
print(answer)


import math
import collections
a = collections.OrderedDict()

def main():
	t = int(input())
	for i in range(t):
		n = int(input())
		t = tuple([int(x) for x in input().split()])
		result = None
		occ = dict()
		for k in range(len(t)):
			ele = t[k]
			if ele in occ:
				new_result = k - occ[ele] + 1
				if result is None or new_result < result:
					result = new_result
			occ[ele] = k
		if result is None:
			print(-1)
		else:
			print(result)

def __starting_point():
	main()
__starting_point()


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	mem = {}
	for i in range(n):
		try:
			mem[arr[i]].append(i)
		except:
			mem[arr[i]] = [i]
	sub = list(mem.values())
	ans = 10 ** 7
	for i in sub:
		l = len(i)
		if l > 1:
			for j in range(1, l):
				if i[j] - i[j - 1] < ans:
					ans = i[j] - i[j - 1]
	if ans == 10 ** 7:
		print(-1)
	else:
		print(ans + 1)


from math import inf
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	els = set()
	els.add(a[0])
	r = 0
	l = 0
	short = inf
	while l != len(a) - 1:
		r = min(max(l + 1, r), len(a) - 1)
		if a[r] in els:
			if a[l] != a[r]:
				l += 1
				els.remove(a[l - 1])
			else:
				short = min(short, r - l + 1)
				l += 1
				r += 1
		else:
			els.add(a[r])
			r += 1
	if short == inf:
		print(-1)
	else:
		print(short)


def main():
	inf = 10 ** 9
	T = int(input())
	Ans = []
	for _ in range(T):
		N = int(input())
		A = list(map(int, input().split()))
		L = [-inf] * (N + 1)
		ans = inf
		for (i, a) in enumerate(A):
			ans = min(ans, i - L[a])
			L[a] = i
		Ans.append(ans + 1 if ans < inf else -1)
	print('\n'.join(map(str, Ans)))
main()


t = int(input())
while t:
	t -= 1
	(n, a, res) = (int(input()), [int(i) for i in input().split()], 300000)
	ls = [-1] * (n + 1)
	resi = -1
	for i in range(n):
		if ls[a[i]] != -1 and resi < ls[a[i]]:
			res = min(i - ls[a[i]] + 1, res)
		ls[a[i]] = i
	if res > n:
		res = -1
	print(res)


from collections import defaultdict as dc
from collections import deque as dq
from bisect import bisect_left, bisect_right, insort_left
import sys
import math
mod = 10 ** 9 + 7

def bs(a, x):
	i = bisect_left(a, x)
	if i != len(a):
		return i
	else:
		return len(a)

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

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

def bs(a, b):
	l = a
	r = b + 1
	x = b
	ans = 0
	while l < r:
		mid = (l + r) // 2
		if x | mid > ans:
			ans = x | mid
			l = mid + 1
		else:
			r = mid
	return ans

def digit(n):
	a = []
	while n > 0:
		a.append(n % 10)
		n = n // 10
	return a

def fibo(n, p):
	a = 1
	b = 1
	for i in range(2, n + 1):
		c = a + b
		a = b
		b = c
		if p[i] == 1:
			p[i] = c
for _ in range(inp()):
	n = inp()
	ans = 0
	a = line()
	z = float('inf')
	p = dc(list)
	for i in range(n):
		p[a[i]].append(i + 1)
	for i in list(p.keys()):
		l = p[i]
		if len(l) > 1:
			ans = 1
			for j in range(1, len(l)):
				z = min(z, l[j] - l[j - 1] + 1)
	if ans == 0:
		print(-1)
	else:
		print(z)


t = int(input())
while t > 0:
	n = int(input())
	a = [n]
	a = list(map(int, input().split()))
	dic = {}
	m = 0
	dic[a[0]] = [0]
	result = []
	for i in range(1, n):
		if a[i] in dic:
			l = len(dic[a[i]])
			if l == 1:
				x = dic[a[i]][0]
				m = i - x
				result.append(m)
			dic[a[i]].append(i)
			if l >= 1:
				m = min(m, i - dic[a[i]][l - 1])
				result.append(m)
		else:
			dic[a[i]] = [i]
	if len(result) == 0:
		print(-1)
	else:
		print(min(result) + 1)
	t = t - 1


for t in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	(m, d) = (n + 1, {})
	for i in range(n):
		if l[i] in d.keys():
			m = min(m, i - d[l[i]] + 1)
		d[l[i]] = i
	print(m if m != n + 1 else -1)


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


import sys, math
input = sys.stdin.readline

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

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

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

def invr():
	(a, b) = map(int, input().split())
	return (a, b)
t = 1
t = int(input())
for i in range(t):
	n = inp()
	a = inlt()
	q = dict()
	for (i, x) in enumerate(a):
		if x in q:
			n = min(n, i - q[x])
		q[x] = i
	if n < len(a):
		print(n + 1)
	else:
		print(-1)


import sys
input = sys.stdin.readline
from collections import Counter

def main():
	T = int(input())
	for _ in range(T):
		N = int(input())
		A = [int(x) for x in input().split()]
		if N == 1:
			print(-1)
		else:
			mi = N + 1
			S = Counter()
			i = 0
			for j in range(N):
				S[A[j]] += 1
				while S[A[j]] == 2:
					mi = min(mi, j - i + 1)
					S[A[i]] -= 1
					i += 1
			if mi == N + 1:
				print(-1)
			else:
				print(mi)
main()


import math
import sys
from collections import defaultdict, Counter
from functools import lru_cache
t = int(input())
for _ in range(t):
	n = int(input())
	arr = [int(i) for i in input().split()]
	count = Counter(arr)
	vals = list(count.values())
	m = max(vals)
	if m == 1:
		print(-1)
	else:
		start = 0
		end = 0
		Dict = defaultdict(int)
		ans = math.inf
		while end < n:
			Dict[arr[end]] += 1
			while Dict[arr[end]] == 2:
				ans = min(ans, end - start + 1)
				Dict[arr[start]] -= 1
				start += 1
			end += 1
		print(ans)


from sys import stdin, stdout
import collections

def main():
	T = int(input())
	for _ in range(T):
		n = int(input())
		a = [int(x) for x in input().split(' ')]
		prev_idx = {}
		min_dist = float('inf')
		for (i, v) in enumerate(a):
			if v in prev_idx:
				min_dist = min(min_dist, i - prev_idx[v] + 1)
			prev_idx[v] = i
		print(-1 if min_dist == float('inf') else min_dist)

def __starting_point():
	main()
__starting_point()


for _ in range(int(input())):
	n = int(input())
	li = list(map(int, input().split()))
	count_dict = {}
	flag = True
	for i in range(n):
		if count_dict.get(li[i]) == None:
			count_dict[li[i]] = (i, n)
		else:
			val = i - count_dict[li[i]][0] + 1
			flag = False
			if val < count_dict[li[i]][1]:
				count_dict[li[i]] = (i, val)
			else:
				count_dict[li[i]] = (i, count_dict[li[i]][1])
	if flag:
		print(-1)
	else:
		li = list(count_dict.values())
		length = len(li)
		mini = li[0][1]
		for i in range(1, length):
			if mini > li[i][1]:
				mini = li[i][1]
		print(mini)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	l = [-2 * 10 ** 6] * (n + 1)
	mn = 2 * 10 ** 6
	for i in range(n):
		mn = min(mn, i - l[a[i]] + 1)
		l[a[i]] = i
	if mn == 2 * 10 ** 6:
		print(-1)
	else:
		print(mn)


for _ in range(int(input())):
	n = int(input())
	d = {}
	r = 2000000.0
	i = 0
	for x in map(int, input().split()):
		r = min(r, i - d.get(x, i - r) + 1)
		d[x] = i
		i += 1
	print((r, -1)[r > n])


for tc in range(int(input())):
	n = int(input())
	line = input().split()
	arr = []
	lengths = []
	d = {}
	for i in range(n):
		element = int(line[i])
		if d.get(element, -1) >= 0:
			lengths.append(i - d.get(element))
		d[element] = i
	if len(lengths) == 0:
		print('-1')
	else:
		print(min(lengths) + 1)


from collections import defaultdict
t = int(input())
for i in range(t):
	input()
	xs = [int(x) for x in input().split()]
	cs = defaultdict(list)
	for (idx, x) in enumerate(xs):
		cs[x].append(idx)
	min = None
	for (item, pos) in cs.items():
		for (l, r) in zip(pos, pos[1:]):
			if min is None or r - l < min:
				min = r - l
	print(-1 if min is None else min + 1)


import os
import sys
import math
import heapq
from decimal import *
from io import BytesIO, IOBase
from collections import defaultdict, deque

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

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

def rl():
	return list(map(int, input().split()))
for _ in range(r()):
	n = r()
	a = rl()
	mini = math.inf
	occ = defaultdict(int)
	for i in range(1, n + 1):
		if occ[a[i - 1]] != 0:
			mini = min(mini, i - occ[a[i - 1]] + 1)
		occ[a[i - 1]] = i
	print(-1 if mini == math.inf else mini)


import sys
import os.path
import functools
import itertools
import re

def solve():
	for tests in range(int(input())):
		n = int(input())
		a = list(map(int, input().split()))
		last = [-1] * (n + 2)
		ans = n << 1
		for (i, val) in enumerate(a, 1):
			if last[val] != -1:
				ans = min(ans, i - last[val] + 1)
			last[val] = i
		print(-1 if ans > n else ans)

def main():
	if os.path.exists('test.inp'):
		sys.stdin = open('test.inp', 'r')
	solve()
main()


T = int(input())
x = {}
while T != 0:
	T -= 1
	s = int(input())
	a = list(map(int, input().split()))
	minn = 999999
	for i in range(s):
		if a[i] not in x:
			x[a[i]] = i
		else:
			if minn > i - x[a[i]] + 1:
				minn = i - x[a[i]] + 1
			x[a[i]] = i
	if minn == 999999:
		print('-1')
	else:
		print(minn)
	x.clear()


from sys import stdin, stdout
for i in stdin:
	t = int(i)
	break

def dom(L):
	d = {}
	for i in range(len(L)):
		if L[i] in d.keys():
			d[L[i]].append(i)
		else:
			d[L[i]] = [i]
	return d

def asd(L):
	return [L[i + 1] - L[i] for i in range(len(L) - 1)]
for i in range(t):
	for i in stdin:
		n = int(i)
		break
	for i in stdin:
		N = list(map(int, i.strip().split()))
		break
	D = dom(N)
	if n == 1:
		stdout.write('-1\n')
	elif n == 2:
		if N[0] == N[1]:
			stdout.write('2\n')
		else:
			stdout.write('-1\n')
	else:
		d = list(dom(N).values())
		x = [asd(i) for i in d]
		s = []
		for i in x:
			s += i
		if s == []:
			stdout.write('-1\n')
		else:
			stdout.write(str(min(s) + 1) + '\n')


from sys import stdin
for _ in range(int(stdin.readline())):
	n = int(stdin.readline())
	arr = list(map(int, stdin.readline().split()))
	d = {}
	for i in range(n):
		if arr[i] in d:
			d[arr[i]].append(i)
		else:
			d[arr[i]] = [i]
	ans = list(filter(lambda x: len(x) >= 2, d.values()))
	if len(ans) == 0:
		print(-1)
	else:
		m = 10000000
		for ele in ans:
			for i in range(len(ele) - 1):
				m = min(ele[i + 1] - ele[i], m)
		print(m + 1)


import sys

def I():
	return sys.stdin.readline().rstrip()
for _ in range(int(I())):
	n = int(I())
	a = list(map(int, I().split()))
	d = dict()
	for (i, x) in enumerate(a):
		if x in d:
			n = min(n, i - d[x])
		d[x] = i
	print(n + 1 if n < len(a) else -1)


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	d = {}
	k = []
	if len(set(l)) == n:
		print(-1)
	else:
		for i in range(n):
			if l[i] in d:
				k.append(i - d[l[i]][-1] + 1)
				d[l[i]] += [i]
			else:
				d[l[i]] = [i]
		print(min(k))


for i in range(int(input())):
	n = int(input())
	a = [int(x) for x in input().split()]
	anser = []
	d = {}
	for j in range(n):
		if a[j] in d:
			anser.append(j - d[a[j]] + 1)
		d[a[j]] = j
	if anser == []:
		print(-1)
	else:
		print(min(anser))


t = int(input())
for j in range(t):
	n = int(input())
	l = [int(i) for i in input().split()]
	hashmap = [[]]
	for i in range(n):
		hashmap.append([])
	for i in range(n):
		hashmap[l[i]].append(i)
	flag = 0
	for i in range(1, n + 1):
		if len(hashmap[i]) > 1:
			flag = 1
			break
	if flag == 0:
		print(-1)
	else:
		min1 = 100000000
		for i in range(1, n + 1):
			l1 = hashmap[i]
			length = len(l1)
			if length > 1:
				for k in range(length - 1):
					if l1[k + 1] - l1[k] < min1:
						min1 = l1[k + 1] - l1[k]
		print(min1 + 1)


for i in range(int(input())):
	input()
	M = [int(n) for n in input().split()]
	D = dict()
	for i in range(len(M)):
		e = M[i]
		if e not in D:
			D[e] = []
		D[e].append(i)
	L = [x for x in D.values() if len(x) > 1]
	if not L:
		print(-1)
	else:
		m = L[0][1] - L[0][0]
		for lis in L:
			for i in range(len(lis) - 1):
				if lis[i + 1] - lis[i] < m:
					m = lis[i + 1] - lis[i]
		print(m + 1)


t = int(input())
for i in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	if len(l) == len(set(l)):
		print(-1)
	else:
		dic = {}
		mindiff = 300000
		for i in range(n):
			if l[i] in dic:
				mindiff = min(mindiff, i - dic[l[i]])
			dic[l[i]] = i
		print(mindiff + 1)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = {}
	minr = -1
	for i in range(n):
		if a[i] not in b:
			b[a[i]] = []
		b[a[i]].append(i)
	for pa in b.values():
		if len(pa) > 1:
			for i in range(1, len(pa)):
				if pa[i] - pa[i - 1] + 1 < minr or minr == -1:
					minr = pa[i] - pa[i - 1] + 1
				if minr == 2:
					break
		if minr == 2:
			break
	print(minr)


def ind(l, x):
	t = []
	for i in range(len(l)):
		if l[i] == x:
			t.append(i)
	return t
t = int(input())
l = []
for i in range(t):
	n = int(input())
	l.append([int(i) for i in input().split()])
for a in l:
	p = len(a) + 1
	t = [[] for i in range(len(a))]
	for i in range(len(a)):
		if t[a[i] - 1] == []:
			t[a[i] - 1].append(i)
		else:
			if i - t[a[i] - 1][0] + 1 < p:
				p = i - t[a[i] - 1][0] + 1
			t[a[i] - 1][0] = i
			if p == 2:
				break
	if p <= len(a):
		print(p)
	else:
		print(-1)


from collections import Counter
T = int(input())
for t in range(T):
	n = int(input())
	a = input().split(' ')
	count_a = Counter(a)
	(dist, last_index) = ({}, {})
	for i in range(n):
		if count_a[str(a[i])] > 1:
			if not str(a[i]) in dist:
				last_index[str(a[i])] = i
				dist[str(a[i])] = int(0)
			else:
				if dist[str(a[i])] == 0:
					dist[str(a[i])] = i - last_index[str(a[i])]
				else:
					dist[str(a[i])] = min(i - last_index[str(a[i])], dist[str(a[i])])
				last_index[str(a[i])] = i
	if len(dist) == 0:
		print(-1)
	else:
		print(min(dist.values()) + 1)


for i in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	v = [-1] * (n + 1)
	m = 999999999
	for i in range(n):
		if v[arr[i]] != -1:
			m = min(m, i - v[arr[i]] + 1)
		v[arr[i]] = i
	if m == 999999999:
		print('-1')
	else:
		print(m)


def solve(n, seq):
	table = {}
	distance = n + 1
	index = 0
	while index < n:
		if seq[index] not in table:
			table[seq[index]] = index
		else:
			dis2 = index - table[seq[index]] + 1
			if dis2 < distance:
				distance = dis2
			table[seq[index]] = index
		index += 1
	if distance > n:
		return -1
	return distance
for x in range(int(input())):
	n = int(input())
	seq = list(map(int, input().split()))
	print(solve(n, seq))


import sys
input = sys.stdin.readline
INF = 10 ** 15
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:
	ans = INF
	dic = {}
	for (i, a) in enumerate(A):
		if a in dic:
			ans = min(ans, i - dic[a] + 1)
			dic[a] = i
		else:
			dic[a] = i
	if ans == INF:
		ans = -1
	print(ans)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	d = {}
	c = 10 ** 7
	for i in range(n):
		if a[i] not in d:
			d[a[i]] = i
		else:
			z = i - d[a[i]] + 1
			if z < c:
				c = z
			d[a[i]] = i
	if c == 10 ** 7:
		print(-1)
	else:
		print(c)


for cas in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	last = [-1] * (n + 1)
	ans = n + 1
	for i in range(n):
		x = a[i]
		if last[x] != -1:
			ans = min(ans, i - last[x] + 1)
		last[x] = i
	print((ans, -1)[ans == n + 1])


from bisect import bisect_right
from bisect import bisect_left
from collections import defaultdict
from math import sqrt, factorial, gcd, log2, inf, ceil
t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	flag = 0
	hash = defaultdict(int)
	posn = defaultdict(int)
	mini = defaultdict(lambda : inf)
	for i in range(n):
		if i != 0:
			if l[i] == l[i - 1]:
				print(2)
				flag = 1
				break
		hash[l[i]] += 1
		if hash[l[i]] > 1:
			mini[l[i]] = min(i - posn[l[i]] + 1, mini[l[i]])
		posn[l[i]] = i
	if not flag:
		flag = 0
		ans = inf
		for i in mini:
			if mini[i] != inf:
				flag = 1
				ans = min(mini[i], ans)
		if flag == 0:
			print(-1)
		else:
			print(ans)


for q in range(int(input())):
	n = int(input())
	s = [int(i) for i in input().split()]
	d = {}
	mn = float('inf')
	for i in range(n):
		if d.get(s[i]) is not None and i - d[s[i]] + 1 < mn:
			mn = i - d[s[i]] + 1
		d[s[i]] = i
	if mn > n:
		print(-1)
	else:
		print(mn)


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	d = {}
	ans = n + 1
	for i in range(n):
		if arr[i] not in d:
			d[arr[i]] = i
		else:
			ans = min(ans, i - d[arr[i]] + 1)
			d[arr[i]] = i
	if ans != n + 1:
		print(ans)
	else:
		print(-1)


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


from collections import defaultdict, deque
from itertools import permutations
from sys import stdin, stdout
from bisect import bisect_left, bisect_right
from copy import deepcopy
int_input = lambda : int(stdin.readline())
string_input = lambda : stdin.readline()
multi_int_input = lambda : map(int, stdin.readline().split())
multi_input = lambda : stdin.readline().split()
list_input = lambda : list(map(int, stdin.readline().split()))
string_list_input = lambda : list(string_input())
MOD = pow(10, 9) + 7
test = int_input()
for _ in range(test):
	n = int_input()
	array = list_input()
	dictionary = {}
	for (i, n) in enumerate(array):
		if n in dictionary:
			dictionary[n].append(i)
		else:
			dictionary[n] = [i]
	mini = 9999999999999999
	for elements in dictionary:
		if len(dictionary[elements]) > 0:
			for i in range(1, len(dictionary[elements])):
				mini = min(mini, dictionary[elements][i] - dictionary[elements][i - 1])
	if mini == 9999999999999999:
		print(-1)
	else:
		print(mini + 1)


def solution(arr):
	d = {}
	ans = len(arr) + 1
	for i in range(len(arr)):
		val = d.get(arr[i], -1)
		if val == -1:
			d[arr[i]] = i
		else:
			dif = i - d[arr[i]]
			d[arr[i]] = i
			ans = min(ans, dif)
	if ans == len(arr) + 1:
		return -1
	return ans + 1
for _ in range(int(input())):
	n = int(input())
	arr = [int(x) for x in input().split()]
	ans = solution(arr)
	print(ans)


from collections import Counter
import math

def printArr(arr):
	for i in arr:
		print(i, end=' ')

def proA(arr):
	if len(arr) == 1:
		return -1
	dic = {}
	mini = math.inf
	for i in range(len(arr)):
		if dic.get(arr[i]) == None:
			dic[arr[i]] = [i]
		else:
			dic[arr[i]].append(i)
	for (i, j) in dic.items():
		if len(j) == 1:
			continue
		for k in range(len(j) - 1):
			mini = min(mini, j[k + 1] - j[k] + 1)
	if mini == math.inf:
		return -1
	return mini
t = int(input())
for i in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	print(proA(arr))


def main():
	print = out.append
	n = get_int()
	li = get_list()
	indexs = defaultdict(list)
	for (i, ele) in enumerate(li):
		indexs[ele].append(i)
	res = -1
	for key in indexs:
		for j in range(1, len(indexs[key])):
			res = min(res, indexs[key][j] - indexs[key][j - 1] + 1) if res != -1 else indexs[key][j] - indexs[key][j - 1] + 1
	print(res)
import sys
from collections import defaultdict
input = iter(sys.stdin.buffer.read().decode().splitlines()).__next__
out = []
get_int = lambda : int(input())
get_list = lambda : list(map(int, input().split()))
[main() for _ in range(int(input()))]
print(*out, sep='\n')


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	l1 = []
	a1 = [0] * (n + 1)
	a2 = [0] * (n + 1)
	for i in range(n):
		if a1[l[i]] == 0:
			a1[l[i]] = 1
			a2[l[i]] = i
		else:
			ans = i - a2[l[i]]
			a2[l[i]] = i
			l1.append(ans)
	if len(l1) == 0:
		print('-1')
	else:
		ans = min(l1)
		print(ans + 1)


for i in range(int(input())):
	input()
	array = list(map(int, input().split()))
	search = {e: -1 for e in array}
	size = 1000000000.0
	for (i, e) in enumerate(array):
		if e in search:
			if search[e] != -1:
				size = min(size, i - search[e])
			search[e] = i
	print(-1 if size == 1000000000.0 else size + 1)


t = int(input())
for _ in range(t):
	n = int(input())
	y = list(map(int, input().split()))
	arr = [-1] * (n + 1)
	mina = -1
	i = 0
	while i < n:
		if arr[y[i]] == -1:
			arr[y[i]] = i
		else:
			if mina == -1:
				mina = i - arr[y[i]] + 1
			elif mina > i - arr[y[i]]:
				mina = i - arr[y[i]] + 1
			arr[y[i]] = i
		i += 1
	print(mina)


for j in range(int(input())):
	subArrayIndexes = {}
	size = int(input())
	results = 200000
	arr = input().split()
	for i in range(size):
		if arr[i] in subArrayIndexes.keys():
			results = min(results, i + 1 - subArrayIndexes[arr[i]])
			if results == 1:
				break
		subArrayIndexes[arr[i]] = i
	print(results if results != 200000 else -1)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	x = n + 1
	b = ['*'] * (n + 1)
	for i in range(n):
		if b[a[i]] != '*':
			x = min(x, i - b[a[i]] + 1)
		b[a[i]] = i
	print([-1, x][x < n + 1])


t = int(input())
for i in range(t):
	n = int(input())
	nums = list(map(int, input().split()))
	i = 0
	j = 0
	min_len = 100010
	sub = set()
	while i < n and j < n:
		sub_len = len(sub)
		sub.add(nums[j])
		if len(sub) == sub_len:
			min_len = min(min_len, sub_len + 1)
			sub.remove(nums[i])
			i += 1
		else:
			j += 1
	if min_len == 100010:
		print(-1)
	else:
		print(min_len)


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

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

def si():
	return input()

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

def li():
	return list(mi())
abc = 'abcdefghijklmnopqrstuvwxyz'
abd = {'a': 0, 'b': 1, 'c': 2, 'd': 3, 'e': 4, 'f': 5, 'g': 6, 'h': 7, 'i': 8, 'j': 9, 'k': 10, 'l': 11, 'm': 12, 'n': 13, 'o': 14, 'p': 15, 'q': 16, 'r': 17, 's': 18, 't': 19, 'u': 20, 'v': 21, 'w': 22, 'x': 23, 'y': 24, 'z': 25}
mod = 1000000007
inf = float('inf')
vow = ['a', 'e', 'i', 'o', 'u']
(dx, dy) = ([-1, 1, 0, 0], [0, 0, 1, -1])

def solve():
	for _ in range(ii()):
		n = ii()
		a = li()
		ans = 1000000000.0
		m = {}
		for i in range(n):
			if a[i] in m:
				ans = min(ans, i - m[a[i]] + 1)
			m[a[i]] = i
		if ans == 1000000000.0:
			ans = -1
		print(ans)
solve()


t = int(input())
for _ in range(t):
	n = int(input())
	arr = [int(x) for x in input().split()]
	occ = n + 1
	d = {}
	tmp = 0
	for e in range(0, n):
		tmp += 1
		num = arr[e]
		if num in d:
			dist = e - d[num] + 1
			if dist < occ:
				occ = dist
			d[num] = e
			tmp = 1
		else:
			d[num] = e
	print(occ if occ <= n else -1)


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

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

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

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

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

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

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

def Hash():
	return dict()

def Mod():
	return 1000000007

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

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

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

def Lcm(x, y):
	return x * y // gcd(x, y)

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

def btod(n):
	return int(n, 2)

def common(l1, l2):
	return set(l1).intersection(l2)

def solution():
	for _ in range(Int()):
		n = Int()
		a = List()
		ans = [-1] * (n + 1)
		mini = sys.maxsize
		for i in range(n):
			if ans[a[i]] != -1:
				mini = min(mini, i - ans[a[i]] + 1)
			ans[a[i]] = i
		if mini == sys.maxsize:
			print(-1)
		else:
			print(mini)
solution()


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


for _ in range(int(input())):
	n = int(input())
	lis = list(map(int, input().split()))
	last = [-1] * (n + 2)
	ans = 1000000000
	if n <= 1:
		print(-1)
	else:
		t = 1
		for i in range(n):
			if last[lis[i]] != -1:
				ans = min(ans, i - last[lis[i]] + 1)
				t -= 1
			last[lis[i]] = i
		if t == 1:
			print(-1)
		else:
			print(ans)


T = int(input())
for _ in range(T):
	n = int(input())
	if n < 2:
		input()
		print(-1)
		continue
	array = input().split()
	dic = {}
	min_array = {}
	for (i, element) in enumerate(array):
		if element in dic:
			if element in min_array:
				min_array[element] = i - dic[element] if i - dic[element] < min_array[element] else min_array[element]
			else:
				min_array[element] = i - dic[element]
		dic[element] = i
	x = [min_array[i] for i in min_array.keys()]
	if x:
		print(min(x) + 1)
	else:
		print(-1)


def solve(v):
	if len(v) < 2:
		return -1
	last = dict()
	best = len(v) + 1
	for i in range(len(v)):
		if v[i] in last:
			best = min(best, i - last[v[i]] + 1)
		last[v[i]] = i
	return best if best <= len(v) else -1
t = int(input())
for i in range(t):
	input()
	v = list(map(int, input().split()))
	print(solve(v))


n = input()
n = int(n)
for _ in range(n):
	input()
	a = map(int, input().split())
	d = {}
	ans = float('inf')
	for (i, x) in enumerate(a):
		if x not in d:
			d[x] = i
		else:
			ans = min(ans, 1 + i - d[x])
			d[x] = i
	if ans == float('inf'):
		ans = -1
	print(ans)


import sys
input = sys.stdin.readline
t = int(input())
for testcases in range(t):
	n = int(input())
	A = list(map(int, input().split()))
	LIST = [[] for i in range(n + 1)]
	for (ind, a) in enumerate(A):
		LIST[a].append(ind)
	ANS = n + 10
	for i in range(n + 1):
		if len(LIST[i]) < 2:
			continue
		for j in range(1, len(LIST[i])):
			ANS = min(ANS, LIST[i][j] - LIST[i][j - 1])
	if ANS == n + 10:
		print(-1)
	else:
		print(ANS + 1)


T = int(input())
for _ in range(T):
	n = int(input())
	array = list(map(int, input().split()))
	array1 = []
	for (pos, num) in enumerate(array):
		array1.append([num, pos])
	array1.sort()
	low = n + 1
	for i in range(len(array1) - 1):
		if array1[i][0] == array1[i + 1][0]:
			if abs(array1[i][1] - array1[i + 1][1]) < low:
				low = abs(array1[i][1] - array1[i + 1][1])
	if low == n + 1:
		print(-1)
	else:
		print(low + 1)


import sys
results = []
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split(' ')))
	a = [0] + a
	pos = [0 for _ in a]
	res = sys.maxsize
	for i in range(1, len(a)):
		if pos[a[i]] == 0:
			pos[a[i]] = i
			continue
		res = min(res, i - pos[a[i]] + 1)
		pos[a[i]] = i
	if res == sys.maxsize:
		results.append(-1)
	else:
		results.append(res)
for i in results:
	print(i)


from collections import defaultdict

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

def inp():
	return list(inp1())

def num():
	return int(input())
for _ in range(num()):
	n = num()
	a = inp()
	d = defaultdict(list)
	for i in range(n):
		try:
			d[a[i]].append(i)
		except KeyError:
			d[a[i]] = [i]
	ans = 99999999999999
	if len(d) == 1:
		if len(d.pop(a[i])) == 1:
			print(-1)
		else:
			print(2)
	else:
		for i in d.values():
			for j in range(1, len(i)):
				temp = i[j] - i[j - 1] + 1
				if temp < ans:
					ans = temp
		if ans == 99999999999999:
			print(-1)
		else:
			print(ans)


def shortest_dominated(a: list) -> int:
	numbers_indices = {}
	minimal_length = -1
	for (idx, el) in enumerate(a):
		if el not in numbers_indices:
			numbers_indices[el] = idx
		else:
			prev_idx = numbers_indices[el]
			numbers_indices[el] = idx
			dominated_length = idx - prev_idx + 1
			if minimal_length == -1:
				minimal_length = dominated_length
			elif dominated_length < minimal_length:
				minimal_length = dominated_length
	return minimal_length

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


import os
import sys

def main():
	T = read_int()
	for _ in range(T):
		N = read_int()
		A = read_ints()
		print(solve(N, A))

def solve(N, A):
	if N <= 1:
		return -1
	INF = N + 1
	best = INF
	pos = {}
	for (i, a) in enumerate(A):
		if a in pos:
			best = min(best, i - pos[a] + 1)
		pos[a] = i
	if best == INF:
		return -1
	return best
DEBUG = 'DEBUG' in os.environ

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

def read_int():
	return int(inp())

def read_ints():
	return [int(e) for e in inp().split()]

def dprint(*value, sep=' ', end='\n'):
	if DEBUG:
		print(*value, sep=sep, end=end)
main()


T = int(input())
for i in range(T):
	n = int(input())
	A = [int(j) for j in input().split()]
	maxx = max(A)
	B = [-1] * (maxx + 1)
	minn = 1000000
	tinn = 1000000
	for i in range(len(A)):
		if B[A[i]] > -1:
			if minn > i - B[A[i]]:
				minn = i - B[A[i]]
		B[A[i]] = i
	minn = minn + 1
	if minn > tinn:
		print(-1)
	else:
		print(minn)


for _ in range(int(input())):
	last = {}
	n = int(input())
	a = list(map(int, input().split()))
	nexts = [-1] * n
	for i in range(n - 1, -1, -1):
		if a[i] in last:
			nexts[i] = last[a[i]]
		last[a[i]] = i
	b = 10 ** 6
	for i in range(n):
		if nexts[i] != -1:
			b = min(b, nexts[i] - i + 1)
	print(-1 if b == 10 ** 6 else b)


t = int(input())
while t > 0:
	t -= 1
	n = int(input())
	a = list(map(int, input().split()))
	if len(a) <= 1:
		print(-1)
		continue
	d = {}
	for i in range(n):
		if a[i] in d:
			d[a[i]][0] += 1
			d[a[i]].append(i)
		else:
			d[a[i]] = [1, i]
	res = []
	for i in d:
		if d[i][0] > 1:
			for j in range(2, len(d[i])):
				tmp = d[i][j] - d[i][j - 1]
				res.append(tmp + 1)
	try:
		print(min(res))
	except:
		print(-1)


t = int(input())
for zz in range(t):
	n = int(input())
	nums = [int(i) for i in input().split()]
	lst = [-1] * (max(nums) + 1)
	mD = n + 1
	for i in range(len(nums)):
		dst = i - lst[nums[i]] + 1
		if dst < mD and lst[nums[i]] != -1:
			mD = dst
		lst[nums[i]] = i
	if mD == n + 1 or n < 2:
		print(-1)
	else:
		print(mD)


for _ in range(int(input())):
	n = int(input())
	ai = list(map(int, input().split()))
	index = [[] for q in range(n + 2)]
	value = []
	for i in range(n):
		index[ai[i]].append(i)
		if len(index[ai[i]]) == 2:
			value.append(index[ai[i]][1] - index[ai[i]][0])
			index[ai[i]].pop(0)
	try:
		e = min(value)
		if e == 10000000:
			print(-1)
		else:
			print(e + 1)
	except ValueError:
		print(-1)


for i in range(int(input())):
	n = int(input())
	if n == 1:
		b = int(input())
		print('-1')
	else:
		lis = list(map(int, input().split()))
		if len(set(lis)) == 1:
			print('2')
		elif len(set(lis)) == n:
			print('-1')
		else:
			mn = n + 1
			ad = [-1] * (n + 1)
			for k in range(0, n):
				if ad[lis[k]] != -1:
					mn = min(mn, k - ad[lis[k]] + 1)
				ad[lis[k]] = k
			print(mn)


import math
testCases = int(input())
for t in range(testCases):
	arrayLen = int(input())
	array = list(map(int, input().split(' ')))
	lastElement = {}
	smallerDominant = math.inf
	for index in range(len(array)):
		if not array[index] in lastElement:
			lastElement[array[index]] = index
		else:
			newDist = index - lastElement[array[index]] + 1
			if newDist < smallerDominant:
				smallerDominant = newDist
			lastElement[array[index]] = index
	if smallerDominant == math.inf:
		smallerDominant = -1
	print(smallerDominant)


T = int(input())
for i in range(T):
	n = int(input())
	A = [int(x) for x in input().strip().split()]
	keys = set()
	d = {}
	m = float('Inf')
	for i in range(n):
		if A[i] in keys:
			dif = i - d[A[i]] + 1
			if m > dif:
				m = dif
			d[A[i]] = i
		else:
			d[A[i]] = i
			keys.add(A[i])
	if m == float('Inf'):
		print(-1)
	else:
		print(m)


t = int(input())
for i in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	d = {}
	res = n + 1
	a = False
	for i in range(n):
		d[l[i]] = []
	for i in range(n):
		d[l[i]].append(i)
		if len(d[l[i]]) >= 2:
			res = min(res, d[l[i]][-1] - d[l[i]][-2] + 1)
			a = True
	if a:
		print(res)
	else:
		print(-1)


maxn = 200000
n = 0
(c, d) = ([0 for x in range(maxn + 5)], [0 for x in range(maxn + 5)])
T = int(input())

def judge():
	for i in range(0, n + 1):
		if d[i] > 1:
			return False
	return True
while T > 0:
	T -= 1
	n = int(input())
	s = input().split()
	for i in range(0, n + 1):
		d[i] = 0
	for i in range(0, n):
		c[i] = int(s[i])
		d[c[i]] += 1
	if judge():
		print(-1)
		continue
	for i in range(0, n + 1):
		d[i] = -1
	ans = n
	for i in range(0, n):
		if d[c[i]] == -1:
			d[c[i]] = i
		else:
			ans = min(ans, i - d[c[i]] + 1)
			d[c[i]] = i
	print(ans)


t = int(input())
i = 0
while i < t:
	n = int(input())
	arr = list(map(int, input().split()))
	diff = float('inf')
	d = [-2] * n
	for x in range(n):
		if d[arr[x] - 1] == -2:
			d[arr[x] - 1] = x
		else:
			a = x - d[arr[x] - 1]
			if a < diff:
				diff = a + 1
			d[arr[x] - 1] = x
	if diff == float('inf'):
		print(-1)
	else:
		print(diff)
	i = i + 1


import operator
t = int(input())
for i in range(1, t + 1):
	n = int(input())
	a = [int(x) for x in input().split()]
	length = []
	index = []
	k = 0
	for i in a:
		index.append((k, a[k]))
		k += 1
	sorted_elm = [(elm, idx) for (idx, elm) in sorted(index, key=lambda item: item[1], reverse=False)]
	minm = n + 1
	for i in range(0, n - 1):
		if sorted_elm[i + 1][0] == sorted_elm[i][0]:
			min_now = sorted_elm[i + 1][1] - sorted_elm[i][1] + 1
			if min_now < minm:
				minm = min_now
	if minm == n + 1:
		print(-1)
	else:
		print(minm)


def run_testcase():
	n = int(input())
	ns = [int(x) for x in input().split()]
	last_index = [None] * (n + 1)
	smallest_pair_distance = float('inf')
	for (i, v) in enumerate(ns):
		if last_index[v] == None:
			last_index[v] = i
			continue
		last_distance = i - last_index[v]
		if last_distance < smallest_pair_distance:
			smallest_pair_distance = last_distance
		last_index[v] = i
	return smallest_pair_distance + 1 if smallest_pair_distance != float('inf') else -1
testcase_count = int(input())
for i in range(testcase_count):
	print(str(run_testcase()))


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	dic = {}
	for i in range(n):
		try:
			if dic[a[i]]:
				dic[a[i]].append(i)
		except KeyError:
			dic[a[i]] = [i]
	res = 200005
	for i in dic:
		temp = dic[i]
		for j in range(len(temp) - 1):
			res = min(res, temp[j + 1] - temp[j])
	if res > n:
		print(-1)
	else:
		print(res + 1)


t = int(input())
while t:
	t -= 1
	n = int(input())
	a = list(map(int, input().split()))
	d = dict()
	for i in range(n):
		if a[i] in d:
			d[a[i]].append(i)
		else:
			d[a[i]] = [i]
	mini = n + n
	for i in d:
		b = d[i]
		for x in range(1, len(b)):
			mini = min(mini, b[x] - b[x - 1] + 1)
	if mini == n + n:
		print(-1)
	else:
		print(mini)


t = int(input())
for _ in range(t):
	n = int(input())
	A = list(map(int, input().split()))
	D = [list() for _ in range(n + 1)]
	for i in range(n):
		D[A[i]].append(i)
	res = 2 ** 100
	for i in range(n + 1):
		for j in range(len(D[i]) - 1):
			res = min(res, D[i][j + 1] - D[i][j] + 1)
	if res == 2 ** 100:
		print(-1)
	else:
		print(res)


t = int(input())
for i in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	dic = {}
	for i in arr:
		dic[i] = 0
	res = []
	for i in range(len(arr)):
		j = arr[i]
		if dic[j] != 0:
			res.append(i + 1 - dic[j] + 1)
			dic[j] = i + 1
		else:
			dic[j] = i + 1
	if len(res) != 0:
		print(min(res))
	else:
		print(-1)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	min = 1000000
	i = 0
	d = {}
	while i < len(a):
		x = a[i]
		if x in d:
			e = d[x]
			r = e[-1]
			di = i - r + 1
			if di < min:
				min = di
			e.append(i)
			d[x] = e
		else:
			d[x] = [i]
		i = i + 1
	if min == 1000000:
		print(-1)
	else:
		print(min)


t = int(input())
for i in range(0, t):
	n = int(input())
	L = list(map(int, input().split()))
	if n == 1:
		print(-1)
	else:
		counter = [0] * (n + 1)
		M = [[] for j in range(n + 1)]
		for i in range(0, n):
			counter[L[i]] += 1
			M[L[i]].append(i)
		if max(counter) == 1:
			print(-1)
		elif max(counter) == n:
			print(2)
		else:
			mini = 1000000.0 + 10
			for i in range(0, n + 1):
				if len(M[i]) > 1:
					for j in range(0, len(M[i]) - 1):
						mini = min(mini, M[i][j + 1] - M[i][j])
			print(mini + 1)


from math import inf
t = int(input())
for i in range(t):
	l = int(input())
	nums = list(map(int, input().split()))
	if l < 2:
		print(-1)
		continue
	else:
		dis = inf
		lst = [None] * (l + 10)
		for j in range(l):
			if lst[nums[j]] is None:
				lst[nums[j]] = j
			elif dis >= j - lst[nums[j]]:
				dis = j - lst[nums[j]]
			lst[nums[j]] = j
		if dis == inf:
			print(-1)
		else:
			print(dis + 1)


t = int(input())
for you in range(t):
	n = int(input())
	l = input().split()
	li = [int(i) for i in l]
	hashi = dict()
	maxa = []
	for i in range(n):
		if li[i] in hashi:
			maxa.append(i - hashi[li[i]] + 1)
		hashi[li[i]] = i
	if maxa == []:
		print(-1)
	else:
		print(min(maxa))


amount = int(input())
for i in range(amount):
	n = int(input())
	array = [int(s) for s in input().split()]
	counter = [[] for i in range(n + 1)]
	ans = 200001
	if n < 2:
		print(-1)
		continue
	for i in range(len(array)):
		counter[array[i]].append(i)
	for i in range(1, len(counter)):
		if len(counter[i]) < 2:
			continue
		else:
			for j in range(1, len(counter[i])):
				ans = min(ans, counter[i][j] - counter[i][j - 1] + 1)
	if ans == 200001:
		print(-1)
	else:
		print(ans)


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

def solve(n, a):
	last: list = [-1] * (n + 1)
	m = n + 1
	for i in range(n):
		if last[a[i]] != -1:
			d = i - last[a[i]] + 1
			m = min(m, d)
		last[a[i]] = i
	return -1 if m == n + 1 else m
for t in range(int(input())):
	result = solve(*read())
	print(result)


q = int(input())
for i in range(q):
	n = int(input())
	a = list(map(int, input().split(' ')))
	if n < 2:
		print(-1)
		continue
	if a[0] == a[1]:
		print(2)
		continue
	cnt = [0] * (n + 1)
	i = 0
	j = 1
	cnt[a[0]] = 1
	cnt[a[1]] = 1
	ans = n + 1
	while 1:
		if j - i + 1 >= ans:
			cnt[a[i]] = cnt[a[i]] - 1
			i = i + 1
		else:
			j = j + 1
			if j >= n:
				break
			cnt[a[j]] = cnt[a[j]] + 1
			if cnt[a[j]] > 1:
				while a[j] != a[i]:
					cnt[a[i]] = cnt[a[i]] - 1
					i = i + 1
				ans = j - i + 1
	if ans > n:
		print(-1)
	else:
		print(ans)


import sys
input = sys.stdin.readline
t = int(input())
from collections import Counter
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	if n == 1:
		print(-1)
	else:
		ans = float('inf')
		r = 1
		s = set()
		s.add(a[0])
		for l in range(n):
			while r < n and a[r] not in s:
				s.add(a[r])
				r += 1
			if r == n:
				r -= 1
			if r != l and a[r] == a[l]:
				ans = min(ans, r - l + 1)
			s.remove(a[l])
		print(ans if ans != float('inf') else -1)


def dominated(n, m):
	t = set(m)
	if len(t) == len(m):
		return -1
	t = list(t)
	points = {}
	for i in t:
		points[i] = []
	for i in range(len(m)):
		points[m[i]].append(i)
	result = []
	for j in points.items():
		if len(j[1]) == 1:
			continue
		for i in range(len(j[1]) - 1):
			result.append(j[1][i + 1] - j[1][i] + 1)
	return min(result)
for i in range(int(input().strip())):
	n = int(input().strip())
	m = input().strip().split()
	m = [int(m[i]) for i in range(len(m))]
	print(dominated(n, m))


for _ in range(int(input())):
	n = int(input())
	a = [int(__) for __ in input().split()]
	l1 = [-1 for ___ in range(n)]
	ans = n + 1
	for i in range(n):
		if l1[a[i] - 1] != -1:
			if i - l1[a[i] - 1] + 1 < ans:
				ans = i - l1[a[i] - 1] + 1
		l1[a[i] - 1] = i
		if ans == 2:
			break
	if ans == n + 1:
		print(-1)
	else:
		print(ans)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = dict()
	ans = float('inf')
	for (i, x) in enumerate(a):
		if x not in b:
			b[x] = i
			continue
		ans = min(ans, i - b[x] + 1)
		b[x] = i
	if ans > n:
		ans = -1
	print(ans)


q = int(input())
for qq in range(q):
	n = int(input())
	a = list(map(int, input().split()))
	if n == 1:
		print('-1')
		continue
	first = {}
	last = {}
	for i in range(n):
		elem = a[i]
		if elem not in first:
			first[elem] = i
		elif elem not in last:
			last[elem] = i
		elif last[elem] - first[elem] >= i - last[elem]:
			first[elem] = last[elem]
			last[elem] = i
	dist = n + 1
	for x in first:
		if x not in last:
			continue
		if last[x] - first[x] + 1 < dist:
			dist = last[x] - first[x] + 1
	if dist <= 1 or dist > n:
		print(-1)
	else:
		print(dist)


from collections import defaultdict as dc
import sys
input = sys.stdin.readline
for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	x = dc(list)
	f = 0
	m = sys.maxsize
	for i in range(n):
		if x[l[i]]:
			p = i - x[l[i]][-1] + 1
			if p < m:
				m = p
				f = 1
		x[l[i]].append(i)
	if f:
		print(m)
	else:
		print(-1)


for _ in range(int(input())):
	n = int(input())
	s = list(map(int, input().split()))
	a = list()
	b = list()
	for i in range(n + 1):
		b.append(-1)
	c = list()
	for i in range(n + 1):
		c.append(-1)
	a.append(b)
	a.append(c)
	for i in range(n):
		f = s[i]
		if a[0][f] == -1:
			a[0][f] = i
		else:
			g = i - a[0][f] + 1
			if a[1][f] == -1:
				a[1][f] = g
			else:
				a[1][f] = min(a[1][f], g)
		a[0][f] = i
	ans = 1000000
	for i in range(1, n + 1):
		if a[1][i] != -1:
			ans = min(ans, a[1][i])
	if ans == 1000000:
		print(-1)
	else:
		print(ans)


from collections import defaultdict as dd
for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	d = dd(list)
	for i in range(len(arr)):
		d[arr[i]] += [i]
	ans = 99999999999999999
	for k in d:
		for i in range(1, len(d[k])):
			ans = min(d[k][i] - d[k][i - 1] + 1, ans)
	if ans == 99999999999999999:
		print(-1)
	else:
		print(ans)


I = input
for _ in [0] * int(I()):
	r = n = int(I())
	a = [-n] * 8 ** 6
	i = 0
	for x in map(int, I().split()):
		r = min(r, i - a[x])
		a[x] = i
		i += 1
	print((-1, r + 1)[r < n])


import sys
for _ in range(int(input())):
	n = int(input())
	arr = list(sys.stdin.readline().split())
	ans = 10 ** 9
	if n == 1:
		print(-1)
	else:
		d = dict()
		for i in range(n):
			if arr[i] in d:
				d[arr[i]].append(i)
				l = d[arr[i]]
				ans = min(ans, l[-1] - l[-2] + 1)
			else:
				d[arr[i]] = [i]
		if ans == 10 ** 9:
			print(-1)
		else:
			print(ans)


import sys
data = [[int(x) for x in y.split()] for y in sys.stdin.readlines()[2::2]]
for a in data:
	d = {}
	min_len = -1
	for (i, x) in enumerate(a):
		if x in d:
			if min_len == -1:
				min_len = i - d[x] + 1
			else:
				min_len = min(min_len, i - d[x] + 1)
		d[x] = i
	print(min_len)


from collections import Counter, defaultdict
t = int(input())
while t:
	t -= 1
	n = int(input())
	(*a,) = map(int, input().split())
	if Counter(a).most_common(1)[0][1] == 1:
		print(-1)
		continue
	pos = {}
	ans = int(1000000000.0)
	for i in range(n):
		if pos.get(a[i]) is None:
			pos[a[i]] = i
		else:
			ans = min(ans, i - pos[a[i]] + 1)
			pos[a[i]] = i
	print(ans)


for _ in range(int(input())):
	lasts = {}
	ans = n = int(input())
	for (i, a) in enumerate(input().split()):
		if a in lasts:
			ans = min(ans, i - lasts[a])
		lasts[a] = i
	ans += 1
	if ans > n:
		ans = -1
	print(ans)


for _ in range(int(input())):
	mydict = dict()
	n = int(input())
	a = list(map(int, input().split()))
	min_ = 10000000000
	for i in range(n):
		prev = mydict.get(a[i])
		if prev is not None:
			min_ = min(min_, i - prev + 1)
		mydict[a[i]] = i
	print(-1 if min_ == 10000000000 else min_)


t = int(input())
while t > 0:
	n = int(input())
	a = list(map(int, input().split(' ')))
	l = 0
	occ = {}
	minlen = n + 1
	for r in range(n):
		if a[r] in occ.keys():
			occ[a[r]] += 1
			while occ[a[r]] > 1:
				minlen = min(r - l + 1, minlen)
				occ[a[l]] -= 1
				l += 1
		else:
			occ[a[r]] = 1
	if minlen < n + 1:
		print(minlen)
	else:
		print(-1)
	t -= 1


t = int(input())
while t:
	n = int(input())
	a = list(map(int, input().split()))
	l = [-200001] * (n + 1)
	mn = 1000000
	for i in range(n):
		mn = min(mn, i - l[a[i]] + 1)
		l[a[i]] = i
	if mn == 200002:
		print(-1)
	else:
		print(mn)
	t = t - 1


for i in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	(d, c, g) = ({}, -1, [])
	for j in range(n):
		if l[j] in d:
			g.append(j - d[l[j]])
			d[l[j]] = j
		else:
			d[l[j]] = j
	if len(g) == 0:
		print(-1)
	else:
		print(min(g) + 1)


x = int(input())
for i in range(x):
	a = int(input())
	l = list(map(int, input().split()))
	mass = []
	for i2 in range(a):
		mass += [[l[i2], i2]]
	mass.sort()
	res = 10000000
	for i3 in range(1, a):
		if mass[i3][0] == mass[i3 - 1][0]:
			res = min(res, mass[i3][1] - mass[i3 - 1][1])
	if res == 10000000:
		print(-1)
	else:
		print(res + 1)


T = int(input())
for _ in range(T):
	n = int(input())
	infinity = n + 1
	A = [int(v) for v in input().split()]
	last = {}
	ans = infinity
	for i in range(len(A)):
		v = A[i]
		if v in last:
			ans = min(ans, 1 + i - last[v])
		last[v] = i
	if ans == infinity:
		ans = -1
	print(ans)


import sys
import collections
import heapq
import math
input = sys.stdin.readline

def rints():
	return map(int, input().strip().split())

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

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

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

def gcd(a, b):
	if b == 0:
		return a
	return gcd(b, a % b)
t = rint()
for _ in range(t):
	n = rint()
	a = rintas()
	memo = {}
	ans = n + 1
	for (i, v) in enumerate(a):
		if v in memo:
			ans = min(ans, i - memo[v] + 1)
		memo[v] = i
	if ans == n + 1 or ans == 1:
		print(-1)
	else:
		print(ans)


t = int(input())
for _ in range(t):
	n = int(input())
	arr = [int(j) for j in input().split()]
	last = {}
	ans = None
	for pos in range(n):
		val = arr[pos]
		if val in last:
			cur_ans = pos - last[val] + 1
			last[val] = pos
			if ans is None:
				ans = cur_ans
			ans = min(ans, cur_ans)
		else:
			last[val] = pos
	if ans is None:
		print(-1)
	else:
		print(ans)


import math
import bisect
from collections import defaultdict as df
from collections import deque
from sys import stdin, stdout
t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().rstrip().split()))
	d = df(list)
	ans = float('inf')
	for i in range(n):
		d[a[i]].append(i)
		if len(d[a[i]]) >= 2:
			ans = min(ans, d[a[i]][-1] - d[a[i]][-2] + 1)
	if n == 1:
		print(-1)
		continue
	if ans == float('inf'):
		print(-1)
	else:
		print(ans)


from sys import stdin
from collections import Counter, deque
T = int(input())
for _ in range(T):
	n = int(input())
	ls = list(map(int, input().split()))
	(mp, mn) = ([-1] * (n + 1), n + 1)
	for (i, u) in enumerate(ls):
		if mp[u] != -1:
			mn = min(mn, i - mp[u] + 1)
		mp[u] = i
	print(-1 if mn == n + 1 else mn)


t = int(input())
for i in range(t):
	d = {}
	n = int(input())
	min = 200001
	l = []
	l = list(map(int, input().split()))
	if n == 1:
		print('-1')
		continue
	else:
		for j in range(n):
			if l[j] in d.keys():
				size = len(d[l[j]])
				if j - d[l[j]][size - 1] + 1 < min:
					min = j - d[l[j]][size - 1] + 1
				d[l[j]].append(j)
			else:
				d[l[j]] = [j]
		if min == 200001:
			print('-1')
		else:
			print(min)


n = int(input())
ss = []
for i in range(n):
	input()
	ss += [list(map(int, input().split(' ')))]
for i in range(n):
	s = ss[i]
	n = len(s)
	mem = [-1 for i in range(n + 1)]
	q = n + 10
	for j in range(n):
		if mem[s[j]] != -1:
			r = j - mem[s[j]] + 1
			if r < q:
				q = r
		mem[s[j]] = j
	if q == n + 10:
		print('-1')
	else:
		print(q)


import math
t = int(input())
while t:
	n = int(input())
	l = list(map(int, input().split()))
	c = n
	c1 = 0
	d = {}
	for i in range(n):
		try:
			if d[l[i]][0] >= 1:
				d[l[i]][0] += 1
				c = min(c, abs(d[l[i]][-1] - i) + 1)
				d[l[i]].append(i)
				c1 = 1
		except KeyError:
			d[l[i]] = [1, i]
	if c1 == 1:
		print(c)
	else:
		print(-1)
	t -= 1


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	d = {}
	z = []
	count = 0
	if len(set(l)) == n:
		print(-1)
	else:
		for i in range(n):
			if l[i] in d:
				z.append(i - d[l[i]][-1] + 1)
				d[l[i]].append(i)
				count += 1
			else:
				d[l[i]] = [i]
		if len(z) == 0:
			print(-1)
		else:
			print(min(z))


inp = lambda : map(int, input().rstrip().split())
for _ in range(int(input())):
	n = int(input())
	a = list(inp())
	li = [-1] * (n + 1)
	minm = n + 1
	for i in range(n):
		x = a[i]
		if li[x] == -1:
			li[x] = i
		else:
			minm = min(minm, i - li[x] + 1)
			li[x] = i
	if minm == n + 1:
		print(-1)
	else:
		print(minm)


from collections import Counter, defaultdict
import statistics
t = int(input())
for _ in range(t):
	n = int(input())
	ls = list(map(int, input().strip().split()))
	if n == 1:
		print('-1')
	else:
		f = 1
		result = 10 ** 6
		dic = defaultdict(list)
		for i in range(n):
			dic[ls[i]].append(i)
		for (key, val) in dic.items():
			if len(val) > 1:
				for (i, v) in enumerate(val[1:], 1):
					x = v - val[i - 1] + 1
					if x < result:
						f = 0
						result = x
		if f == 1:
			print('-1')
		else:
			print(result)


def solve(n, arr):
	ans = n + 5
	lst = dict()
	for (i, x) in enumerate(arr):
		if lst.get(x) is not None:
			ans = min(ans, i - lst[x] + 1)
		lst[x] = i
	if ans > n:
		print(-1)
	else:
		print(ans)
t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().rstrip().split()))
	solve(n, arr)


t = int(input())
for ti in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	u = {}
	for x in a:
		u[x] = -1
	ans = 10 ** 9
	for i in range(n):
		x = a[i]
		if u[x] != -1:
			ans = min(ans, i - u[x] + 1)
		u[x] = i
	if ans == 10 ** 9:
		print('-1')
	else:
		print(ans)


T = int(input())
for i in range(T):
	n = int(input())
	arr = list(map(int, input().split()))
	remove_duplicated = set(arr)
	if len(arr) == len(remove_duplicated):
		print(-1)
		continue
	answer = 10 ** 5 * 3
	counter = [[] for i in range(n + 1)]
	for i in range(len(arr)):
		counter[arr[i]].append(i)
	for i in range(n + 1):
		if len(counter[i]) <= 1:
			continue
		for j in range(len(counter[i]) - 1):
			answer = min(answer, counter[i][j + 1] - counter[i][j])
	print(answer + 1)


from sys import stdin

def input():
	return stdin.readline()[:-1]

def intput():
	return int(input())

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

def intsput():
	return map(int, sinput())
t = intput()
for _ in range(t):
	n = intput()
	a = list(intsput())
	d = {}
	found = False
	op = []
	for i in range(n):
		x = a[i]
		if x not in d:
			d[x] = [i]
		else:
			d[x].append(i)
			op.append(d[x][-1] - d[x][-2] + 1)
	if op:
		print(min(op))
	else:
		print(-1)


def solve(arr, n):
	d = {}
	mv = 99999999999999
	for i in range(n):
		try:
			val = d[arr[i]]
			x = val[-1]
			val[-1] = i
			c = i - x + 1 if x == 0 else i - x
			mv = min(mv, i - x + 1)
		except:
			d[arr[i]] = [1, i]
	if mv == 99999999999999:
		return -1
	return mv
t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	if n < 2:
		print(-1)
	else:
		print(solve(arr, n))


from heapq import heappush, heappop
from collections import deque, defaultdict, Counter
import itertools
from itertools import permutations, combinations
import sys
import bisect
import string

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

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

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

def LI_():
	return [int(i) - 1 for i in input().split()]

def show(*inp, end='\n'):
	if show_flg:
		print(*inp, end=end)
YN = ['Yes', 'No']
mo = 10 ** 9 + 7
inf = float('inf')
input = sys.stdin.readline
show_flg = False
show_flg = True
t = I()
for _ in range(t):
	n = I()
	a = LI()
	p = [-1] * (1 + n)
	ans = inf
	for i in range(n):
		if p[a[i]] != -1:
			ans = min(ans, i - p[a[i]] + 1)
		p[a[i]] = i
	if ans == inf:
		ans = -1
	print(ans)


from math import inf
for t in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	lasts = [-inf] * (n + 1)
	res = inf
	for i in range(n):
		value = a[i]
		res = min(res, i - lasts[value] + 1)
		lasts[value] = i
	print(-1 if res == inf else res)


t = int(input())
for i in range(t):
	l = int(input())
	s = input().split()
	d = {}
	for j in range(0, l):
		diff = 200001
		if s[j] not in d:
			d[s[j]] = [j, diff]
		else:
			diff = min(j - d[s[j]][0], d[s[j]][1])
			d[s[j]] = [j, diff]
	my_list = []
	my_iter = iter(d.values())
	for i in my_iter:
		my_list.append(i[1])
	my_list.sort()
	z = my_list[0]
	if z == 200001:
		print(-1)
	else:
		print(z + 1)


import math, string, itertools, fractions, heapq, collections, re, array, bisect, sys, copy, functools
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 YES(c):
	return IF(c, 'YES', 'NO')

def Yes(c):
	return IF(c, 'Yes', 'No')

def main():
	t = I()
	rr = []
	for _ in range(t):
		n = I()
		a = LI()
		r = inf
		d = {}
		for (i, c) in enumerate(a):
			if c in d:
				t = i - d[c] + 1
				if r > t:
					r = t
			d[c] = i
		rr.append(IF(r == inf, -1, r))
	return JA(rr, '\n')
print(main())


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


for i in range(int(input())):
	n = int(input())
	arr = [[int(i), 0] for i in input().split()]
	for i in range(n):
		arr[i][1] = i
	arr.sort()
	ans = n + 5
	for i in range(n - 1):
		if arr[i][0] == arr[i + 1][0]:
			ans = min(ans, 1 + abs(arr[i][1] - arr[i + 1][1]))
	if ans > n:
		print(-1)
	else:
		print(ans)


from collections import defaultdict as dd
from collections import deque
import bisect
import heapq

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

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

def solve():
	n = ri()
	A = rl()
	if n == 1:
		print(-1)
		return
	shortest = n + 1
	last = {}
	for (i, a) in enumerate(A):
		if a in last:
			shortest = min(shortest, i - last[a] + 1)
		last[a] = i
	if shortest == n + 1:
		shortest = -1
	print(shortest)
mode = 'T'
if mode == 'T':
	t = ri()
	for i in range(t):
		solve()
else:
	solve()


for _ in range(int(input())):
	x = int(input())
	a = [*map(int, input().split())]
	mini = x
	start = [0] * (x + 1)
	end = [0] * (x + 1)
	count = [0] * (x + 1)
	st = 0
	ele = 0
	en = 0
	for i in range(x):
		el = a[i]
		count[el] += 1
		if count[el] % 2 == 1:
			end[el] = i
		else:
			start[el] = i
		if count[el] >= 2:
			diff = abs(start[el] - end[el])
			if mini > diff:
				ele = el
				st = start[el]
				en = end[el]
				mini = diff
	if count[ele] > 1:
		print(mini + 1)
	else:
		print(-1)


t = int(input())
for i in range(t):
	n = int(input())
	lst = list(map(int, input().split()))
	Dict = {}
	ans = float('inf')
	for i in range(n):
		ch = lst[i]
		if Dict.get(ch) == None:
			Dict[ch] = [i]
		else:
			ans = min(ans, i - Dict[ch][-1])
			Dict[ch].append(i)
	if ans == float('inf'):
		print('-1')
	else:
		print(ans + 1)


t = int(input())
for i in range(t):
	l = int(input())
	s = input().split()
	d = {}
	for j in range(0, l):
		diff = 200001
		if s[j] not in d:
			d[s[j]] = [j, diff]
		else:
			diff = min(j - d[s[j]][0], d[s[j]][1])
			d[s[j]] = [j, diff]
	a = dict(sorted(d.items(), key=lambda x: x[1][1]))
	z = next(iter(a.values()))[1]
	if z == 200001:
		print(-1)
	else:
		print(z + 1)


def solve(arr):
	if len(arr) < 2:
		return -1
	if len(arr) == 2:
		if arr[0] == arr[1]:
			return 2
		else:
			return -1
	(d, ans) = ({}, len(arr) + 1)
	for (i, x) in enumerate(arr):
		if x in d:
			if ans > i - d[x] + 1:
				ans = i - d[x] + 1
		d[x] = i
	if ans < len(arr) + 1:
		return ans
	else:
		return -1
t = int(input())
for _ in range(t):
	n = int(input().strip())
	if n == 1:
		arr = int(input().strip())
		print('-1')
		continue
	arr = list(map(int, input().strip().split()))
	assert len(arr) == n
	print(solve(arr))


t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	d = {}
	ans = 1000000000
	for i in range(n):
		if l[i] not in d:
			d[l[i]] = i
		else:
			if ans > i - d[l[i]] + 1:
				ans = i - d[l[i]] + 1
			d[l[i]] = i
	if ans == 1000000000:
		print(-1)
	else:
		print(ans)


for _ in range(int(input())):
	w = {}
	a = int(input())
	d = input().split()
	for i in range(a):
		if d[i] in w:
			w[d[i]].append(i)
		else:
			w[d[i]] = [i]
	a += 1
	for i in w:
		z = w[i]
		for p in range(len(z) - 1):
			a = min(a, z[p + 1] - z[p])
	if len(d) < a:
		print(-1)
	else:
		print(a + 1)


t = int(input())
while t > 0:
	n = int(input())
	l = list(map(int, input().split()))
	d = dict()
	for i in range(len(l)):
		if l[i] not in d:
			d[l[i]] = [i]
		else:
			d[l[i]].append(i)
	mini = n + 1
	flag = 0
	for i in l:
		for j in range(1, len(d[i])):
			if len(d[i]) == 0:
				mini = n + 1
				break
			mini = min(mini, d[i][j] - d[i][j - 1])
			if mini == 1:
				flag = 1
				break
		if flag == 1:
			break
	if mini <= n:
		print(mini + 1)
	else:
		print(-1)
	t -= 1


t = int(input())
for test in range(t):
	n = int(input())
	arr = [int(x) for x in input().split()]
	if n == 1:
		print(-1)
		continue
	d = [-1] * (2 * 100001)
	minn = n + 1
	for i in range(n):
		if d[arr[i]] == -1:
			d[arr[i]] = i
		elif i - d[arr[i]] + 1 < minn:
			minn = i - d[arr[i]] + 1
		d[arr[i]] = i
	if minn == n + 1:
		print(-1)
	else:
		print(minn)


from collections import *
for _ in range(int(input())):
	n = int(input())
	l = [int(i) for i in input().split(' ')]
	if n == 1:
		print(-1)
		continue
	ma = max(Counter(l).values())
	if ma == 1:
		print(-1)
		continue
	visited = {}
	ans = n
	for i in range(n):
		if l[i] in visited:
			ans = min(ans, i - visited[l[i]])
		visited[l[i]] = i
	print(ans + 1)


q = int(input())
for rew in range(q):
	n = int(input())
	l = list(map(int, input().split()))
	if len(l) == 1:
		print(-1)
	else:
		poz = {}
		for i in range(n + 1):
			poz[i] = []
		for i in range(n):
			poz[l[i]].append(i)
		spr = [10000000] * (n + 1)
		for i in range(n + 1):
			k = len(poz[i])
			for j in range(1, k):
				spr[i] = min(spr[i], poz[i][j] - poz[i][j - 1])
		if min(spr) == 10000000:
			print(-1)
		else:
			print(min(spr) + 1)


vis = [-1 for i in range(200005)]
last = [-1 for i in range(200005)]
for tc in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	ret = n + 1
	for i in range(n):
		x = a[i]
		if vis[x] == tc:
			ret = min(ret, 1 + i - last[x])
		else:
			vis[x] = tc
		last[x] = i
	if ret > n:
		ret = -1
	print(ret)


t = int(input())
for T in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	anum = dict()
	if n < 2:
		print(-1)
		continue
	maxi = 1
	for i in range(n):
		if a[i] in anum:
			anum[a[i]].append(i)
			if len(anum[a[i]]) > maxi:
				maxi = len(anum[a[i]])
		else:
			anum[a[i]] = list()
			anum[a[i]].append(i)
	if maxi == 1:
		print(-1)
		continue
	miks = n + 1
	l = 0
	r = n + 1
	for z in anum:
		for m in range(len(anum[z]) - 1):
			if anum[z][m + 1] - anum[z][m] < miks:
				miks = anum[z][m + 1] - anum[z][m]
				l = anum[z][m]
				r = anum[z][m + 1]
	print(r - l + 1)


from sys import stdin
input = stdin.readline
for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	d = {}
	mn = 10000000000.0
	for i in range(n):
		if l[i] in d.keys():
			mn = min(mn, i - d[l[i]])
		d[l[i]] = i
	print(-1 if mn == 10000000000.0 else mn + 1)


from collections import defaultdict
for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	d = defaultdict(int)
	m = 10 ** 10
	for (i, j) in enumerate(l):
		if d[j] == 0:
			d[j] = i + 1
		else:
			m = min(m, i + 1 - d[j] + 1)
			d[j] = i + 1
	if m == 10 ** 10:
		print(-1)
	else:
		print(m)


from collections import defaultdict
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	mydick = dict()
	mydick = defaultdict(lambda : list(), mydick)
	ans = 100000000.0
	ok = False
	for i in range(n):
		mydick[a[i]].append(i)
		if len(mydick[a[i]]) > 1:
			p = len(mydick[a[i]])
			ans = min(mydick[a[i]][p - 1] - mydick[a[i]][p - 2], ans)
			ok = True
	if ok:
		print(ans + 1)
	else:
		print(-1)


t = int(input().strip())
for case in range(t):
	occ = {}
	n = int(input().strip())
	ls = list(map(int, input().strip().split()))
	ans = 999999
	for i in range(n):
		if not ls[i] in occ.keys():
			occ[ls[i]] = i
		else:
			if ans > i - occ[ls[i]]:
				ans = i - occ[ls[i]]
			occ[ls[i]] = i
	if ans == 999999:
		print('-1')
	else:
		print(ans + 1)


from math import inf
t = int(input())
for _ in range(t):
	n = int(input())
	var = inf
	arr = list(map(int, input().split()))
	ls = [-inf] * (n + 1)
	for i in range(n):
		var = min(i - ls[arr[i]] + 1, var)
		ls[arr[i]] = i
	print(-1 if var == inf else var)


import math
t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	s = set(arr)
	d = dict()
	min_here = n
	if len(arr) == 1:
		print('-1')
	elif len(s) == len(arr):
		print('-1')
	else:
		for i in range(n):
			if arr[i] not in d:
				d[arr[i]] = i
		for i in range(n):
			if d[arr[i]] != i:
				min_here = min(min_here, i - d[arr[i]])
				d[arr[i]] = i
		print(min_here + 1)


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	o = []
	d = -1
	for i in range(n):
		o.append(-1)
	for i in range(n):
		x = a[i] - 1
		if o[x] != -1:
			if d > i - o[x] or d == -1:
				d = i - o[x] + 1
		o[x] = i
	print(d)


inf = 10 ** 18
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	cnt = {}
	ans = inf
	for i in range(n):
		if a[i] in cnt:
			ans = min(ans, i - cnt[a[i]] + 1)
		cnt[a[i]] = i
	print(ans if ans != inf else -1)


for step in range(int(input())):
	n = int(input())
	A = [int(i) for i in input().split()]
	dict = {}
	ans = float('inf')
	for (i, e) in enumerate(A):
		if e not in dict:
			dict[e] = i
		else:
			ans = min(i - dict[e] + 1, ans)
			dict[e] = i
	if ans == float('inf'):
		print(-1)
	else:
		print(ans)


t = int(input())
for _ in range(t):
	n = int(input())
	seen = [-1] * (n + 1)
	ans = n * n
	for (i, v) in enumerate(map(int, input().split())):
		if seen[v] != -1:
			ans = min(ans, i - seen[v] + 1)
		seen[v] = i
	print(ans if ans != n * n else -1)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	mark = [-1] * 200001
	dist = 200001
	for i in range(n):
		if mark[a[i]] == -1:
			mark[a[i]] = i
		else:
			temp = i - mark[a[i]]
			if temp >= 1:
				dist = min(temp, dist)
			mark[a[i]] = i
	if dist == 200001:
		print('-1')
		continue
	print(dist + 1)


t = int(input())
for gg in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	if len(set(a)) == n or n == 1:
		print(-1)
	else:
		wp = 10000000000000
		kk = [-1] * (n + 1)
		mn = 10000000000000
		for gg in range(n):
			j = a[gg]
			if kk[j] == -1:
				kk[j] = gg
			else:
				wp = gg - kk[j]
				mn = min(mn, wp)
				kk[j] = gg
		print(mn + 1)


t = int(input())
for i in range(t):
	n = int(input())
	dic = {}
	a = []
	m = -1
	if n == 1:
		tmp = int(input())
		print('-1')
	else:
		a = list(map(int, input().split()))
		for i in range(len(a)):
			if a[i] not in dic:
				dic[a[i]] = i
			else:
				k = i - dic[a[i]] + 1
				dic[a[i]] = i
				if m == -1 or k < m:
					m = k
		print(m)


from collections import defaultdict
from math import inf as INFINITY

def solve(n, a):
	nearest = INFINITY
	last_seen = defaultdict(lambda : -INFINITY)
	for (i, x) in enumerate(a):
		d = i - last_seen[x] + 1
		if d < nearest:
			nearest = d
		last_seen[x] = i
	return -1 if nearest == INFINITY else nearest
for _ in range(int(input())):
	n = int(input())
	(*a,) = map(int, input().split())
	print(solve(n, a))


test = int(input())
while test > 0:
	test -= 1
	n = int(input())
	lst = sorted(zip(map(int, input().split()), range(1, n + 1)))
	cnt = [0] * (n + 1)
	pos = 1
	for (a, b) in lst:
		cnt[pos] = b
		pos += 1
	f = [0] * (n + 1)
	pos = 1
	pre = 1
	val = 0
	for (a, b) in lst:
		if pos > 1 and a != val:
			pre = pos
		if pos - pre + 1 < 2:
			f[b] = -1
		else:
			f[b] = cnt[pre]
			pre += 1
		pos += 1
		val = a
	mx = -1
	rs = n + 1
	for i in range(1, n + 1):
		if f[i] > mx:
			rs = min(rs, i - f[i] + 1)
		mx = max(mx, f[i])
	if rs <= n:
		print(rs)
	else:
		print(-1)


from collections import defaultdict
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	if n == 1 or n == len(set(a)):
		print(-1)
		continue
	so_far = defaultdict(lambda : [0, 0])
	counter = defaultdict(lambda : 0)
	distance = defaultdict(lambda : 0)
	for i in range(n):
		if counter[a[i]] >= 2:
			if distance[a[i]] >= i + 1 - so_far[a[i]][1]:
				distance[a[i]] = i + 1 - so_far[a[i]][1]
				so_far[a[i]][1] = i + 1
			continue
		so_far[a[i]][counter[a[i]]] = i + 1
		counter[a[i]] += 1
		if counter[a[i]] == 2:
			distance[a[i]] = so_far[a[i]][1] - so_far[a[i]][0]
	print(min(distance.values()) + 1)


t = int(input())
for i in range(t):
	n = int(input())
	a = [int(j) for j in input().split(' ')]
	if n == 1:
		print(-1)
	else:
		small = 1000000
		m = [-1 for j in range(n)]
		for j in range(n):
			if m[a[j] - 1] == -1:
				m[a[j] - 1] = j
			else:
				if small > j - m[a[j] - 1] + 1:
					small = j - m[a[j] - 1] + 1
				m[a[j] - 1] = j
		if small == 1000000:
			print(-1)
		else:
			print(small)


test_count = int(input())
for _ in range(test_count):
	n = int(input())
	a = list(map(int, input().split()))
	positions = list()
	for _ in range(n):
		positions.append(list())
	for i in range(len(a)):
		positions[a[i] - 1].append(i)
	ans = -1
	for row in positions:
		for i in range(1, len(row)):
			if ans == -1:
				ans = row[i] - row[i - 1] + 1
			else:
				ans = min(row[i] - row[i - 1] + 1, ans)
	print(ans)


from math import *
from collections import *
import sys
sys.setrecursionlimit(10 ** 9)
t = int(input())
for y in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	d = dict()
	m = 1000000000.0
	for i in range(n):
		if l[i] not in d:
			d[l[i]] = [i]
		else:
			d[l[i]].append(i)
			m = min(m, i - d[l[i]][-2] + 1)
	if m == 1000000000.0:
		print(-1)
	else:
		print(m)


import math
testCases = int(input())
for t in range(testCases):
	n = int(input())
	arr = [int(x) for x in input().split()]
	if n == 1:
		print('-1')
		continue
	hmap = {}
	min_len = math.inf
	for (i, nums) in enumerate(arr):
		if nums in hmap:
			min_len = min(min_len, i - hmap[nums] + 1)
		hmap[nums] = i
	if min_len == math.inf:
		min_len = -1
	print(min_len)


import sys
import collections
from collections import Counter
import itertools
import math
import timeit

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

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

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

def flin(d, x, default=-1):
	left = right = -1
	for i in range(len(d)):
		if d[i] == x:
			if left == -1:
				left = i
			right = i
	if left == -1:
		return (default, default)
	else:
		return (left, right)

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

def prr(a, sep=' '):
	print(sep.join(map(str, a)))

def dd():
	return collections.defaultdict(int)
t = ii()
for _ in range(t):
	n = ii()
	d = li()
	if n < 2:
		print(-1)
		continue
	res = 10 ** 6
	pos = [-1 for i in range(1, n + 2)]
	for i in range(n):
		if pos[d[i]] != -1:
			res = min(res, i - pos[d[i]])
		pos[d[i]] = i
	print(res + 1) if res != 10 ** 6 else print(-1)


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	mem = {}
	ans = 10 ** 7
	for i in range(n):
		try:
			ans = min(i - mem[arr[i]] + 1, ans)
			mem[arr[i]] = i
		except:
			mem[arr[i]] = i
	if ans == 10 ** 7:
		print(-1)
	else:
		print(ans)


import collections

def sol(lst):
	d = collections.defaultdict(list)
	for (i, x) in enumerate(lst):
		d[x].append(i)
	MIN_SOL = 10 ** 10
	for x in d:
		for (a, b) in zip(d[x], d[x][1:]):
			MIN_SOL = min(MIN_SOL, b - a + 1)
	if MIN_SOL == 10 ** 10:
		print(-1)
	else:
		print(MIN_SOL)
t = int(input())
for _ in range(t):
	_ = int(input())
	sol([int(x) for x in input().split()])


import math

def revolt(l, n):
	d = {}
	min_ = math.inf
	for i in range(n):
		if d.get(l[i], -1) >= 0:
			min_ = min(i - d[l[i]] + 1, min_)
			d[l[i]] = i
		else:
			d[l[i]] = i
	if min_ > n:
		return -1
	return min_
for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	print(revolt(l, n))


t = int(input())
for w in range(t):
	n = int(input())
	a = [int(i) for i in input().split()]
	if n < 2:
		print(-1)
		continue
	else:
		pos = [-1 for i in range(n + 1)]
		ans = int(1000000.0)
		for i in range(n):
			if pos[a[i]] == -1:
				pos[a[i]] = i
			else:
				ans = min(ans, i - pos[a[i]] + 1)
				pos[a[i]] = i
		if ans == int(1000000.0):
			print(-1)
		else:
			print(ans)


import collections
for t in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	counter = dict()
	if len(set(arr)) == n:
		print(-1)
		continue
	ans = 10 ** 10
	for i in range(n):
		ele = arr[i]
		if ele in counter:
			diff = i - counter[ele] + 1
			ans = min(diff, ans)
			counter[ele] = i
		else:
			counter[ele] = i
	print(ans)


from collections import defaultdict
for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	idxs = defaultdict(list)
	ans = 10 ** 9
	for (i, ele) in enumerate(arr):
		idxs[ele].append(i)
		if len(idxs[ele]) > 1:
			ans = min(ans, idxs[ele][-1] - idxs[ele][-2] + 1)
	print(ans if ans != 10 ** 9 else -1)


import math, sys, bisect, heapq, os
from collections import defaultdict, Counter, deque
from itertools import groupby, accumulate
from functools import lru_cache
pr = lambda x: x

def input():
	return sys.stdin.readline().rstrip('\r\n')
aj = lambda : list(map(int, input().split()))

def list3d(a, b, c, d):
	return [[[d] * c for j in range(b)] for i in range(a)]

def Y(c):
	print(['NO', 'YES'][c])

def y(c):
	print(['no', 'yes'][c])

def Yy(c):
	print(['No', 'Yes'][c])

def solve():
	for _ in range(int(input())):
		(n,) = aj()
		A = aj()
		C = {}
		ans = 1000000000.0
		for (ii, i) in enumerate(A):
			v = C.get(i, -1)
			if v == -1:
				C[i] = ii
			else:
				ans = min(ans, ii - v + 1)
				C[i] = ii
		if ans == 1000000000.0:
			print(-1)
		else:
			print(ans)
try:
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
	from aj import *
except:
	pass
solve()


def array(arr, struc):
	return list(map(struc, arr.split()))
n = int(input())
while n:
	lena = int(input()) + 1
	arr = array(input(), int)
	domArr = [lena] * lena
	mn = 2000001
	for i in range(lena - 1):
		if domArr[arr[i]] == lena:
			domArr[arr[i]] += i + 1
		elif domArr[arr[i]] > lena:
			domArr[arr[i]] -= lena
			tmp = i + 1 - domArr[arr[i]]
			domArr[arr[i]] = i + 1
			mn = min(mn, tmp)
		else:
			tmp = i + 1 - domArr[arr[i]]
			domArr[arr[i]] = i + 1
			mn = min(mn, tmp)
	if min(domArr) < lena:
		print(mn + 1)
	else:
		print(-1)
	n -= 1


from collections import Counter
from collections import defaultdict
import math
t = int(input())
for i in range(0, t):
	n = int(input())
	a = list(map(int, input().split()))
	d = defaultdict(lambda : [])
	g = set()
	for i in range(0, n):
		if d.get(a[i]) == None:
			d[a[i]].append(i)
		else:
			d[a[i]].append(i)
	k = list(d.keys())
	mn = 999999999
	for i in k:
		l = len(d[i])
		if l >= 2:
			for j in range(1, l):
				if d[i][j] - d[i][j - 1] < mn:
					mn = d[i][j] - d[i][j - 1]
	if mn == 999999999:
		print(-1)
	else:
		print(mn + 1)


from collections import defaultdict
for x in range(int(input())):
	n = int(input())
	m = list(map(int, input().split()))
	d = defaultdict(list)
	for i in range(n):
		d[m[i]].append(i)
	a = []
	for i in d:
		d[i].sort()
		for j in range(1, len(d[i])):
			a.append(d[i][j] - d[i][j - 1] + 1)
	if len(a) == 0:
		print(-1)
	else:
		print(min(a))


t = int(input())
for i in range(t):
	n = int(input())
	a = [int(i) for i in input().split()]
	c = {}
	m = -1
	if n == -1:
		print(-1)
		continue
	for i in range(len(a)):
		if a[i] not in c:
			c[a[i]] = i
		else:
			k = i - c[a[i]] + 1
			c[a[i]] = i
			if m == -1 or k < m:
				m = k
	print(m)


import os, sys, bisect, copy
from collections import defaultdict, Counter, deque
from functools import lru_cache
if os.path.exists('in.txt'):
	sys.stdin = open('in.txt', 'r')
if os.path.exists('out.txt'):
	sys.stdout = open('out.txt', 'w')

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

def mapi(arg=0):
	return map(int if arg == 0 else str, input().split())
for _ in range(int(input())):
	n = int(input())
	a = list(mapi())
	if n <= 1:
		print(-1)
	else:
		mp = defaultdict(list)
		res = float('inf')
		for i in range(n):
			if a[i] in mp:
				res = min(res, i - mp[a[i]][-1] + 1)
			mp[a[i]].append(i)
		if res == float('inf'):
			print(-1)
		else:
			print(res)


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

class FastIO(IOBase):
	newlines = 0

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

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

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

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

class IOWrapper(IOBase):

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

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

def gcd(a, b):
	return math.gcd(a, b)

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

def factors(n):
	step = 2 if n % 2 else 1
	return set(reduce(list.__add__, ([i, n // i] for i in range(1, int(math.sqrt(n)) + 1, step) if n % i == 0)))

def comb(n, k):
	factn = math.factorial(n)
	factk = math.factorial(k)
	fact = math.factorial(n - k)
	ans = factn // (factk * fact)
	return ans

def is_prime(n):
	if n <= 1:
		return False
	if n == 2:
		return True
	if n > 2 and n % 2 == 0:
		return False
	max_div = math.floor(math.sqrt(n))
	for i in range(3, 1 + max_div, 2):
		if n % i == 0:
			return False
	return True

def maxpower(n, x):
	B_max = int(math.log(n, x)) + 1
	return B_max

def binaryToDecimal(n):
	return int(n, 2)
t = int(input())
for _ in range(t):
	n = int(input())
	l = listt()
	d = {}
	if n < 2:
		print(-1)
	else:
		for i in range(n):
			if l[i] in d:
				d[l[i]].append(i + 1)
			else:
				d[l[i]] = [i + 1]
		val = []
		for i in d:
			if len(d[i]) > 1:
				val.append(d[i])
		if len(val) == 0:
			print(-1)
		else:
			m = n
			for i in val:
				for j in range(1, len(i)):
					if i[j] - i[j - 1] < m:
						m = i[j] - i[j - 1]
			print(m + 1)


from collections import defaultdict
t = int(input())
for _ in range(t):
	k = 0
	for ii in range(1111):
		k += 5
	inf = 99999999999999999999999999999999999999999999999999999999999999999999999999999999999
	n = int(input())
	l = list(map(int, input().split()))
	fflag = 0
	h = defaultdict(int)
	posn = defaultdict(int)
	mini = defaultdict(lambda : inf)
	for i in range(n):
		if i != 0:
			if l[i] == l[i - 1]:
				print(2)
				fflag = 1
				break
		h[l[i]] += 1
		if h[l[i]] > 1:
			mini[l[i]] = min(i - posn[l[i]] + 1, mini[l[i]])
		posn[l[i]] = i
	if not fflag:
		fflag = 0
		ans = inf
		for i in mini:
			if mini[i] != inf:
				fflag = 1
				ans = min(mini[i], ans)
		if fflag == 0:
			print(-1)
		else:
			print(ans)


t = int(input())

def check(arr, n):
	hehe = [0] * n
	for i in range(n):
		j = arr[i] - 1
		if hehe[j] == 1:
			return False
		hehe[j] += 1
	return True
for i in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	initial = [-1] * n
	diff = [n] * n
	if check(arr, n):
		print(-1)
	else:
		for j in range(n):
			l = arr[j] - 1
			k = initial[l]
			initial[l] = j
			if k == -1:
				continue
			differ = j - k + 1
			if differ < diff[l]:
				diff[l] = differ
		print(min(diff))


import math
t = int(input())
while t:
	n = int(input())
	s = input().split(' ')
	a = [0]
	v = [[]]
	for i in range(0, n):
		a.append(int(s[i]))
		v.append([])
	for i in range(1, n + 1):
		v[a[i]].append(i)
	ok = 0
	ans = 200005
	for i in range(1, n + 1):
		if len(v[i]) > 1:
			ok = 1
		for j in range(0, len(v[i]) - 1):
			ans = min(ans, v[i][j + 1] - v[i][j])
	if ok == 0:
		print(-1)
	else:
		print(ans + 1)
	t -= 1


T = int(input())
for t in range(T):
	N = int(input())
	line = input().split()
	list = []
	for i in range(N):
		list.append(int(line[i]))
	res = N + 1
	ants = [-1] * (N + 1)
	for i in range(N):
		if ants[list[i]] != -1:
			res = min(res, i - ants[list[i]] + 1)
		ants[list[i]] = i
	print(res if res <= N else -1)


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


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


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	d = dict()
	if n == 1:
		print(-1)
	else:
		for (i, j) in enumerate(a):
			if d.get(j):
				d[j].append(i + 1)
			else:
				d[j] = [i + 1]
		minimum = n
		flag = 0
		for i in d.keys():
			x = d[i]
			n1 = len(x)
			if n1 > 1:
				flag = 1
			j = 0
			while j < n1 - 1:
				size = x[j + 1] - x[j] + 1
				if size < minimum:
					minimum = size
				j += 1
		if flag == 0:
			print(-1)
		else:
			print(minimum)


import math
for _ in range(int(input())):
	n = int(input())
	a = {}
	x = [int(n) for n in input().split()]
	m = 1000000000
	for i in range(len(x)):
		if x[i] not in a:
			a[x[i]] = []
		a[x[i]].append(i)
	if len(a) == n:
		print(-1)
		continue
	for i in a:
		l = len(a[i])
		if l > 1:
			for j in range(1, len(a[i])):
				m = min(abs(a[i][j] - a[i][j - 1]) + 1, m)
	print(m)


t = int(input())
while t:
	n = int(input())
	l = 10 ** 6
	a = list(map(int, input().split()))
	dict = {}
	for (i, k) in enumerate(a):
		if k in dict:
			l = min(i - dict[k] + 1, l)
			dict[k] = i
		else:
			dict[k] = i
	if l == 10 ** 6:
		print(-1)
	else:
		print(l)
	t -= 1


import sys
n = int(input())
for i in range(n):
	a_l = int(input())
	l = list(map(int, input().split()))
	dicti = {}
	mini = {}
	for j in range(a_l):
		if l[j] not in dicti:
			dicti[l[j]] = j
			mini[l[j]] = -1
		else:
			temp = j - dicti[l[j]]
			if mini[l[j]] == -1:
				mini[l[j]] = temp
			else:
				mini[l[j]] = min(temp, mini[l[j]])
			dicti[l[j]] = j
	m = sys.maxsize
	for a in mini:
		if mini[a] > 0 and mini[a] < m:
			m = mini[a]
	if m == sys.maxsize:
		print(-1)
	else:
		print(m + 1)


t = int(input())
for _ in range(t):
	dic = {}
	n = int(input())
	temp = list(map(int, input().split(' ')))
	for x in temp:
		dic[x] = []
	i = 0
	while i < n:
		dic[temp[i]].append(i)
		i += 1
	res = 30000000000
	for x in dic:
		i = 0
		while i < len(dic[x]) - 1:
			if dic[x][i + 1] - dic[x][i] < res:
				res = dic[x][i + 1] - dic[x][i]
			i += 1
	if res == 30000000000:
		print(-1)
	else:
		print(res + 1)


import sys, math

def input():
	return sys.stdin.readline()[:-1]

def main():
	q = int(input())
	for _ in range(q):
		n = int(input())
		a = list(map(int, input().split()))
		p = [[] for k in range(n + 1)]
		for k in range(n):
			p[a[k]].append(k)
		ans = 1000000000
		for e in p:
			if len(e) >= 2:
				for l in range(len(e) - 1):
					ans = min(ans, 1 + abs(e[l] - e[l + 1]))
		if ans == 1000000000:
			print(-1)
		else:
			print(ans)
main()


from collections import defaultdict
t = int(input())
while t != 0:
	n = int(input())
	arr = list((int(x) for x in input().split()))[:n]
	myMap = defaultdict(int)
	ans = n + 1
	for i in range(n):
		if myMap.get(arr[i]) is None:
			myMap[arr[i]] = i
		else:
			ans = min(ans, i - myMap.get(arr[i]) + 1)
			myMap[arr[i]] = i
	if ans > n:
		print(-1)
	else:
		print(ans)
	t -= 1


def f(n, array):
	if n == 1:
		return -1
	ocorr = {}
	for l in range(1, n + 1):
		ocorr[l] = -1
	res = -1
	for i in range(n):
		if ocorr[array[i]] == -1:
			ocorr[array[i]] = i
		else:
			if res == -1:
				res = i + 1 - ocorr[array[i]]
			else:
				res = min(res, i + 1 - ocorr[array[i]])
			ocorr[array[i]] = i
	return res
t = int(input())
for __ in range(t):
	n = int(input())
	array = [int(k) for k in input().split()]
	print(f(n, array))


import sys
from functools import lru_cache, cmp_to_key
from heapq import merge, heapify, heappop, heappush
from math import *
from collections import defaultdict as dd, deque, Counter as C
from itertools import combinations as comb, permutations as perm
from bisect import bisect_left as bl, bisect_right as br, bisect
from time import perf_counter
from fractions import Fraction
mod = int(pow(10, 9) + 7)
mod2 = 998244353

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

def out(*var, end='\n'):
	sys.stdout.write(' '.join(map(str, var)) + end)

def l():
	return list(sp())

def sl():
	return list(ssp())

def sp():
	return map(int, data().split())

def ssp():
	return map(str, data().split())

def l1d(n, val=0):
	return [val for i in range(n)]

def l2d(n, m, val=0):
	return [l1d(n, val) for j in range(m)]
for i in range(l()[0]):
	n = l()[0]
	A = l()
	if n < 2 or len(A) == len(set(A)):
		print(-1)
		continue
	d = {}
	for i in range(n):
		if A[i] not in d:
			d[A[i]] = []
		d[A[i]].append(i)
	m = n
	for ks in d:
		if len(d[ks]) == 1:
			continue
		for i in range(1, len(d[ks])):
			curr = d[ks][i] - d[ks][i - 1]
			m = min(m, curr)
	print(m + 1)


for _ in range(int(input())):
	n = int(input())
	lista = list(map(int, input().split()))
	if len(lista) < 2:
		print(-1)
	else:
		m = 10000000
		dictio = {}
		for i in range(len(lista)):
			if lista[i] in dictio:
				dictio[lista[i]].append(i)
			else:
				dictio[lista[i]] = [i]
		for x in dictio:
			try:
				sum = 1000000000
				try:
					for j in range(len(dictio[x])):
						if dictio[x][j + 1] - dictio[x][j] < sum:
							sum = dictio[x][j + 1] - dictio[x][j]
				except:
					pass
				if sum < m:
					m = sum
			except:
				pass
		if m == 10000000:
			print(-1)
		else:
			print(m + 1)


c = 200001
t = int(input())
while t:
	_ = int(input())
	a = [-1] * c
	ans = c
	for (i, e) in enumerate(map(int, input().split())):
		if a[e] == -1:
			a[e] = i
		else:
			ans = min(ans, i - a[e] + 1)
			a[e] = i
	print([ans, -1][ans == c])
	t -= 1


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	d = {}
	mn = 10000000000.0
	for i in range(n):
		if l[i] in d.keys():
			mn = min(mn, i - d[l[i]])
		d[l[i]] = i
	print(-1 if mn == 10000000000.0 else mn + 1)


t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	d = {}
	ans = float('+inf')
	for (i, el) in enumerate(l):
		if el not in d:
			d[el] = i
		else:
			ans = min(ans, i - d[el] + 1)
			d[el] = i
	if ans == float('+inf'):
		print('-1')
	else:
		print(ans)


T = int(input())
for t in range(T):
	n = int(input())
	lst = list(map(int, input().split()))
	d_min = n
	value2index = dict()
	for (counter, value) in enumerate(lst):
		if value in value2index:
			index = value2index[value]
			d = counter - index
			if d < d_min:
				d_min = d
		value2index[value] = counter
	if d_min == n:
		print(-1)
	else:
		print(d_min + 1)


for _ in range(int(input())):
	n = int(input())
	co = [[-1, 10 ** 6] for i in range(n + 1)]
	l = list(map(int, input().split()))
	s = set(l)
	if len(s) == n:
		print(-1)
	else:
		for i in range(n):
			if co[l[i]][0] == -1:
				co[l[i]][0] = i
				co[l[i]][1] = 10 ** 6
			else:
				co[l[i]][1] = min(co[l[i]][1], i - co[l[i]][0])
				co[l[i]][0] = i
		ans = 10 ** 6
		for i in range(n + 1):
			ans = min(ans, co[i][1])
		print(ans + 1)


from sys import maxsize
for _ in range(int(input())):
	n = int(input())
	arr = [int(i) for i in input().split()]
	elements = len(set(arr))
	if elements == n:
		print(-1)
		continue
	elif elements == 1:
		print(2)
		continue
	pos_map = {}
	result = maxsize
	for (index, elem) in enumerate(arr):
		if elem in pos_map:
			result = min(result, index - pos_map[elem] + 1)
		pos_map[elem] = index
	print(result)


for __ in range(int(input())):
	n = int(input())
	x = list(map(int, input().split()))
	d = {}
	ll = []
	for i in range(len(x)):
		try:
			ind = d[x[i]]
			ll.append(i - ind)
			d[x[i]] = i
		except:
			d[x[i]] = i
	if len(ll) == 0:
		print('-1')
	else:
		print(min(ll) + 1)


T = int(input())
for t in range(T):
	n = int(input())
	A = [int(s) for s in input().split()]
	ans = n + 1
	memo = dict()
	for (i, num) in enumerate(A):
		if num not in memo:
			memo[num] = i
		else:
			d = i - memo[num] + 1
			if d < ans:
				ans = d
			memo[num] = i
	if ans == n + 1:
		print(-1)
	else:
		print(ans)


for _ in range(int(input())):
	n = int(input())
	s = list(map(int, input().split()))
	min_len = n + 1
	ind_list = []
	for i in range(n):
		ele = []
		ele.append(s[i])
		ele.append(i)
		ind_list.append(ele)
	ind_list.sort()
	for i in range(n - 1):
		if ind_list[i][0] == ind_list[i + 1][0]:
			diff = ind_list[i + 1][1] - ind_list[i][1]
			diff += 1
			if min_len > diff:
				min_len = diff
	if min_len == n + 1:
		print('-1')
	else:
		print(min_len)


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


T = int(input())
for t in range(T):
	n = int(input())
	a = [int(x) - 1 for x in input().split()]
	if n == 1:
		print(-1)
	else:
		w = [[] for x in range(n)]
		for i in range(n):
			w[a[i]].append(i)
		m = n
		for i in range(n):
			b = w[i]
			l = len(b)
			if l > 1:
				for j in range(l - 1, 0, -1):
					b[j] -= b[j - 1]
				M = min(b[1:l])
				if M < m:
					m = M
		if m == n:
			print(-1)
		else:
			print(m + 1)


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	s = {}
	l = []
	for i in range(n):
		if a[i] not in s:
			s[a[i]] = i
		else:
			l.append(i - s[a[i]] + 1)
			s[a[i]] = i
	if len(l) == 0:
		print(-1)
	else:
		print(min(l))


from collections import defaultdict

def ab():
	b = int(input())
	c = list(map(int, input().split(' ')))
	q = []
	bc = defaultdict(list)
	min = b + 1
	if b == 1:
		print('-1')
		return
	else:
		for i in range(0, b):
			bc[c[i]].append(i)
		for x in bc:
			for r in range(0, len(bc[x]) - 1):
				w = abs(bc[x][r] - bc[x][r + 1])
				if min > w:
					min = w
	if min > b:
		print('-1')
	else:
		print(min + 1)
a = int(input())
for i in range(0, a):
	ab()


t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(x) for x in input().split()]
	ans = -1
	s = {a[0]}
	i = 0
	j = 1
	while i < n and j < n:
		while a[j] in s:
			if ans == -1:
				ans = j - i + 1
			else:
				ans = min(ans, j - i + 1)
			s.remove(a[i])
			i += 1
		s.add(a[j])
		j += 1
	print(ans)


def solve(arr):
	dic = {}
	f = 0
	for i in range(len(arr)):
		try:
			dic[arr[i]].append(i)
			f = 1
		except:
			dic[arr[i]] = [i]
	mini = 999999999999
	for i in dic:
		curr = 999999999999
		if len(dic[i]) > 1:
			for j in range(1, len(dic[i])):
				curr = min(dic[i][j] - dic[i][j - 1], curr)
		mini = min(curr, mini)
	if f == 1:
		return mini + 1
	else:
		return -1
for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	print(solve(arr))


for _ in range(int(input())):
	n = int(input())
	a = [*map(int, input().split())]
	ans = float('inf')
	ind = {}
	for (i, j) in enumerate(a):
		if j not in ind:
			ind[j] = [i]
		else:
			ind[j].append(i)
	for i in ind.values():
		if len(i) >= 2:
			ans = min(ans, min([y - x + 1 for (y, x) in zip(i[1:], i[:-1])]))
	print(-1 if ans == float('inf') else ans)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	d = {}
	s = set()
	c = 10000000
	for i in range(n):
		if a[i] not in s:
			s.add(a[i])
			d[a[i]] = i
		else:
			if c > i - d[a[i]] + 1:
				c = i - d[a[i]] + 1
			d[a[i]] = i
	if c == 10000000:
		print(-1)
	else:
		print(c)


import sys, math
from heapq import heappush, heappop
readline = sys.stdin.readline
mr = lambda : map(int, readline().split())
t = int(readline())
for _ in range(t):
	n = int(readline())
	tmp = list(mr())
	tp = [[] for _ in range(n)]
	asnArr = []
	for i in range(n):
		tp[tmp[i] - 1].append(i)
	for i in range(n):
		if len(tp[i]) > 1:
			d = tp[i][1] - tp[i][0] + 1
			for j in range(len(tp[i]) - 1):
				if tp[i][j + 1] - tp[i][j] < d:
					d = tp[i][j + 1] - tp[i][j] + 1
			asnArr.append(d)
	if n == 1 or len(asnArr) == 0:
		print(-1)
	else:
		ans = n + 1
		for i in range(len(asnArr)):
			if asnArr[i] != 0 and ans > asnArr[i]:
				ans = asnArr[i]
		print(ans)


import sys
input = lambda : sys.stdin.readline().strip()
print = lambda s: sys.stdout.write(s)
t = int(input())
for i in range(t):
	n = int(input())
	ls = list(map(int, input().split()))
	d = {}
	for i in ls:
		d[i] = []
	for i in range(n):
		d[ls[i]].append(i)
	ans = []
	for (k, v) in d.items():
		v.sort()
		for i in range(len(v) - 1):
			ans.append(v[i + 1] - v[i])
	try:
		res = min(ans) + 1
	except:
		res = -1
	res = res if res >= 2 else -1
	print(str(res) + '\n')


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	el = {}
	for i in range(n):
		if a[i] in el:
			el[a[i]].append(i)
		else:
			el[a[i]] = [i]
	md = 99999999999999
	for i in el:
		for j in range(1, len(el[i])):
			md = min(md, 1 + el[i][j] - el[i][j - 1])
	if md != 99999999999999:
		print(md)
	else:
		print(-1)


def get_count_dominated(array):
	if len(array) < 2 or len(array) == len(set(array)):
		return -1
	shortest = 200000
	result = dict()
	for (index, elem) in enumerate(array):
		if elem not in result.keys():
			result[elem] = [index, shortest]
		else:
			diff = index - result[elem][0] + 1
			if diff < result[elem][1]:
				result[elem] = [index, diff]
			else:
				result[elem][0] = index
	result = [result[x] for x in result.keys()]
	return min(list(map(lambda x: x[1], result)))
N = int(input())
for _ in range(N):
	_ = input()
	print(get_count_dominated(list(map(int, input().split()))))


T = int(input())
for t in range(T):
	n = int(input())
	arr = list(map(int, input().split()))
	d = {i: [] for i in range(1, n + 1)}
	for i in range(n):
		d[arr[i]].append(i)
	rec = []
	for i in d:
		if len(d[i]) > 1:
			rec.append(d[i])
	ans = 10 ** 6
	for i in range(len(rec)):
		for j in range(len(rec[i]) - 1):
			diff = rec[i][j + 1] - rec[i][j]
			ans = min(ans, diff + 1)
	if ans == 10 ** 6:
		print(-1)
		continue
	print(ans)


t = int(input())
for _ in range(t):
	n = int(input())
	s = [int(x) for x in input().split()]
	pos = []
	for i in range(n + 2):
		h = []
		pos.append(h)
	for i in range(0, len(s)):
		pos[s[i]].append(i)
	temp = n + 100
	for i in range(0, len(pos)):
		for j in range(1, len(pos[i])):
			temp = min(temp, pos[i][j] - pos[i][j - 1] + 1)
	if temp == n + 100:
		print(-1)
	else:
		print(temp)


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()]
	dict = {}
	for i in range(n):
		if arr[i] in dict:
			dict[arr[i]].append(i)
		else:
			dict[arr[i]] = [i]
	ans = float('inf')
	for i in dict:
		for j in range(len(dict[i]) - 1):
			ans = min(ans, dict[i][j + 1] - dict[i][j] + 1)
	if ans == float('inf'):
		ans = -1
	print(ans)


for _ in range(int(input())):
	n = int(input())
	d = dict()
	a = list(map(int, input().split()))
	for i in range(n):
		if not d.get(a[i]):
			d[a[i]] = [float('inf'), i]
		elif i - d[a[i]][1] + 1 < d[a[i]][0]:
			d[a[i]] = [i - d[a[i]][1] + 1, i]
		else:
			d[a[i]] = [d[a[i]][0], i]
	ans = min(d.values())[0]
	if ans == float('inf'):
		print(-1)
	else:
		print(ans)


q = int(input())

def solve(nums):
	if len(nums) == 2:
		if nums[0] == nums[1]:
			return 2
		else:
			return -1
	dict_of_ind = dict()
	min_diff = 2 * 10 ** 5 + 1
	for x in range(len(nums)):
		if nums[x] not in dict_of_ind:
			dict_of_ind[nums[x]] = x
			continue
		if x - dict_of_ind[nums[x]] + 1 < min_diff:
			min_diff = x - dict_of_ind[nums[x]] + 1
		dict_of_ind[nums[x]] = x
	if min_diff == 2 * 10 ** 5 + 1:
		return -1
	return min_diff
for z in range(q):
	n = int(input())
	array = list(map(int, input().split()))
	print(solve(array))


def gns():
	return list(map(int, input().split()))
t = int(input())
for i in range(t):
	n = int(input())
	ns = gns()
	lst = {}
	ans = n + 1
	for i in range(n):
		x = ns[i]
		if x in lst:
			ans = min(ans, i - lst[x] + 1)
		lst[x] = i
	if ans == n + 1:
		print(-1)
	else:
		print(ans)


q = int(input())
for z in range(q):
	n = int(input())
	a = list(map(int, input().split()))
	d = {}
	_m = 1000000000000000
	if len(a) == 1:
		print(-1)
		continue
	for (j, i) in enumerate(a):
		if i not in d:
			d[i] = [j]
		else:
			d[i].append(j)
	o = False
	for (k, v) in d.items():
		if len(v) > 1:
			o = True
			j = len(v) - 2
			_min = 12123123132132
			while j >= 0:
				if abs(v[j + 1] - v[j]) + 1 < _min:
					_min = abs(v[j] - v[j + 1]) + 1
				j -= 1
			_m = min(_m, _min)
	if not o:
		print(-1)
	else:
		print(_m)


from sys import stdin, stdout
t = int(stdin.readline())
for a in range(t):
	n = int(stdin.readline())
	l = [None] + list(map(int, stdin.readline().split()))
	d = {}
	Min = 0
	temp = 0
	for a in range(1, len(l)):
		if l[a] not in d.keys():
			d[l[a]] = a
		else:
			temp = a - d[l[a]] + 1
			if Min == 0:
				Min = temp
			elif temp < Min:
				Min = temp
			d[l[a]] = a
	if Min == 0:
		print(-1)
	else:
		print(Min)


t = int(input())
for i in range(0, t):
	c = []
	n = int(input())
	for j in range(0, n + 10):
		c.append(-1)
	m = input()
	if n == 1:
		print(-1)
		continue
	x = []
	x = [int(xx) for xx in m.split()]
	a = 0
	min = n + 10
	for xx in x:
		if c[xx] == -1:
			c[xx] = a
		else:
			dif = a - c[xx] + 1
			c[xx] = a
			if dif < min:
				min = dif
		a = a + 1
	if min == n + 10:
		min = -1
	print(min)


def minSize(arr):
	ans = -1
	hashmap = dict()
	for i in range(len(arr)):
		if arr[i] in hashmap:
			lastIndex = hashmap[arr[i]]
			length = i - lastIndex + 1
			if ans == -1 or length < ans:
				ans = length
		hashmap[arr[i]] = i
	return ans
numTestCases = int(input(''))
while numTestCases > 0:
	n = int(input(''))
	arr = input('').strip().split()
	for i in range(len(arr)):
		arr[i] = int(arr[i])
	print(minSize(arr))
	numTestCases -= 1


def check(n, a):
	lst = [-1] * (n + 1)
	result = n + 5
	for i in range(n):
		if lst[a[i]] != -1:
			result = min(result, i - lst[a[i]] + 1)
		lst[a[i]] = i
	if result > n:
		result = -1
	return result
for _ in range(int(input())):
	m = int(input())
	b = [int(j) for j in input().split()]
	print(check(m, b))


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


import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	li = {}
	for i in range(n):
		if a[i] not in li:
			li[a[i]] = [i]
		else:
			li[a[i]].append(i)
	ans = 10 ** 9
	for num in li:
		tmp = li[num]
		for j in range(len(tmp) - 1):
			ans = min(ans, tmp[j + 1] - tmp[j] + 1)
	if ans == 10 ** 9:
		print(-1)
	else:
		print(ans)


for j in range(int(input())):
	p = int(input())

	def maxDistance(arr, n):
		mp = {}
		maxDict = 0
		k = []
		for i in range(n):
			if arr[i] not in mp.keys():
				mp[arr[i]] = i
			else:
				maxDict = max(maxDict, i - mp[arr[i]])
				k.append(i - mp[arr[i]] + 1)
				mp[arr[i]] = i
		if len(k) == 0:
			k.append(-1)
		return min(k)
	if __name__ == '__main__':
		arr = list(map(int, input().split()))
		n = p
		print(maxDistance(arr, n))


n = int(input())
for nn in range(n):
	length = int(input())
	arr = [int(x) for x in input().split(' ')]
	last = {}
	min_length = 2 * 10 ** 5 + 1
	for i in range(len(arr)):
		if arr[i] in last:
			diff = i - last[arr[i]]
			if diff < min_length:
				min_length = diff
		last[arr[i]] = i
	if min_length == 2 * 10 ** 5 + 1:
		print(str(-1))
	else:
		print(str(min_length + 1))


def solve():
	n = int(input())
	a = list(map(int, input().split()))
	h = {}
	ans = n * 2
	idx = 0
	for x in a:
		idx += 1
		if h.get(x) == None:
			h[x] = idx
		else:
			ans = min(idx - h[x] + 1, ans)
			h[x] = idx
	if ans == n * 2:
		print('-1')
	else:
		print(ans)
t = int(input())
while t > 0:
	t -= 1
	solve()


t = int(input())
for x in range(t):
	n = int(input())
	ar = list(map(int, input().split()))
	mx = n + 1000
	kol = [-1] * (n + 3)
	for i in range(n):
		if kol[ar[i]] != -1:
			mx = min(mx, abs(i - kol[ar[i]] + 1))
		kol[ar[i]] = i
	if mx != n + 1000:
		print(mx)
	else:
		print(-1)


from collections import defaultdict
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	pos = defaultdict(list)
	for (i, v) in enumerate(a):
		pos[v].append(i)
	ans = 10000000
	for p in pos.values():
		if len(p) <= 1:
			continue
		for i in range(len(p) - 1):
			ans = min(p[i + 1] - p[i] + 1, ans)
	if ans == 10000000:
		print(-1)
	else:
		print(ans)


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	min_r = 10 ** 9
	f = []
	for j in range(n + 1):
		f.append([])
	for j in range(len(a)):
		f[a[j]].append(j)
	for j in range(len(f)):
		if len(f[j]) > 1:
			k = f[j]
			r = 10 ** 9
			for u in range(len(k) - 1):
				r = min(r, k[u + 1] - k[u] + 1)
			min_r = min(min_r, r)
	if min_r == 10 ** 9:
		print(-1)
	else:
		print(min_r)


def main():
	import sys
	from collections import defaultdict
	input = sys.stdin.readline
	t = int(input())
	for _ in range(t):
		N = int(input())
		A = list(map(int, input().split()))
		idx = defaultdict(int)
		ans = 10 ** 9
		for (i, a) in enumerate(A):
			if idx[a] == 0:
				idx[a] = i + 1
			else:
				ans = min(ans, i + 1 - idx[a] + 1)
				idx[a] = i + 1
		if ans == 10 ** 9:
			print(-1)
		else:
			print(ans)
main()


def find_sub_array(array_object):
	n = int(input())
	full_array = [int(iv) for iv in input().split(' ')]
	sub_array_len = n
	unique_values_only = True
	if len(full_array) < 2:
		return -1
	else:
		for (index, value) in enumerate(full_array):
			if array_object.get(value) is None:
				array_object[value] = index
			else:
				unique_values_only = False
				new_sub_array_len = abs(array_object[value] - index) + 1
				if new_sub_array_len < sub_array_len:
					sub_array_len = new_sub_array_len
				array_object[value] = index
	return -1 if unique_values_only else sub_array_len
T = int(input())
dominated_object = {}
for i in range(0, T):
	print(find_sub_array(dominated_object))
	dominated_object = {}


t = int(input())
for _ in range(t):
	n = int(input())
	A = list(map(int, input().split()))
	if n == 1:
		print(-1)
		continue
	d = dict()
	res = n
	for i in range(n):
		if A[i] in d:
			res = min(res, i - d[A[i]] + 1)
		d[A[i]] = i
	if len(d) == n:
		print(-1)
	else:
		print(res)


import sys, math, queue, collections
MOD = 10 ** 9 + 7
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	x = [[] for i in range(n + 1)]
	for i in range(n):
		x[a[i]].append(i)
	ans = -1
	for i in range(n + 1):
		if len(x[i]) < 2:
			continue
		for j in range(1, len(x[i])):
			if ans == -1:
				ans = x[i][j] - x[i][j - 1] + 1
			else:
				ans = min(ans, x[i][j] - x[i][j - 1] + 1)
	print(ans)


t = int(input())
for i in range(t):
	ans = []
	d = {}
	n = int(input())
	l = [int(s) for s in input().split()]
	for j in range(n):
		if l[j] in d:
			ans.append(j - d[l[j]] + 1)
		d[l[j]] = j
	if ans != []:
		print(min(ans))
	else:
		print(-1)


from collections import defaultdict
t = int(input())
for query in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	d = defaultdict(list)
	s = {}
	gans = float('Infinity')
	for i in range(n):
		d[a[i]].append(i)
		if a[i] not in s:
			s[a[i]] = float('Infinity')
	for elem in d:
		if len(d[elem]) >= 2:
			k = d[elem]
			for j in range(1, len(d[elem])):
				s[elem] = min(abs(k[j] - k[j - 1]), s[elem])
			gans = min(s[elem], gans)
	if gans < float('Infinity'):
		print(int(gans) + 1)
	else:
		print(-1)


for _ in range(int(input())):
	n = int(input())
	a = [int(__) for __ in input().split()]
	if n >= 2:
		d = {}
		ans = float('inf')
		for i in range(n):
			if a[i] in d:
				ans = min(i - d[a[i]], ans)
			d[a[i]] = i
		if ans == float('inf'):
			print(-1)
		else:
			print(ans + 1)
	else:
		print(-1)


def solve(arr):
	idxs = dict()
	n = len(arr)
	for i in range(0, n):
		v = arr[i]
		if not v in idxs:
			idxs[v] = [i]
		else:
			lv = idxs[v]
			lv[len(lv):] = [i]
	minRange = n
	for key in idxs:
		lv = idxs[key]
		for i in range(1, len(lv)):
			minRange = min(minRange, lv[i] - lv[i - 1])
	print(minRange + 1 if minRange != n else -1)
for _ in range(0, int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	solve(arr)


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	cnt = [0] * (n + 1)
	id_last = [-1] * (n + 1)
	len_min = [n + 1] * (n + 1)
	id_last[arr[0]] = 0
	len_best = n + 1
	left = 0
	for i in range(1, n):
		if id_last[arr[i]] != -1:
			len_min[arr[i]] = min(len_min[arr[i]], i - id_last[arr[i]] + 1)
			len_best = min(len_best, len_min[arr[i]])
		id_last[arr[i]] = i
	if len_best == n + 1:
		print(-1)
	else:
		print(len_best)


import sys
import math
import itertools
import collections

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

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

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

def wr(arr):
	return ''.join(map(str, arr))

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

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

def convn(number, base=3):
	newnumber = ''
	while number > 0:
		newnumber = str(number % base) + newnumber
		number //= base
	return newnumber
t = ii()
for _ in range(t):
	n = ii()
	a = li()
	posd = {}
	for i in range(n):
		if a[i] not in posd:
			posd[a[i]] = [i]
		else:
			posd[a[i]].append(i)
	ans = n + 1
	for item in posd:
		lid = len(posd[item])
		if lid > 1:
			for i in range(lid - 1):
				ans = min(ans, posd[item][i + 1] - posd[item][i] + 1)
	if ans == n + 1:
		print(-1)
	else:
		print(ans)


import sys

def main():
	from collections import Counter
	from operator import itemgetter
	for tc in range(int(input())):
		n = int(input())
		arr = get_array()
		if n < 2:
			print(-1)
			continue
		d = {}
		ans = 10 ** 9
		for i in range(n):
			if arr[i] not in d:
				d[arr[i]] = i
			else:
				ans = min(ans, i - d[arr[i]] + 1)
				d[arr[i]] = i
		if ans == 10 ** 9:
			ans = -1
		print(ans)
get_array = lambda : list(map(int, sys.stdin.readline().split()))
get_ints = lambda : map(int, sys.stdin.readline().split())
input = lambda : sys.stdin.readline().strip()
main()


test = int(input())
pos = [-1] * 200005
for c in range(test):
	n = int(input())
	a = list(map(int, input().split()))
	for x in a:
		pos[x] = -1
	len = n + 5
	for i in range(n):
		if pos[a[i]] == -1:
			pos[a[i]] = i
		else:
			len = min(len, i - pos[a[i]] + 1)
			pos[a[i]] = i
	if len == n + 5 or len == 1:
		len = -1
	print(len)


from math import inf
t = int(input())
for i in range(t):
	n = int(input())
	array = list(map(int, input().split()))
	last_seen = dict()
	min_length = inf
	for j in range(len(array)):
		if array[j] in last_seen:
			length = j - last_seen[array[j]]
			min_length = min(min_length, length)
			last_seen[array[j]] = j
		else:
			last_seen[array[j]] = j
	if min_length != inf:
		print(min_length + 1)
	else:
		print(-1)


def fastio():
	import sys
	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()
MOD = 10 ** 9 + 7
INF = 10 ** 20
I = lambda : list(map(int, input().split()))
from collections import defaultdict as dd
from math import gcd
(t,) = I()
while t:
	t -= 1
	(n,) = I()
	l = I()
	ans = INF
	d = [[] for i in range(n + 1)]
	for i in range(n):
		d[l[i]].append(i)
	for i in range(n + 1):
		if len(d[i]) <= 1:
			continue
		for j in range(1, len(d[i])):
			ans = min(ans, d[i][j] - d[i][j - 1] + 1)
	if ans == INF:
		ans = -1
	print(ans)


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	d = {}
	mini = float('inf')
	if len(a) < 2:
		print(-1)
	else:
		for i in range(n):
			if a[i] not in d:
				d[a[i]] = i
			else:
				mini = min(mini, i - d[a[i]] + 1)
				d[a[i]] = i
		if mini != float('inf'):
			print(mini)
		else:
			print(-1)


import sys

def readline_single(dtype):
	return dtype(sys.stdin.readline().rstrip())

def readline_multiple(dtype):
	return map(dtype, sys.stdin.readline().split())

def readline_list(dtype):
	return list(map(dtype, sys.stdin.readline().split()))

def readline_string():
	return sys.stdin.readline()[:-1]

def print_list(lst):
	print(' '.join((str(item) for item in lst)))

def print_list_of_list(lists):
	for lst in lists:
		print_list(lst)

def solve(N, a):
	visited = {}
	ans = None
	start = 0
	for i in range(N):
		if a[i] in visited:
			if ans is None:
				ans = i - visited[a[i]] + 1
			else:
				ans = min(ans, i - visited[a[i]] + 1)
			start += 1
		visited[a[i]] = i
	return -1 if ans is None else ans

def main():
	T = readline_single(int)
	for _ in range(T):
		N = readline_single(int)
		a = readline_list(int)
		ans = solve(N, a)
		print(ans)
main()


from collections import defaultdict
t = int(input())
while t > 0:
	n = int(input())
	arr = [int(i) for i in input().split()]
	d = defaultdict(list)
	for i in range(n):
		d[arr[i]].append(i)
	mini = 100000000
	for i in d.keys():
		if len(d[i]) > 1:
			k = d[i]
			for i in range(1, len(k)):
				mini = min(mini, k[i] - k[i - 1] + 1)
	if mini == 100000000:
		mini = -1
	print(mini)
	t -= 1


t = int(input())
for i in range(t):
	n = int(input())
	dict_occ = {}
	arr = list(map(int, input().split()))
	for i in range(n):
		el = arr[i]
		if el in dict_occ:
			if i - dict_occ[el][1] < dict_occ[el][0]:
				dict_occ[el][0] = i - dict_occ[el][1]
			dict_occ[el][1] = i
		else:
			dict_occ[el] = [n, i]
	min_dist_i = min(dict_occ, key=lambda i: dict_occ[i][0])
	min_dist = dict_occ[min_dist_i][0]
	if min_dist == n:
		print(-1)
	else:
		print(min_dist + 1)


from math import inf
for _ in range(int(input())):
	i = int(input())
	l = list(map(int, input().split()))
	log = {x: -inf for x in set(l)}
	m = inf
	for x in range(i):
		m = min(x - log[l[x]] + 1, m)
		log[l[x]] = x
	print(-1 if m == inf else m)


import sys
import string
import math
import bisect as bi
input = sys.stdin.readline

def cin():
	return map(int, sin().split())

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

def sin():
	return input()

def inin():
	return int(input())
for i in range(inin()):
	n = inin()
	l = ain()
	d = dict()
	d1 = dict()
	if n == 1 or len(list(set(l))) == n:
		print(-1)
	else:
		ans = 1234567899
		for i in range(n):
			d[l[i]] = d.get(l[i], 0) + 1
			if d[l[i]] >= 2:
				temp = i - d1[l[i]] + 1
				if temp < ans:
					ans = temp
			d1[l[i]] = d1.get(0, 0) + i
		print(ans)


t = int(input())
while t:
	n = int(input())
	arr = [int(x) for x in input().split()]
	mmap = dict()
	ans = float('inf')
	for i in range(0, n):
		if arr[i] in mmap:
			ans = min(ans, i - mmap[arr[i]] + 1)
		mmap[arr[i]] = i
	print(ans if ans != float('inf') else -1)
	t -= 1


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


def dominated(n, l):
	p = {}
	m = n + 1
	for i in range(n):
		if l[i] in p.keys():
			m = min(m, i - p[l[i]] + 1)
		p[l[i]] = i
	return m if m != n + 1 else -1
for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	print(dominated(n, l))


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


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()
	if len(set(lst)) == n:
		print(-1)
	else:
		d = {}
		for i in range(n):
			item = lst[i]
			if item not in d:
				d[item] = [i, float('inf')]
			else:
				(index, size) = d[item]
				d[item] = [i, min(i - index + 1, size)]
		l = [v for (i, v) in d.values()]
		print(min(l))


import sys
ip = lambda : sys.stdin.readline().rstrip()
from collections import *
for _ in range(int(ip())):
	n = int(ip())
	a = list(map(int, ip().split()))
	d = {}
	ans = float('inf')
	for i in range(n):
		if d.get(a[i], -1) != -1:
			ans = min(ans, i - d[a[i]] + 1)
		d[a[i]] = i
	if ans == float('inf'):
		print(-1)
	else:
		print(ans)


from math import inf
t = int(input())
for tc in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	lasts = [-inf] * (n + 1)
	mn = inf
	for i in range(n):
		mn = min(mn, i - lasts[a[i]] + 1)
		lasts[a[i]] = i
	print(-1 if mn == inf else mn)


t = int(input())
for test in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	ind = [[] for i in range(n + 1)]
	for i in range(n):
		ind[arr[i]].append(i)
	ans = n
	for i in range(1, n + 1):
		if len(ind[i]) >= 2:
			for j in range(len(ind[i]) - 1):
				ans = min(ans, ind[i][j + 1] - ind[i][j])
	if ans == n:
		ans = -1
	else:
		ans += 1
	print(ans)


import sys
from array import array
import typing as Tp

def input():
	return sys.stdin.buffer.readline().decode('utf-8')

def main():
	t = int(input())
	inf = 10 ** 9
	for _ in range(t):
		n = int(input())
		a = list(map(int, input().split()))
		prev = {}
		ans = inf
		for (i, v) in enumerate(a):
			if v in prev and ans > i - prev[v] + 1:
				ans = i - prev[v] + 1
			prev[v] = i
		print(ans if ans < inf else -1)
main()


from sys import stdin, stdout
from collections import defaultdict
t = int(stdin.readline())
while t > 0:
	n = int(stdin.readline())
	a = list(map(int, stdin.readline().split()))
	(low, high) = (1, n)
	ans = 1
	while low <= high:
		mid = (low + high) // 2
		valid = False
		d = defaultdict(int)
		for i in range(mid):
			d[a[i]] += 1
			if d[a[i]] == 2:
				valid = True
		for i in range(mid, n + 1):
			d[a[i - mid]] -= 1
			if d[a[i - mid]] == 0:
				del d[a[i - mid]]
			if i < len(a):
				d[a[i]] += 1
				if d[a[i]] == 2:
					valid = True
					break
		if valid:
			ans = mid
			high = mid - 1
		else:
			low = mid + 1
	if ans == 1:
		ans = -1
	stdout.write('{}\n'.format(ans))
	t -= 1


T = int(input())
for _ in range(T):
	n = int(input())
	a = tuple((int(_) for _ in input().split()))
	if len(a) < 2:
		print(-1)
	else:
		freq = {}
		for (index, item) in enumerate(a):
			if item in freq:
				freq[item].append(index)
			else:
				freq[item] = [index]
		min_len = -1
		for (k, v) in freq.items():
			if len(v) >= 2:
				for i in range(len(v) - 1):
					if min_len == -1:
						min_len = v[i + 1] - v[i] + 1
					else:
						min_len = min(min_len, v[i + 1] - v[i] + 1)
		print(min_len)


cases = int(input())
for _ in range(cases):
	n = int(input())
	arr = list(map(int, input().split()))
	unique = {arr[0]}
	idx = {arr[0]: 0}
	left = 0
	right = 1
	prev = 0
	max = 200000.0 + 1
	ans = max
	while right < n:
		if arr[right] in unique:
			ans = min(ans, right - idx[arr[right]] + 1)
			left = idx[arr[right]] + 1
			for num in arr[prev:left]:
				unique.remove(num)
			prev = left
		else:
			unique.add(arr[right])
			idx[arr[right]] = right
			right += 1
	if ans == max:
		print(-1)
	else:
		print(ans)


from sys import stdin
rd = stdin.readline
rrd = lambda : map(int, rd().strip().split())
t = int(input())
while t:
	n = int(input())
	a = list(rrd())
	last = [0] * -~n
	ans = float('inf')
	for (i, v) in enumerate(a):
		if last[v]:
			ans = min(ans, i + 2 - last[v])
		last[v] = i + 1
	if ans == float('inf'):
		print(-1)
	else:
		print(ans)
	t -= 1


import sys
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	(d, flag) = ({}, 0)
	m = sys.maxsize
	if n == 1:
		flag = 1
		m = -1
	else:
		for i in range(n):
			if a[i] in d:
				d[a[i]].append(i)
			else:
				d[a[i]] = [i]
		for i in d:
			s = len(d[i])
			if s > 1:
				for j in range(s - 1):
					flag = 1
					m = min(m, d[i][j + 1] - d[i][j] + 1)
	if flag:
		print(m)
	else:
		print(-1)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	occ = [-1] * (n + 1)
	m = 200001
	(x, y) = (0, 0)
	for i in range(n):
		if occ[a[i]] != -1:
			m = min(m, i - occ[a[i]])
		occ[a[i]] = i
	if m < 200001:
		print(m + 1)
	else:
		print(-1)


import math, sys
from collections import Counter, defaultdict, deque
from sys import stdin, stdout
input = stdin.readline
lili = lambda : list(map(int, sys.stdin.readlines()))
li = lambda : list(map(int, input().split()))
I = lambda : int(input())
S = lambda : input()
t = I()
for i in range(0, t):
	n = I()
	a = li()
	d = defaultdict(list)
	for i in range(0, n):
		d[a[i]].append(i)
	m = 10 ** 6
	(l, r) = (0, 0)
	for i in d:
		if len(d[i]) == 1:
			continue
		for j in range(0, len(d[i]) - 1):
			f = d[i][j + 1] - d[i][j]
			m = min(m, f)
	if m == 10 ** 6:
		print(-1)
	else:
		print(m + 1)


import math
t = int(input())
for i in range(t):
	mini = 200007
	ans = dict()
	n = int(input())
	arr = list(map(int, input().split()))
	for j in range(n):
		if arr[j] not in ans.keys():
			ans[arr[j]] = j
		else:
			curr = j - ans[arr[j]]
			if curr < mini:
				mini = curr
			ans[arr[j]] = j
	if mini == 200007:
		print(-1)
	else:
		print(mini + 1)


n = int(input())
for i in range(n):
	x = int(input())
	xlist = [int(x) for x in input().split(' ')]
	if len(xlist) == 1:
		print('-1')
	else:
		xdic = {}
		d = 0
		for i in range(x):
			if str(xlist[i]) not in xdic:
				xdic[str(xlist[i])] = i
			else:
				c = xdic[str(xlist[i])]
				if d == 0:
					d = i - c
					xdic[str(xlist[i])] = i
				else:
					d = min(d, i - c)
					xdic[str(xlist[i])] = i
			if d == 1:
				break
		if d == 0:
			print('-1')
		else:
			print(d + 1)


t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(i) for i in input().split()]
	if n < 2:
		print(-1)
	elif n == len(set(a)):
		print(-1)
	else:
		last = {}
		ans = n
		for i in range(n):
			if a[i] in last:
				ans = min(ans, i - last[a[i]] + 1)
			last[a[i]] = i
		print(ans)


t = int(input())
for _ in range(t):
	n = int(input())
	li = list(map(int, input().split()))
	ans = n
	dic = {}
	for i in range(n):
		val = dic.get(li[i], -1)
		if val == -1:
			dic.update({li[i]: i})
		else:
			ans = min(ans, i - val + 1)
			dic.update({li[i]: i})
	if len(dic) == n:
		print(-1)
	else:
		print(ans)


for _ in range(int(input())):
	n = int(input())
	d = {}
	a = list(map(int, input().split()))
	if n == 1:
		print(-1)
	else:
		ans = 2 * 10 ** 5
		p = [None] * (n + 1)
		for i in range(1, n + 1):
			if p[a[i - 1]] == None:
				p[a[i - 1]] = i
			else:
				ans = min(ans, i - p[a[i - 1]] + 1)
				p[a[i - 1]] = i
		if ans == 2 * 10 ** 5:
			print(-1)
		else:
			print(ans)


N = int(200000.0 + 3)
adj = [[] for _ in range(N)]
t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	for i in range(n + 1):
		adj[i] = []
	for i in range(n):
		adj[arr[i]].append(i)
	res = int(2000000000.0)
	for i in range(n + 1):
		res = min([res] + [adj[i][j] - adj[i][j - 1] + 1 for j in range(1, len(adj[i]))])
	print(-1 if res == int(2000000000.0) else res)


for _ in range(int(input())):
	n = int(input())
	ans = n + 1
	d = {}
	for (x, y) in enumerate(map(int, input().split())):
		if y in d:
			ans = min(ans, x - d[y] + 1)
		d[y] = x
	print(-1 if ans > n else ans)


for _ in ' ' * int(input()):
	a = int(input())
	b = list(map(int, input().split()))
	c = [0] * (a + 1)
	k = a + 1
	for i in range(1, a + 1):
		if c[b[i - 1]]:
			k = min(k, i - c[b[i - 1]] + 1)
		c[b[i - 1]] = i
	print(k if k != a + 1 else -1)


from sys import stdin, stdout
from collections import defaultdict
nmbr = lambda : int(input())
lst = lambda : list(map(int, input().split()))
PI = float('inf')
for _ in range(nmbr()):
	n = nmbr()
	a = lst()
	freq = defaultdict(int)
	l = r = 0
	ans = PI
	while r < n:
		v = a[r]
		freq[v] += 1
		while freq[v] == 2:
			ans = min(ans, r - l + 1)
			freq[a[l]] -= 1
			l += 1
		r += 1
	print(ans if ans != PI else -1)


T = int(input())
for jsb in range(T):
	n = int(input())
	a = [int(i) for i in input().split()]
	(d, count) = (dict(), n)
	if n < 2:
		answer = -1
	else:
		for i in range(n):
			if a[i] not in d:
				d[a[i]] = i
			elif a[i] in d:
				count = min(i - d[a[i]], count)
				d[a[i]] = i
		if len(d) == n:
			answer = -1
		else:
			answer = count + 1
	print(answer)


import math
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	d = {}
	ans = math.inf
	for i in range(n):
		if a[i] not in d:
			d[a[i]] = i
		else:
			ans = min(ans, i - d[a[i]] + 1)
			d[a[i]] = i
	if ans == math.inf:
		print(-1)
	else:
		print(ans)


t = int(input())
for i in range(t):
	n = int(input())
	lst = list(map(int, input().strip().split(' ')))
	m = 200000
	l = [0] * (n + 1)
	for j in range(n):
		if l[lst[j]] != 0:
			if j - l[lst[j]] + 2 < m:
				m = j - l[lst[j]] + 2
		l[lst[j]] = j + 1
	if m == 200000:
		print(-1)
	else:
		print(m)


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

class FastIO(IOBase):
	newlines = 0

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

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

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

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

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
for t in range(int(input())):
	n = int(input())
	nums = list(map(int, input().split()))
	ans = 1000000.0
	li = [-1] * (n + 1)
	for j in range(len(nums)):
		if li[nums[j]] != -1:
			ans = min(ans, j - li[nums[j]] + 1)
		li[nums[j]] = j
	if ans > n:
		print(-1)
		continue
	print(ans)


(I, Z) = (input, int)
for _ in [0] * Z(I()):
	(n, a) = (Z(I()), [*map(Z, I().split())])
	(l, r, m, s) = (0, 0, 1000000000.0, [0] * 2 * n)
	while r < n:
		if s[a[r]]:
			(m, s[a[l]], l) = (min(m, r - l + 1), 0, l + 1)
		else:
			(s[a[r]], r) = (1, r + 1)
	print(-1 if m > n else m)


for w in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	if n == 1 or sorted(a) == sorted(list(set(a))):
		print(-1)
	elif len(set(a)) == 1:
		print(2)
	else:
		d = {}
		for i in range(n):
			if a[i] not in d:
				d[a[i]] = [i]
			else:
				d[a[i]].append(i)
		l1 = list(d.keys())
		l2 = list(d.values())
		ans = n
		for i in range(len(l2)):
			for j in range(len(l2[i]) - 1):
				ans = min(ans, l2[i][j + 1] - l2[i][j] + 1)
		print(ans)


def dominated(n, a):
	d = {}
	ans = -1
	for i in range(n):
		if d.get(a[i]) == None:
			d[a[i]] = i
		elif ans == -1:
			ans = i - d[a[i]] + 1
			d[a[i]] = i
		else:
			size = i - d[a[i]] + 1
			d[a[i]] = i
			if size < ans:
				ans = size
	return ans
T = int(input())
for i in range(T):
	n = int(input())
	a = [int(j) for j in input().split()]
	print(dominated(n, a))


t = int(input())
for i in range(t):
	c = 9999999999
	n = int(input())
	s = list(map(int, input().split()))
	d = {}
	a = []
	for j in range(n):
		if s[j] not in d:
			d[s[j]] = j
		else:
			a.append(j - d[s[j]] + 1)
			d[s[j]] = j
	if len(a) == 0:
		print(-1)
	else:
		print(min(a))


t = int(input())
for q in range(t):
	n = int(input())
	li = list(map(int, input().split(' ', n)[:n]))
	hs = [-1] * (max(li) + 1)
	mi = 1000000000000
	for i in range(n):
		if hs[li[i]] != -1:
			if i - hs[li[i]] + 1 >= 2 and i - hs[li[i]] + 1 < mi:
				mi = i - hs[li[i]] + 1
		hs[li[i]] = i
	if mi < 10000000000:
		print(mi)
	else:
		print(-1)


t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(x) for x in input().split()]
	if n == 1:
		print('-1')
	elif n == 2:
		if a[0] != a[1]:
			print('-1')
		else:
			print('2')
	else:
		d = {}
		mini = n + 1
		for i in range(n):
			if d.get(a[i]):
				d[a[i]].append(i)
			else:
				d[a[i]] = [i]
		for k in d.keys():
			if len(d[k]) > 1:
				temp = d[k][0]
				for i in range(1, len(d[k])):
					mini = min(mini, d[k][i] - temp + 1)
					temp = d[k][i]
		if mini == n + 1:
			print('-1')
		else:
			print(mini)


import os
import sys
from io import BytesIO, IOBase
from collections import defaultdict

def solution(arr, n):
	if n == 1:
		write(-1)
		return
	di = defaultdict(list)
	for i in range(n):
		di[arr[i]].append(i)
	(res, found) = (float('inf'), False)
	for (_, indices) in di.items():
		l = len(indices)
		for i in range(1, l):
			res = min(res, indices[i] - indices[i - 1] + 1)
			found = True
			if res == 2:
				write(2)
				return
	write(res if found else -1)

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


from sys import stdin
input = stdin.readline
for i in range(int(input())):
	n = int(input())
	d = dict()
	a = list(map(int, input().split()))
	if n == 1:
		print(-1)
		continue
	i = 0
	j = 1
	d[a[i]] = 1
	d[a[j]] = 1
	if a[i] == a[j]:
		print(2)
		continue
	j += 1
	M = float('inf')
	while i < n and j < n:
		if d.get(a[j], -1) == -1:
			d[a[j]] = 1
		else:
			while a[i] != a[j]:
				d[a[i]] = -1
				i += 1
			M = min(j - i + 1, M)
			i += 1
		j += 1
	if M == float('inf'):
		print(-1)
	else:
		print(M)


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	temp = {}
	for i in range(n):
		if a[i] not in temp.keys():
			temp[a[i]] = [i]
		else:
			temp[a[i]].append(i)
	ans = 10 ** 9
	for (key, val) in temp.items():
		if len(val) == 1:
			continue
		else:
			for i in range(1, len(val)):
				if val[i] - val[i - 1] + 1 < ans:
					ans = val[i] - val[i - 1] + 1
	if ans == 10 ** 9:
		print(-1)
	else:
		print(ans)


for i in range(int(input())):
	(N, X, MyDict, Min) = (int(input()), list(map(int, input().split())), {}, 10 ** 9)
	for j in range(N):
		if X[j] not in MyDict:
			MyDict[X[j]] = j
		else:
			Min = min(Min, j - MyDict[X[j]])
			MyDict[X[j]] = j
	print(-1 if Min == 10 ** 9 else Min + 1)


import collections
ii = lambda : int(input())
iia = lambda : list(map(int, input().split()))
isa = lambda : list(input().split())
t = ii()
for i in range(t):
	n = ii()
	x = iia()
	if n == 1:
		print('-1')
	else:
		dist = collections.defaultdict(lambda : n)
		last = collections.defaultdict(lambda : -1)
		mini = n
		flag = 0
		for i in range(n):
			if dist[x[i]] == n and last[x[i]] == -1:
				last[x[i]] = i
			elif last[x[i]] != -1 and dist[x[i]] > i - last[x[i]]:
				dist[x[i]] = i - last[x[i]] + 1
				last[x[i]] = i
				mini = min(dist[x[i]], mini)
				flag = 1
		if flag == 0:
			print(-1)
		else:
			print(mini)


for t in range(int(input())):
	n = int(input())
	a = [*map(int, input().split())]
	pos_ls = [[] for i in range(n)]
	for (i, e) in enumerate(a):
		pos_ls[e - 1].append(i)
	res = n + 1
	for l in pos_ls:
		if len(l) > 1:
			for i in range(1, len(l)):
				res = min(res, l[i] - l[i - 1])
	print(-1 if res == n + 1 else res + 1)


t = int(input())
for case_num in range(t):
	n = int(input())
	a = list(map(int, input().split(' ')))
	last = dict()
	ans = n + 1
	for i in range(n):
		if a[i] in last:
			ans = min(ans, i - last[a[i]] + 1)
		last[a[i]] = i
	print(-1 if ans == n + 1 else ans)


t = int(input())
while t > 0:
	n = int(input())
	arr = list(map(int, input().split()))
	m = [[] for _ in range(n + 1)]
	for i in range(1, n + 1):
		if len(m[arr[i - 1]]) < 2:
			m[arr[i - 1]].append(i)
			if len(m[arr[i - 1]]) == 2:
				d = m[arr[i - 1]][1] = m[arr[i - 1]][1] - m[arr[i - 1]][0]
				m[arr[i - 1]][0] = i
				m[arr[i - 1]][1] = d
		else:
			m[arr[i - 1]][1] = min(m[arr[i - 1]][1], i - m[arr[i - 1]][0])
			m[arr[i - 1]][0] = i
	flag = 0
	mini = 1000000
	for i in range(1, n + 1):
		if len(m[i]) > 1:
			flag = 1
			mini = min(m[i][1], mini, m[i][0])
	if flag == 0:
		print('-1')
	else:
		print(mini + 1)
	t -= 1


import sys

def func(arr, d, party, n):
	if n == 1 or n == 0:
		print(-1)
	else:
		cnt = 0
		for cur in arr:
			if cur in party:
				d = min(d, cnt - party[cur] + 1)
				party[cur] = cnt
			else:
				party[cur] = cnt
			cnt += 1
		if d == sys.maxsize:
			print(-1)
		else:
			print(d)
for _ in range(int(input())):
	party = {}
	d = sys.maxsize
	n = int(input())
	arr = list(map(int, input().split()))
	func(arr, d, party, n)


import math
import sys
sys.setrecursionlimit(10 ** 8)
input = sys.stdin.readline
INF = 10 ** 9
t = int(input())
for i in range(t):
	n = int(input())
	a = [int(item) for item in input().split()]
	if len(a) == 1:
		print(-1)
		continue
	seen = dict()
	ret = INF
	for (i, item) in enumerate(a):
		if item not in seen:
			seen[item] = i
		else:
			ret = min(ret, abs(seen[item] - i) + 1)
			seen[item] = i
	if ret == INF:
		print(-1)
	else:
		print(ret)


import sys
from collections import defaultdict

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')
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 18
MOD = 10 ** 9 + 7
ans = []
for _ in range(INT()):
	N = INT()
	A = LIST()
	d = defaultdict(lambda : -INF)
	mn = INF
	for (i, a) in enumerate(A):
		mn = min(mn, i - d[a] + 1)
		d[a] = i
	if mn == INF:
		ans.append(str(-1))
	else:
		ans.append(str(mn))
print('\n'.join(ans))


T = int(input())
for t in range(T):
	n = int(input())
	A = list(map(int, input().split()))
	if n < 2:
		print(-1)
	elif len(set(A)) == n:
		print(-1)
	else:
		M = {a: [] for a in A}
		for i in range(n):
			M[A[i]].append(i)
		ans = n
		for x in M:
			if len(M[x]) > 1:
				for i in range(1, len(M[x])):
					ans = min(ans, M[x][i] - M[x][i - 1] + 1)
		print(ans)


from datetime import datetime
import math

class Read:

	@staticmethod
	def string():
		return input()

	@staticmethod
	def int():
		return int(input())

	@staticmethod
	def list(sep=' '):
		return input().split(sep)

	@staticmethod
	def list_int(sep=' '):
		return list(map(int, input().split(sep)))

def solve():
	n = Read.int()
	a = Read.list_int()
	data = {}
	for i in range(n):
		if a[i] in data:
			data[a[i]].append(i)
		else:
			data[a[i]] = [i]
	m = math.inf
	for (i, v) in data.items():
		l = len(v)
		if l < 2:
			continue
		else:
			for j in range(l - 1):
				r = v[j + 1] - v[j]
				if r < m:
					m = r
	if m == math.inf:
		print('-1')
	else:
		print(m + 1)
query_count = Read.int()
while query_count:
	query_count -= 1
	solve()


t = int(input())
for i in range(t):
	n = int(input())
	d = list(map(int, input().split()))
	c = dict()
	x = float('inf')
	for i in range(n):
		if d[i] in c:
			x = min(x, i - c[d[i]] + 1)
		c[d[i]] = i
	if x == float('inf'):
		print(-1)
	else:
		print(x)


import sys
count = int(input())
result = []
for _ in range(count):
	length = int(input())
	array = list(map(lambda num: int(num), input().split()))
	posdict = {}
	ans = sys.maxsize
	flag = 0
	for (index, num) in enumerate(array):
		if not num in posdict.keys():
			posdict[num] = index
		else:
			flag = 1
			test = index - posdict[num]
			if test < ans:
				ans = test
			posdict[num] = index
			if ans == 1:
				break
	if flag == 0:
		result.append('-1')
	else:
		result.append(str(ans + 1))
print('\n'.join(result))


t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().strip().split()))[:n]
	from collections import defaultdict
	ans = float('inf')
	store = defaultdict(lambda : -1)
	for i in range(n):
		if store[arr[i]] != -1:
			ans = min(ans, i - store[arr[i]] + 1)
		store[arr[i]] = i
	if ans == float('inf'):
		ans = -1
	print(ans)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	if n == 1 or len(set(a)) == n:
		print('-1')
	else:
		m = 10 ** 6
		b = [-1] * n
		for j in range(n):
			if b[a[j] - 1] == -1:
				b[a[j] - 1] = j
			else:
				m = min(m, j - b[a[j] - 1] + 1)
				b[a[j] - 1] = j
		print(m)


import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	ls = [0] * (n + 1)
	ans = 10 ** 9
	for i in range(n):
		if ls[a[i]] != 0:
			ans = min(ans, i - ls[a[i]] + 2)
		ls[a[i]] = i + 1
	if ans == 10 ** 9:
		print(-1)
	else:
		print(ans)


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	if n < 2:
		print(-1)
	else:
		i = 0
		j = 0
		seen = set()
		pos = {}
		ans = n
		while i < n:
			if a[i] in seen:
				for ind in range(j, pos[a[i]]):
					seen.remove(a[ind])
					pos.pop(a[ind])
				j = pos[a[i]] + 1
				ans = min(ans, i - pos[a[i]] + 1)
				pos[a[i]] = i
			else:
				seen.add(a[i])
				pos[a[i]] = i
			i += 1
		if len(set(a)) == n:
			print(-1)
		else:
			print(ans)


for i in range(int(input())):
	(N, A, R) = (int(input()), [int(x) for x in input().strip().split()], False)
	(S, M) = ({c: -1 for c in set(A)}, N + 1)
	for (j, v) in enumerate(A):
		if S[v] != -1:
			M = min(M, j - S[v] + 1)
		S[v] = j
	print(M if M <= N else -1)


from sys import stdin as sin

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

def amap():
	return map(int, sin.readline().split())

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

def astr():
	return input()
for _ in range(aint()):
	n = aint()
	a = alist()
	d = {}
	ans = float('inf')
	for i in range(n):
		if d.get(a[i], None) == None:
			d[a[i]] = i
		else:
			ans = min(ans, i - d[a[i]] + 1)
		d[a[i]] = i
	if ans == float('inf') and ans != 0:
		print(-1)
	else:
		print(ans)


from collections import defaultdict
t = int(input())
for i in range(t):
	n = int(input())
	d = defaultdict(list)
	a = list(map(int, input().split()))
	for i in range(n):
		d[a[i]].append(i)
	m = n + 1
	for i in d.values():
		for j in range(1, len(i)):
			m = min(m, i[j] - i[j - 1] + 1)
	if m != n + 1:
		print(m)
	else:
		print(-1)


t = int(input())
while t:
	t -= 1
	n = int(input())
	a = list(map(int, input().split()))
	l = r = 0
	hashmap = {}
	ans = 1000000
	while r < n:
		if hashmap.get(a[r], None) == None:
			hashmap[a[r]] = r
		else:
			ans = min(ans, r - hashmap[a[r]] + 1)
			hashmap[a[r]] = r
			l += 1
		r += 1
	if ans == 1000000:
		print('-1')
	else:
		print(ans)


tc = int(input())
for ii in range(tc):
	n = int(input())
	ar = list(map(int, input().split()))
	if n == 1:
		print(-1)
		continue
	x = dict()
	m = float('inf')
	c = 0
	for i in range(n):
		if ar[i] in x:
			dif = i - x[ar[i]]
			m = min(m, dif)
			c = 1
			x[ar[i]] = i
		else:
			x[ar[i]] = i
	if c == 0:
		print(-1)
	else:
		print(m + 1)


from collections import defaultdict

def find(x):
	if len(li) == 1:
		return -1
	if len(li) == 2 and li[0] == li[1]:
		return 2
	ans = float('inf')
	dic = {}
	i = 0
	j = 0
	while i < len(li) and j < len(li):
		if li[i] in dic:
			index = dic[li[i]]
			ans = min(ans, i - index + 1)
			dic[li[i]] = i
		else:
			dic[li[i]] = i
		i += 1
	if ans == float('inf'):
		return -1
	return ans
for i in range(int(input())):
	x = input()
	li = list(map(int, input().split()))
	ans = find(li)
	print(ans)


test = int(input())
for t in range(test):
	n = int(input())
	a = list(map(int, input().split()))
	occurence = {}
	min_length = 10 ** 8
	for i in range(n):
		try:
			occurence[a[i]].append(i)
			temp = occurence[a[i]][-1] - occurence[a[i]][-2] + 1
			if temp < min_length:
				min_length = temp
		except KeyError:
			occurence[a[i]] = [i]
	if min_length != 10 ** 8:
		print(min_length)
	else:
		print(-1)


from collections import defaultdict
x = int(input())
for __ in range(x):
	d = defaultdict(int)
	n = int(input())
	a = list(map(int, input().split()))
	k = 1000000000001
	if len(a) > 1:
		for i in range(n):
			if d[a[i]] == 0:
				d[a[i]] = i + 1
			else:
				k = min(k, i + 1 - d[a[i]])
				d[a[i]] = i + 1
		if k != 1000000000001:
			print(k + 1)
		else:
			print(-1)
	else:
		print(-1)


t = int(input())
for I in range(t):
	n = int(input())
	a = [int(i) for i in input().split(' ')]
	last = [-1] * (n + 1)
	ans = 10 ** 6
	for i in range(n):
		if last[a[i]] == -1:
			last[a[i]] = i
		elif last[a[i]] >= 0:
			ans = min(ans, i - last[a[i]] + 1)
			last[a[i]] = i
	if ans == 10 ** 6:
		ans = -1
	print(ans)


import sys
from collections import defaultdict
t = int(input())
for kkk in range(t):
	n = int(sys.stdin.readline())
	a = list(map(int, sys.stdin.readline().split()))
	d = defaultdict(int)
	m = n + 1
	for i in range(len(a)):
		if d[a[i]] == 0:
			d[a[i]] = i + 1
		else:
			m = min(m, i - d[a[i]] + 2)
			d[a[i]] = i + 1
	if m == n + 1:
		print(-1)
	else:
		print(m)


t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(x) for x in input().split()]
	dic = {}
	s = set(a)
	m = 1000000000000000000000
	if len(a) == 1:
		print(-1)
	elif len(s) == len(a):
		print(-1)
	elif a.count(a[0]) == len(a):
		print('2')
	else:
		for i in range(len(a)):
			if a[i] in dic:
				m = min(m, i - dic[a[i]])
			dic[a[i]] = i
		print(m + 1)


for _ in range(int(input())):
	n = int(input())
	ans = n + 1
	c = {}
	for (i, x) in enumerate(map(int, input().split())):
		if x in c:
			ans = min(ans, i - c[x] + 1)
		c[x] = i
	print(-1 if ans > n else ans)


t = int(input())
while t > 0:
	t -= 1
	m = 9999999999
	n = int(input())
	a = [int(x) for x in input().split()]
	d = [[m, -1] for i in range(n + 1)]
	for i in range(n):
		if d[a[i]][0] == m and d[a[i]][1] == -1:
			d[a[i]][1] = i
		else:
			d[a[i]][0] = min(d[a[i]][0], i - d[a[i]][1])
			d[a[i]][1] = i
	mini = m
	for i in range(n):
		mini = min(mini, d[a[i]][0])
	print(mini + 1 if mini != m else -1)


t = int(input())
while t > 0:
	t -= 1
	n = int(input())
	a = []
	a = list(map(int, input().split()))
	if n < 2:
		print('-1')
		continue
	mp = {}
	size = 100000000
	for i in range(n):
		if not a[i] in mp:
			mp[a[i]] = i
		else:
			size = min(size, i - mp[a[i]] + 1)
			mp[a[i]] = i
	if size == 100000000:
		size = -1
	print(size)


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

class FastIO(IOBase):
	newlines = 0

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

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

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

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

class IOWrapper(IOBase):

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

def lcm(x, y):
	return x * y / gcd(x, y)

def fact(x, mod=mod):
	ans = 1
	for i in range(1, x + 1):
		ans = ans * i % mod
	return ans

def arr2D(n, m, default=0):
	return [[default for j in range(m)] for i in range(n)]

def arr3D(n, m, r, default=0):
	return [[[default for k in range(r)] for j in range(m)] for i in range(n)]

def sortDictV(x):
	return {k: v for (k, v) in sorted(x.items(), key=lambda item: item[1])}

class DSU:

	def __init__(self, length):
		self.length = length
		self.parent = [-1] * self.length

	def getParent(self, node, start):
		if node >= self.length:
			return False
		if self.parent[node] < 0:
			if start != node:
				self.parent[start] = node
			return node
		return self.getParent(self.parent[node], start)

	def union(self, node1, node2):
		parent1 = self.getParent(node1, node1)
		parent2 = self.getParent(node2, node2)
		if parent1 == parent2:
			return False
		elif self.parent[parent1] <= self.parent[parent2]:
			self.parent[parent1] += self.parent[parent2]
			self.parent[parent2] = parent1
		else:
			self.parent[parent2] += self.parent[parent1]
			self.parent[parent1] = parent2
		return True

	def getCount(self, node):
		return -self.parent[self.getParent(node, node)]

def solve(n, lst):
	map1 = {}
	dis = float('inf')
	for i in range(n):
		if map1.get(lst[i]) == None:
			map1[lst[i]] = i
		else:
			dis = min(dis, i - map1[lst[i]])
			map1[lst[i]] = i
	if dis == float('inf'):
		return -1
	return dis + 1
for _ in range(int(input())):
	n = int(input())
	lst = list(map(int, input().split()))
	print(solve(n, lst))


from sys import stdin
input = stdin.readline
for i in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	v = [0] * n
	l = []
	for i in range(n):
		if v[a[i] - 1]:
			l.append(i + 2 - v[a[i] - 1])
			v[a[i] - 1] = i + 1
		else:
			v[a[i] - 1] = i + 1
	print(min(l) if l else -1)


import math
from decimal import Decimal
import heapq
from collections import deque

def na():
	n = int(input())
	b = [int(x) for x in input().split()]
	return (n, b)

def nab():
	n = int(input())
	b = [int(x) for x in input().split()]
	c = [int(x) for x in input().split()]
	return (n, b, c)

def dv():
	(n, m) = map(int, input().split())
	return (n, m)

def dva():
	(n, m) = map(int, input().split())
	a = [int(x) for x in input().split()]
	b = [int(x) for x in input().split()]
	return (n, m, b)

def eratosthenes(n):
	sieve = list(range(n + 1))
	for i in sieve:
		if i > 1:
			for j in range(i + i, len(sieve), i):
				sieve[j] = 0
	return sorted(set(sieve))

def lol(lst, k):
	k = k % len(lst)
	ret = [0] * len(lst)
	for i in range(len(lst)):
		if i + k < len(lst) and i + k >= 0:
			ret[i] = lst[i + k]
		if i + k >= len(lst):
			ret[i] = lst[i + k - len(lst)]
		if i + k < 0:
			ret[i] = lst[i + k + len(lst)]
	return ret

def nm():
	n = int(input())
	b = [int(x) for x in input().split()]
	m = int(input())
	c = [int(x) for x in input().split()]
	return (n, b, m, c)

def dvs():
	n = int(input())
	m = int(input())
	return (n, m)

def fact(a, b):
	c = []
	ans = 0
	f = int(math.sqrt(a))
	for i in range(1, f + 1):
		if a % i == 0:
			c.append(i)
	l = len(c)
	for i in range(l):
		c.append(a // c[i])
	for i in range(len(c)):
		if c[i] <= b:
			ans += 1
	if a / f == f and b >= f:
		return ans - 1
	return ans
t = int(input())
for i in range(t):
	(n, a) = na()
	if n < 2 or len(set(a)) == n:
		print(-1)
	else:
		d = {}
		b = [-1] * (n + 1)
		mn = 124124124
		for q in range(n):
			j = a[q]
			if b[j] == -1:
				b[j] = q
			else:
				d = q - b[j]
				mn = min(mn, d)
				b[j] = q
		print(mn + 1)


a = int(input())
for i in range(a):
	b = int(input())
	lst = list(map(int, input().split()))
	c = b + 1
	d = [-1] * (b + 1)
	for t in range(b):
		if d[lst[t]] != -1:
			c = min(c, t - d[lst[t]] + 1)
		d[lst[t]] = t
	if c > b:
		c = -1
	print(c)


from collections import Counter, defaultdict
from sys import stdin, stdout
input = stdin.readline
I = lambda : int(input())
M = lambda : map(int, input().split())
LI = lambda : list(map(int, input().split()))
for _ in range(I()):
	n = I()
	a = LI()
	d = Counter(a)
	e = {}
	for i in d.keys():
		e[i] = []
	mi = -1
	for i in range(n):
		e[a[i]] += [i]
		if len(e[a[i]]) > 1:
			if mi == -1:
				mi = e[a[i]][-1] - e[a[i]][-2] + 1
			else:
				mi = min(e[a[i]][-1] - e[a[i]][-2] + 1, mi)
	print(mi)


T = int(input())
for t in range(T):
	n = int(input())
	arr = list(map(int, input().split()))
	d = {}
	for i in arr:
		d[i] = -1
	ans = 10 ** 6
	for i in range(n):
		if d[arr[i]] == -1:
			d[arr[i]] = i
		else:
			a = i - d[arr[i]] + 1
			ans = min(ans, a)
			d[arr[i]] = i
	if ans == 10 ** 6:
		print(-1)
		continue
	print(ans)


t = int(input())
for _ in range(t):
	a = int(input())
	arr = list(map(int, input().split()))
	(x, y) = ({}, [])
	for i in range(a):
		if arr[i] in x:
			y.append(i - x[arr[i]])
			x[arr[i]] = i
		else:
			x[arr[i]] = i
	if len(y) == 0:
		print(-1)
	else:
		print(min(y) + 1)


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split(' ')))
	d = dict()
	for i in range(n):
		if l[i] in d:
			d[l[i]].append(i + 1)
		else:
			d[l[i]] = [i + 1]
	l1 = list(filter(lambda x: len(x) > 1, d.values()))
	if len(l1) > 0:
		t = 9e+18
		for i in l1:
			for j in range(1, len(i)):
				if t > i[j] - i[j - 1] + 1:
					t = i[j] - i[j - 1] + 1
					if t == 2:
						break
			if t == 2:
				break
		print(t)
	else:
		print('-1')


for _ in range(int(input())):
	n = int(input())
	a = [*map(int, input().split())]
	d = {}
	ans = -1
	for i in range(n):
		if a[i] in d and ans == -1:
			ans = i + 1 - d[a[i]]
		elif a[i] in d and ans != -1:
			ans = min(ans, i + 1 - d[a[i]])
		d[a[i]] = i + 1
	print(ans + 1 if ans != -1 else ans)


t = int(input())
while t:
	t -= 1
	n = int(input())
	a = [int(x) for x in input().split()]
	if len(set(a)) == n:
		print(-1)
	else:
		mx = 1000000
		d = {i: 0 for i in range(2 * n)}
		i = 0
		j = 0
		while j < n:
			d[a[j]] += 1
			while i < j and d[a[j]] == 2:
				d[a[i]] -= 1
				i += 1
				mx = min(mx, j - i + 2)
			j += 1
		print(mx)


f = lambda : map(int, input().split())
t = int(input())
for _ in range(t):
	n = int(input())
	m = n + 1
	A = list(f())
	S = set()
	last = {}
	leng = {}
	for i in range(n):
		if A[i] not in S:
			last[A[i]] = i
			leng[A[i]] = 1
			S.add(A[i])
		else:
			if leng[A[i]] == 1:
				leng[A[i]] = i - last[A[i]] + 1
				last[A[i]] = i
				continue
			leng[A[i]] = min(leng[A[i]], i - last[A[i]] + 1)
			last[A[i]] = i
	for i in leng:
		if leng[i] != 1 and leng[i] < m:
			m = leng[i]
	print(-1 if m == n + 1 else m)


q = int(input())
for _ in range(q):
	n = int(input())
	a = list(map(int, input().split()))
	prev_pos = [-1] * n
	md = n
	is_dom = False
	for i in range(n):
		v = a[i]
		if prev_pos[v - 1] >= 0:
			md = min(md, i - prev_pos[v - 1])
			is_dom = True
		prev_pos[v - 1] = i
	if is_dom:
		print(md + 1)
	else:
		print(-1)


t = int(input())
for i in range(0, t):
	n = int(input())
	a = list(map(int, input().split()))
	b = [0] * int(300000.0)
	longest = n
	idx = 0
	for j in range(0, len(a)):
		if b[a[j]] != 0:
			idx = 1
			longest = min(longest, j + 1 - b[a[j]] + 1)
		b[a[j]] = j + 1
	if idx == 1:
		print(longest)
	else:
		print(-1)


r = ''
for i in range(int(input())):
	n = int(input())
	s = [int(i) for i in input().split()]
	d = {}
	ans = 10 ** 9
	for i in range(n):
		if s[i] in d:
			ans = min(i - d[s[i]] + 1, ans)
			d[s[i]] = i
		else:
			d[s[i]] = i
	if ans > 1000000:
		r += '-1\n'
	else:
		r += str(ans) + '\n'
print(r)


mod = 10 ** 9 + 7
import sys
sys.setrecursionlimit(10 ** 6)
from sys import stdin, stdout
import bisect
from bisect import bisect_left as bl
from bisect import bisect_right as br
import itertools
import collections
import math
import heapq
import random

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 cin():
	return map(int, sin().split())

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

def sin():
	return input()

def inin():
	return int(input())

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

def main():
	t = inin()
	for _ in range(t):
		n = inin()
		a = ain()
		d = {}
		e = {}
		f = {}
		ind = 0
		for i in a:
			if i not in d:
				d[i] = ind
			else:
				if i not in e:
					e[i] = ind - d[i]
				else:
					e[i] = min(e[i], ind - d[i])
				d[i] = ind
			ind += 1
		m = 10000000000000
		for i in e:
			m = min(e[i], m)
		if m > 1000000000000:
			m = -2
		print(m + 1)
py2 = round(0.5)
if py2:
	from future_builtins import ascii, filter, hex, map, oct, zip
	range = xrange
import os, sys
from io import IOBase, BytesIO
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))
input = lambda : sys.stdin.readline().rstrip('\r\n')
import sys

class ostream:

	def __lshift__(self, a):
		sys.stdout.write(str(a))
		return self
cout = ostream()
endl = '\n'

def readnumbers(zero=0):
	conv = ord if py2 else lambda x: x
	A = []
	numb = zero
	sign = 1
	i = 0
	s = sys.stdin.buffer.read()
	try:
		while True:
			if s[i] >= b'0'[0]:
				numb = 10 * numb + conv(s[i]) - 48
			elif s[i] == b'-'[0]:
				sign = -1
			elif s[i] != b'\r'[0]:
				A.append(sign * numb)
				numb = zero
				sign = 1
			i += 1
	except:
		pass
	if s and s[-1] >= b'0'[0]:
		A.append(sign * numb)
	return A
main()


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

class FastIO(IOBase):
	newlines = 0

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

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

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

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

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
testcases = int(input())
for j in range(testcases):
	n = int(input())
	vals = list(map(int, input().split()))
	setty = set([])
	arr1 = []
	dict1 = {}
	for s in range(n):
		if not vals[s] in setty:
			setty.add(vals[s])
			dict1[vals[s]] = [s]
		else:
			dict1[vals[s]].append(s)
	exi = False
	for s in dict1.keys():
		if len(dict1[s]) >= 2:
			exi = True
			break
	if exi == False:
		print(-1)
	else:
		minlength = n + 1
		for s in dict1.keys():
			val = dict1[s]
			for b in range(1, len(val)):
				length = val[b] + 1 - val[b - 1]
				if length < minlength:
					minlength = length
		print(minlength)


t = int(input())
for i in range(t):
	m = {}
	input()
	arr = list(map(int, input().split()))
	ans = float('inf')
	for (i, e) in enumerate(arr):
		if e not in m:
			m[e] = i
		else:
			ans = min(i - m[e] + 1, ans)
			m[e] = i
	if ans == float('inf'):
		ans = -1
	print(ans)


t = int(input())
for T in range(0, t):
	mp = {}
	array = []
	n = int(input())
	l = list(map(int, input().split()))
	for i in range(0, n):
		if l[i] in mp:
			array.append(i - mp[l[i]] + 1)
		mp[l[i]] = i
	if array == []:
		print(-1)
	else:
		print(min(array))


t = int(input())
for case in range(t):
	n = int(input())
	l = [int(x) - 1 for x in input().split()]
	last_seen = [-1 for x in range(n)]
	min_dist = [n for x in range(n)]
	for i in range(n):
		if last_seen[l[i]] != -1:
			min_dist[l[i]] = min(min_dist[l[i]], i - last_seen[l[i]])
		last_seen[l[i]] = i
	if min(min_dist) == n:
		print(-1)
	else:
		print(min(min_dist) + 1)


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	mp = {}
	pos = {}
	freq = {}
	for i in range(len(a)):
		if a[i] in mp:
			mini = i - pos[a[i]]
			if mini < mp[a[i]]:
				mp[a[i]] = mini
			pos[a[i]] = i
			freq[a[i]] += 1
		else:
			pos[a[i]] = i
			mp[a[i]] = 500000
			freq[a[i]] = 1
	mini = min(mp.values())
	if mini >= 200001:
		print(-1)
	else:
		print(mini + 1)


for _ in range(int(input())):
	n = int(input())
	s = list(map(int, input().split()))
	d = dict()
	mn_sub = 10 ** 6
	for i in range(n):
		if s[i] not in d:
			d[s[i]] = i
		else:
			if i - d[s[i]] < mn_sub:
				mn_sub = i - d[s[i]] + 1
			d[s[i]] = i
	if mn_sub == 10 ** 6:
		print(-1)
	else:
		print(mn_sub)


for nt in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	if len(set(l)) == n:
		print(-1)
		continue
	d = {}
	for i in range(n):
		if l[i] not in d:
			d[l[i]] = [i]
		else:
			d[l[i]].append(i)
	ans = n
	for i in d:
		for j in range(1, len(d[i])):
			ans = min(ans, d[i][j] - d[i][j - 1] + 1)
	print(ans)


def dom(n, arr):
	if n < 2:
		return -1
	nums = dict()
	min = n
	notRep = True
	for i in range(n):
		if arr[i] not in nums:
			nums[arr[i]] = i
		else:
			notRep = False
			dif = i - nums[arr[i]]
			if dif + 1 == 2:
				return 2
			elif dif + 1 < min:
				min = dif + 1
			nums[arr[i]] = i
	if notRep:
		return -1
	return min
num = int(input())
for i in range(num):
	n = int(input())
	arr = list(map(int, input().split()))
	print(dom(n, arr))


t = int(input())

def find(arr):
	res = -1
	d = {}
	for (i, el) in enumerate(arr):
		if el in d:
			if res == -1:
				res = i - d[el] + 1
			else:
				res = min(res, i - d[el] + 1)
		d[el] = i
	return res
for t in range(t):
	n = int(input())
	arr = map(int, input().split())
	print(find(arr))


for _ in range(int(input())):
	n = int(input())
	s = list(map(int, input().split()))
	f = {}
	m = 10 ** 6
	if n == 1 or len(set(s)) == n:
		print('-1')
	else:
		for i in range(n):
			if s[i] not in f:
				f[s[i]] = i
			else:
				m = min(m, i - f[s[i]] + 1)
				f[s[i]] = i
		print(m)


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().strip().split(' ')))
	x = [[0, -1] for _ in range(max(a) + 1)]
	ans = 10000000
	f = len(list(set(a)))
	if f == n:
		print(-1)
	elif f == 1 and f != n:
		print(2)
	else:
		for j in range(n):
			if x[a[j]][0] == 0:
				x[a[j]][0] += 1
				x[a[j]][1] = j
			elif x[a[j]][0] > 0:
				k = j - x[a[j]][1]
				x[a[j]][1] = j
				if k < ans:
					ans = k
		print(ans + 1)


from collections import Counter

def dominated_subarray(n, array):
	if n < 2:
		return -1
	curr_sub = Counter(array[:2])
	if len(curr_sub) == 1:
		return 2
	curr_sub[array[0]] = [curr_sub[array[0]], 0]
	curr_sub[array[1]] = [curr_sub[array[1]], 1]
	ans = n + 1
	for i in range(2, n):
		if array[i] in curr_sub:
			ans = min(ans, i + 1 - curr_sub[array[i]][1])
			curr_sub[array[i]][1] = i
		else:
			curr_sub[array[i]] = [1, i]
		if ans == 2:
			break
	if ans == n + 1:
		return -1
	return ans
q = int(input())
for i in range(q):
	n = int(input())
	array = list(map(int, input().split()))
	print(dominated_subarray(n, array))


from collections import defaultdict
from math import inf
for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	d = defaultdict(lambda : -1)
	m = inf
	for i in range(n):
		if d[arr[i]] != -1:
			m = min(i - d[arr[i]] + 1, m)
		d[arr[i]] = i
	if m == inf:
		print(-1)
	else:
		print(m)


for i in range(int(input())):
	n = int(input())
	a = [int(x) for x in input().split()]
	if n == 1:
		print(-1)
		continue
	boolean = False
	b = a.copy()
	b.sort()
	for j in range(len(b) - 1):
		if b[j] == b[j + 1]:
			boolean = True
			break
	if not boolean:
		print(-1)
		continue
	b = [-1] * (max(a) + 5)
	ans = n + 5
	for i in range(n):
		if b[a[i]] != -1:
			ans = min(ans, i - b[a[i]] + 1)
		b[a[i]] = i
	print(ans)


from sys import stdin

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

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

def linput():
	return list(map(int, stdin.readline().split()))
for _ in range(input()):
	n = input()
	l = linput()
	d = {}
	min = 20001999
	for i in range(n):
		if l[i] not in d:
			d[l[i]] = i
		elif min > i - d[l[i]] + 1:
			min = i - d[l[i]] + 1
			d[l[i]] = i
		else:
			d[l[i]] = i
	if min != 20001999:
		print(min)
	else:
		print(-1)