t=int(input())
from collections import Counter
for _ in range(t):
    n=int(input())
    a=list(map(int,input().split()))
    if n==1:
        print(-1)
    else:
        ans=float("inf")
        r=1
        s=set()
        s.add(a[0])
        for l in range(n):
            while r<n and a[r] not in s:
                s.add(a[r])
                r+=1
            if r==n:
                r-=1
            if r!=l and a[r]==a[l]:
                ans=min(ans,r-l+1)
            s.remove(a[l])
        print(ans if ans!=float("inf") else -1)
