t=int(input())
for _ in range(t):
    n=int(input())
    arr=[int(x) for x in input().split()]
    occ=n+1
    d={}
    tmp=0
    for e in range(0,n):
        tmp+=1
        num=arr[e]
        if num in d:
            dist=e-d[num]+1
            if dist<occ:
                occ=dist
            d[num]=e
            tmp=1
        else:
            d[num]=e
    print(occ if occ<=n else -1)
 			 				  					 		   			 	   	