from sys import stdin,stdout
t=int(stdin.readline())
for a in range(t):
    n=int(stdin.readline())
    l=[None]+list(map(int,stdin.readline().split()))
    d={}
    Min=0
    temp=0
    for a in range(1,len(l)):
        if(l[a] not in d.keys()):
            d[l[a]]=a
        else:
            temp=a-d[l[a]]+1
            if(Min==0):
                Min=temp
            elif(temp<Min):
                Min=temp
            d[l[a]]=a
    if(Min==0):
        print(-1)
    else:
        print(Min)


    

                
            
            
            
        
    
