405228: GYM101853 C Intersections
Description
In this problem, you are given two permutations a and b of n numbers, and you need to play a game with them! In this game, you are required to perform the following steps:
- For each number x, draw a line segment connecting between its positions in the given permutations.
- Count the number of intersections between the line segments.
For example, let us consider two permutations (5, 4, 2, 1, 3) and (2, 5, 4, 1, 3). The following picture shows the permutations after drawing all line segments. In the picture, the number of intersections between the line segments is 2.
Given the permutations a and b, your task is to play the game and to count number of intersections between the line segments. Can you?
InputThe first line contains an integer T (1 ≤ T ≤ 100), in which T is the number of test cases.
The first line of each test case contains an integer n (1 ≤ n ≤ 105), in which n is the size of permutations.
Then a line follow containing n distinct integers a1, a2, ..., an (1 ≤ ai ≤ n), giving the first permutation a.
Then a line follow containing n distinct integers b1, b2, ..., bn (1 ≤ bi ≤ n), giving the second permutation b.
The sum of n overall test cases does not exceed 7 × 105.
OutputFor each test case, print a single line containing the number of intersections between the line segments.
ExampleInput2Output
5
5 4 2 1 3
2 5 4 1 3
4
1 2 3 4
1 2 4 3
2
1