func_code_string: #include <bits/stdc++.h>   using namespace std;   template <typename T>   using minpq = priority_queue<T, vector<T>, greater<T>>;   struct node {     int val, le, ri;     node() : val(1e9 + 5), le(0), ri(0) {}   };   const int N = 1e5 + 5;   int n, r, m, p, q, u, v, a[N], depth[N], root[N], tin[N], tout[N], ti = 1;   vector<int> adj[N];   bool vis[N];   vector<node> tree;   void dfs(int x) {     vis[x] = true;     tin[x] = ti++;     for (int y : adj[x]) {       if (!vis[y]) {         depth[y] = 1 + depth[x];         dfs(y);       }     }     tout[x] = ti;   }   int build(int l, int r) {     int k = ((int)(tree).size());     tree.emplace_back();     if (l < r) {       int m = (l + r) / 2;       tree[k].le = build(l, m);       tree[k].ri = build(m + 1, r);       tree[k].val = min(tree[tree[k].le].val, tree[tree[k].ri].val);     }     return k;   }   int upd(int i, int l, int r, int idx, int x) {     int k = ((int)(tree).size());     tree.emplace_back();     if (l < r) {       int m = (l + r) / 2;       if (idx <= m) {         tree[k].le = upd(tree[i].le, l, m, idx, x);         tree[k].ri = tree[i].ri;       } else {         tree[k].le = tree[i].le;         tree[k].ri = upd(tree[i].ri, m + 1, r, idx, x);       }       tree[k].val = min(tree[tree[k].le].val, tree[tree[k].ri].val);     } else {       tree[k].val = x;     }     return k;   }   int query(int i, int l, int r, int L, int R) {     if (r < L || R < l) return 1e9 + 5;     if (L <= l && r <= R) return tree[i].val;     int m = (l + r) / 2;     return min(query(tree[i].le, l, m, L, R), query(tree[i].ri, m + 1, r, L, R));   }   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     cin >> n >> r;     for (int i = (1); i < (n + 1); i++) {       cin >> a[i];     }     for (int i = (0); i < (n - 1); i++) {       cin >> u >> v;       adj[u].push_back(v);       adj[v].push_back(u);     }     dfs(r);     vector<pair<int, int>> ve;     for (int i = (1); i < (n + 1); i++) {       ve.emplace_back(depth[i], i);     }     sort((ve).begin(), (ve).end());     root[0] = build(1, n);     int prevdepth = 0;     for (auto &p : ve) {       int u = p.second;       root[depth[u]] = upd(root[prevdepth], 1, n, tin[u], a[u]);       prevdepth = depth[u];     }     cin >> m;     int last = 0;     while (m--) {       cin >> p >> q;       int x = 1 + ((p + last) % n);       int k = (q + last) % n;       last = query(root[min(prevdepth, depth[x] + k)], 1, n, tin[x], tout[x] - 1);       cout << last <<   n ;     }   }   