import Data.List
import qualified Data.ByteString.Char8 as B
import Control.Monad 

main :: IO ()
main = readLn >>= main'
    where
        main' n = replicateM n breads >>= mapM_ print . solve

solve :: [[Int]] -> [Int]
solve ([c, s, f] : xs) = calc (s + c) xs : solve xs
solve _ = [0]

calc :: Int -> [[Int]] -> Int
calc n ([c, s, f] : xs) = calc (f * (max sr nr) + c) xs
    where
        sr = s `div` f
        (r, m) = n `divMod` f
        nr
            | m > 0 = r + 1
            | otherwise = r
calc n _ = n

breads :: IO [Int]
breads = unfoldr uff <$> B.getLine
    where
        uff b = check <$> B.readInt b
        check (a, b)
            | B.null b = (a, b)
            | otherwise = (a, B.tail b)