import Control.Monad
import Data.List
import qualified Data.ByteString.Char8 as B
main=do
    n<-readLn
    abs<-reverse <$> replicateM n (unfoldr (B.readInt.B.dropWhile(==' '))<$>B.getLine)
    print $ sol 0 abs
sol c [] = c
sol c ([a,b]:abs)
    | (a+c)`mod`b == 0 = sol c abs
    | otherwise = sol (c+ ((a+c+b)`div`b)*b-(a+c)) abs