from sys import stdin
from collections import Counter, deque
#input = stdin.buffer.readline

T = int(input())
for _ in range(T):
    n = int(input())
    ls = list(map(int, input().split()))
    mp, mn = [-1]*(n+1), n+1
    for i, u in enumerate(ls):
        if mp[u] != -1:
            mn = min(mn, i-mp[u]+1)
        mp[u] = i
    print(-1 if mn==n+1 else mn)