#P9070. [CTS2023] 琪露诺的符卡交换
[CTS2023] 琪露诺的符卡交换
Problem Description
Affected by the incident, Cirno found that cards sealing her abilities were circulating in Gensokyo.
After investigating, Cirno found that there are a total of different types of cards, and the total number of each type is exactly . There are people who bought these cards. Each person bought exactly cards, and they may buy cards of the same type.
However, Cirno wants everyone to hold exactly types of cards, so she gathers these people together and tries to achieve her goal through card exchanges.
Each time, Cirno chooses one card from each of two people and swaps them, until everyone holds exactly types of cards.
Since each swap reduces the magic on the cards, Cirno wants each card to be swapped at most once.
But she does not know how to swap them, so she asks for your help.
You need to tell her the swapping process, or tell her that no such solution exists.
Input Format
The first line contains a positive integer , the number of test cases.
For each test case, the first line contains a positive integer , as described in the statement.
The next lines each contain positive integers. The -th integer in the -th line represents the type of the -th card held by the -th person.
Output Format
For each test case, if there is no solution that makes everyone hold types of cards, output one line containing .
Otherwise, first output one line containing a positive integer , the number of swaps.
Then output lines. Each line contains four positive integers , meaning that the -th card of person is swapped with the -th card of person .
Note that you must ensure that no card is swapped twice, and after all swaps, each person holds exactly types of cards.
2
3
1 2 2
2 3 3
3 1 1
3
1 2 3
2 3 1
3 2 1
2
1 3 3 1
2 3 3 2
0
Hint
Sample Explanation.
In the first test case, in the first swap we swap the third card of the first person with the first card of the third person.
In the second swap we swap the third card of the second person with the second card of the third person.
With a total of two swaps, everyone can end up holding three types of cards.
Other valid solutions are also accepted.
In the second test case, since everyone already holds three types of cards at the beginning, no swaps are needed, so output a single line .
Constraints.
Subtask ( points): Each person holds only one type of card.
Subtask ( points): Each person holds at least cards of the same type.
Subtask ( points): No special constraints.
For all testdata, .
Translated by ChatGPT 5