T=int(input())
for i in range(T):
    n = int(input())
    arr = list(map(int, input().split()))
    remove_duplicated = set(arr)
    if len(arr) == len(remove_duplicated):
        print(-1)
        continue 

    answer = 10**5 * 3
    counter = [ [] for i in range(n+1) ]
    for i in range(len(arr)):
        counter[arr[i]].append(i)

    for i in range(n+1):
        if len(counter[i]) <= 1:
            continue
        
        for j in range(len(counter[i]) - 1):
            answer = min(answer, counter[i][j+1] - counter[i][j])

    print(answer+1)