from collections import Counter
for _ in '0'*int(input()):
    n = int(input())
    ns = list(map(int, input().split()))
    if n < 2:
        print(-1)
        continue
    d = dict(filter(lambda e: e[1]>1, Counter(ns[:]).items()))
    m = n
    for k in d.keys():
        a = 0
        for _ in range(d.get(k)-1):
            a = ns.index(k, a)
            b = ns.index(k, a+1)
            m = min(m, b - a + 1)
            a = b
    print(m)