for i in range(int(input())):
    n=int(input())
    a=list(map(int,input().split()))
    b=[]
    for i in set(a):
        b.append([a.count(i),i])
    c=max(b)[1]
    s=1;d=[]
    for i in a[(a.index(c)+1):]:
        s+=1
        if i==c:
            d.append(s)
            s=1
    print(min(d) if d!=[] else -1)
        
        
        
        
