import sys
input=sys.stdin.readline

t=int(input())
for _ in range(t):
  n=int(input())
  a=list(map(int,input().split()))
  ls=[0]*(n+1)
  ans=10**9
  for i in range(n):
    if ls[a[i]]!=0:
      ans=min(ans,i-ls[a[i]]+2)
    ls[a[i]]=i+1
  if ans==10**9:
    print(-1)
  else:
    print(ans)