def count(b):
    freq={}
    for i in b:
        if(i in freq):
            freq[i]+=1
        else:
            freq[i]=1
    return freq
if __name__=="__main__":
    t=int(input())
    while t!=0:
        t-=1
        n=int(input())
        a=list()
        a=raw_input()
        if n==1:
            print(-1)
            continue
        a=list(map(int,a.split(' ')))
        index=[0]*(n+1)
        x=0
        minv=n+2;
        for i in a:
            x+=1
            if index[i]!=0:
                minv=min(minv,x-index[i]+1)
            index[i]=x
        if(minv==n+2):
            print(-1)
        else:
            print(minv)