T = int(input())
while T > 0:
    n = int(input())
    arr = list(map(int, input().split()))
    dic = {}
    start = 0
    end = 0
    res = n+1
    while end < n:
        if arr[end] in dic:
            res = min(res, end-dic[arr[end]]+1)
            while arr[start] != arr[end]:
                dic.pop(arr[start])
                start += 1
            start += 1
        dic[arr[end]] = end
        end += 1
    if res == n+1:
        res = -1
    print(res)


    T -= 1