T = int(input())
for t in range(T):
    n = int(input())
    A = [int(s) for s in input().split()]
    ans = n+1
    memo = dict()
    for i,num in enumerate(A):
        if num not in memo:
            memo[num] = i
        else:
            d = i - memo[num] + 1
            if d<ans:
                ans = d
            memo[num] = i
    if ans == n+1:
        print(-1)
    else:
        print(ans)