import math

t = int(input())

for k in range(0, t):
    n = int(input())
    arr = input()
    arr = arr.split(" ")
    
    for i in range(0, len(arr)):
        arr[i] = int(arr[i])
    
    map = {}

    for i in range(0, len(arr)):
        if arr[i] in map:
            map[arr[i]].append(i)
        else:
            map[arr[i]] = [i]
    
    min_subarray = math.inf
    
    for value in map.values():
        if len(value) < min_subarray and len(value) >= 2:
            min_subarray = len(value)

    if(min_subarray == math.inf):
        print(-1)
    else:
        min_distance = math.inf

        for j in range(2, min_subarray + 1):
            for value in map.values():
                for k in range(0, len(value) - (j - 1)):
                    if (value[k + (j - 1)] - value[k]) < min_distance:
                        min_distance = value[k+(j - 1)] - value[k]
                    
                    if min_distance == 1:
                        break
                if min_distance == 1:
                    break
            if min_distance == 1:
                break
        print(min_distance + 1)     

        