for q in range(int(input())):
    n = int(input())
    s = [int(i) for i in input().split()]

    d = {}

    mn = float("inf")
    for i in range(n):
        if d.get(s[i]) is not None and i - d[s[i]] + 1 < mn:
            mn = i - d[s[i]] + 1
        d[s[i]] = i

    if mn > n:
        print(-1)
    else:
        print(mn)