#C4233. Maximum Simultaneous Green Lanes

    ID: 47749 Type: Default 1000ms 256MiB

Maximum Simultaneous Green Lanes

Maximum Simultaneous Green Lanes

You are given a set of test cases describing traffic lanes. Each lane has a green light duration and a red light duration. The traffic light on each lane cycles with its given green duration G and red duration R, starting with the green phase at time 1. At each time unit t (with 1 ≤ t ≤ 100), a lane's light is green if and only if the remainder when t is divided by (G+R) is between 1 and G (inclusive). If the remainder is 0 or greater than G, the lane is red.

Your task is to determine, for each test case, the maximum number of lanes that can have a green light simultaneously over the time period from t = 1 to 100. However, to ensure safety, it is required that at least one lane must always have a red light at any moment, so if the maximum possible number of green lights equals the total number of lanes, you must subtract one from this number.

Input Format: Multiple test cases are provided; the first line contains an integer T representing the number of test cases. Each test case begins with an integer N, the number of lanes, followed by N lines each containing two integers G and R.

Output Format: For each test case, output a single integer on a new line indicating the maximum number of lanes that can simultaneously be green (adjusted so that at least one lane is red).

inputFormat

The input is read from standard input (stdin) and follows this format:

T
N
G₁ R₁
G₂ R₂
... 
Gₙ Rₙ
[Repeat the above for each of the T test cases]

Where:

  • T is the number of test cases.
  • N is the number of lanes for a test case.
  • Each of the following N lines contains two positive integers G (green duration) and R (red duration).

outputFormat

For each test case, output a single integer on a new line representing the maximum number of lanes that can show a green light simultaneously, ensuring that at least one lane is red.

## sample
2
4
5 3
3 2
2 4
4 1
3
6 5
5 5
4 6
3

2

</p>