#P1872B. The Corridor or There and Back Again
The Corridor or There and Back Again
No submission language available for this problem.
Description
You are in a corridor that extends infinitely to the right, divided into square rooms. You start in room , proceed to room , and then return to room . You can choose the value of . Moving to an adjacent room takes second.
Additionally, there are traps in the corridor: the -th trap is located in room and will be activated seconds after you enter the room . Once a trap is activated, you cannot enter or exit a room with that trap.

Determine the maximum value of that allows you to travel from room to room and then return to room safely.
For instance, if and , you can proceed to room and return safely (the trap will activate at the moment , it can't prevent you to return back). But if you attempt to reach room , the trap will activate at the moment , preventing your return (you would attempt to enter room on your way back at second , but the activated trap would block you). Any larger value for is also not feasible. Thus, the answer is .
The first line of the input contains an integer () — the number of test cases.
The descriptions of the test cases follow.
The first line of each test case description contains an integer () — the number of traps.
The following lines of each test case description present two integers and () — the parameters of a trap (you must leave room strictly before seconds have passed since entering this room). It's possible for multiple traps to occupy a single room (the values of can be repeated).
For each test case, print the maximum value of that allows you to travel to room and return to room without encountering an active trap.
Input
The first line of the input contains an integer () — the number of test cases.
The descriptions of the test cases follow.
The first line of each test case description contains an integer () — the number of traps.
The following lines of each test case description present two integers and () — the parameters of a trap (you must leave room strictly before seconds have passed since entering this room). It's possible for multiple traps to occupy a single room (the values of can be repeated).
Output
For each test case, print the maximum value of that allows you to travel to room and return to room without encountering an active trap.
Note
The first test case is explained in the problem statement above.
In the second test case, the second trap prevents you from achieving . If , the second trap will activate at the moment (the time you enter room plus ). In the case of , you will return to room at time or later. The trap will be active at that time. It can be shown that room can be reached without encountering an active trap.
In the third test case, you can make it to room and then immediately return to room .