There are posts in a line numbered from to from left to right. Each post has a distance which describes how far it is from the base of operation. The more left you go, the closer you get to the base. More formally, .
Each post has a different number of soldiers. Post has a troop of soldiers. Now it is time for the drill and you need to shuffle the positions of the troops. You want the troop to go the position for all .
To keep the discipline, you will give the following commands one at a time. Choose two distinct posts, and where , and swap the troops. All soldiers currently stationed at post will go to post and all soldiers currently stationed at post will go to post . When moving from post to , one soldier needs to walk unit of distance.
You can give as many commands as you want until each troop is at its desired position but you want to do it such that the total distance crossed by all soldiers is as minimum as possible. You also want to minimize the total number of commands after minimizing total distance crossed.
The first line of the input will contain an integer, which denotes the number of test cases.
Each of the test cases starts with one integer describing the number of posts.
The next line will contain space separated integers where denotes the distance of post from base. .
The next line will contain more space separated integers . Troop currently stationed at post should be stationed at post after all the commands are given. , so is a permutation of length .
It is guaranteed that the sum of over all test cases does not exceed .
For each test case output two integers and in the first line, where is the minimum distance crossed by all soldiers and is the minimum number of commands needed to do that. Then output more lines, each of them will contain two integers which denotes a command. NOTE: Troop currently stationed at post must be stationed at post after all commands are applied in chronological order. First you need to minimize , then minimize . If there are multiple sets of commands of size so that the total distance crossed remains , print any of them.
Input | Output |
---|---|
2 3 1 10 20 2 3 1 4 1 2 3 4 3 4 1 2 | 86 2 3 2 2 1 20 2 3 1 4 2 |