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-----
180
3
60 120 180
3
30 90
61 1
180 180

-----Sample Output-----
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.

Difficulty: VERY_HARD

Solutions:
import bisect
oX = int(input())
K = int(input())
r = list(map(int, input().split()))

def calc_time(t):
	val = bisect.bisect_left(t, r)
	diff = t - r[val]
	return (val, diff)
last = (0, oX, 0, 0, oX)
res = [last]
for (i, tr) in enumerate(r):
	(sep1, sep2, X, Y, Z) = last
	dt = tr if i == 0 else tr - r[i - 1]
	if i % 2 == 0:
		X = max(0, X - dt)
		Y = Y - dt
		Z = max(0, Z - dt)
		sep1 = max(sep1, -Y)
	else:
		X = min(oX, X + dt)
		Y = Y + dt
		Z = min(oX, Z + dt)
		sep2 = min(sep2, oX - Y)
	last = (sep1, sep2, X, Y, Z)
	res.append(last)
Q = int(input())
r_index = 0
r.insert(0, 0)
r.append(10000000000)
for i in range(Q):

	def calc(X, r_index, diff):
		tmp = 1 if r_index % 2 else -1
		return min(oX, max(0, X + tmp * diff))
	(t, a) = list(map(int, input().split()))
	while r[r_index] < t:
		if r[r_index + 1] > t:
			break
		r_index += 1
	(sep1, sep2, X, Y, Z) = res[r_index]
	diff = t - r[r_index]
	if a < sep1:
		print(calc(X, r_index, diff))
	elif a > sep2:
		print(calc(Z, r_index, diff))
	else:
		print(calc(a + Y, r_index, diff))


import sys

def read_int_list():
	return list(map(int, input().split()))

def read_int():
	return int(input())

def read_str_list():
	return input().split()

def read_str():
	return input()

def solve():
	X = read_int()
	K = read_int()
	r = read_int_list()
	Q = read_int()
	j = 0
	sign = -1
	s = 0
	e = X
	y = 0
	origin = 0
	out = [0] * Q
	for i in range(Q):
		(t, a) = read_int_list()
		while j < K and r[j] < t:
			d = r[j] - origin
			y += d * sign
			if y < 0:
				s += abs(y)
				if s > e:
					s = e
				y = 0
			if y + e - s > X:
				diff = y + e - s - X
				e -= diff
				if e < s:
					e = s
			if y > X:
				y = X
			origin = r[j]
			j += 1
			sign *= -1
		d = t - origin
		if a <= s:
			res = y
		elif a <= e:
			res = y + a - s
		else:
			res = y + e - s
		res += d * sign
		if res < 0:
			res = 0
		if res > X:
			res = X
		out[i] = res
	return out

def main():
	res = solve()
	print(*res, sep='\n')
main()


from collections import deque
X = int(input())
K = int(input())
r = list(map(int, input().split()))
r.append(float('inf'))
r.append(0)
ta = deque([])
Q = int(input())
for i in range(Q):
	(t, a) = map(int, input().split())
	ta.append([t, a])
ZA = 0
XA = X
D = 0
Zmax = 0
Xmin = X
for i in range(K + 1):
	time = r[i] - r[i - 1]
	if i % 2 == 0:
		while len(ta) > 0 and ta[0][0] <= r[i]:
			(t, a) = ta.popleft()
			td = t - r[i - 1]
			if a <= Zmax:
				print(max(0, ZA - td))
			elif a >= Xmin:
				print(max(0, XA - td))
			else:
				print(max(0, a + D - td))
		D -= time
		Zmax = max(Zmax, -1 * D)
		ZA = max(0, ZA - time)
		XA = max(0, XA - time)
	else:
		while len(ta) > 0 and ta[0][0] <= r[i]:
			(t, a) = ta.popleft()
			td = t - r[i - 1]
			if a <= Zmax:
				print(min(X, ZA + td))
			elif a >= Xmin:
				print(min(X, XA + td))
			else:
				print(min(X, a + D + td))
		D += time
		Xmin = min(Xmin, X - D)
		ZA = min(X, ZA + time)
		XA = min(X, XA + time)


x = int(input())
k = int(input())
(*R,) = map(int, input().split() + [1 << 30])
Q = int(input())
ta = [tuple(map(int, input().split())) for _ in range(Q)]

def f(x, l):
	return max(l[0], min(l[1], x + l[2]))
L = (a, b, c) = (0, x, 0)
idx = t0 = 0
g = -1
for r in R:
	while idx < Q and ta[idx][0] < r:
		print(f(f(ta[idx][1], L) + g * (ta[idx][0] - t0), (0, x, 0)))
		idx += 1
	(d, t0) = (g * (r - t0), r)
	L = (a, b, c) = (f(a, (0, x, d)), f(b, (0, x, d)), c + d)
	g *= -1


X = int(input())
K = int(input())
rs = list(map(int, input().split()))
Q = int(input())
qs = [tuple(map(int, input().split())) for i in range(Q)]
dx = -1
ri = 0
offset = 0
(upper, lower) = (X, 0)
prev_r = 0
ans = []
for (t, a) in qs:
	while ri < len(rs) and rs[ri] <= t:
		tmp_offset = dx * (rs[ri] - prev_r)
		if dx == 1:
			upper = min(X, upper + tmp_offset)
			lower = min(X, lower + tmp_offset)
		else:
			upper = max(0, upper + tmp_offset)
			lower = max(0, lower + tmp_offset)
		offset += tmp_offset
		dx *= -1
		prev_r = rs[ri]
		ri += 1
	a = max(lower, min(upper, a + offset))
	dt = t - prev_r
	a = max(0, min(X, a + dx * dt))
	ans.append(a)
print(*ans, sep='\n')


def calc_ans(x):
	if x < 0:
		return 0
	elif x < X:
		return x
	else:
		return X
X = int(input())
K = int(input())
r = list(map(int, input().split()))
Q = int(input())
j = 0
sign = -1
s = 0
e = X
y = 0
sand_quantity = [r[0]]
for i in range(1, K):
	sand_quantity.append(r[i] - r[i - 1])
chasm_time = 0
for i in range(Q):
	(t, a) = list(map(int, input().split()))
	while j < K and r[j] < t:
		y += sign * sand_quantity[j]
		if y < 0:
			s += -y
			if e < s:
				s = e
			y = 0
		if X < y + e - s:
			tmp_diff = y + e - s - X
			e -= tmp_diff
			if e < s:
				e = s
		if X < y:
			y = X
		chasm_time = r[j]
		j += 1
		sign *= -1
	tmp_time = t - chasm_time
	if a < s:
		ret = y
	elif a < e:
		ret = y + a - s
	else:
		ret = y + e - s
	ret += tmp_time * sign
	print(calc_ans(ret))