main = do
  n <- readLn :: IO Int
  dat <- getContents >>= return . (map ((map read) . words)) . lines :: IO [[Int]]


  mapM_ print [func (drop i dat) 0 | i<-[0..n-1]]


  where
    func [] cr = cr
    func ((c:s:f:_):xs) cr
      | cr < s = func xs (s+c)
      | otherwise = func xs (((div cr f)*f) + c)
