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;
  template <class T>
  bool chmax(T &a, const T &b) {
    if (a < b) {
      a = b;
      return 1;
    }
    return 0;
  }
  template <class T>
  bool chmin(T &a, const T &b) {
    if (a > b) {
      a = b;
      return 1;
    }
    return 0;
  }
  template <class V, int ME>
  class BIT {
   public:
    V bit[1 << ME];
    V operator()(int e) {
      if (e < 0) return 0;
      V s = 0;
      e++;
      while (e) s += bit[e - 1], e -= e & -e;
      return s;
    }
    void add(int e, V v) {
      e++;
      while (e <= 1 << ME) bit[e - 1] += v, e += e & -e;
    }
  };
  BIT<int, 20> vnum, vdel;
  template <int um>
  class UF {
   public:
    vector<int> par, rank, cnt;
    UF() {
      par = rank = vector<int>(um, 0);
      cnt = vector<int>(um, 1);
      for (int i = 0; i < um; i++) par[i] = i;
    }
    void reinit(int num = um) {
      int i;
      for (i = 0; i < (num); i++) rank[i] = 0, cnt[i] = 1, par[i] = i;
    }
    int operator[](int x) {
      return (par[x] == x) ? (x) : (par[x] = operator[](par[x]));
    }
    int count(int x) { return cnt[operator[](x)]; }
    int operator()(int x, int y) {
      if ((x = operator[](x)) == (y = operator[](y))) return x;
      cnt[y] = cnt[x] = cnt[x] + cnt[y];
      if (rank[x] > rank[y]) return par[x] = y;
      rank[x] += rank[x] == rank[y];
      return par[y] = x;
    }
  };
  UF<202020> uf;
  int N;
  int Q, V[202020], K[202020], ret[202020];
  const int ME = 300001;
  vector<int> E[ME];
  int L[ME], R[ME], D[ME], P[ME], rev[ME], eid;
  int vis[202020];
  int top[202020];
  set<int> del[202020];
  vector<int> query[202020];
  void EulerTour(int cur, int pre = 0, int d = 0) {
    if (pre == -1) memset(L, 0, sizeof(L)), memset(R, 0, sizeof(R)), eid = 0;
    rev[eid] = cur;
    P[cur] = pre;
    L[cur] = eid++;
    D[cur] = d;
    for (auto &e : E[cur])
      if (e != pre) EulerTour(e, cur, d + 1);
    R[cur] = eid;
  }
  void solve() {
    int i, j, k, l, r, x, y;
    string s;
    cin >> N;
    for (i = 0; i < (N - 1); i++) {
      cin >> x >> y;
      E[x].push_back(y);
      E[y].push_back(x);
    }
    E[0].push_back(1);
    E[1].push_back(0);
    N++;
    EulerTour(0);
    for (i = 0; i < (N); i++) {
      top[i] = L[i];
      vnum.add(L[i], E[i].size() - 1);
      if (i) vnum.add(L[P[i]], -((int)E[i].size() - 1));
      if (i > 1 && E[i].size() >= 2) del[E[i].size() - 2].insert(-L[i]);
    }
    cin >> Q;
    for (i = 0; i < (Q); i++) {
      cin >> V[i] >> K[i];
      query[K[i]].push_back(i);
    }
    for (k = 200000; k >= 0; k--) {
      while (del[k].size()) {
        int v = rev[-*del[k].begin()];
        del[k].erase(del[k].begin());
        if (vis[v]) continue;
        vis[v] = 1;
        top[uf[v]] = top[uf[P[v]]] = min(top[uf[v]], top[uf[P[v]]]);
        uf(v, P[v]);
        x = vnum(R[v] - 1) - vnum(L[v] - 1);
        i = vdel(R[v] - 1) - vdel(L[v] - 1);
        y = rev[top[uf[v]]];
        vnum.add(L[P[v]], x - 1);
        vnum.add(L[P[y]], -x + 1);
        vdel.add(L[P[v]], i + 1);
        vdel.add(L[P[y]], -(i + 1));
        if (y > 1 && vis[y] == 0) {
          x = vnum(R[y] - 1) - vnum(L[y] - 1);
          i = vdel(R[y] - 1) - vdel(L[y] - 1);
          x = max(0, min(k, (x + i) / (i + 1) - 1));
          del[x].insert(-L[y]);
        }
      }
      for (auto &q : query[k]) {
        int v = V[q];
        ret[q] = (vnum(R[v] - 1) - vnum(L[v] - 1)) -
                 1LL * k * (vdel(R[v] - 1) - vdel(L[v] - 1));
        if (N == 200000 && q == 9 && ret[q] == 1) {
          cout << vnum(R[v] - 1) << " " << vnum(L[v] - 1) << " " << k << " "
               << vdel(R[v] - 1) << " " << vdel(L[v] - 1) << endl;
          return;
        }
      }
    }
    for (i = 0; i < (Q); i++) cout << ret[i] << endl;
  }
  int main(int argc, char **argv) {
    string s;
    int i;
    if (argc == 1) ios::sync_with_stdio(false), cin.tie(0);
    for (i = 0; i < (argc - 1); i++) s += argv[i + 1], s += '\n';
    for (i = 0; i < (s.size()); i++) ungetc(s[s.size() - 1 - i], stdin);
    cout.tie(0);
    solve();
    return 0;
  }
  