#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 nn different types of cards, and the total number of each type is exactly nn. There are nn people who bought these cards. Each person bought exactly nn cards, and they may buy cards of the same type.

However, Cirno wants everyone to hold exactly nn types of cards, so she gathers these nn 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 nn 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 TT, the number of test cases.

For each test case, the first line contains a positive integer nn, as described in the statement.

The next nn lines each contain nn positive integers. The jj-th integer in the ii-th line represents the type of the jj-th card held by the ii-th person.

Output Format

For each test case, if there is no solution that makes everyone hold nn types of cards, output one line containing 1-1.

Otherwise, first output one line containing a positive integer mm, the number of swaps.

Then output mm lines. Each line contains four positive integers a,b,c,da,b,c,d, meaning that the bb-th card of person aa is swapped with the dd-th card of person cc.

Note that you must ensure that no card is swapped twice, and after all swaps, each person holds exactly nn 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 00.

Constraints.

Subtask 11 (2020 points): Each person holds only one type of card.

Subtask 22 (2020 points): Each person holds at least n1n-1 cards of the same type.

Subtask 33 (6060 points): No special constraints.

For all testdata, i=1Tni200\sum\limits_{i=1}^{T}n_{i} \leq 200.

Translated by ChatGPT 5