import math
import bisect
from collections import defaultdict as df
from collections import deque
from sys import stdin,stdout
t=int(input())
for i in range(t):
    n=int(input())
    a=list(map(int,input().rstrip().split()))
    d=df(list)
    ans=float('inf')
    for i in range(n):
        d[a[i]].append(i)
        if len(d[a[i]])>=2:
            ans=min(ans,d[a[i]][-1]-d[a[i]][-2] + 1)
        
    if n==1:
        print(-1)
        continue
    
    if ans==float('inf'):
        print(-1)
    else:
        print(ans)
        
    
    