def minDist(arr, n): 
    mp = {}  
    minDis = 100000000000
    for i in range(n): 
        if minDis == 1:
            return 2
        if arr[i] not in mp.keys(): 
            mp[arr[i]] = i 
        else: 
            minDis = min(minDis, abs(i-mp[arr[i]])) 
            mp[arr[i]] = i
  
    if minDis == 100000000000:
        return -1
    else:
        return minDis+1

for _ in range(int(input())):
    n = int(input())
    li = list(map(int, input().split()))
    print(minDist(li, n))