t = int(input())
while t:
    n = int(input())
    arr = [int(x) for x in input().split()]
    mmap = dict()
    ans = float("inf")
    for i in range(0, n):
        if arr[i] in mmap:
            ans = min(ans, i - mmap[arr[i]] + 1)
        mmap[arr[i]] = i
    print(ans if ans != float("inf") else -1)
    t -= 1
