#SDNU1579. FFFFFunctions
FFFFFunctions
Description
Define two functions. Fucntion 1:
$$f(a,b) = \begin{cases} a & {if\;b=0} \\\\ f(b,\mid a-b \mid) & {else} \end{cases} $$Function 2:
$$S(n)=f(f(\cdots f(f(a_{p_{1}},a_{p_{2}}),a_{p_{3}})\cdots ,a_{p_{n-1}}),a_{p_{n}}) $$Now, you are given two sequences: and You are supposed to calculate the value of the function 2.
Format
Input
The input contains several test cases and the number of test cases is no more than 500. For each test case, the first line contains interger . The second line contains intergers for the sequence . The third line contains intergers for the sequence which is a permutation for . $(1 \leq n \leq 1000000, 1 \leq a_i \leq 1000000000, 1 \leq p_i \leq n)$
(while n is 0,you should output nothing and input next case)
Output
For each test case, output interger in one line for the value of the function 2.
Samples
2
3 5
2 1
1