import Data.List

f::Int->Int->Int->Int->Int
f x c s fi
    |x<=s = (s-x)+c
    |(s-x) `mod` fi==0 = c
    |otherwise=(fi-((x-s) `mod` fi))+c

f3::[String]->[(Int,Int,Int)]
f3 []=[]
f3 (x:xs)=let (a:b:c:[])=map read (words x)::[Int]
          in (a,b,c):f3 xs

f2::Int->[(Int,Int,Int)]->Int
f2 ans []=ans
f2 ans ((ci,si,fi):xs) = f2 (ans+(f ans ci si fi)) xs

f4 :: [(Int,Int,Int)]->[Int]
f4 []=[0]
f4 (x:xs)=f2 0 (x:xs) : f4 xs

main = do
         e<-getLine
         es<-getContents
         mapM_ putStrLn $ map show $ f4 $ f3 (lines es)