for _ in range(int(input())):
    n = int(input())
    ans = n + 1
    c = {}
    for i, x in enumerate(map(int, input().split())):
        if x in c:
            ans = min(ans, i - c[x] + 1)
        c[x] = i
    print(-1 if ans > n else ans)
