#P1606F. Tree Queries

Tree Queries

Description

You are given a tree consisting of nn vertices. Recall that a tree is an undirected connected acyclic graph. The given tree is rooted at the vertex 11.

You have to process qq queries. In each query, you are given a vertex of the tree vv and an integer kk.

To process a query, you may delete any vertices from the tree in any order, except for the root and the vertex vv. When a vertex is deleted, its children become the children of its parent. You have to process a query in such a way that maximizes the value of c(v)mkc(v) - m \cdot k (where c(v)c(v) is the resulting number of children of the vertex vv, and mm is the number of vertices you have deleted). Print the maximum possible value you can obtain.

The queries are independent: the changes you make to the tree while processing a query don't affect the tree in other queries.

The first line contains one integer nn (1n21051 \le n \le 2 \cdot 10^5) — the number of vertices in the tree.

Then n1n-1 lines follow, the ii-th of them contains two integers xix_i and yiy_i (1xi,yin1 \le x_i, y_i \le n; xiyix_i \ne y_i) — the endpoints of the ii-th edge. These edges form a tree.

The next line contains one integer qq (1q21051 \le q \le 2 \cdot 10^5) — the number of queries.

Then qq lines follow, the jj-th of them contains two integers vjv_j and kjk_j (1vjn1 \le v_j \le n; 0kj21050 \le k_j \le 2 \cdot 10^5) — the parameters of the jj-th query.

For each query, print one integer — the maximum value of c(v)mkc(v) - m \cdot k you can achieve.

Input

The first line contains one integer nn (1n21051 \le n \le 2 \cdot 10^5) — the number of vertices in the tree.

Then n1n-1 lines follow, the ii-th of them contains two integers xix_i and yiy_i (1xi,yin1 \le x_i, y_i \le n; xiyix_i \ne y_i) — the endpoints of the ii-th edge. These edges form a tree.

The next line contains one integer qq (1q21051 \le q \le 2 \cdot 10^5) — the number of queries.

Then qq lines follow, the jj-th of them contains two integers vjv_j and kjk_j (1vjn1 \le v_j \le n; 0kj21050 \le k_j \le 2 \cdot 10^5) — the parameters of the jj-th query.

Output

For each query, print one integer — the maximum value of c(v)mkc(v) - m \cdot k you can achieve.

Sample Input 1

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

Sample Output 1

5
2
1
4
0
4

Note

The tree in the first example is shown in the following picture:

Answers to the queries are obtained as follows:

  1. v=1,k=0v=1,k=0: you can delete vertices 77 and 33, so the vertex 11 has 55 children (vertices 22, 44, 55, 66, and 88), and the score is 520=55 - 2 \cdot 0 = 5;
  2. v=1,k=2v=1,k=2: you can delete the vertex 77, so the vertex 11 has 44 children (vertices 33, 44, 55, and 66), and the score is 412=24 - 1 \cdot 2 = 2.
  3. v=1,k=3v=1,k=3: you shouldn't delete any vertices, so the vertex 11 has only one child (vertex 77), and the score is 103=11 - 0 \cdot 3 = 1;
  4. v=7,k=1v=7,k=1: you can delete the vertex 33, so the vertex 77 has 55 children (vertices 22, 44, 55, 66, and 88), and the score is 511=45 - 1 \cdot 1 = 4;
  5. v=5,k=0v=5,k=0: no matter what you do, the vertex 55 will have no children, so the score is 00;
  6. v=7,k=200000v=7,k=200000: you shouldn't delete any vertices, so the vertex 77 has 44 children (vertices 33, 44, 55, and 66), and the score is 40200000=44 - 0 \cdot 200000 = 4.