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)