import           Control.Monad

main :: IO ()
main = do
    n <- readLn
    abs <- replicateM n (map read . words <$> getLine)
    print $ solve abs

solve :: [[Int]] -> Int
solve = foldr f 0
    where 
        f [a, b] n = if (a + n) `mod` b == 0
            then n
            else n + b - ((a + n) `mod` b)
