gets.to_i.times{ a,b=gets.split(" ").map{|e| e.to_i} if b==1 then puts a-1 else ans=0 s1=1 m1=1 while s1