I,Z=input,int
for _ in[0]*Z(I()):
 n,a=Z(I()),[*map(Z,I().split())]
 l,r,m,s=0,0,1e9,[0]*2*n
 while r<n:
  if s[a[r]]:
   m,s[a[l]],l=min(m,r-l+1),0,l+1
  else:
   s[a[r]],r=1,r+1
 print(-1 if m>n else m)

		 	  		    					 	 			   	 			