import collections
ii = lambda: int(input())
iia = lambda: list(map(int,input().split()))
isa = lambda: list(input().split())

t = ii()

for i in range(t):
	n = ii()
	x = iia()
	if(n==1):
		print('-1')
	else:
		dist = collections.defaultdict(lambda : n)
		last = collections.defaultdict(lambda : -1)
		mini = n
		flag = 0
		for i in range(n):
			if(dist[x[i]] == n and last[x[i]] == -1):
				last[x[i]] = i
			elif(last[x[i]] != -1 and dist[x[i]] > i-last[x[i]]):
				dist[x[i]] = i-last[x[i]]+1
				last[x[i]] = i
				mini = min(dist[x[i]], mini)
				flag = 1
		if(flag == 0):
			print(-1)
		else:
			print(mini)
