Scanf.scanf "%d" (fun n ->
    let ab = Array.init n (fun _ -> Scanf.scanf " %d %d" (fun a b -> a, b)) in

    let rec loop i acc =
        if i < 0 then acc else
            let a, b = ab.(i) in
            let c = (a + acc + b - 1) / b * b - a - acc in
            loop (i - 1) (acc + c)
    in
    loop (n - 1) 0 |> Printf.printf "%d\n"
)