t=int(input())

while t>0:
    n=int(input())
    l=list(map(int,input().split()))
    d=dict()
    for i in range(len(l)):
        if l[i] not in d:
            d[l[i]]=[i]
        else:
            d[l[i]].append(i)
    mini=n+1
    flag=0
    for i in l:
        for j in range(1,len(d[i])):
            if(len(d[i])==0):
                mini=n+1
                break;
            mini=min(mini,d[i][j]-d[i][j-1])
            if(mini==1):
                flag=1
                break;
        if (flag==1):
            break;
    if mini<=n:
        print(mini+1)
    else:
        print(-1)
                    
    t-=1
        
