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 <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <ctime>
#include <cctype>
#include <cassert>
#include <map>
#include <vector>
#include <queue>

#define REP(i, a, b) for (int i = (a), _end_ = (b); i != _end_; ++i)
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define x first
#define y second
#define mp make_pair
#define pb push_back
#define SZ(x) (int((x).size()))

using namespace std;

template<typename T0, typename T1> bool chkmin(T0 &a, const T1 &b) { return a > b ? a = b, 1 : 0; }
template<typename T0, typename T1> bool chkmax(T0 &a, const T1 &b) { return a < b ? a = b, 1 : 0; }

const int oo = 0x3f3f3f3f, Max_val = 314000000;

const int maxn = 1000000, maxm = 200000, max0 = 30000000;

pair<int, int> p[maxn + 5];

int n, m;

pair<pair<int, int>, pair<int, int> > q[(maxm << 1) + 5];
int ans[maxm + 5];

struct node
{
	int val;
	node *c[2];

	node(): val(0) { memset(c, 0, sizeof c); }
};

node nd[max0 + 5];
int tot_nd = 0;

void insert(node *&rt, const int &x, const int &l = 0, const int &r = maxn)
{
	if (rt == NULL) rt = nd + (tot_nd++);
	else
	{
		node tmp = *rt;
		rt = nd + (tot_nd++);
		*rt = tmp;
	}
	++rt->val;
	if (r - l <= 1) return;
	int mid = (l + r) >> 1;
	if (x < mid) insert(rt->c[0], x, l, mid);
	else insert(rt->c[1], x, mid, r);
}

int query(node *rt, const int &x, const int &y, const int &l = 0, const int &r = maxn)
{
	if (rt == NULL) return 0;
	if (x <= l && r <= y) return rt->val;
	int mid = (l + r) >> 1;
	int ret = 0;
	if (x < mid) ret += query(rt->c[0], x, y, l, mid);
	if (y > mid) ret += query(rt->c[1], x, y, mid, r);
	return ret;
}

node *rt[maxn + 5];

int main()
{
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	scanf("%d", &n);
	REP(i, 0, n)
	{
		static int x;
		scanf("%d", &x), --x;
		p[x].x = i;
	}
	REP(i, 0, n)
	{
		static int x;
		scanf("%d", &x), --x;
		p[x].y = i;
	}
	sort(p, p + n);
	int j = 0;
	rt[0] = NULL;
	REP(i, 0, n)
	{
		rt[i + 1] = rt[i];
		while (p[j].x == i) insert(rt[i + 1], p[j++].y);
	}
	scanf("%d", &m);
	int lst = 0;
	REP(i, 0, m)
	{
		static int x0, y0, x1, y1;
		scanf("%d%d%d%d", &x0, &y0, &x1, &y1), --x0, --y0, --x1, --y1;
		(x0 += lst) %= n, (y0 += lst) %= n, (x1 += lst) %= n, (y1 += lst) %= n;
		if (x0 > y0) swap(x0, y0);
		if (x1 > y1) swap(x1, y1);
		lst = query(rt[y0 + 1], x1, y1 + 1) - query(rt[x0], x1, y1 + 1);
		printf("%d\n", lst);
		++lst;
	}
	return 0;
}
