def array(arr, struc):
    return (list(map(struc, arr.split())))


n = int(input())
while n:
    lena = int(input()) + 1
    arr = array(input(), int)
    domArr = [lena] * (lena)

    for i in range(lena-1):
        if domArr[arr[i]] == lena:
            domArr[arr[i]] = lena + i + 1
        elif domArr[arr[i]] > lena:
            domArr[arr[i]] -= lena
            domArr[arr[i]] = i + 1 - domArr[arr[i]]
        else:
            domArr[arr[i]] = min(domArr[arr[i]], i-domArr[arr[i]])

    # print(domArr)
    if min(domArr) < lena:
        print(min(domArr)+1)
    else:
        print(-1)

    n -= 1
