for _ in range(int(input())):
    n=int(input())
    a=list(map(int,input().split()))
    b=[]
    for i in range(n):
        b.append([])
    for i in range(n):
        b[a[i]-1].append(i)
        ans=10**15
   
    for i in range(n):
        for j in range(max(0,len(b[i])-1)):
            ans=min(ans,b[i][j+1]-b[i][j]+1)
    if ans==10**15:
        print(-1)
        continue
    print(ans)        
        
        
    
        