def solve(arr):
    if len(arr) < 2:
        return -1
    if len(arr) == 2:
        if arr[0] == arr[1]:
            return 2
        else:
            return -1

    d, ans = {}, len(arr)+1

    for i, x in enumerate(arr):
        if x in d:
            if ans > (i - d[x] + 1):
                ans = i - d[x] + 1
        d[x] = i

    if ans < len(arr) + 1:
        return ans
    else:
        return -1


if __name__ == '__main__':
    t = int(input())

    for _ in range(t):
        n = int(input().strip())
        if n == 1:
            arr = int(input().strip())
            print("-1")
            continue

        arr = list(map(int, input().strip().split()))

        assert len(arr) == n

        print(solve(arr))
