t = int(input())
for i in range(t):
    n = int(input())
    dict_occ = {}
    arr = list(map(int, input().split()))
    for i in range(n):
        el = arr[i]
        if el in dict_occ:
            if i - dict_occ[el][1] < dict_occ[el][0]:
                dict_occ[el][0] = i - dict_occ[el][1]
            dict_occ[el][1] = i
        else:
            dict_occ[el] = [n, i]
    min_dist_i = min(dict_occ, key = lambda i: dict_occ[i][0])
    min_dist = dict_occ[min_dist_i][0]
    if min_dist == n:
        print(-1)
    else:
        print(min_dist + 1)