#ABC196E. [ABC196E] Filters

[ABC196E] Filters

Score : 500500 points

Problem Statement

Given are integer sequences A=(a1,a2,,aN)A = (a_1, a_2, \dots, a_N), T=(t1,t2,,tN)T = (t_1, t_2, \dots, t_N), and X=(x1,x2,,xQ)X = (x_1, x_2, \dots, x_Q). Let us define NN functions f1(x),f2(x),,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,,Qi = 1, 2, \dots, Q, find fN(f2(f1(xi)))f_N( \dots f_2(f_1(x_i)) \dots ).

Constraints

  • All values in input are integers.
  • 1N2×1051 \leq N \leq 2 \times 10^5
  • 1Q2×1051 \leq Q \leq 2 \times 10^5
  • ai109|a_i| \leq 10^9
  • 1ti31 \leq t_i \leq 3
  • xi109|x_i| \leq 10^9

Input

Input is given from Standard Input in the following format:

NN

a1a_1 t1t_1

a2a_2 t2t_2

\vdots

aNa_N tNt_N

QQ

x1x_1 x2x_2 \cdots xQx_Q

Output

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

3
-10 2
10 1
10 3
5
-15 -10 -5 0 5
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