t=int(input())
i=0
while i<t:
    n=int(input())
    arr=list(map(int,input().split()))
    diff=float('inf')
    d=[-2]*(n)
    for x in range(n):
        if d[arr[x]-1]==-2:
            d[arr[x]-1]=x
        else:
            a=x-d[arr[x]-1]
            if a<diff:
                diff=a+1
            d[arr[x]-1]=x
    if diff==float('inf'):
        print(-1)
    else:
        print(diff)


    i=i+1
