for t in range(int(input())):
    n = int(input())
    l = list(map(int, input().split()))
    m, d = n+1, {}
    for i in range(n):
        if l[i] in d.keys():
            m = min(m, i - d[l[i]] + 1)
        d[l[i]] = i
    print(m if m != n+1 else -1)