import collections
for _ in range(int(raw_input())):
    n=int(raw_input())
    a=[int(x) for x in raw_input().split()]
    jj=0
    for i in range(n-1):
        if a[i]==a[i+1]:
            print(2)
            jj=1
            break 
    if jj==0:
        fre=dict(collections.Counter(a))
        f=[]
        for x in fre:
            if fre[x]>1:
                f.append(x)
        mi=n
        k=0
        #print(f)
        if n>=10**3:
            for x in f:
                ind=[]
                for i in range(0,n):
                    if x==a[i]:
                        if i-k<mi and i-k>=1:
                            mi=i-k 
                            k=i
        else:
            ind=[]
            for x in f:
                ind=[]
                for i in range(n):
                    if x==a[i]:
                        ind.append(i)
                for j in range(len(ind)-1):
                    if mi>ind[j+1]-ind[j]:
                        mi=ind[j+1]-ind[j]
        
        ans=mi+1 
        if ans>n or f==[]:
            print(-1)
        else:
            print(mi+1)
                    
                
            
            
            
            