cases = int(input())
for _ in range(cases):
    n = int(input())
    arr = list(map(int, input().split()))
    unique = {arr[0]}
    idx = {arr[0]: 0}
    left = 0
    right = 1
    prev = 0
    max = 2e5 + 1
    ans = max
    while right < n:
        if arr[right] in unique:
            ans = min(ans, right - idx[arr[right]] + 1)
            left = idx[arr[right]] + 1
            for num in arr[prev:left]:
                unique.remove(num)
            prev = left
        else:
            unique.add(arr[right])
            idx[arr[right]] = right
            right += 1
    if ans == max:
        print(-1)
    else:
        print(ans)