Two permutations
You are given two permutations of n elements and m queries, each described by four integers: l_1, r_1, l_2, r_2.
For each query, your task is to determine how many numbers from 1 to n have their position in the first permutation within the range [l_1, r_1] and their position in the second permutation within the range [l_2, r_2].
Input
The first line contains a single integer n (1 ≤ n ≤ 10^6), representing the number of elements in both permutations. The second line lists n integers, separated by spaces: a_1, a_2, ..., a_n (1 ≤ a_i ≤ n), which are the elements of the first permutation. The third line provides the second permutation in the same format.
The fourth line contains a single integer m (1 ≤ m ≤ 10^5), indicating the number of queries.
The following m lines each describe a query with four integers: a, b, c, d (1 ≤ a, b, c, d ≤ n). The parameters l_1, r_1, l_2, r_2 for each query are derived from a, b, c, d as follows:
Define a variable x. For the first query, x is 0. For subsequent queries, x is the answer to the previous query plus one.
Define a function f(z) = ((z + x - 1) mod n) + 1.
Replace a with f(a), b with f(b), c with f(c), and d with f(d).
Set l_1 = min(a, b), r_1 = max(a, b), l_2 = min(c, d), r_2 = max(c, d).
Output
For each query, output a single integer on a separate line, representing the answer to that query.