t= int(input())
while t:
    n= int(input())
    a= list(map(int,input().split()))
    l=[-200001]*(n+1)
    mn=1000000

    for i in range(n):
        mn= min(mn,i-l[a[i]]+1)
        l[a[i]]=i

    if mn==200002:
        print(-1)

    else:
        print(mn)
    t=t-1
