You are given two permutations p and q, consisting of n elements, and m queries of the form: l1, r1, l2, r2 (l1 ≤ r1; l2 ≤ r2). The response for the query is the number of such integers from 1 to n, that their position in the first permutation is in segment [l1, r1] (borders included), and position in the second permutation is in segment [l2, r2] (borders included too).

A permutation of n elements is the sequence of n distinct integers, each not less than 1 and not greater than n.

Position of number v (1 ≤ v ≤ n) in permutation g1, g2, ..., gn is such number i, that gi = v.

Input

The first line contains one integer n (1 ≤ n ≤ 106), the number of elements in both permutations. The following line contains n integers, separated with spaces: p1, p2, ..., pn (1 ≤ pi ≤ n). These are elements of the first permutation. The next line contains the second permutation q1, q2, ..., qn in same format.

The following line contains an integer m (1 ≤ m ≤ 2·105), that is the number of queries.

The following m lines contain descriptions of queries one in a line. The description of the i-th query consists of four integers: a, b, c, d (1 ≤ a, b, c, d ≤ n). Query parameters l1, r1, l2, r2 are obtained from the numbers a, b, c, d using the following algorithm: 

  1. Introduce variable x. If it is the first query, then the variable equals 0, else it equals the response for the previous query plus one. 
  2. Introduce function f(z) = ((z - 1 + x) mod n) + 1. 
  3. Suppose l1 = min(f(a), f(b)), r1 = max(f(a), f(b)), l2 = min(f(c), f(d)), r2 = max(f(c), f(d)). 

Output

Print a response for each query in a separate line.

Examples

Input

3
3 1 2
3 2 1
1
1 2 3 3


Output

1


Input

4
4 3 2 1
2 3 4 1
3
1 2 3 4
1 3 2 1
1 4 2 3


Output

1
1
2
#include <bits/stdc++.h>
using namespace std;
const int inf = ((1 << 30) - 1);
const long long linf = ((1ll << 62) - 1);
const int cmax = 1e6;
const int kmax = 1 << 12;
int n;
int a[cmax], b[cmax], res[cmax];
vector<int> rmq[cmax * 4], _left[cmax * 4], _right[cmax * 4];
void buildLowerBound(vector<int> &a, vector<int> &b, vector<int> &res) {
  int it = 0, size1 = ((int)(a).size()), size2 = ((int)(b).size());
  for (int i = 0; i < size1; i++) {
    while (it < size2 && b[it] < a[i]) ++it;
    res[i] = it;
  }
}
void make_rmq(int i, int l, int r) {
  if (l == r) {
    rmq[i].resize(1);
    rmq[i][0] = a[l];
    return;
  }
  make_rmq(i * 2, l, (l + r) / 2);
  make_rmq(i * 2 + 1, (l + r) / 2 + 1, r);
  int size = r - l + 1;
  rmq[i].resize(size);
  merge((rmq[i * 2]).begin(), (rmq[i * 2]).end(), (rmq[i * 2 + 1]).begin(),
        (rmq[i * 2 + 1]).end(), rmq[i].begin());
  _left[i].resize(size), _right[i].resize(size);
  buildLowerBound(rmq[i], rmq[i * 2], _left[i]);
  buildLowerBound(rmq[i], rmq[i * 2 + 1], _right[i]);
}
inline int getValue(int i, int ll, int rr, int l, int r, int b, int t) {
  t = min(t, rr - ll + 1);
  if (ll > r || rr < l) return 0;
  if (l <= ll && rr <= r) return max(0, t - b);
  return getValue(i * 2, ll, (ll + rr) / 2, l, r,
                  (b >= ((int)(_left[i]).size()) ? ((int)(_left[i]).size())
                                                 : _left[i][b]),
                  (t >= ((int)(_left[i]).size()) ? ((int)(_left[i]).size())
                                                 : _left[i][t])) +
         getValue(i * 2 + 1, (ll + rr) / 2 + 1, rr, l, r,
                  (b >= ((int)(_right[i]).size()) ? ((int)(_right[i]).size())
                                                  : _right[i][b]),
                  (t >= ((int)(_right[i]).size()) ? ((int)(_right[i]).size())
                                                  : _right[i][t]));
}
int x = 0;
int f(int z) { return ((z - 1 + x) % n) + 1; }
int main() {
  assert(scanf("%d", &n));
  for (int i = 0; i < n; i++) {
    assert(scanf("%d", &a[i])), --a[i];
  }
  for (int i = 0; i < n; i++) {
    int t;
    assert(scanf("%d", &t)), --t;
    b[t] = i;
  }
  for (int i = 0; i < n; i++) res[i] = b[a[i]];
  memcpy(a, res, sizeof(int) * n);
  make_rmq(1, 0, n - 1);
  int m;
  assert(scanf("%d", &m));
  for (int i = 0; i < m; i++) {
    int a, b, c, d;
    assert(scanf("%d%d%d%d", &a, &b, &c, &d));
    int l1 = min(f(a), f(b)) - 1, r1 = max(f(a), f(b)) - 1;
    int l2 = min(f(c), f(d)) - 1, r2 = max(f(c), f(d)) - 1;
    printf("%d\n",
           x = getValue(1, 0, n - 1, l1, r1,
                        lower_bound((rmq[1]).begin(), (rmq[1]).end(), l2) -
                            rmq[1].begin(),
                        upper_bound((rmq[1]).begin(), (rmq[1]).end(), r2) -
                            rmq[1].begin())),
        x++;
    if ((i & 2047) == 2047) ({});
  }
  return 0;
}
