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)