q = int(input())

def solve(nums):
    if len(nums) == 2:
        if nums[0] == nums[1]:
            return 2
        else:
            return -1
    dict_of_ind = dict()
    min_diff = 2*10**5 + 1

    for x in range(len(nums)):
        if nums[x] not in dict_of_ind:
            dict_of_ind[nums[x]] = x
            continue
        if x - dict_of_ind[nums[x]] + 1 < min_diff:
            min_diff = x - dict_of_ind[nums[x]] + 1
        dict_of_ind[nums[x]] = x
    if min_diff == 2*10**5 + 1:
        return -1
    return min_diff


for z in range(q):
    n = int(input())
    array = list(map(int, input().split()))

    print(solve(array))
