for _ in range(int(input())):
    n, A = int(input()), input().split()
    d = n
    seen = {}
    for i, a in enumerate(A):
        if a in seen:
            d = min(d, i - seen[a])
        seen[a] = i
    print(d+1 if d < n else -1)