# How the flying fuck is python3 faster
for j in range(int(input())):
    subArrayIndexes = {}
    size = int(input()); results = 200000
    arr = input().split()
    for i in range(size):
        if arr[i] in subArrayIndexes.keys():
            results = min(results,i+1 - subArrayIndexes[arr[i]])
            if results == 1:
                break
        # moving the sorting part here(above) from the previous dictionay sort
        # Replacing the update function
        subArrayIndexes[arr[i]] = i
    print (results  if results != 200000 else -1)