for _ in '0'*int(input()):
    m = n = int(input())
    ns = list(map(int, input().split()))
    d = {}
    for i in range(n):
        if ns[i] in d.keys():
            m = min(m, i - d[ns[i]] + 1)
        d[ns[i]] = i
    print(m if m != n else -1)