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