from collections import defaultdict

T = int(input())

for _ in range(T):
    n = int(input())
    a = list(map(int, input().split()))
    d = defaultdict(int)
    ans = 10**18
    
    for i in range(n):
        if a[i] in d:
            ans = min(ans, i-d[a[i]]+1)
         
        d[a[i]] = i
    
    if ans == 10**18:
        print(-1)
    else:
        print(ans)