t=int(input())
while(t>0):
    n=int(input())
    a=[n]
    a = list(map(int, input().split()))
    dic={}
    m=0
    dic[a[0]]=[0]
    result=[]
    for i in range(1,n):
        if(a[i] in dic):
            l=len(dic[a[i]])
            if(l==1):
                x=dic[a[i]][0]
                m=i-x
                result.append(m)
            dic[a[i]].append(i)
            if(l>=1):
                m=min(m,i-dic[a[i]][l-1])
                result.append(m)
        else:
            dic[a[i]]=[i]
    if(len(result)==0):
        print(-1)
    else:
        print(min(result)+1)
    t=t-1
            
    
