#abc196e. [abc196_e]Filters

[abc196_e]Filters

Problem Statement

Given are integer sequences A=(a1,a2,dots,aN)A = (a_1, a_2, \\dots, a_N), T=(t1,t2,dots,tN)T = (t_1, t_2, \\dots, t_N), and X=(x1,x2,dots,xQ)X = (x_1, x_2, \\dots, x_Q).
Let us define NN functions f1(x),f2(x),dots,fN(x)f_1(x), f_2(x), \\dots, f_N(x) as follows:

$f_i(x) = \\begin{cases} x + a_i & (t_i = 1)\\\\ \\max(x, a_i) & (t_i = 2)\\\\ \\min(x, a_i) & (t_i = 3)\\\\ \\end{cases}$

For each i=1,2,dots,Qi = 1, 2, \\dots, Q, find fN(dotsf2(f1(xi))dots)f_N( \\dots f_2(f_1(x_i)) \\dots ).

Constraints

  • All values in input are integers.
  • 1N2times1051 ≤ N ≤ 2 \\times 10^5
  • 1Q2times1051 ≤ Q ≤ 2 \\times 10^5
  • ai109|a_i| ≤ 10^9
  • 1ti31 ≤ t_i ≤ 3
  • xi109|x_i| ≤ 10^9

Input

Input is given from Standard Input in the following format:

NN a1a_1 t1t_1 a2a_2 t2t_2 vdots\\vdots aNa_N tNt_N QQ x1x_1 x2x_2 cdots\\cdots xQx_Q

Output

Print QQ lines. The ii-th line should contain fN(dotsf2(f1(xi))dots)f_N( \\dots f_2(f_1(x_i)) \\dots ).


Sample Input 1

3
-10 2
10 1
10 3
5
-15 -10 -5 0 5

Sample Output 1

0
0
5
10
10

We have $f_1(x) = \\max(x, -10), f_2(x) = x + 10, f_3(x) = \\min(x, 10)$, thus:

  • f3(f2(f1(15)))=0f_3(f_2(f_1(-15))) = 0
  • f3(f2(f1(10)))=0f_3(f_2(f_1(-10))) = 0
  • f3(f2(f1(5)))=5f_3(f_2(f_1(-5))) = 5
  • f3(f2(f1(0)))=10f_3(f_2(f_1(0))) = 10
  • f3(f2(f1(5)))=10f_3(f_2(f_1(5))) = 10