atcoder#ABC233H. [ABC233Ex] Manhattan Christmas Tree

[ABC233Ex] Manhattan Christmas Tree

Score : 600600 points

Problem Statement

There are NN Christmas trees in the two-dimensional plane. The ii-th tree is at coordinates (xi,yi)(x_i,y_i).

Answer the following QQ queries.

Query ii: What is the distance between (ai,bi)(a_i,b_i) and the KiK_i-th nearest Christmas tree to that point, measured in Manhattan distance?

Constraints

  • 1N1051\leq N \leq 10^5
  • 0xi1050\leq x_i\leq 10^5
  • 0yi1050\leq y_i\leq 10^5
  • (xi,yi)(xj,yj)(x_i,y_i) \neq (x_j,y_j) if iji\neq j.
  • 1Q1051\leq Q \leq 10^5
  • 0ai1050\leq a_i\leq 10^5
  • 0bi1050\leq b_i\leq 10^5
  • 1KiN1\leq K_i\leq N
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

NN

x1x_1 y1y_1

\vdots

xNx_N yNy_N

QQ

a1a_1 b1b_1 K1K_1

\vdots

aQa_Q bQb_Q KQK_Q

Output

Print QQ lines. The ii-th line should contain the answer to Query ii.

4
3 3
4 6
7 4
2 5
6
3 5 1
3 5 2
3 5 3
3 5 4
100 200 3
300 200 1
1
2
2
5
293
489

The distances from (3,5)(3,5) to the 11-st, 22-nd, 33-rd, 44-th trees to that point are 22, 22, 55, 11, respectively. Thus, the answers to the first four queries are 11, 22, 22, 55, respectively.