In order to fight Life Battle-Fibers, little G, the god of computer, created a mysterious organization, but this organization isn’t important in this problem. In addition to founding the organization, little G also studies various ways to fight Life Battle-Fibers.The most powerful weapon that he created is a kind of clothing named Kamui. Among all Kamui, Senketsu has the strongest power.

Little G now has the blueprint of Senketsu, but sewing Senketsu is very troublesome, which need to put different kinds of fibers in the correct position. The blueprint can be described by an permutation of integers from 1 to N, with different numbers representing different kinds of fibers. We use ‘A’ to represent this permutation. If the i-th number in A is j, then the j-th kind of fiber should be put in the i-th position. Little G initialy took out n kinds of fibers and placed the i-th kind of fiber in the i-th position. In order to change the fiber sequence to the same as the the blueprint of Senketsu, he can exchange fibers in any two positions any times. If we want to exchange the x-th kind and the y-th kind of fibers, then the exchange needs Wx+Wy units time. Given the array A and W, you need to tell little G the shortest time to complete Senketsu.

In the first line there is an integer T(1<=T<=3), which indicates the number of test cases. For each test case, the first line is an integer representing N(1<=N<=10^6). The second line consists of n integers, representing the array W(1<=Wi<=10^9). The third line consists of n integers, representing the array A. It is guaranteed that A is a permutation of integers from 1 to N.