for _ in '0'*int(input()):
    m = n = int(input())
    s = input()
    ns, set_s = s.replace(' ', ''), set(s.split())
    for x in set_s:
        a = 0
        while True:
            a = ns.find(x, a)
            b = ns.find(x, a+1)
            if a == -1 or b == -1:
                break
            m = min(m, b - a + 1)
            a = b
    print(m if n != len(set_s) else -1)