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
1
1
1
1
1 1 1 1

1

#include <bits/stdc++.h>
int IntMaxVal = (int)1e20;
int IntMinVal = (int)-1e20;
long long LongMaxVal = (long long)1e20;
long long LongMinVal = (long long)-1e20;
using namespace std;
template <typename T>
struct argument_type;
template <typename T, typename U>
struct argument_type<T(U)> {};
template <typename T1, typename T2>
istream& operator>>(istream& is, pair<T1, T2>& s) {
  is >> s.first >> s.second;
  return is;
}
template <typename T>
ostream& operator<<(ostream& os, const vector<T>& v) {
  for (int i = 0; i < v.size(); i++) os << v[i] << ' ';
  os << '\n';
  ;
  return os;
}
template <typename T1, typename T2>
ostream& operator<<(ostream& s, pair<T1, T2>& t) {
  s << t.first << ' ' << t.second;
  return s;
}
int main() {
  srand(time(NULL));
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n;
  cin >> n;
  ;
  vector<argument_type<void(int)>::U> p1(n);
  {
    for (int i = 0; i < n; i++) cin >> p1[i];
  };
  vector<argument_type<void(int)>::U> p2(n);
  {
    for (int i = 0; i < n; i++) cin >> p2[i];
  };
  vector<int> ixP2(n + 1);
  for (int i = 0; i < n; ++i) ixP2[p2[i]] = i;
  for (int i = 0; i < n; ++i) p1[i] = ixP2[p1[i]];
  double s = sqrt(n);
  vector<pair<int, int> > segments((int)(s + 0.5));
  for (int i = 0; i < segments.size(); ++i) {
    if (i) segments[i].first = segments[i - 1].second;
    if (i == segments.size() - 1)
      segments[i].second = n;
    else
      segments[i].second = (int)(segments[i].first + s);
  }
  vector<int> toSegment(n);
  int p = 0;
  for (int i = 0; i < n; ++i) {
    if (segments[p].second < i) p++;
    toSegment[i] = p;
  }
  vector<int> index1(n);
  for (int i = 0; i < n; ++i) index1[p1[i]] = i;
  int ns = segments.size();
  int countBySegment[ns][ns];
  for (int segment = 0; segment < ns; ++segment) {
    for (int ts = 0; ts < ns; ++ts) countBySegment[segment][ts] = 0;
    for (int i = segments[segment].first; i < segments[segment].second; ++i)
      countBySegment[segment][toSegment[p1[i]]]++;
    for (int i = 1; i < ns; ++i)
      countBySegment[segment][i] += countBySegment[segment][i - 1];
    if (segment != 0)
      for (int ts = 0; ts < ns; ++ts)
        countBySegment[segment][ts] += countBySegment[segment - 1][ts];
  }
  int m;
  cin >> m;
  ;
  int x = 0;
  while (m-- > 0) {
    int a;
    cin >> a;
    ;
    int b;
    cin >> b;
    ;
    int c;
    cin >> c;
    ;
    int d;
    cin >> d;
    ;
    int f1 = (a - 1 + x) % n;
    int f2 = (b - 1 + x) % n;
    int f3 = (c - 1 + x) % n;
    int f4 = (d - 1 + x) % n;
    int l1 = min(f1, f2);
    int r1 = f1 + f2 - l1;
    int l2 = min(f3, f4);
    int r2 = f3 + f4 - l2;
    x = 0;
    if (toSegment[r1] > toSegment[l1] + 1 &&
        toSegment[r2] > toSegment[l2] + 1) {
      if (toSegment[r1] && toSegment[r2])
        x += countBySegment[toSegment[r1] - 1][toSegment[r2] - 1];
      if (toSegment[r1] && toSegment[r2])
        x -= countBySegment[toSegment[r1] - 1][toSegment[l2]];
      if (toSegment[r1] && toSegment[r2])
        x -= countBySegment[toSegment[l1]][toSegment[r2] - 1];
      if (toSegment[r1] && toSegment[r2])
        x += countBySegment[toSegment[l1]][toSegment[l2]];
    }
    if (toSegment[l1] == toSegment[r1]) {
      for (int i = l1; i < r1 + 1; ++i)
        if (toSegment[l2] < toSegment[p1[i]] &&
            toSegment[p1[i]] < toSegment[r2])
          x++;
    } else {
      for (int i = l1; i < segments[toSegment[l1]].second; ++i)
        if (toSegment[l2] < toSegment[p1[i]] &&
            toSegment[p1[i]] < toSegment[r2])
          x++;
      for (int i = segments[toSegment[r1]].first; i < r1 + 1; ++i)
        if (toSegment[l2] < toSegment[p1[i]] &&
            toSegment[p1[i]] < toSegment[r2])
          x++;
    }
    if (toSegment[l2] == toSegment[r2]) {
      for (int i = l2; i < r2 + 1; ++i)
        if (l1 <= index1[i] && index1[i] <= r1) x++;
    } else {
      for (int i = l2; i < segments[toSegment[l2]].second; ++i)
        if (l1 <= index1[i] && index1[i] <= r1) x++;
      for (int i = segments[toSegment[r2]].first; i < r2 + 1; ++i)
        if (l1 <= index1[i] && index1[i] <= r1) x++;
    }
    cout << x << '\n';
    ;
    x++;
  }
}
