description: Omkar is hosting tours of his country, Omkarland! There are n cities in Omkarland, and, rather curiously, there are exactly n-1 bidirectional roads connecting the cities to each other. It is guaranteed that you can reach any city from any other city through the road network.
  
  Every city has an enjoyment value e. Each road has a capacity c, denoting the maximum number of vehicles that can be on it, and an associated toll t. However, the toll system in Omkarland has an interesting quirk: if a vehicle travels on multiple roads on a single journey, they pay only the highest toll of any single road on which they traveled. (In other words, they pay max t over all the roads on which they traveled.) If a vehicle traverses no roads, they pay 0 toll.
  
  Omkar has decided to host q tour groups. Each tour group consists of v vehicles starting at city x. (Keep in mind that a tour group with v vehicles can travel only on roads with capacity ≥ v.) Being the tour organizer, Omkar wants his groups to have as much fun as they possibly can, but also must reimburse his groups for the tolls that they have to pay. Thus, for each tour group, Omkar wants to know two things: first, what is the enjoyment value of the city y with maximum enjoyment value that the tour group can reach from their starting city, and second, how much per vehicle will Omkar have to pay to reimburse the entire group for their trip from x to y? (This trip from x to y will always be on the shortest path from x to y.)
  
  In the case that there are multiple reachable cities with the maximum enjoyment value, Omkar will let his tour group choose which one they want to go to. Therefore, to prepare for all possible scenarios, he wants to know the amount of money per vehicle that he needs to guarantee that he can reimburse the group regardless of which city they choose.
  
  Input
  
  The first line contains two integers n and q (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ q ≤ 2 ⋅ 10^5), representing the number of cities and the number of groups, respectively.
  
  The next line contains n integers e_1, e_2, …, e_n (1 ≤ e_i ≤ 10^9), where e_i represents the enjoyment value for city i.
  
  The next n-1 lines each contain four integers a, b, c, and t (1 ≤ a,b ≤ n, 1 ≤ c ≤ 10^9, 1 ≤ t ≤ 10^9), representing an road between city a and city b with capacity c and toll t.
  
  The next q lines each contain two integers v and x (1 ≤ v ≤ 10^9, 1 ≤ x ≤ n), representing the number of vehicles in the tour group and the starting city, respectively.
  
  Output
  
  Output q lines. The i-th line should contain two integers: the highest possible enjoyment value of a city reachable by the i-th tour group, and the amount of money per vehicle Omkar needs to guarantee that he can reimburse the i-th tour group.
  
  Examples
  
  Input
  
  
  5 3
  2 2 3 3 3
  1 2 4 7
  1 3 2 8
  2 4 8 2
  2 5 1 1
  1 3
  9 5
  6 2
  
  
  Output
  
  
  3 8
  3 0
  3 2
  
  
  Input
  
  
  5 5
  1 2 3 4 5
  1 2 4 1
  1 3 3 1
  1 4 2 1
  2 5 1 1
  5 1
  4 1
  3 1
  2 1
  1 1
  
  
  Output
  
  
  1 0
  2 1
  3 1
  4 1
  5 1
  
  
  Input
  
  
  5 5
  1 2 2 2 2
  1 2 5 8
  1 3 6 3
  1 4 4 5
  1 5 7 1
  4 1
  5 1
  6 1
  7 1
  8 1
  
  
  Output
  
  
  2 8
  2 8
  2 3
  2 1
  1 0
  
  Note
  
  A map of the first sample is shown below. For the nodes, unbolded numbers represent indices and bolded numbers represent enjoyment values. For the edges, unbolded numbers represent capacities and bolded numbers represent tolls.
  
  <image>
  
  For the first query, a tour group of size 1 starting at city 3 can reach cities 1, 2, 3, 4, and 5. Thus, the largest enjoyment value that they can reach is 3. If the tour group chooses to go to city 4, Omkar will have to pay 8 per vehicle, which is the maximum.
  
  For the second query, a tour group of size 9 starting at city 5 can reach only city 5. Thus, the largest reachable enjoyment value is still 3, and Omkar will pay 0 per vehicle.
  
  For the third query, a tour group of size 6 starting at city 2 can reach cities 2 and 4. The largest reachable enjoyment value is again 3. If the tour group chooses to go to city 4, Omkar will have to pay 2 per vehicle, which is the maximum.
  
  A map of the second sample is shown below:
  
  <image>
  
  For the first query, a tour group of size 5 starting at city 1 can only reach city 1. Thus, their maximum enjoyment value is 1 and the cost Omkar will have to pay is 0 per vehicle.
  
  For the second query, a tour group of size 4 starting at city 1 can reach cities 1 and 2. Thus, their maximum enjoyment value is 2 and Omkar will pay 1 per vehicle.
  
  For the third query, a tour group of size 3 starting at city 1 can reach cities 1, 2, and 3. Thus, their maximum enjoyment value is 3 and Omkar will pay 1 per vehicle.
  
  For the fourth query, a tour group of size 2 starting at city 1 can reach cities 1, 2, 3 and 4. Thus, their maximum enjoyment value is 4 and Omkar will pay 1 per vehicle.
  
  For the fifth query, a tour group of size 1 starting at city 1 can reach cities 1, 2, 3, 4, and 5. Thus, their maximum enjoyment value is 5 and Omkar will pay 1 per vehicle.
