def solution(arr):
    d = {}
    ans = len(arr) + 1 
    for i in range(len(arr)):
        val = d.get(arr[i],-1)
        if val == -1 :
            d[arr[i]] = i 
        else:
            dif = i - d[arr[i]]
            d[arr[i]] = i 
            ans = min(ans,dif)
    if ans == len(arr)+1 :
        return -1 
    return ans + 1
for _ in range(int(input())):
    n =int(input())
    arr = [int(x) for x in input().split()]
    ans = solution(arr)
    print(ans)