t = int(input())
for _ in range(t):
    n = int(input())
    arr = [int(j) for j in input().split()]
    last = {}
    ans = None
    for pos in range(n):
        val = arr[pos]
        if val in last:
            cur_ans = pos - last[val] + 1
            last[val] = pos
            if ans is None:
                ans = cur_ans
            ans = min(ans, cur_ans)
        else:
            last[val] = pos
    if ans is None:
        print(-1)
    else:
        print(ans)
