t = int(input())
for ti in range(t):
    n = int(input())
    a = list(map(int, input().split()))
    u = {}
    for x in a:
        u[x] = -1
    ans = 10 ** 9
    for i in range(n):
        x = a[i]
        if u[x] != -1:
            ans = min(ans, i - u[x] + 1)
        u[x] = i
    if ans == 10 ** 9:
        print("-1")
    else:
        print(ans)