Airport
Time limit1sMemory limit128 MB
The tower serves one of two waiting lines at each time to minimize the worst waiting position any aircraft receives.
- Level
Medium7 of 10
- Topics
- Binary search, Greedy, Queue
- Solved
- No attempts yet
Problem

A large city runs an international airport that handles 40 million passengers a year, yet it is infamous as one of the most congested airports in the world. As the figure shows, the airport has a single runway, so the runway is always crowded with aircraft waiting to take off. The runway can be reached from two directions, the west road and the east road , and aircraft line up on these two roads to wait for takeoff.
At each time , an arbitrary number of aircraft arrive on roads and . When an aircraft arrives on a road at time , it is given a rank equal to the number of aircraft already waiting ahead of it on the same road. After all arrivals at time , the control tower picks one of the two roads, and the aircraft at the front of that road takes off and leaves the runway. Given the arrivals at every time, we want the tower's takeoff schedule that minimizes the maximum rank assigned to any aircraft.

For example, the table above lists the aircraft arriving on roads and at each time. At time 1, aircraft , , receive ranks 0, 1, 2, and aircraft , receive ranks 0, 1. The tower then lets on road take off, and leaves. At time 2, aircraft , , receive ranks 1, 2, 3, and then on road takes off and leaves. At time 3, aircraft , receive ranks 2, 3. So the maximum rank over all aircraft is 3, and this is the smallest possible maximum rank over every takeoff schedule.
Input
The input is read from standard input. The first line contains the number of test cases . Each test case is given as follows. The first line of a test case contains an integer (), the number of times. Each of the next lines contains two integers and (), the number of aircraft arriving on road and on road , respectively, at time .
Output
For each test case, print one line containing the minimum, over all takeoff schedules, of the maximum rank of the aircraft.