solution: #include <bits/stdc++.h>
  using namespace std;
  mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
  const long long max_n = 200005, max_q = 200005;
  long long timer = 0;
  long long n, q;
  long long enj[max_n];
  long long a[max_n], b[max_n], c[max_n], t[max_n];
  vector<pair<long long, long long> > adj[max_n];
  long long sz[max_q], st[max_q];
  long long tin[max_n], tout[max_n], depth[max_n];
  pair<long long, long long> jump[max_n][25];
  long long dsu[max_n], max_enjoyment[max_n], max_pos[max_n], max_inside[max_n];
  vector<pair<long long, long long> > queries;
  pair<long long, long long> ans[max_q];
  bool custom_comp(pair<long long, long long> a_, pair<long long, long long> b_) {
    long long cap_a, cap_b;
    if (a_.first == 1)
      cap_a = c[a_.second];
    else
      cap_a = sz[a_.second];
    if (b_.first == 1)
      cap_b = c[b_.second];
    else
      cap_b = sz[b_.second];
    if (cap_a != cap_b) return cap_a > cap_b;
    return a_.first < b_.first;
  }
  void dfs(long long u, long long p) {
    depth[u] = depth[p] + 1;
    tin[u] = timer++;
    for (long long i = 1; i < 20; i++) {
      jump[u][i].first = jump[jump[u][i - 1].first][i - 1].first;
      jump[u][i].second =
          max(jump[u][i - 1].second, jump[jump[u][i - 1].first][i - 1].second);
    }
    for (auto e : adj[u]) {
      long long v = e.first, w = e.second;
      if (v == p) continue;
      jump[v][0] = make_pair(u, w);
      dfs(v, u);
    }
    tout[u] = timer++;
  }
  bool anc(long long u, long long v) {
    return (tin[u] <= tin[v] && tout[u] >= tout[v]);
  }
  long long lca(long long u, long long v) {
    if (anc(u, v)) return u;
    if (anc(v, u)) return v;
    for (long long i = 19; i >= 0; i--) {
      if (!anc(jump[u][i].first, v)) u = jump[u][i].first;
    }
    return jump[u][0].first;
  }
  long long query_path(long long u, long long v) {
    long long l = lca(u, v);
    long long du = depth[u] - depth[l], dv = depth[v] - depth[l];
    long long mx = 0;
    for (long long i = 19; i >= 0; i--) {
      if ((du >> i) & 1) {
        mx = max(mx, jump[u][i].second);
        u = jump[u][i].first;
      }
      if ((dv >> i) & 1) {
        mx = max(mx, jump[v][i].second);
        v = jump[v][i].first;
      }
    }
    return mx;
  }
  long long find_root(long long u) {
    if (dsu[u] < 0) return u;
    return dsu[u] = find_root(dsu[u]);
  }
  void join(long long u, long long v) {
    u = find_root(u);
    v = find_root(v);
    if (dsu[u] > dsu[v]) swap(u, v);
    dsu[u] += dsu[v];
    dsu[v] = u;
    if (max_enjoyment[u] < max_enjoyment[v]) {
      max_enjoyment[u] = max_enjoyment[v];
      max_pos[u] = max_pos[v];
      max_inside[u] = max_inside[v];
    } else if (max_enjoyment[u] == max_enjoyment[v]) {
      max_inside[u] = max(max(max_inside[u], max_inside[v]),
                          query_path(max_pos[u], max_pos[v]));
    }
  }
  pair<long long, long long> query(long long start) {
    long long root = find_root(start);
    return make_pair(max_enjoyment[root],
                     max(max_inside[root], query_path(start, max_pos[root])));
  }
  int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin >> n >> q;
    for (long long i = 1; i <= n; i++) cin >> enj[i];
    for (long long i = 1; i < n; i++) {
      cin >> a[i] >> b[i] >> c[i] >> t[i];
      adj[a[i]].emplace_back(b[i], t[i]);
      adj[b[i]].emplace_back(a[i], t[i]);
      queries.emplace_back(1, i);
    }
    for (long long i = 1; i <= q; i++) {
      cin >> sz[i] >> st[i];
      queries.emplace_back(2, i);
    }
    tin[0] = -1;
    dfs(1, 0);
    tout[0] = tout[1] + 1;
    for (long long i = 1; i <= n; i++) {
      dsu[i] = -1;
      max_enjoyment[i] = enj[i];
      max_pos[i] = i;
    }
    sort(queries.begin(), queries.end(), custom_comp);
    for (auto qr : queries) {
      long long type = qr.first, id = qr.second;
      if (type == 1)
        join(a[id], b[id]);
      else
        ans[id] = query(st[id]);
    }
    for (long long i = 1; i <= q; i++)
      cout << ans[i].first << " " << ans[i].second << "\n";
  }
  