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)