#include <bits/stdc++.h>   using namespace std;   mt19937 rd(time(0));   vector<long long> v(100010), val(100010), con[100010], st(100010, 0),       en(100010, 0), d(100010, 0);   pair<long long, long long> t[20][100010];   long long cur = 0;   void dfs(long long node, long long par, long long dep) {     st[node] = ++cur;     d[st[node]] = dep;     for (auto i : con[node]) {       if (i != par) dfs(i, node, dep + 1);     }     en[node] = cur;   }   void build(long long c, long long l, long long r) {     if (l == r)       t[c][l] = {d[l], val[l]};     else {       long long mid = (l + r) / 2;       build(c + 1, l, mid);       build(c + 1, mid + 1, r);       long long i = l, j = mid + 1, k = l;       while (i <= mid && j <= r) {         if (t[c + 1][i].first < t[c + 1][j].first)           t[c][k++] = t[c + 1][i++];         else           t[c][k++] = t[c + 1][j++];       }       while (i <= mid) t[c][k++] = t[c + 1][i++];       while (j <= r) t[c][k++] = t[c + 1][j++];       for (i = l + 1; i <= r; i++)         t[c][i].second = min(t[c][i].second, t[c][i - 1].second);     }   }   long long query(long long c, long long l, long long r, long long l1,                   long long r1, long long k) {     if (r1 < l || l1 > r) return 1000000007;     if (l >= l1 && r <= r1) {       pair<long long, long long> temp = {k, 1000000007};       long long w = upper_bound(t[c] + l, t[c] + r + 1, temp) - (t[c]);       if (w == l) return 1000000007;       return t[c][w - 1].second;     } else {       long long mid = (l + r) / 2;       return min(query(c + 1, l, mid, l1, r1, k),                  query(c + 1, mid + 1, r, l1, r1, k));     }   }   signed main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     long long n, r, m, l, x, k;     cin >> n >> r;     for (long long i = 1; i < n + 1; i++) cin >> v[i];     for (long long i = 0; i < n - 1; i++) {       long long u, v;       cin >> u >> v;       con[u].push_back(v);       con[v].push_back(u);     }     dfs(r, -1, 0);     for (long long i = 1; i <= n; i++) val[st[i]] = v[i];     build(1, 1, cur);     cin >> m;     l = 0;     while (m--) {       cin >> x >> k;       x = (x + l) % n + 1;       k = (k + l) % n;       l = query(1, 1, cur, st[x], en[x], k + d[st[x]]);       cout << l << endl;     }     return 0;   }   