Andrii is good in Math, but not in Programming. He is asking you to solve following problem: Given an integer number N and two sets of integer A and B. Let set A contain all numbers from 1 to N and set B contain all numbers from N + 1 to 2N. Multiset C contains all sums a + b such that a belongs to A and b belongs to B. Note that multiset may contain several elements with the same values. For example, if N equals to three, then A = {1, 2, 3}, B = {4, 5, 6} and C = {5, 6, 6, 7, 7, 7, 8, 8, 9}. Andrii has M queries about multiset C. Every query is defined by a single integer q. Andrii wants to know the number of times q is contained in C. For example, number 6 is contained two times, 1 is not contained in C at all. 

Please, help Andrii to answer all the queries.

-----Input-----

The first line of the input contains two integers N and M. Each of the next M line contains one integer q, the query asked by Andrii.

-----Output-----
Output the answer for each query in separate lines as in example.

-----Constraints-----
- 1 ≤ N ≤ 109
- 1 ≤ M ≤ 105
- 1 ≤ q ≤ 3N

-----Example-----
Input:
3 5
6
2
9
7
5
Output:
2
0
1
3
1

Difficulty: MEDIUM
Tags: Mathematics

Starter code:

Solutions:
input_arr = str(input()).split(' ')
N = int(input_arr[0])
M = int(input_arr[1])
for m in range(M):
	q = int(input())
	a = N + 1
	b = 2 * N + 1
	c = 2 * N + 1 + (N - 1)
	if q <= a:
		print(0)
	elif q <= b:
		print(q - N - 1)
	elif q <= c:
		print(c - q + 1)
	else:
		print(0)


import sys
import math
import bisect
import numpy as np
from sys import stdin, stdout
from itertools import accumulate, chain, combinations, groupby, permutations, product
from collections import deque, Counter
from bisect import bisect_left, bisect_right
from math import gcd, sqrt, sin, cos, tan, degrees, radians
from fractions import Fraction
from decimal import Decimal
import sys
MOD = 10 ** 9 + 7
INF = 10 ** 20
sys.setrecursionlimit(100000000)
inp = lambda : int(input())
strng = lambda : input().strip()
jn = lambda x, l: x.join(map(str, l))
strl = lambda : list(input().strip())
mul = lambda : map(int, input().strip().split())
mulf = lambda : map(float, input().strip().split())
seq = lambda : list(map(int, input().strip().split()))
ceil = lambda x: int(x) if x == int(x) else int(x) + 1
ceildiv = lambda x, d: x // d if x % d == 0 else x // d + 1
flush = lambda : stdout.flush()
stdstr = lambda : stdin.readline()
stdint = lambda : int(stdin.readline())
stdpr = lambda x: stdout.write(str(x))

def solve():
	(n, m) = map(int, input().split())
	p = n + 2
	q = 3 * n
	m1 = 2 * n + 1
	for i in range(m):
		g = int(input())
		if g not in range(p, q + 1):
			print(0)
		else:
			print(n - abs(m1 - g))
solve()


(n, m) = map(int, input().split())
lower = n + 2
higher = n + n + n
list1 = []
for x in range(m):
	z = int(input())
	if z <= n * 2 + 1 and lower <= z < higher:
		print(z - n - 1)
	elif z > n * 2 + 1 and lower <= z < higher:
		print(n - z % n + 1)
	elif z == higher:
		print(1)
	else:
		print(0)


(n, m) = map(int, input().split())
for _ in range(m):
	q = int(input())
	if q - n - 1 > n:
		print(3 * n - q + 1)
	elif q < n + 2 or q > 3 * n:
		print(0)
	else:
		print(q - n - 1)


(n, m) = map(int, input().split())
(mn, mx) = (n + 2, n * 3)
for i in range(m):
	q = int(input())
	if mn <= q <= mx:
		mf = (mn + mx) // 2
		diff = abs(mf - q)
		print(n - diff)
	else:
		print(0)


(n, m) = map(int, input().split())
mid = n * 2 + 1
for i in range(m):
	q = int(input())
	if q < n + 2 or q > mid + n - 1:
		print(0)
	else:
		print(n - abs(mid - q))


