T=int(input())
x={}
while T!=0:
    T-=1
    s=int(input())
    a=list(map(int,input().split()))
    minn=999999
    for i in range(s):
        if a[i] not in x:
            x[a[i]]=i
        else:
            if minn>i-x[a[i]]+1:
                minn=i-x[a[i]]+1
            x[a[i]]=i
    if minn==999999:
        print("-1")
    else:
        print(minn)
    x.clear()
        
    
