405228: GYM101853 C Intersections

Memory Limit:256 MB Time Limit:1 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

C. Intersectionstime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard output

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:

  1. For each number x, draw a line segment connecting between its positions in the given permutations.
  2. 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?

Input

The 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.

Output

For each test case, print a single line containing the number of intersections between the line segments.

ExampleInput
2
5
5 4 2 1 3
2 5 4 1 3
4
1 2 3 4
1 2 4 3
Output
2
1

加入题单

算法标签: