def f(n, array):
    if n == 1: return -1
    ocorr = {}
    for l in range(1, n+1): ocorr[l] = -1
    res = -1
    for i in range(n):
        if ocorr[array[i]] == -1:
            ocorr[array[i]] = i
        else:
            if res == -1: res = i+1-ocorr[array[i]]
            else: res = min(res, i+1-ocorr[array[i]])
            ocorr[array[i]] = i
    
    return res


t = int(input())

for __ in range(t):
    n = int(input())
    array = [int(k) for k in input().split()]

    print(f(n, array))
