atcoder#ABC271B. [ABC271B] Maintain Multiple Sequences

[ABC271B] Maintain Multiple Sequences

Score : 200200 points

Problem Statement

There are NN sequences of integers. The ii-th (1iN)(1 \leq i \leq N) sequence has LiL_i terms; the jj-th (1jLi)(1 \leq j \leq L_i) term of the ii-th sequence is ai,ja_{i, j}.

You are given QQ queries. For the kk-th (1kQ)(1 \leq k \leq Q) query, given integers sks_k and tkt_k, find the tkt_k-th term of the sks_k-th sequence.

Constraints

  • 1N,Q2×1051 \leq N, Q \leq 2 \times 10^5
  • Li1(1iN)L_i \geq 1 \, (1 \leq i \leq N)
  • i=1NLi2×105\sum_{i=1}^N L_i \leq 2 \times 10^5
  • $1 \leq a_{i, j} \leq 10^9 \, (1 \leq i \leq N, 1 \leq j \leq L_i)$
  • $1 \leq s_k \leq N, 1 \leq t_k \leq L_{s_k} \, (1 \leq k \leq Q)$
  • All values in the input are integers.

Input

The input is given from Standard Input in the following format:

NN QQ

L1L_1 a1,1a_{1, 1} \ldots a1,L1a_{1, L_1}

\vdots

LNL_N aN,1a_{N, 1} \ldots aN,LNa_{N, L_N}

s1s_1 t1t_1

\vdots

sQs_Q tQt_Q

Output

Print QQ lines. The kk-th (1kQ)(1 \leq k \leq Q) line should contain the answer to the kk-th query.

2 2
3 1 4 7
2 5 9
1 3
2 1
7
5

The 11-st sequence is (1,4,7)(1, 4, 7) and the 22-nd is (5,9)(5, 9). The answer to each query is as follows:

  • The 33-rd term of the 11-st sequence is 77.
  • The 11-st term of the 22-nd sequence is 55.
3 4
4 128 741 239 901
2 1 1
3 314 159 26535
1 1
2 2
3 3
1 4
128
1
26535
901