
maxn=200000
n=0
c,d=[0 for x in range(maxn+5)],[0 for x in range(maxn+5)]
T=int(input())
def judge():
    for i in range(0,n+1):
        if(d[i]>1):return False
    return True

while T>0:
    T-=1
    n=int(input())
    s=input().split()
    for i in range(0,n+1):d[i]=0
    for i in range(0,n):c[i]=int(s[i]);d[c[i]]+=1
    if(judge()):print(-1);continue
    for i in range(0,n+1):d[i]=-1
    ans=n
    for i in range(0,n):
        if(d[c[i]]==-1):d[c[i]]=i
        else:ans=min(ans,i-d[c[i]]+1);d[c[i]]=i
    print(ans)