Time limit: 2 seconds Memory limit: 256 MB The professor, who has just returned from…
- User
Time limit: 2 seconds Memory limit: 256 MB The professor, who has just returned from Farang's trip, has not yet gotten used to the Persian language. Therefore, on the occasion of his return, he has organized a ceremony.
teacher n He invited n people to his ceremony and identified them with numbers 1 1 to n n is numbered. Any of these n n person has one left shoe and one right shoe.
The shoe rack of the master's house includes a large bar 1 0 9 10 9 The house is with numbers 1 1 to 1 0 9 10 9 are numbered. We know that guests are first i i-m his left shoe at home L i L i Shoe rack and your right shoes at home R i R i it has placed It is guaranteed that no two shoes are in the same house.
Master's friend, who doesn't want guests to get into trouble when leaving the party, likes each guest's left shoe to be in the house to the (immediate) left of their right shoe. In other words, he likes that if in the final position, the right shoe is in the house x x is the left shoe in the house x
1 be x−1.
To reach this situation, in each operation, he can choose two houses adjacent to the shoe rack and move the contents of them together.
Help your friend and print the minimum number of operations to bring the shoe rack to the desired state.
input In the first line of the integer input t t ( 1 ≤ t ≤ 1 0 5 1≤t≤10 5 ) which is equal to the number of scenarios.
In the first line of each scenario, the integer n n ( 1 ≤ n ≤ 5 x 1 0 5 1≤n≤5×10 5 ) which indicates the number of ceremony guests.
in each of n n the next line of the scenario, two numbers L i L i and R i R i ( 1 ≤ L i , R i ≤ 1 0 9 , 1≤L i , R i ≤10 9 ,) which respectively indicate the placement positions of the guest's left and right shoes i They are coming.
It is guaranteed that the total n n's in all scenarios is max 5 x 1 0 5 5 x 10 5 is
output For each scenario, print the minimum number of operations to reach the desired state.
Example Sample entry 1 3 3 1 6 5 2 3 4 2 19 2 10 1 1000000000 1 Plain text Sample output 1 7 13 999999999 write in java
- Assistant