func_code_string: #include <bits/stdc++.h>   using namespace std;   constexpr int inf = numeric_limits<int>::max() >> 1;   constexpr int N = 100001;   constexpr int mod = 1000000007;   int h[N], tin[N], tout[N], ap[N], n, root, a[N], T;   int mx[N * 4], mn[N * 4], ml[N * 4];   vector<int> g[N];   void dfs(int u, int p) {     tin[u] = ++T;     ap[T] = u;     h[u] = h[p] + 1;     for (int v : g[u])       if (v != p) dfs(v, u);     tout[u] = T;   }   void build(int id, int l, int r) {     if (l == r) {       mx[id] = h[ap[l]];       mn[id] = a[ap[l]];       ml[id] = h[ap[l]];       return;     }     build((id << 1), l, (l + r) / 2);     build(((id << 1) + 1), (l + r) / 2 + 1, r);     mx[id] = max(mx[(id << 1)], mx[((id << 1) + 1)]);     mn[id] = min(mn[(id << 1)], mn[((id << 1) + 1)]);     ml[id] = min(ml[(id << 1)], ml[((id << 1) + 1)]);     return;   }   int query(int id, int l, int r, int L, int R, int dep) {     if (R < l || r < L) return inf;     if (ml[id] > dep) return inf;     if (L <= l && r <= R && mx[id] <= dep) return mn[id];     return min(query((id << 1), l, (l + r) / 2, L, R, dep),                query(((id << 1) + 1), (l + r) / 2 + 1, r, L, R, dep));   }   void solution() {     cin >> n >> root;     for (int i = 1; i <= n; i++) cin >> a[i];     for (int i = 1; i < n; i++) {       int u, v;       cin >> u >> v;       g[u].push_back(v);       g[v].push_back(u);     }     dfs(root, 0);     build(1, 1, n);     int m;     cin >> m;     int last = 0;     while (m--) {       int p, q;       cin >> p >> q;       p = 1 + ((p + last) % n);       q = (q + last) % n;       last = query(1, 1, n, tin[p], tout[p], h[p] + q);       cout << last <<   n ;     }   }   signed main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     int tc = 1;     while (tc--) {       solution();       cout <<   n ;     }   }   