As it would be way too easy to have to solve just problems in a contest, the scientific committee decided to give you a permutation too. But because we live in the present, we want, from a moral standpoint, to give it further, in a modified version, so that it respects certain properties.
Task
You are given a permutation. Make as few swaps of adjacent values as possible, in such a way that for the final permutation, for at least indexes between and .
Input data
The first line of the input will contain , the number of test cases. Each test case contains on the first line the order of the permutation, and on the second line the permutation, formed of distinct values between and .
Output data
The output will contain lines, on each line you will print the minimal answer for the corresponding test case.
Constraints and clarifications
- For points,
- For more points,
- For more points, one of the permutations for which we can obtain the minimum number of swaps is the identity permutation .
- For more points, the answer is at most
Example
stdin
5
4
1 2 3 4
4
4 1 2 3
4
2 3 4 1
5
3 2 1 5 4
6
1 2 4 3 5 6
stdout
0
0
0
4
1
Explanation
We have tests. In the first tests, the permutations already respect the given property, therefore there is no need of any swaps.
The fourth permutation needs swaps of adjacent values to be brought to either or .
For the fifth permutation it is enough to swap and to obtain the identical permutation.