description: 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. 
solution: #include <bits/stdc++.h>
  using namespace std;
  int n, q, V, c[200015], par[200015], ans[200015], dfn[200015], siz[200015],
      son[200015], dep[200015], clk, top[200015];
  vector<int> e[200015];
  vector<pair<int, int>> que[200015];
  struct node {
    int c, m;
    bool operator<(const node &rhs) const {
      return (long long)(c - 1) * (rhs.m + 1) > (long long)(rhs.c - 1) * (m + 1);
    }
    node operator+(const node &rhs) const { return (node){c + rhs.c, m + rhs.m}; }
  };
  node sum[200015];
  namespace seg {
  node tr[200015 << 2], lazy[200015 << 2];
  void down(int p, node v) {
    tr[p] = tr[p] + v;
    lazy[p] = lazy[p] + v;
  }
  void pushdown(int p) {
    down((p << 1), lazy[p]);
    down((p << 1 | 1), lazy[p]);
    lazy[p] = (node){0, 0};
  }
  void add(int p, int l, int r, int x, int y, node v) {
    if (x <= l && r <= y) return down(p, v);
    pushdown(p);
    int mid = (l + r) >> 1;
    if (x <= mid) add((p << 1), l, mid, x, y, v);
    if (y > mid) add((p << 1 | 1), mid + 1, r, x, y, v);
  }
  node query(int p, int l, int r, int pos) {
    if (l == r) return tr[p];
    pushdown(p);
    int mid = (l + r) >> 1;
    if (pos <= mid)
      return query((p << 1), l, mid, pos);
    else
      return query((p << 1 | 1), mid + 1, r, pos);
  }
  };  // namespace seg
  using namespace seg;
  set<pair<node, int>> S;
  int fa[200015];
  int find(int x) { return x == fa[x] ? x : fa[x] = find(fa[x]); }
  void dfs(int u, int pa) {
    par[u] = pa;
    dep[u] = dep[pa] + 1;
    siz[u] = 1;
    for (auto v : e[u])
      if (v != pa) {
        c[u]++;
        dfs(v, u);
        siz[u] += siz[v];
        if (!son[u] || siz[v] > siz[son[u]]) son[u] = v;
      }
  }
  void dfs2(int u, int pa, int tp) {
    dfn[u] = ++clk;
    top[u] = tp;
    if (son[u]) dfs2(son[u], u, tp);
    for (auto v : e[u]) {
      if (v == pa || v == son[u]) continue;
      dfs2(v, u, v);
    }
  }
  void add(int u, int v, node val) {
    while (top[u] != top[v]) {
      add(1, 1, n, dfn[top[u]], dfn[u], val);
      u = par[top[u]];
    }
    add(1, 1, n, dfn[v], dfn[u], val);
  }
  int main() {
    scanf("%d", &n);
    for (int i = (1); i <= (n); i++) fa[i] = i;
    for (int i = (2); i <= (n); i++) {
      int u, v;
      scanf("%d%d", &u, &v);
      e[u].push_back(v);
      e[v].push_back(u);
    }
    dfs(1, 0);
    dfs2(1, 0, 1);
    for (int i = (1); i <= (n); i++)
      sum[i] = (node){c[i], 0}, add(1, 1, n, dfn[i], dfn[i], sum[i]);
    scanf("%d", &q);
    for (int i = (1); i <= (q); i++) {
      int v, k;
      scanf("%d%d", &v, &k);
      que[k].push_back(make_pair(v, i));
      V = max(V, k);
    }
    for (int i = (1); i <= (n); i++) S.insert(make_pair(sum[i], i));
    for (int k = (V); k >= (0); k--) {
      while (((int)S.size()) > 0) {
        if (S.begin()->first.c - 1 < (long long)(S.begin()->first.m + 1) * k)
          break;
        int u = S.begin()->second;
        S.erase(S.begin());
        if (!par[u]) continue;
        int v = find(par[u]);
        S.erase(make_pair(sum[v], v));
        node val = sum[u] + (node){-1, 1};
        sum[v] = sum[v] + val;
        fa[u] = v;
        add(par[u], v, val);
        if (par[v]) S.insert(make_pair(sum[v], v));
      }
      for (auto x : que[k]) {
        node u = query(1, 1, n, dfn[x.first]);
        ans[x.second] = u.c - u.m * k;
      }
    }
    for (int i = (1); i <= (q); i++) printf("%d\n", ans[i]);
    return 0;
  }
  