import math
t=int(input())
for _ in range(t):
    n=int(input())
    l=list(map(int,input().split()))
    s=set(l)
    m=math.inf
    if(n==1):
        print(-1)
    elif(n==len(s)):
        print(-1)
    else:
        for i in range(n-1):
            a=l[i]
            if(a in l[i+1:]):
                b=l[i+1:].index(a)
                b=b+i+1
                m=min(m,b-i)
                m=m+1
            if(m==2):
                break
        print(m)
        
