from sys import stdin
rd = stdin.readline
rrd = lambda: map(int, rd().strip().split())

t = int(input())
while t:
    n = int(input())
    a = list(rrd())
    last = [0]*-~n
    ans = float('inf')
    for i,v in enumerate(a):
        if last[v]:
            ans = min(ans , i+2-last[v])
        last[v] = i+1
    if ans == float('inf'):
        print(-1)
    else:
        print(ans)



    t -= 1
