def dom(n, arr):
    if n < 2:
        return -1
    nums = dict()
    min = n
    notRep = True
    for i in range(n):
        if arr[i] not in nums:
            nums[arr[i]] = i
        else:
            notRep = False
            dif = i - nums[arr[i]]
            if (dif+1) == 2:
                return 2
            elif (dif+1) < min:
                min = dif+1
            nums[arr[i]] = i
    if notRep:
        return -1
    return min

if __name__=="__main__":
    num = int(input())
    for i in range(num):
        n = int(input())
        arr = list(map(int,input().split()))
        print(dom(n,arr))

	   		  		 	    		      		  	 	