#include <bits/stdc++.h>
using namespace std;
int N, M, P[1111111], Q[1111111], z[4], x;
int l1, r1, l2, r2;
vector<int> Num[1111111];
void updata(int n, int x) {
  while (n <= N) {
    Num[n].push_back(x);
    n += n & (-n);
  }
}
int cnt(int n, int x) {
  int ret = 0;
  while (n >= 1) {
    ret += upper_bound(Num[n].begin(), Num[n].end(), x) - Num[n].begin();
    n -= n & (-n);
  }
  return ret;
}
int main() {
  scanf("%d", &N);
  for (int n = 1; n <= N; ++n) {
    scanf("%d", &x);
    P[x] = n;
  }
  for (int n = 1; n <= N; ++n) {
    scanf("%d", &x);
    Q[x] = n;
  }
  for (int n = 1; n <= N; ++n) updata(P[n], Q[n]);
  for (int n = 1; n <= N; ++n) sort(Num[n].begin(), Num[n].end());
  scanf("%d", &M);
  x = 0;
  while (M--) {
    for (int d = 0; d < 4; ++d) {
      scanf("%d", z + d);
      z[d] = (z[d] - 1 + x) % N + 1;
    }
    l1 = min(z[0], z[1]);
    r1 = max(z[0], z[1]);
    l2 = min(z[2], z[3]);
    r2 = max(z[2], z[3]);
    x = cnt(r1, r2) - cnt(r1, l2 - 1) - cnt(l1 - 1, r2) + cnt(l1 - 1, l2 - 1);
    printf("%d\n", x++);
  }
  return 0;
}
