t = int(input())
for i in range(t):
    n = int(input())
    a = list(map(int, input().split()))
    d = n + 1
    c = dict()
    for j in range(n):
        if a[j] in c:
            d = min(d, j - c[a[j]] + 1)
            c[a[j]] = j
        else:
            c[a[j]] = j
    if d == n + 1:
        print(-1)
    else:
        print(d)