def count(b):
    freq={}
    for i in b:
        if(i in freq):
            freq[i]+=1
        else:
            freq[i]=1
    return freq
if __name__=="__main__":
    t=int(input())
    while t!=0:
        t-=1
        n=int(input())
        a=list()
        a=raw_input()
        if n<=2:
            print(-1)
            continue
        a=list(map(int,a.split(' ')))
        freq={}
        freq=count(a)
        values=[(value,key)for key,value in freq.items()]
        # print(values.count(max(values)))
        if values.count(max(values))>1:
            print(-1)
            continue
        # print('dd:',values)
        # print('cc:',values[1])
        x=0
        y=100000000
        minval=10000000
        for j in a:
            x+=1
            if j==max(values)[1]:
                # print('cc:',max(values)[1])
                minval=min(minval,abs(x-y)+1)
                # print(x-y+1)
                y=x
        print(minval)