(n, m) = map(int, input().split())
f = 1 + n + 1
l = n + 2 * n
for i in range(m):
	m1 = int(input())
	if m1 < f or m1 > l:
		print(0)
	elif m1 == f or m1 == l:
		print(1)
	else:
		print(min(m1 - f, l - m1) + 1)


(n, m) = list(map(int, input().split()))
num = 2 * n - 1
mid = num // 2 + 1
for _ in range(m):
	q = int(input())
	if n + 2 <= q and 3 * n >= q:
		d = q - n - 1
		if d <= mid:
			print(d)
		else:
			print(2 * mid - d)
	else:
		print(0)


(n, m) = map(int, input().split())
for i in range(m):
	q = int(input())
	l = max(q - n, n + 1)
	r = min(q - 1, 2 * n)
	if l > r:
		print(0)
	else:
		print(r - l + 1)


(n, m) = map(int, input().split())
maxi = n + 2 * n
mini = 2 + n
avg = (maxi + mini) // 2
for _ in range(m):
	q = int(input())
	if mini <= q <= maxi:
		if q <= avg:
			print(q - mini + 1)
		else:
			print(maxi - q + 1)
	else:
		print(0)


(n, m) = map(int, input().split())
for i in range(m):
	N = int(input())
	if N < n + 2 or N > 3 * n:
		print('0')
	else:
		print(n - abs(2 * n + 1 - N))


(n, m) = map(int, input().split())
for i in range(m):
	a = int(input())
	if a < n + 2 or a > 3 * n:
		print(0)
	elif a <= 2 * n + 1:
		print(a - n - 1)
	else:
		print(3 * n - a + 1)


ans = []
(N, M) = [int(i) for i in input().split()]
for i in range(M):
	q = int(input())
	if q <= N:
		s = 0
	elif q <= 2 * N:
		s = q - N - 1
	else:
		s = N - (q - 2 * N - 1)
	ans.append(s)
for i in ans:
	print(i)


(a, b) = map(int, input().strip().split())
for j in range(b):
	n = int(input())
	if n <= a + 1 or n > 3 * a:
		print(0)
	else:
		mi = min(n - a - 1, 3 * a - n + 1)
		print(mi)


s = input().split()
n = int(s[0])
m = int(s[1])
for i in range(m):
	q = int(input())
	count = 0
	if q <= 2 * n and q > n:
		count = q - n - 1
	if q > 2 * n and q <= 3 * n:
		count = n - (q - 2 * n - 1)
	if q == 2 * n + 1:
		count = n
	print(count)


(x, y) = map(int, input().split())
for k in range(y):
	a = int(input())
	if a < x + 2 or a > 3 * x:
		print('0')
	else:
		print(x - abs(2 * x + 1 - a))


(n, m) = map(int, input().split())
minVal = n + 2
maxVal = 3 * n
for _ in range(m):
	q = int(input())
	if q >= minVal and q <= maxVal:
		if q - minVal + 1 <= n:
			print(q - minVal + 1)
		else:
			midVal = minVal + n - 1
			print(n - (q - midVal))
	else:
		print(0)


(a, b) = map(int, input().split())
for _ in range(b):
	n = int(input())
	c = 0
	if n < a + 2 or n > 3 * a:
		print('0')
	else:
		m = 1 + a * 2
		c = a - abs(m - n)
		print(c)


(n, m) = list(map(int, input().split(' ')))
for i in range(m):
	q = int(input())
	if q < n + 2:
		print('0')
		continue
	else:
		print(n - abs(2 * n + 1 - q))


(n, m) = map(int, input().split())
for i in range(m):
	q = int(input())
	if q < n + 2:
		print(0)
	elif q >= n + 2 and q <= 2 * n:
		print(q - n - 1)
	elif q > 2 * n and q <= 3 * n:
		print(3 * n - q + 1)
	else:
		print(0)


tc = 1
for _ in range(1, 1 + tc):
	(n, m) = map(int, input().split())
	for __ in range(m):
		v = int(input())
		ans = 0
		if v < n + 2 or v > n + 2 * n:
			print(ans)
			continue
		half = v // 2
		if v % 2 == 0:
			i = half - 1
			j = half + 1
		else:
			i = half
			j = half + 1
		ans += min(i, 1 + 2 * n - j) + min(j - n, n - i + 1) - 1
		print(ans)


