from sys import stdin


def input():
    return stdin.readline()[:-1]


def intput():
    return int(input())


def sinput():
    return input().split()


def intsput():
    return map(int, sinput())


t = intput()

for _ in range(t):
    n = intput()
    a = list(intsput())

    d = {}
    found = False
    op = []
    for i in range(n):
        x = a[i]
        if x not in d:
            d[x] = [i]
        else:
            d[x].append(i)
            op.append(d[x][-1] - d[x][-2] + 1)
    if op:
        print(min(op))
    else:
        print(-1)
