Babushka and her pierogi
Time limit6sMemory limit1024 MB
Given current and desired pierogi counts on plates, find the cheapest swap sequence (each swap costs |x-y|+C) that reaches the desired counts.
Problem
Babushka Bajtmiła is throwing a party! The main dish is her famous pierogi.
There are plates at the party. Babushka plans to put exactly pierogi on the -th plate, and all the values are distinct. She prepared all the pierogi and divided them among the plates as , but then she mixed up the order of the plates.
Bajtmiła is tired and will only perform one kind of operation. She chooses two plates and and swaps the amounts of pierogi on them. If plate holds pierogi and plate holds , then afterwards plate holds and plate holds . This operation takes seconds: seconds to find a proper spoon, plus 1 second for each pierogi moved.
The party starts soon. Bajtmiła will not let you touch anything in the kitchen, but she trusts your algorithm. Find a sequence of operations that restores the desired order in the shortest possible time. Can you help Bajtmiła?
Input
The first line contains the number of test cases (). Each test case starts with a line containing and (, ). The next lines each contain and (), the current and desired amounts on the -th plate.
In each test case, the values are distinct, and the sets and are equal. The sum of over all test cases does not exceed .
Output
For each test case, print two integers and : the total time and the number of operations. Then print lines. The -th line contains two integers and , meaning the -th operation swaps the amounts on plates and . After all operations, plate must contain exactly pierogi.
Hint
The sequence should become . First swap plates 1 and 2, which gives at cost . Then swap plates 1 and 4, which gives at cost . The total cost is , and this is the minimum.