let () =
  let n = Scanf.scanf " %d\n" (fun x -> x) in
  let rec read = function
      0 -> ()
    | i ->
       let (x,y) = Scanf.scanf " %d %d\n" (fun x y-> (x,y)) in
       let r = x mod y in
       Printf.printf "%d\n" (if r = 0 then y else r)
       ;read (i-1)
  in read n
;;