for t in range(int(input())):
    n=int(input())
    a=[int(i) for i in input().strip().split()]
    if n==1:
        print(-1)
    else:
        d={}
        for i in a:
            try:
                d[i]+=1
            except:
                d[i]=1
        mx=0
        m=max(d.values())
        for i in d.values():
            if m==i:
                mx+=1
        if mx>1:
            print(-1)
        else:
            for i in d.keys():
                if d[i]==m:
                    mx=i
                    break
            inx=[]
            for i in range(len(a)):
                if a[i]==mx:
                    inx.append(i)
            mn=9999999
            for i in range(len(inx)-1):
                mn=min(mn,inx[i+1]-inx[i])
            print(mn+1)
            
        
