inp = lambda: map(int, input().rstrip().split())
for _ in range(int(input())):
    n = int(input())
    a = list(inp())
    li = [-1] * (n + 1)
    minm = n + 1
    for i in range(n):
        x = a[i]
        if li[x] == -1:
            li[x] = i
        else:
            minm = min(minm, i - li[x] + 1)
            li[x] = i
    if minm == n + 1:
        print(-1)
    else:
        print(minm)
