Problem:
Score : 700 points

Problem Statement

We have a sandglass consisting of two bulbs, bulb A and bulb B. These bulbs contain some amount of sand.
When we put the sandglass, either bulb A or B lies on top of the other and becomes the upper bulb. The other bulb becomes the lower bulb.

The sand drops from the upper bulb to the lower bulb at a rate of 1 gram per second.
When the upper bulb no longer contains any sand, nothing happens.

Initially at time 0, bulb A is the upper bulb and contains a grams of sand; bulb B contains X-a grams of sand (for a total of X grams).

We will turn over the sandglass at time r_1,r_2,..,r_K. Assume that this is an instantaneous action and takes no time. Here, time t refer to the time t seconds after time 0.

You are given Q queries.
Each query is in the form of (t_i,a_i).
For each query, assume that a=a_i and find the amount of sand that would be contained in bulb A at time t_i.

Constraints

1≤X≤10^9

1≤K≤10^5

1≤r_1<r_2< .. <r_K≤10^9

1≤Q≤10^5

0≤t_1<t_2< .. <t_Q≤10^9

0≤a_i≤X (1≤i≤Q)

All input values are integers.

Input

The input is given from Standard Input in the following format:

X
K
r_1 r_2 .. r_K
Q
t_1 a_1
t_2 a_2
:
t_Q a_Q

Output

For each query, print the answer in its own line.

Sample Input 1

180
3
60 120 180
3
30 90
61 1
180 180

Sample Output 1

60
1
120

In the first query, 30 out of the initial 90 grams of sand will drop from bulb A, resulting in 60 grams.
In the second query, the initial 1 gram of sand will drop from bulb A, and nothing will happen for the next 59 seconds. Then, we will turn over the sandglass, and 1 second after this, bulb A contains 1 gram of sand at the time in question.

Sample Input 2

100
1
100000
4
0 100
90 100
100 100
101 100

Sample Output 2

100
10
0
0

In every query, the upper bulb initially contains 100 grams, and the question in time comes before we turn over the sandglass.

Sample Input 3

100
5
48 141 231 314 425
7
0 19
50 98
143 30
231 55
342 0
365 100
600 10

Sample Output 3

19
52
91
10
58
42
100

Solution in C++14 (GCC 5.4.1) (judge verdict: Accepted)

#include <bits/stdc++.h>

#define int long long
#define double long double

using namespace std;

#define REP(i, a, b) for (int(i) = (a); i < (b); (i)++)
#define rep(i, n) REP(i, 0, n)
#define PER(i, a, b) for (int(i) = (a - 1); i >= (b); (i)--)
#define per(i, n) PER(i, n, 0)
#define each(i, n) for (auto &i : n)
#define clr(a) memset((a), 0, sizeof(a))
#define mclr(a) memset((a), -1, sizeof(a))
#define all(a) (a).begin(), (a).end()
#define dum(val) cerr << #val " = " << val << " ";
#define dump(val) cerr << #val " = " << val << endl;
#define FILL(a, v) fill(a, a + sizeof(a) / sizeof(*a), v)

typedef pair<int, int> P;
typedef tuple<int, int, int> T;

const int mod = 998244353;
const int INF = 1e9;

signed main() {
	int x, k;
	cin >> x >> k;

	vector<int> r(k + 1, 0);
	for (int i = 1; i < k + 1; i++) {
		cin >> r[i];
	}
	k++;

	vector<int> dr(k);
	vector<int> dm(k);
	vector<int> df(k);
	dm[0] = 0;
	dr[0] = 0;
	df[0] = 0;

	REP(i, 1, k) {
		dr[i] = min(x, r[i] - r[i - 1]);
		dm[i] = dr[i] - dm[i - 1];
		df[i] = df[i - 1] + (i % 2 ? -1 : +1) * dr[i];
	}

	vector<P> ud(k);
	vector<P> dd(k);
	ud[0].first = x;
	ud[0].second = 0;
	dd[0].first = 0;
	dd[0].second = x;

	REP(i, 1, k) {
		ud[i] = ud[i - 1];
		dd[i] = dd[i - 1];
		if (i % 2 == 0) {
			{
				int df = min(dr[i], ud[i].second);
				ud[i].first += df;
				ud[i].second -= df;
			}
			{
				int df = min(dr[i], dd[i].second);
				dd[i].first += df;
				dd[i].second -= df;
			}
		} else {
			{
				int df = min(dr[i], ud[i].first);
				ud[i].first -= df;
				ud[i].second += df;
			}
			{
				int df = min(dr[i], dd[i].first);
				dd[i].first -= df;
				dd[i].second += df;
			}
		}
	}

	map<int, int> tmp;
	rep(i, k) {
		tmp[r[i]] = i;
	}

	map<int, int> mpA, mpB;
	rep(i, k) {
		int p = dm[i];
		if (i % 2 == 1 && (mpA.size() == 0 || prev(mpA.end())->first < p)) {
			mpA[p] = i;
		}
		if (i % 2 == 0 && (mpB.size() == 0 || prev(mpB.end())->first < p)) {
			mpB[p] = i;
		}
	}

	int q;
	cin >> q;

	for (int i = 0; i < q; i++) {
		int t, a;
		cin >> t >> a;
		int b = x - a;

		int rt = prev(tmp.upper_bound(t))->first;
		int ti = prev(tmp.upper_bound(t))->second;
		int dt = (t - rt) * (ti % 2 ? +1 : -1);

		int left = INF;
		int la, lb;
		if (mpA.lower_bound(a) != mpA.end()) {
			if (left > mpA.lower_bound(a)->second) {
				left = mpA.lower_bound(a)->second;
				la = 0;
				lb = x;
			}
		}
		if (mpB.lower_bound(b) != mpB.end()) {
			if (left > mpB.lower_bound(b)->second) {
				left = mpB.lower_bound(b)->second;
				la = x;
				lb = 0;
			}
		}

		// dump(ti);
		// dump(dt);
		// dum(t);
		// dum(a);
		// dump(b);
		// dump(left);
		// dum(la);
		// dump(lb);
		// dum(ud[left].first);
		// dump(ud[left].second);
		// dum(dd[left].first);
		// dump(dd[left].second);

		int ans;
		if (ti < left) {
			ans = a + df[ti] + dt;
		} else if (la == ud[left].first && lb == ud[left].second) {
			ans = ud[ti].first + dt;
		} else if (la == dd[left].first && lb == dd[left].second) {
			ans = dd[ti].first + dt;
		} else {
			assert(0);
		}
		cout << max(0ll, min(x, ans)) << endl;
	}

	return 0;
}