# f = open('input', 'r')

from collections import Counter
results = []

# for _ in range(int(f.readline())):
for _ in range(int(input())):
    # n = int(f.readline())
    n = int(input())
    # a = list(map(int, f.readline()[:-1].split(' ')))
    a = list(map(int, input().split(' ')))
    c = Counter()
    res = len(a)+1
    for i in range(0,len(a)):
        if c[a[i]] == 0:
            c[a[i]] = i-1
            continue
        res = min(res, i-c[a[i]])
        c[a[i]] = i-1
    if res == len(a)+1:
        results.append(-1)
    else:
        results.append(res)
for i in results:
    print(i)
