1 second

256 megabytes

standard input

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.

Each test contains multiple test cases. The first line contains an integerÂ đť‘ˇtÂ (1â‰¤đť‘ˇâ‰¤2â‹…1041â‰¤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â‰¤đť‘›â‰¤2â‹…1051â‰¤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Â 2â‹…1052â‹…105.

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

26 24 134

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.