T = int(input())

for _ in range(T):
    n = int(input())
    if n < 2:
        input()
        print(-1)
        continue
    array = input().split()
    dic = {}
    min_array = {}
    for i,element in enumerate(array):
        if element in dic:
            if element in min_array:
                min_array[element] = i - dic[element] if i - dic[element] < min_array[element] else min_array[element]
            else:
                min_array[element] = i - dic[element]
        dic[element] = i
    x = [min_array[i] for i in min_array.keys()]
    if x:
        print(min(x) + 1)
    else:
        print(-1)

	 	 				   	  	 	 	 	    	    	