t=int(input())
for i in range(0,t):
	n=int(input())
	L=list(map(int,input().split()))
	if(n==1):
		print(-1)
	else:
		counter=[0]*(n+1)
		M=[[] for j in range (n+1)]
		for i in range(0,n):
			counter[L[i]]+=1
			M[L[i]].append(i)
		if(max(counter)==1):
			print(-1)
		elif(max(counter)==n):
			print(2)
		else:
			mini=1e6+10
			for i in range (0,n+1):
				if(len(M[i])>1):
					for j in range (0,len(M[i])-1):
						mini=min(mini,M[i][j+1]-M[i][j])
			
			
			print(mini+1)

