from sys import maxsize

for _ in range(int(input())):
    n = int(input())
    arr = [int(i) for i in input().split()]
    elements = len(set(arr))
    if elements == n:
        print(-1)
        continue
    elif elements == 1:
        print(2)
        continue
    pos_map = {}
    result = maxsize
    for index, elem in enumerate(arr):
        if elem in pos_map:
            result = min(result, index - pos_map[elem] + 1)
        pos_map[elem] = index
    print(result)
