for _ in range(int(input())):
    n=int(input())
    a=list(map(int,input().split()))
    d={}
    c=10**7
    for i in range(n):
        if a[i] not in d:
            d[a[i]]=i
        else:
            z=i-d[a[i]]+1
            if z<c:
                c=z
            d[a[i]]=i
    if c==10**7: print(-1)
    else: print(c)