n = gets.to_i
ary = []

n.times do |i|
  k,pe = gets.split(" ").map(&:to_i)
  if(k >= pe)
    if(k%pe === 0)
      ary.push(pe)
    else
      ary.push(k%pe)
    end
  else
    ary.push(k)
  end
end

puts ary