CF BUDDY
← Problems·

1607H · Banquet Preparations 2

2200 · greedy, sortings, two pointers

Problem: The chef has cooked nn dishes yet again: the ii-th dish consists of aia_i grams of fish and bib_i grams of meat.

Banquet organizers consider two dishes ii and jj equal if ai=aja_i=a_j and bi=bjb_i=b_j at the same time.

The banquet organizers estimate the variety of nn dishes as follows. The variety of a set of dishes is equal to the number of different dishes in it. The less variety is, the better.

In order to reduce the variety, a taster was invited. He will eat exactly mim_i 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 will eat exactly mim_i grams of the ii-th dish in total.

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

Input Format: The first line of input data contains an integer tt (1t1041 \leq t \leq 10^4) — the number of test cases.

Each test case's description is preceded by a blank line. Next comes a line that contains an integer nn (1n21051 \leq n \leq 2 \cdot 10^5) — the number of dishes. Then follows nn lines, ii-th of which contains three integers aia_i, bib_i and mim_i (0ai,bi1060 \leq a_i, b_i \le 10^6; 0miai+bi0 \le m_i \le a_i+b_i) — the mass of fish in ii-th dish, the mass of meat in ii-th dish and how many grams in total the taster should eat in ii-th dish.

The sum of all nn values for all input data sets in the test does not exceed 21052 \cdot 10^5.

Output Format: For each test case, print on the first line the minimum value of variety that can be achieved by eating exactly mim_i grams of food (for all ii from 11 to nn) from a dish ii.

Then print nn lines that describe a way to do this: the ii-th line should contain two integers xix_i and yiy_i (0xiai0 \leq x_i \leq a_i; 0yibi0 \leq y_i \leq b_i; xi+yi=mix_i+y_i=m_i), where xix_i is how many grams of fish the taster should eat from ii-th dish, and yiy_i is how many grams of meat.

If there are several ways to achieve a minimum balance, print any of them.

Sample Cases

Case 1

Input

5

3
10 10 2
9 9 0
10 9 1

2
3 4 1
5 1 2

3
7 2 5
6 5 4
5 5 6

1
13 42 50

5
5 7 12
3 1 4
7 3 7
0 0 0
4 1 5

Output

1
1 1
0 0
1 0
2
0 1
1 1
2
3 2
0 4
1 5
1
8 42
2
5 7
3 1
4 3
0 0
4 1

Similar problems

00:00:00
Loading editor…
Welcome! I'm your coding tutor for this problem. Use the chips below to reveal stored hints or get AI feedback on your code. I'll guide you step by step — never giving away the solution.

Sign in to unlock AI tutor feedback