import sys
count = int(input())
result = []

for _ in range(count):

    length = int(input())
    array = list(map(lambda num: int(num), input().split()))
    posdict = {}
    ans = sys.maxsize
    flag = 0
    for index, num in enumerate(array):
        if not (num in posdict.keys()):
            posdict[num] = index
        else:
            flag = 1
            test = index - posdict[num]
            if test < ans:
                ans = test
            posdict[num] = index
            if ans == 1:
                break

    if flag == 0:
        result.append("-1")
    else:
        result.append(str(ans + 1))

print('\n'.join(result))
