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 Rust (1.42.0) (judge verdict: Wrong Answer)

use proconio::input;

#[derive(Debug, Clone, Copy, Eq, PartialEq)]
enum Dir {
    Upward,
    Downward,
}

#[derive(Debug, Clone, Copy, Eq, PartialEq)]
enum State {
    Point(i32),
    Interval(i32, i32, i32),
}

impl State {
    pub fn shift(self, dr: i32, ex: i32, dir: Dir) -> Self {
        match self {
            State::Point(x) => match dir {
                Dir::Upward => State::Point(ex.min(x + dr)),
                Dir::Downward => State::Point(0.min(x - dr)),
            },
            State::Interval(x, y, d) => match dir {
                Dir::Upward => {
                    let y = y + dr;
                    if ex <= y {
                        State::Point(ex)
                    } else if ex <= y + d {
                        State::Interval(x, y, d - ((y + d) - ex))
                    } else {
                        State::Interval(x, y, d)
                    }
                }
                Dir::Downward => {
                    let y = y - dr;
                    if y + d <= 0 {
                        State::Point(0)
                    } else if y <= 0 {
                        State::Interval(x - y, 0, d + y)
                    } else {
                        State::Interval(x, y, d)
                    }
                }
            },
        }
    }

    pub fn get(&self, z: i32, ex: i32) -> i32 {
        assert!(0 <= z && z <= ex);
        match self {
            State::Point(x) => *x,
            State::Interval(x, y, d) => y + (z - x).max(0).min(*d),
        }
    }
}

fn main() {
    input!(
        ex: i32,
        n: usize,
        r: [usize; n],
        q: usize,
        ta: [(usize, usize); q]
    );
    let mut states = vec![(0, State::Interval(0, 0, ex)); n + 1];
    for i in 0..n {
        let prv = states[i].0;
        let state = states[i].1.shift(
            (r[i] - prv) as i32,
            ex,
            if i % 2 == 0 {
                Dir::Downward
            } else {
                Dir::Upward
            },
        );
        states[i + 1] = (r[i], state);
    }

    for (t, a) in ta {
        let mut l = 0;
        let mut r = n + 1;
        while 1 < r - l {
            let c = l + (r - l) / 2;
            if states[c].0 <= t {
                l = c;
            } else {
                r = c;
            }
        }
        let dt = t - states[l].0;
        let state = states[l].1.shift(
            dt as i32,
            ex,
            if l % 2 == 0 {
                Dir::Downward
            } else {
                Dir::Upward
            },
        );
        println!("{}", state.get(a as i32, ex));
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_shift() {
        // Upward
        assert_eq!(
            State::Interval(5, 10, 20).shift(5, 40, Dir::Upward),
            State::Interval(5, 15, 20)
        );
        assert_eq!(
            State::Interval(5, 10, 20).shift(10, 40, Dir::Upward),
            State::Interval(5, 20, 20)
        );
        assert_eq!(
            State::Interval(5, 10, 20).shift(15, 40, Dir::Upward),
            State::Interval(5, 25, 15)
        );
        assert_eq!(
            State::Interval(5, 10, 20).shift(25, 40, Dir::Upward),
            State::Interval(5, 35, 5)
        );
        assert_eq!(
            State::Interval(5, 10, 20).shift(30, 40, Dir::Upward),
            State::Point(40)
        );
        // Downward
        assert_eq!(
            State::Interval(5, 10, 20).shift(5, 40, Dir::Downward),
            State::Interval(5, 5, 20)
        );
        assert_eq!(
            State::Interval(5, 10, 20).shift(10, 40, Dir::Downward),
            State::Interval(5, 0, 20)
        );
        assert_eq!(
            State::Interval(5, 10, 20).shift(15, 40, Dir::Downward),
            State::Interval(10, 0, 15)
        );
        assert_eq!(
            State::Interval(5, 10, 20).shift(25, 40, Dir::Downward),
            State::Interval(20, 0, 5)
        );
        assert_eq!(
            State::Interval(5, 10, 20).shift(30, 40, Dir::Downward),
            State::Point(0)
        );
    }

    #[test]
    fn test_get() {
        assert_eq!(State::Interval(5, 10, 20).get(0, 40), 10);
        assert_eq!(State::Interval(5, 10, 20).get(5, 40), 10);
        assert_eq!(State::Interval(5, 10, 20).get(10, 40), 15);
        assert_eq!(State::Interval(5, 10, 20).get(20, 40), 25);
        assert_eq!(State::Interval(5, 10, 20).get(25, 40), 30);
        assert_eq!(State::Interval(5, 10, 20).get(35, 40), 30);
        assert_eq!(State::Interval(5, 10, 20).get(40, 40), 30);
    }
}