for _ in range(int(input())):
    n=int(input())
    arr=list(map(int,input().split()))
    dic={}
    if n==1:
        print(-1)
    else:
        ma=float('inf')
        flag=0
        for i in arr:
            dic[i]=-1
        for i in range(n):
            if dic[arr[i]]==-1:
                dic[arr[i]]=i
            else:
                ma=min(i-dic[arr[i]]+1,ma)
                dic[arr[i]]=i
                flag=1
                
        if flag==0:
            print(-1)
        else:
            print(ma)
                
                
    
    