t=int(input())
for tst in range(t):
    n=int(input())
    l=list(map(int,input().split()))
    mini=1000000000
    d={}
    if len(l)<2:
        print(-1)
    else:
        for i in range(n):
            if l[i] not in d:
                d[l[i]]=i

                #print(d)
            else:

                    mini=min(i-d[l[i]]+1,mini)
                    d[l[i]]=i
                    #print(d,"555555")
        if mini!=1000000001:
            print(mini)
        else:
            print(-1)









