T = int(input())
results = []
for i in range(T):
  n = int(input())
  vals = input().split()
  a = [int(val) for val in vals]
  if len(a) < 2:
    results.append(-1)
  else:
    d = {}
    minLen = -1
    # key: number, val: lastPos
    for i in range(n):
      a_i = a[i]
      if a_i in d:
        if minLen == -1:
          minLen = i - d[a_i] + 1
        else:
          minLen = min(minLen, i - d[a_i] + 1)
      d[a_i] = i
    results.append(minLen)

for result in results:
  print(result)
