Problem: 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

Solution (in CPP):

Solution:
#include <bits/stdc++.h>
class Counter {
 public:
  Counter(std::vector<size_t> perm1, std::vector<size_t> perm2)
      : size_(perm1.size()) {
    std::vector<size_t> inv2(size_, 0);
    for (size_t i = 0; i < size_; ++i) {
      inv2[perm2[i]] = i;
    }
    auto& points = perm1;
    for (size_t x = 0; x < size_; ++x) {
      points[x] = inv2[perm1[x]];
    }
    size_t nextPowOfTwo = 1;
    while (nextPowOfTwo < size_) nextPowOfTwo <<= 1;
    segTree_.resize(2 * nextPowOfTwo - 1);
    build(points, 0, 0, size_ - 1);
  }
  int count(size_t minX, size_t maxX, size_t minY, size_t maxY) {
    return countAux(0, 0, size_ - 1, minX, maxX, minY, maxY);
  }

 private:
  void build(const std::vector<size_t>& points, int vertex, int from, int to) {
    if (from == to) {
      segTree_[vertex].push_back(points[from]);
    } else {
      int middle = from + (to + 1 - from) / 2;
      build(points, 2 * vertex + 1, from, middle - 1);
      build(points, 2 * vertex + 2, middle, to);
      const auto& left = segTree_[2 * vertex + 1];
      const auto& right = segTree_[2 * vertex + 2];
      segTree_[vertex].reserve(left.size() + right.size());
      std::merge(left.begin(), left.end(), right.begin(), right.end(),
                 std::back_inserter(segTree_[vertex]));
    }
  }
  int countAux(int vertex, int from, int to, int minX, int maxX, int minY,
               int maxY) {
    if (maxX < minX) {
      return 0;
    }
    if (from == minX && to == maxX) {
      const auto& range = segTree_[vertex];
      auto it1 = std::lower_bound(range.begin(), range.end(), minY);
      auto it2 = std::lower_bound(range.begin(), range.end(), maxY + 1);
      return it2 - it1;
    }
    int middle = from + (to + 1 - from) / 2;
    int countLeft = countAux(2 * vertex + 1, from, middle - 1, minX,
                             std::min(maxX, middle - 1), minY, maxY);
    int countRight = countAux(2 * vertex + 2, middle, to,
                              std::max(minX, middle), maxX, minY, maxY);
    return countLeft + countRight;
  }

 private:
  size_t size_;
  std::vector<std::vector<int>> segTree_;
};
int main() {
  size_t size;
  ::scanf("%lu", &size);
  auto readPerm = [](size_t size) {
    std::vector<size_t> perm;
    perm.reserve(size);
    for (size_t i = 0; i < size; ++i) {
      size_t elem;
      ::scanf("%lu", &elem);
      --elem;
      perm.push_back(elem);
    }
    return perm;
  };
  auto perm1 = readPerm(size);
  auto perm2 = readPerm(size);
  Counter counter(std::move(perm1), std::move(perm2));
  size_t nQueries;
  ::scanf("%lu", &nQueries);
  size_t x = 0;
  auto f = [&](size_t z) { return (z + x - 1) % size; };
  for (size_t i = 0; i < nQueries; ++i) {
    size_t a, b, c, d;
    ::scanf("%lu %lu %lu %lu", &a, &b, &c, &d);
    size_t count = counter.count(std::min(f(a), f(b)), std::max(f(a), f(b)),
                                 std::min(f(c), f(d)), std::max(f(c), f(d)));
    std::cout << count << "\n";
    x = count + 1;
  }
}