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.15.1) (judge verdict: Wrong Answer)

#![allow(unused_imports, unused_variables, dead_code)]
use std::io::*;
use std::fmt::*;
use std::str::*;
use std::cmp::*;
use std::collections::*;

pub trait InputValue {
    fn parse(s: &str) -> Self;
}

pub fn read<T: InputValue>() -> T {
    let mut buf = String::new();
    let _ = stdin().read_line(&mut buf);
    T::parse(&buf.trim())
}

pub fn readnc<T: InputValue>() -> Vec<T> {
    let mut vec = vec![];
    let line: String = read();
    for token in line.split_whitespace() {
        vec.push(T::parse(token));
    }
    vec
}

pub fn readn<T: InputValue>(n: usize) -> Vec<T> {
    let mut vec = vec![];
    for _ in 0..n {
        vec.push(read());
    }
    vec
}

macro_rules! parse_single_value {
    ($($t:ty),*) => {
        $(
            impl InputValue for $t {
                fn parse(s: &str) -> $t { s.parse().unwrap() }
            }
        )*
	}
}
parse_single_value!(i32, i64, f32, f64, usize, String);

macro_rules! parse_tuple {
	($($t:ident),*) => {
		impl<$($t),*> InputValue for ($($t),*) where $($t: InputValue),* {
			fn parse(s: &str) -> ($($t),*) {
				let mut tokens = s.split_whitespace();
				let t = ($($t::parse(tokens.next().unwrap())),*);
				t
			}
		}
	}
}
parse_tuple!(A, B);
parse_tuple!(A, B, C);

// ===

struct Query {
    time: i32,
    initial: i32
}

fn main() {
    let x: i32 = read();
    let k: usize = read();
    let mut reverse_at: Vec<i32> = readnc();
    reverse_at.insert(0, 0);
    reverse_at.push(1000000010);

    let q: usize = read();
    let queries: Vec<(i32, i32)> = readn(q);
    let queries: Vec<Query> = queries.into_iter().map(|(a, b)| {
        Query { time: a, initial: b }
    }).collect();

    let mut min_sand = 0;
    let mut max_sand = x;
    let mut argmin = 0;
    let mut argmax = x;

    let mut answers: Vec<i32> = vec![0; q];

    let mut head: usize = 0;
    let mut incr = -1;
    for ti in 1..k+2 {
        let next_reverse = reverse_at[ti];
        let prev_reverse = reverse_at[ti-1];
        while head < q && queries[head].time < next_reverse {
            let q = &queries[head];
            let diff = q.time - prev_reverse;
            let at0= if q.initial < argmin {
                min_sand
            } else if argmax < q.initial {
                max_sand
            } else {
                min_sand + q.initial - argmin
            };

            let to = min(x, max(0, at0 + diff * incr));
            answers[head] = to;
            head += 1;
        }

        let diff = next_reverse - prev_reverse;
        let to_min_sand = min_sand + diff * incr;
        let to_max_sand = max_sand + diff * incr;

        if to_min_sand < 0 {
            argmin -= to_min_sand;
        }
        if to_max_sand > x {
            argmax -= to_max_sand - x;
        }
        min_sand = max(0, to_min_sand);
        max_sand = min(x, to_max_sand);
        if min_sand == max_sand {
            argmin = x;
            argmax = x;
        }
        incr *= -1;
    }

    for a in answers {
        println!("{}", a);
    }
}