def gns():
    return list(map(int,input().split()))

t=int(input())
for i in range(t):
    n=int(input())
    ns=gns()
    lst={}
    ans=n+1
    for i in range(n):
        x=ns[i]
        if x in lst:
            ans=min(ans,i-lst[x]+1)
        lst[x]=i
    if ans==n+1:
        print(-1)
    else:
        print(ans)