#include <bits/stdc++.h>
using namespace std;
int n, p[1000005], q[1000005];
vector<int> rt[2000005];
void init() {
  for (int i = (n); i < (n + n); i++) rt[i].push_back(q[i - n]);
  for (int i = n - 1; i; i--)
    merge(rt[2 * i].begin(), rt[2 * i].end(), rt[2 * i + 1].begin(),
          rt[2 * i + 1].end(), back_inserter(rt[i]));
}
int qu(int l, int r, int a, int b) {
  int res = 0;
  for (l += n, r += n; l < r; l /= 2, r /= 2) {
    if (l & 1) {
      res += lower_bound(rt[l].begin(), rt[l].end(), b) -
             lower_bound(rt[l].begin(), rt[l].end(), a);
      l++;
    }
    if (r & 1) {
      r--;
      res += lower_bound(rt[r].begin(), rt[r].end(), b) -
             lower_bound(rt[r].begin(), rt[r].end(), a);
    }
  }
  return res;
}
int main() {
  scanf("%d", &n);
  for (int i = (0); i < (n); i++) {
    int x;
    scanf("%d", &x);
    p[x - 1] = i;
  }
  for (int i = (0); i < (n); i++) {
    int x;
    scanf("%d", &x);
    q[i] = p[x - 1];
  }
  init();
  int m;
  scanf("%d", &m);
  int x = 0;
  for (int i = (0); i < (m); i++) {
    int a, b, c, d;
    scanf("%d%d%d%d", &a, &b, &c, &d);
    a = ((a - 1 + x) % n), b = ((b - 1 + x) % n), c = ((c - 1 + x) % n),
    d = ((d - 1 + x) % n);
    if (a > b) swap(a, b);
    if (c > d) swap(c, d);
    b++;
    d++;
    x = qu(c, d, a, b) + 1;
    printf("%d\n", x - 1);
  }
}
