ADV04E - Prisoner of Benda

no tags 

Daniel enjoys watching TV series. One of his favourite is Futurama. One episode has the following plot.

Professor Farnsworth switches bodies with Amy using a new invention so that he can relive his youth. Likewise, Amy is reminiscent of her younger days of constantly eating and wishes to use the Professor's skinny body to gorge herself with food once again. Later, they find they cannot switch their bodies back with each other, because the device will not operate on the same pairing of bodies. The Professor thoughtlessly suggests they might be able to switch back to their original bodies with a third person. Bender switches bodies with the Professor (Amy's body) so he can perform a robbery without being identified. After realizing his mistake, the Professor, now in Bender's body, tires of trying to solve the problem. He decides to live a life of daring stunts and joins a robot circus. Bender, now in Amy's body, is caught aboard the yacht of Robo-Hungarian emperor Nikolai. When Bender states that he is really a robot who has switched bodies with a human, Nikolai reveals that he feels trapped by his wealth and wishes to live the life of a normal, "peasant" robot. Bender tricks him into switching bodies with a robot wash bucket and inhabits Nikolai's body, planning to live like an emperor. However, he discovers that Nikolai's fiancée and first officer are plotting to kill him. Bender is saved with the assistance of the Professor and the circus' loyal Robo-Hungarian citizens. Meanwhile, Leela switches bodies with Amy, thus inhabiting the Professor's body, when she comes to believe that Fry only loves her for her beauty. In order to beat Leela at her own game, Fry switches bodies with Dr. Zoidberg in an attempt to repulse Leela. This leads them to one-up each other using various disgusting acts while on a date, which climaxes when the two have sex with each other in their equally grotesque bodies, and reconcile. During this time, Amy has overeaten in Leela's body, making it overweight. She switches bodies with Hermes, so she can continue eating while Hermes slims Leela's body back down. While eating, she witnesses Fry and Leela making out in the Professor and Dr. Zoidberg's bodies and loses her appetite for food. Concurrently, Zoidberg and Nikolai, in the respective bodies of Fry and the robot wash bucket, become friends and attempt to assume the lives of Fry and Bender, blowing up their apartment in the process. The bucket, now in Amy's body, professes its love to Scruffy the Janitor, but he turns it away. Finally, two Globetrotters, Ethan "Bubblegum" Tate and "Sweet" Clyde Dixon, mathematically prove that everyone's minds can be restored using two additional bodies and then successfully do so, with themselves as the extras.

We need to replicate how they have accomplished this. We will consider that a certain amount of body switches already took place. We need to determine the sequence of switches after which everyone is in self body, using no more than two extra bodies. Don't forget that two specific bodies can be mind switched only once.

Input

The first line of input contains t - the number of test cases. The description of tests follows. The first line of each test is numbers n - the number of characters and m - the number of body switches already taken place. Next m lines contain the description of switches. Let us mark all bodies with numbers from 1 to n. Then each switch is defined by two numbers a, b - the numbers of bodies used in the switch. The switches are listed chronologically.

Constraints

1 <= t <= 50
2 <= n <= 200
1 <= m <= n(n-1)/2
1 <= a, b <= n

Output

Print the number of switches needed to return everyone to their own bodies in the first line of the output. Then you should print the switches themselves in the order they should be performed. The format should be the same as in the input data. You can output any valid solution. However you shouldn't use more than two extra bodies. The extra bodies should be marked as n+1 and n+2. After all the switches the extra characters should be in their own bodies as well. Also you can't use more than 3n swtiches.

Example

Input:
1
2 1
1 2

Output:
5
1 3
2 4
2 3
1 4
3 4

hide comments
Spooky: 2010-11-29 12:52:48

try this one as well - https://www.spoj.pl/problems/BENDA/

ymGXX: 2010-11-29 07:56:00

nice problem.

:D: 2010-11-23 00:51:56

Take note that the input and output swaps are describing BODIES and NOT CHARACTERS (you're not swapping indexes, but vaules). I know it's explained, but it's still easy mix up.

P.S.
Shouldn't there be spoiler tags in the main description? The whole episode is described and it was a rather good one :)

Last edit: 2010-11-23 00:54:19

Added by:Spooky
Date:2010-11-14
Time limit:1s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ASM64
Resource:Advancement Autumn 2010, http://sevolymp.uuuq.com/