from sys import stdin
t = int(stdin.readline())
for _ in xrange(t):
    n = int(stdin.readline())
    a = map(int,stdin.readline().split())
    di = {}
    ans = 10**9 
    ii = 0
    for i in a:
        cur = ii-di.get(i,-10**9) + 1
        ans = min(ans,cur)
        di[i] = ii
        ii+=1
    if ans > n:
        ans = -1
    print ans