def solve():
    n = int(input())
    a = list(map(int, input().split()))
    d = {}
    ans = float("inf")
    for i, ai in enumerate(a):
        if ai in d:
            ans = min(ans, i-d[ai]+1)
        d[ai] = i
    if ans == float("inf"):
        ans = -1
    print(ans)


t = int(input())
for i in range(t):
    solve()