import sys

data = [[int(x) for x in y.split()] for y in sys.stdin.readlines()[2::2]]

for a in data:
    d = {}
    min_len = -1
    for i, x in enumerate(a):
        if x in d:
            if min_len == -1:
                min_len = i-d[x]+1
            else:
                min_len = min(min_len, i-d[x]+1)
        d[x] = i
    print(min_len)
