#include <iostream>
#include <vector>
#include <chrono>
#include <random>

std::mt19937 rng((int) std::chrono::steady_clock::now().time_since_epoch().count());

const int me = 20;
const int ms = 1001000;

int to[me][ms], cost[ms];

int main() {
	std::ios_base::sync_with_stdio(false); std::cin.tie(NULL);
	int n, r, c;
	std::cin >> n >> r >> c;
	std::vector<std::string> words(n);
	std::vector<int> a(n);
	for(int i = 0; i < n; i++) {
		std::cin >> words[i];
		a[i] = words[i].size();
	}
	for(int i = 0; i < n; i++) {
		to[0][i] = i;
		cost[i] = -1;
		if(i && to[0][i-1] > i) {
			to[0][i] = to[0][i-1];
			cost[i] = cost[i-1] - a[i-1] - 1;
		}
		while(to[0][i] < n && cost[i] + a[to[0][i]] + 1 <= c) {
			cost[i] += a[to[0][i]++] + 1;
		}
		if(cost[i] < 0) {
			cost[i] = 0;
		}
	}
	to[0][n] = n;
	for(int i = 1; i < me; i++) {
		for(int j = 0; j < n; j++) {
			to[i][j] = to[i-1][to[i-1][j]];
		}
		to[i][n] = n;
	}
	int st = 0, en = -1;
	for(int i = 0; i < n; i++) {
		int on = i;
		for(int j = 0; j < me; j++) {
			if((1 << j) & r) {
				on = to[j][on];
			}
		}
		if(en - st < on - i) {
			en = on;
			st = i;
		}
	}
	while(st != en) {
		int on = to[0][st];
		for(; st < on; st++) {
			std::cout << words[st] << (st + 1 == on ? '\n' : ' ');
		}
	}
}


#include<bits/stdc++.h>
using namespace std;

const int N = 1e6;
const int lg = 20;

int n,r,c;
vector<string> li;
int par[N+5][lg+2];

void make_sparse(){
	for(int i=1;i<lg;i++){
		for(int j=0;j<=n;j++){
			par[j][i] = par[par[j][i-1]][i-1];
		}
	}
}

int get_uj(int x){
	for(int i=0;i<lg;i++){
		if((1<<i) & r){
			x = par[x][i];
		}
	}
	return x;
}

