from collections import defaultdict
from math import inf as INFINITY


def solve(n, a):
  nearest = INFINITY
  last_seen = defaultdict(lambda: -INFINITY)

  for i,x in enumerate(a):
    d = i - last_seen[x] + 1
    if d < nearest:
      nearest = d
    last_seen[x] = i

  return -1 if nearest == INFINITY else nearest


for _ in range(int(input())):
  n = int(input())
  *a, = map(int, input().split())

  print(solve(n, a))