#abc254e. [abc254_e]Small d and k

[abc254_e]Small d and k

Problem Statement

We have a simple undirected graph with NN vertices and MM edges. The vertices are numbered 1,ldots,N1,\\ldots,N. For each i=1,ldots,Mi=1,\\ldots,M, the ii-th edge connects Vertex aia_i and Vertex bib_i. Additionally, the degree of each vertex is at most 33.

For each i=1,ldots,Qi=1,\\ldots,Q, answer the following query.

  • Find the sum of indices of vertices whose distances from Vertex xix_i are at most kik_i.

Constraints

  • 1leqNleq1.5times1051 \\leq N \\leq 1.5 \\times 10^5
  • $0 \\leq M \\leq \\min (\\frac{N(N-1)}{2},\\frac{3N}{2})$
  • 1leqailtbileqN1 \\leq a_i \\lt b_i \\leq N
  • (ai,bi)neq(aj,bj)(a_i,b_i) \\neq (a_j,b_j), if ineqji\\neq j.
  • The degree of each vertex in the graph is at most 33.
  • 1leqQleq1.5times1051 \\leq Q \\leq 1.5 \\times 10^5
  • 1leqxileqN1 \\leq x_i \\leq N
  • 0leqkileq30 \\leq k_i \\leq 3
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

NN MM a1a_1 b1b_1 vdots\\vdots aMa_M bMb_M QQ x1x_1 k1k_1 vdots\\vdots xQx_Q kQk_Q

Output

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


Sample Input 1

6 5
2 3
3 4
3 5
5 6
2 6
7
1 1
2 2
2 0
2 3
4 1
6 0
4 3

Sample Output 1

1
20
2
20
7
6
20

For the 11-st query, the only vertex whose distance from Vertex 11 is at most 11 is Vertex 11, so the answer is 11.
For the 22-nd query, the vertices whose distances from Vertex 22 are at most 22 are Vertex 22, 33, 44, 55, and 66, so the answer is their sum, 2020.
The 33-rd and subsequent queries can be answered similarly.