Under construction

Still being built. Things may change

Babushka and her pierogi

Time limit6sMemory limit1024 MB

Summary
Given current and desired pierogi counts on plates, find the cheapest swap sequence (each swap costs |x-y|+C) that reaches the desired counts.
Level

Medium7 of 10

Topics
Greedy, Sorting, Array
Solved
No attempts yet

Problem

Babushka Bajtmiła is throwing a party! The main dish is her famous pierogi.

There are nn plates at the party. Babushka plans to put exactly pip_i pierogi on the ii-th plate, and all the pip_i values are distinct. She prepared all the pierogi and divided them among the nn plates as (p1,…,pn)(p_1, \ldots, p_n), but then she mixed up the order of the plates.

Bajtmiła is tired and will only perform one kind of operation. She chooses two plates ii and jj and swaps the amounts of pierogi on them. If plate ii holds xx pierogi and plate jj holds yy, then afterwards plate ii holds yy and plate jj holds xx. This operation takes ∣x−y∣+C|x - y| + C seconds: CC seconds to find a proper spoon, plus 1 second for each pierogi moved.

The party starts soon. Bajtmiła will not let you touch anything in the kitchen, but she trusts your algorithm. Find a sequence of operations that restores the desired order in the shortest possible time. Can you help Bajtmiła?

Input

The first line contains the number of test cases zz (1≤z≤10001 \le z \le 1000). Each test case starts with a line containing nn and CC (1≤n≤200 0001 \le n \le 200\,000, 1≤C≤1091 \le C \le 10^9). The next nn lines each contain aia_i and pip_i (1≤ai,pi≤1091 \le a_i, p_i \le 10^9), the current and desired amounts on the ii-th plate.

In each test case, the values aia_i are distinct, and the sets {a1,…,an}\{a_1, \ldots, a_n\} and {p1,…,pn}\{p_1, \ldots, p_n\} are equal. The sum of nn over all test cases does not exceed 1 000 0001\,000\,000.

Output

For each test case, print two integers SS and KK: the total time and the number of operations. Then print KK lines. The kk-th line contains two integers xkx_k and yky_k, meaning the kk-th operation swaps the amounts on plates xkx_k and yky_k. After all operations, plate ii must contain exactly pip_i pierogi.

Hint

The sequence (2,3,1,4)(2,3,1,4) should become (4,2,1,3)(4,2,1,3). First swap plates 1 and 2, which gives (3,2,1,4)(3,2,1,4) at cost ∣3−2∣+2=3|3-2|+2=3. Then swap plates 1 and 4, which gives (4,2,1,3)(4,2,1,3) at cost ∣3−4∣+2=3|3-4|+2=3. The total cost is 66, and this is the minimum.

Examples1

  1. Example 1

    Input
    1
    4 2
    2 4
    3 2
    1 1
    4 3
    
    Expected output
    6 2
    2 1
    4 1