from collections import Counter
for _ in range(int(input())):
    a=int(input())
    x=list(map(int,input().split()))
    d=dict(Counter(x))
    #print(d)
    m=max(d.values())
    #print(m)
    l=[]
    e={}
    if(m==1):
        print(-1)
    else:
        for i in d:
            if(d[i]==m):
                l.append(i)
        for j in l:            
            for i in range(a):
                if(x[i]==j):
                    if(j not in e):
                        e[j]=[i]
                    else:
                        e[j].append(i)
        #print(e)                    
        m=10000000000
        for i in e:
            for j in range(len(e[i])-1):
                if(m>(e[i][j+1]-e[i][j])+1):
                    m=e[i][j+1]-e[i][j]+1
        print(m)                
                
        
            