import sys
for t in range(int(sys.stdin.readline())):
    n = int(sys.stdin.readline())
    d = [int(i) for i in sys.stdin.readline().split()]
    inds = [[] for i in range(n+1)]
    for i in range(n):
        inds[d[i]].append(i)
    mn = float('inf')
    for i in range(1, n+1):
        q = len(inds[i])
        kk = [inds[i][j+1] - inds[i][j] for j in range(q-1)]
        if len(kk):
            mn = min(mn, min(kk))
    if mn == float('inf'):
        print(-1)
    else:
        print(mn + 1)