def get_count_dominated(array):
    if len(array) < 2 or len(array) == len(set(array)):
        return -1
    # # shortest = 200000
    # # for i in range(len(array)):
    # #     if i + shortest - 1 > len(array):
    # #         temp = len(array)
    # #     else:
    # #         temp = i + shortest - 1
    # #     for j in range(i, temp):
    # #         if shortest == 2:
    # #             return shortest
    # #         if i != j and array[i] == array[j] and j - i + 1 < shortest:
    # #             shortest = j - i + 1
    # #             break
    # step, shortest = 2, None
    # while shortest is None:
    #     for step_num in range(step):
    #         for i in range(0, len(array), step):
    #             if i + step_num >= len(array) - 1:
    #                 continue
    #             elif i + step_num + step - 1 >= len(array):
    #                 _to = len(array) - 1
    #             else:
    #                 _to = i + step_num + step - 1
    #             if array[i + step_num] == array[_to]:
    #                 shortest = _to - (i + step_num) + 1
    #                 break
    #         if shortest is not None:
    #             break
    #     step += 1
    shortest = 200000
    result = dict()
    for index, elem, in enumerate(array):
        if elem not in result.keys():
            result[elem] = [index, shortest]
        else:
            diff = index - result[elem][0] + 1
            if diff < result[elem][1]:
                result[elem] = [index, diff]
            else:
                result[elem][0] = index
    result = [result[x] for x in result.keys()]
    return min(list(map(lambda x: x[1], result)))


N = int(input())
for _ in range(N):
    _ = input()
    print(get_count_dominated(list(map(int, input().split()))))
