from sys import stdin,stdout
for i in stdin:
    t=int(i)
    break
def dom(L):
    d={}
    for i in range(len(L)):
        if L[i] in d.keys():
            d[L[i]].append(i)
        else:
            d[L[i]]=[i]
    return d
def asd(L):
    return [L[i+1]-L[i] for i in range(len(L)-1)]
for i in range(t):
    for i in stdin:
        n=int(i)
        break
    for i in stdin:
        N=list(map(int,i.strip().split()))
        break    
    D=dom(N)
    if n==1:
        stdout.write('-1\n')
    elif n==2:
        if N[0]==N[1]:
            stdout.write('2\n')
        else:
            stdout.write('-1\n')
    else:
        d=list(dom(N).values())
        
        x=[asd(i) for i in d]
        
        s=[]
        for i in x:
            s+=i
        if s==[]:
            stdout.write('-1\n')
        else:
            stdout.write(str(min(s)+1)+'\n')
        