Under construction

Still being built. Things may change

Airport

Time limit1sMemory limit128 MB

Summary
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 WW and the east road EE, and aircraft line up on these two roads to wait for takeoff.

At each time tt, an arbitrary number of aircraft arrive on roads WW and EE. When an aircraft arrives on a road at time tt, 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 tt, 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 WW and EE at each time. At time 1, aircraft A1A_1, A2A_2, A3A_3 receive ranks 0, 1, 2, and aircraft B1B_1, B2B_2 receive ranks 0, 1. The tower then lets B1B_1 on road EE take off, and B1B_1 leaves. At time 2, aircraft B3B_3, B4B_4, B5B_5 receive ranks 1, 2, 3, and then A1A_1 on road WW takes off and leaves. At time 3, aircraft A4A_4, A5A_5 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 TT. Each test case is given as follows. The first line of a test case contains an integer nn (1≤n≤50001 \le n \le 5000), the number of times. Each of the next nn lines contains two integers aia_i and bib_i (0≤ai,bi≤200 \le a_i, b_i \le 20), the number of aircraft arriving on road WW and on road EE, respectively, at time ii.

Output

For each test case, print one line containing the minimum, over all takeoff schedules, of the maximum rank of the aircraft.

Examples1

  1. Example 1

    Input
    3
    1
    1 1
    3
    3 2
    0 3
    2 0
    6
    0 1
    1 1
    1 2
    1 1
    1 1
    6 0
    
    Expected output
    0
    3
    5