import sys
 
input = sys.stdin.readline
 
 
t = int(input())
 
while t:
    t -= 1
 
    n = int(input())
    a = list(map(int, input().split()))
 
    ind = {}
    ans = float("inf")
    for i, el in enumerate(a):
        if el in ind:
            ans = min(ans, i-ind[el]+1)
        ind[el] = i
    
    print(-1 if ans > n else ans)