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 <iostream>
#include <vector>
#include <deque>
#include <math.h>
#include <set>
#include <iomanip>
#include <time.h>
#include <list>
#include <stdio.h>
#include <queue>
#include <map>
#include <algorithm>
#include <assert.h>
#include <memory.h>

#define mk make_pair
#define sc second
#define fr first
#define pb emplace_back
#define all(s) s.begin(), s.end()
#define sz(s) ( (int)s.size() )
#define int long long

using namespace std;

const int N = 1e5 + 5, inf = 1e18;

struct st
{
    int m, s, mn;
    st()
    {
        m = 1;
        s = 0;
        mn = inf;
    }
};

st t[N * 4];

int x, n, ar[N], q, a, b, ans[N], mp[N];

vector <pair <int, int> > V;

vector <pair <int, int> > vec;
vector <pair <int, pair <int, int> > >   M[N];

void build (int v = 1, int tl = 1, int tr = q)
{
    if (tl == tr)
        t[v].mn  = V[tl - 1].fr, mp[ V[tl - 1].sc ] = tl;
    else
    {
        int tm = (tl + tr) >> 1;

        build(v + v, tl, tm);
        build(v + v + 1, tm + 1, tr);

        t[v].mn = t[v + v].mn;
    }
}
void push (int v, int tl, int tr)
{
    t[v].mn = t[v].mn * t[v].m + t[v].s;

    if (tl != tr)
    {
        t[v + v].m *= t[v].m;
        t[v + v].s = t[v + v].s * t[v].m + t[v].s;

        t[v + v + 1].m *= t[v].m;
        t[v + v + 1].s = t[v + v + 1].s * t[v].m + t[v].s;
    }
    t[v].m = 1;
    t[v].s = 0;
}
int get_pos(int val, int v = 1, int tl = 1, int tr = q)
{
    push(v, tl, tr);

    if (t[v].mn >= val) return 0;

    if ( tl == tr )
        return tl;

    int tm = (tl + tr) >> 1;

    push(v + v, tl, tm);
    push(v + v + 1, tm + 1, tr);

    if (t[v + v + 1].mn < val)
        return get_pos( val, v + v + 1, tm + 1, tr );

    return get_pos( val, v + v , tl, tm );

}
void update (int l, int r, int m, int s, int v = 1, int tl = 1, int tr = q)
{
    push (v, tl, tr);

    if (l > r || tl > r || l > tr) return;

    if (l <= tl && tr <= r)
    {
        t[v].s = s;
        t[v].m = m;
        push (v, tl, tr);
        return;
    }
    int tm = (tl + tr) >> 1;

    update(l, r, m, s, v + v, tl, tm);
    update(l, r, m, s, v + v + 1, tm + 1, tr);

    t[v].mn = t[v + v].mn;
}
int get (int pos, int v = 1, int tl = 1, int tr = q)
{
    push(v, tl, tr);

    if (tl == tr)
        return t[v].mn;

    int tm = (tl + tr) >> 1;

    if (pos <= tm)
        return get(pos, v + v, tl, tm);

    return get(pos, v + v + 1, tm + 1, tr);
}
main ()
{
    cin >> x >> n;
    for (int i = 1; i <= n; i++)
    {
        scanf("%lld" ,&ar[i]);
        vec.pb( mk( ar[i], ar[i] - ar[i - 1] ) );
    }
    cin >> q;
    for (int i = 1; i <= q; i++)
    {
        scanf("%lld%lld", &a, &b);

        int pos = upper_bound( all(vec), mk(a, -1ll) ) - vec.begin() - 1;

        if (pos < 0) ans[i] = max(0ll, b - a);

        else M[pos].pb( mk(i, mk(a, b) ) );

        V.pb( mk(b,i) );
    }
    sort( all(V) );

    build ();

    for (int i = 0; i < vec.size(); i++)
    {
        int val = vec[i].sc;

        if(i % 2 == 0)
        {
            int pos = get_pos( val );
            update( 1, pos, 0, 0);
            update( pos + 1, q, 1, -val);
        }
        else
        {
            int pos = get_pos(x - val);

            update(1, pos, 1, val);
            update(pos + 1, q, 0, x);
        }
        for (int j = 0; j < M[i].size(); j++)
        {
            ans[ M[i][j].fr ] = get( mp[ M[i][j].fr] );

            if (i % 2 == 0)
                ans[ M[i][j].fr ] += min( x - ans[ M[i][j].fr ], (M[i][j].sc.fr - vec[i].fr) );
            else
                ans[ M[i][j].fr ] -= min( ans[ M[i][j].fr ], (M[i][j].sc.fr - vec[i].fr) );
        }
    }
    for (int i = 1; i <= q; i++)
        printf("%lld\n", ans[i]);
}
/****/