

n = int(input())

for nn in range(n):

    length = int(input())
    arr = [int(x) for x in input().split(" ")]
    # count = {}
    last = {}
    min_length = 2*10**5+1

    for i in range(len(arr)):

        if arr[i] in last:
            diff = i - last[arr[i]]
            # if arr[i] in count and count[arr[i]] > diff:
            #     count[arr[i]] = diff
            # else:
            #     count[arr[i]] = diff
            if diff < min_length:
                min_length = diff

        last[arr[i]] = i

    if min_length == 2*10**5+1:
        print(str(-1))
    else:
        print(str(min_length+1))


