t = int(input())
for i in range(t):
    n = int(input())
    a = list(map(int, input().split()))
    v = [-1 for i in range(n + 1)]
    ms = n+1
    for i in range(n):
        if v[a[i]] == -1:
            v[a[i]] = i
        else:
            if ms > (i - v[a[i]] + 1):
                ms = i - v[a[i]] + 1
            v[a[i]] = i
    if ms == n+1:
        print(-1)
    else:
        print(ms)