Problem:
Score : 300 points

Problem Statement

A railroad running from west to east in Atcoder Kingdom is now complete.

There are N stations on the railroad, numbered 1 through N from west to east.

Tomorrow, the opening ceremony of the railroad will take place.

On this railroad, for each integer i such that 1≤i≤N-1, there will be trains that run from Station i to Station i+1 in C_i seconds. No other trains will be operated.

The first train from Station i to Station i+1 will depart Station i S_i seconds after the ceremony begins. Thereafter, there will be a train that departs Station i every F_i seconds.

Here, it is guaranteed that F_i divides S_i.

That is, for each Time t satisfying S_i≤t and t％F_i=0, there will be a train that departs Station i t seconds after the ceremony begins and arrives at Station i+1 t+C_i seconds after the ceremony begins, where A％B denotes A modulo B, and there will be no other trains.

For each i, find the earliest possible time we can reach Station N if we are at Station i when the ceremony begins, ignoring the time needed to change trains.

Constraints

1≤N≤500

1≤C_i≤100

1≤S_i≤10^5

1≤F_i≤10

S_i％F_i=0

All input values are integers.

Input

Input is given from Standard Input in the following format:

N
C_1 S_1 F_1
:
C_{N-1} S_{N-1} F_{N-1}

Output

Print N lines. Assuming that we are at Station i (1≤i≤N) when the ceremony begins, if the earliest possible time we can reach Station N is x seconds after the ceremony begins, the i-th line should contain x.

Sample Input 1

3
6 5 1
1 10 1

Sample Output 1

12
11
0

We will travel from Station 1 as follows:

5 seconds after the beginning: take the train to Station 2.

11 seconds: arrive at Station 2.

11 seconds: take the train to Station 3.

12 seconds: arrive at Station 3.

We will travel from Station 2 as follows:

10 seconds: take the train to Station 3.

11 seconds: arrive at Station 3.

Note that we should print 0 for Station 3.

Sample Input 2

4
12 24 6
52 16 4
99 2 2

Sample Output 2

187
167
101
0

Sample Input 3

4
12 13 1
44 17 17
66 4096 64

Sample Output 3

4162
4162
4162
0

Solution in Scala (2.11.7) (judge verdict: Accepted)

import java.util.Scanner

import scala.annotation.tailrec

object Main {

  /**
    * 駅の情報
    *
    * @param timeToNext 次の駅までの所要時間
    * @param firstTime  開通式開始から発車するまでの時間
    * @param frequency  発車間隔
    */
  case class Station(timeToNext: Int, firstTime: Int, frequency: Int)

  implicit class EnrichInt(i: Int) {
    def isDivisibleBy(a: Int): Boolean = i % a == 0
  }

  def read() = {
    val sc = new Scanner(System.in)
    val n = sc.nextInt()
    val sts = for (_ <- 0 until n - 1) yield Station(sc.nextInt(), sc.nextInt(), sc.nextInt())
    sts.toList
  }

  def nextDepartureTime(time: Int, st: Station): Int = {
    if (time < st.firstTime) {
      // まだ開通式開始から発車されていない。
      return st.firstTime
    }
    // 最初の電車が発車してからの時間
    val timeLengthSinceFirst = time - st.firstTime

    if (timeLengthSinceFirst isDivisibleBy st.frequency) {
      // すぐ発車
      return time
    }

    val timeLengthSincePrevDeparture = timeLengthSinceFirst % st.frequency
    val waitTimeLength = st.frequency - timeLengthSincePrevDeparture

    time + waitTimeLength
  }

  /**
    * @param time     現在の時刻
    * @param stations 駅の列
    * @return 最後の駅に着く時刻
    */
  @tailrec
  def timeToTerminal(time: Int, stations: List[Station]): Int = {
    stations match {
      case Nil => time
      case (currentStation :: nextStations) =>
        val departureTime = nextDepartureTime(time, currentStation)
        timeToTerminal(departureTime + currentStation.timeToNext, nextStations)
    }
  }

  /**
    * @param sts 駅の列
    * @return 最後の駅に着くまでの時間
    */
  def timeLengthToTerminal(sts: List[Station]): Int = {
    // 時刻を0とする。
    timeToTerminal(0, sts)
  }

  def main(args: Array[String]): Unit = {
    val stations = read()
    stations.tails.map(timeLengthToTerminal).foreach(println)
  }
}