(n, m) = map(int, input().split())
start = n + 2
end = 3 * n
mid = (start + end) / 2
for i in range(m):
	q = int(input())
	if q > end or q < start:
		print(0)
	elif q < mid:
		print(q - start + 1)
	else:
		print(end - q + 1)


(x, y) = map(int, input().split())
for i in range(y):
	lund = int(input())
	if lund <= 3 * x and lund >= x + 1:
		print(x - abs(2 * x + 1 - lund))
	else:
		print(0)


(n, m) = map(int, input().split())
l = n + 2
r = 3 * n
mid = (l + r) // 2
for i in range(m):
	q = int(input())
	if q not in range(n + 2, 3 * n + 1):
		print(0)
	elif q == mid:
		print(n)
	elif q < mid:
		print(q - n - 1)
	else:
		print(3 * n - q + 1)


(n, m) = map(int, input().split())
f1 = n + 2
f2 = n + 2 * n
for _ in range(m):
	q = int(input())
	if q >= f1 and q <= f2:
		fx = n + n + 1
		v = abs(q - fx)
		print(n - v)
	else:
		print(0)


from collections import defaultdict

def def_value():
	return 0
(n, m) = map(int, input().split())
for i in range(m):
	x = int(input())
	if x <= n + 1 or x > 3 * n:
		print(0)
	elif x <= 2 * n + 1:
		print(x % (n + 1))
	else:
		print(3 * n + 1 - x)


(a, b) = map(int, input().split())
x = a + 2
y = 3 * a
for i in range(b):
	m = int(input())
	if m < x or m > y:
		print('0')
	elif m == x or m == y:
		print('1')
	else:
		print(min(m - x, y - m) + 1)


from collections import Counter
import numpy as np
(n, m) = map(int, input().split())
for i in range(m):
	inp = int(input())
	if inp <= n + 1 or inp > 3 * n + 1:
		print(0)
	elif inp > n + 1 and inp <= 2 * n + 1:
		print(inp - n - 1)
	else:
		print(n - (inp - (2 * n + 1)))


from collections import Counter, UserString
mod = lambda n: n % (10 ** 9 + 7)
fc_ = [1]
for i in range(1, 1000001):
	fc_.append(i * fc_[i - 1])
	fc_[i] = mod(fc_[i])

def fact(n):
	return fc_[n]

def printm(n):
	print(mod(n))

def isPrime(n):
	if n > 1:
		for i in range(2, int(n ** 0.5) + 1):
			if not n % i:
				return False
		else:
			return True
	return True

class mutableStr(UserString):

	def ins(self, n, s):
		if n < 0:
			self.data = self.data = ''.join(list(self.data)[:n + 1]) + s + ''.join(list(self.data)[n + 1:])
		else:
			self.data = ''.join(list(self.data)[:n]) + s + ''.join(list(self.data)[n:])

	def rem(self, s):
		self.data = self.data.replace(s, '')
alphabetOrd = [97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122]
alphabet = list('abcdefghijklmnopqrstuvwxyz')
r1 = lambda n: range(n)
r2 = lambda n: range(1, n + 1)
alph = lambda s: s.isalpha()
sl = lambda l: list(set(l))
inf = float('inf')
soi = lambda : sorted(input())
ii = lambda : int(input())
li = lambda : list(map(int, input().split()))
si = lambda : mutableStr(input())
lsi = lambda : list(input())
sepi = lambda : map(int, input().split())
seti = lambda : set(map(int, input().split()))
(n, m) = sepi()
l1 = range(1, n + 1)
l2 = range(n + 1, 2 * n)
for i in r2(m):
	q = ii()
	mi = abs(q - l2[0] - l1[-1])
	print(max(n - mi, 0))


(N, M) = map(int, input().split())
for _ in range(M):
	q = int(input())
	k = q - N
	if k <= 0:
		print(0)
	else:
		print(min(k - 1, N) - max(k - N, 1) + 1)


