T = int(raw_input())
for t in range(T):
    N = int(raw_input())
    A = list(map(int, raw_input().split()))
    # print("***"*20)
    # print("A : ", A)
    if N == 1:
        res = -1
    else:
        d = {}
        mini = N + 1
        for i, ai in enumerate(A):
            prev_occ = d.get(ai, -1)
            if prev_occ != -1:
                l = i - prev_occ + 1
                mini = min(mini, l)
            d[ai] = i
        if mini == N + 1:
            res = -1
        else:
            res = mini
    print(res)



# T = int(raw_input())
# for t in range(T):
#     N = int(raw_input())
#     A = list(map(int, raw_input().split()))
#     # print("***"*20)
#     # print("A : ", A)
#     if N == 1:
#         res = -1
#     else:
#         mini = N + 1
#         res = -1
#         max_occ = 1
#         d_occ = {}
#         ind0 = 0
#         # TODO : Move ind0
#         while(ind0 < N - 1):
#             # print("---"*10)
#             # print("ind0 : ", ind0)
#             # print(max_occ)
#             a0 = A[ind0]
#             d_occ = {}
#             d_occ[a0] = 1
#             for ind1 in range(ind0 + 1, N):
#                 ai = A[ind1]
#                 # print("ind1 / ai: ", ind1, ai)
#                 nb_occ = d_occ.get(ai, 0) + 1
#                 d_occ[ai] = nb_occ
#                 # print(d_occ)
#                 if nb_occ > max_occ:
#                     max_occ = nb_occ
#                     # Find minimum length
#                     min_l = -1
#                     # print(A)
#                     # print(ind0, ind1, d_occ)
#                     for ind2 in range(ind0, ind1):
#                         aj = A[ind2]
#                         nb_occ2 = d_occ.get(aj, 1)
#                         if nb_occ2 == nb_occ:
#                             min_l = ind1 - ind2 + 1
#                             d_occ[aj] = max_occ - 1
#                             max_occ -= 1
#                             break
#                         else:
#                             new_occ = d_occ[aj]
#                             d_occ[aj] = new_occ - 1
#                     assert min_l != -1
#                     # print("min_l : ", min_l)
#                     mini = min(mini, min_l)
#                     ind0 = ind1 - min_l + 1
#                     ind0 += 1
#                     # ind0 = ind1
#                     break
#                 # print(ind1, N)
#                 if ind1 == N - 1:
#                     # print("done")
#                     ind0 = N
#         if mini == N + 1:
#             res = -1
#         else:
#             res = mini
#     print(res)

