# -*- coding: utf-8 -*-

t = int(input().strip())

for i in range(t):
    n = int(input().strip())
    A = [int(j) for j in input().strip().split()]
    if n == 1:
        print('-1')
    else:
        result = n + 1
        right_ind = 1
        elems = {A[0]: 0}
        while result != 2 and right_ind < n:
            right = A[right_ind]
            if right in elems:
                dist = right_ind - elems.get(right) + 1
                if dist < result:
                    result = dist
            elems.update({right: right_ind})
            right_ind += 1
        if result == n + 1:
            print('-1')
        else:
            print(result)