(n, m) = map(int, input().split())
max_number = n * 2 + 1
max_value = n
min_limit = n + 1
max_limit = n * 3 + 1
for _ in range(m):
	query = int(input())
	if query <= min_limit or query >= max_limit:
		print(0)
	else:
		difference = abs(max_number - query)
		print(max_value - difference)


(n, m) = map(int, input().split())
for i in range(m):
	q = int(input())
	ma = n + (n + 1)
	max_count = n
	count = 0
	if q < n + 2 or q > 2 * n + n:
		print(0)
	else:
		count = max_count - abs(ma - q)
		print(count)


(n, m) = map(int, input().split())
for _ in range(m):
	a = int(input())
	if a < n + 2 or a > 3 * n:
		print(0)
	else:
		mid = (n + 2 + 3 * n) // 2
		print(n - abs(mid - a))


(n, m) = map(int, input().split())
for i in range(m):
	x = int(input())
	avg = (4 * n + 2) // 2
	if x in range(n + 2, 3 * n + 1):
		ans = n - abs(avg - x)
		print(ans)
	else:
		print(0)


(n, q) = map(int, input().split())
for i in range(q):
	num = int(input())
	if num < n + 2 or num > 3 * n:
		print('0')
	else:
		print(n - abs(2 * n + 1 - num))


import math
from math import gcd, sqrt, floor, factorial, ceil
from bisect import bisect_left, bisect_right
import bisect
import sys
from sys import stdin, stdout
import os
sys.setrecursionlimit(pow(10, 7))
import collections
from collections import defaultdict, Counter
from statistics import median
from queue import Queue
inf = float('inf')
from operator import neg
(n, m) = map(int, input().split())
for i in range(m):
	k = int(input())
	print(max(0, min(k - n - 1, 3 * n + 1 - k)))


(n, m) = map(int, input().split())
for _ in range(m):
	q = int(input())
	if q < n + 2 or q > 2 * n + n:
		print(0)
		continue
	else:
		low = n + 2
		high = 2 * n + n
		count = 1 + min(abs(q - low), abs(q - high))
		print(count)


(N, M) = map(int, input().split())
for i in range(M):
	k = int(input())
	if k < N + 2 or k > 3 * N:
		print(0)
	else:
		print(N - abs(k - 2 * N - 1))


(N, M) = input().split()
N = int(N)
M = int(M)
first = N + 2
last = 3 * N
mid = (first + last) // 2
for i in range(0, M):
	a = int(input())
	if a > last or a < first:
		print(0)
	elif a < mid:
		print(a - first + 1)
	elif a > mid:
		print(last - a + 1)
	elif a == mid:
		print(N)


(n, m) = map(int, input().split())
first = 1 + n + 1
last = n + 2 * n
for i in range(m):
	a = int(input())
	if a < first or a > last:
		print(0)
	elif a == first or a == last:
		print(1)
	else:
		print(min(a - first, last - a) + 1)


(n, m) = map(int, input().split())
for _ in range(m):
	ele = int(input())
	if ele < n + 1 or ele > n + n * 2:
		print(0)
	else:
		min_val = 1 + n + 1
		max_val = n + n * 2
		ans = min(ele - min_val, max_val - ele) + 1
		print(ans)


(a, b) = map(int, input().split())
for _ in range(b):
	no = int(input())
	if 2 * a < no:
		print(a - (no - 2 * a - 1))
	elif no > a:
		print(min(a, no - a - 1))
	else:
		print(0)


(n, m) = map(int, input().split())
for k in range(m):
	q = int(input())
	if n + 2 <= q <= 3 * n:
		if q <= 2 * n:
			print(q - n - 1)
		else:
			print(3 * n - q + 1)
	else:
		print(0)


from bisect import bisect_left, bisect_right
import math
from bisect import bisect_left
from heapq import heapify, heappush, heappop
from collections import deque
import sys
from math import gcd, sqrt, log10, log2, floor, ceil, sqrt
from collections import deque, defaultdict
input = sys.stdin.readline

def il():
	return [int(a) for a in input().strip().split()]

def ip():
	return input().strip()

def ii():
	return int(input())

def ot(in_p, *args, e=' '):
	if type(in_p) == list:
		print(*in_p, end=e)
	if type(in_p) == str or type(in_p) == int:
		print(in_p, end=e)
		for l in args:
			print(l, end=e)
	print()

