for i in range(int(input())):
    n=int(input())
    arr=list(map(int,input().split()))
    v=[-1]*(n+1)
    m=999999999
    for i in range(n):
        if(v[arr[i]]!=-1):
            m=min(m,(i-v[arr[i]])+1)
        v[arr[i]]=i
    if(m==999999999):
        print("-1")
    else:
        print(m)