• # For Solution

There are nn points and mm segments on the coordinate line. The initial coordinate of the ii-th point is aiai. The endpoints of the jj-th segment are ljlj and rjrj — left and right endpoints, respectively.

You can move the points. In one move you can move any point from its current coordinate xx to the coordinate x1x−1 or the coordinate x+1x+1. The cost of this move is 11.

You should move the points in such a way that each segment is visited by at least one point. A point visits the segment [l,r][l,r] if there is a moment when its coordinate was on the segment [l,r][l,r] (including endpoints).

You should find the minimal possible total cost of all moves such that all segments are visited.

Points Movement solution codeforces

The input consists of multiple test cases. The first line contains a single integer tt (1t1041≤t≤104) — the number of test cases. Description of the test cases follows.

The first line of each test case contains two integers nn and mm (1n,m21051≤n,m≤2⋅105) — the number of points and segments respectively.

The next line contains nn distinct integers a1,a2,,ana1,a2,…,an (109ai109−109≤ai≤109) — the initial coordinates of the points.

Each of the next mm lines contains two integers ljljrjrj (109ljrj109−109≤lj≤rj≤109) — the left and the right endpoints of the jj-th segment.

It’s guaranteed that the sum of nn and the sum of mm over all test cases does not exceed 21052⋅105.

Points Movement solution codeforces

For each test case print a single integer — the minimal total cost of all moves such that all segments are visited.

Example
input

Copy
2
4 11
2 6 14 18
0 3
4 5
11 15
3 5
10 13
16 16
1 4
8 12
17 19
7 13
14 19
4 12
-9 -16 12 3
-20 -18
-14 -13
-10 -7
-3 -1
0 4
6 11
7 9
8 10
13 15
14 18
16 17
18 19

Points Movement solution codeforces

Copy
5
22

Points Movement solution codeforces

In the first test case the points can be moved as follows:

• Move the second point from the coordinate 66 to the coordinate 55.
• Move the third point from the coordinate 1414 to the coordinate 1313.
• Move the fourth point from the coordinate 1818 to the coordinate 1717.
• Move the third point from the coordinate 1313 to the coordinate 1212.
• Move the fourth point from the coordinate 1717 to the coordinate 1616.

The total cost of moves is 55. It is easy to see, that all segments are visited by these movements. For example, the tenth segment ([7,13][7,13]) is visited after the second move by the third point.

Here is the image that describes the first test case: 