def find_sub_array(array_object):
    n = int(input())
    full_array = [int(iv) for iv in input().split(' ')]

    sub_array_len = n
    unique_values_only = True

    if len(full_array) < 2:
        return -1
    else:
        for index, value in enumerate(full_array):
            if array_object.get(value) is None:
                array_object[value] = index
            else:
                unique_values_only = False
                new_sub_array_len = abs(array_object[value] - index) + 1
                if new_sub_array_len < sub_array_len:
                    sub_array_len = new_sub_array_len
                array_object[value] = index

    return -1 if unique_values_only else sub_array_len


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

    dominated_object = {}

    for i in range(0, T):
        print(find_sub_array(dominated_object))
        dominated_object = {}