def a_pwr_n(a, n, mod):
	if n == 1:
		return a % mod
	if n % 2 == 0:
		val = a_pwr_n(a, n // 2, mod)
		return val % mod * val % mod
	else:
		return a % mod * a_pwr_n(a, n - 1, mod) % mod

def rg(*ag):
	if len(ag) == 3:
		return range(ag[0], ag[1], ag[2])
	elif len(ag) == 2:
		return range(ag[0], ag[1])
	elif len(ag) == 1:
		return range(ag[0])
	else:
		raise 'invalid input'

def ispal(i, j, st):
	while i <= j:
		if st[i] == st[j]:
			i = i + 1
			j = j - 1
		else:
			return False
	return True
mod = 1000000000 + 7
dx = [-1, -2, 1, 2, 2, -2, -1, 1]
dy = [2, 1, 2, 1, -1, -1, -2, -2]
(n, m) = il()
(min, max) = (1 + n + 1, n + 2 * n)
mid = (min + max) / 2
for _ in range(m):
	t = ii()
	if t == mid:
		print(n)
	elif t < mid and t >= min:
		print(t - min + 1)
	elif t > mid and t <= max:
		print(max - t + 1)
	else:
		print(0)


data = list(map(int, input().split()))
mylist = []
for i in range(data[1]):
	check = int(input())
	if check > 3 * data[0] or check < data[0] + 2:
		print(0)
	else:
		print(data[0] - abs(check - 2 * data[0] - 1))


(n, m) = list(map(int, input().split()))

def ans(n, q):
	if q not in range(n + 1, 3 * n + 1):
		return 0
	at_max = 2 * n + 1
	diverge = abs(q - at_max)
	return n - diverge
for _ in range(m):
	q = int(input())
	print(ans(n, q))


(n, m) = map(int, input().split())
for _ in range(m):
	q = int(input())
	k = q - n
	if k <= 0:
		print(0)
	else:
		print(min(k - 1, n) - max(k - n, 1) + 1)


(n, m) = map(int, input().split())
for _ in range(m):
	q = int(input())
	if q < n + 2:
		print(0)
		continue
	print(max(0, n - abs(q - 2 * n - 1)))


(n, m) = map(int, input().split())
ma = n * 2 + 1
for i in range(m):
	x = int(input())
	if x > n + 1:
		z = abs(ma - x)
		print(n - z)
	else:
		print(0)


(n, m) = map(int, input().split())
for _ in range(m):
	p = int(input())
	if p < n + 2:
		print(0, end='\n')
	elif p <= 2 * n + 1:
		print(p - n - 1, end='\n')
	else:
		print(3 * n - p + 1, end='\n')


(n, m) = map(int, input().split())
start = n + 2
end = n * 3
for _ in range(m):
	q = int(input())
	if q < start or q > end:
		print(0)
	else:
		print(min(end - q, q - start) + 1)


(n, m) = [int(i) for i in input().split()]
for i in range(m):
	q = int(input())
	if q <= 3 * n and q >= n + 2:
		x = n + 2
		mx = n
		ans = abs(x - q) + 1
		if ans > mx:
			ans = mx - (ans - mx)
	else:
		ans = 0
	print(ans)


(n, m) = map(int, input().split())
for j in range(m):
	q = int(input())
	if q < n + 2 or q > 3 * n:
		print(0)
	else:
		print(n - abs(2 * n + 1 - q))


(N, M) = list(map(int, input().split()))
mn = 2 + N
mx = N + 2 * N
for _ in range(M):
	q = int(input())
	if q > mx or q < mn:
		print(0)
		continue
	print(min(abs(mx - q), abs(mn - q)) + 1)


from sys import stdin, stdout

def problem():
	(n, m) = [int(x) for x in stdin.readline().split()]
	for _ in range(m):
		q = int(stdin.readline())
		l = max(q - n, n + 1)
		r = min(q - 1, 2 * n)
		if r >= l:
			print(r - l + 1)
		else:
			print(0)
problem()


(N, M) = [int(s) for s in input().split()]
for _ in range(M):
	q = int(input())
	print(max(min(q - N - 1, 3 * N + 1 - q), 0))