func_code_string: #include <bits/stdc++.h>   using namespace std;   inline int read() {     register int res = 0, c;     while (c = getchar(), c <  0  || c >  9 )       ;     do {       res = (res << 3) + (res << 1) + (c ^ 48);     } while (c = getchar(), c >=  0  && c <=  9 );     return res;   }   const int inf = 0x3f3f3f3f;   const int maxn = 100010;   const int maxd = 8000010;   int T[maxd], s[maxn], lson[maxd], rson[maxd], val[maxd], tot;   int l[maxn], r[maxn], d[maxn];   vector<int> edge[maxn], lay[maxn];   int N, R, last, dep, dfn;   void dfs(int x, int dep) {     l[x] = ++dfn;     d[x] = dep;     ::dep = max(dep, ::dep);     lay[dep].push_back(x);     for (auto i : edge[x])       if (!l[i]) {         dfs(i, dep + 1);       }     r[x] = dfn;   }   void update(int to, int L, int R, int& rt) {     if (L == R) {       val[rt = ++tot] = s[to];       return;     }     int mid = L + R >> 1;     lson[++tot] = lson[rt];     rson[tot] = rson[rt];     rt = tot;     if (l[to] <= mid)       update(to, L, mid, lson[rt]);     else       update(to, mid + 1, R, rson[rt]);     val[rt] = min(val[lson[rt]], val[rson[rt]]);   }   int query(int l, int r, int L, int R, int rt) {     if (L >= l && R <= r) {       return val[rt];     }     int mid = L + R >> 1, res = inf;     if (mid >= l) res = query(l, r, L, mid, lson[rt]);     if (mid < r) res = min(res, query(l, r, mid + 1, R, rson[rt]));     return res;   }   int main() {     N = read(), R = read();     for (int i = 1; i <= N; i++) s[i] = read();     for (int i = 1; i < N; i++) {       int x = read(), y = read();       edge[x].push_back(y);       edge[y].push_back(x);     }     dfs(R, 1);     T[0] = lson[0] = rson[0] = 0;     val[0] = inf;     for (int d = 1; d <= dep; d++) {       T[d] = ++tot;       lson[tot] = lson[T[d - 1]];       rson[tot] = rson[T[d - 1]];       for (auto i : lay[d]) update(i, 1, N, T[d]);     }     int Q = read();     while (Q--) {       int X = (last + read()) % N + 1, K = (read() + last) % N;       printf( %d n , last = query(l[X], r[X], 1, N, T[min(dep, d[X] + K)]));     }   }   