#C6040. Minimal Time to Pass Traffic Lights

    ID: 49757 Type: Default 1000ms 256MiB

Minimal Time to Pass Traffic Lights

Minimal Time to Pass Traffic Lights

You are given a number of test scenarios where a car tries to pass through a set of two parallel traffic lights. Each scenario is described by a 3-character string. The first two characters indicate the state of the traffic lights on lane 1 and lane 2 respectively, where 'G' denotes a green light and 'R' denotes a red light. The third character (either '1' or '2') denotes the lane from which the car starts. The car can proceed immediately if either the light in its starting lane or the light in the opposite lane is green. However, if both lights are red, it must wait for a duration of g seconds. Your task is to compute the minimal waiting time for each scenario.

In mathematical notation, if we let (L_1) and (L_2) denote the state of lane 1 and lane 2 respectively (with (L_i = G) or (R)), and let (s) be the starting lane (1 or 2), then the answer for that scenario is given by:

[ \text{result} = \begin{cases} 0, & \text{if } L_s = G \text{ or } L_{3-s} = G,\ g, & \text{if } L_1 = R \text{ and } L_2 = R. \end{cases} ]

Make sure to read the input from standard input and output the solution to standard output.

inputFormat

The first line contains two integers, n and g, where n is the number of test scenarios and g is the waiting time if both lights are red. This is followed by n lines, each containing a 3-character string that describes a scenario.

outputFormat

Output n lines, each containing a single integer that represents the minimal waiting time for the corresponding scenario.## sample

3 5
RG1
GG2
RR1
0

0 5

</p>