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)
    mn = 2000001

    for i in range(lena-1):
        if domArr[arr[i]] == lena:
            domArr[arr[i]] += (i+1)
        elif domArr[arr[i]] > lena:
            domArr[arr[i]] -= lena
            tmp = i+1 - domArr[arr[i]]
            domArr[arr[i]] = i+1
            mn = min(mn, tmp)
        else:
            tmp = i+1 - domArr[arr[i]]
            domArr[arr[i]] = i+1
            mn = min(mn, tmp)
            # print('Yes')

        #print(domArr, mn)

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

    n -= 1
