Jumbo Extra Cheese 2 solution codeforces

Jumbo Extra Cheese 2
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Pak Chanek has 𝑛n two-dimensional slices of cheese. The 𝑖i-th slice of cheese can be represented as a rectangle of dimensions 𝑎𝑖×𝑏𝑖ai×bi. We want to arrange them on the two-dimensional plane such that:

  • Each edge of each cheese is parallel to either the x-axis or the y-axis.
  • The bottom edge of each cheese is a segment of the x-axis.
  • No two slices of cheese overlap, but their sides can touch.
  • They form one connected shape.

Note that we can arrange them in any order (the leftmost slice of cheese is not necessarily the first slice of cheese). Also note that we can rotate each slice of cheese in any way as long as all conditions still hold.

Find the minimum possible perimeter of the constructed shape.

Input

Each test contains multiple test cases. The first line contains an integer 𝑡t (1𝑡21041≤t≤2⋅104) — the number of test cases. The following lines contain the description of each test case.

The first line of each test case contains an integer 𝑛n (1𝑛21051≤n≤2⋅105) — the number of slices of cheese Pak Chanek has.

The 𝑖i-th of the next 𝑛n lines of each test case contains two integers 𝑎𝑖ai and 𝑏𝑖bi (1𝑎𝑖,𝑏𝑖1091≤ai,bi≤109) — the dimensions of the 𝑖i-th slice of cheese.

It is guaranteed that the sum of 𝑛n over all test cases does not exceed 21052⋅105.

Output

For each test case, output a line containing an integer representing the minimum possible perimeter of the constructed shape.

Example
input

Copy
3
4
4 1
4 5
1 1
2 3
3
2 4
2 6
2 3
1
2 65
output

Copy
26
24
134
Note

In the first test case, a way of getting the minimum possible perimeter is to arrange the slices of cheese as follows.

We can calculate that the perimeter of the constructed shape is 2+5+1+1+1+1+3+1+5+1+2+3=262+5+1+1+1+1+3+1+5+1+2+3=26. It can be shown that we cannot get a smaller perimeter.

Consider the following invalid arrangement.

Even though the perimeter of the shape above is 2424, it does not satisfy all conditions of the problem. The bottom edge of the 1×11×1 slice of cheese is not a segment of the x-axis.

In the second test case, a way of getting the minimum possible perimeter is to arrange the slices of cheese as follows.

We can calculate that the perimeter of the constructed shape is 2+2+2+3+2+3+2+2+2+4=242+2+2+3+2+3+2+2+2+4=24. It can be shown that we cannot get a smaller perimeter.

Leave a Comment