for t in range(int(input())):
    n = int(input())
    a = [*map(int, input().split())]
    pos_ls = [[] for i in range(n)]
    for i, e in enumerate(a):
        pos_ls[e - 1].append(i)
    res = n + 1
    for l in pos_ls:
        if len(l) > 1:
            for i in range(1, len(l)):
                res = min(res, l[i] - l[i - 1])
    print(-1 if res == n + 1 else (res + 1))