for i in range(input()):
    n = input()
    a = map(int, raw_input().split())
    p = [-1] * (n + 1)
    ans = 1 << 30
    for i in range(n):
        if p[a[i]] != -1:
            ans = min(ans, i - p[a[i]])
        p[a[i]] = i
    print -1 if ans == 1 << 30 else ans + 1
