T = int(input())
for i in range(T):
    n = int(input())
    A = [int(x) for x in input().strip().split()]
    keys = set()
    d = {}
    m = float("Inf")
    for i in range(n):
        if A[i] in keys:
            dif = i-d[A[i]]+1
            if m>dif:
                m = dif
            d[A[i]]=i
        else:
            d[A[i]]=i
            keys.add(A[i])
    if m==float("Inf"):
        print(-1)
    else:
        print(m)