q = int(input())
for _ in range(q):
    n = int(input())
    a = list(map(int, input().split()))
    prev_pos = [-1] * n
    md = n
    is_dom = False
    for i in range(n):
        v = a[i]
        if prev_pos[v - 1] >= 0:
            md = min(md, i - prev_pos[v - 1])
            is_dom = True
        prev_pos[v - 1] = i
    if is_dom:
        print(md + 1)
    else:
        print(-1)
