for _ in range(int(input())):
    n=int(input())
    a=list(map(int,input().split()))
    d={}
    s=set()
    c=10000000
    for i in range(n):
        if a[i] not in s:
            s.add(a[i])
            d[a[i]]=i
        else:
            if c > i-d[a[i]]+1:
                c=i-d[a[i]]+1
            d[a[i]]=i
    if c==10000000:
        print(-1)
    else:
        print(c)