Under construction

Still being built. Things may change

Tree and Query 15

Time limit5sMemory limit1024 MB

Summary
For each query, count the tree vertices within distance r_i of at least one of the k given vertices v_i.
Level

Hard9 of 10

Topics
Tree, Divide and conquer, BFS
Solved
No attempts yet

Problem

There is a tree with NN vertices. The vertices are numbered from 1 to NN. Every edge has weight 1.

Write a program that handles the queries below.

  • k v1 r1 v2 r2 ... vk rk: A vertex xx satisfies condition ii if its distance to viv_i is at most rir_i. Print the number of vertices in the tree that satisfy at least one of the kk conditions in the query.

Input

The first line contains an integer NN (1≤N≤100,0001 \le N \le 100{,}000). The next N−1N-1 lines each contain two vertices uu and vv connected by an edge (1≤u,v≤N1 \le u, v \le N).

The next line contains an integer MM (1≤M≤300,0001 \le M \le 300{,}000). Each query does not fit on one line. Each query is split across k+1k+1 lines, as shown in the sample input. Constraints: 1≤vi≤N1 \le v_i \le N, 0≤ri<N0 \le r_i < N, 1≤k1 \le k.

The sum of all kk values in the queries does not exceed 300000.

Output

For each query, print the result on its own line.

Examples1

  1. Example 1

    Input
    10
    1 3
    6 4
    9 8
    1 8
    3 4
    2 8
    10 3
    4 5
    8 7
    2
    3
    8 1
    3 1
    3 2
    2
    7 3
    6 0
    
    Expected output
    10
    7