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++ (GCC 9.2.1) (judge verdict: Wrong Answer)

#include <bits/stdc++.h>
using namespace std;

#define int long long
#define rep(i, n) for (int i = 0; i < (int) (n); i++)
#define reps(i, n) for (int i = 1; i <= (int) (n); i++)
#define all(x) (x).begin(), (x).end()
#define uniq(x) (x).erase(unique(all(x)), (x).end())
#define bit(n) (1LL << (n))
#define cdiv(a, b) (((a) - 1) / (b) + 1)
#define dump(x) cerr << #x " = " << (x) << endl
using vint = vector<int>;
using vvint = vector<vint>;
using pint = pair<int, int>;
using vpint = vector<pint>;
template<typename T> using priority_queue_rev = priority_queue<T, vector<T>, greater<T>>;
constexpr double PI = 3.1415926535897932384626433832795028;
constexpr int DY[9] = {0, 1, 0, -1, 1, 1, -1, -1, 0};
constexpr int DX[9] = {1, 0, -1, 0, 1, -1, -1, 1, 0};
int gcd(int a, int b) {
    while (b) { swap(a %= b, b); }
    return a;
}
int lcm(int a, int b) { return a / gcd(a, b) * b; }
int sign(int x) { return (x > 0) - (x < 0); }
template<typename T> void fin(T mes) {
    cout << mes << endl;
    exit(0);
}
template<typename T, typename U> bool chmax(T &a, const U &b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}
template<typename T, typename U> bool chmin(T &a, const U &b) {
    if (b < a) {
        a = b;
        return true;
    }
    return false;
}
template<typename T, typename U> ostream &operator<<(ostream &os, const pair<T, U> &rhs) {
    os << "(" << rhs.first << ", " << rhs.second << ")";
    return os;
}
template<typename T> ostream &operator<<(ostream &os, const vector<T> &rhs) {
    os << "{";
    for (auto itr = rhs.begin(); itr != rhs.end(); itr++) {
        os << *itr << (next(itr) != rhs.end() ? ", " : "");
    }
    os << "}";
    return os;
}
template<typename T> ostream &operator<<(ostream &os, const deque<T> &rhs) {
    os << "{";
    for (auto itr = rhs.begin(); itr != rhs.end(); itr++) {
        os << *itr << (next(itr) != rhs.end() ? ", " : "");
    }
    os << "}";
    return os;
}
template<typename T> ostream &operator<<(ostream &os, const set<T> &rhs) {
    os << "{";
    for (auto itr = rhs.begin(); itr != rhs.end(); itr++) {
        os << *itr << (next(itr) != rhs.end() ? ", " : "");
    }
    os << "}";
    return os;
}
template<typename T, typename U> ostream &operator<<(ostream &os, const map<T, U> &rhs) {
    os << "{";
    for (auto itr = rhs.begin(); itr != rhs.end(); itr++) {
        os << *itr << (next(itr) != rhs.end() ? ", " : "");
    }
    os << "}";
    return os;
}
struct setup {
    static constexpr int PREC = 20;
    setup() {
        cout << fixed << setprecision(PREC);
        cerr << fixed << setprecision(PREC);
    };
} setup;

int X, K;
int r[110000];
int Q;
int t[110000], a[110000];
signed main() {
    cin >> X >> K;
    vpint seg{{0, 0}};
    int sgn = 1;
    rep(i, K) { cin >> r[i]; }
    r[K] = 2e9, r[K + 1] = 5e9;
    K += 2;
    rep(i, K) { seg.emplace_back(pint(r[i], seg.back().second + (r[i] - r[i - 1]) * (sgn *= -1))); }
    vpint peaku{{0, 0}}, peakd{{0, 0}}, segu{{0, X}}, segd{{0, 0}};
    {
        int p = 0;
        reps(i, K) {
            pint s = seg[i];
            if (chmax(p, s.second)) { peaku.emplace_back(pint(s.second, s.first)); }
            pint nxt = {s.first, s.second - seg[i - 1].second + segu.back().second};
            if (0 <= nxt.second && nxt.second <= X) { segu.emplace_back(nxt); }
            else if (nxt.second > X) {
                if (segu.back().second == X) { segu.emplace_back(nxt.first, X); }
                else {
                    segu.emplace_back(pint(nxt.first - (nxt.second - X), X));
                    segu.emplace_back(pint(nxt.first, X));
                }
            } else if (nxt.second < 0) {
                if (segu.back().second == 0) { segu.emplace_back(nxt.first, 0); }
                else {
                    segu.emplace_back(pint(nxt.first + nxt.second, 0));
                    segu.emplace_back(pint(nxt.first, 0));
                }
            }
        }
    }
    {
        int p = 0;
        reps(i, K) {
            pint s = seg[i];
            if (chmin(p, s.second)) { peakd.emplace_back(pint(s.second, s.first)); }
            pint nxt = {s.first, s.second - seg[i - 1].second + segd.back().second};
            if (0 <= nxt.second && nxt.second <= X) { segd.emplace_back(nxt); }
            else if (nxt.second > X) {
                if (segd.back().second == X) { segd.emplace_back(nxt.first, X); }
                else {
                    segd.emplace_back(pint(nxt.first - (nxt.second - X), X));
                    segd.emplace_back(pint(nxt.first, X));
                }
            } else if (nxt.second < 0) {
                if (segd.back().second == 0) { segd.emplace_back(nxt.first, 0); }
                else {
                    segd.emplace_back(pint(nxt.first + nxt.second, 0));
                    segd.emplace_back(pint(nxt.first, 0));
                }
            }
        }
    }
    cin >> Q;
    rep(i, Q) {
        cin >> t[i] >> a[i];
        int tu = lower_bound(all(peaku), pint(X - a[i], 0))->second;
        int td = lower_bound(all(peakd), pint(-a[i], 0), [](pint l, pint r) { return l > r; })->second;
        vpint::iterator p;
        if (t[i] < min(tu, td)) {
            p = upper_bound(all(seg), pint(t[i], LLONG_MAX)) - 1;
            cout << clamp(a[i] + p->second + (t[i] - p->first) * sign((p + 1)->second - p->second), 0LL, X) << endl;
        } else {
            if (tu < td) { p = upper_bound(all(segu), pint(t[i], LLONG_MAX)) - 1; }
            else { p = upper_bound(all(segd), pint(t[i], LLONG_MAX)) - 1; }
            cout << clamp(p->second + (t[i] - p->first) * sign((p + 1)->second - p->second), 0LL, X) << endl;
        }
    }
}