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;
  mt19937_64 rnd(time(NULL));
  const int N = 1000010;
  const long long mod = 998244353;
  vector<int> G[N];
  vector<int> g[N];
  int p[N];
  int tin[N], tout[N];
  int timer = 0;
  void dfs0(int v, int pr) {
    p[v] = pr;
    tin[v] = ++timer;
    for (int to : G[v]) {
      if (to != pr) {
        g[v].push_back(to);
        dfs0(to, v);
      }
    }
    tout[v] = timer;
  }
  int n, q;
  int ans[N];
  bool good[N];
  int dsu[N];
  int get(int x) {
    if (!good[p[dsu[x]]]) {
      return dsu[x];
    }
    return dsu[x] = get(p[dsu[x]]);
  }
  int val[N];
  int sum[N], cnt[N];
  struct BIT {
    int T[N];
    void upd(int pos, int x) {
      for (; pos <= n; pos |= pos + 1) T[pos] += x;
    }
    int get(int pos) {
      int sum = 0;
      for (; pos >= 0; pos &= pos + 1, pos--) sum += T[pos];
      return sum;
    }
  };
  BIT T[2];
  void add(int l, int r, int x, int i) {
    T[i].upd(l, x);
    T[i].upd(r + 1, -x);
  }
  void solve() {
    cin >> n;
    for (int i = 1; i < n; i++) {
      int x, y;
      cin >> x >> y;
      G[x].push_back(y);
      G[y].push_back(x);
    }
    cin >> q;
    dfs0(1, 0);
    vector<pair<pair<int, int>, int> > Q;
    for (int i = 1; i <= q; i++) {
      int v, k;
      cin >> v >> k;
      Q.push_back({{k, v}, i});
    }
    sort(Q.begin(), Q.end());
    reverse(Q.begin(), Q.end());
    for (int i = 1; i <= n; i++)
      good[i] = false, dsu[i] = i, sum[i] = g[i].size() - 1, cnt[i] = 1;
    set<pair<int, int> > st;
    for (int i = 1; i <= n; i++) st.insert({sum[i] / cnt[i], i});
    for (auto cur : Q) {
      int k = cur.first.first;
      int v = cur.first.second;
      while (!st.empty()) {
        int v = (*st.rbegin()).second;
        if (sum[v] / cnt[v] < k) break;
        st.erase(--st.end());
        good[v] = true;
        int pr = p[get(v)];
        T[0].upd(tin[p[pr]], -sum[v]);
        T[0].upd(tin[p[v]], sum[v]);
        T[1].upd(tin[p[pr]], -cnt[v]);
        T[1].upd(tin[p[v]], cnt[v]);
        if (pr) {
          st.erase({sum[pr] / cnt[pr], pr});
          sum[pr] += sum[v];
          cnt[pr] += cnt[v];
          st.insert({sum[pr] / cnt[pr], pr});
        }
      }
      int res = g[v].size() + T[0].get(tout[v]) - T[0].get(tin[v] - 1) -
                (T[1].get(tout[v]) - T[1].get(tin[v] - 1)) * k;
      ans[cur.second] = res;
    }
    for (int i = 1; i <= q; i++) cout << ans[i] << '\n';
  }
  int32_t main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    int tt = 1;
    while (tt--) {
      solve();
    }
    return 0;
  }
  