n = int(input())
ss = []
for i in range(n):
    input()
    ss += [list(map(int, input().split(' ')))]

for i in range(n):
    s = ss[i]
    n = len(s)
    mem = [-1 for i in range(n+1)]

    q = n+10
    for j in range(n):
        if mem[s[j]] != -1:
            r = j - mem[s[j]] + 1
            if r < q:
                q = r
        mem[s[j]] = j
    if q == n+10:
        print('-1')
    else:
        print(q)