def solve(arr,n):
    d = {}
    mv = 99999999999999
    for i in range(n):
        try:
            val = d[arr[i]]
            x = val[-1]
            val[-1] = i
            c = i-x+1 if x == 0 else i-x
            mv = min(mv,(i-x+1))
        except:
            d[arr[i]] = [1,i]
    if mv == 99999999999999:
        return -1
    return mv
t = int(input())
for _ in range(t):
    n = int(input())
    arr = list(map(int, input().split()))
    # arr.sort()
    if n < 2:
        print(-1)
    else:
        print(solve(arr,n))