from sys import stdin, stdout
from collections import defaultdict
nmbr = lambda: int(input())
lst = lambda: list(map(int, input().split()))
PI=float('inf')
for _ in range(nmbr()):
    n=nmbr()
    a=lst()
    freq=defaultdict(int)
    l=r=0
    ans=PI
    while r<n:
        v=a[r]
        freq[v]+=1
        while freq[v]==2:
            ans=min(ans, r-l+1)
            freq[a[l]]-=1
            l+=1
        r+=1
    print(ans if ans!=PI else -1)