int main(){
	cin >> n >> r >> c;
	for(int i=0;i<n;i++){
		string s;
		cin >> s;
		li.push_back(s);
	}

	c++;
	int r = -1;
	int cur = 0;
	for(int i=0;i<n;i++){
		while(cur <= c && r<n){
			r++;
			if(r == n) break;
			cur += li[r].size() + 1;
		}
		par[i][0] = r;
		cur -= li[i].size() + 1;
//		cout << par[i][0] << endl;
	}
	par[n][0] = n;
	make_sparse();
	int ans = 0;
//	cout << "DONE\n";
	for(int i=0;i<n;i++){
//		cout << get_uj(i) << endl;
		ans = max(ans,get_uj(i) - i);
	}
	for(int i=0;i<n;i++){
		if(get_uj(i)-i == ans){
			int lol = get_uj(i);
			bool spas = 0;
			int cur = 0;
			int bef = i;
			for(int j=i;j<lol;j++){
				if(cur != 0) cout << " ";
				cout << li[j];
				cur+=li[j].size()+1;
				if(par[bef][0]-1 == j){
					cur = 0;
					cout << endl;
					bef = par[bef][0];
				}
			}
		}
	}
//	printf("%d\n",ans);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;

const int N = 1e6;
const int lg = 20;

int n,R,c;
vector<string> li;
int par[N+5][lg+2];

void make_sparse(){
	for(int i=1;i<lg;i++){
		for(int j=0;j<=n;j++){
			par[j][i] = par[par[j][i-1]][i-1];
		}
	}
}

int get_uj(int x){
	for(int i=0;i<lg;i++){
		if((1<<i) & R){
			x = par[x][i];
		}
	}
	return x;
}

int main(){
	cin >> n >> R >> c;
	for(int i=0;i<n;i++){
		string s;
		cin >> s;
		li.push_back(s);
	}

	c++;
	int r = -1;
	int cur = 0;
	for(int i=0;i<n;i++){
		while(cur <= c && r<n){
			r++;
			if(r == n) break;
			cur += li[r].size() + 1;
		}
		par[i][0] = r;
		cur -= li[i].size() + 1;
//		cout << par[i][0] << endl;
	}
	par[n][0] = n;
	make_sparse();
	int ans = 0;
//	cout << "DONE\n";
	for(int i=0;i<n;i++){
//		cout << get_uj(i) << endl;
		ans = max(ans,get_uj(i) - i);
	}
	for(int i=0;i<n;i++){
		if(get_uj(i)-i == ans){
			int lol = get_uj(i);
			bool spas = 0;
			int cur = 0;
			int bef = i;
			for(int j=i;j<lol;j++){
				if(cur != 0) cout << " ";
				cout << li[j];
				cur+=li[j].size()+1;
				if(par[bef][0]-1 == j){
					cur = 0;
					cout << endl;
					bef = par[bef][0];
				}
			}
		}
	}
//	printf("%d\n",ans);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;

const int N = 1e6;
const int lg = 20;

int n,R,c;
vector<string> li;
int par[N+5][lg+2];

void make_sparse(){
	for(int i=1;i<lg;i++){
		for(int j=0;j<=n;j++){
			par[j][i] = par[par[j][i-1]][i-1];
		}
	}
}

int get_uj(int x){
	for(int i=0;i<lg;i++){
		if((1<<i) & R){
			x = par[x][i];
		}
	}
	return x;
}

int main(){
	cin >> n >> R >> c;
	for(int i=0;i<n;i++){
		string s;
		cin >> s;
		li.push_back(s);
	}

	c++;
	int r = -1;
	int cur = 0;
	for(int i=0;i<n;i++){
		while(cur <= c && r<n){
			r++;
			if(r == n) break;
			cur += li[r].size() + 1;
		}
		par[i][0] = r;
		cur -= li[i].size() + 1;
//		cout << par[i][0] << endl;
	}
	par[n][0] = n;
	make_sparse();
	int ans = 0;
//	cout << "DONE\n";
	for(int i=0;i<n;i++){
//		cout << get_uj(i) << endl;
		ans = max(ans,get_uj(i) - i);
	}
	for(int i=0;i<n;i++){
		if(get_uj(i)-i == ans){
			int lol = get_uj(i);
			bool spas = 0;
			int cur = 0;
			int bef = i;
			for(int j=i;j<lol;j++){
				if(cur != 0) cout << " ";
				cout << li[j];
				cur+=li[j].size()+1;
				if(par[bef][0]-1 == j){
					cur = 0;
					cout << endl;
					bef = par[bef][0];
				}
			}
			break;
		}
	}
//	printf("%d\n",ans);
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int OO = 1e9;
const double EPS = 1e-9;

#define ndl cout << '\n'
#define sz(v) int(v.size())
#define pb push_back
#define mp make_pair
#define fs first
#define sc second
#define present(a, x) (a.find(x) != a.end())
#ifdef LOCAL
#define db(...) ({cout << "> Line " << __LINE__  \
		<< ": "; _db(#__VA_ARGS__, __VA_ARGS__);})
#define RNG() rng()
#else
#define db(...) true
#define RNG() true
#endif

template<class T>
void _db(const char *dbStr, T e) {
	cout << dbStr << " = " << e << endl;
}
template<class T, class... L>
void _db(const char *dbStr, T e, L... r) {
  while(*dbStr != ',') cout << *dbStr++;
  cout << " = " << e << ','; _db(dbStr + 1, r...);
}
template<class S, class T>
ostream& operator<<(ostream& o, const map<S, T>& v) {
	o << "["; int i = 0;
	for (const pair<S, T>& pr : v) o << (!i++ ? "" : ", ") << "{"
	<< pr.fs << " : " << pr.sc << "}"; return o << "]";
}
template<template <class, class...> class S, class T, class... L>
ostream& operator<<(ostream& o, const S<T, L...>& v) {
	o << "["; int i = 0;
	for (const auto& e : v) o << (!i++ ? "" : ", ") << e;
	return o << "]";
}
template<class S, class T>
ostream& operator<<(ostream& o, const pair<S, T>& pr) {
	return o << "(" << pr.fs << ", " << pr.sc << ")";
}
ostream& operator<<(ostream& o, const string& s) {
	for (const char& c : s) o << c;
	return o;
}

template<class T> using V = vector<T>;
template<class T> using VV = V<V<T>>;
template<class T> using VVV = VV<V<T>>;
using ll = long long;
using pii = pair<int, int>;
using vi = V<int>;
using vii = V<pii>;
using vvi = VV<int>;
using mii = map<int, int>;
using umii = unordered_map<int, int>;
using si = set<int>;
using usi = unordered_set<int>;

int n, r, c;
V<string> s;
vi sum;

bool good(int i, int j) {
	return sum[j] - sum[i - 1] + (j - i) > c;
}

int main() {
	#ifdef LOCAL
	auto stTime = clock();
//	freopen("in.txt", "r", stdin);
	mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
	#endif
	ios::sync_with_stdio(false);
	cout.precision(10);
	cin.tie(0);

	cin >> n >> r >> c;
	s.assign(n + 2, "");
	sum.assign(n + 2, 0);

	for (int i = 1; i <= n; ++i) {
		cin >> s[i];
		sum[i] = sum[i - 1] + sz(s[i]);
	}

	sum.back() = OO;

	vi next(n + 2, n + 1);

	for (int i = 1; i <= n; ++i) {
		int lo = i, hi = n + 1;

		while (lo < hi) {
			int mid = (lo + hi) / 2;

			if (good(i, mid))
				hi = mid;
			else
				lo = mid + 1;
		}

		db(i, lo);

		next[i] = lo;
	}

	db(sum, next);

	V<bool> done(n + 2);
	vvi ans;

	int bestIdx = -1, bestCnt = 0;

	for (int i = 1; i <= n; ++i) {
		if (next[i] != -1 && !done[i]) {
			ans.pb({});
			vi& seq = ans.back();
			int curr = i;
			seq.pb(curr);
			while (sz(seq) <= r && next[curr] != seq.back())
				curr = next[curr], seq.pb(curr), done[curr] = true;

			int score = seq.back() - seq[0];
			if (score > bestCnt)
				bestIdx = sz(ans) - 1, bestCnt = score;
		}
	}

	if (bestIdx == -1) {
		ndl;
	} else {
		for (int l = ans[bestIdx][0], r = ans[bestIdx].back(), Sz = sz(s[l]); l < r; ++l) {
			cout << s[l];
			if (l + 1 == r || (l + 1 < r && Sz + 1 + sz(s[l + 1]) > c)) {
				ndl;
				Sz = sz(s[l + 1]);
			} else {
				cout << ' ';
				Sz += 1 + sz(s[l + 1]);
			}
		}
	}

	#ifdef LOCAL
	cout << "\n\n\nExecution time: " <<
		(clock() - stTime) * 1e3 / CLOCKS_PER_SEC << " ms" << endl;
	#endif
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int OO = 1e9;
const double EPS = 1e-9;

#define ndl cout << '\n'
#define sz(v) int(v.size())
#define pb push_back
#define mp make_pair
#define fs first
#define sc second
#define present(a, x) (a.find(x) != a.end())
#ifdef LOCAL
#define db(...) ({cout << "> Line " << __LINE__  \
		<< ": "; _db(#__VA_ARGS__, __VA_ARGS__);})
#define RNG() rng()
#else
#define db(...) true
#define RNG() true
#endif

template<class T>
void _db(const char *dbStr, T e) {
	cout << dbStr << " = " << e << endl;
}
template<class T, class... L>
void _db(const char *dbStr, T e, L... r) {
  while(*dbStr != ',') cout << *dbStr++;
  cout << " = " << e << ','; _db(dbStr + 1, r...);
}
template<class S, class T>
ostream& operator<<(ostream& o, const map<S, T>& v) {
	o << "["; int i = 0;
	for (const pair<S, T>& pr : v) o << (!i++ ? "" : ", ") << "{"
	<< pr.fs << " : " << pr.sc << "}"; return o << "]";
}
template<template <class, class...> class S, class T, class... L>
ostream& operator<<(ostream& o, const S<T, L...>& v) {
	o << "["; int i = 0;
	for (const auto& e : v) o << (!i++ ? "" : ", ") << e;
	return o << "]";
}
template<class S, class T>
ostream& operator<<(ostream& o, const pair<S, T>& pr) {
	return o << "(" << pr.fs << ", " << pr.sc << ")";
}
ostream& operator<<(ostream& o, const string& s) {
	for (const char& c : s) o << c;
	return o;
}

template<class T> using V = vector<T>;
template<class T> using VV = V<V<T>>;
template<class T> using VVV = VV<V<T>>;
using ll = long long;
using pii = pair<int, int>;
using vi = V<int>;
using vii = V<pii>;
using vvi = VV<int>;
using mii = map<int, int>;
using umii = unordered_map<int, int>;
using si = set<int>;
using usi = unordered_set<int>;

int n, r, c;
V<string> s;
vi sum;
vvi adj;
vi path;
pii ans = mp(0, 0);

bool good(int i, int j) {
	return sum[j] - sum[i - 1] + (j - i) > c;
}

void dfs(int u, int p) {
	path.pb(u);
	int l = path[max(sz(path) - 1 - r, 0)] - u;
	db(u, path, l);
	if (l > ans.fs)
		ans = mp(l, u);
	for (auto& v : adj[u]) {
		if (v != p)
			dfs(v, u);
	}
	path.pop_back();
}

int main() {
	#ifdef LOCAL
	auto stTime = clock();
	freopen("in.txt", "r", stdin);
	mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
	#endif
	ios::sync_with_stdio(false);
	cout.precision(10);
	cin.tie(0);

	cin >> n >> r >> c;
	s.assign(n + 2, "");
	sum.assign(n + 2, 0);
	adj.assign(n + 2, vi());

	for (int i = 1; i <= n; ++i) {
		cin >> s[i];
		sum[i] = sum[i - 1] + sz(s[i]);
	}

	sum.back() = OO;

	vi next(n + 2, n + 1);

	for (int i = 1; i <= n; ++i) {
		int lo = i, hi = n + 1;

		while (lo < hi) {
			int mid = (lo + hi) / 2;

			if (good(i, mid))
				hi = mid;
			else
				lo = mid + 1;
		}

		db(i, lo);

		next[i] = lo;
		adj[next[i]].pb(i);
	}

	db(next);

	for (int i = 1; i <= n + 1; ++i) {
		if (next[i] == i)
			dfs(i, i);
	}

	db(ans);

	if (!ans.fs)
		ndl;
	else {
		for (int l = ans.sc, r = ans.sc + ans.fs, Sz = sz(s[l]); l < r; ++l) {
			cout << s[l];
			if (l + 1 == r || (l + 1 < r && Sz + 1 + sz(s[l + 1]) > c)) {
				ndl;
				Sz = sz(s[l + 1]);
			} else {
				cout << ' ';
				Sz += 1 + sz(s[l + 1]);
			}
		}
	}

	#ifdef LOCAL
	cout << "\n\n\nExecution time: " <<
		(clock() - stTime) * 1e3 / CLOCKS_PER_SEC << " ms" << endl;
	#endif
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

const int N = 1e6 + 5, M = 21;
int n, r, c;
string S[N];
int pre[N], par[N][M]; //PARent

int get(int i) // get the parent
{
    int l = i, r = n, ret = -2; //ret - bc we want the parents so the parent of root is needed in extra -1 to lnow that there is no ans
    while (l <= r)
    {
        int mid = (l + r) / 2;
        if (pre[mid] - pre[i - 1] + mid - i <= c)
        {
            ret = mid, l = mid + 1;
        }
        else
        {
            r = mid - 1;
        }
    }

    return ret + 1;
}

int farthest(int i) // farthest jump incase that we will use less than r lines
{
    for (int j = M - 1; j >= 0; j--)
    {
        if (~par[i][j])
        {
            i = par[i][j];
        }
    }
    return i;
}

int get_kth(int i)
{
    for (int j = 0; ~i && j < M; j++)
    {
        if (r & (1 << j))
        {
            i = par[i][j];
        }
    }
    return i;
}

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    cin >> n >> r >> c;

    for (int i = 1; i <= n; i++)
    {
        cin >> S[i];
        pre[i] = pre[i - 1] + S[i].size();
    }

    memset(par, -1, sizeof par);

    int mx = 0, pos = -1;
    for (int i = n; i > 0; i--)
    {
        par[i][0] = get(i);

        for (int j = 1; j < M; j++)
        {
            if (~par[i][j - 1])
            {
                par[i][j] = par[par[i][j - 1]][j - 1];
            }
        }

        int cur = get_kth(i);
        if (cur == -1)
        {
            cur = farthest(i);
        }

        if (cur - i > mx)
        {
            pos = i;
            mx = cur - i;
        }
    }

    for (int i = pos; r && ~i; i = par[i][0], r--)
    {
        for (int j = i; j < par[i][0]; j++)
        {
            cout << S[j] << " \n"[j == par[i][0] - 1];   // if j == par[i][0] - 1 == 1 the it will return endl else if 0 then just space

        }
    }
}


// Optimise
#include <bits/stdc++.h>
using namespace std;

#ifdef LOCAL
#include "/home/shahraaz/bin/debug.h"
#else
#define db(...)
#endif

using ll = long long;
#define f first
#define s second
#define pb push_back
#define all(v) v.begin(), v.end()

const int NAX = 1e6 + 5, MOD = 1000000007;
const int LOG = __lg(NAX);

int next_node[LOG][NAX];

// int Len[NAX];

void solveCase()
{
    int n, r, c;
    cin >> n >> r >> c;
    vector<string> vecc(n + 1);
    for (size_t i = 1; i <= n; i++)
        cin >> vecc[i];
    // vector<int> maxConsec(n + 1, 0);
    vector<int> dp(n + 2, 0);
    int len = 0, rptr = n - 1;
    for (int i = n; i >= 1; i--)
    {
        if (rptr < i)
        {
            rptr = i;
            len = vecc[i].size();
        }
        else
            len += vecc[i].size();
        while ((len + rptr - i) > c && rptr >= i)
        {
            len -= vecc[rptr].size();
            rptr--;
        }
        next_node[0][i] = rptr + 1;
    }
    for (size_t i = 0; i < LOG; i++)
        next_node[i][n + 1] = n + 1;
    for (int i = 1; i < LOG; i++)
        for (size_t j = 1; j <= n; j++)
            next_node[i][j] = next_node[i - 1][next_node[i - 1][j]];
    int maxLen = 0, maxIdx = 0;
    for (size_t i = 1; i <= n; i++)
    {
        int now = i;
        for (size_t j = 0; j < LOG; j++)
            if (r & (1 << j))
                now = next_node[j][now];
        db(i, now);
        if (maxLen < now - i)
        {
            maxLen = now - i;
            maxIdx = i;
        }
    }
    for (size_t i = 0; i < r; i++)
    {
        if (next_node[0][maxIdx] == maxIdx)
            break;
        for (size_t j = maxIdx; j < next_node[0][maxIdx]; j++)
            cout << vecc[j] << ' ';
        cout << '\n';
        maxIdx = next_node[0][maxIdx];
    }
}

int32_t main()
{
#ifndef LOCAL
    ios_base::sync_with_stdio(0);
    cin.tie(0);
#endif
    int t = 1;
    // cin >> t;
    for (int i = 1; i <= t; ++i)
    {
        solveCase();
#ifdef LOCAL
        cerr << "Case #" << i << ": Time " << chrono::duration<double>(chrono::steady_clock::now() - TimeStart).count() << " s.\n";
        TimeStart = chrono::steady_clock::now();
#endif
    }
    return 0;
}


// Optimise
#include <bits/stdc++.h>
using namespace std;

#ifdef LOCAL
#include "/home/shahraaz/bin/debug.h"
#else
#define db(...)
#endif

using ll = long long;
#define f first
#define s second
#define pb push_back
#define all(v) v.begin(), v.end()

const int NAX = 1e6 + 5, MOD = 1000000007;
const int LOG = __lg(NAX);

int next_node[LOG][NAX];

// int Len[NAX];

void solveCase()
{
    int n, r, c;
    cin >> n >> r >> c;
    vector<string> vecc(n + 1);
    for (size_t i = 1; i <= n; i++)
        cin >> vecc[i];
    // vector<int> maxConsec(n + 1, 0);
    vector<int> dp(n + 2, 0);
    int len = 0, rptr = n - 1;
    for (int i = n; i >= 1; i--)
    {
        if (rptr < i)
        {
            rptr = i;
            len = vecc[i].size();
        }
        else
            len += vecc[i].size();
        while ((len + rptr - i) > c && rptr >= i)
        {
            len -= vecc[rptr].size();
            rptr--;
        }
        next_node[0][i] = rptr + 1;
    }
    for (size_t i = 0; i < LOG; i++)
        next_node[i][n + 1] = n + 1;
    for (int i = 1; i < LOG; i++)
        for (size_t j = 1; j <= n; j++)
            next_node[i][j] = next_node[i - 1][next_node[i - 1][j]];
    int maxLen = 0, maxIdx = 0;
    for (size_t i = 1; i <= n; i++)
    {
        int now = i;
        for (size_t j = 0; j < LOG; j++)
            if (r & (1 << j))
                now = next_node[j][now];
        db(i, now);
        if (maxLen < now - i)
        {
            maxLen = now - i;
            maxIdx = i;
        }
    }
    for (size_t i = 0; i < r; i++)
    {
        if (next_node[0][maxIdx] == maxIdx)
            break;
        for (size_t j = maxIdx; j < next_node[0][maxIdx]; j++)
        {
            cout << vecc[j];
            if (j + 1 < next_node[0][maxIdx])
                cout
                    << ' ';
        }
        cout << '\n';
        maxIdx = next_node[0][maxIdx];
    }
}

int32_t main()
{
#ifndef LOCAL
    ios_base::sync_with_stdio(0);
    cin.tie(0);
#endif
    int t = 1;
    // cin >> t;
    for (int i = 1; i <= t; ++i)
    {
        solveCase();
#ifdef LOCAL
        cerr << "Case #" << i << ": Time " << chrono::duration<double>(chrono::steady_clock::now() - TimeStart).count() << " s.\n";
        TimeStart = chrono::steady_clock::now();
#endif
    }
    return 0;
}


// Optimise
#include <bits/stdc++.h>
using namespace std;

#ifdef LOCAL
#include "/home/shahraaz/bin/debug.h"
#else
#define db(...)
#endif

using ll = long long;
#define f first
#define s second
#define pb push_back
#define all(v) v.begin(), v.end()

const int NAX = 1e6 + 5, MOD = 1000000007;

deque<int> adj[NAX];
// int Len[NAX];

void solveCase()
{
    int n, r, c;
    cin >> n >> r >> c;
    vector<string> vecc(n + 1);
    for (size_t i = 1; i <= n; i++)
        cin >> vecc[i];
    vector<int> maxConsec(n + 1, 0);
    vector<int> dp(n + 2, 0);
    int len = 0, rptr = n - 1;
    for (int i = n; i >= 1; i--)
    {
        if (rptr < i)
        {
            rptr = i;
            len = vecc[i].size();
        }
        else
            len += vecc[i].size();
        while ((len + rptr - i) > c && rptr >= i)
        {
            len -= vecc[rptr].size();
            rptr--;
        }
        // db(i, rptr, len, len + rptr - i);
        if (i <= rptr)
            maxConsec[i] = rptr;
        ////
        if (i <= rptr)
        {
            // adj[i].pb({rptr + 1, rptr - i + 1});
            // db(i, rptr + 1, dp[rptr + 1]);
            // db(adj[rptr + 1]);
            adj[i] = adj[rptr + 1];
            dp[i] = dp[rptr + 1];
            dp[i] += rptr - i + 1;
            adj[i].push_front(rptr - i + 1);
            if (adj[i].size() > r)
            {
                dp[i] -= adj[i].back();
                adj[i].pop_back();
            }
        }
        db(i, dp[i], rptr, rptr - i + 1);
        db(adj[i]);
        // db("\n");
    }
    auto maxIdx = max_element(all(dp)) - dp.begin();
    int lineCnt = 0;
    while (dp[maxIdx] && lineCnt < r)
    {
        lineCnt++;
        int rptr = maxConsec[maxIdx];
        while (maxIdx <= rptr)
        {
            cout << vecc[maxIdx] << ' ';
            maxIdx++;
        }
        cout << '\n';
        maxIdx = rptr + 1;
    }
}

int32_t main()
{
#ifndef LOCAL
    ios_base::sync_with_stdio(0);
    cin.tie(0);
#endif
    int t = 1;
    // cin >> t;
    for (int i = 1; i <= t; ++i)
    {
        solveCase();
#ifdef LOCAL
        cerr << "Case #" << i << ": Time " << chrono::duration<double>(chrono::steady_clock::now() - TimeStart).count() << " s.\n";
        TimeStart = chrono::steady_clock::now();
#endif
    }
    return 0;
}


#include<stdio.h>
#include<iostream>
#include<queue>
#include<map>
#include<set>
#include<vector>
#include<utility>
#include<stack>
#include<algorithm>
#include<string>
#include<string.h>
#include<assert.h>
#include<iomanip>
#define F first
#define S second
#define pb push_back
using namespace std;
typedef long long ll;
typedef pair<ll,ll> ii;

const int N = 1e6 + 5;
const int LOG = 21;
int n, r, c;

string lst[N];
int nx[LOG][N];

int main(){
  ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);

  cin >> n >> r >> c;
  for(int i = 0; i < n; ++i)cin >> lst[i];

  int sum = -1;
  int p2 = 0;
  for(int i = 0; i < n; ++i){
    while(p2 < i){
      sum += lst[p2].size() + 1;
      p2++;
    }
    if(i > 0) sum -= lst[i - 1].size() + 1;
    while(p2 < n && sum + lst[p2].size() + 1 <= c){
      sum += lst[p2].size() + 1;
      p2++;
    }
    // cout << i << " -> " << p2 << " " << sum << endl;
    nx[0][i] = p2;
  }
  nx[0][n] = n;

  for(int i = 1; i < LOG; ++i){
    for(int j = 0; j < n; ++j){
      if(nx[i - 1][j] == n)nx[i][j] = n;
      else nx[i][j] = nx[i - 1][nx[i - 1][j]];
    }
    nx[i][n] = n;
  }

  int start = -1;
  int best = -1;

  for(int i = 0; i < n; ++i){
    int tmp = r;
    int cur = i;
    for(int j = LOG - 1; j >= 0; --j){
      if(tmp & (1 << j)){
        // cout << (1 << j) << " -> " << cur << " to " << nx[j][cur] << endl;
        cur = nx[j][cur];
      }
    }

    // cout << i << " -> " << cur << endl;

    if(best < cur - i){
      best = cur - i;
      start = i;
    }
  }
  // cout << best << " " << start << endl;
  while(r--){
    for(int i = start; i < nx[0][start]; ++i){
      if(i != start)cout << " ";
      cout << lst[i];
    }
    start = nx[0][start];
    cout << endl;
  }

}


#include<stdio.h>
#include<iostream>
#include<queue>
#include<map>
#include<set>
#include<vector>
#include<utility>
#include<stack>
#include<algorithm>
#include<string>
#include<string.h>
#include<assert.h>
#include<iomanip>
#define F first
#define S second
#define pb push_back
using namespace std;
typedef long long ll;
typedef pair<ll,ll> ii;

const int N = 1e6 + 5;
const int LOG = 20;
int n, r, c;

string lst[N];
int nx[LOG][N];

int main(){
  ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);

  cin >> n >> r >> c;
  for(int i = 0; i < n; ++i)cin >> lst[i];

  int sum = -1;
  int p2 = 0;
  for(int i = 0; i < n; ++i){
    if(lst[i].size() > c){
      nx[0][i] = i;
      p2 = i + 1;
      sum = -1;
      continue;
    }
    if(i > 0 && lst[i - 1].size() <= c)sum = sum - (lst[i - 1].size() + 1);
    while(p2 < n && sum + lst[p2].size() + 1 <= c){
      sum += lst[p2].size() + 1;
      p2++;
    }
    // cout << i << " -> " << p2 << " " << sum << endl;
    nx[0][i] = p2;
  }

  for(int i = 1; i < LOG; ++i){
    for(int j = 0; j < n; ++j){
      if(nx[i - 1][j] == n)nx[i][j] = n;
      else nx[i][j] = nx[i - 1][nx[i - 1][j]];
    }
  }

  int start = -1;
  int best = -1;

  for(int i = 0; i < n; ++i){
    int tmp = r;
    int cur = i;
    for(int j = LOG - 1; j >= 0; --j){
      if(tmp & (1 << j)){
        cur = nx[j][cur];
      }
    }

    if(best < cur - i){
      best = cur - i;
      start = i;
    }
  }

  while(r--){
    for(int i = start; i < nx[0][start]; ++i){
      if(i != start)cout << " ";
      cout << lst[i];
    }
    start = nx[0][start];
    cout << endl;
  }

}


#include<stdio.h>
#include<iostream>
#include<queue>
#include<map>
#include<set>
#include<vector>
#include<utility>
#include<stack>
#include<algorithm>
#include<string>
#include<string.h>
#include<assert.h>
#include<iomanip>
#define F first
#define S second
#define pb push_back
using namespace std;
typedef long long ll;
typedef pair<ll,ll> ii;

const int N = 1e6 + 5;
const int LOG = 20;
int n, r, c;

string lst[N];
int nx[LOG][N];

int main(){
  ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);

  cin >> n >> r >> c;
  for(int i = 0; i < n; ++i)cin >> lst[i];

  int sum = -1;
  int p2 = 0;
  for(int i = 0; i < n; ++i){
    while(p2 < i){
      sum += lst[p2].size() + 1;
      p2++;
    }
    if(i > 0) sum -= lst[i - 1].size() + 1;
    while(p2 < n && sum + lst[p2].size() + 1 <= c){
      sum += lst[p2].size() + 1;
      p2++;
    }
    // cout << i << " -> " << p2 << " " << sum << endl;
    nx[0][i] = p2;
  }

  for(int i = 1; i < LOG; ++i){
    for(int j = 0; j < n; ++j){
      if(nx[i - 1][j] == n)nx[i][j] = n;
      else nx[i][j] = nx[i - 1][nx[i - 1][j]];
    }
  }

  int start = -1;
  int best = -1;

  for(int i = 0; i < n; ++i){
    int tmp = r;
    int cur = i;
    for(int j = LOG - 1; j >= 0; --j){
      if(tmp & (1 << j)){
        cur = nx[j][cur];
      }
    }

    if(best < cur - i){
      best = cur - i;
      start = i;
    }
  }

  while(r--){
    for(int i = start; i < nx[0][start]; ++i){
      if(i != start)cout << " ";
      cout << lst[i];
    }
    start = nx[0][start];
    cout << endl;
  }

}


#include <cstdio>
#include <iostream>

#define mid ((l + r) >> 1)
#define RI register int

using namespace std;

template <class T>
inline void read(T &x) {
	x = 0; char c = getchar(); T f = 1;
	while(c > '9' || c < '0') {
		if(c == '-')
			f = -f;
		c = getchar();
	}
	while(c >= '0' && c <= '9') {
		x = x * 10 + c - '0';
		c = getchar();
	}
	x *= f;
}

const int N = 1e6 + 101;
const int Inf = 0x7fffffff;
struct Edge {
	int nxt, to;
} edge[N * 2];
int head[N], tot;
struct tree {
	int lc, rc, dat;
} t[N << 5];
int val[N], n, rt, dep[N];
int root[N << 5], cnt;
int lst;

inline void add(int x, int y) {
	edge[++tot].nxt = head[x]; head[x] = tot; edge[tot].to = y;
}

inline int New() {
	cnt++;
	t[cnt].lc = t[cnt].rc = 0;
	t[cnt].dat = Inf;
	return cnt;
}

inline void Insert(int &p, int l, int r, int dat, int val) {
	if(!p)
		p = New();
	t[p].dat = min(t[p].dat, val);
	if(l == r)
		return ;
	if(dat <= mid)
		Insert(t[p].lc, l, mid, dat, val);
	else
		Insert(t[p].rc, mid + 1, r, dat, val);
}

inline int Query(int p, int l, int r, int x, int y) {
	if(!p)
		return Inf;
	if(l >= x && r <= y)
		return t[p].dat;
	int res = Inf;
	if(x <= mid)
		res = min(res, Query(t[p].lc, l, mid, x, y));
	if(y > mid)
		res = min(res, Query(t[p].rc, mid + 1, r, x, y));
	return res;
}

inline int merge(int p, int q) {
	if(!p)
		return q;
	if(!q)
		return p;
	int res = New();
	t[res].dat = min(t[p].dat, t[q].dat);
	t[res].lc = merge(t[p].lc, t[q].lc);
	t[res].rc = merge(t[p].rc, t[q].rc);
	return res;
}

inline void dfs(int u, int fa) {
	dep[u] = dep[fa] + 1;
	for(RI i = head[u]; i; i = edge[i].nxt) {
		int v = edge[i].to;
		if(v == fa)
			continue;
		dfs(v, u);
		root[u] = merge(root[v], root[u]);
	}
	Insert(root[u], 1, n, dep[u], val[u]);
}

int main() {
	read(n), read(rt);
	for(RI i = 1; i <= n; i++)
		read(val[i]);
	for(RI i = 2; i <= n; i++) {
		int x, y;
		read(x), read(y);
		add(x, y), add(y, x);
	}
	dfs(rt, 0);
	int T;
	read(T);
	while(T--) {
		int x, k;
		read(x), read(k);
		x = (x + lst) % n + 1,
		k = (k + lst) % n;
		printf("%d\n", lst = Query(root[x], 1, n, dep[x], min(dep[x] + k, n)));
	}
	return 0;
}


#include <cstdio>
#include <iostream>

#define mid ((l + r) >> 1)
#define RI register int

using namespace std;

template <class T>
inline void read(T &x) {
	x = 0; char c = getchar(); T f = 1;
	while(c > '9' || c < '0') {
		if(c == '-')
			f = -f;
		c = getchar();
	}
	while(c >= '0' && c <= '9') {
		x = x * 10 + c - '0';
		c = getchar();
	}
	x *= f;
}

const int N = 1e6 + 101;
const int Inf = 0x7fffffff;
struct Edge {
	int nxt, to;
} edge[N * 2];
int head[N], tot;
struct tree {
	int lc, rc, dat;
} t[N];
int val[N], n, rt, dep[N];
int root[N], cnt;
int lst;

inline void add(int x, int y) {
	edge[++tot].nxt = head[x]; head[x] = tot; edge[tot].to = y;
}

inline int New() {
	cnt++;
	t[cnt].lc = t[cnt].rc = 0;
	t[cnt].dat = Inf;
	return cnt;
}

inline void Insert(int &p, int l, int r, int dat, int val) {
	if(!p)
		p = New();
		t[p].dat = min(t[p].dat, val);
	if(l == r)
		return ;
	if(dat <= mid)
		Insert(t[p].lc, l, mid, dat, val);
	else
		Insert(t[p].rc, mid + 1, r, dat, val);
}

inline int Query(int p, int l, int r, int x, int y) {
	if(!p)
		return Inf;
	if(l >= x && r <= y)
		return t[p].dat;
	int res = Inf;
	if(x <= mid)
		res = min(res, Query(t[p].lc, l, mid, x, y));
	if(y > mid)
		res = min(res, Query(t[p].rc, mid + 1, r, x, y));
	return res;
}

inline int merge(int p, int q) {
	if(!p)
		return q;
	if(!q)
		return p;
	int res = New();
	t[res].dat = min(t[p].dat, t[q].dat);
	t[res].lc = merge(t[p].lc, t[q].lc);
	t[res].rc = merge(t[p].rc, t[q].rc);
	return res;
}

inline void dfs(int u, int fa) {
	dep[u] = dep[fa] + 1;
	for(RI i = head[u]; i; i = edge[i].nxt) {
		int v = edge[i].to;
		if(v == fa)
			continue;
		dfs(v, u);
		root[u] = merge(root[v], root[u]);
	}
	Insert(root[u], 1, n, dep[u], val[u]);
}

int main() {
	read(n), read(rt);
	for(RI i = 1; i <= n; i++)
		read(val[i]);
	for(RI i = 2; i <= n; i++) {
		int x, y;
		read(x), read(y);
		add(x, y), add(y, x);
	}
	dfs(rt, 0);
	int T;
	read(T);
	while(T--) {
		int x, k;
		read(x), read(k);
		x = (x + lst) % n + 1,
		k = (k + lst) % n;
		printf("%d\n", lst = Query(root[x], 1, n, dep[x], min(dep[x] + k, n)));
	}
	return 0;
}


#include <cstdio>
#include <iostream>

#define mid ((l + r) >> 1)
#define RI register int

using namespace std;

template <class T>
inline void read(T &x) {
	x = 0; char c = getchar(); T f = 1;
	while(c > '9' || c < '0') {
		if(c == '-')
			f = -f;
		c = getchar();
	}
	while(c >= '0' && c <= '9') {
		x = x * 10 + c - '0';
		c = getchar();
	}
	x *= f;
}

const int N = 4e6 + 1;
const int Inf = 0x7fffffff;
struct Edge {
	int nxt, to;
} edge[N];
int head[N], tot;
struct tree {
	int lc, rc, dat;
} t[N];
int val[N], n, rt, dep[N];
int root[N], cnt;
int lst;

inline void add(int x, int y) {
	edge[++tot].nxt = head[x]; head[x] = tot; edge[tot].to = y;
}

inline int New() {
	t[++cnt].lc = t[cnt].rc = 0;
	t[cnt].dat = Inf;
	return cnt;
}

inline void Update(int p) {
	t[p].dat = min(t[t[p].lc].dat, t[t[p].rc].dat);
}

inline void Insert(int &p, int l, int r, int dat, int val) {
	if(!p)
		p = New();
	if(l == r) {
		t[p].dat = min(t[p].dat, val);
		return ;
	}
	if(dat <= mid)
		Insert(t[p].lc, l, mid, dat, val);
	else
		Insert(t[p].rc, mid + 1, r, dat, val);
	Update(p);
}

inline int Query(int p, int l, int r, int x, int y) {
	if(!p)
		return Inf;
	if(l >= x && r <= y)
		return t[p].dat;
	int res = Inf;
	if(l <= mid)
		res = min(res, Query(t[p].lc, l, mid, x, y));
	if(r > mid)
		res = min(res, Query(t[p].rc, mid + 1, r, x, y));
	return res;
}

inline int merge(int p, int q) {
	if(!p)
		return q;
	if(!q)
		return p;
	int res = New();
	t[res].dat = min(t[p].dat, t[q].dat);
	t[res].lc = merge(t[p].lc, t[q].lc);
	t[res].rc = merge(t[p].rc, t[q].rc);
	return res;
}

inline void dfs(int u, int fa) {
	dep[u] = dep[fa] + 1;
	for(RI i = head[u]; i; i = edge[i].nxt) {
		int v = edge[i].to;
		if(v == fa)
			continue;
		dfs(v, u);
		root[u] = merge(root[v], root[u]);
	}
	Insert(root[u], 1, n, dep[u], val[u]);
}

int main() {
	read(n), read(rt);
	for(RI i = 1; i <= n; i++)
		read(val[i]);
	for(RI i = 2; i <= n; i++) {
		int x, y;
		read(x), read(y);
		add(x, y), add(y, x);
	}
	dfs(rt, 0);
	int T;
	read(T);
	while(T--) {
		int x, k;
		read(x), read(k);
		x = (x + lst) % n + 1,
		k = (k + lst) % n;
		printf("%d\n", lst = Query(root[x], 1, n, dep[x], min(dep[x] + k, n)));
	}
	return 0;
}


#include <stdio.h>
#include <vector>
using namespace std;
#define pb push_back
const int N=100050;
const int M=20*N;
const int inf=1e9+69;
int min(int a, int b){ return a>b?b:a;}
int x[M],ls[M],rs[M],clock,root[N];
void Set(int p, int &c, int ss, int se, int qi, int val)
{
	c=++clock,ls[c]=ls[p],rs[c]=rs[p],x[c]=min(x[p],val);
	if(ss==se) return;
	int mid=ss+se>>1;
	if(qi<=mid) Set(ls[p],ls[c],ss,mid,qi,val);
	else Set(rs[p],rs[c],mid+1,se,qi,val);
}
int Get(int c, int ss, int se, int qs, int qe)
{
	if(ss>qe || qs>se) return inf;
	if(qs<=ss && qe>=se) return x[c];
	int mid=ss+se>>1;
	return min(Get(ls[c],ss,mid,qs,qe),Get(rs[c],mid+1,se,qs,qe));
}
vector<int> E[N],D[N];
int b[N],e[N],d[N],val[N],time;
void DFS(int u, int p, int l)
{
	d[u]=l,b[u]=++time,D[l].pb(u);
	for(int i=0;i<E[u].size();i++)
	{
		int v=E[u][i];
		if(p!=v) DFS(v,u,l+1);
	}
	e[u]=time;
}
int main()
{
	x[0]=inf;
	int n,r,u,v,i,j,q,k,p;
	scanf("%i %i",&n,&r);
	for(i=1;i<=n;i++) scanf("%i",&val[i]);
	for(i=1;i<n;i++) scanf("%i %i",&u,&v),E[u].pb(v),E[v].pb(u);
	DFS(r,0,1);
	for(i=1;i<N;i++)
	{
		root[i]=root[i-1];
		for(j=0;j<D[i].size();j++) Set(root[i],root[i],1,N,b[D[i][j]],val[D[i][j]]);
	}
	scanf("%i",&q);
	int last=0;
	while(q--)
	{
		scanf("%i %i",&k,&p);
		u=((k+last)%n)+1;
		k=(p+last)%n;
		last=Get(root[min(N-1,d[u]+k)],1,N,b[u],e[u]);
		printf("%i\n",last);
	}
	return 0;
}


#include <stdio.h>
#include <vector>
#include <algorithm>
using namespace std;
const int N=100050;
const int M=17*17*N;
const int inf=1e9+69;
int min(int a, int b){ return a>b?b:a;}
int max(int a, int b){ return a>b?a:b;}
int x[M],ls[M],rs[M],clock,n;
vector<int> E[N],Index[N*2],index[N];
int FindIndex(int d, int b){ return lower_bound(Index[d].begin(),Index[d].end(),b)-Index[d].begin()+1;}
int FindUpper(int d, int b){ return upper_bound(Index[d].begin(),Index[d].end(),b)-Index[d].begin();}
void Set(int &c, int ss, int se, int qi, int val)
{
	if(!c) c=++clock,x[c]=inf;
	x[c]=min(x[c],val);
	if(ss==se) return;
	int mid=ss+se>>1;
	if(qi<=mid) Set(ls[c],ss,mid,qi,val);
	else Set(rs[c],mid+1,se,qi,val);
}
int Get(int c, int ss, int se, int qs, int qe)
{
	if(!c) return inf;
	if(ss>qe || qs>se) return inf;
	if(qs<=ss && qe>=se) return x[c];
	int mid=ss+se>>1;
	return min(Get(ls[c],ss,mid,qs,qe),Get(rs[c],mid+1,se,qs,qe));
}
int root[N*2],LS[N*2],RS[N*2],cnt,First;
void Build(int &c, int ss, int se)
{
	c=++cnt;
	if(ss==se){ Index[c]=index[ss];return;}
	int mid=ss+se>>1;
	Build(LS[c],ss,mid);
	Build(RS[c],mid+1,se);
	int i=0,j=0,k,a=Index[LS[c]].size(),b=Index[RS[c]].size();
	for(k=0;k<a+b;k++)
	{
		if(i<a && (j==b || (Index[LS[c]][i]<Index[RS[c]][j]))) Index[c].push_back(Index[LS[c]][i]),i++;
		else Index[c].push_back(Index[RS[c]][j]),j++;
	}
}
void Set(int &c, int ss, int se, int qi, int x, int val)
{
	if(!c) c=++cnt;
	int H=Index[qi].size();
	Set(root[c],1,H,FindIndex(c,x),val);
	if(ss==se) return;
	int mid=ss+se>>1;
	if(qi<=mid) Set(LS[c],ss,mid,qi,x,val);
	else Set(RS[c],mid+1,se,qi,x,val);
}
int Get(int c, int ss, int se, int qs, int qe, int x, int y)
{
	if(!c) return inf;
	if(ss>qe || qs>se) return inf;
	if(qs<=ss && qe>=se) return Get(root[c],1,Index[c].size(),FindIndex(c,x),FindUpper(c,y));
	int mid=ss+se>>1;
	return min(Get(LS[c],ss,mid,qs,qe,x,y),Get(RS[c],mid+1,se,qs,qe,x,y));
}
int Depth[N],b[N],e[N],time,val[N];
int MaxDepth;
void DFS(int u, int p, int d)
{
	Depth[u]=d;
	b[u]=++time;
	index[Depth[u]].push_back(b[u]);
	MaxDepth=max(MaxDepth,d);
	//Set(First,1,N,Depth[u],b[u],val[u]);
	for(int i=0;i<E[u].size();i++)
	{
		int v=E[u][i];
		if(v!=p) DFS(v,u,d+1);
	}
	e[u]=time;
}
int main()
{
	x[0]=inf;
	int i,u,v,r;
	scanf("%i %i",&n,&r);
	for(i=1;i<=n;i++) scanf("%i",&val[i]);
	for(i=1;i<n;i++)
	{
		scanf("%i %i",&u,&v);
		E[u].push_back(v);
		E[v].push_back(u);
	}
	DFS(r,0,1);
	Build(First,1,MaxDepth);
	for(i=1;i<=n;i++)
	{
		Set(First,1,MaxDepth,Depth[i],b[i],val[i]);
	}
	int q,last=0,k,p;
	scanf("%i",&q);
	while(q--)
	{
		scanf("%i %i",&k,&p);
		u=((k+last)%n)+1;
		k=(p+last)%n;
		last=Get(First,1,MaxDepth,Depth[u],min(Depth[u]+k,MaxDepth),b[u],e[u]);
		printf("%i\n",last);
	}
	return 0;
}


#include <stdio.h>
#include <vector>
using namespace std;
const int N=100050;
const int M=17*17*N;
const int inf=1e9+69;
int min(int a, int b){ return a>b?b:a;}
int max(int a, int b){ return a>b?a:b;}
int x[M],ls[M],rs[M],clock;
void Set(int &c, int ss, int se, int qi, int val)
{
	if(!c) c=++clock,x[c]=inf;
	x[c]=min(x[c],val);
	if(ss==se) return;
	int mid=ss+se>>1;
	if(qi<=mid) Set(ls[c],ss,mid,qi,val);
	else Set(rs[c],mid+1,se,qi,val);
}
int Get(int c, int ss, int se, int qs, int qe)
{
	if(ss>qe || qs>se) return inf;
	if(qs<=ss && qe>=se) return x[c];
	int mid=ss+se>>1;
	return min(Get(ls[c],ss,mid,qs,qe),Get(rs[c],mid+1,se,qs,qe));
}
int root[N*2],LS[N*2],RS[N*2],cnt,First;
void Set(int &c, int ss, int se, int qi, int x, int val)
{
	if(!c) c=++cnt;
	Set(root[c],1,N,x,val);
	if(ss==se) return;
	int mid=ss+se>>1;
	if(qi<=mid) Set(LS[c],ss,mid,qi,x,val);
	else Set(RS[c],mid+1,se,qi,x,val);
}
int Get(int c, int ss, int se, int qs, int qe, int x, int y)
{
	if(ss>qe || qs>se) return inf;
	if(qs<=ss && qe>=se) return Get(root[c],1,N,x,y);
	int mid=ss+se>>1;
	return min(Get(LS[c],ss,mid,qs,qe,x,y),Get(RS[c],mid+1,se,qs,qe,x,y));
}
vector<int> E[N];
int Depth[N],b[N],e[N],time,val[N];
int MaxDepth;
void DFS(int u, int p, int d)
{
	Depth[u]=d;
	b[u]=++time;
	MaxDepth=max(MaxDepth,d);
	//Set(First,1,N,Depth[u],b[u],val[u]);
	for(int i=0;i<E[u].size();i++)
	{
		int v=E[u][i];
		if(v!=p) DFS(v,u,d+1);
	}
	e[u]=time;
}
int main()
{
	x[0]=inf;
	int n,i,u,v,r;
	scanf("%i %i",&n,&r);
	for(i=1;i<=n;i++) scanf("%i",&val[i]);
	for(i=1;i<n;i++)
	{
		scanf("%i %i",&u,&v);
		E[u].push_back(v);
		E[v].push_back(u);
	}
	DFS(r,0,1);
	for(i=1;i<=n;i++)
	{
		Set(First,1,MaxDepth,Depth[i],b[i],val[i]);
	}
	int q,last=0,k,p;
	scanf("%i",&q);
	while(q--)
	{
		scanf("%i %i",&k,&p);
		u=((k+last)%n)+1;
		k=(p+last)%n;
		last=Get(First,1,MaxDepth,Depth[u],min(Depth[u]+k,MaxDepth),b[u],e[u]);
		printf("%i\n",last);
	}
	return 0;
}


#include <stdio.h>
#include <vector>
using namespace std;
const int N=100050;
const int M=17*17*N;
const int inf=1e9+69;
int min(int a, int b){ return a>b?b:a;}
int x[M],ls[M],rs[M],clock;
void Set(int &c, int ss, int se, int qi, int val)
{
	if(!c) c=++clock,x[c]=inf;
	x[c]=min(x[c],val);
	if(ss==se) return;
	int mid=ss+se>>1;
	if(qi<=mid) Set(ls[c],ss,mid,qi,val);
	else Set(rs[c],mid+1,se,qi,val);
}
int Get(int c, int ss, int se, int qs, int qe)
{
	if(ss>qe || qs>se) return inf;
	if(qs<=ss && qe>=se) return x[c];
	int mid=ss+se>>1;
	return min(Get(ls[c],ss,mid,qs,qe),Get(rs[c],mid+1,se,qs,qe));
}
int root[N*2],LS[N*2],RS[N*2],cnt,First;
void Set(int &c, int ss, int se, int qi, int x, int val)
{
	if(!c) c=++cnt;
	Set(root[c],1,N,x,val);
	if(ss==se) return;
	int mid=ss+se>>1;
	if(qi<=mid) Set(LS[c],ss,mid,qi,x,val);
	else Set(RS[c],mid+1,se,qi,x,val);
}
int Get(int c, int ss, int se, int qs, int qe, int x, int y)
{
	if(ss>qe || qs>se) return inf;
	if(qs<=ss && qe>=se) return Get(root[c],1,N,x,y);
	int mid=ss+se>>1;
	return min(Get(LS[c],ss,mid,qs,qe,x,y),Get(RS[c],mid+1,se,qs,qe,x,y));
}
vector<int> E[N];
int Depth[N],b[N],e[N],time,val[N];
void DFS(int u, int p, int d)
{
	Depth[u]=d;
	b[u]=++time;
	Set(First,1,N,Depth[u],b[u],val[u]);
	for(int i=0;i<E[u].size();i++)
	{
		int v=E[u][i];
		if(v!=p) DFS(v,u,d+1);
	}
	e[u]=time;
}
int main()
{
	x[0]=inf;
	int n,i,u,v,r;
	scanf("%i %i",&n,&r);
	for(i=1;i<=n;i++) scanf("%i",&val[i]);
	for(i=1;i<n;i++)
	{
		scanf("%i %i",&u,&v);
		E[u].push_back(v);
		E[v].push_back(u);
	}
	DFS(r,0,1);
	int q,last=0,k,p;
	scanf("%i",&q);
	while(q--)
	{
		scanf("%i %i",&k,&p);
		u=((k+last)%n)+1;
		k=(p+last)%n;
		last=Get(First,1,N,Depth[u],Depth[u]+k,b[u],e[u]);
		printf("%i\n",last);
	}
	return 0;
}


#include <stdio.h>
#include <vector>
using namespace std;
#define pb push_back
const int N=100050;
const int M=17*N;
const int inf=1e9+69;
int min(int a, int b){ return a>b?b:a;}
int x[M],ls[M],rs[M],clock,root[N];
void Set(int p, int &c, int ss, int se, int qi, int val)
{
	c=++clock,ls[c]=ls[p],rs[c]=rs[p],x[c]=min(x[p],val);
	if(ss==se) return;
	int mid=ss+se>>1;
	if(qi<=mid) Set(ls[p],ls[c],ss,mid,qi,val);
	else Set(rs[p],rs[c],mid+1,se,qi,val);
}
int Get(int c, int ss, int se, int qs, int qe)
{
	if(ss>qe || qs>se) return inf;
	if(qs<=ss && qe>=se) return x[c];
	int mid=ss+se>>1;
	return min(Get(ls[c],ss,mid,qs,qe),Get(rs[c],mid+1,se,qs,qe));
}
vector<int> E[N],D[N];
int b[N],e[N],d[N],val[N],time;
void DFS(int u, int p, int l)
{
	d[u]=l,b[u]=++time,D[l].pb(u);
	for(int i=0;i<E[u].size();i++)
	{
		int v=E[u][i];
		if(p!=v) DFS(v,u,l+1);
	}
	e[u]=time;
}
int main()
{
	x[0]=inf;
	int n,r,u,v,i,j,q,k,p;
	scanf("%i %i",&n,&r);
	for(i=1;i<=n;i++) scanf("%i",&val[i]);
	for(i=1;i<n;i++) scanf("%i %i",&u,&v),E[u].pb(v),E[v].pb(u);
	DFS(r,0,1);
	for(i=1;i<N;i++)
	{
		root[i]=root[i-1];
		for(j=0;j<D[i].size();j++) Set(root[i],root[i],1,N,b[D[i][j]],val[D[i][j]]);
	}
	scanf("%i",&q);
	int last=0;
	while(q--)
	{
		scanf("%i %i",&k,&p);
		u=((k+last)%n)+1;
		k=(p+last)%n;
		last=Get(root[min(N-1,d[u]+k)],1,N,b[u],e[u]);
		printf("%i\n",last);
	}
	return 0;
}


#include <stdio.h>
#include <vector>
using namespace std;
const int N=100050;
const int M=17*17*N;
const int inf=1e9+69;
int min(int a, int b){ return a>b?b:a;}
int max(int a, int b){ return a>b?a:b;}
int x[M],ls[M],rs[M],clock;
void Set(int &c, int ss, int se, int qi, int val)
{
	if(!c) c=++clock,x[c]=inf;
	x[c]=min(x[c],val);
	if(ss==se) return;
	int mid=ss+se>>1;
	if(qi<=mid) Set(ls[c],ss,mid,qi,val);
	else Set(rs[c],mid+1,se,qi,val);
}
int Get(int c, int ss, int se, int qs, int qe)
{
	if(!c) return inf;
	if(ss>qe || qs>se) return inf;
	if(qs<=ss && qe>=se) return x[c];
	int mid=ss+se>>1;
	return min(Get(ls[c],ss,mid,qs,qe),Get(rs[c],mid+1,se,qs,qe));
}
int root[N*2],LS[N*2],RS[N*2],cnt,First;
void Set(int &c, int ss, int se, int qi, int x, int val)
{
	if(!c) c=++cnt;
	Set(root[c],1,N,x,val);
	if(ss==se) return;
	int mid=ss+se>>1;
	if(qi<=mid) Set(LS[c],ss,mid,qi,x,val);
	else Set(RS[c],mid+1,se,qi,x,val);
}
int Get(int c, int ss, int se, int qs, int qe, int x, int y)
{
	if(!c) return inf;
	if(ss>qe || qs>se) return inf;
	if(qs<=ss && qe>=se) return Get(root[c],1,N,x,y);
	int mid=ss+se>>1;
	return min(Get(LS[c],ss,mid,qs,qe,x,y),Get(RS[c],mid+1,se,qs,qe,x,y));
}
vector<int> E[N];
int Depth[N],b[N],e[N],time,val[N];
int MaxDepth;
void DFS(int u, int p, int d)
{
	Depth[u]=d;
	b[u]=++time;
	MaxDepth=max(MaxDepth,d);
	//Set(First,1,N,Depth[u],b[u],val[u]);
	for(int i=0;i<E[u].size();i++)
	{
		int v=E[u][i];
		if(v!=p) DFS(v,u,d+1);
	}
	e[u]=time;
}
int main()
{
	x[0]=inf;
	int n,i,u,v,r;
	scanf("%i %i",&n,&r);
	for(i=1;i<=n;i++) scanf("%i",&val[i]);
	for(i=1;i<n;i++)
	{
		scanf("%i %i",&u,&v);
		E[u].push_back(v);
		E[v].push_back(u);
	}
	DFS(r,0,1);
	for(i=1;i<=n;i++)
	{
		Set(First,1,MaxDepth,Depth[i],b[i],val[i]);
	}
	int q,last=0,k,p;
	scanf("%i",&q);
	while(q--)
	{
		scanf("%i %i",&k,&p);
		u=((k+last)%n)+1;
		k=(p+last)%n;
		last=Get(First,1,MaxDepth,Depth[u],min(Depth[u]+k,MaxDepth),b[u],e[u]);
		printf("%i\n",last);
	}
	return 0;
}


#include <stdio.h>
#include <vector>
using namespace std;
const int N=100050;
const int M=17*17*N;
const int inf=1e9+69;
int min(int a, int b){ return a>b?b:a;}
int max(int a, int b){ return a>b?a:b;}
int x[M],ls[M],rs[M],clock,n;
void Set(int &c, int ss, int se, int qi, int val)
{
	if(!c) c=++clock,x[c]=inf;
	x[c]=min(x[c],val);
	if(ss==se) return;
	int mid=ss+se>>1;
	if(qi<=mid) Set(ls[c],ss,mid,qi,val);
	else Set(rs[c],mid+1,se,qi,val);
}
int Get(int c, int ss, int se, int qs, int qe)
{
	if(!c) return inf;
	if(ss>qe || qs>se) return inf;
	if(qs<=ss && qe>=se) return x[c];
	int mid=ss+se>>1;
	return min(Get(ls[c],ss,mid,qs,qe),Get(rs[c],mid+1,se,qs,qe));
}
int root[N*2],LS[N*2],RS[N*2],cnt,First;
void Set(int &c, int ss, int se, int qi, int x, int val)
{
	if(!c) c=++cnt;
	Set(root[c],1,n,x,val);
	if(ss==se) return;
	int mid=ss+se>>1;
	if(qi<=mid) Set(LS[c],ss,mid,qi,x,val);
	else Set(RS[c],mid+1,se,qi,x,val);
}
int Get(int c, int ss, int se, int qs, int qe, int x, int y)
{
	if(!c) return inf;
	if(ss>qe || qs>se) return inf;
	if(qs<=ss && qe>=se) return Get(root[c],1,n,x,y);
	int mid=ss+se>>1;
	return min(Get(LS[c],ss,mid,qs,qe,x,y),Get(RS[c],mid+1,se,qs,qe,x,y));
}
vector<int> E[N];
int Depth[N],b[N],e[N],time,val[N];
int MaxDepth;
void DFS(int u, int p, int d)
{
	Depth[u]=d;
	b[u]=++time;
	MaxDepth=max(MaxDepth,d);
	//Set(First,1,N,Depth[u],b[u],val[u]);
	for(int i=0;i<E[u].size();i++)
	{
		int v=E[u][i];
		if(v!=p) DFS(v,u,d+1);
	}
	e[u]=time;
}
int main()
{
	x[0]=inf;
	int i,u,v,r;
	scanf("%i %i",&n,&r);
	for(i=1;i<=n;i++) scanf("%i",&val[i]);
	for(i=1;i<n;i++)
	{
		scanf("%i %i",&u,&v);
		E[u].push_back(v);
		E[v].push_back(u);
	}
	DFS(r,0,1);
	for(i=1;i<=n;i++)
	{
		Set(First,1,MaxDepth,Depth[i],b[i],val[i]);
	}
	int q,last=0,k,p;
	scanf("%i",&q);
	while(q--)
	{
		scanf("%i %i",&k,&p);
		u=((k+last)%n)+1;
		k=(p+last)%n;
		last=Get(First,1,MaxDepth,Depth[u],min(Depth[u]+k,MaxDepth),b[u],e[u]);
		printf("%i\n",last);
	}
	return 0;
}


#include <stdio.h>
#include <vector>
#include <algorithm>
using namespace std;
const int N=100050;
const int M=17*17*N;
const int inf=1e9+69;
int min(int a, int b){ return a>b?b:a;}
int max(int a, int b){ return a>b?a:b;}
int x[M],ls[M],rs[M],clock,n;
vector<int> E[N],Index[N*2],index[N];
int FindIndex(int d, int b){ return lower_bound(Index[d].begin(),Index[d].end(),b)-Index[d].begin()+1;}
int FindUpper(int d, int b){ return upper_bound(Index[d].begin(),Index[d].end(),b)-Index[d].begin();}
void Set(int &c, int ss, int se, int qi, int val)
{
	if(!c) c=++clock,x[c]=inf;
	x[c]=min(x[c],val);
	if(ss==se) return;
	int mid=ss+se>>1;
	if(qi<=mid) Set(ls[c],ss,mid,qi,val);
	else Set(rs[c],mid+1,se,qi,val);
}
int Get(int c, int ss, int se, int qs, int qe)
{
	if(!c) return inf;
	if(ss>qe || qs>se) return inf;
	if(qs<=ss && qe>=se) return x[c];
	int mid=ss+se>>1;
	return min(Get(ls[c],ss,mid,qs,qe),Get(rs[c],mid+1,se,qs,qe));
}
int root[N*2],LS[N*2],RS[N*2],cnt,First;
void Build(int &c, int ss, int se)
{
	c=++cnt;
	if(ss==se){ Index[c]=index[ss];return;}
	int mid=ss+se>>1;
	Build(LS[c],ss,mid);
	Build(RS[c],mid+1,se);
	int i=0,j=0,k,a=Index[LS[c]].size(),b=Index[RS[c]].size();
	for(k=0;k<a+b;k++)
	{
		if(i<a && (j==b || (Index[LS[c]][i]<Index[RS[c]][j]))) Index[c].push_back(Index[LS[c]][i]),i++;
		else Index[c].push_back(Index[RS[c]][j]),j++;
	}
}
void Set(int &c, int ss, int se, int qi, int x, int val)
{
	if(!c) c=++cnt;
	int H=Index[c].size();
	Set(root[c],1,H,FindIndex(c,x),val);
	if(ss==se) return;
	int mid=ss+se>>1;
	if(qi<=mid) Set(LS[c],ss,mid,qi,x,val);
	else Set(RS[c],mid+1,se,qi,x,val);
}
int Get(int c, int ss, int se, int qs, int qe, int x, int y)
{
	if(!c) return inf;
	if(ss>qe || qs>se) return inf;
	if(qs<=ss && qe>=se) return Get(root[c],1,Index[c].size(),FindIndex(c,x),FindUpper(c,y));
	int mid=ss+se>>1;
	return min(Get(LS[c],ss,mid,qs,qe,x,y),Get(RS[c],mid+1,se,qs,qe,x,y));
}
int Depth[N],b[N],e[N],time,val[N];
int MaxDepth;
void DFS(int u, int p, int d)
{
	Depth[u]=d;
	b[u]=++time;
	index[Depth[u]].push_back(b[u]);
	MaxDepth=max(MaxDepth,d);
	//Set(First,1,N,Depth[u],b[u],val[u]);
	for(int i=0;i<E[u].size();i++)
	{
		int v=E[u][i];
		if(v!=p) DFS(v,u,d+1);
	}
	e[u]=time;
}
int main()
{
	x[0]=inf;
	int i,u,v,r;
	scanf("%i %i",&n,&r);
	for(i=1;i<=n;i++) scanf("%i",&val[i]);
	for(i=1;i<n;i++)
	{
		scanf("%i %i",&u,&v);
		E[u].push_back(v);
		E[v].push_back(u);
	}
	DFS(r,0,1);
	Build(First,1,MaxDepth);
	for(i=1;i<=n;i++)
	{
		Set(First,1,MaxDepth,Depth[i],b[i],val[i]);
	}
	int q,last=0,k,p;
	scanf("%i",&q);
	while(q--)
	{
		scanf("%i %i",&k,&p);
		u=((k+last)%n)+1;
		k=(p+last)%n;
		last=Get(First,1,MaxDepth,Depth[u],min(Depth[u]+k,MaxDepth),b[u],e[u]);
		printf("%i\n",last);
	}
	return 0;
}


#include<bits/stdc++.h>
#define LL long long
using namespace std;
int read(){
	char ch=getchar(); int w=1,c=0;
	for(;!isdigit(ch);ch=getchar()) if (ch=='-') w=-1;
	for(;isdigit(ch);ch=getchar()) c=(c<<1)+(c<<3)+(ch^48);
	return w*c;
}
const int M=1e5+10;
vector<int> v[M];
int s[M*25],L[M*25],R[M*25],w[M];
int d[M];
int rt[M],n,root,tot;
#define mid (l+r>>1)
void update(int &x,int l,int r,int p,int d){
	if (!x) x=++tot;

	if (!s[x]) s[x]=d; else s[x]=min(s[x],d);

	if (l==r) return ;
	if (p<=mid) update(L[x],l,mid,p,d);
	else update(R[x],mid+1,r,p,d);
}
void MG(int &lx,int rx){
	if (!lx || !rx) {
		lx+=rx;
		return ;
	}
	s[lx]=min(s[lx],s[rx]);
	MG(L[lx],L[rx]);
	MG(R[lx],R[rx]);
}
int qry(int x,int l,int r,int ll,int rr){
	if (l==ll && r==rr) return s[x];
	if (rr<=mid) return qry(L[x],l,mid,ll,rr);
	if (ll>mid) return qry(R[x],mid+1,r,ll,rr);
	return min(qry(L[x],l,mid,ll,mid),qry(R[x],mid+1,r,mid+1,rr));
}
void dfs(int x,int fa,int dep){
	d[x]=dep;
	update(rt[x],0,n,dep,w[x]);
	for (int i=0;i<v[x].size();i++){
		int y=v[x][i];
		if (y==fa) continue;
		dfs(y,x,dep+1);
		MG(rt[x],rt[y]);
	}
}
int main(){
	n=read(); root=read();
	for (int i=1;i<=n;i++) w[i]=read();
	for (int i=1;i<n;i++){
		int x=read(),y=read();
		v[x].push_back(y);
		v[y].push_back(x);
	}
	dfs(root,0,0);
	int m=read();
	int lastans=0;
	s[0]=1e9+100;
	while (m--){
		int x=read(),k=read();
//		cout<<d[x]<<" "<<d[x]+k<<"\n";
		cout<<qry(rt[x],0,n,d[x],min(d[x]+k,n))<<"\n";
//		x=(x+lastans)%n+1; k=(k+lastans)%n;
//		cout<<(lastans=qry(rt[x],0,n,d[x],min(d[x]+k,n)))<<"\n";
	}
	return 0;
}


#include<bits/stdc++.h>
#define LL long long
using namespace std;
int read(){
	char ch=getchar(); int w=1,c=0;
	for(;!isdigit(ch);ch=getchar()) if (ch=='-') w=-1;
	for(;isdigit(ch);ch=getchar()) c=(c<<1)+(c<<3)+(ch^48);
	return w*c;
}
const int M=1e5+10;
vector<int> v[M];
int s[M*25],L[M*25],R[M*25],w[M];
int d[M];
int rt[M],n,root,tot;
#define mid (l+r>>1)
void update(int &x,int l,int r,int p,int d){
	if (!x) x=++tot;

	if (!s[x]) s[x]=d; else s[x]=min(s[x],d);

	if (l==r) return ;
	if (p<=mid) update(L[x],l,mid,p,d);
	else update(R[x],mid+1,r,p,d);
}
void MG(int &lx,int rx){
	if (!lx || !rx) {
		lx+=rx;
		return ;
	}
	s[lx]=min(s[lx],s[rx]);
	MG(L[lx],L[rx]);
	MG(R[lx],R[rx]);
}
int qry(int x,int l,int r,int ll,int rr){
	if (l==ll && r==rr) return s[x];
	if (rr<=mid) return qry(L[x],l,mid,ll,rr);
	if (ll>mid) return qry(R[x],mid+1,r,ll,rr);
	return min(qry(L[x],l,mid,ll,mid),qry(R[x],mid+1,r,mid+1,rr));
}
void dfs(int x,int fa,int dep){
	d[x]=dep;
	update(rt[x],0,n,dep,w[x]);
	for (int i=0;i<v[x].size();i++){
		int y=v[x][i];
		if (y==fa) continue;
		dfs(y,x,dep+1);
		MG(rt[x],rt[y]);
	}
}
int main(){
	n=read(); root=read();
	int mi=0;
	for (int i=1;i<=n;i++) w[i]=read(),mi=min(mi,w[i]);
	for (int i=1;i<n;i++){
		int x=read(),y=read();
		v[x].push_back(y);
		v[y].push_back(x);
	}
	for (int i=0;i<=25*n;i++) s[i]=1e9+10;
	dfs(root,0,0);
	int m=read();
	int lastans=0;
//	s[0]=1e9+100;
	for (int i=0;i<=25*n;i++) assert(s[i]>=mi);
	while (m--){
		int x=read(),k=read();
//		cout<<d[x]<<" "<<d[x]+k<<"\n";
//		cout<<qry(rt[x],0,n,d[x],min(d[x]+k,n))<<"\n";
		x=((x+lastans)%n)+1; k=(k+lastans)%n;
		cout<<(lastans=qry(rt[x],0,n,d[x],min(d[x]+k,n)))<<"\n";
	}
	return 0;
}


#include<bits/stdc++.h>
#define LL long long
using namespace std;
int read(){
	char ch=getchar(); int w=1,c=0;
	for(;!isdigit(ch);ch=getchar()) if (ch=='-') w=-1;
	for(;isdigit(ch);ch=getchar()) c=(c<<1)+(c<<3)+(ch^48);
	return w*c;
}
const int M=1e5+10;
vector<int> v[M];
int s[M*40],L[M*40],R[M*40],w[M];
int d[M];
int rt[M],n,root,tot;
#define mid (l+r>>1)
void update(int &x,int l,int r,int p,int d){
	if (!x) x=++tot;

	if (!s[x]) s[x]=d; else s[x]=min(s[x],d);

	if (l==r) return ;
	if (p<=mid) update(L[x],l,mid,p,d);
	else update(R[x],mid+1,r,p,d);
}
int MG(int lx,int rx){
	int x=0;
	if (!lx || !rx) {
		return lx+rx;
	}
	x=++tot;
	s[x]=min(s[lx],s[rx]);
	L[x]=MG(L[lx],L[rx]);
	R[x]=MG(R[lx],R[rx]);
	return x;
}
int qry(int x,int l,int r,int ll,int rr){
	if (l==ll && r==rr) return s[x];
	if (rr<=mid) return qry(L[x],l,mid,ll,rr);
	if (ll>mid) return qry(R[x],mid+1,r,ll,rr);
	return min(qry(L[x],l,mid,ll,mid),qry(R[x],mid+1,r,mid+1,rr));
}
void dfs(int x,int fa,int dep){
	d[x]=dep;
	update(rt[x],0,n,dep,w[x]);
	for (int i=0;i<v[x].size();i++){
		int y=v[x][i];
		if (y==fa) continue;
		dfs(y,x,dep+1);
		rt[x]=MG(rt[x],rt[y]);
	}
}
int main(){
	n=read(); root=read();
	int mi=INT_MAX;
	for (int i=1;i<=n;i++) w[i]=read(),mi=min(mi,w[i]);
	for (int i=1;i<n;i++){
		int x=read(),y=read();
		v[x].push_back(y);
		v[y].push_back(x);
	}
	for (int i=0;i<=25*n;i++) s[i]=1e9+10;
	dfs(root,0,0);
	int m=read();
	int lastans=0;
//	s[0]=1e9+100;
//	if (n>100)cout<<mi<<"\n";
//	for (int i=0;i<=25*n;i++) assert(s[i]>=mi);
	while (m--){
		int x=read(),k=read();
//		cout<<d[x]<<" "<<d[x]+k<<"\n";
//		cout<<qry(rt[x],0,n,d[x],min(d[x]+k,n))<<"\n";
		x=((x+lastans)%n)+1; k=(k+lastans)%n;
		cout<<(lastans=qry(rt[x],0,n,d[x],min(d[x]+k,n)))<<"\n";
	}
	return 0;
}


#include<bits/stdc++.h>
#define LL long long
using namespace std;
int read(){
	char ch=getchar(); int w=1,c=0;
	for(;!isdigit(ch);ch=getchar()) if (ch=='-') w=-1;
	for(;isdigit(ch);ch=getchar()) c=(c<<1)+(c<<3)+(ch^48);
	return w*c;
}
const int M=1e5+10;
vector<int> v[M];
int s[M*25],L[M*25],R[M*25],w[M];
int d[M];
int rt[M],n,root,tot;
#define mid (l+r>>1)
void update(int &x,int l,int r,int p,int d){
	if (!x) x=++tot;

	if (!s[x]) s[x]=d; else s[x]=min(s[x],d);

	if (l==r) return ;
	if (p<=mid) update(L[x],l,mid,p,d);
	else update(R[x],mid+1,r,p,d);
}
void MG(int &lx,int rx){
	if (!lx || !rx) {
		lx+=rx;
		return ;
	}
	s[lx]=min(s[lx],s[rx]);
	MG(L[lx],L[rx]);
	MG(R[lx],R[rx]);
}
int qry(int x,int l,int r,int ll,int rr){
	if (l==ll && r==rr) return s[x];
	if (rr<=mid) return qry(L[x],l,mid,ll,rr);
	if (ll>mid) return qry(R[x],mid+1,r,ll,rr);
	return min(qry(L[x],l,mid,ll,mid),qry(R[x],mid+1,r,mid+1,rr));
}
void dfs(int x,int fa,int dep){
	d[x]=dep;
	update(rt[x],0,n,dep,w[x]);
	for (int i=0;i<v[x].size();i++){
		int y=v[x][i];
		if (y==fa) continue;
		dfs(y,x,dep+1);
		MG(rt[x],rt[y]);
	}
}
int main(){
	n=read(); root=read();
	for (int i=1;i<=n;i++) w[i]=read();
	for (int i=1;i<n;i++){
		int x=read(),y=read();
		v[x].push_back(y);
		v[y].push_back(x);
	}
	for (int i=0;i<=25*n;i++) s[i]=1e9+10;
	dfs(root,0,0);
	int m=read();
	int lastans=0;
//	s[0]=1e9+100;
	while (m--){
		int x=read(),k=read();
//		cout<<d[x]<<" "<<d[x]+k<<"\n";
//		cout<<qry(rt[x],0,n,d[x],min(d[x]+k,n))<<"\n";
		x=(x+lastans)%n+1; k=(k+lastans)%n;
		cout<<(lastans=qry(rt[x],0,n,d[x],min(d[x]+k,n)))<<"\n";
	}
	return 0;
}


#include<bits/stdc++.h>
#define LL long long
using namespace std;
int read(){
	char ch=getchar(); int w=1,c=0;
	for(;!isdigit(ch);ch=getchar()) if (ch=='-') w=-1;
	for(;isdigit(ch);ch=getchar()) c=(c<<1)+(c<<3)+(ch^48);
	return w*c;
}
const int M=1e5+10;
vector<int> v[M];
int s[M*25],L[M*25],R[M*25],w[M];
int d[M];
int rt[M],n,root,tot;
#define mid (l+r>>1)
void update(int &x,int l,int r,int p,int d){
	if (!x) x=++tot;

	if (!s[x]) s[x]=d; else s[x]=min(s[x],d);

	if (l==r) return ;
	if (p<=mid) update(L[x],l,mid,p,d);
	else update(R[x],mid+1,r,p,d);
}
void MG(int &lx,int rx){
	if (!lx || !rx) {
		lx+=rx;
		return ;
	}
	s[lx]=min(s[lx],s[rx]);
	MG(L[lx],L[rx]);
	MG(R[lx],R[rx]);
}
int qry(int x,int l,int r,int ll,int rr){
	if (l==r) return s[x];
	if (rr<=mid) return qry(L[x],l,mid,ll,rr);
	if (ll>mid) return qry(R[x],mid+1,r,ll,rr);
	return min(qry(L[x],l,mid,ll,mid),qry(R[x],mid+1,r,mid+1,rr));
}
void dfs(int x,int fa,int dep){
	d[x]=dep;
	update(rt[x],0,n,dep,w[x]);
	for (int i=0;i<v[x].size();i++){
		int y=v[x][i];
		if (y==fa) continue;
		dfs(y,x,dep+1);
		MG(rt[x],rt[y]);
	}
}
int main(){
	n=read(); root=read();
	for (int i=1;i<=n;i++) w[i]=read();
	for (int i=1;i<n;i++){
		int x=read(),y=read();
		v[x].push_back(y);
		v[y].push_back(x);
	}
	dfs(root,0,0);
	int m=read();
	int lastans=0;
	while (m--){
		int x=read(),k=read();
		x=(x+lastans)%n+1; k=(k+lastans)%n;
		cout<<(lastans=qry(rt[x],0,n,d[x],d[x]+k))<<"\n";
	}
	return 0;
}


#include<bits/stdc++.h>
#define LL long long
using namespace std;
int read(){
	char ch=getchar(); int w=1,c=0;
	for(;!isdigit(ch);ch=getchar()) if (ch=='-') w=-1;
	for(;isdigit(ch);ch=getchar()) c=(c<<1)+(c<<3)+(ch^48);
	return w*c;
}
const int M=1e5+50;
vector<int> v[M];
int s[M*40],L[M*40],R[M*40],w[M];
int d[M];
int rt[M],n,root,tot;
#define mid (l+r>>1)
void update(int &x,int l,int r,int p,int d){
	if (!x) x=++tot;

	if (!s[x]) s[x]=d; else s[x]=min(s[x],d);

	if (l==r) return ;
	if (p<=mid) update(L[x],l,mid,p,d);
	else update(R[x],mid+1,r,p,d);
}
int MG(int lx,int rx){
	int x=0;
	if (!lx || !rx) {
		return lx+rx;
	}
	x=++tot;
	s[x]=min(s[lx],s[rx]);
	L[x]=MG(L[lx],L[rx]);
	R[x]=MG(R[lx],R[rx]);
	return x;
}
int qry(int x,int l,int r,int ll,int rr){
	if (l==ll && r==rr) return s[x];
	if (rr<=mid) return qry(L[x],l,mid,ll,rr);
	if (ll>mid) return qry(R[x],mid+1,r,ll,rr);
	return min(qry(L[x],l,mid,ll,mid),qry(R[x],mid+1,r,mid+1,rr));
}
void dfs(int x,int fa,int dep){
	d[x]=dep;
	update(rt[x],0,n,dep,w[x]);
	for (int i=0;i<v[x].size();i++){
		int y=v[x][i];
		if (y==fa) continue;
		dfs(y,x,dep+1);
		rt[x]=MG(rt[x],rt[y]);
	}
}
int main(){
	n=read(); root=read();
	int mi=INT_MAX;
	for (int i=1;i<=n;i++) w[i]=read(),mi=min(mi,w[i]);
	for (int i=1;i<n;i++){
		int x=read(),y=read();
		v[x].push_back(y);
		v[y].push_back(x);
	}
	for (int i=0;i<=25*n;i++) s[i]=1e9+10;
	dfs(root,0,0);
	int m=read();
	int lastans=0;
//	s[0]=1e9+100;
//	if (n>100)cout<<mi<<"\n";
//	for (int i=0;i<=25*n;i++) assert(s[i]>=mi);
	while (m--){
		int x=read(),k=read();
//		cout<<d[x]<<" "<<d[x]+k<<"\n";
//		cout<<qry(rt[x],0,n,d[x],min(d[x]+k,n))<<"\n";
		x=((x+lastans)%n)+1; k=(k+lastans)%n;
		cout<<(lastans=qry(rt[x],0,n,d[x],min(d[x]+k,n)))<<"\n";
	}
	return 0;
}


#include<bits/stdc++.h>
#define LL long long
using namespace std;
int read(){
	char ch=getchar(); int w=1,c=0;
	for(;!isdigit(ch);ch=getchar()) if (ch=='-') w=-1;
	for(;isdigit(ch);ch=getchar()) c=(c<<1)+(c<<3)+(ch^48);
	return w*c;
}
const int M=1e5+10;
vector<int> v[M];
int s[M*25],L[M*25],R[M*25],w[M];
int d[M];
int rt[M],n,root,tot;
#define mid (l+r>>1)
void update(int &x,int l,int r,int p,int d){
	if (!x) x=++tot;

	if (!s[x]) s[x]=d; else s[x]=min(s[x],d);

	if (l==r) return ;
	if (p<=mid) update(L[x],l,mid,p,d);
	else update(R[x],mid+1,r,p,d);
}
void MG(int &lx,int rx){
	if (!lx || !rx) {
		lx+=rx;
		return ;
	}
	s[lx]=min(s[lx],s[rx]);
	MG(L[lx],L[rx]);
	MG(R[lx],R[rx]);
}
int qry(int x,int l,int r,int ll,int rr){
	if (l==r) return s[x];
	if (rr<=mid) return qry(L[x],l,mid,ll,rr);
	if (ll>mid) return qry(R[x],mid+1,r,ll,rr);
	return min(qry(L[x],l,mid,ll,mid),qry(R[x],mid+1,r,mid+1,rr));
}
void dfs(int x,int fa,int dep){
	d[x]=dep;
	update(rt[x],0,n,dep,w[x]);
	for (int i=0;i<v[x].size();i++){
		int y=v[x][i];
		if (y==fa) continue;
		dfs(y,x,dep+1);
		MG(rt[x],rt[y]);
	}
}
int main(){
	n=read(); root=read();
	for (int i=1;i<=n;i++) w[i]=read();
	for (int i=1;i<n;i++){
		int x=read(),y=read();
		v[x].push_back(y);
		v[y].push_back(x);
	}
	dfs(root,0,0);
	int m=read();
	int lastans=0;
	while (m--){
		int x=read(),k=read();
		x=(x+lastans)%n+1; k=(k+lastans)%n;
		cout<<(lastans=qry(rt[x],0,n,d[x],min(d[x]+k,n)))<<"\n";
	}
	return 0;
}


#include<bits/stdc++.h>
#define LL long long
using namespace std;
int read(){
	char ch=getchar(); int w=1,c=0;
	for(;!isdigit(ch);ch=getchar()) if (ch=='-') w=-1;
	for(;isdigit(ch);ch=getchar()) c=(c<<1)+(c<<3)+(ch^48);
	return w*c;
}
const int M=1e5+10;
vector<int> v[M];
int s[M*25],L[M*25],R[M*25],w[M];
int d[M];
int rt[M],n,root,tot;
#define mid (l+r>>1)
void update(int &x,int l,int r,int p,int d){
	if (!x) x=++tot;

	if (!s[x]) s[x]=d; else s[x]=min(s[x],d);

	if (l==r) return ;
	if (p<=mid) update(L[x],l,mid,p,d);
	else update(R[x],mid+1,r,p,d);
}
void MG(int &lx,int rx){
	if (!lx || !rx) {
		lx+=rx;
		return ;
	}
	s[lx]=min(s[lx],s[rx]);
	MG(L[lx],L[rx]);
	MG(R[lx],R[rx]);
}
int qry(int x,int l,int r,int ll,int rr){
	if (l==ll && r==rr) return s[x];
	if (rr<=mid) return qry(L[x],l,mid,ll,rr);
	if (ll>mid) return qry(R[x],mid+1,r,ll,rr);
	return min(qry(L[x],l,mid,ll,mid),qry(R[x],mid+1,r,mid+1,rr));
}
void dfs(int x,int fa,int dep){
	d[x]=dep;
	update(rt[x],0,n,dep,w[x]);
	for (int i=0;i<v[x].size();i++){
		int y=v[x][i];
		if (y==fa) continue;
		dfs(y,x,dep+1);
		MG(rt[x],rt[y]);
	}
}
int main(){
	n=read(); root=read();
	int mi=0;
	for (int i=1;i<=n;i++) w[i]=read(),mi=min(mi,w[i]);
	for (int i=1;i<n;i++){
		int x=read(),y=read();
		v[x].push_back(y);
		v[y].push_back(x);
	}
	for (int i=0;i<=25*n;i++) s[i]=1e9+10;
	dfs(root,0,0);
	int m=read();
	int lastans=0;
//	s[0]=1e9+100;
	if (n>100)cout<<mi<<"\n";
	for (int i=0;i<=25*n;i++) assert(s[i]>=mi);
	while (m--){
		int x=read(),k=read();
//		cout<<d[x]<<" "<<d[x]+k<<"\n";
//		cout<<qry(rt[x],0,n,d[x],min(d[x]+k,n))<<"\n";
		x=((x+lastans)%n)+1; k=(k+lastans)%n;
		cout<<(lastans=qry(rt[x],0,n,d[x],min(d[x]+k,n)))<<"\n";
	}
	return 0;
}


#include<bits/stdc++.h>
#define LL long long
using namespace std;
int read(){
	char ch=getchar(); int w=1,c=0;
	for(;!isdigit(ch);ch=getchar()) if (ch=='-') w=-1;
	for(;isdigit(ch);ch=getchar()) c=(c<<1)+(c<<3)+(ch^48);
	return w*c;
}
const int M=1e5+10;
vector<int> v[M];
int s[M*25],L[M*25],R[M*25],w[M];
int d[M];
int rt[M],n,root,tot;
#define mid (l+r>>1)
void update(int &x,int l,int r,int p,int d){
	if (!x) x=++tot;

	if (!s[x]) s[x]=d; else s[x]=min(s[x],d);

	if (l==r) return ;
	if (p<=mid) update(L[x],l,mid,p,d);
	else update(R[x],mid+1,r,p,d);
}
void MG(int &lx,int rx){
	if (!lx || !rx) {
		lx+=rx;
		return ;
	}
	s[lx]=min(s[lx],s[rx]);
	MG(L[lx],L[rx]);
	MG(R[lx],R[rx]);
}
int qry(int x,int l,int r,int ll,int rr){
	if (l==ll && r==rr) return s[x];
	if (rr<=mid) return qry(L[x],l,mid,ll,rr);
	if (ll>mid) return qry(R[x],mid+1,r,ll,rr);
	return min(qry(L[x],l,mid,ll,mid),qry(R[x],mid+1,r,mid+1,rr));
}
void dfs(int x,int fa,int dep){
	d[x]=dep;
	update(rt[x],0,n,dep,w[x]);
	for (int i=0;i<v[x].size();i++){
		int y=v[x][i];
		if (y==fa) continue;
		dfs(y,x,dep+1);
		MG(rt[x],rt[y]);
	}
}
int main(){
	n=read(); root=read();
	int mi=INT_MAX;
	for (int i=1;i<=n;i++) w[i]=read(),mi=min(mi,w[i]);
	for (int i=1;i<n;i++){
		int x=read(),y=read();
		v[x].push_back(y);
		v[y].push_back(x);
	}
	for (int i=0;i<=25*n;i++) s[i]=1e9+10;
	dfs(root,0,0);
	int m=read();
	int lastans=0;
//	s[0]=1e9+100;
	if (n>100)cout<<mi<<"\n";
	for (int i=0;i<=25*n;i++) assert(s[i]>=mi);
	while (m--){
		int x=read(),k=read();
//		cout<<d[x]<<" "<<d[x]+k<<"\n";
//		cout<<qry(rt[x],0,n,d[x],min(d[x]+k,n))<<"\n";
		x=((x+lastans)%n)+1; k=(k+lastans)%n;
		cout<<(lastans=qry(rt[x],0,n,d[x],min(d[x]+k,n)))<<"\n";
	}
	return 0;
}


#include<bits/stdc++.h>
#define LL long long
using namespace std;
int read(){
	char ch=getchar(); int w=1,c=0;
	for(;!isdigit(ch);ch=getchar()) if (ch=='-') w=-1;
	for(;isdigit(ch);ch=getchar()) c=(c<<1)+(c<<3)+(ch^48);
	return w*c;
}
const int M=1e5+10;
vector<int> v[M];
int s[M*25],L[M*25],R[M*25],w[M];
int d[M];
int rt[M],n,root,tot;
#define mid (l+r>>1)
void update(int &x,int l,int r,int p,int d){
	if (!x) x=++tot;

	if (!s[x]) s[x]=d; else s[x]=min(s[x],d);

	if (l==r) return ;
	if (p<=mid) update(L[x],l,mid,p,d);
	else update(R[x],mid+1,r,p,d);
}
void MG(int &lx,int rx){
	if (!lx || !rx) {
		lx+=rx;
		return ;
	}
	s[lx]=min(s[lx],s[rx]);
	MG(L[lx],L[rx]);
	MG(R[lx],R[rx]);
}
int qry(int x,int l,int r,int ll,int rr){
	if (l==ll && r==rr) return s[x];
	if (rr<=mid) return qry(L[x],l,mid,ll,rr);
	if (ll>mid) return qry(R[x],mid+1,r,ll,rr);
	return min(qry(L[x],l,mid,ll,mid),qry(R[x],mid+1,r,mid+1,rr));
}
void dfs(int x,int fa,int dep){
	d[x]=dep;
	update(rt[x],0,n,dep,w[x]);
	for (int i=0;i<v[x].size();i++){
		int y=v[x][i];
		if (y==fa) continue;
		dfs(y,x,dep+1);
		MG(rt[x],rt[y]);
	}
}
int main(){
	n=read(); root=read();
	for (int i=1;i<=n;i++) w[i]=read();
	for (int i=1;i<n;i++){
		int x=read(),y=read();
		v[x].push_back(y);
		v[y].push_back(x);
	}
	for (int i=0;i<=25*n;i++) s[i]=1e9+10;
	dfs(root,0,0);
	int m=read();
	int lastans=0;
//	s[0]=1e9+100;
	while (m--){
		int x=read(),k=read();
//		cout<<d[x]<<" "<<d[x]+k<<"\n";
//		cout<<qry(rt[x],0,n,d[x],min(d[x]+k,n))<<"\n";
		x=((x+lastans)%n)+1; k=(k+lastans)%n;
		cout<<(lastans=qry(rt[x],0,n,d[x],min(d[x]+k,n)))<<"\n";
	}
	return 0;
}


#include<bits/stdc++.h>
#define LL long long
using namespace std;
int read(){
	char ch=getchar(); int w=1,c=0;
	for(;!isdigit(ch);ch=getchar()) if (ch=='-') w=-1;
	for(;isdigit(ch);ch=getchar()) c=(c<<1)+(c<<3)+(ch^48);
	return w*c;
}
const int M=1e5+10;
vector<int> v[M];
int s[M*25],L[M*25],R[M*25],w[M];
int d[M];
int rt[M],n,root,tot;
#define mid (l+r>>1)
void update(int &x,int l,int r,int p,int d){
	if (!x) x=++tot;

	if (!s[x]) s[x]=d; else s[x]=min(s[x],d);

	if (l==r) return ;
	if (p<=mid) update(L[x],l,mid,p,d);
	else update(R[x],mid+1,r,p,d);
}
void MG(int &lx,int rx){
	if (!lx || !rx) {
		lx+=rx;
		return ;
	}
	s[lx]=min(s[lx],s[rx]);
	MG(L[lx],L[rx]);
	MG(R[lx],R[rx]);
}
int qry(int x,int l,int r,int ll,int rr){
	if (l==ll && r==rr) return s[x];
	if (rr<=mid) return qry(L[x],l,mid,ll,rr);
	if (ll>mid) return qry(R[x],mid+1,r,ll,rr);
	return min(qry(L[x],l,mid,ll,mid),qry(R[x],mid+1,r,mid+1,rr));
}
void dfs(int x,int fa,int dep){
	d[x]=dep;
	update(rt[x],0,n,dep,w[x]);
	for (int i=0;i<v[x].size();i++){
		int y=v[x][i];
		if (y==fa) continue;
		dfs(y,x,dep+1);
		MG(rt[x],rt[y]);
	}
}
int main(){
	n=read(); root=read();
	int mi=0;
	for (int i=1;i<=n;i++) w[i]=read(),mi=min(mi,w[i]);
	for (int i=1;i<n;i++){
		int x=read(),y=read();
		v[x].push_back(y);
		v[y].push_back(x);
	}
	for (int i=0;i<=25*n;i++) s[i]=1e9+10;
	dfs(root,0,0);
	int m=read();
	int lastans=0;
//	s[0]=1e9+100;
cout<<mi<<"\n";
	for (int i=0;i<=25*n;i++) assert(s[i]>=mi);
	while (m--){
		int x=read(),k=read();
//		cout<<d[x]<<" "<<d[x]+k<<"\n";
//		cout<<qry(rt[x],0,n,d[x],min(d[x]+k,n))<<"\n";
		x=((x+lastans)%n)+1; k=(k+lastans)%n;
		cout<<(lastans=qry(rt[x],0,n,d[x],min(d[x]+k,n)))<<"\n";
	}
	return 0;
}


#include<bits/stdc++.h>
#define LL long long
using namespace std;
int read(){
	char ch=getchar(); int w=1,c=0;
	for(;!isdigit(ch);ch=getchar()) if (ch=='-') w=-1;
	for(;isdigit(ch);ch=getchar()) c=(c<<1)+(c<<3)+(ch^48);
	return w*c;
}
const int M=1e5+10;
vector<int> v[M];
int s[M*25],L[M*25],R[M*25],w[M];
int d[M];
int rt[M],n,root,tot;
#define mid (l+r>>1)
void update(int &x,int l,int r,int p,int d){
	if (!x) x=++tot;

	if (!s[x]) s[x]=d; else s[x]=min(s[x],d);

	if (l==r) return ;
	if (p<=mid) update(L[x],l,mid,p,d);
	else update(R[x],mid+1,r,p,d);
}
void MG(int &lx,int rx){
	if (!lx || !rx) {
		lx+=rx;
		return ;
	}
	s[lx]=min(s[lx],s[rx]);
	MG(L[lx],L[rx]);
	MG(R[lx],R[rx]);
}
int qry(int x,int l,int r,int ll,int rr){
	if (l==ll && r==rr) return s[x];
	if (rr<=mid) return qry(L[x],l,mid,ll,rr);
	if (ll>mid) return qry(R[x],mid+1,r,ll,rr);
	return min(qry(L[x],l,mid,ll,mid),qry(R[x],mid+1,r,mid+1,rr));
}
void dfs(int x,int fa,int dep){
	d[x]=dep;
	update(rt[x],0,n,dep,w[x]);
	for (int i=0;i<v[x].size();i++){
		int y=v[x][i];
		if (y==fa) continue;
		dfs(y,x,dep+1);
		MG(rt[x],rt[y]);
	}
}
int main(){
	n=read(); root=read();
	for (int i=1;i<=n;i++) w[i]=read();
	for (int i=1;i<n;i++){
		int x=read(),y=read();
		v[x].push_back(y);
		v[y].push_back(x);
	}
	dfs(root,0,0);
	int m=read();
	int lastans=0;
	while (m--){
		int x=read(),k=read();
		x=(x+lastans)%n+1; k=(k+lastans)%n;
		cout<<(lastans=qry(rt[x],0,n,d[x],min(d[x]+k,n)))<<"\n";
	}
	return 0;
}


#include<bits/stdc++.h>
#define LL long long
using namespace std;
int read(){
	char ch=getchar(); int w=1,c=0;
	for(;!isdigit(ch);ch=getchar()) if (ch=='-') w=-1;
	for(;isdigit(ch);ch=getchar()) c=(c<<1)+(c<<3)+(ch^48);
	return w*c;
}
const int M=1e5+10;
vector<int> v[M];
int s[M*25],L[M*25],R[M*25],w[M];
int d[M];
int rt[M],n,root,tot;
#define mid (l+r>>1)
void update(int &x,int l,int r,int p,int d){
	if (!x) x=++tot;

	if (!s[x]) s[x]=d; else s[x]=min(s[x],d);

	if (l==r) return ;
	if (p<=mid) update(L[x],l,mid,p,d);
	else update(R[x],mid+1,r,p,d);
}
void MG(int &lx,int rx){
	if (!lx || !rx) {
		lx+=rx;
		return ;
	}
	s[lx]=min(s[lx],s[rx]);
	MG(L[lx],L[rx]);
	MG(R[lx],R[rx]);
}
int qry(int x,int l,int r,int ll,int rr){
	if (l==ll && r==rr) return s[x];
	if (rr<=mid) return qry(L[x],l,mid,ll,rr);
	if (ll>mid) return qry(R[x],mid+1,r,ll,rr);
	return min(qry(L[x],l,mid,ll,mid),qry(R[x],mid+1,r,mid+1,rr));
}
void dfs(int x,int fa,int dep){
	d[x]=dep;
	update(rt[x],0,n,dep,w[x]);
	for (int i=0;i<v[x].size();i++){
		int y=v[x][i];
		if (y==fa) continue;
		dfs(y,x,dep+1);
		MG(rt[x],rt[y]);
	}
}
int main(){
	n=read(); root=read();
	for (int i=1;i<=n;i++) w[i]=read();
	for (int i=1;i<n;i++){
		int x=read(),y=read();
		v[x].push_back(y);
		v[y].push_back(x);
	}
	dfs(root,0,0);
	int m=read();
	int lastans=0;
	s[0]=1e9+100;
	while (m--){
		int x=read(),k=read();
//		cout<<d[x]<<" "<<d[x]+k<<"\n";
//		cout<<qry(rt[x],0,n,d[x],min(d[x]+k,n))<<"\n";
		x=(x+lastans)%n+1; k=(k+lastans)%n;
		cout<<(lastans=qry(rt[x],0,n,d[x],min(d[x]+k,n)))<<"\n";
	}
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define M 300005

int n, m, t_root, cnt, last_ans;
int a[M], dep[M], ls[M], rs[M];
int rt[M*25], dat[M*25];

vector<int> G[M];

inline void add_edge(int u, int v)
{
	G[u].push_back(v);
	G[v].push_back(u);
}

inline int read()
{
	int x = 0, f = 1; char ch = getchar();
	while (!isdigit(ch)) { if (ch == '-') f = -1; ch = getchar(); }
	while (isdigit(ch)) { (x *= 10) += ch - '0'; ch = getchar(); }
	return x * f;
}

void upd(int &cur, int l, int r, int p, int v)
{
	if (!cur) cur = ++cnt;
	if (l == r) { dat[cur] = v; return; }
	int mid = (l + r) >> 1;
	if (p <= mid) upd(ls[cur], l, mid, p, v);
	else upd(rs[cur], mid+1, r, p, v);
	dat[cur] = min(dat[ls[cur]], dat[rs[cur]]);
}

int merge(int x, int y)
{
	if (!x || !y) return x + y;
	int cur = ++cnt; dat[cur] = min(dat[x], dat[y]);
	ls[cur] = merge(ls[x], ls[y]);
	rs[cur] = merge(rs[x], rs[y]);
	return cur;
}

void dfs(int u, int c_fa)
{
	dep[u] = dep[c_fa] + 1;
	upd(rt[u], 1, n, dep[u], a[u]);
	for (int v : G[u])
	{
		if (v == c_fa) continue;
		dfs(v, u);
		rt[u] = merge(rt[u], rt[v]);
	}
}

int query(int cur, int l, int r, int ql, int qr)
{
	if (ql <= l && qr >= r) return dat[cur]; if (!cur) return 0x3f3f3f3f;
	int mid = (l + r) >> 1, res = 0x3f3f3f3f;
	if (ql <= mid) res = min(res, query(ls[cur], l, mid, ql, qr));
	if (qr > mid) res = min(res, query(rs[cur], mid+1, r, ql, qr));
	return res;
}

int main()
{
	memset(dat, 0x3f3f3f3f, sizeof(dat));
	n = read(), t_root = read();
	for (int i = 1; i <= n; i++)
		a[i] = read();
	for (int i = 1; i < n; i++)
		add_edge(read(), read());

	dfs(t_root, 0);

	m = read();

	for (int i = 1; i <= m; i++)
	{
		int x = read(), y = read();
		x = (x + last_ans) % n + 1;
		y = (y + last_ans) % n;
		last_ans = query(rt[x], 1, n, dep[x], min(dep[x] + y, n));
		printf("%d\n", last_ans);
	}

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define M 200005

int n, m, t_root, cnt, last_ans;
int a[M], dep[M], ls[M], rs[M];
int rt[M*50], dat[M*50];

vector<int> G[M];

inline void add_edge(int u, int v)
{
	G[u].push_back(v);
	G[v].push_back(u);
}

inline int read()
{
	int x = 0, f = 1; char ch = getchar();
	while (!isdigit(ch)) { if (ch == '-') f = -1; ch = getchar(); }
	while (isdigit(ch)) { (x *= 10) += ch - '0'; ch = getchar(); }
	return x * f;
}

void upd(int &cur, int l, int r, int p, int v)
{
	if (!cur) cur = ++cnt;
	if (l == r) { dat[cur] = v; return; }
	int mid = (l + r) >> 1;
	if (p <= mid) upd(ls[cur], l, mid, p, v);
	else upd(rs[cur], mid+1, r, p, v);
	dat[cur] = min(dat[ls[cur]], dat[rs[cur]]);
}

int merge(int x, int y)
{
	if (!x || !y) return x + y;
	int cur = ++cnt; dat[cur] = min(dat[x], dat[y]);
	ls[cur] = merge(ls[x], ls[y]);
	rs[cur] = merge(rs[x], rs[y]);
	return cur;
}

void dfs(int u, int c_fa)
{
	dep[u] = dep[c_fa] + 1;
	upd(rt[u], 1, n, dep[u], a[u]);
	for (int v : G[u])
	{
		if (v == c_fa) continue;
		dfs(v, u);
		rt[u] = merge(rt[u], rt[v]);
	}
}

int query(int cur, int l, int r, int ql, int qr)
{
	if (ql <= l && qr >= r) return dat[cur]; if (!cur) return 0x3f3f3f3f;
	int mid = (l + r) >> 1, res = 0x3f3f3f3f;
	if (ql <= mid) res = min(res, query(ls[cur], l, mid, ql, qr));
	if (qr > mid) res = min(res, query(rs[cur], mid+1, r, ql, qr));
	return res;
}

int main()
{
	memset(dat, 0x3f3f3f3f, sizeof(dat));
	n = read(), t_root = read();
	for (int i = 1; i <= n; i++)
		a[i] = read();
	for (int i = 1; i < n; i++)
		add_edge(read(), read());

	dfs(t_root, 0);

	m = read();

	for (int i = 1; i <= m; i++)
	{
		int x = read(), y = read();
		x = (x + last_ans) % n + 1;
		y = (y + last_ans) % n;
		last_ans = query(rt[x], 1, n, dep[x], min(dep[x] + y, n));
		printf("%d\n", last_ans);
	}

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define M 200005

int n, m, t_root, cnt, last_ans;
int a[M], dep[M], ls[M*50], rs[M*50];
int rt[M*50], dat[M*50];

vector<int> G[M];

inline void add_edge(int u, int v)
{
	G[u].push_back(v);
	G[v].push_back(u);
}

inline int read()
{
	int x = 0, f = 1; char ch = getchar();
	while (!isdigit(ch)) { if (ch == '-') f = -1; ch = getchar(); }
	while (isdigit(ch)) { (x *= 10) += ch - '0'; ch = getchar(); }
	return x * f;
}

void upd(int &cur, int l, int r, int p, int v)
{
	if (!cur) cur = ++cnt;
	if (l == r) { dat[cur] = v; return; }
	int mid = (l + r) >> 1;
	if (p <= mid) upd(ls[cur], l, mid, p, v);
	else upd(rs[cur], mid+1, r, p, v);
	dat[cur] = min(dat[ls[cur]], dat[rs[cur]]);
}

int merge(int x, int y)
{
	if (!x || !y) return x + y;
	int cur = ++cnt; dat[cur] = min(dat[x], dat[y]);
	ls[cur] = merge(ls[x], ls[y]);
	rs[cur] = merge(rs[x], rs[y]);
	return cur;
}

void dfs(int u, int c_fa)
{
	dep[u] = dep[c_fa] + 1;
	upd(rt[u], 1, n, dep[u], a[u]);
	for (int v : G[u])
	{
		if (v == c_fa) continue;
		dfs(v, u);
		rt[u] = merge(rt[u], rt[v]);
	}
}

int query(int cur, int l, int r, int ql, int qr)
{
	if (ql <= l && qr >= r) return dat[cur]; if (!cur) return 0x3f3f3f3f;
	int mid = (l + r) >> 1, res = 0x3f3f3f3f;
	if (ql <= mid) res = min(res, query(ls[cur], l, mid, ql, qr));
	if (qr > mid) res = min(res, query(rs[cur], mid+1, r, ql, qr));
	return res;
}

int main()
{
	memset(dat, 0x3f3f3f3f, sizeof(dat));
	n = read(), t_root = read();
	for (int i = 1; i <= n; i++)
		a[i] = read();
	for (int i = 1; i < n; i++)
		add_edge(read(), read());

	dfs(t_root, 0);

	m = read();

	for (int i = 1; i <= m; i++)
	{
		int x = read(), y = read();
		x = (x + last_ans) % n + 1;
		y = (y + last_ans) % n;
		last_ans = query(rt[x], 1, n, dep[x], min(dep[x] + y, n));
		printf("%d\n", last_ans);
	}

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define M 100005

int n, m, t_root, cnt, last_ans;
int a[M], dep[M], ls[M], rs[M];
int rt[M*40], dat[M*40];

vector<int> G[M];

inline void add_edge(int u, int v)
{
	G[u].push_back(v);
	G[v].push_back(u);
}

inline int read()
{
	int x = 0, f = 1; char ch = getchar();
	while (!isdigit(ch)) { if (ch == '-') f = -1; ch = getchar(); }
	while (isdigit(ch)) { (x *= 10) += ch - '0'; ch = getchar(); }
	return x * f;
}

void upd(int &cur, int l, int r, int p, int v)
{
	if (!cur) cur = ++cnt;
	if (l == r) { dat[cur] = v; return; }
	int mid = (l + r) >> 1;
	if (p <= mid) upd(ls[cur], l, mid, p, v);
	else upd(rs[cur], mid+1, r, p, v);
	dat[cur] = min(dat[ls[cur]], dat[rs[cur]]);
}

int merge(int x, int y)
{
	if (!x || !y) return x + y;
	int cur = ++cnt; dat[cur] = min(dat[x], dat[y]);
	ls[cur] = merge(ls[x], ls[y]);
	rs[cur] = merge(rs[x], rs[y]);
	return cur;
}

void dfs(int u, int c_fa)
{
	dep[u] = dep[c_fa] + 1;
	upd(rt[u], 1, n, dep[u], a[u]);
	for (int v : G[u])
	{
		if (v == c_fa) continue;
		dfs(v, u);
		rt[u] = merge(rt[u], rt[v]);
	}
}

int query(int cur, int l, int r, int ql, int qr)
{
	if (ql <= l && qr >= r) return dat[cur]; if (!cur) return 0x3f3f3f3f;
	int mid = (l + r) >> 1, res = 0x3f3f3f3f;
	if (ql <= mid) res = min(res, query(ls[cur], l, mid, ql, qr));
	if (qr > mid) res = min(res, query(rs[cur], mid+1, r, ql, qr));
	return res;
}

int main()
{
	memset(dat, 0x3f3f3f3f, sizeof(dat));
	n = read(), t_root = read();
	for (int i = 1; i <= n; i++)
		a[i] = read();
	for (int i = 1; i < n; i++)
		add_edge(read(), read());

	dfs(t_root, 0);

	m = read();

	for (int i = 1; i <= m; i++)
	{
		int x = read(), y = read();
		x = (x + last_ans) % n + 1;
		y = (y + last_ans) % n;
		last_ans = query(rt[x], 1, n, dep[x], min(dep[x] + y, n));
		printf("%d\n", last_ans);
	}

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define M 200005

int n, m, t_root, cnt, last_ans;
int a[M], dep[M], ls[M], rs[M];
int rt[M*25], dat[M*25];

vector<int> G[M];

inline void add_edge(int u, int v)
{
	G[u].push_back(v);
	G[v].push_back(u);
}

inline int read()
{
	int x = 0, f = 1; char ch = getchar();
	while (!isdigit(ch)) { if (ch == '-') f = -1; ch = getchar(); }
	while (isdigit(ch)) { (x *= 10) += ch - '0'; ch = getchar(); }
	return x * f;
}

void upd(int &cur, int l, int r, int p, int v)
{
	if (!cur) cur = ++cnt;
	if (l == r) { dat[cur] = v; return; }
	int mid = (l + r) >> 1;
	if (p <= mid) upd(ls[cur], l, mid, p, v);
	else upd(rs[cur], mid+1, r, p, v);
	dat[cur] = min(dat[ls[cur]], dat[rs[cur]]);
}

int merge(int x, int y)
{
	if (!x || !y) return x + y;
	int cur = ++cnt; dat[cur] = min(dat[x], dat[y]);
	ls[cur] = merge(ls[x], ls[y]);
	rs[cur] = merge(rs[x], rs[y]);
	return cur;
}

void dfs(int u, int c_fa)
{
	dep[u] = dep[c_fa] + 1;
	upd(rt[u], 1, n, dep[u], a[u]);
	for (int v : G[u])
	{
		if (v == c_fa) continue;
		dfs(v, u);
		rt[u] = merge(rt[u], rt[v]);
	}
}

int query(int cur, int l, int r, int ql, int qr)
{
	if (ql <= l && qr >= r) return dat[cur]; if (!cur) return 0x3f3f3f3f;
	int mid = (l + r) >> 1, res = 0x3f3f3f3f;
	if (ql <= mid) res = min(res, query(ls[cur], l, mid, ql, qr));
	if (qr > mid) res = min(res, query(rs[cur], mid+1, r, ql, qr));
	return res;
}

int main()
{
	memset(dat, 0x3f3f3f3f, sizeof(dat));
	n = read(), t_root = read();
	for (int i = 1; i <= n; i++)
		a[i] = read();
	for (int i = 1; i < n; i++)
		add_edge(read(), read());

	dfs(t_root, 0);

	m = read();

	for (int i = 1; i <= m; i++)
	{
		int x = read(), y = read();
		x = (x + last_ans) % n + 1;
		y = (y + last_ans) % n;
		last_ans = query(rt[x], 1, n, dep[x], min(dep[x] + y, n));
		printf("%d\n", last_ans);
	}

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define M 400005

int n, m, t_root, cnt, last_ans;
int a[M], dep[M], ls[M], rs[M];
int rt[M*35], dat[M*35];

vector<int> G[M];

inline void add_edge(int u, int v)
{
	G[u].push_back(v);
	G[v].push_back(u);
}

inline int read()
{
	int x = 0, f = 1; char ch = getchar();
	while (!isdigit(ch)) { if (ch == '-') f = -1; ch = getchar(); }
	while (isdigit(ch)) { (x *= 10) += ch - '0'; ch = getchar(); }
	return x * f;
}

void upd(int &cur, int l, int r, int p, int v)
{
	if (!cur) cur = ++cnt;
	if (l == r) { dat[cur] = v; return; }
	int mid = (l + r) >> 1;
	if (p <= mid) upd(ls[cur], l, mid, p, v);
	else upd(rs[cur], mid+1, r, p, v);
	dat[cur] = min(dat[ls[cur]], dat[rs[cur]]);
}

int merge(int x, int y)
{
	if (!x || !y) return x + y;
	int cur = ++cnt; dat[cur] = min(dat[x], dat[y]);
	ls[cur] = merge(ls[x], ls[y]);
	rs[cur] = merge(rs[x], rs[y]);
	return cur;
}

void dfs(int u, int c_fa)
{
	dep[u] = dep[c_fa] + 1;
	upd(rt[u], 1, n, dep[u], a[u]);
	for (int v : G[u])
	{
		if (v == c_fa) continue;
		dfs(v, u);
		rt[u] = merge(rt[u], rt[v]);
	}
}

int query(int cur, int l, int r, int ql, int qr)
{
	if (ql <= l && qr >= r) return dat[cur]; if (!cur) return 0x3f3f3f3f;
	int mid = (l + r) >> 1, res = 0x3f3f3f3f;
	if (ql <= mid) res = min(res, query(ls[cur], l, mid, ql, qr));
	if (qr > mid) res = min(res, query(rs[cur], mid+1, r, ql, qr));
	return res;
}

int main()
{
	memset(dat, 0x3f3f3f3f, sizeof(dat));
	n = read(), t_root = read();
	for (int i = 1; i <= n; i++)
		a[i] = read();
	for (int i = 1; i < n; i++)
		add_edge(read(), read());

	dfs(t_root, 0);

	m = read();

	for (int i = 1; i <= m; i++)
	{
		int x = read(), y = read();
		x = (x + last_ans) % n + 1;
		y = (y + last_ans) % n;
		last_ans = query(rt[x], 1, n, dep[x], min(dep[x] + y, n));
		printf("%d\n", last_ans);
	}

	return 0;
}


//Zory-2018
#include<cmath>
#include<ctime>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<map>
#include<set>
#include<queue>
#include<deque>
#include<stack>
#include<bitset>
#include<vector>
#include<algorithm>
#include<iostream>
using namespace std;
#ifdef DEBUG
const int LOCAL=1;
#else
const int LOCAL=0;
#endif

namespace mine
{
	typedef long long ll;
	const int INF=0x3f3f3f3f;
	int qread()
	{
		int ans=0;char c=getchar();
		while(c<'0' or c>'9') c=getchar();
		while(c>='0' and c<='9') ans=ans*10+c-'0',c=getchar();
		return ans;
	}
	void qwrite(int num)
	{
		if(num>=10) qwrite(num/10);
		putchar('0'+num%10);
	}
	void qwriteln(int num) {qwrite(num);puts("");}

	int n;

	const int MAX_N=110000;
	struct Nod{int hou;int num;int bfsid;Nod(){hou=bfsid=0;}}p[MAX_N];
	int yz[MAX_N];//dfn->id
	struct Edge{int y,g;}e[MAX_N*2];
	int ln=0;void ins(int x,int y) {e[++ln]=(Edge){y,p[x].hou};p[x].hou=ln;}

	int down[MAX_N];//down
	void predep(int x,int fa)
	{
		down[x]=0;
		for(int k=p[x].hou;k>0;k=e[k].g)
		{
			int y=e[k].y;if(y==fa) continue;
			predep(y,x);down[x]=max(down[x],down[y]+1);
		}
	}

	//bin意义下
	int bin[30],log[MAX_N];
	int mxpos[MAX_N];
	void pre()
	{
		bin[0]=1;for(int i=1;i<=20;i++) bin[i]=bin[i-1]<<1;
		log[1]=0;for(int i=2;i<MAX_N;i++) log[i]=log[i>>1]+1;
		mxpos[1]=0;for(int i=2;i<MAX_N;i++) mxpos[i]=mxpos[i>>1]+1;
	}

	struct Tree//一切编号按照bfs序
	{
		//L、R树中，编号大为父亲
		int fa[MAX_N];
		int ff[MAX_N][30];
		vector<int> son[MAX_N];
		void pre1()
		{
			for(int i=n;i>=1;i--)
			{
				son[fa[i]].push_back(i);
				ff[i][0]=fa[i];
				for(int t=1;t<=20;t++) ff[i][t]=ff[ff[i][t-1]][t-1];
			}
		}
		int mxdep[MAX_N];
		int bson[MAX_N];//长儿子
		void pre2(int x)//0=虚根
		{
			mxdep[x]=0;bson[x]=0;
			for(int t=0;t<(int)son[x].size();t++)
			{
				int y=son[x][t];
				pre2(y);
				mxdep[x]=max(mxdep[x],mxdep[y]+1);
				if(bson[x]==0 or mxdep[y]>=mxdep[bson[x]]) bson[x]=y;
			}
		}
		int belg[MAX_N],pos[MAX_N];//所在长链、位置
		vector<int> id[MAX_N];int tot;
		void pre3(int x,int blg)
		{
			pos[x]=id[blg].size();
			id[blg].push_back(x);
			belg[x]=blg;
			if(bson[x]>0) pre3(bson[x],blg);
			for(int t=0;t<(int)son[x].size();t++) if(son[x][t]!=bson[x]) pre3(son[x][t],++tot);
		}
		vector<int> up[MAX_N];//在链顶记录，向上的，长度为链长的链
		void pre4()
		{
			//int all=0;
			for(int now=1;now<=tot;now++)
			{
				int top=id[now][0];
				int tmp=top;up[top].push_back(-1);
				for(int t=1;t<=(int)id[now].size();t++)
				{
					up[top].push_back(tmp=fa[tmp]);
					if(tmp==0) break;
				}
				//all+=up[top].size();
				//printf("all=%d\n",all);
			}
		}

		int getfa(int x,int k)
		{
			if(k==0) return -1;

			for(int t=20;t>=0;t--)
				if(ff[x][t]!=0) x=ff[x][t],k-=bin[t];
			return k==0?x:-1;

			/*int wt=mxpos[k];
			x=ff[x][wt];k-=bin[wt];
			if(pos[x]>=k) return id[belg[x]][pos[x]-k];
			else
			{
				int top=id[belg[x]][0];
				if(k-pos[x]<=(int)up[top].size()-1) return up[top][k-pos[x]];
				else return -1;//debug
			}*/
		}

		Tree() {tot=1;}
		void pre()
		{
			pre1();pre2(0);pre3(0,1);pre4();
		}
	}L,R;
	int h[MAX_N];//bfs深度
	queue<int> q;
	void bfs(int rt)//bfs序和L、R指针
	{
		int id=1;q.push(rt);h[rt]=0;
		p[rt].bfsid=id;yz[id]=rt;
		while(!q.empty())
		{
			int x=q.front();q.pop();
			bool bk=1;//是否叶子
			int st=id+1,now=p[x].bfsid;
			for(int k=p[x].hou;k>0;k=e[k].g)
			{
				int y=e[k].y;if(p[y].bfsid) continue;
				h[y]=h[x]+1;bk=0;
				p[y].bfsid=++id;yz[id]=y;
				q.push(y);
			}
			if(!bk) L.fa[now]=st,R.fa[now]=id;
			else L.fa[now]=id+1,R.fa[now]=id;

		}
		for(int i=1;i<=n;i++)//边缘
		{
			if(h[yz[i]]+1!=h[yz[L.fa[i]]]) L.fa[i]=0;
			if(h[yz[i]]+1!=h[yz[R.fa[i]]]) R.fa[i]=0;
		}
	}

	int dp[MAX_N][30][30];
	void DP()
	{
		memset(dp,63,sizeof dp);
		for(int i=n;i>=1;i--)
		{
			for(int a=0;a<=20 and i+bin[a]-1<=n and h[yz[i]]==h[yz[i+bin[a]-1]];a++)//同一层
			{
				for(int b=0;b<=20;b++)
				{
					//if(i==5 and a==0 and b==2)
					//	puts("");

					if(a==0 and b==0) dp[i][a][b]=p[yz[i]].num;
					else if(b==0) dp[i][a][b]=min(dp[i][a-1][b],dp[i+bin[a-1]][a-1][b]);
					else
					{
						int fl=L.getfa(i,bin[b-1]),fr=R.getfa(i+bin[a]-1,bin[b-1]);
						if(fl<=0 or fr<=0)
						{
							dp[i][a][b]=dp[i][a][b-1];//debug 延长转移，避免参差不齐的深度
							break;
						}
						int k=log[fr-fl+1];
						dp[i][a][b]=min(dp[i][a][b-1],min(dp[fl][k][b-1],dp[fr-bin[k]+1][k][b-1]));
					}
				}
			}
		}
	}
	int solve(int x,int cc)//层数
	{
		cc=min(cc,down[x]+1);
		if(cc==1) return p[x].num;
		else
		{
			x=p[x].bfsid;

			int k=log[cc];
			int fl,fr;
			if(cc==bin[k]) fl=fr=x;
			else fl=L.getfa(x,cc-1-bin[k]+1),fr=R.getfa(x,cc-1-bin[k]+1);//转步数
			//往下

			if(fl<0 or fr<0) puts("error");

			int k2=log[fr-fl+1];
			return min(dp[x][0][k],min(dp[fl][k2][k],dp[fr-bin[k2]+1][k2][k]));
		}
	}
	void main()
	{
		pre();

		int rt;scanf("%d%d",&n,&rt);
		for(int i=1;i<=n;i++) scanf("%d",&p[i].num);
		for(int i=1;i<=n-1;i++) {int x,y;scanf("%d%d",&x,&y);ins(x,y);ins(y,x);}

		bfs(rt);predep(rt,0);
		L.pre();R.pre();
		DP();

		int m;scanf("%d",&m);
		int lst=0;
		while(m--)
		{
			int x=qread(),dis=qread();
			x=(x+lst)%n+1;dis=(dis+lst)%n;
			qwriteln(lst=solve(x,dis+1));
		}
	}
};
int main()
{
	mine::main();
}


//Zory-2018
#include<cmath>
#include<ctime>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<map>
#include<set>
#include<queue>
#include<deque>
#include<stack>
#include<bitset>
#include<vector>
#include<algorithm>
#include<iostream>
using namespace std;
#ifdef DEBUG
const int LOCAL=1;
#else
const int LOCAL=0;
#endif

namespace mine
{
	typedef long long ll;
	const int INF=0x3f3f3f3f;
	int qread()
	{
		int ans=0;char c=getchar();
		while(c<'0' or c>'9') c=getchar();
		while(c>='0' and c<='9') ans=ans*10+c-'0',c=getchar();
		return ans;
	}
	void qwrite(int num)
	{
		if(num>=10) qwrite(num/10);
		putchar('0'+num%10);
	}
	void qwriteln(int num) {qwrite(num);puts("");}

	int n;

	const int MAX_N=110000;
	struct Nod{int hou;int num;int bfsid;Nod(){hou=bfsid=0;}}p[MAX_N];
	int yz[MAX_N];//dfn->id
	struct Edge{int y,g;}e[MAX_N*2];
	int ln=0;void ins(int x,int y) {e[++ln]=(Edge){y,p[x].hou};p[x].hou=ln;}

	//bin意义下
	int bin[30],log[MAX_N];
	int mxpos[MAX_N];
	void pre()
	{
		bin[0]=1;for(int i=1;i<=20;i++) bin[i]=bin[i-1]<<1;
		log[1]=0;for(int i=2;i<MAX_N;i++) log[i]=log[i>>1]+1;
		mxpos[1]=0;for(int i=2;i<MAX_N;i++) mxpos[i]=mxpos[i>>1]+1;
	}

	struct Tree//一切编号按照bfs序
	{
		//L、R树中，编号大为父亲
		int fa[MAX_N];
		int ff[MAX_N][30];
		vector<int> son[MAX_N];
		void pre1()
		{
			for(int i=n;i>=1;i--)
			{
				son[fa[i]].push_back(i);
				ff[i][0]=fa[i];
				for(int t=1;t<=20;t++) ff[i][t]=ff[ff[i][t-1]][t-1];
			}
		}
		int mxdep[MAX_N];
		int bson[MAX_N];//长儿子
		void pre2(int x)//0=虚根
		{
			mxdep[x]=0;bson[x]=0;
			for(int t=0;t<(int)son[x].size();t++)
			{
				int y=son[x][t];
				pre2(y);
				mxdep[x]=max(mxdep[x],mxdep[y]+1);
				if(bson[x]==0 or mxdep[y]>=mxdep[bson[x]]) bson[x]=y;
			}
		}
		int belg[MAX_N],pos[MAX_N];//所在长链、位置
		vector<int> id[MAX_N];int tot;
		void pre3(int x,int blg)
		{
			pos[x]=id[blg].size();
			id[blg].push_back(x);
			belg[x]=blg;
			if(bson[x]>0) pre3(bson[x],blg);
			for(int t=0;t<(int)son[x].size();t++) pre3(son[x][t],++tot);
		}
		vector<int> up[MAX_N];//在链顶记录，向上的，长度为链长的链
		void pre4()
		{
			for(int now=1;now<=tot;now++)
			{
				int top=id[now][0];
				int tmp=top;up[top].push_back(-1);
				for(int t=1;t<=(int)id[now].size();t++) up[top].push_back(tmp=fa[tmp]);
			}
		}

		int getfa(int x,int k)
		{
			if(k==0) return -1;
			int wt=mxpos[k];
			x=ff[x][wt];k-=bin[wt];
			if(pos[x]>=k) return id[belg[x]][pos[x]-k];
			else return up[ id[belg[x]][0] ][k-pos[x]];
		}

		Tree() {tot=1;}
		void pre()
		{
			pre1();pre2(0);pre3(0,1);pre4();
		}
	}L,R;
	int h[MAX_N];//bfs深度
	queue<int> q;
	void bfs(int rt)//bfs序和L、R指针
	{
		int id=1;q.push(rt);h[rt]=0;
		p[rt].bfsid=id;yz[id]=rt;
		while(!q.empty())
		{
			int x=q.front();q.pop();
			bool bk=1;//是否叶子
			int st=id+1,now=p[x].bfsid;
			for(int k=p[x].hou;k>0;k=e[k].g)
			{
				int y=e[k].y;if(p[y].bfsid) continue;
				h[y]=h[x]+1;bk=0;
				p[y].bfsid=++id;yz[id]=y;
				q.push(y);
			}
			if(!bk) L.fa[now]=st,R.fa[now]=id;
			else L.fa[now]=id,R.fa[now]=id+1;

		}
		for(int i=1;i<=n;i++)//边缘
		{
			if(h[yz[i]]+1!=h[yz[L.fa[i]]]) L.fa[i]=0;
			if(h[yz[i]]+1!=h[yz[R.fa[i]]]) R.fa[i]=0;
		}
	}

	int dp[MAX_N][30][30];
	void DP()
	{
		memset(dp,63,sizeof dp);
		for(int i=n;i>=1;i--)
		{
			for(int a=0;a<=20 and i+bin[a]-1<=n and h[yz[i]]==h[yz[i+bin[a]-1]];a++)//同一层
			{
				for(int b=0;b<=20 and bin[b]<=h[n]-h[yz[i]]+1;b++)
				{
					if(a==0 and b==0) dp[i][a][b]=p[yz[i]].num;
					else if(b==0) dp[i][a][b]=min(dp[i][a-1][b],dp[i+bin[a-1]][a-1][b]);
					else
					{
						int fl=L.getfa(i,bin[b-1]),fr=R.getfa(i+bin[a]-1,bin[b-1]);
						if(fl<0 or fr<0) break;
						int k=log[fr-fl+1];
						dp[i][a][b]=min(dp[i][a][b-1],min(dp[fl][k][b-1],dp[fr-bin[k]+1][k][b-1]));
					}
				}
			}
		}
	}
	int solve(int x,int cc)//层数
	{
		if(cc==1) return p[x].num;
		else
		{
			x=p[x].bfsid;

			cc=min(cc,L.mxdep[0]-L.mxdep[x]-1+1);//最大向下跳=L向上跳

			int k=log[cc];
			int fl,fr;
			if(cc==bin[k]) fl=fr=x;
			else fl=L.getfa(x,cc-1-bin[k]+1),fr=R.getfa(x,cc-1-bin[k]+1);//转步数
			//往下

			int k2=log[fr-fl+1];
			return min(dp[x][0][k],min(dp[fl][k2][k],dp[fr-bin[k2]+1][k2][k]));
		}
	}
	void main()
	{
		pre();

		int rt;scanf("%d%d",&n,&rt);
		for(int i=1;i<=n;i++) scanf("%d",&p[i].num);
		for(int i=1;i<=n-1;i++) {int x,y;scanf("%d%d",&x,&y);ins(x,y);ins(y,x);}

		bfs(rt);
		L.pre();R.pre();
		/*puts("");
		for(int i=1;i<=n;i++)
		{
			for(int t=1;t<=15;t++) printf("(%d,%d) ",L.getfa(i,t),R.getfa(i,t));
			puts("");
		}*/
		DP();

		int m;scanf("%d",&m);
		int lst=0;
		while(m--)
		{
			int x=qread(),dis=qread();
			x=(x+lst)%n+1;dis=(dis+lst)%n;
			qwriteln(lst=solve(x,dis+1));
		}
	}
};
int main()
{
	mine::main();
}


//Zory-2018
#include<cmath>
#include<ctime>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<map>
#include<set>
#include<queue>
#include<deque>
#include<stack>
#include<bitset>
#include<vector>
#include<algorithm>
#include<iostream>
using namespace std;
#ifdef DEBUG
const int LOCAL=1;
#else
const int LOCAL=0;
#endif

namespace mine
{
	typedef long long ll;
	const int INF=0x3f3f3f3f;
	int qread()
	{
		int ans=0;char c=getchar();
		while(c<'0' or c>'9') c=getchar();
		while(c>='0' and c<='9') ans=ans*10+c-'0',c=getchar();
		return ans;
	}
	void qwrite(int num)
	{
		if(num>=10) qwrite(num/10);
		putchar('0'+num%10);
	}
	void qwriteln(int num) {qwrite(num);puts("");}

	int n;

	const int MAX_N=110000;
	struct Nod{int hou;int num;int bfsid;Nod(){hou=bfsid=0;}}p[MAX_N];
	int yz[MAX_N];//dfn->id
	struct Edge{int y,g;}e[MAX_N*2];
	int ln=0;void ins(int x,int y) {e[++ln]=(Edge){y,p[x].hou};p[x].hou=ln;}

	int down[MAX_N];//down
	void predep(int x,int fa)
	{
		down[x]=0;
		for(int k=p[x].hou;k>0;k=e[k].g)
		{
			int y=e[k].y;if(y==fa) continue;
			predep(y,x);down[x]=max(down[x],down[y]+1);
		}
	}

	//bin意义下
	int bin[30],log[MAX_N];
	int mxpos[MAX_N];
	void pre()
	{
		bin[0]=1;for(int i=1;i<=20;i++) bin[i]=bin[i-1]<<1;
		log[1]=0;for(int i=2;i<MAX_N;i++) log[i]=log[i>>1]+1;
		mxpos[1]=0;for(int i=2;i<MAX_N;i++) mxpos[i]=mxpos[i>>1]+1;
	}

	struct Tree//一切编号按照bfs序
	{
		//L、R树中，编号大为父亲
		int fa[MAX_N];
		int ff[MAX_N][30];
		vector<int> son[MAX_N];
		void pre1()
		{
			for(int i=n;i>=1;i--)
			{
				son[fa[i]].push_back(i);
				ff[i][0]=fa[i];
				for(int t=1;t<=20;t++) ff[i][t]=ff[ff[i][t-1]][t-1];
			}
		}
		int mxdep[MAX_N];
		int bson[MAX_N];//长儿子
		void pre2(int x)//0=虚根
		{
			mxdep[x]=0;bson[x]=0;
			for(int t=0;t<(int)son[x].size();t++)
			{
				int y=son[x][t];
				pre2(y);
				mxdep[x]=max(mxdep[x],mxdep[y]+1);
				if(bson[x]==0 or mxdep[y]>=mxdep[bson[x]]) bson[x]=y;
			}
		}
		int belg[MAX_N],pos[MAX_N];//所在长链、位置
		vector<int> id[MAX_N];int tot;
		void pre3(int x,int blg)
		{
			pos[x]=id[blg].size();
			id[blg].push_back(x);
			belg[x]=blg;
			if(bson[x]>0) pre3(bson[x],blg);
			for(int t=0;t<(int)son[x].size();t++) if(son[x][t]!=bson[x]) pre3(son[x][t],++tot);
		}
		vector<int> up[MAX_N];//在链顶记录，向上的，长度为链长的链
		void pre4()
		{
			//int all=0;
			for(int now=1;now<=tot;now++)
			{
				int top=id[now][0];
				int tmp=top;up[top].push_back(-1);
				for(int t=1;t<=(int)id[now].size();t++)
				{
					up[top].push_back(tmp=fa[tmp]);
					if(tmp==0) break;
				}
				//all+=up[top].size();
				//printf("all=%d\n",all);
			}
		}

		int getfa(int x,int k)
		{
			if(k==0) return -1;
			int wt=mxpos[k];
			x=ff[x][wt];k-=bin[wt];
			if(pos[x]>=k) return id[belg[x]][pos[x]-k];
			else
			{
				int top=id[belg[x]][0];
				if(k-pos[x]<=(int)up[top].size()-1) return up[top][k-pos[x]];
				else return -1;//debug
			}
		}

		Tree() {tot=1;}
		void pre()
		{
			pre1();pre2(0);pre3(0,1);pre4();
		}
	}L,R;
	int h[MAX_N];//bfs深度
	queue<int> q;
	void bfs(int rt)//bfs序和L、R指针
	{
		int id=1;q.push(rt);h[rt]=0;
		p[rt].bfsid=id;yz[id]=rt;
		while(!q.empty())
		{
			int x=q.front();q.pop();
			bool bk=1;//是否叶子
			int st=id+1,now=p[x].bfsid;
			for(int k=p[x].hou;k>0;k=e[k].g)
			{
				int y=e[k].y;if(p[y].bfsid) continue;
				h[y]=h[x]+1;bk=0;
				p[y].bfsid=++id;yz[id]=y;
				q.push(y);
			}
			if(!bk) L.fa[now]=st,R.fa[now]=id;
			else L.fa[now]=id+1,R.fa[now]=id;

		}
		for(int i=1;i<=n;i++)//边缘
		{
			if(h[yz[i]]+1!=h[yz[L.fa[i]]]) L.fa[i]=0;
			if(h[yz[i]]+1!=h[yz[R.fa[i]]]) R.fa[i]=0;
		}
	}

	int dp[MAX_N][30][30];
	void DP()
	{
		memset(dp,63,sizeof dp);
		for(int i=n;i>=1;i--)
		{
			for(int a=0;a<=20 and i+bin[a]-1<=n and h[yz[i]]==h[yz[i+bin[a]-1]];a++)//同一层
			{
				for(int b=0;b<=20;b++)
				{
					//if(i==2 and a==1 and b==1)
					//	puts("");

					if(a==0 and b==0) dp[i][a][b]=p[yz[i]].num;
					else if(b==0) dp[i][a][b]=min(dp[i][a-1][b],dp[i+bin[a-1]][a-1][b]);
					else
					{
						int fl=L.getfa(i,bin[b-1]),fr=R.getfa(i+bin[a]-1,bin[b-1]);
						if(fl<0 or fr<0) break;
						int k=log[fr-fl+1];
						dp[i][a][b]=min(dp[i][a][b-1],min(dp[fl][k][b-1],dp[fr-bin[k]+1][k][b-1]));
					}
				}
			}
		}
	}
	int solve(int x,int cc)//层数
	{
		cc=min(cc,down[x]+1);
		if(cc==1) return p[x].num;
		else
		{
			x=p[x].bfsid;

			int k=log[cc];
			int fl,fr;
			if(cc==bin[k]) fl=fr=x;
			else fl=L.getfa(x,cc-1-bin[k]+1),fr=R.getfa(x,cc-1-bin[k]+1);//转步数
			//往下

			if(fl<0 or fr<0) puts("error");

			int k2=log[fr-fl+1];
			return min(dp[x][0][k],min(dp[fl][k2][k],dp[fr-bin[k2]+1][k2][k]));
		}
	}
	void main()
	{
		//freopen("tmp.in","r",stdin);

		pre();

		int rt;scanf("%d%d",&n,&rt);
		for(int i=1;i<=n;i++) scanf("%d",&p[i].num);
		for(int i=1;i<=n-1;i++) {int x,y;scanf("%d%d",&x,&y);ins(x,y);ins(y,x);}

		bfs(rt);predep(rt,0);
		L.pre();R.pre();
		/*puts("");
		for(int i=1;i<=n;i++)
		{
			for(int t=1;t<=15;t++) printf("(%d,%d) ",L.getfa(i,t),R.getfa(i,t));
			puts("");
		}*/
		DP();

		int m;scanf("%d",&m);
		int lst=0;
		while(m--)
		{
			int x=qread(),dis=qread();
			x=(x+lst)%n+1;dis=(dis+lst)%n;
			qwriteln(lst=solve(x,dis+1));
		}
	}
};
int main()
{
	mine::main();
}


//Zory-2018
#include<cmath>
#include<ctime>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<map>
#include<set>
#include<queue>
#include<deque>
#include<stack>
#include<bitset>
#include<vector>
#include<algorithm>
#include<iostream>
using namespace std;
#ifdef DEBUG
const int LOCAL=1;
#else
const int LOCAL=0;
#endif

namespace mine
{
	typedef long long ll;
	const int INF=0x3f3f3f3f;
	int qread()
	{
		int ans=0;char c=getchar();
		while(c<'0' or c>'9') c=getchar();
		while(c>='0' and c<='9') ans=ans*10+c-'0',c=getchar();
		return ans;
	}
	void qwrite(int num)
	{
		if(num>=10) qwrite(num/10);
		putchar('0'+num%10);
	}
	void qwriteln(int num) {qwrite(num);puts("");}

	int n;

	const int MAX_N=110000;
	struct Nod{int hou;int num;int bfsid;Nod(){hou=bfsid=0;}}p[MAX_N];
	int yz[MAX_N];//dfn->id
	struct Edge{int y,g;}e[MAX_N*2];
	int ln=0;void ins(int x,int y) {e[++ln]=(Edge){y,p[x].hou};p[x].hou=ln;}

	int down[MAX_N];//down
	void predep(int x,int fa)
	{
		down[x]=0;
		for(int k=p[x].hou;k>0;k=e[k].g)
		{
			int y=e[k].y;if(y==fa) continue;
			predep(y,x);down[x]=max(down[x],down[y]+1);
		}
	}

	//bin意义下
	int bin[30],log[MAX_N];
	int mxpos[MAX_N];
	void pre()
	{
		bin[0]=1;for(int i=1;i<=20;i++) bin[i]=bin[i-1]<<1;
		log[1]=0;for(int i=2;i<MAX_N;i++) log[i]=log[i>>1]+1;
		mxpos[1]=0;for(int i=2;i<MAX_N;i++) mxpos[i]=mxpos[i>>1]+1;
	}

	struct Tree//一切编号按照bfs序
	{
		//L、R树中，编号大为父亲
		int fa[MAX_N];
		int ff[MAX_N][30];
		vector<int> son[MAX_N];
		void pre1()
		{
			for(int i=n;i>=1;i--)
			{
				son[fa[i]].push_back(i);
				ff[i][0]=fa[i];
				for(int t=1;t<=20;t++) ff[i][t]=ff[ff[i][t-1]][t-1];
			}
		}
		int mxdep[MAX_N];
		int bson[MAX_N];//长儿子
		void pre2(int x)//0=虚根
		{
			mxdep[x]=0;bson[x]=0;
			for(int t=0;t<(int)son[x].size();t++)
			{
				int y=son[x][t];
				pre2(y);
				mxdep[x]=max(mxdep[x],mxdep[y]+1);
				if(bson[x]==0 or mxdep[y]>=mxdep[bson[x]]) bson[x]=y;
			}
		}
		int belg[MAX_N],pos[MAX_N];//所在长链、位置
		vector<int> id[MAX_N];int tot;
		void pre3(int x,int blg)
		{
			pos[x]=id[blg].size();
			id[blg].push_back(x);
			belg[x]=blg;
			if(bson[x]>0) pre3(bson[x],blg);
			for(int t=0;t<(int)son[x].size();t++) if(son[x][t]!=bson[x]) pre3(son[x][t],++tot);
		}
		vector<int> up[MAX_N];//在链顶记录，向上的，长度为链长的链
		void pre4()
		{
			//int all=0;
			for(int now=1;now<=tot;now++)
			{
				int top=id[now][0];
				int tmp=top;up[top].push_back(-1);
				for(int t=1;t<=(int)id[now].size();t++)
				{
					up[top].push_back(tmp=fa[tmp]);
					if(tmp==0) break;
				}
				//all+=up[top].size();
				//printf("all=%d\n",all);
			}
		}

		int getfa(int x,int k)
		{
			if(k==0) return -1;
			int wt=mxpos[k];
			x=ff[x][wt];k-=bin[wt];
			if(pos[x]>=k) return id[belg[x]][pos[x]-k];
			else
			{
				int top=id[belg[x]][0];
				if(k-pos[x]<=(int)up[top].size()-1) return up[top][k-pos[x]];
				else return -1;//debug
			}
		}

		Tree() {tot=1;}
		void pre()
		{
			pre1();pre2(0);pre3(0,1);pre4();
		}
	}L,R;
	int h[MAX_N];//bfs深度
	queue<int> q;
	void bfs(int rt)//bfs序和L、R指针
	{
		int id=1;q.push(rt);h[rt]=0;
		p[rt].bfsid=id;yz[id]=rt;
		while(!q.empty())
		{
			int x=q.front();q.pop();
			bool bk=1;//是否叶子
			int st=id+1,now=p[x].bfsid;
			for(int k=p[x].hou;k>0;k=e[k].g)
			{
				int y=e[k].y;if(p[y].bfsid) continue;
				h[y]=h[x]+1;bk=0;
				p[y].bfsid=++id;yz[id]=y;
				q.push(y);
			}
			if(!bk) L.fa[now]=st,R.fa[now]=id;
			else L.fa[now]=id+1,R.fa[now]=id;

		}
		for(int i=1;i<=n;i++)//边缘
		{
			if(h[yz[i]]+1!=h[yz[L.fa[i]]]) L.fa[i]=0;
			if(h[yz[i]]+1!=h[yz[R.fa[i]]]) R.fa[i]=0;
		}
	}

	int dp[MAX_N][30][30];
	void DP()
	{
		memset(dp,63,sizeof dp);
		for(int i=n;i>=1;i--)
		{
			for(int a=0;a<=20 and i+bin[a]-1<=n and h[yz[i]]==h[yz[i+bin[a]-1]];a++)//同一层
			{
				for(int b=0;b<=20;b++)
				{
					//if(i==2 and a==1 and b==1)
					//	puts("");

					if(a==0 and b==0) dp[i][a][b]=p[yz[i]].num;
					else if(b==0) dp[i][a][b]=min(dp[i][a-1][b],dp[i+bin[a-1]][a-1][b]);
					else
					{
						int fl=L.getfa(i,bin[b-1]),fr=R.getfa(i+bin[a]-1,bin[b-1]);
						if(fl<=0 or fr<=0) break;
						int k=log[fr-fl+1];
						dp[i][a][b]=min(dp[i][a][b-1],min(dp[fl][k][b-1],dp[fr-bin[k]+1][k][b-1]));
					}
				}
			}
		}
	}
	int solve(int x,int cc)//层数
	{
		cc=min(cc,down[x]+1);
		if(cc==1) return p[x].num;
		else
		{
			x=p[x].bfsid;

			int k=log[cc];
			int fl,fr;
			if(cc==bin[k]) fl=fr=x;
			else fl=L.getfa(x,cc-1-bin[k]+1),fr=R.getfa(x,cc-1-bin[k]+1);//转步数
			//往下

			if(fl<0 or fr<0) puts("error");

			int k2=log[fr-fl+1];
			return min(dp[x][0][k],min(dp[fl][k2][k],dp[fr-bin[k2]+1][k2][k]));
		}
	}
	void main()
	{
		//freopen("tmp.in","r",stdin);

		pre();

		int rt;scanf("%d%d",&n,&rt);
		for(int i=1;i<=n;i++) scanf("%d",&p[i].num);
		for(int i=1;i<=n-1;i++) {int x,y;scanf("%d%d",&x,&y);ins(x,y);ins(y,x);}

		bfs(rt);predep(rt,0);
		L.pre();R.pre();
		/*puts("");
		for(int i=1;i<=n;i++)
		{
			for(int t=1;t<=15;t++) printf("(%d,%d) ",L.getfa(i,t),R.getfa(i,t));
			puts("");
		}*/
		DP();

		int m;scanf("%d",&m);
		int lst=0;
		while(m--)
		{
			int x=qread(),dis=qread();
			x=(x+lst)%n+1;dis=(dis+lst)%n;
			qwriteln(lst=solve(x,dis+1));
		}
	}
};
int main()
{
	mine::main();
}