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)
