T = int(input())

for _ in range(T):
    n = int(input())
    array = list(map(int, input().split()))
    array1 = []
    for pos, num in enumerate(array):
        array1.append([num, pos])
    array1.sort()
    low = n + 1
    for i in range(len(array1) - 1):
        if array1[i][0] == array1[i + 1][0]:
            if abs(array1[i][1] - array1[i + 1][1]) < low:
                low = abs(array1[i][1] - array1[i + 1][1])
    if low == n + 1:
        print(-1)
    else:
        print(low + 1)