import math
t = int(input())


def hello():
    n = int(input())
    lst = list(map(int, input().split()))
    have = set()
    min_len = 0
    length = 0
    indexes = dict()
    for i in range(len(lst)):
        if not lst[i] in indexes:
            indexes[lst[i]] = i
        else:
            if min_len > (i - indexes[lst[i]] + 1) or min_len == 0:
                min_len = i - indexes[lst[i]] + 1
            indexes[lst[i]] = i
    if min_len == 0:
        return -1
    else:
        return min_len


for i in range(t):
    print(hello())