for _ in range(int(input())):
    n=int(input())
    d={}
    a=list(map(int,input().split()))
    if n==1:
        print(-1)
    else:
        ans=2*(10**5)
        p=[None]*(n+1)
        for i in range(1,n+1):
            if p[a[i-1]]==None:
                p[a[i-1]]=i
            else:
                ans=min(ans,i-p[a[i-1]]+1)  
                p[a[i-1]]=i
        if ans == 2*(10**5):
            print(-1)
        else:
            print(ans)