num = STDIN.gets

games = Array.new
num.to_i.times do
  games << ((STDIN.gets).split).map { |i| i.to_i }
end

games.each do |val|
  a1 = val[0] % val[1]
  if a1 == 0
    print "#{val[1]}\n"
  else
    print "#{a1}\n"
  end
end