for cas in range(int(input())):
    n = int(input())
    a = list(map(int, input().split()))
    last = [-1] * (n + 1)
    ans = n + 1

    for i in range(n):
        x = a[i]
        if last[x] != -1:
            ans = min(ans, i - last[x] + 1)
        last[x] = i

    print((ans, -1)[ans == n + 1])