import math,sys,bisect,heapq,os
from collections import defaultdict,Counter,deque
from itertools import groupby,accumulate
from functools import lru_cache
#sys.setrecursionlimit(200000000)
pr = lambda x:	x
def input(): return sys.stdin.readline().rstrip('\r\n')
#input = iter(sys.stdin.buffer.read().decode().splitlines()).__next__
aj = lambda: list(map(int, input().split()))
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
#MOD = 1000000000 + 7
def Y(c):  print(["NO","YES"][c])
def y(c):  print(["no","yes"][c])
def Yy(c):  print(["No","Yes"][c])

def solve():
	for _ in range(int(input())):
		n, = aj()
		A = aj()
		C = {}
		ans = 1e9
		for ii,i in enumerate(A):
			v = C.get(i,-1)
			if v == -1:
				C[i] = ii
			else:
				ans = min(ans, ii - v + 1)
				C[i] = ii
		if ans == 1e9:
			print(-1)
		else:
			print(ans)




try:
	#os.system("online_judge.py")
	sys.stdin = open('input.txt', 'r') 
	sys.stdout = open('output.txt', 'w')
	from aj import *
except:
	pass

solve()