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: Wrong Answer)

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;

#define REP(i,n) for(long long i = 0; i < (n); i++)
#define FOR(i, m, n) for(long long i = (m);i < (n); ++i)
#define ALL(obj) (obj).begin(),(obj).end()

template<class T> using V = vector<T>;
template<class T, class U> using P = pair<T, U>;

const ll MOD = (ll)1e9 + 7;
const ll MOD2 = 998244353;
const ll LLINF = (ll)1e18;
const ll INTINF = (ll)1e9;
const long double PI = 3.1415926535897932384626433;

template <class T> void corner(bool flg, T hoge) {if (flg) {cout << hoge << endl; exit(0);}}
template <class T, class U>ostream &operator<<(ostream &o, const map<T, U>&obj) {o << "{"; for (auto &x : obj) o << " {" << x.first << " : " << x.second << "}" << ","; o << " }"; return o;}
template <class T>ostream &operator<<(ostream &o, const set<T>&obj) {o << "{"; for (auto itr = obj.begin(); itr != obj.end(); ++itr) o << (itr != obj.begin() ? ", " : "") << *itr; o << "}"; return o;}
template <class T>ostream &operator<<(ostream &o, const vector<T>&obj) {o << "{"; for (int i = 0; i < (int)obj.size(); ++i)o << (i > 0 ? ", " : "") << obj[i]; o << "}"; return o;}
template <class T, class U>ostream &operator<<(ostream &o, const pair<T, U>&obj) {o << "{" << obj.first << ", " << obj.second << "}"; return o;}
template <template <class tmp>  class T, class U> ostream &operator<<(ostream &o, const T<U> &obj) {o << "{"; for (auto itr = obj.begin(); itr != obj.end(); ++itr)o << (itr != obj.begin() ? ", " : "") << *itr; o << "}"; return o;}
void print(void) {cout << endl;}
template <class Head> void print(Head&& head) {cout << head;print();}
template <class Head, class... Tail> void print(Head&& head, Tail&&... tail) {cout << head << " ";print(forward<Tail>(tail)...);}
template <class T> void chmax(T& a, const T b){a=max<T>(a,b);}
template <class T> void chmin(T& a, const T b){a=min<T>(a,b);}
void YN(bool flg) {cout << ((flg) ? "YES" : "NO") << endl;}
void Yn(bool flg) {cout << ((flg) ? "Yes" : "No") << endl;}
void yn(bool flg) {cout << ((flg) ? "yes" : "no") << endl;}

int main() {
	ll X,K; cin >> X >> K;
	V<ll> r(K+3),u(K+3),v(K+3),sum(K+3);
	r[0] = 0;
	r[K + 1] = (ll)1e10;
	r[K + 2] = (ll)2e10;
	for(int i = 1; i <= K; ++i) cin >> r[i];
	int Q; cin >> Q;
	V<ll> t(Q),a(Q);
	for(int i = 0; i < Q; ++i) cin >> t[i] >> a[i];

	u[0] = 0;
	v[0] = X;
	for(int i = 1; i < K + 3; ++i){
		if(i%2){
			u[i] = max(u[i-1] - (r[i] - r[i-1]),0LL);
			v[i] = max(v[i-1] - (r[i] - r[i-1]),0LL);
		}
		else{
			u[i] = min(u[i-1] + (r[i] - r[i-1]),X);
			v[i] = min(v[i-1] + (r[i] - r[i-1]),X);
		}
	}
	sum[0] = 0;
	for(int i = 1; i < K + 3; ++i) sum[i] = sum[i-1] + (i%2?-1:1)*r[i];
	V<P<ll,int>> ord(Q);
	for(int i = 0; i < Q; ++i) ord[i] = {a[i],i};
	sort(ALL(ord));
	V<int> idx(Q,K+3);
	for(int i = 0,j = 1; i < Q; ++i){
		int k = ord[i].second;
		while(0 <= a[k] + sum[j] && j + 2 < K + 3) j += 2;
		chmin(idx[k],j);
	}
	for(int i = Q-1,j = 0; 0 <= i; --i){
		int k = ord[i].second;
		while(a[k] + sum[j] <= X && j + 2 < K + 3) j += 2;
		chmin(idx[k],j);
	}
	V<ll> ans(Q);
	for(int i = 0, j = 0; i < Q; ++i){
		if(t[i] == 0){
			ans[i] = a[i];
			continue;
		}
		while(t[i]>r[j] && j + 1 < K + 3) ++j;
		
		if(j <= idx[i]){
			ans[i] = a[i] + sum[j - 1];
		}
		else{
			if(idx[i]%2){
				ans[i] = u[j-1];
			}
			else{
				ans[i] = v[j-1];
			}
		}

		if(j%2){
			ans[i] = max(ans[i] - (t[i] - r[j - 1]),0LL);
		}
		else{
			ans[i] = min(ans[i] + (t[i] - r[j - 1]),X);
		}

	}
	for(int i = 0; i < Q; ++i) cout << ans[i] << endl;
	return 0;
}