t = int(input())


def find(arr):
    res = -1
    d = {}
    for i, el in enumerate(arr):
        if el in d:
            if res == -1:
                res = i - d[el] + 1
            else:
                res = min(res, i - d[el] + 1)
        d[el] = i
    return res


for t in range(t):
    n = int(input())
    arr = map(int, input().split())
    print(find(arr))