def ii(): return int(input())
def si(): return input()
def mi(): return map(int,input().split())
def li(): return list(mi()) 

import math

t=ii()
for i in range(t):
    n=ii()
    a=li()
    l=[-1]*(n+5) 
    mn=1e7
    for i in range(n):
        if l[a[i]]==-1:
            l[a[i]]=i 
        else:
            mn=min(mn,i-l[a[i]]+1) 
            l[a[i]]=i 
    if mn==1e7:
        print(-1)
    else:
        print(mn)