t = int(input())
for _ in range(t):
    n = int(input())
    A = list(map(int,input().split()))
    D = [list() for _ in range(n+1)]
    for i in range(n):
        D[A[i]].append(i)
    res = 2**100
    for i in range(n+1):
        for j in range(len(D[i])-1):
            res = min(res, D[i][j+1]-D[i][j]+1)
    if res == 2**100:
        print(-1)
    else: print(res)
