def process_input_line(dom_obj):
    length = int(input())
    input_array = [int(x) for x in input().split(' ')]
    dom_obj = {}

    if length == 1:
        return -1
    else:
        for i in range(0, length):
            if dom_obj.get(input_array[i]) is None:  # Unknown Number
                dom_obj[input_array[i]] = i
            else:  # Know number
                if dom_obj[input_array[i]] == i - 1:
                    return 2
                else:
                    if dom_obj.get(-1) is None:  # first sub array
                        dom_obj[-1] = abs(dom_obj[input_array[i]] - i) + 1  # set length
                    else:
                        if abs(dom_obj[input_array[i]] - i) + 1 < dom_obj[-1]:
                            dom_obj[-1] = abs(dom_obj[input_array[i]] - i) + 1
                    dom_obj[input_array[i]] = i

        if dom_obj[-1] is None:
            return -1
        else:
            return dom_obj[-1]


if __name__ == '__main__':
    T = int(input())
    dominated_object = {}
    try:
        for input_index in range(T):
            print(process_input_line(dom_obj=dominated_object))
    except Exception as e:
        print(e)
