A graph G is a set of vertices V together with a set of edges E, written G=(V,E). Both sets are usually listed out, but some graphs fix a construction rule instead of listing the edges. A permutation graph is one of those.
Take two permutations of {1,2,…,n}. Draw two parallel lines. On the upper line place the numbers from left to right in the order of the first permutation, and on the lower line place them in the order of the second permutation. Then join every number on the upper line to the same number on the lower line with a segment. The pairs of segments that cross are the edges of the permutation graph, and the vertices are the numbers 1 through n.
When the two permutations are (2,5,4,1,3) and (1,5,3,2,4), six pairs of segments cross, so the permutation graph is V={1,2,3,4,5}, E={(1,2),(1,4),(1,5),(2,3),(2,5),(3,4)}.
Given two permutations of {1,2,…,n}, write a program that counts the edges of the permutation graph they build.
The first line contains the number of test cases T. Each test case takes three lines. The first line holds n (1≤n≤100,000), and the next two lines hold one permutation each. Both are permutations of {1,2,…,n} and their elements are separated by spaces.
For each test case, print the number of edges of the permutation graph on its own line.