Under construction

Still being built. Things may change

Tree and Queries 14

Time limit5sMemory limit1024 MB

Summary
Given a tree with unit-weight edges, each query asks how many vertices lie within distance r_i of at least k-1 of the k given centers v_i.
Level

Hard8 of 10

Topics
Tree, Divide and conquer
Solved
No attempts yet

Problem

The tree has NN vertices numbered from 1 to NN. Every edge has weight 1.

Write a program that processes the following query.

  • k v1 r1 v2 r2 ... vk rk: A vertex xx satisfies condition ii if its distance from viv_i is at most rir_i. Among all vertices in the tree, count the vertices that satisfy at least k−1k-1 of the kk conditions in the query.

Input

The first line contains NN (1≤N≤100,0001 \le N \le 100{,}000).

Each of the next N−1N-1 lines contains two vertex numbers uu and vv connected by an edge (1≤u,v≤N1 \le u, v \le N).

The next line contains MM (1≤M≤300,0001 \le M \le 300{,}000), the number of queries.

Each query spans k+1k+1 lines, not one line. The MM queries follow in this format. (1≤vi≤N1 \le v_i \le N, 0≤ri<N0 \le r_i < N, 1≤k1 \le k)

The sum of kk over all queries does not exceed 300000300000.

Output

Print the answer to each query 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
    5
    7