#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
const int INF = 0x3F3F3F3F;
vector<int> G[N];
int n, m, cnt;
int a[N], sz[N], fa[N], dfn[N];
long long ans[N];
bool del[N];
set<pair<int, int> > S;
struct rec {
  int x, k, id;
  bool operator<(const rec &b) const { return k < b.k; }
} q[N];
namespace FastIn {
char Buf[1 << 22], *S(Buf), *T(Buf);
inline int ReadInt() {
  int x = 0, sign = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') {
      sign = -1;
    }
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = (x << 1) + (x << 3) + (ch ^ 48);
    ch = getchar();
  }
  return x * sign;
}
}  // namespace FastIn
void DFS(int x, int pre) {
  fa[x] = pre;
  dfn[x] = ++cnt;
  sz[x] = 1, a[x] = -1;
  for (auto y : G[x]) {
    if (y != pre) {
      ++a[x], DFS(y, x);
      sz[x] += sz[y];
    }
  }
}
struct DSU {
  int pre[N];
  void Init(int sz) {
    for (int i = 1; i <= sz; ++i) {
      pre[i] = i;
    }
  }
  inline int find(int x) {
    if (x == pre[x]) {
      return x;
    } else {
      return pre[x] = find(pre[x]);
    }
  }
  inline void merge(int x, int y) { pre[find(x)] = find(y); }
} DS;
struct BIT {
  long long dt[N];
  inline void update(int x, long long v) {
    for (; x <= n; x += x & -x) {
      dt[x] += v;
    }
  }
  inline long long query(int x) {
    long long ret = 0;
    for (; x; x -= x & -x) {
      ret += dt[x];
    }
    return ret;
  }
} T[2];
void GetTop(long long &v, long long &id) {
  if (S.empty()) {
    id = v = INF;
    return;
  }
  int x = S.begin()->second;
  while (del[x]) {
    S.erase(S.begin());
    if (S.empty()) {
      id = v = INF;
      return;
    }
    x = S.begin()->second;
  }
  id = x, v = S.begin()->first;
}
inline long long Get(int x, bool tp) {
  return T[tp].query(dfn[x] + sz[x] - 1) - T[tp].query(dfn[x] - 1);
}
inline void update(int x) {
  S.insert(make_pair(ceil((long double)-Get(x, 0) / Get(x, 1)), x));
}
inline long long query(int x, int k) { return Get(x, 0) + Get(x, 1) * k; }
int main() {
  n = FastIn ::ReadInt();
  for (int i = 1; i < n; ++i) {
    int x = FastIn ::ReadInt();
    int y = FastIn ::ReadInt();
    G[x].push_back(y);
    G[y].push_back(x);
  }
  DS.Init(n), DFS(1, 0);
  m = FastIn ::ReadInt();
  for (int i = 1; i <= m; ++i) {
    q[i].x = FastIn ::ReadInt();
    q[i].k = -FastIn ::ReadInt();
    q[i].id = i;
  }
  sort(q + 1, q + m + 1);
  for (int i = 1; i <= n; ++i) {
    T[0].update(dfn[i], a[i]);
    T[1].update(dfn[i], 1);
    if (fa[i]) {
      T[0].update(dfn[fa[i]], -a[i]);
      T[1].update(dfn[fa[i]], -1);
    }
    if (i > 1) {
      S.insert(make_pair(-a[i], i));
    }
  }
  for (int i = 1; i <= m; ++i) {
    long long v, x, sz, dt, nxt;
    GetTop(v, x);
    while (v <= q[i].k) {
      sz = Get(x, 1);
      dt = Get(x, 0);
      nxt = DS.find(fa[x]);
      DS.merge(x, fa[x]);
      S.erase(S.begin());
      del[x] = 1;
      T[0].update(dfn[fa[x]], dt);
      T[1].update(dfn[fa[x]], sz);
      if (fa[nxt]) {
        T[0].update(dfn[fa[nxt]], -dt);
        T[1].update(dfn[fa[nxt]], -sz);
        update(nxt);
      }
      GetTop(v, x);
    }
    ans[q[i].id] =
        max(query(q[i].x, q[i].k) - q[i].k + 1, (long long)a[q[i].x] + 1);
  }
  for (int i = 1; i <= m; ++i) {
    printf("%lld\n", ans[i]);
  }
  return 0;
}

### Prompt

Your task is to create a cpp solution to the following problem:

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

You have to process q queries. In each query, you are given a vertex of the tree v and an integer k.

