CF1895B.Points and Minimum Distance

传统题 时间 2000 ms 内存 256 MiB 3 尝试 1 已通过 1 标签

Points and Minimum Distance

You are given a sequence of integers aa of length 2n2n. You have to split these 2n2n integers into nn pairs; each pair will represent the coordinates of a point on a plane. Each number from the sequence aa should become the xx or yy coordinate of exactly one point. Note that some points can be equal.

After the points are formed, you have to choose a path ss that starts from one of these points, ends at one of these points, and visits all nn points at least once.

The length of path ss is the sum of distances between all adjacent points on the path. In this problem, the distance between two points (x1,y1)(x_1, y_1) and (x2,y2)(x_2, y_2) is defined as x1x2+y1y2|x_1-x_2| + |y_1-y_2|.

Your task is to form nn points and choose a path ss in such a way that the length of path ss is minimized.

Input

The first line contains a single integer tt (1t1001 \le t \le 100) — the number of testcases.

The first line of each testcase contains a single integer nn (2n1002 \le n \le 100) — the number of points to be formed.

The next line contains 2n2n integers a1,a2,,a2na_1, a_2, \dots, a_{2n} (0ai10000 \le a_i \le 1\,000) — the description of the sequence aa.

Output

For each testcase, print the minimum possible length of path ss in the first line.

In the ii-th of the following nn lines, print two integers xix_i and yiy_i — the coordinates of the point that needs to be visited at the ii-th position.

If there are multiple answers, print any of them.

Note

In the first testcase, for instance, you can form points (10,1)(10, 1) and (15,5)(15, 5) and start the path ss from the first point and end it at the second point. Then the length of the path will be 1015+15=5+4=9|10 - 15| + |1 - 5| = 5 + 4 = 9.

In the second testcase, you can form points (20,20)(20, 20), (10,30)(10, 30), and (10,30)(10, 30), and visit them in that exact order. Then the length of the path will be $|20 - 10| + |20 - 30| + |10 - 10| + |30 - 30| = 10 + 10 + 0 + 0 = 20$.

Samples

2
2
15 1 10 5
3
10 30 20 20 30 10
9
10 1
15 5
20
20 20
10 30
10 30

在线编程 IDE

建议全屏模式获得最佳体验