The problem could be reduced to sorting the array by some trivial position tricks.
Then you can use the "inversion count" to find the swaps required. This can be done in O(nlogn) time as done here : http://www.geeksforgeeks.org/archives/3968

EDIT:
Considering 1 of the test cases in the problem:
5
3 4 5 2 1
4 1 5 2 3
Let us give ranks to the numbers according to the second sequence
rank(4)=1,rank(1)=2,rank(5)=3,rank(2)=4,rank(3)=5
Now in the original sequence, we replace the numbers by their ranks,
5 1 3 4 2
Now we need to essentially sort this new permutation. We can apply the inversion count method on this new sequence.

Well, I did the same and it was giving correct answer for all the testcases that were given and even I checked for upto 20 numbers and it was giving me the correct answer but my submission showed the incorrect answer and I dont know why it is showing incorrect.Here is my code: