import math

testCases = int(input())
for t in range(testCases):
    arrayLen = int(input())
    array = list(map(int, input().split(" ")))

    lastElement = {}
    smallerDominant = math.inf
    for index in range(len(array)):
        if(not array[index] in lastElement):
            lastElement[array[index]] = index
        else:
            newDist = index - lastElement[array[index]] + 1
            if(newDist < smallerDominant):
                smallerDominant = newDist

            lastElement[array[index]] = index

    if(smallerDominant == math.inf):
        smallerDominant = -1
    print(smallerDominant)

      	  	  		 	  		  	  					