Banquet Preparations 1 solution codeforces

Banquet Preparations 1 solution codeforces

A known chef has prepared nn dishes: the ii-th dish consists of aiai grams of fish and bibi grams of meat.

The banquet organizers estimate the balance of nn dishes as follows. The balance is equal to the absolute value of the difference between the total mass of fish and the total mass of meat.

Technically, the balance equals to ∣∣∣i=1naii=1nbi∣∣∣|∑i=1nai−∑i=1nbi|. The smaller the balance, the better.

In order to improve the balance, a taster was invited. He will eat exactly mm grams of food from each dish. For each dish, the taster determines separately how much fish and how much meat he will eat. The only condition is that he should eat exactly mm grams of each dish in total.

Banquet Preparations 1 solution codeforces

Determine how  much of what type of food the taster should eat from each dish so that the value of the balance is as minimal as possible. If there are several correct answers, you may choose any of them.

Input

The first line of input data contains an integer tt (1t1041≤t≤104) — the number of the test cases.

Each test case’s description is preceded by a blank line. Next comes a line that contains integers nn and mm (1n21051≤n≤2⋅1050m1060≤m≤106). The next nn lines describe dishes, the ii-th of them contains a pair of integers aiai and bibi (0ai,bi1060≤ai,bi≤106) — the masses of fish and meat in the ii-th dish.

It is guaranteed that it is possible to eat mm grams of food from each dish. In other words, mai+bim≤ai+bi for all ii from 11 to nn inclusive.

The sum of all nn values over all test cases in the test does not exceed 21052⋅105.

Output

Banquet Preparations 1 solution codeforces

For each test case, print on the first line the minimal balance value that can be achieved by eating exactly mm grams of food from each dish.

Then print nn lines that describe a way to do this: the ii-th line should contain two integers xixi and yiyi (0xiai0≤xi≤ai0yibi0≤yi≤bixi+yi=mxi+yi=m), where xixi is how many grams of fish taster should eat from the ii-th meal and yiyi is how many grams of meat.

If there are several ways to achieve a minimal balance, find any of them.

Example

Banquet Preparations 1 solution codeforces

input

Copy
8

1 5
3 4

1 6
3 4

2 2
1 3
4 2

2 4
1 3
1 7

3 6
1 7
1 8
1 9

3 6
1 8
1 9
30 10

3 4
3 1
3 2
4 1

5 4
0 7
6 4
0 8
4 1
5 3
output

Banquet Preparations 1 solution codeforces

Copy
0
2 3
1
3 3
0
1 1
1 1
2
1 3
0 4
3
0 6
0 6
0 6
7
1 5
1 5
6 0
0
3 1
3 1
3 1
0
0 4
2 2
0 4
3 1
1 3

Leave a Comment