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)
