gets.to_i.times{ n,s=gets.split n=n.to_i s=(s.to_r*1000).to_i ans=0 1.upto([n,999].min){|k| ans+=1 if (-s*k)%1000999 ans+=n-999 end puts ans }