import sys
import string
import math
import bisect as bi
input=sys.stdin.readline
def cin():
    return map(int,sin().split())
def ain():            
    return list(map(int,sin().split()))
def sin():
    return input()
def inin():
    return int(input())
##dp1=[1]*100
##dp1[0]=2
##for i in range(1,100):
##    dp1[i]=dp1[i-1]*2
for i in range(inin()):
    n=inin()
    l=ain()
    d=dict()
    d1=dict()
    if(n==1 or len(list(set(l)))==n):
        print(-1)
    else:
        ans=1234567899
        for i in range(n):
            d[l[i]]=d.get(l[i],0)+1
            if(d[l[i]]>=2):
                temp=i-d1[l[i]]+1
##                print(temp,i,d1[l[i]])
                if(temp<ans):
                    ans=temp
            d1[l[i]]=d1.get(0,0)+i
        print(ans)
                
        
    
        
##    n,x,a,b=cin()
##    if(a>b):a,b=b,a
##    if(x>=n-2 or (a==1 and b==n)):
##        print(n-1)
##    elif(b+x<=n):
##        b=b+x
##        print(b-a)
##    elif(a-x>0):
##        a=a-x
##        print(b-a)
##    else:
##        a=1
##        x=x-a+1
##        if(b+x>=n):
##            b=n
##        else:
##            b=b+x
##        print(b-a)

            
            
            

