#4 1 2 4 5 4 3 2 1
for i in range(int(input())):
    d = dict()
    n = int(input())
    ans = n+1
    l = [int(j) for j in input().split()]
    for i in range(n):
        if l[i] in d:
            ans=min(ans, i-d[l[i]]+1)
            d[l[i]] = i
        else:
            d[l[i]] = i
    if ans!=n+1:
        print(ans)
    else:
        print(-1)
    
