
t = int(input())

for gg in range(t):
	
	n = int(input())
	a = list(map(int, input().split()))
	
	if len(set(a)) == n or n == 1:
		print(-1)
	
	else:
		wp = 10000000000000
		kk = [-1] * (n + 1)
		mn = 10000000000000
		
		
		for gg in range(n):
			j = a[gg]
			if kk[j] == -1:
				kk[j] = gg
			else:
				wp = gg - kk[j]
				mn = min(mn, wp)
				kk[j] = gg
		
		
		print(mn + 1)