To process a query, you may delete any vertices from the tree in any order, except for the root and the vertex v. 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) - m ⋅ k (where c(v) is the resulting number of children of the vertex v, and m 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.

Input

The first line contains one integer n (1 ≤ n ≤ 2 ⋅ 10^5) — the number of vertices in the tree.

Then n-1 lines follow, the i-th of them contains two integers x_i and y_i (1 ≤ x_i, y_i ≤ n; x_i ≠ y_i) — the endpoints of the i-th edge. These edges form a tree.

The next line contains one integer q (1 ≤ q ≤ 2 ⋅ 10^5) — the number of queries.

Then q lines follow, the j-th of them contains two integers v_j and k_j (1 ≤ v_j ≤ n; 0 ≤ k_j ≤ 2 ⋅ 10^5) — the parameters of the j-th query.

Output

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

Example

Input


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


Output


5
2
1
4
0
4

Note

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

<image>

Answers to the queries are obtained as follows:

  1. v=1,k=0: you can delete vertices 7 and 3, so the vertex 1 has 5 children (vertices 2, 4, 5, 6, and 8), and the score is 5 - 2 ⋅ 0 = 5; 
  2. v=1,k=2: you can delete the vertex 7, so the vertex 1 has 4 children (vertices 3, 4, 5, and 6), and the score is 4 - 1 ⋅ 2 = 2. 
  3. v=1,k=3: you shouldn't delete any vertices, so the vertex 1 has only one child (vertex 7), and the score is 1 - 0 ⋅ 3 = 1; 
  4. v=7,k=1: you can delete the vertex 3, so the vertex 7 has 5 children (vertices 2, 4, 5, 6, and 8), and the score is 5 - 1 ⋅ 1 = 4; 
  5. v=5,k=0: no matter what you do, the vertex 5 will have no children, so the score is 0; 
  6. v=7,k=200000: you shouldn't delete any vertices, so the vertex 7 has 4 children (vertices 3, 4, 5, and 6), and the score is 4 - 0 ⋅ 200000 = 4. 

### Response

```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
const int INF = 0x3F3F3F3F;
vector<int> G[N];
int n, m, cnt;
int a[N], sz[N], fa[N], dfn[N];
long long ans[N];
bool del[N];
set<pair<int, int> > S;
struct rec {
  int x, k, id;
  bool operator<(const rec &b) const { return k < b.k; }
} q[N];
namespace FastIn {
char Buf[1 << 22], *S(Buf), *T(Buf);
inline int ReadInt() {
  int x = 0, sign = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') {
      sign = -1;
    }
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = (x << 1) + (x << 3) + (ch ^ 48);
    ch = getchar();
  }
  return x * sign;
}
}  // namespace FastIn
void DFS(int x, int pre) {
  fa[x] = pre;
  dfn[x] = ++cnt;
  sz[x] = 1, a[x] = -1;
  for (auto y : G[x]) {
    if (y != pre) {
      ++a[x], DFS(y, x);
      sz[x] += sz[y];
    }
  }
}
struct DSU {
  int pre[N];
  void Init(int sz) {
    for (int i = 1; i <= sz; ++i) {
      pre[i] = i;
    }
  }
  inline int find(int x) {
    if (x == pre[x]) {
      return x;
    } else {
      return pre[x] = find(pre[x]);
    }
  }
  inline void merge(int x, int y) { pre[find(x)] = find(y); }
} DS;
struct BIT {
  long long dt[N];
  inline void update(int x, long long v) {
    for (; x <= n; x += x & -x) {
      dt[x] += v;
    }
  }
  inline long long query(int x) {
    long long ret = 0;
    for (; x; x -= x & -x) {
      ret += dt[x];
    }
    return ret;
  }
} T[2];
void GetTop(long long &v, long long &id) {
  if (S.empty()) {
    id = v = INF;
    return;
  }
  int x = S.begin()->second;
  while (del[x]) {
    S.erase(S.begin());
    if (S.empty()) {
      id = v = INF;
      return;
    }
    x = S.begin()->second;
  }
  id = x, v = S.begin()->first;
}
inline long long Get(int x, bool tp) {
  return T[tp].query(dfn[x] + sz[x] - 1) - T[tp].query(dfn[x] - 1);
}
inline void update(int x) {
  S.insert(make_pair(ceil((long double)-Get(x, 0) / Get(x, 1)), x));
}
inline long long query(int x, int k) { return Get(x, 0) + Get(x, 1) * k; }
int main() {
  n = FastIn ::ReadInt();
  for (int i = 1; i < n; ++i) {
    int x = FastIn ::ReadInt();
    int y = FastIn ::ReadInt();
    G[x].push_back(y);
    G[y].push_back(x);
  }
  DS.Init(n), DFS(1, 0);
  m = FastIn ::ReadInt();
  for (int i = 1; i <= m; ++i) {
    q[i].x = FastIn ::ReadInt();
    q[i].k = -FastIn ::ReadInt();
    q[i].id = i;
  }
  sort(q + 1, q + m + 1);
  for (int i = 1; i <= n; ++i) {
    T[0].update(dfn[i], a[i]);
    T[1].update(dfn[i], 1);
    if (fa[i]) {
      T[0].update(dfn[fa[i]], -a[i]);
      T[1].update(dfn[fa[i]], -1);
    }
    if (i > 1) {
      S.insert(make_pair(-a[i], i));
    }
  }
  for (int i = 1; i <= m; ++i) {
    long long v, x, sz, dt, nxt;
    GetTop(v, x);
    while (v <= q[i].k) {
      sz = Get(x, 1);
      dt = Get(x, 0);
      nxt = DS.find(fa[x]);
      DS.merge(x, fa[x]);
      S.erase(S.begin());
      del[x] = 1;
      T[0].update(dfn[fa[x]], dt);
      T[1].update(dfn[fa[x]], sz);
      if (fa[nxt]) {
        T[0].update(dfn[fa[nxt]], -dt);
        T[1].update(dfn[fa[nxt]], -sz);
        update(nxt);
      }
      GetTop(v, x);
    }
    ans[q[i].id] =
        max(query(q[i].x, q[i].k) - q[i].k + 1, (long long)a[q[i].x] + 1);
  }
  for (int i = 1; i <= m; ++i) {
    printf("%lld\n", ans[i]);
  }
  return 0;
}

```