for _ in " "*int(input()):
    a=int(input());b=list(map(int,input().split()));c=[0]*(a+1);k=a+1
    for i in range(1,a+1):
        if c[b[i-1]]:k=min(k,i-c[b[i-1]]+1)
        c[b[i-1]]=i
    print(k if k!=a+1 else -1)