#include<iostream>
#include<vector>
using namespace std;
#define INT long long
INT number[100001];
vector< INT > g[100001];
INT timer;
INT Pos[100001];
INT Hsh[100001];
INT Sz[100001];
INT Ds[100001];
void dfs(INT node, INT pa, INT dist){
    Pos[node] = ++timer;
    Hsh[timer] = node;
    Sz[node] = 1;
    Ds[node] = dist;
    for(INT i = 0 ; i < (INT)g[node].size() ; ++i){
        INT to = g[node][i];
        if(to == pa) continue;
        dfs(to, node, dist + 1);
        Sz[node] += Sz[to];
    }
}
vector< pair< INT , INT > > seg[400000];
INT query(INT node, INT l, INT r, INT s, INT e, INT dist){
    if(s > r || l > e) return 2e9;
    if(l >= s && r <= e){
        INT b = 0;
        INT e = (INT)seg[node].size() - 1;
        INT idx = -1;
        while(b <= e){
            INT mi = (b + e)/2;
            if(seg[node][mi].first <= dist){
                idx = mi;
                b = mi + 1;
            } else
                e = mi - 1;
        }
        return seg[node][idx].second;
    }
    INT p = query(2 * node, l, (l + r)/2 , s , e, dist);
    INT q = query(2 * node + 1, (l + r)/2 + 1, r, s, e, dist);
    return min(p, q);
}
void Merge(INT node){
    INT l = 2 * node;
    INT r = l + 1;
    INT n = (INT)seg[l].size();
    INT m = (INT)seg[r].size();
    INT i = 0;
    INT j = 0;
    while(i < n && j < m){
        if(seg[l][i].first <= seg[r][j].first){
            seg[node].push_back(seg[l][i]);
            ++i;
        }else{
                seg[node].push_back(seg[r][j]);
            ++j;
        }
    }
    while(i < n) {
        seg[node].push_back(seg[l][i]) ;
        ++i;
    }
    while(j < m){
        seg[node].push_back(seg[r][j]);
        ++j;
    }
}
void build(INT node, INT l, INT r){
    if(l == r){
        seg[node].push_back({Ds[Hsh[l]], number[Hsh[l]]});
        return;
    }
    build(2 * node, l, (l + r)/2);
    build(2 * node + 1, (l + r)/2 + 1, r);
    Merge(node);
}
int main(){
    INT n, r;
    scanf("%lld %lld", &n, &r);
    for(INT i = 1; i <= n; ++i) scanf("%lld", number + i);
    INT e = n - 1;
    while(e--){
        INT u, v;
        scanf("%lld %lld", &u, &v);
        g[u].push_back(v);
        g[v].push_back(u);
    }
    dfs(r, -1, 0);
    build(1, 1, n);
    for(INT i = 0 ; i < 4 * n  ;++i){
        INT s = (INT)seg[i].size();
        for(INT j = 1 ; j < s ; ++j) seg[i][j].second = min(seg[i][j].second, seg[i][j - 1].second);
    }
    INT q;
    long long last = 0;
    scanf("%lld", &q);
    while(q--){
        INT pi, qi;
        scanf("%lld %lld", &pi, &qi);
        INT xi = (pi + last) % n + 1;
        INT ki = (qi + last) % n;
        last = query(1, 1, n, Pos[xi], Pos[xi] + Sz[xi] - 1, ki + Ds[xi]);
        printf("%lld\n", last);
    }

}


#include<cstdio>
#include<algorithm>
using namespace std;
int idx[100005],num[100005],root[100005],dep[100005];
int top=1,n,m,r,ans;
struct edge{int to,next;}e[200005];
void add(int x,int y) {e[top].to=y;e[top].next=idx[x];idx[x]=top++;}
int size=0;
struct node
{
	int ch[2];
	int val;
	void init(){ch[0]=ch[1]=0;val=2e9;}
}nd[100005<<6];
void pushup(int rt)
{
	nd[rt].val=min(nd[nd[rt].ch[0]].val,nd[nd[rt].ch[1]].val);
}
void update(int pos,int num,int l,int r,int& rt)
{
	nd[rt=++size].init();
	if(l==r)
	{
		nd[rt].val=num;
		return ;
	}
	int mid=(l+r)/2;
	if(pos<=mid) update(pos,num,l,mid,nd[rt].ch[0]);
	else update(pos,num,mid+1,r,nd[rt].ch[1]);
	pushup(rt);
}
int merge(int x,int y)
{
	if(x==0) return y;
	if(y==0) return x;
	int rt;
	nd[rt=++size].init();
	nd[rt].ch[0]=merge(nd[x].ch[0],nd[y].ch[0]);
	nd[rt].ch[1]=merge(nd[x].ch[1],nd[y].ch[1]);
	nd[rt].val=min(nd[x].val,nd[y].val);
	return rt;
}
int query(int L,int R,int l,int r,int rt)
{
	//if(rt==0) return 2e9;
	if(L<=l&&r<=R) return nd[rt].val;
	int mid=(l+r)/2;
	int ret=2e9;
	if(L<=mid) ret=min(ret,query(L,R,l,mid,nd[rt].ch[0]));
	if(mid+1<=R) ret=min(ret,query(L,R,mid+1,r,nd[rt].ch[1]));
	return ret;
}
void dfs(int x)
{
	update(dep[x],num[x],1,n,root[x]);
	for(int i=idx[x];i;i=e[i].next)
	{
		if(dep[e[i].to]==0)
		{
			dep[e[i].to]=dep[x]+1;
			dfs(e[i].to);
			root[x]=merge(root[x],root[e[i].to]);
		}
	}
}
int main()
{
	nd[0].init();
	scanf("%d%d",&n,&r);
	for(int i=1;i<=n;i++) scanf("%d",&num[i]);
	for(int i=1;i<n;i++) {int x,y;scanf("%d%d",&x,&y);add(x,y);add(y,x);}
	dep[r]=1;
	dfs(r);
	scanf("%d",&m);
	for(int i=1;i<=m;i++)
	{
		int x,y;
		scanf("%d%d",&x,&y);
		x=(x+ans)%n+1;
		y=(y+ans)%n;
		ans=query(dep[x],min(n,dep[x]+y),1,n,root[x]);
		printf("%d\n",ans);
	}
	return 0;
}


#include<cstdio>
#include<algorithm>
using namespace std;
int idx[100005],num[100005],root[100005],dep[100005];
int top=1,n,m,r,ans;
struct edge{int to,next;}e[200005];
void add(int x,int y) {e[top].to=y;e[top].next=idx[x];idx[x]=top++;}
int size=0;
inline int getint()
{
	int ret=0;
	char c=getchar();
	while(c>'9'||c<'0') c=getchar();
	while(c<='9'&&c>='0') {ret=(ret<<1)+(ret<<3)+c-'0';c=getchar();}
	return ret;
}
struct node
{
	int ch[2];
	int val;
	void init(){ch[0]=ch[1]=0;val=2e9;}
}nd[100005<<6];
void pushup(int rt)
{
	nd[rt].val=min(nd[nd[rt].ch[0]].val,nd[nd[rt].ch[1]].val);
}
void update(int pos,int num,int l,int r,int& rt)
{
	nd[rt=++size].init();
	if(l==r)
	{
		nd[rt].val=num;
		return ;
	}
	int mid=(l+r)>>1;
	if(pos<=mid) update(pos,num,l,mid,nd[rt].ch[0]);
	else update(pos,num,mid+1,r,nd[rt].ch[1]);
	pushup(rt);
}
int merge(int x,int y)
{
	if(x==0) return y;
	if(y==0) return x;
	int rt;
	nd[rt=++size].init();
	nd[rt].ch[0]=merge(nd[x].ch[0],nd[y].ch[0]);
	nd[rt].ch[1]=merge(nd[x].ch[1],nd[y].ch[1]);
	nd[rt].val=min(nd[x].val,nd[y].val);
	return rt;
}
int query(int L,int R,int l,int r,int rt)
{
	//if(rt==0) return 2e9;
	if(L<=l&&r<=R) return nd[rt].val;
	int mid=(l+r)>>1;
	int ret=2e9;
	if(L<=mid) ret=min(ret,query(L,R,l,mid,nd[rt].ch[0]));
	if(mid+1<=R) ret=min(ret,query(L,R,mid+1,r,nd[rt].ch[1]));
	return ret;
}
void dfs(int x)
{
	update(dep[x],num[x],1,n,root[x]);
	for(int i=idx[x];i;i=e[i].next)
	{
		if(dep[e[i].to]==0)
		{
			dep[e[i].to]=dep[x]+1;
			dfs(e[i].to);
			root[x]=merge(root[x],root[e[i].to]);
		}
	}
}
int main()
{
	nd[0].init();
	n=getint();
	r=getint();
	//scanf("%d%d",&n,&r);
	for(int i=1;i<=n;i++) num[i]=getint();
	for(int i=1;i<n;i++) {int x,y;x=getint();y=getint();add(x,y);add(y,x);}
	dep[r]=1;
	dfs(r);
	m=getint();
	for(int i=1;i<=m;i++)
	{
		int x,y;
		x=getint();
		y=getint();
		//scanf("%d%d",&x,&y);
		x=(x+ans)%n+1;
		y=(y+ans)%n;
		ans=query(dep[x],min(n,dep[x]+y),1,n,root[x]);
		printf("%d\n",ans);
	}
	return 0;
}


#include<cstdio>
#include<algorithm>
#include<climits>
using namespace std;
int n,m,r;
int a[100005];
int dep[100005];
int root[100005];
int last=0;
int x,k;
struct edge{int to,next;}e[200005];
int idx[100005];
int top=1;
int rsz=0;
struct node
{
    int value;
    int ch[2];
    void init(){value=LONG_MAX;ch[0]=ch[1]=0;}
}nd[6400005];
void add(int x,int y)
{
    e[top].to=y;
    e[top].next=idx[x];
    idx[x]=top++;
}
void pushup(int u)
{
    nd[u].value=min(nd[nd[u].ch[0]].value,nd[nd[u].ch[1]].value);
}
int merge(int u,int v)
{
    if(v==0) return u;
    if(u==0) return v;
    int ret=++rsz;
    nd[ret].init();
    nd[ret].ch[0]=merge(nd[u].ch[0],nd[v].ch[0]);
    nd[ret].ch[1]=merge(nd[u].ch[1],nd[v].ch[1]);
    nd[ret].value=min(nd[u].value,nd[v].value);
    return ret;
}
void update(int p,int value,int l,int r,int &rt)
{
    nd[rt=++rsz].init();
    if(l==r)
    {
        nd[rt].value=value;
        return ;
    }
    int m=(l+r)>>1;
    if(p<=m) update(p,value,l,m,nd[rt].ch[0]);
    if(p>m) update(p,value,m+1,r,nd[rt].ch[1]);
    pushup(rt);
}
void dfs(int x,int fa,int cur)
{
    dep[x]=cur;
    update(dep[x],a[x],1,n,root[x]);
    for(int i=idx[x];i;i=e[i].next)
    {
        if(fa!=e[i].to) {dfs(e[i].to,x,cur+1);
        root[x]=merge(root[x],root[e[i].to]);}
    }
}
int query(int L,int R,int l,int r,int rt)
{
    if(rt==0) return LONG_MAX;
    if(L<=l&&r<=R) return nd[rt].value;
    int m=(l+r)>>1;
    int ret=LONG_MAX;
    if(L<=m) ret=min(ret,query(L,R,l,m,nd[rt].ch[0]));
    if(R>m) ret=min(ret,query(L,R,m+1,r,nd[rt].ch[1]));
    return ret;
}
int main()
{
    scanf("%d%d",&n,&r);
    for(int i=1;i<=n;i++) scanf("%d",&a[i]);
    for(int i=1;i<n;i++)
    {
        int b,c;
        scanf("%d%d",&c,&b);
        add(c,b);
        add(b,c);
    }
    nd[0].init();
    dfs(r,0,1);
    scanf("%d",&m);
    for(int i=1;i<=m;i++)
    {
        int p,q;
        scanf("%d%d",&p,&q);
        x=(last+p)%n+1;
        k=(last+q)%n;
        last=query(dep[x],dep[x]+k,1,n,root[x]);
        printf("%d\n",last);
    }
    return 0;
}


#include<cstdio>
#include<algorithm>
using namespace std;
#define lson rt<<1,l,mid
#define rson rt<<1|1,mid+1,r
int w[10000005],ch[10000005][2];
int size[100005],dep[100005],num[100005],idx[100005],xu[100005],root[100005<<2],dir[100005];
int n,r,m,top=1,cur;
int ans;
struct edge
{
	int to,next;
}e[200005];
void add(int x,int y)
{
	e[top].to=y;
	e[top].next=idx[x];
	idx[x]=top++;
}
void dfs(int x)
{
	xu[++top]=x;
	dir[x]=top;
	size[x]=1;
	for(int i=idx[x];i;i=e[i].next)
	{
		if(dep[e[i].to]==0)
		{
			dep[e[i].to]=dep[x]+1;
			dfs(e[i].to);
			size[x]+=size[e[i].to];
		}
	}
}
void build2(int &rt,int l,int r,int pos,int num)
{
	if(rt==0) {rt=++cur;w[rt]=num;}
	else w[rt]=min(w[rt],num);
	if(l==r) return ;
	int mid=(l+r)/2;
	if(pos<=mid) build2(ch[rt][0],l,mid,pos,num);
	else build2(ch[rt][1],mid+1,r,pos,num);
}
void build1(int rt,int l,int r,int pos,int dep,int num)
{
	build2(root[rt],1,n,dep,num);
	if(l==r) return ;
	int mid=(l+r)/2;
	if(pos<=mid) build1(lson,pos,dep,num);
	else build1(rson,pos,dep,num);
}
int query2(int rt,int l,int r,int L,int R)
{
	if(rt==0) return 2e9;
	if(l>=L&&r<=R) return w[rt];
	int ret=2e9;
	int mid=(l+r)/2;
	if(L<=mid) ret=min(ret,query2(ch[rt][0],l,mid,L,R));
	if(R>=mid+1) ret=min(ret,query2(ch[rt][1],mid+1,r,L,R));
	return ret;
}
int query1(int rt,int l,int r,int L,int R,int left,int right)
{
	if(l>=L&&r<=R)
	{
		return query2(root[rt],1,n,left,right);
	}
	int mid=(l+r)/2;
	int ret=2e9;
	if(L<=mid) ret=min(ret,query1(lson,L,R,left,right));
	if(R>=mid+1) ret=min(ret,query1(rson,L,R,left,right));
	return ret;
}
int main()
{
	scanf("%d%d",&n,&r);
	for(int i=1;i<=n;i++) scanf("%d",&num[i]);
	for(int i=1;i<n;i++){int x,y;scanf("%d%d",&x,&y);add(x,y);add(y,x);}
	top=0;dep[r]=1;dfs(r);
	for(int i=1;i<=n;i++)
	{
		build1(1,1,n,i,dep[xu[i]],num[xu[i]]);
	}
	scanf("%d",&m);
	for(int i=1;i<=m;i++)
	{
		int x,y;
		scanf("%d%d",&x,&y);
		x=(x+ans)%n+1;
		y=(y+ans)%n;
		ans=query1(1,1,n,dir[x],dir[x]+size[x]-1,dep[x],min(n,y+dep[x]));
		printf("%d\n",ans);
	}
	return 0;
}


#include<cstdio>
#include<algorithm>
using namespace std;
int idx[100005],num[100005],root[100005],dep[100005];
int top=1,n,m,r,ans;
struct edge{int to,next;}e[200005];
void add(int x,int y) {e[top].to=y;e[top].next=idx[x];idx[x]=top++;}
int size=0;
struct node
{
	int ch[2];
	int val;
	void init(){ch[0]=ch[1]=0;val=2e9;}
}nd[100005<<5];
void pushup(int rt)
{
	nd[rt].val=min(nd[nd[rt].ch[0]].val,nd[nd[rt].ch[1]].val);
}
void update(int pos,int num,int l,int r,int& rt)
{
	nd[rt=++size].init();
	if(l==r)
	{
		nd[rt].val=num;
		return ;
	}
	int mid=(l+r)/2;
	if(pos<=mid) update(pos,num,l,mid,nd[rt].ch[0]);
	else update(pos,num,mid+1,r,nd[rt].ch[1]);
	pushup(rt);
}
int merge(int x,int y)
{
	if(x==0) return y;
	if(y==0) return x;
	int rt;
	nd[rt=++size].init();
	nd[rt].ch[0]=merge(nd[x].ch[0],nd[y].ch[0]);
	nd[rt].ch[1]=merge(nd[x].ch[1],nd[y].ch[1]);
	nd[rt].val=min(nd[x].val,nd[y].val);
	return rt;
}
int query(int L,int R,int l,int r,int rt)
{
	if(L<=l&&r<=R) return nd[rt].val;
	int mid=(l+r)/2;
	int ret=2e9;
	if(L<=mid) ret=min(ret,query(L,R,l,mid,nd[rt].ch[0]));
	if(mid+1<=R) ret=min(ret,query(L,R,mid+1,r,nd[rt].ch[1]));
	return ret;
}
void dfs(int x)
{
	update(dep[x],num[x],1,n,root[x]);
	for(int i=idx[x];i;i=e[i].next)
	{
		if(dep[e[i].to]==0)
		{
			dep[e[i].to]=dep[x]+1;
			dfs(e[i].to);
			root[x]=merge(root[x],root[e[i].to]);
		}
	}
}
int main()
{
	nd[0].init();
	scanf("%d%d",&n,&r);
	for(int i=1;i<=n;i++) scanf("%d",&num[i]);
	for(int i=1;i<n;i++) {int x,y;scanf("%d%d",&x,&y);add(x,y);add(y,x);}
	dep[r]=1;
	dfs(r);
	scanf("%d",&m);
	for(int i=1;i<=m;i++)
	{
		int x,y;
		scanf("%d%d",&x,&y);
		x=(x+ans)%n+1;
		y=(y+ans)%n;
		ans=query(dep[x],min(n,dep[x]+y),1,n,root[x]);
		printf("%d\n",ans);
	}
	return 0;
}


#include<algorithm>
#include<cstdio>
#include<vector>
#define mxn 1000010
#define pb push_back
using namespace std;
int n,m,rt,ans,sl,fh,hd,tl,a[mxn],q[mxn],pw[mxn],lg[mxn],ps[mxn],f[100010][20][20];
int t,h[mxn];
struct Tre
{
	int to,nxt;
}e[mxn<<1];
struct Tree
{
	int dep[mxn],mxl[mxn],son[mxn],top[mxn],f[mxn][20];
	int tp,stk[mxn];
	int t,h[mxn];
	struct Tre
	{
		int to,nxt;
	}e[mxn<<1];
	vector<int> p[mxn];
	void add(int u,int v) {e[++t]=(Tre){v,h[u]};h[u]=t;}
	void dfs1(int u)
	{
		for(int i=1;i<=lg[dep[u]];++i) f[u][i]=f[f[u][i-1]][i-1];
		int v;mxl[u]=1;son[u]=-1;
		for(int i=h[u];i;i=e[i].nxt)
		{
			dep[v=e[i].to]=dep[u]+1;dfs1(v);
			if(mxl[v]+1>mxl[u]) son[u]=v,mxl[u]=mxl[v]+1;
		}
	}
	void dfs2(int u,int tp)
	{
		top[u]=tp;
		if(~son[u]) dfs2(son[u],tp);
		int v;
		for(int i=h[u];i;i=e[i].nxt)
			if((v=e[i].to)!=son[u])
				dfs2(v,v);
	}
	void init()
	{
		for(int i=1;i<=n;++i) add(f[i][0],i);
		dep[0]=1;dfs1(0);dfs2(0,0);
		for(int x,i=0;i<=n;++i)
			if(top[i]==i)
			{
				for(x=f[i][0],tp=0;tp<mxl[i];stk[++tp]=x,x=f[x][0]);
				for(;tp;p[i].pb(stk[tp--]));
				for(x=i;~x;p[i].pb(x),x=son[x]);
			}
	}
	int qry(int x,int k)
	{
		if(!k) return x;
		if(dep[x]<k) return 0;
		int u=top[f[x][lg[k]]];
		return p[u][dep[x]-k-dep[u]+mxl[u]];
	}
}TL,TR;
vector<int> vt[mxn];
int rd()
{
	sl=0;fh=1;
	char ch=getchar();
	while(ch<'0'||'9'<ch) {if(ch=='-') fh=-1; ch=getchar();}
	while('0'<=ch&&ch<='9') sl=sl*10+ch-'0',ch=getchar();
	return sl*fh;
}
void add(int u,int v)
{
	e[++t]=(Tre){v,h[u]};h[u]=t;
	e[++t]=(Tre){u,h[v]};h[v]=t;
}
int L[mxn],R[mxn],vis[mxn],dep[mxn];
int main()
{
	n=rd();rt=rd();int x,y;pw[0]=1;
	for(int i=2;i<=n+1;++i) lg[i]=lg[i>>1]+1;
	for(int i=1;i<=20;++i) pw[i]=2*pw[i-1];
	for(int i=1;i<=n;++i) a[i]=rd();
	for(int i=1;i<n;++i) x=rd(),y=rd(),add(x,y);
	q[++tl]=rt;dep[rt]=1;int u,v;vis[rt]=1;
	for(;hd<tl;)
	{
		u=q[++hd];L[u]=tl+1;ps[u]=hd;vt[dep[u]].pb(u);
		for(int i=h[u];i;i=e[i].nxt)
			if(!vis[v=e[i].to])
				vis[q[++tl]=v]=1,dep[v]=dep[u]+1;
		R[u]=tl;
	}
	for(int i=1;i<=n;++i)
	{
		if(dep[q[L[i]]]>dep[i]) TL.f[i][0]=q[L[i]];
		if(dep[q[R[i]]]>dep[i]) TR.f[i][0]=q[R[i]];
		m=max(m,dep[i]);f[i][0][0]=a[i];
	}
	if(n>10) {printf("%d\n",m); for(int i=1;i<=n;++i) printf("%d ",q[i]); puts("");}
	TL.init();TR.init();
	int sz;
	for(int i=1;i<=m;++i)
	{
		sz=vt[i].size();
		for(int j=1;j<20;++j)
			for(int k=0;k+pw[j]<=sz;++k)
				f[vt[i][k]][j][0]=min(f[vt[i][k]][j-1][0],f[vt[i][k+pw[j-1]]][j-1][0]);
	}
	for(int d=1;d<=m;++d)
	{
		sz=vt[d].size();
		for(int i=0;i<20;++i)
			for(int j=1;j<20;++j)
				for(int k=0;k+pw[i]<=sz;++k)
				{
					x=TL.f[u=vt[d][k]][j-1];y=TR.f[vt[d][k+pw[i]-1]][j-1];
					if(x&&y&&ps[x]<=ps[y])
					{
						v=lg[ps[y]-ps[x]+1];
						f[u][i][j]=min(f[u][i][j-1],min(f[x][v][j-1],f[q[ps[y]-pw[v]+1]][v][j-1]));
					}
				}
	}
	int k;
	for(int Q=rd();Q;--Q)
	{
		x=(rd()+ans)%n+1;y=(rd()+ans)%n;
		if(y)
		{
			ans=f[x][0][lg[y]];
			u=TL.qry(x,y-pw[lg[y]]);v=TR.qry(x,y-pw[lg[y]]);
			if(u&&v&&ps[u]<=ps[v])
			{
				k=lg[ps[v]-ps[u]+1];
				ans=min(ans,min(f[u][k][lg[y]],f[q[ps[v]-pw[k]+1]][k][lg[y]]));
			}
			printf("%d\n",ans);
		}
		else printf("%d\n",a[x]);
	}
	return 0;
}


#include<algorithm>
#include<cstdio>
#include<vector>
#define mxn 1000010
#define pb push_back
using namespace std;
int n,m,rt,ans,sl,fh,hd,tl,a[mxn],q[mxn],pw[mxn],lg[mxn],ps[mxn],f[100010][20][20];
int t,h[mxn];
struct Tre
{
	int to,nxt;
}e[mxn<<1];
struct Tree
{
	int dep[mxn],mxl[mxn],son[mxn],top[mxn],f[mxn][20];
	int tp,stk[mxn];
	int t,h[mxn];
	struct Tre
	{
		int to,nxt;
	}e[mxn<<1];
	vector<int> p[mxn];
	void add(int u,int v) {e[++t]=(Tre){v,h[u]};h[u]=t;}
	void dfs1(int u)
	{
		for(int i=1;i<=lg[dep[u]];++i) f[u][i]=f[f[u][i-1]][i-1];
		int v;mxl[u]=1;son[u]=-1;
		for(int i=h[u];i;i=e[i].nxt)
		{
			dep[v=e[i].to]=dep[u]+1;dfs1(v);
			if(mxl[v]+1>mxl[u]) son[u]=v,mxl[u]=mxl[v]+1;
		}
	}
	void dfs2(int u,int tp)
	{
		top[u]=tp;
		if(~son[u]) dfs2(son[u],tp);
		int v;
		for(int i=h[u];i;i=e[i].nxt)
			if((v=e[i].to)!=son[u])
				dfs2(v,v);
	}
	void init()
	{
		for(int i=1;i<=n;++i) add(f[i][0],i);
		dep[0]=1;dfs1(0);dfs2(0,0);
		for(int x,i=0;i<=n;++i)
			if(top[i]==i)
			{
				for(x=f[i][0],tp=0;tp<mxl[i];stk[++tp]=x,x=f[x][0]);
				for(;tp;p[i].pb(stk[tp--]));
				for(x=i;~x;p[i].pb(x),x=son[x]);
			}
	}
	int qry(int x,int k)
	{
		if(!k) return x;
		if(dep[x]<=k) return 0;
		int u=top[f[x][lg[k]]];
		return p[u][dep[x]-k-dep[u]+mxl[u]];
	}
}TL,TR;
vector<int> vt[mxn];
int rd()
{
	sl=0;fh=1;
	char ch=getchar();
	while(ch<'0'||'9'<ch) {if(ch=='-') fh=-1; ch=getchar();}
	while('0'<=ch&&ch<='9') sl=sl*10+ch-'0',ch=getchar();
	return sl*fh;
}
void add(int u,int v)
{
	e[++t]=(Tre){v,h[u]};h[u]=t;
	e[++t]=(Tre){u,h[v]};h[v]=t;
}
int L[mxn],R[mxn],vis[mxn],dep[mxn];
int main()
{
	n=rd();rt=rd();int x,y;pw[0]=1;
	for(int i=2;i<=n+1;++i) lg[i]=lg[i>>1]+1;
	for(int i=1;i<=20;++i) pw[i]=2*pw[i-1];
	for(int i=1;i<=n;++i) a[i]=rd();
	for(int i=1;i<n;++i) x=rd(),y=rd(),add(x,y);
	q[++tl]=rt;dep[rt]=1;int u,v;vis[rt]=1;
	for(;hd<tl;)
	{
		u=q[++hd];L[u]=tl+1;ps[u]=hd;vt[dep[u]].pb(u);
		for(int i=h[u];i;i=e[i].nxt)
			if(!vis[v=e[i].to])
				vis[q[++tl]=v]=1,dep[v]=dep[u]+1;
		R[u]=tl;
	}
	for(int i=1;i<=n;++i)
	{
		if(dep[q[L[i]]]>dep[i]) TL.f[i][0]=q[L[i]];
		if(dep[q[R[i]]]>dep[i]) TR.f[i][0]=q[R[i]];
		m=max(m,dep[i]);f[i][0][0]=a[i];
	}
	TL.init();TR.init();
	int sz;
	for(int i=1;i<=m;++i)
	{
		sz=vt[i].size();
		for(int j=1;j<20;++j)
			for(int k=0;k+pw[j]<=sz;++k)
				f[vt[i][k]][j][0]=min(f[vt[i][k]][j-1][0],f[vt[i][k+pw[j-1]]][j-1][0]);
	}
	for(int d=m;d;--d)
	{
		sz=vt[d].size();
		for(int i=0;i<20;++i)
			for(int j=1;j<20;++j)
				for(int k=0;k+pw[i]<=sz;++k)
				{
					x=TL.f[u=vt[d][k]][j-1];y=TR.f[vt[d][k+pw[i]-1]][j-1];
					f[u][i][j]=f[u][i][j-1];
					if(x&&y&&ps[x]<=ps[y])
					{
						v=lg[ps[y]-ps[x]+1];
						f[u][i][j]=min(f[u][i][j],min(f[x][v][j-1],f[q[ps[y]-pw[v]+1]][v][j-1]));
					}
				}
	}
	int k;
	for(int Q=rd();Q;--Q)
	{
		x=(rd()+ans)%n+1;y=(rd()+ans)%n;
		if(y)
		{
			ans=f[x][0][lg[y]];
			u=TL.qry(x,y-pw[lg[y]]);v=TR.qry(x,y-pw[lg[y]]);
			if(u&&v&&ps[u]<=ps[v])
			{
				k=lg[ps[v]-ps[u]+1];
				ans=min(ans,min(f[u][k][lg[y]],f[q[ps[v]-pw[k]+1]][k][lg[y]]));
			}
			printf("%d\n",ans);
		}
		else printf("%d\n",a[x]);
	}
	return 0;
}


#include<algorithm>
#include<cstdio>
#include<vector>
#define mxn 1000010
#define pb push_back
using namespace std;
int n,m,rt,ans,sl,fh,hd,tl,a[mxn],q[mxn],pw[mxn],lg[mxn],ps[mxn],f[100010][20][20];
int t,h[mxn];
struct Tre
{
	int to,nxt;
}e[mxn<<1];
struct Tree
{
	int dep[mxn],mxl[mxn],son[mxn],top[mxn],f[mxn][20];
	int tp,stk[mxn];
	int t,h[mxn];
	struct Tre
	{
		int to,nxt;
	}e[mxn<<1];
	vector<int> p[mxn];
	void add(int u,int v) {e[++t]=(Tre){v,h[u]};h[u]=t;}
	void dfs1(int u)
	{
		for(int i=1;i<=lg[dep[u]];++i) f[u][i]=f[f[u][i-1]][i-1];
		int v;mxl[u]=1;son[u]=-1;
		for(int i=h[u];i;i=e[i].nxt)
		{
			dep[v=e[i].to]=dep[u]+1;dfs1(v);
			if(mxl[v]+1>mxl[u]) son[u]=v,mxl[u]=mxl[v]+1;
		}
	}
	void dfs2(int u,int tp)
	{
		top[u]=tp;
		if(~son[u]) dfs2(son[u],tp);
		int v;
		for(int i=h[u];i;i=e[i].nxt)
			if((v=e[i].to)!=son[u])
				dfs2(v,v);
	}
	void init()
	{
		for(int i=1;i<=n;++i) add(f[i][0],i);
		dep[0]=1;dfs1(0);dfs2(0,0);
		for(int x,i=0;i<=n;++i)
			if(top[i]==i)
			{
				for(x=f[i][0],tp=0;tp<mxl[i];stk[++tp]=x,x=f[x][0]);
				for(;tp;p[i].pb(stk[tp--]));
				for(x=i;~x;p[i].pb(x),x=son[x]);
			}
	}
	int qry(int x,int k)
	{
		if(!k) return x;
		if(dep[x]<k) return 0;
		int u=top[f[x][lg[k]]];
		return p[u][dep[x]-k-dep[u]+mxl[u]];
	}
}TL,TR;
vector<int> vt[mxn];
int rd()
{
	sl=0;fh=1;
	char ch=getchar();
	while(ch<'0'||'9'<ch) {if(ch=='-') fh=-1; ch=getchar();}
	while('0'<=ch&&ch<='9') sl=sl*10+ch-'0',ch=getchar();
	return sl*fh;
}
void add(int u,int v)
{
	e[++t]=(Tre){v,h[u]};h[u]=t;
	e[++t]=(Tre){u,h[v]};h[v]=t;
}
int L[mxn],R[mxn],vis[mxn],dep[mxn];
int main()
{
	n=rd();rt=rd();int x,y;pw[0]=1;
	for(int i=2;i<=n+1;++i) lg[i]=lg[i>>1]+1;
	for(int i=1;i<=20;++i) pw[i]=2*pw[i-1];
	for(int i=1;i<=n;++i) a[i]=rd();
	for(int i=1;i<n;++i) x=rd(),y=rd(),add(x,y);
	q[++tl]=rt;dep[rt]=1;int u,v;vis[rt]=1;
	for(;hd<tl;)
	{
		u=q[++hd];L[u]=tl+1;ps[u]=hd;vt[dep[u]].pb(u);
		for(int i=h[u];i;i=e[i].nxt)
			if(!vis[v=e[i].to])
				vis[q[++tl]=v]=1,dep[v]=dep[u]+1;
		R[u]=tl;
	}
	for(int i=1;i<=n;++i)
	{
		if(dep[q[L[i]]]>dep[i]) TL.f[i][0]=q[L[i]];
		if(dep[q[R[i]]]>dep[i]) TR.f[i][0]=q[R[i]];
		m=max(m,dep[i]);f[i][0][0]=a[i];
	}
//	if(n>10) {printf("%d\n",m); for(int i=1;i<=n;++i) printf("%d ",q[i]); puts("");}
	TL.init();TR.init();
	int sz;
	for(int i=1;i<=m;++i)
	{
		sz=vt[i].size();
		for(int j=1;j<20;++j)
			for(int k=0;k+pw[j]<=sz;++k)
				f[vt[i][k]][j][0]=min(f[vt[i][k]][j-1][0],f[vt[i][k+pw[j-1]]][j-1][0]);
	}
	for(int d=1;d<=m;++d)
	{
		sz=vt[d].size();
		for(int i=0;i<20;++i)
			for(int j=1;j<20;++j)
				for(int k=0;k+pw[i]<=sz;++k)
				{
					x=TL.f[u=vt[d][k]][j-1];y=TR.f[vt[d][k+pw[i]-1]][j-1];
					f[u][i][j]=f[u][i][j-1];
					if(x&&y&&ps[x]<=ps[y])
					{
						v=lg[ps[y]-ps[x]+1];
						f[u][i][j]=min(f[u][i][j],min(f[x][v][j-1],f[q[ps[y]-pw[v]+1]][v][j-1]));
					}
				}
	}
	int k;
	for(int Q=rd();Q;--Q)
	{
		x=(rd()+ans)%n+1;y=(rd()+ans)%n;
		if(y)
		{
			ans=f[x][0][lg[y]];
			u=TL.qry(x,y-pw[lg[y]]);v=TR.qry(x,y-pw[lg[y]]);
			if(u&&v&&ps[u]<=ps[v])
			{
				k=lg[ps[v]-ps[u]+1];
				ans=min(ans,min(f[u][k][lg[y]],f[q[ps[v]-pw[k]+1]][k][lg[y]]));
			}
			printf("%d\n",ans);
		}
		else printf("%d\n",a[x]);
	}
	return 0;
}


#include<algorithm>
#include<cstdio>
#include<vector>
#define mxn 1000010
#define pb push_back
using namespace std;
int n,m,rt,ans,sl,fh,hd,tl,a[mxn],q[mxn],pw[mxn],lg[mxn],ps[mxn],f[100010][20][20];
int t,h[mxn];
struct Tre
{
	int to,nxt;
}e[mxn<<1];
struct Tree
{
	int dep[mxn],mxl[mxn],son[mxn],top[mxn],f[mxn][20];
	int tp,stk[mxn];
	int t,h[mxn];
	struct Tre
	{
		int to,nxt;
	}e[mxn<<1];
	vector<int> p[mxn];
	void add(int u,int v) {e[++t]=(Tre){v,h[u]};h[u]=t;}
	void dfs1(int u)
	{
		for(int i=1;i<=lg[dep[u]];++i) f[u][i]=f[f[u][i-1]][i-1];
		int v;mxl[u]=1;son[u]=-1;
		for(int i=h[u];i;i=e[i].nxt)
		{
			dep[v=e[i].to]=dep[u]+1;dfs1(v);
			if(mxl[v]+1>mxl[u]) son[u]=v,mxl[u]=mxl[v]+1;
		}
	}
	void dfs2(int u,int tp)
	{
		top[u]=tp;
		if(~son[u]) dfs2(son[u],tp);
		int v;
		for(int i=h[u];i;i=e[i].nxt)
			if((v=e[i].to)!=son[u])
				dfs2(v,v);
	}
	void init()
	{
		for(int i=1;i<=n;++i) add(f[i][0],i);
		dep[0]=1;dfs1(0);dfs2(0,0);
		for(int x,i=0;i<=n;++i)
			if(top[i]==i)
			{
				for(x=f[i][0],tp=0;tp<mxl[i];stk[++tp]=x,x=f[x][0]);
				for(;tp;p[i].pb(stk[tp--]));
				for(x=i;~x;p[i].pb(x),x=son[x]);
			}
	}
	int qry(int x,int k)
	{
		if(!k) return x;
		if(dep[x]<k) return 0;
		int u=top[f[x][lg[k]]];
		return p[u][dep[x]-k-dep[u]+mxl[u]];
	}
}TL,TR;
vector<int> vt[mxn];
int rd()
{
	sl=0;fh=1;
	char ch=getchar();
	while(ch<'0'||'9'<ch) {if(ch=='-') fh=-1; ch=getchar();}
	while('0'<=ch&&ch<='9') sl=sl*10+ch-'0',ch=getchar();
	return sl*fh;
}
void add(int u,int v)
{
	e[++t]=(Tre){v,h[u]};h[u]=t;
	e[++t]=(Tre){u,h[v]};h[v]=t;
}
int L[mxn],R[mxn],vis[mxn],dep[mxn];
int main()
{
	n=rd();rt=rd();int x,y;pw[0]=1;
	for(int i=2;i<=n+1;++i) lg[i]=lg[i>>1]+1;
	for(int i=1;i<=20;++i) pw[i]=2*pw[i-1];
	for(int i=1;i<=n;++i) a[i]=rd();
	for(int i=1;i<n;++i) x=rd(),y=rd(),add(x,y);
	q[++tl]=rt;dep[rt]=1;int u,v;vis[rt]=1;
	for(;hd<tl;)
	{
		u=q[++hd];L[u]=tl+1;ps[u]=hd;vt[dep[u]].pb(u);
		for(int i=h[u];i;i=e[i].nxt)
			if(!vis[v=e[i].to])
				vis[q[++tl]=v]=1,dep[v]=dep[u]+1;
		R[u]=tl;
	}
	for(int i=1;i<=n;++i)
	{
		if(dep[q[L[i]]]>dep[i]) TL.f[i][0]=q[L[i]];
		if(dep[q[R[i]]]>dep[i]) TR.f[i][0]=q[R[i]];
		m=max(m,dep[i]);f[i][0][0]=a[i];
	}
	if(n>10) {printf("%d\n",m); for(int i=1;i<=n;++i) printf("%d ",q[i]); puts("");}
	TL.init();TR.init();
	int sz;
	for(int i=1;i<=m;++i)
	{
		sz=vt[i].size();
		for(int j=1;j<20;++j)
			for(int k=0;k+pw[j]<=sz;++k)
				f[vt[i][k]][j][0]=min(f[vt[i][k]][j-1][0],f[vt[i][k+pw[j-1]]][j-1][0]);
	}
	for(int d=m;d;--d)
	{
		sz=vt[d].size();
		for(int i=0;i<20;++i)
			for(int j=1;j<20;++j)
				for(int k=0;k+pw[i]<=sz;++k)
				{
					x=TL.f[u=vt[d][k]][j-1];y=TR.f[vt[d][k+pw[i]-1]][j-1];
					f[u][i][j]=f[u][i][j-1];
					if(x&&y&&ps[x]<=ps[y])
					{
						v=lg[ps[y]-ps[x]+1];
						f[u][i][j]=min(f[u][i][j],min(f[x][v][j-1],f[q[ps[y]-pw[v]+1]][v][j-1]));
					}
				}
	}
	int k;
	for(int Q=rd();Q;--Q)
	{
		x=(rd()+ans)%n+1;y=(rd()+ans)%n;
		if(y)
		{
			ans=f[x][0][lg[y]];
			u=TL.qry(x,y-pw[lg[y]]);v=TR.qry(x,y-pw[lg[y]]);
			if(u&&v&&ps[u]<=ps[v])
			{
				k=lg[ps[v]-ps[u]+1];
				ans=min(ans,min(f[u][k][lg[y]],f[q[ps[v]-pw[k]+1]][k][lg[y]]));
			}
			printf("%d\n",ans);
		}
		else printf("%d\n",a[x]);
	}
	return 0;
}


#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#define mid ((l+r)>>1)
using namespace std;
const int mxn=100010;
int n,rt,q,lg[mxn];
struct ed{int to,nxt,val;};
struct kth_fa{
	int m,fa[mxn],head[mxn],dep[mxn],dis[mxn],son[mxn],top[mxn];
	int tot,tp,p[mxn][21],id[mxn],stk[mxn];
	vector<int> a[mxn];
	ed edge[mxn<<1];
	void addedge(int u,int v){
		edge[++m]=(ed){v,head[u]},head[u]=m;
		edge[++m]=(ed){u,head[v]},head[v]=m;
	}
	void dfs1(int u,int fa,int d){
		dep[u]=1,p[u][0]=fa,dis[u]=d;
		for (int i=1;i<=20;++i)
			p[u][i]=p[p[u][i-1]][i-1];
		for (int i=head[u],v;i;i=edge[i].nxt)
			if ((v=edge[i].to)!=fa){
				dfs1(v,u,d+1);
				if (dep[v]+1>dep[u]) dep[u]=dep[v]+1,son[u]=v;
			}
	}
	void dfs2(int u,int fa,int tp){
		top[u]=tp;
		for (int i=head[u],v;i;i=edge[i].nxt)
			if ((v=edge[i].to)!=fa)
				if (v==son[u]) dfs2(v,u,tp);
				else dfs2(v,u,v);
			else;
	}
	void init(){
		m=0;
		for (int i=1;i<=n;++i) addedge(i,fa[i]);
		dfs1(0,0,1),dfs2(0,0,0);
		for (int u=0;u<=n;++u)
			if (top[u]==u){
				id[u]=++tot,tp=0;
				for (int i=1,v=u;i<=dep[u];++i) stk[++tp]=(v=fa[v]);
				for (;tp;a[tot].push_back(stk[tp--]));
				for (int i=1,v=u;i<=dep[u];++i) a[tot].push_back(v),v=son[v];
			}
	}
	int qry(int u,int k){
		if (!k) return u;
		if (dis[u]<=k) return 0;
		int x=top[p[u][lg[k]]];
		return a[id[x]][dis[u]-k-dis[x]+dep[x]];
	}
}TL,TR;
ed edge[mxn<<1];
int m,tot,head[mxn],dep[mxn],id[mxn],idx[mxn],idy[mxn],f[mxn][21][21];
vector<int> b[mxn];
void addedge(int u,int v){
	edge[++m]=(ed){v,head[u]},head[u]=m;
	edge[++m]=(ed){u,head[v]},head[v]=m;
}
void dfs(int u,int fa,int d){
	idx[u]=++tot,dep[u]=d;
	id[u]=b[d].size(),b[d].push_back(u);
	for (int i=head[u],v;i;i=edge[i].nxt)
		if ((v=edge[i].to)!=fa) dfs(v,u,d+1);
	idy[u]=tot;
}
int main()
{
	scanf("%d%d",&n,&rt);
	for (int i=1;i<=n;++i) scanf("%d",&f[i][0][0]);
	for (int i=1,x,y;i<n;++i)
		scanf("%d%d",&x,&y),addedge(x,y);
	for (int i=1;i<=n;++i){
		lg[i]=lg[i-1]+1;
		if (1<<lg[i]>i) --lg[i];
	}
	dfs(rt,0,1);
	for (int i=1,l,r;i<=n;++i){
		int d=dep[i]+1;
		for (l=0,r=b[d].size()-1;l<=r;)
			if (idx[b[d][mid]]>=idx[i]) r=mid-1;
			else l=mid+1;
		if ((unsigned)l==b[d].size()) TL.fa[i]=0;
		else TL.fa[i]=b[d][l];
		for (l=0,r=b[d].size()-1;l<=r;)
			if (idx[b[d][mid]]<=idy[i]) l=mid+1;
			else r=mid-1;
		if (r<0) TR.fa[i]=0;
		else TR.fa[i]=b[d][r];
	}
	TL.init();
	TR.init();
	for (int i=1;i<=n;++i){
		m=b[i].size();
		for (int k=1;k<=20;++k)
			for (int j=0;j+(1<<k)<=m;++j)
				f[b[i][j]][k][0]=min(f[b[i][j]][k-1][0],f[b[i][j+(1<<(k-1))]][k-1][0]);
	}
	for (int k=1;k<=20;++k)
		for (int i=1;i<=n;++i){
			m=b[dep[i]].size();
			for (int j=0;id[i]+(1<<j)<=m;++j){
				f[i][j][k]=f[i][j][k-1];
				int u=TL.qry(i,(1<<(k-1)));
				int v=TR.qry(b[dep[i]][id[i]+(1<<j)-1],(1<<(k-1)));
				if (!u||!v||idx[u]>idx[v]) continue;
				int x=lg[id[v]-id[u]+1];
				f[i][j][k]=min(f[i][j][k],f[u][x][k-1]);
				f[i][j][k]=min(f[i][j][k],f[b[dep[v]][id[v]-(1<<x)+1]][x][k-1]);
			}
		}
	scanf("%d",&q);
	int ans=0;
	for (int i=1,x,k;i<=q;++i){
		scanf("%d%d",&x,&k);
		x=(x+ans)%n+1,k=(k+ans)%n;
		ans=f[x][0][lg[k]];
		int u=TL.qry(x,k-(1<<lg[k])),v=TR.qry(x,k-(1<<lg[k]));
		if (u&&v&&idx[u]<=idx[v]){
			x=lg[id[v]-id[u]+1];
			ans=min(ans,f[u][x][lg[k]]);
			ans=min(ans,f[b[dep[v]][id[v]-(1<<x)+1]][x][lg[k]]);
		}
		printf("%d\n",ans);
	}
	return 0;
}


#include<algorithm>
#include<cstdio>
#include<vector>
#define mxn 1000010
#define pb push_back
using namespace std;
int n,m,rt,ans,sl,fh,hd,tl,a[mxn],q[mxn],pw[mxn],lg[mxn],ps[mxn],f[100010][20][20];
int t,h[mxn];
struct Tre
{
	int to,nxt;
}e[mxn<<1];
struct Tree
{
	int dep[mxn],mxl[mxn],son[mxn],top[mxn],f[mxn][20];
	int tp,stk[mxn];
	int t,h[mxn];
	struct Tre
	{
		int to,nxt;
	}e[mxn<<1];
	vector<int> p[mxn];
	void add(int u,int v) {e[++t]=(Tre){v,h[u]};h[u]=t;}
	void dfs1(int u)
	{
		for(int i=1;i<=lg[dep[u]];++i)
			f[u][i]=f[f[u][i-1]][i-1];
		int v;mxl[u]=1;son[u]=-1;
		for(int i=h[u];i;i=e[i].nxt)
		{
			dep[v=e[i].to]=dep[u]+1;dfs1(v);
			if(mxl[v]+1>mxl[u]) son[u]=v,mxl[u]=mxl[v]+1;
		}
	}
	void dfs2(int u,int tp)
	{
		top[u]=tp;
		if(~son[u]) dfs2(son[u],tp);
		int v;
		for(int i=h[u];i;i=e[i].nxt)
			if((v=e[i].to)!=son[u])
				dfs2(v,v);
	}
	void init()
	{
		for(int i=1;i<=n;++i) add(f[i][0],i);
		dep[0]=1;dfs1(0);dfs2(0,0);
		for(int x,i=0;i<=n;++i)
			if(top[i]==i)
			{
				for(x=f[i][0],tp=0;tp<mxl[i];stk[++tp]=x,x=f[x][0]);
				for(;tp;p[i].pb(stk[tp--]));
				for(x=i;~x;p[i].pb(x),x=son[x]);
			}
	}
	int qry(int x,int k)
	{
		if(!k) return x;
		if(k<0) return 0;
		if(dep[x]<k) return 0;
		int u=top[f[x][lg[k]]];
		return p[u][dep[x]-k-dep[u]+mxl[u]];
	}
}TL,TR;
vector<int> vt[mxn];
int rd()
{
	sl=0;fh=1;
	char ch=getchar();
	while(ch<'0'||'9'<ch) {if(ch=='-') fh=-1; ch=getchar();}
	while('0'<=ch&&ch<='9') sl=sl*10+ch-'0',ch=getchar();
	return sl*fh;
}
void add(int u,int v)
{
	e[++t]=(Tre){v,h[u]};h[u]=t;
	e[++t]=(Tre){u,h[v]};h[v]=t;
}
int L[mxn],R[mxn],vis[mxn],dep[mxn];
int main()
{
	n=rd();rt=rd();int x,y;pw[0]=1;
	for(int i=2;i<=n+1;++i) lg[i]=lg[i>>1]+1;
	for(int i=1;i<=20;++i) pw[i]=2*pw[i-1];
	for(int i=1;i<=n;++i) a[i]=rd();
	for(int i=1;i<n;++i) x=rd(),y=rd(),add(x,y);
	q[++tl]=rt;dep[rt]=1;int u,v;vis[rt]=1;
	for(;hd<tl;)
	{
		u=q[++hd];L[u]=tl+1;ps[u]=hd;vt[dep[u]].pb(u);
		for(int i=h[u];i;i=e[i].nxt)
			if(!vis[v=e[i].to])
				vis[q[++tl]=v]=1,dep[v]=dep[u]+1;
		R[u]=tl;
	}
	for(int i=1;i<=n;++i)
	{
		if(dep[q[L[i]]]>dep[i]) TL.f[i][0]=q[L[i]];
		if(dep[q[R[i]]]>dep[i]) TR.f[i][0]=q[R[i]];
		m=max(m,dep[i]);f[i][0][0]=a[i];
	}
	TL.init();TR.init();
	int sz;
	for(int i=1;i<=m;++i)
	{
		sz=vt[i].size();
		for(int j=1;j<20;++j)
			for(int k=0;k+pw[j]<=sz;++k)
				f[vt[i][k]][j][0]=min(f[vt[i][k]][j-1][0],f[vt[i][k+pw[j-1]]][j-1][0]);
	}
	for(int d=1;d<=m;++d)
	{
		sz=vt[d].size();
		for(int i=0;i<20;++i)
			for(int j=1;j<20;++j)
				for(int k=0;k+pw[i]<=sz;++k)
				{
					x=TL.f[u=vt[d][k]][j-1];y=TR.f[vt[d][k+pw[i]-1]][j-1];
					if(x&&y&&ps[x]<=ps[y])
					{
						v=lg[ps[y]-ps[x]+1];
						f[u][i][j]=min(f[u][i][j-1],min(f[x][v][j-1],f[q[ps[y]-pw[v]+1]][v][j-1]));
					}
				}
	}
	int k;
	for(int Q=rd();Q;--Q)
	{
		x=(rd()+ans)%n+1;y=(rd()+ans)%n;
		ans=f[x][0][lg[y]];
		u=TL.qry(x,y-pw[lg[y]]);v=TR.qry(x,y-pw[lg[y]]);
		if(u&&v&&ps[u]<=ps[v])
		{
			k=lg[ps[v]-ps[u]+1];
			ans=min(ans,min(f[u][k][lg[y]],f[q[ps[v]-pw[k]+1]][k][lg[y]]));
		}
		printf("%d\n",ans);
	}
	return 0;
}


#include<algorithm>
#include<cstdio>
#include<vector>
#define mxn 1000010
#define pb push_back
using namespace std;
int n,m,rt,ans,sl,fh,hd,tl,a[mxn],q[mxn],pw[mxn],lg[mxn],ps[mxn],f[100010][20][20];
int t,h[mxn];
struct Tre
{
	int to,nxt;
}e[mxn<<1];
struct Tree
{
	int dep[mxn],mxl[mxn],son[mxn],top[mxn],f[mxn][20];
	int tp,stk[mxn];
	int t,h[mxn];
	struct Tre
	{
		int to,nxt;
	}e[mxn<<1];
	vector<int> p[mxn];
	void add(int u,int v) {e[++t]=(Tre){v,h[u]};h[u]=t;}
	void dfs1(int u)
	{
		for(int i=1;i<=lg[dep[u]];++i) f[u][i]=f[f[u][i-1]][i-1];
		int v;mxl[u]=1;son[u]=-1;
		for(int i=h[u];i;i=e[i].nxt)
		{
			dep[v=e[i].to]=dep[u]+1;dfs1(v);
			if(mxl[v]+1>mxl[u]) son[u]=v,mxl[u]=mxl[v]+1;
		}
	}
	void dfs2(int u,int tp)
	{
		top[u]=tp;
		if(~son[u]) dfs2(son[u],tp);
		int v;
		for(int i=h[u];i;i=e[i].nxt)
			if((v=e[i].to)!=son[u])
				dfs2(v,v);
	}
	void init()
	{
		for(int i=1;i<=n;++i) add(f[i][0],i);
		dep[0]=1;dfs1(0);dfs2(0,0);
		for(int x,i=0;i<=n;++i)
			if(top[i]==i)
			{
				for(x=f[i][0],tp=0;tp<mxl[i];stk[++tp]=x,x=f[x][0]);
				for(;tp;p[i].pb(stk[tp--]));
				for(x=i;~x;p[i].pb(x),x=son[x]);
			}
	}
	int qry(int x,int k)
	{
		if(!k) return x;
		if(dep[x]<k) return 0;
		int u=top[f[x][lg[k]]];
		return p[u][dep[x]-k-dep[u]+mxl[u]];
	}
}TL,TR;
vector<int> vt[mxn];
int rd()
{
	sl=0;fh=1;
	char ch=getchar();
	while(ch<'0'||'9'<ch) {if(ch=='-') fh=-1; ch=getchar();}
	while('0'<=ch&&ch<='9') sl=sl*10+ch-'0',ch=getchar();
	return sl*fh;
}
void add(int u,int v)
{
	e[++t]=(Tre){v,h[u]};h[u]=t;
	e[++t]=(Tre){u,h[v]};h[v]=t;
}
int L[mxn],R[mxn],vis[mxn],dep[mxn];
int main()
{
	n=rd();rt=rd();int x,y;pw[0]=1;
	for(int i=2;i<=n+1;++i) lg[i]=lg[i>>1]+1;
	for(int i=1;i<=20;++i) pw[i]=2*pw[i-1];
	for(int i=1;i<=n;++i) a[i]=rd();
	for(int i=1;i<n;++i) x=rd(),y=rd(),add(x,y);
	q[++tl]=rt;dep[rt]=1;int u,v;vis[rt]=1;
	for(;hd<tl;)
	{
		u=q[++hd];L[u]=tl+1;ps[u]=hd;vt[dep[u]].pb(u);
		for(int i=h[u];i;i=e[i].nxt)
			if(!vis[v=e[i].to])
				vis[q[++tl]=v]=1,dep[v]=dep[u]+1;
		R[u]=tl;
	}
	for(int i=1;i<=n;++i)
	{
		if(dep[q[L[i]]]>dep[i]) TL.f[i][0]=q[L[i]];
		if(dep[q[R[i]]]>dep[i]) TR.f[i][0]=q[R[i]];
		m=max(m,dep[i]);f[i][0][0]=a[i];
	}
	if(n>10) {for(int i=1;i<=n;++i) printf("%d ",TL.f[i][0]); puts("");}
	if(n>10) {for(int i=1;i<=n;++i) printf("%d ",TR.f[i][0]); puts("");}
	TL.init();TR.init();
	int sz;
	for(int i=1;i<=m;++i)
	{
		sz=vt[i].size();
		for(int j=1;j<20;++j)
			for(int k=0;k+pw[j]<=sz;++k)
				f[vt[i][k]][j][0]=min(f[vt[i][k]][j-1][0],f[vt[i][k+pw[j-1]]][j-1][0]);
	}
	for(int d=1;d<=m;++d)
	{
		sz=vt[d].size();
		for(int i=0;i<20;++i)
			for(int j=1;j<20;++j)
				for(int k=0;k+pw[i]<=sz;++k)
				{
					x=TL.f[u=vt[d][k]][j-1];y=TR.f[vt[d][k+pw[i]-1]][j-1];
					if(x&&y&&ps[x]<=ps[y])
					{
						v=lg[ps[y]-ps[x]+1];
						f[u][i][j]=min(f[u][i][j-1],min(f[x][v][j-1],f[q[ps[y]-pw[v]+1]][v][j-1]));
					}
				}
	}
	int k;
	for(int Q=rd();Q;--Q)
	{
		x=(rd()+ans)%n+1;y=(rd()+ans)%n;
		if(y)
		{
			ans=f[x][0][lg[y]];
			u=TL.qry(x,y-pw[lg[y]]);v=TR.qry(x,y-pw[lg[y]]);
			if(u&&v&&ps[u]<=ps[v])
			{
				k=lg[ps[v]-ps[u]+1];
				ans=min(ans,min(f[u][k][lg[y]],f[q[ps[v]-pw[k]+1]][k][lg[y]]));
			}
			printf("%d\n",ans);
		}
		else printf("%d\n",a[x]);
	}
	return 0;
}


#include<algorithm>
#include<cstdio>
#include<vector>
#define mxn 1000010
#define pb push_back
using namespace std;
int n,m,rt,ans,sl,fh,hd,tl,a[mxn],q[mxn],pw[mxn],lg[mxn],ps[mxn],f[100010][20][20];
int t,h[mxn];
struct Tre
{
	int to,nxt;
}e[mxn<<1];
struct Tree
{
	int dep[mxn],mxl[mxn],son[mxn],top[mxn],f[mxn][20];
	int tp,stk[mxn];
	int t,h[mxn];
	struct Tre
	{
		int to,nxt;
	}e[mxn<<1];
	vector<int> p[mxn];
	void add(int u,int v) {e[++t]=(Tre){v,h[u]};h[u]=t;}
	void dfs1(int u)
	{
		for(int i=1;i<=lg[dep[u]];++i) f[u][i]=f[f[u][i-1]][i-1];
		int v;mxl[u]=1;son[u]=-1;
		for(int i=h[u];i;i=e[i].nxt)
		{
			dep[v=e[i].to]=dep[u]+1;dfs1(v);
			if(mxl[v]+1>mxl[u]) son[u]=v,mxl[u]=mxl[v]+1;
		}
	}
	void dfs2(int u,int tp)
	{
		top[u]=tp;
		if(~son[u]) dfs2(son[u],tp);
		int v;
		for(int i=h[u];i;i=e[i].nxt)
			if((v=e[i].to)!=son[u])
				dfs2(v,v);
	}
	void init()
	{
		for(int i=1;i<=n;++i) add(f[i][0],i);
		dep[0]=1;dfs1(0);dfs2(0,0);
		for(int x,i=0;i<=n;++i)
			if(top[i]==i)
			{
				for(x=f[i][0],tp=0;tp<mxl[i];stk[++tp]=x,x=f[x][0]);
				for(;tp;p[i].pb(stk[tp--]));
				for(x=i;~x;p[i].pb(x),x=son[x]);
			}
	}
	int qry(int x,int k)
	{
		if(!k) return x;
		if(dep[x]<k) return 0;
		int u=top[f[x][lg[k]]];
		return p[u][dep[x]-k-dep[u]+mxl[u]];
	}
}TL,TR;
vector<int> vt[mxn];
int rd()
{
	sl=0;fh=1;
	char ch=getchar();
	while(ch<'0'||'9'<ch) {if(ch=='-') fh=-1; ch=getchar();}
	while('0'<=ch&&ch<='9') sl=sl*10+ch-'0',ch=getchar();
	return sl*fh;
}
void add(int u,int v)
{
	e[++t]=(Tre){v,h[u]};h[u]=t;
	e[++t]=(Tre){u,h[v]};h[v]=t;
}
int L[mxn],R[mxn],vis[mxn],dep[mxn];
int main()
{
	n=rd();rt=rd();int x,y;pw[0]=1;
	for(int i=2;i<=n+1;++i) lg[i]=lg[i>>1]+1;
	for(int i=1;i<=20;++i) pw[i]=2*pw[i-1];
	for(int i=1;i<=n;++i) a[i]=rd();
	for(int i=1;i<n;++i) x=rd(),y=rd(),add(x,y);
	q[++tl]=rt;dep[rt]=1;int u,v;vis[rt]=1;
	for(;hd<tl;)
	{
		u=q[++hd];L[u]=tl+1;ps[u]=hd;vt[dep[u]].pb(u);
		for(int i=h[u];i;i=e[i].nxt)
			if(!vis[v=e[i].to])
				vis[q[++tl]=v]=1,dep[v]=dep[u]+1;
		R[u]=tl;
	}
	for(int i=1;i<=n;++i)
	{
		if(dep[q[L[i]]]>dep[i]) TL.f[i][0]=q[L[i]];
		if(dep[q[R[i]]]>dep[i]) TR.f[i][0]=q[R[i]];
		m=max(m,dep[i]);f[i][0][0]=a[i];
	}
	TL.init();TR.init();
	int sz;
	for(int i=1;i<=m;++i)
	{
		sz=vt[i].size();
		for(int j=1;j<20;++j)
			for(int k=0;k+pw[j]<=sz;++k)
				f[vt[i][k]][j][0]=min(f[vt[i][k]][j-1][0],f[vt[i][k+pw[j-1]]][j-1][0]);
	}
	for(int d=m;d;--d)
	{
		sz=vt[d].size();
		for(int i=0;i<20;++i)
			for(int j=1;j<20;++j)
				for(int k=0;k+pw[i]<=sz;++k)
				{
					x=TL.f[u=vt[d][k]][j-1];y=TR.f[vt[d][k+pw[i]-1]][j-1];
					f[u][i][j]=f[u][i][j-1];
					if(x&&y&&ps[x]<=ps[y])
					{
						v=lg[ps[y]-ps[x]+1];
						f[u][i][j]=min(f[u][i][j],min(f[x][v][j-1],f[q[ps[y]-pw[v]+1]][v][j-1]));
					}
				}
	}
	int k;
	for(int Q=rd();Q;--Q)
	{
		x=(rd()+ans)%n+1;y=(rd()+ans)%n;
		if(y)
		{
			ans=f[x][0][lg[y]];
			u=TL.qry(x,y-pw[lg[y]]);v=TR.qry(x,y-pw[lg[y]]);
			if(u&&v&&ps[u]<=ps[v])
			{
				k=lg[ps[v]-ps[u]+1];
				ans=min(ans,min(f[u][k][lg[y]],f[q[ps[v]-pw[k]+1]][k][lg[y]]));
			}
			printf("%d\n",ans);
		}
		else printf("%d\n",a[x]);
	}
	return 0;
}


#include<algorithm>
#include<cstdio>
#include<vector>
#define mxn 1000010
#define pb push_back
using namespace std;
int n,m,rt,ans,sl,fh,hd,tl,a[mxn],q[mxn],pw[mxn],lg[mxn],ps[mxn],f[100010][20][20];
int t,h[mxn];
struct Tre
{
	int to,nxt;
}e[mxn<<1];
struct Tree
{
	int dep[mxn],mxl[mxn],son[mxn],top[mxn],f[mxn][20];
	int tp,stk[mxn];
	int t,h[mxn];
	struct Tre
	{
		int to,nxt;
	}e[mxn<<1];
	vector<int> p[mxn];
	void add(int u,int v) {e[++t]=(Tre){v,h[u]};h[u]=t;}
	void dfs1(int u)
	{
		for(int i=1;i<=lg[dep[u]];++i) f[u][i]=f[f[u][i-1]][i-1];
		int v;mxl[u]=1;son[u]=-1;
		for(int i=h[u];i;i=e[i].nxt)
		{
			dep[v=e[i].to]=dep[u]+1;dfs1(v);
			if(mxl[v]+1>mxl[u]) son[u]=v,mxl[u]=mxl[v]+1;
		}
	}
	void dfs2(int u,int tp)
	{
		top[u]=tp;
		if(~son[u]) dfs2(son[u],tp);
		int v;
		for(int i=h[u];i;i=e[i].nxt)
			if((v=e[i].to)!=son[u])
				dfs2(v,v);
	}
	void init()
	{
		for(int i=1;i<=n;++i) add(f[i][0],i);
		dep[0]=1;dfs1(0);dfs2(0,0);
		for(int x,i=0;i<=n;++i)
			if(top[i]==i)
			{
				for(x=f[i][0],tp=0;tp<mxl[i];stk[++tp]=x,x=f[x][0]);
				for(;tp;p[i].pb(stk[tp--]));
				for(x=i;~x;p[i].pb(x),x=son[x]);
			}
	}
	int qry(int x,int k)
	{
		if(!k) return x;
		if(dep[x]<k) return 0;
		int u=top[f[x][lg[k]]];
		return p[u][dep[x]-k-dep[u]+mxl[u]];
	}
}TL,TR;
vector<int> vt[mxn];
int rd()
{
	sl=0;fh=1;
	char ch=getchar();
	while(ch<'0'||'9'<ch) {if(ch=='-') fh=-1; ch=getchar();}
	while('0'<=ch&&ch<='9') sl=sl*10+ch-'0',ch=getchar();
	return sl*fh;
}
void add(int u,int v)
{
	e[++t]=(Tre){v,h[u]};h[u]=t;
	e[++t]=(Tre){u,h[v]};h[v]=t;
}
int L[mxn],R[mxn],vis[mxn],dep[mxn];
int main()
{
	n=rd();rt=rd();int x,y;pw[0]=1;
	for(int i=2;i<=n+1;++i) lg[i]=lg[i>>1]+1;
	for(int i=1;i<=20;++i) pw[i]=2*pw[i-1];
	for(int i=1;i<=n;++i) a[i]=rd();
	for(int i=1;i<n;++i) x=rd(),y=rd(),add(x,y);
	q[++tl]=rt;dep[rt]=1;int u,v;vis[rt]=1;
	for(;hd<tl;)
	{
		u=q[++hd];L[u]=tl+1;ps[u]=hd;vt[dep[u]].pb(u);
		for(int i=h[u];i;i=e[i].nxt)
			if(!vis[v=e[i].to])
				vis[q[++tl]=v]=1,dep[v]=dep[u]+1;
		R[u]=tl;
	}
	for(int i=1;i<=n;++i)
	{
		if(dep[q[L[i]]]>dep[i]) TL.f[i][0]=q[L[i]];
		if(dep[q[R[i]]]>dep[i]) TR.f[i][0]=q[R[i]];
		m=max(m,dep[i]);f[i][0][0]=a[i];
	}
	if(n>10) {for(int i=1;i<=n;++i) printf("%d ",q[i]); puts("");}
	TL.init();TR.init();
	int sz;
	for(int i=1;i<=m;++i)
	{
		sz=vt[i].size();
		for(int j=1;j<20;++j)
			for(int k=0;k+pw[j]<=sz;++k)
				f[vt[i][k]][j][0]=min(f[vt[i][k]][j-1][0],f[vt[i][k+pw[j-1]]][j-1][0]);
	}
	for(int d=1;d<=m;++d)
	{
		sz=vt[d].size();
		for(int i=0;i<20;++i)
			for(int j=1;j<20;++j)
				for(int k=0;k+pw[i]<=sz;++k)
				{
					x=TL.f[u=vt[d][k]][j-1];y=TR.f[vt[d][k+pw[i]-1]][j-1];
					if(x&&y&&ps[x]<=ps[y])
					{
						v=lg[ps[y]-ps[x]+1];
						f[u][i][j]=min(f[u][i][j-1],min(f[x][v][j-1],f[q[ps[y]-pw[v]+1]][v][j-1]));
					}
				}
	}
	int k;
	for(int Q=rd();Q;--Q)
	{
		x=(rd()+ans)%n+1;y=(rd()+ans)%n;
		if(y)
		{
			ans=f[x][0][lg[y]];
			u=TL.qry(x,y-pw[lg[y]]);v=TR.qry(x,y-pw[lg[y]]);
			if(u&&v&&ps[u]<=ps[v])
			{
				k=lg[ps[v]-ps[u]+1];
				ans=min(ans,min(f[u][k][lg[y]],f[q[ps[v]-pw[k]+1]][k][lg[y]]));
			}
			printf("%d\n",ans);
		}
		else printf("%d\n",a[x]);
	}
	return 0;
}


#include<algorithm>
#include<cstdio>
#include<vector>
#define mxn 1000010
#define pb push_back
using namespace std;
int n,m,rt,ans,sl,fh,hd,tl,a[mxn],q[mxn],pw[mxn],lg[mxn],ps[mxn],f[mxn][20][20];
int t,h[mxn];
struct Tre
{
	int to,nxt;
}e[mxn<<1];
struct Tree
{
	int dep[mxn],mxl[mxn],son[mxn],top[mxn],f[mxn][20];
	int tp,stk[mxn];
	int t,h[mxn];
	struct Tre
	{
		int to,nxt;
	}e[mxn<<1];
	vector<int> p[mxn];
	void add(int u,int v) {e[++t]=(Tre){v,h[u]};h[u]=t;}
	void dfs1(int u)
	{
		for(int i=1;i<=lg[dep[u]];++i)
			f[u][i]=f[f[u][i-1]][i-1];
		int v;mxl[u]=1;son[u]=-1;
		for(int i=h[u];i;i=e[i].nxt)
		{
			dep[v=e[i].to]=dep[u]+1;dfs1(v);
			if(mxl[v]+1>mxl[u]) son[u]=v,mxl[u]=mxl[v]+1;
		}
	}
	void dfs2(int u,int tp)
	{
		top[u]=tp;
		if(~son[u]) dfs2(son[u],tp);
		int v;
		for(int i=h[u];i;i=e[i].nxt)
			if((v=e[i].to)!=son[u])
				dfs2(v,v);
	}
	void init()
	{
		for(int i=1;i<=n;++i) add(f[i][0],i);
		dep[0]=1;dfs1(0);dfs2(0,0);
		for(int x,i=0;i<=n;++i)
			if(top[i]==i)
			{
				for(x=f[i][0],tp=0;tp<mxl[i];stk[++tp]=x,x=f[x][0]);
				for(;tp;p[i].pb(stk[tp--]));
				for(x=i;~x;p[i].pb(x),x=son[x]);
			}
	}
	int qry(int x,int k)
	{
		if(!k) return x;
		if(k<0) return 0;
		if(dep[x]<k) return 0;
		int u=top[f[x][lg[k]]];
		return p[u][dep[x]-k-dep[u]+mxl[u]];
	}
}TL,TR;
vector<int> vt[mxn];
int rd()
{
	sl=0;fh=1;
	char ch=getchar();
	while(ch<'0'||'9'<ch) {if(ch=='-') fh=-1; ch=getchar();}
	while('0'<=ch&&ch<='9') sl=sl*10+ch-'0',ch=getchar();
	return sl*fh;
}
void add(int u,int v)
{
	e[++t]=(Tre){v,h[u]};h[u]=t;
	e[++t]=(Tre){u,h[v]};h[v]=t;
}
int L[mxn],R[mxn],vis[mxn],dep[mxn];
int main()
{
	n=rd();rt=rd();int x,y;pw[0]=1;
	for(int i=2;i<=n+1;++i) lg[i]=lg[i>>1]+1;
	for(int i=1;i<=20;++i) pw[i]=2*pw[i-1];
	for(int i=1;i<=n;++i) a[i]=rd();
	for(int i=1;i<n;++i) x=rd(),y=rd(),add(x,y);
	q[++tl]=rt;dep[rt]=1;int u,v;vis[rt]=1;
	for(;hd<tl;)
	{
		u=q[++hd];L[u]=tl+1;ps[u]=hd;vt[dep[u]].pb(u);
		for(int i=h[u];i;i=e[i].nxt)
			if(!vis[v=e[i].to])
				vis[q[++tl]=v]=1,dep[v]=dep[u]+1;
		R[u]=tl;
	}
	for(int i=1;i<=n;++i)
	{
		if(dep[q[L[i]]]>dep[i]) TL.f[i][0]=q[L[i]];
		if(dep[q[R[i]]]>dep[i]) TR.f[i][0]=q[R[i]];
		m=max(m,dep[i]);f[i][0][0]=a[i];
	}
	TL.init();TR.init();
	int sz;
	for(int i=1;i<=m;++i)
	{
		sz=vt[i].size();
		for(int j=1;j<20;++j)
			for(int k=0;k+pw[j]<=sz;++k)
				f[vt[i][k]][j][0]=min(f[vt[i][k]][j-1][0],f[vt[i][k+pw[j-1]]][j-1][0]);
	}
	for(int d=1;d<=m;++d)
	{
		sz=vt[d].size();
		for(int i=0;i<20;++i)
			for(int j=1;j<20;++j)
				for(int k=0;k+pw[i]<=sz;++k)
				{
					x=TL.f[u=vt[d][k]][j-1];y=TR.f[vt[d][k+pw[i]-1]][j-1];
					if(x&&y&&ps[x]<=ps[y])
					{
						v=lg[ps[y]-ps[x]+1];
						f[u][i][j]=min(f[u][i][j-1],min(f[x][v][j-1],f[q[ps[y]-pw[v]+1]][v][j-1]));
					}
				}
	}
	int k;
	for(int Q=rd();Q;--Q)
	{
		x=(rd()+ans)%n+1;y=(rd()+ans)%n;
		ans=f[x][0][lg[y]];
		u=TL.qry(x,y-pw[lg[y]]);v=TR.qry(x,y-pw[lg[y]]);
		if(u&&v&&ps[u]<=ps[v])
		{
			k=lg[ps[v]-ps[u]+1];
			ans=min(ans,min(f[u][k][lg[y]],f[q[ps[v]-pw[k]+1]][k][lg[y]]));
		}
		printf("%d\n",ans);
	}
	return 0;
}


#include<algorithm>
#include<cstdio>
#include<vector>
#define mxn 1000010
#define pb push_back
using namespace std;
int n,m,rt,ans,sl,fh,hd,tl,a[mxn],q[mxn],pw[mxn],lg[mxn],ps[mxn],f[100010][20][20];
int t,h[mxn];
struct Tre
{
	int to,nxt;
}e[mxn<<1];
struct Tree
{
	int dep[mxn],mxl[mxn],son[mxn],top[mxn],f[mxn][20];
	int tp,stk[mxn];
	int t,h[mxn];
	struct Tre
	{
		int to,nxt;
	}e[mxn<<1];
	vector<int> p[mxn];
	void add(int u,int v) {e[++t]=(Tre){v,h[u]};h[u]=t;}
	void dfs1(int u)
	{
		for(int i=1;i<=lg[dep[u]];++i) f[u][i]=f[f[u][i-1]][i-1];
		int v;mxl[u]=1;son[u]=-1;
		for(int i=h[u];i;i=e[i].nxt)
		{
			dep[v=e[i].to]=dep[u]+1;dfs1(v);
			if(mxl[v]+1>mxl[u]) son[u]=v,mxl[u]=mxl[v]+1;
		}
	}
	void dfs2(int u,int tp)
	{
		top[u]=tp;
		if(~son[u]) dfs2(son[u],tp);
		int v;
		for(int i=h[u];i;i=e[i].nxt)
			if((v=e[i].to)!=son[u])
				dfs2(v,v);
	}
	void init()
	{
		for(int i=1;i<=n;++i) add(f[i][0],i);
		dep[0]=1;dfs1(0);dfs2(0,0);
		for(int x,i=0;i<=n;++i)
			if(top[i]==i)
			{
				for(x=f[i][0],tp=0;tp<mxl[i];stk[++tp]=x,x=f[x][0]);
				for(;tp;p[i].pb(stk[tp--]));
				for(x=i;~x;p[i].pb(x),x=son[x]);
			}
	}
	int qry(int x,int k)
	{
		if(!k) return x;
		if(dep[x]<=k) return 0;
		int u=top[f[x][lg[k]]];
		return p[u][dep[x]-k-dep[u]+mxl[u]];
	}
}TL,TR;
vector<int> vt[mxn];
int rd()
{
	sl=0;fh=1;
	char ch=getchar();
	while(ch<'0'||'9'<ch) {if(ch=='-') fh=-1; ch=getchar();}
	while('0'<=ch&&ch<='9') sl=sl*10+ch-'0',ch=getchar();
	return sl*fh;
}
void add(int u,int v)
{
	e[++t]=(Tre){v,h[u]};h[u]=t;
	e[++t]=(Tre){u,h[v]};h[v]=t;
}
int L[mxn],R[mxn],vis[mxn],dep[mxn];
int main()
{
	n=rd();rt=rd();int x,y;pw[0]=1;
	for(int i=2;i<=n+1;++i) lg[i]=lg[i>>1]+1;
	for(int i=1;i<=20;++i) pw[i]=2*pw[i-1];
	for(int i=1;i<=n;++i) a[i]=rd();
	for(int i=1;i<n;++i) x=rd(),y=rd(),add(x,y);
	q[++tl]=rt;dep[rt]=1;int u,v;vis[rt]=1;
	for(;hd<tl;)
	{
		u=q[++hd];L[u]=tl+1;ps[u]=hd;vt[dep[u]].pb(u);
		for(int i=h[u];i;i=e[i].nxt)
			if(!vis[v=e[i].to])
				vis[q[++tl]=v]=1,dep[v]=dep[u]+1;
		R[u]=tl;
	}
	for(int i=1;i<=n;++i)
	{
		if(dep[q[L[i]]]>dep[i]) TL.f[i][0]=q[L[i]];
		if(dep[q[R[i]]]>dep[i]) TR.f[i][0]=q[R[i]];
		m=max(m,dep[i]);f[i][0][0]=a[i];
	}
	TL.init();TR.init();
	int sz;
	for(int i=1;i<=m;++i)
	{
		sz=vt[i].size();
		for(int j=1;j<20;++j)
			for(int k=0;k+pw[j]<=sz;++k)
				f[vt[i][k]][j][0]=min(f[vt[i][k]][j-1][0],f[vt[i][k+pw[j-1]]][j-1][0]);
	}
	for(int d=m;d;--d)
	{
		sz=vt[d].size();
		for(int i=0;i<20;++i)
			for(int j=1;j<20;++j)
				for(int k=0;k+pw[i]<=sz;++k)
				{
					x=TL.f[u=vt[d][k]][j-1];y=TR.f[vt[d][k+pw[i]-1]][j-1];
					f[u][i][j]=f[u][i][j-1];
					if(x&&y&&ps[x]<=ps[y])
					{
						v=lg[ps[y]-ps[x]+1];
						f[u][i][j]=min(f[u][i][j],min(f[x][v][j-1],f[q[ps[y]-pw[v]+1]][v][j-1]));
					}
				}
	}
	int k;
	for(int Q=rd();Q;--Q)
	{
		x=(rd()+ans)%n+1;y=(rd()+ans)%n+1;
		if(y)
		{
			ans=f[x][0][lg[y]];
			u=TL.qry(x,y-pw[lg[y]]);v=TR.qry(x,y-pw[lg[y]]);
			if(u&&v&&ps[u]<=ps[v])
			{
				k=lg[ps[v]-ps[u]+1];
				ans=min(ans,min(f[u][k][lg[y]],f[q[ps[v]-pw[k]+1]][k][lg[y]]));
			}
			printf("%d\n",ans);
		}
		else printf("%d\n",a[x]);
	}
	return 0;
}


#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#define mid ((l+r)>>1)
using namespace std;
const int mxn=100010;
int n,rt,q,lg[mxn];
struct ed{int to,nxt,val;};
struct kth_fa{
	int m,fa[mxn],head[mxn],dep[mxn],dis[mxn],son[mxn],top[mxn];
	int tot,tp,p[mxn][21],id[mxn],stk[mxn];
	vector<int> a[mxn];
	ed edge[mxn<<1];
	void addedge(int u,int v){
		edge[++m]=(ed){v,head[u]},head[u]=m;
		edge[++m]=(ed){u,head[v]},head[v]=m;
	}
	void dfs1(int u,int fa,int d){
		dep[u]=1,p[u][0]=fa,dis[u]=d;
		for (int i=1;i<=20;++i)
			p[u][i]=p[p[u][i-1]][i-1];
		for (int i=head[u],v;i;i=edge[i].nxt)
			if ((v=edge[i].to)!=fa){
				dfs1(v,u,d+1);
				if (dep[v]+1>dep[u]) dep[u]=dep[v]+1,son[u]=v;
			}
	}
	void dfs2(int u,int fa,int tp){
		top[u]=tp;
		for (int i=head[u],v;i;i=edge[i].nxt)
			if ((v=edge[i].to)!=fa)
				if (v==son[u]) dfs2(v,u,tp);
				else dfs2(v,u,v);
			else;
	}
	void init(){
		m=0;
		for (int i=1;i<=n;++i) addedge(i,fa[i]);
		dfs1(0,0,1),dfs2(0,0,0);
		for (int u=0;u<=n;++u)
			if (top[u]==u){
				id[u]=++tot,tp=0;
				for (int i=1,v=u;i<=dep[u];++i) stk[++tp]=(v=fa[v]);
				for (;tp;a[tot].push_back(stk[tp--]));
				for (int i=1,v=u;i<=dep[u];++i) a[tot].push_back(v),v=son[v];
			}
	}
	int qry(int u,int k){
		if (!k) return u;
		if (dis[u]<=k) return 0;
		int x=top[p[u][lg[k]]];
		return a[id[x]][dis[u]-k-dis[x]+dep[x]];
	}
}TL,TR;
ed edge[mxn<<1];
int m,tot,head[mxn],dep[mxn],id[mxn],idx[mxn],idy[mxn],f[mxn][21][21];
vector<int> b[mxn];
void addedge(int u,int v){
	edge[++m]=(ed){v,head[u]},head[u]=m;
	edge[++m]=(ed){u,head[v]},head[v]=m;
}
void dfs(int u,int fa,int d){
	idx[u]=++tot,dep[u]=d;
	id[u]=b[d].size(),b[d].push_back(u);
	for (int i=head[u],v;i;i=edge[i].nxt)
		if ((v=edge[i].to)!=fa) dfs(v,u,d+1);
	idy[u]=tot;
}
int main()
{
	scanf("%d%d",&n,&rt);
	for (int i=1;i<=n;++i) scanf("%d",&f[i][0][0]);
	for (int i=1,x,y;i<n;++i)
		scanf("%d%d",&x,&y),addedge(x,y);
	for (int i=1;i<=n;++i){
		lg[i]=lg[i-1]+1;
		if (1<<lg[i]>i) --lg[i];
	}
	dfs(rt,0,1);int mxd=0;
	for (int i=1,l,r;i<=n;++i){
		int d=dep[i]+1;mxd=max(mxd,dep[i]);
		for (l=0,r=b[d].size()-1;l<=r;)
			if (idx[b[d][mid]]>=idx[i]) r=mid-1;
			else l=mid+1;
		if ((unsigned)l==b[d].size()) TL.fa[i]=0;
		else TL.fa[i]=b[d][l];
		for (l=0,r=b[d].size()-1;l<=r;)
			if (idx[b[d][mid]]<=idy[i]) l=mid+1;
			else r=mid-1;
		if (r<0) TR.fa[i]=0;
		else TR.fa[i]=b[d][r];
	}
	if(n>10) printf("%d\n",mxd);
	TL.init();
	TR.init();
	for (int i=1;i<=n;++i){
		m=b[i].size();
		for (int k=1;k<=20;++k)
			for (int j=0;j+(1<<k)<=m;++j)
				f[b[i][j]][k][0]=min(f[b[i][j]][k-1][0],f[b[i][j+(1<<(k-1))]][k-1][0]);
	}
	for (int k=1;k<=20;++k)
		for (int i=1;i<=n;++i){
			m=b[dep[i]].size();
			for (int j=0;id[i]+(1<<j)<=m;++j){
				f[i][j][k]=f[i][j][k-1];
				int u=TL.qry(i,(1<<(k-1)));
				int v=TR.qry(b[dep[i]][id[i]+(1<<j)-1],(1<<(k-1)));
				if (!u||!v||idx[u]>idx[v]) continue;
				int x=lg[id[v]-id[u]+1];
				f[i][j][k]=min(f[i][j][k],f[u][x][k-1]);
				f[i][j][k]=min(f[i][j][k],f[b[dep[v]][id[v]-(1<<x)+1]][x][k-1]);
			}
		}
	scanf("%d",&q);
	int ans=0;
	for (int i=1,x,k;i<=q;++i){
		scanf("%d%d",&x,&k);
		x=(x+ans)%n+1,k=(k+ans)%n+1;
		ans=f[x][0][lg[k]];
		int u=TL.qry(x,k-(1<<lg[k])),v=TR.qry(x,k-(1<<lg[k]));
		if (u&&v&&idx[u]<=idx[v]){
			x=lg[id[v]-id[u]+1];
			ans=min(ans,f[u][x][lg[k]]);
			ans=min(ans,f[b[dep[v]][id[v]-(1<<x)+1]][x][lg[k]]);
		}
		printf("%d\n",ans);
	}
	return 0;
}


#include<algorithm>
#include<cstdio>
#include<vector>
#define mxn 1000010
#define pb push_back
using namespace std;
int n,m,rt,ans,sl,fh,hd,tl,a[mxn],q[mxn],pw[mxn],lg[mxn],ps[mxn],f[100010][20][20];
int t,h[mxn];
struct Tre
{
	int to,nxt;
}e[mxn<<1];
struct Tree
{
	int dep[mxn],mxl[mxn],son[mxn],top[mxn],f[mxn][20];
	int tp,stk[mxn];
	int t,h[mxn];
	struct Tre
	{
		int to,nxt;
	}e[mxn<<1];
	vector<int> p[mxn];
	void add(int u,int v) {e[++t]=(Tre){v,h[u]};h[u]=t;}
	void dfs1(int u)
	{
		for(int i=1;i<=lg[dep[u]];++i) f[u][i]=f[f[u][i-1]][i-1];
		int v;mxl[u]=1;son[u]=-1;
		for(int i=h[u];i;i=e[i].nxt)
		{
			dep[v=e[i].to]=dep[u]+1;dfs1(v);
			if(mxl[v]+1>mxl[u]) son[u]=v,mxl[u]=mxl[v]+1;
		}
	}
	void dfs2(int u,int tp)
	{
		top[u]=tp;
		if(~son[u]) dfs2(son[u],tp);
		int v;
		for(int i=h[u];i;i=e[i].nxt)
			if((v=e[i].to)!=son[u])
				dfs2(v,v);
	}
	void init()
	{
		for(int i=1;i<=n;++i) add(f[i][0],i);
		dep[0]=1;dfs1(0);dfs2(0,0);
		for(int x,i=0;i<=n;++i)
			if(top[i]==i)
			{
				for(x=f[i][0],tp=0;tp<mxl[i];stk[++tp]=x,x=f[x][0]);
				for(;tp;p[i].pb(stk[tp--]));
				for(x=i;~x;p[i].pb(x),x=son[x]);
			}
	}
	int qry(int x,int k)
	{
		if(!k) return x;
		if(dep[x]<=k) return 0;
		int u=top[f[x][lg[k]]];
		return p[u][dep[x]-k-dep[u]+mxl[u]];
	}
}TL,TR;
vector<int> vt[mxn];
int rd()
{
	sl=0;fh=1;
	char ch=getchar();
	while(ch<'0'||'9'<ch) {if(ch=='-') fh=-1; ch=getchar();}
	while('0'<=ch&&ch<='9') sl=sl*10+ch-'0',ch=getchar();
	return sl*fh;
}
void add(int u,int v)
{
	e[++t]=(Tre){v,h[u]};h[u]=t;
	e[++t]=(Tre){u,h[v]};h[v]=t;
}
int L[mxn],R[mxn],vis[mxn],dep[mxn];
int main()
{
	n=rd();rt=rd();int x,y;pw[0]=1;
	for(int i=2;i<=n+1;++i) lg[i]=lg[i>>1]+1;
	for(int i=1;i<=20;++i) pw[i]=2*pw[i-1];
	for(int i=1;i<=n;++i) a[i]=rd();
	for(int i=1;i<n;++i) x=rd(),y=rd(),add(x,y);
	q[++tl]=rt;dep[rt]=1;int u,v;vis[rt]=1;
	for(;hd<tl;)
	{
		u=q[++hd];L[u]=tl+1;ps[u]=hd;vt[dep[u]].pb(u);
		for(int i=h[u];i;i=e[i].nxt)
			if(!vis[v=e[i].to])
				vis[q[++tl]=v]=1,dep[v]=dep[u]+1;
		R[u]=tl;
	}
	for(int i=1;i<=n;++i)
	{
		if(dep[q[L[i]]]>dep[i]) TL.f[i][0]=q[L[i]];
		if(dep[q[R[i]]]>dep[i]) TR.f[i][0]=q[R[i]];
		m=max(m,dep[i]);f[i][0][0]=a[i];
	}
	TL.init();TR.init();
	int sz;
	for(int i=1;i<=m;++i)
	{
		sz=vt[i].size();
		for(int j=1;j<20;++j)
			for(int k=0;k+pw[j]<=sz;++k)
				f[vt[i][k]][j][0]=min(f[vt[i][k]][j-1][0],f[vt[i][k+pw[j-1]]][j-1][0]);
	}
	for(int d=m;d;--d)
	{
		sz=vt[d].size();
		for(int i=0;i<20;++i)
			for(int j=1;j<20;++j)
				for(int k=0;k+pw[i]<=sz;++k)
				{
					x=TL.f[u=vt[d][k]][j-1];y=TR.f[vt[d][k+pw[i]-1]][j-1];
					f[u][i][j]=f[u][i][j-1];
					if(x&&y&&ps[x]<=ps[y])
					{
						v=lg[ps[y]-ps[x]+1];
						f[u][i][j]=min(f[u][i][j],min(f[x][v][j-1],f[q[ps[y]-pw[v]+1]][v][j-1]));
					}
				}
	}
	int k;
	for(int Q=rd();Q;--Q)
	{
		x=(rd()+ans)%n+1;y=(rd()+ans)%n;
		if(y)
		{
			ans=f[x][0][lg[y]];
			u=TL.qry(x,y-pw[lg[y]]+1);v=TR.qry(x,y-pw[lg[y]]+1);
			if(u&&v&&ps[u]<=ps[v])
			{
				k=lg[ps[v]-ps[u]+1];
				ans=min(ans,min(f[u][k][lg[y]],f[q[ps[v]-pw[k]+1]][k][lg[y]]));
			}
			printf("%d\n",ans);
		}
		else printf("%d\n",a[x]);
	}
	return 0;
}


#include<algorithm>
#include<cstdio>
#include<vector>
#define mxn 1000010
#define pb push_back
using namespace std;
int n,m,rt,ans,sl,fh,hd,tl,a[mxn],q[mxn],pw[mxn],lg[mxn],ps[mxn],f[100010][20][20];
int t,h[mxn];
struct Tre
{
	int to,nxt;
}e[mxn<<1];
struct Tree
{
	int dep[mxn],mxl[mxn],son[mxn],top[mxn],f[mxn][20];
	int tp,stk[mxn];
	int t,h[mxn];
	struct Tre
	{
		int to,nxt;
	}e[mxn<<1];
	vector<int> p[mxn];
	void add(int u,int v) {e[++t]=(Tre){v,h[u]};h[u]=t;}
	void dfs1(int u)
	{
		for(int i=1;i<=lg[dep[u]];++i) f[u][i]=f[f[u][i-1]][i-1];
		int v;mxl[u]=1;son[u]=-1;
		for(int i=h[u];i;i=e[i].nxt)
		{
			dep[v=e[i].to]=dep[u]+1;dfs1(v);
			if(mxl[v]+1>mxl[u]) son[u]=v,mxl[u]=mxl[v]+1;
		}
	}
	void dfs2(int u,int tp)
	{
		top[u]=tp;
		if(~son[u]) dfs2(son[u],tp);
		int v;
		for(int i=h[u];i;i=e[i].nxt)
			if((v=e[i].to)!=son[u])
				dfs2(v,v);
	}
	void init()
	{
		for(int i=1;i<=n;++i) add(f[i][0],i);
		dep[0]=1;dfs1(0);dfs2(0,0);
		for(int x,i=0;i<=n;++i)
			if(top[i]==i)
			{
				for(x=f[i][0],tp=0;tp<mxl[i];stk[++tp]=x,x=f[x][0]);
				for(;tp;p[i].pb(stk[tp--]));
				for(x=i;~x;p[i].pb(x),x=son[x]);
			}
	}
	int qry(int x,int k)
	{
		if(!k) return x;
		if(dep[x]<k) return 0;
		int u=top[f[x][lg[k]]];
		return p[u][dep[x]-k-dep[u]+mxl[u]];
	}
}TL,TR;
vector<int> vt[mxn];
int rd()
{
	sl=0;fh=1;
	char ch=getchar();
	while(ch<'0'||'9'<ch) {if(ch=='-') fh=-1; ch=getchar();}
	while('0'<=ch&&ch<='9') sl=sl*10+ch-'0',ch=getchar();
	return sl*fh;
}
void add(int u,int v)
{
	e[++t]=(Tre){v,h[u]};h[u]=t;
	e[++t]=(Tre){u,h[v]};h[v]=t;
}
int L[mxn],R[mxn],vis[mxn],dep[mxn];
int main()
{
	n=rd();rt=rd();int x,y;pw[0]=1;
	for(int i=2;i<=n+1;++i) lg[i]=lg[i>>1]+1;
	for(int i=1;i<=20;++i) pw[i]=2*pw[i-1];
	for(int i=1;i<=n;++i) a[i]=rd();
	for(int i=1;i<n;++i) x=rd(),y=rd(),add(x,y);
	q[++tl]=rt;dep[rt]=1;int u,v;vis[rt]=1;
	for(;hd<tl;)
	{
		u=q[++hd];L[u]=tl+1;ps[u]=hd;vt[dep[u]].pb(u);
		for(int i=h[u];i;i=e[i].nxt)
			if(!vis[v=e[i].to])
				vis[q[++tl]=v]=1,dep[v]=dep[u]+1;
		R[u]=tl;
	}
	for(int i=1;i<=n;++i)
	{
		if(dep[q[L[i]]]>dep[i]) TL.f[i][0]=q[L[i]];
		if(dep[q[R[i]]]>dep[i]) TR.f[i][0]=q[R[i]];
		m=max(m,dep[i]);f[i][0][0]=a[i];
	}
	if(n>10) {for(int i=1;i<=n;++i) printf("%d ",TL.f[q[i]][0]); puts("");}
	TL.init();TR.init();
	int sz;
	for(int i=1;i<=m;++i)
	{
		sz=vt[i].size();
		for(int j=1;j<20;++j)
			for(int k=0;k+pw[j]<=sz;++k)
				f[vt[i][k]][j][0]=min(f[vt[i][k]][j-1][0],f[vt[i][k+pw[j-1]]][j-1][0]);
	}
	for(int d=1;d<=m;++d)
	{
		sz=vt[d].size();
		for(int i=0;i<20;++i)
			for(int j=1;j<20;++j)
				for(int k=0;k+pw[i]<=sz;++k)
				{
					x=TL.f[u=vt[d][k]][j-1];y=TR.f[vt[d][k+pw[i]-1]][j-1];
					if(x&&y&&ps[x]<=ps[y])
					{
						v=lg[ps[y]-ps[x]+1];
						f[u][i][j]=min(f[u][i][j-1],min(f[x][v][j-1],f[q[ps[y]-pw[v]+1]][v][j-1]));
					}
				}
	}
	int k;
	for(int Q=rd();Q;--Q)
	{
		x=(rd()+ans)%n+1;y=(rd()+ans)%n;
		if(y)
		{
			ans=f[x][0][lg[y]];
			u=TL.qry(x,y-pw[lg[y]]);v=TR.qry(x,y-pw[lg[y]]);
			if(u&&v&&ps[u]<=ps[v])
			{
				k=lg[ps[v]-ps[u]+1];
				ans=min(ans,min(f[u][k][lg[y]],f[q[ps[v]-pw[k]+1]][k][lg[y]]));
			}
			printf("%d\n",ans);
		}
		else printf("%d\n",a[x]);
	}
	return 0;
}


//Re-re-retry
#include <cstdio>
#include <algorithm>
using namespace std;
const int N = 1e5;
int n,m,r;
int a[N + 5];
int to[(N << 1) + 5],pre[(N << 1) + 5],first[N + 5];
int lastans;
inline void add(int u,int v)
{
    static int tot = 0;
    to[++tot] = v,pre[tot] = first[u],first[u] = tot;
}
struct node
{
    int min;
    int ls,rs;
} seg[(N << 7) + 10];
int rt[N + 5];
int seg_tot;
void insert(int x,int k,int &p,int tl,int tr)
{
    if(!p)
        p = ++seg_tot,seg[p].min = 0x3f3f3f3f;
    seg[p].min = min(seg[p].min,k);
    if(tl == tr)
        return ;
    int mid = tl + tr >> 1;
    x <= mid ? insert(x,k,seg[p].ls,tl,mid) : insert(x,k,seg[p].rs,mid + 1,tr);
}
int query(int l,int r,int p,int tl,int tr)
{
    if(!p || (l <= tl && tr <= r))
        return seg[p].min;
    int mid = tl + tr >> 1;
    int ret = 0x3f3f3f3f;
    if(l <= mid)
        ret = min(ret,query(l,r,seg[p].ls,tl,mid));
    if(r > mid)
        ret = min(ret,query(l,r,seg[p].rs,mid + 1,tr));
    return ret;
}
int merge(int x,int y)
{
    if(!x || !y)
        return x | y;
    int p = ++seg_tot;
    seg[p].min = min(seg[x].min,seg[y].min);
    seg[p].ls = merge(seg[x].ls,seg[y].ls),seg[p].rs = merge(seg[x].rs,seg[y].rs);
    return p;
}
int fa[N + 5],dep[N + 5];
void dfs(int p)
{
    insert(dep[p],a[p],rt[p],1,n);
    for(register int i = first[p];i;i = pre[i])
        if(to[i] ^ fa[p])
            fa[to[i]] = p,dep[to[i]] = dep[p] + 1,dfs(to[i]),rt[p] = merge(rt[p],rt[to[i]]);
}
int main()
{
    seg[0].min = 0x3f3f3f3f;
    scanf("%d%d",&n,&r);
    for(register int i = 1;i <= n;++i)
        scanf("%d",a + i);
    int u,v;
    for(register int i = 1;i < n;++i)
        scanf("%d%d",&u,&v),add(u,v),add(v,u);
    dep[r] = 1,dfs(r);
    scanf("%d",&m);
    int x,k;
    while(m--)
    {
        scanf("%d%d",&x,&k);
        x = (x + lastans) % n + 1,k = (k + lastans) % n;
        printf("%d\n",lastans = query(dep[x],min(dep[x] + k,n),rt[x],1,n));
    }
}


//Retry
#include <cstdio>
#include <algorithm>
using namespace std;
const int N = 1e5;
int n,m,r;
int a[N + 5];
int to[(N << 1) + 5],pre[(N << 1) + 5],first[N + 5];
int lastans;
inline void add(int u,int v)
{
    static int tot = 0;
    to[++tot] = v,pre[tot] = first[u],first[u] = tot;
}
struct node
{
    int min;
    int ls,rs;
} seg[(N << 7) + 10];
int rt[N + 5];
int seg_tot;
void insert(int x,int k,int &p,int tl,int tr)
{
    if(!p)
        p = ++seg_tot,seg[p].min = 0x3f3f3f3f;
    seg[p].min = min(seg[p].min,k);
    if(tl == tr)
        return ;
    int mid = tl + tr >> 1;
    x <= mid ? insert(x,k,seg[p].ls,tl,mid) : insert(x,k,seg[p].rs,mid + 1,tr);
}
int query(int l,int r,int p,int tl,int tr)
{
    if(!p || (l <= tl && tr <= r))
        return seg[p].min;
    int mid = tl + tr >> 1;
    int ret = 0x3f3f3f3f;
    if(l <= mid)
        ret = min(ret,query(l,r,seg[p].ls,tl,mid));
    if(r > mid)
        ret = min(ret,query(l,r,seg[p].rs,mid + 1,tr));
    return ret;
}
int merge(int x,int y)
{
    if(!x || !y)
        return x | y;
    int p = ++seg_tot;
    seg[p].min = min(seg[x].min,seg[y].min);
    seg[p].ls = merge(seg[x].ls,seg[y].ls),seg[p].rs = merge(seg[x].rs,seg[y].rs);
    return p;
}
int fa[N + 5],dep[N + 5];
void dfs(int p)
{
    insert(dep[p],a[p],rt[p],1,n);
    for(register int i = first[p];i;i = pre[i])
        if(to[i] ^ fa[p])
            fa[to[i]] = p,dep[to[i]] = dep[p] + 1,dfs(to[i]),rt[p] = merge(rt[p],rt[to[i]]);
}
int main()
{
    seg[0].min = 0x3f3f3f3f;
    scanf("%d%d",&n,&r);
    for(register int i = 1;i <= n;++i)
        scanf("%d",a + i);
    int u,v;
    for(register int i = 1;i < n;++i)
        scanf("%d%d",&u,&v),add(u,v),add(v,u);
    dep[r] = 1,dfs(r);
    scanf("%d",&m);
    int x,k;
    while(m--)
    {
        scanf("%d%d",&x,&k);
        x = (x + lastans) % n + 1,k = (k + lastans) % n;
        printf("%d\n",lastans = query(dep[x],min(dep[x] + k,n),rt[x],1,n));
    }
}


//Re-retry
#include <cstdio>
#include <algorithm>
using namespace std;
const int N = 1e5;
int n,m,r;
int a[N + 5];
int to[(N << 1) + 5],pre[(N << 1) + 5],first[N + 5];
int lastans;
inline void add(int u,int v)
{
    static int tot = 0;
    to[++tot] = v,pre[tot] = first[u],first[u] = tot;
}
struct node
{
    int min;
    int ls,rs;
} seg[(N << 7) + 10];
int rt[N + 5];
int seg_tot;
void insert(int x,int k,int &p,int tl,int tr)
{
    if(!p)
        p = ++seg_tot,seg[p].min = 0x3f3f3f3f;
    seg[p].min = min(seg[p].min,k);
    if(tl == tr)
        return ;
    int mid = tl + tr >> 1;
    x <= mid ? insert(x,k,seg[p].ls,tl,mid) : insert(x,k,seg[p].rs,mid + 1,tr);
}
int query(int l,int r,int p,int tl,int tr)
{
    if(!p || (l <= tl && tr <= r))
        return seg[p].min;
    int mid = tl + tr >> 1;
    int ret = 0x3f3f3f3f;
    if(l <= mid)
        ret = min(ret,query(l,r,seg[p].ls,tl,mid));
    if(r > mid)
        ret = min(ret,query(l,r,seg[p].rs,mid + 1,tr));
    return ret;
}
int merge(int x,int y)
{
    if(!x || !y)
        return x | y;
    int p = ++seg_tot;
    seg[p].min = min(seg[x].min,seg[y].min);
    seg[p].ls = merge(seg[x].ls,seg[y].ls),seg[p].rs = merge(seg[x].rs,seg[y].rs);
    return p;
}
int fa[N + 5],dep[N + 5];
void dfs(int p)
{
    insert(dep[p],a[p],rt[p],1,n);
    for(register int i = first[p];i;i = pre[i])
        if(to[i] ^ fa[p])
            fa[to[i]] = p,dep[to[i]] = dep[p] + 1,dfs(to[i]),rt[p] = merge(rt[p],rt[to[i]]);
}
int main()
{
    seg[0].min = 0x3f3f3f3f;
    scanf("%d%d",&n,&r);
    for(register int i = 1;i <= n;++i)
        scanf("%d",a + i);
    int u,v;
    for(register int i = 1;i < n;++i)
        scanf("%d%d",&u,&v),add(u,v),add(v,u);
    dep[r] = 1,dfs(r);
    scanf("%d",&m);
    int x,k;
    while(m--)
    {
        scanf("%d%d",&x,&k);
        x = (x + lastans) % n + 1,k = (k + lastans) % n;
        printf("%d\n",lastans = query(dep[x],min(dep[x] + k,n),rt[x],1,n));
    }
}


#include <cstdio>
#include <algorithm>
using namespace std;
const int N = 1e5;
int n,m,r;
int a[N + 5];
int to[(N << 1) + 5],pre[(N << 1) + 5],first[N + 5];
int lastans;
inline void add(int u,int v)
{
    static int tot = 0;
    to[++tot] = v,pre[tot] = first[u],first[u] = tot;
}
struct node
{
    int min;
    int ls,rs;
} seg[(N << 5) + 10];
int rt[N + 5];
int seg_tot;
void insert(int x,int k,int &p,int tl,int tr)
{
    if(!p)
        p = ++seg_tot,seg[p].min = 0x3f3f3f3f;
    seg[p].min = min(seg[p].min,k);
    if(tl == tr)
        return ;
    int mid = tl + tr >> 1;
    x <= mid ? insert(x,k,seg[p].ls,tl,mid) : insert(x,k,seg[p].rs,mid + 1,tr);
}
int query(int l,int r,int p,int tl,int tr)
{
    if(!p || (l <= tl && tr <= r))
        return seg[p].min;
    int mid = tl + tr >> 1;
    int ret = 0x3f3f3f3f;
    if(l <= mid)
        ret = min(ret,query(l,r,seg[p].ls,tl,mid));
    if(r > mid)
        ret = min(ret,query(l,r,seg[p].rs,mid + 1,tr));
    return ret;
}
int merge(int x,int y)
{
    if(!x || !y)
        return x | y;
    int p = ++seg_tot;
    seg[p].min = min(seg[x].min,seg[y].min);
    seg[p].ls = merge(seg[x].ls,seg[y].ls),seg[p].rs = merge(seg[x].rs,seg[y].rs);
    return p;
}
int fa[N + 5],dep[N + 5];
void dfs(int p)
{
    insert(dep[p],a[p],rt[p],1,n);
    for(register int i = first[p];i;i = pre[i])
        if(to[i] ^ fa[p])
            fa[to[i]] = p,dep[to[i]] = dep[p] + 1,dfs(to[i]),rt[p] = merge(rt[p],rt[to[i]]);
}
int main()
{
    seg[0].min = 0x3f3f3f3f;
    scanf("%d%d",&n,&r);
    for(register int i = 1;i <= n;++i)
        scanf("%d",a + i);
    int u,v;
    for(register int i = 1;i < n;++i)
        scanf("%d%d",&u,&v),add(u,v),add(v,u);
    dep[r] = 1,dfs(r);
    scanf("%d",&m);
    int x,k;
    while(m--)
    {
        scanf("%d%d",&x,&k);
        x = (x + lastans) % n + 1,k = (k + lastans) % n;
        printf("%d\n",lastans = query(dep[x],min(dep[x] + k,n),rt[x],1,n));
    }
}


#include <cstdio>
#include <algorithm>
using namespace std;
const int N = 1e5;
int n,m,r;
int a[N + 5];
int to[(N << 1) + 5],pre[(N << 1) + 5],first[N + 5];
int lastans;
inline void add(int u,int v)
{
    static int tot = 0;
    to[++tot] = v,pre[tot] = first[u],first[u] = tot;
}
struct node
{
    int min;
    int ls,rs;
} seg[(N << 7) + 10];
int rt[N + 5];
int seg_tot;
void insert(int x,int k,int &p,int tl,int tr)
{
    if(!p)
        p = ++seg_tot,seg[p].min = 0x3f3f3f3f;
    seg[p].min = min(seg[p].min,k);
    if(tl == tr)
        return ;
    int mid = tl + tr >> 1;
    x <= mid ? insert(x,k,seg[p].ls,tl,mid) : insert(x,k,seg[p].rs,mid + 1,tr);
}
int query(int l,int r,int p,int tl,int tr)
{
    if(!p || (l <= tl && tr <= r))
        return seg[p].min;
    int mid = tl + tr >> 1;
    int ret = 0x3f3f3f3f;
    if(l <= mid)
        ret = min(ret,query(l,r,seg[p].ls,tl,mid));
    if(r > mid)
        ret = min(ret,query(l,r,seg[p].rs,mid + 1,tr));
    return ret;
}
int merge(int x,int y)
{
    if(!x || !y)
        return x | y;
    int p = ++seg_tot;
    seg[p].min = min(seg[x].min,seg[y].min);
    seg[p].ls = merge(seg[x].ls,seg[y].ls),seg[p].rs = merge(seg[x].rs,seg[y].rs);
    return p;
}
int fa[N + 5],dep[N + 5];
void dfs(int p)
{
    insert(dep[p],a[p],rt[p],1,n);
    for(register int i = first[p];i;i = pre[i])
        if(to[i] ^ fa[p])
            fa[to[i]] = p,dep[to[i]] = dep[p] + 1,dfs(to[i]),rt[p] = merge(rt[p],rt[to[i]]);
}
int main()
{
    seg[0].min = 0x3f3f3f3f;
    scanf("%d%d",&n,&r);
    for(register int i = 1;i <= n;++i)
        scanf("%d",a + i);
    int u,v;
    for(register int i = 1;i < n;++i)
        scanf("%d%d",&u,&v),add(u,v),add(v,u);
    dep[r] = 1,dfs(r);
    scanf("%d",&m);
    int x,k;
    while(m--)
    {
        scanf("%d%d",&x,&k);
        x = (x + lastans) % n + 1,k = (k + lastans) % n;
        printf("%d\n",lastans = query(dep[x],min(dep[x] + k,n),rt[x],1,n));
    }
}


#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define ld long double
#define pll pair <ll , ll>

#define pb push_back
#define pf push_front
#define pob pop_back
#define pof pop_front
#define mp make_pair

#define X first
#define Y second

#define LB(x) ((x) & -(x))
#define BIT(a , b) (((a)>>(b)) & 1)

#define ll int

const ll MAXN=1e5+10;
const ll INF=1e9+10;

struct node
{
    vector <pll> m;
    vector <ll> e;
};

node segt[4*MAXN];

vector <ll> vertex[MAXN];

ll a[MAXN];
ll b[MAXN];
ll h[MAXN];
ll st[MAXN];
ll ft[MAXN];

bool check[MAXN];

ll t=0;

void set_time_dfs(ll v , ll z)
{
    check[v]=true;
    st[v]=++t;
    h[v]=z;

    for(ll i=0;i<vertex[v].size();i++)
    {
        if (!check[vertex[v][i]])
        {
            set_time_dfs(vertex[v][i] , z+1);
        }
    }

    ft[v]=t;
}

void build(ll id , ll l , ll r)
{
    if (l==r)
    {
        segt[id].m.pb(mp(h[b[l]] , a[b[l]]));
        segt[id].e.pb(a[b[l]]);

        return;
    }

    ll mid=(l+r)/2;

    build(2*id+1 , l , mid);
    build(2*id+2 , mid+1 , r);

    segt[id].m.reserve(r-l+1);
    segt[id].e.reserve(r-l+1);

    for(ll i=0;i<segt[2*id+1].m.size();i++)
    {
        segt[id].m.pb(segt[2*id+1].m[i]);
    }

    for(ll i=0;i<segt[2*id+2].m.size();i++)
    {
        segt[id].m.pb(segt[2*id+2].m[i]);
    }

    sort(segt[id].m.begin() , segt[id].m.end());

    for(ll i=0;i<segt[id].m.size();i++)
    {
        if (i==0)
        {
            segt[id].e.pb(segt[id].m[i].Y);
        }
        else
        {
            segt[id].e.pb(min(segt[id].e.back() , segt[id].m[i-1].Y));
        }
    }
}

ll get_res(ll id , ll L , ll R , ll l , ll r , ll val)
{
    if (L>r || R<l)
    {
        return INF;
    }

    if (L>=l && R<=r)
    {
        ll k=upper_bound(segt[id].m.begin() , segt[id].m.end() , mp(val , INF))-segt[id].m.begin();

        if (k==0)
        {
            return INF;
        }
        else
        {
            return segt[id].e[k-1];
        }
    }

    ll mid=(L+R)/2;

    return min(get_res(2*id+1 , L , mid , l , r , val) , get_res(2*id+2 , mid+1 , R , l , r , val));
}

int main()
{
	ll n , r;
	scanf("%d%d" , &n , &r);

	for(ll i=1;i<=n;i++)
    {
        scanf("%d" , &a[i]);
    }

	for(ll i=0;i<(n-1);i++)
    {
        ll v , u;
        scanf("%d%d" , &v , &u);

        vertex[v].pb(u);
        vertex[u].pb(v);
    }

    set_time_dfs(r , 0);

    for(ll i=1;i<=n;i++)
    {
        b[st[i]]=i;
    }

    build(0 , 1 , n);

    ll q , last=0;
    scanf("%d" , &q);

    for(ll i=0;i<q;i++)
    {
        ll f , g;
        scanf("%d%d" , &f , &g);

        ll x=((f+last)%n)+1 , k=(g+last)%n;
        k+=h[x];

        last=get_res(0 , 1 , n , st[x] , ft[x] , k);
        printf("%d\n" , last);
    }
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back

const int maxn = 1e5 + 20;
const int maxa = 1e9 + 20;

vector<int> adj[maxn] , pm[maxn * 4];

vector<pair<int , int> > seg[maxn * 4];

int h[maxn] , st[maxn] , ft[maxn] , rst[maxn] , a[maxn] , t;

void dfs(int v , int p = -1)
{
	st[v] = ++t;
	rst[t] = v;
	for(auto u : adj[v])
		if(u != p)
		{
			h[u] = h[v] + 1;
			dfs(u , v);
		}
	ft[v] = t;
}

void build(int s , int e , int v)
{
	if(e - s < 2)
	{
		pm[v].pb(maxa);
		pm[v].pb(a[rst[s + 1]]);
		seg[v].pb({h[rst[s + 1]] , a[rst[s + 1]]});
		return;
	}
	int m = (s + e) / 2;
	build(s , m , 2 * v);
	build(m , e , 2 * v + 1);
	for(auto x : seg[2 * v])
		seg[v].pb(x);
	for(auto x : seg[2 * v + 1])
		seg[v].pb(x);
	sort(seg[v].begin() , seg[v].end());
	pm[v].pb(maxa);
	for(auto x : seg[v])
		pm[v].pb(min(pm[v].back() , x.second));
}

int get(int l , int r , int val , int s , int e , int v)
{
	if(l <= s && e <= r)
	{
		int k = upper_bound(seg[v].begin() , seg[v].end() , make_pair(val , maxa)) - seg[v].begin();
		return pm[v][k];
	}
	if(r <= s || e <= l)
		return maxa;
	int m = (s + e) / 2;
	return min(get(l , r , val , s , m , 2 * v) , get(l , r , val , m , e , 2 * v + 1));
}

int main()
{
	int n , r;
	scanf("%d%d", &n, &r);
	r--;
	for(int i = 0; i < n; i++)
		scanf("%d", &a[i]);
	for(int i = 0; i < n - 1; i++)
	{
		int a , b;
		scanf("%d%d", &a, &b);
		adj[--a].pb(--b);
		adj[b].pb(a);
	}

	dfs(r);

	build(0 , n , 1);

	int q;
	scanf("%d", &q);
	int last = 0;
	while(q--)
	{
		int p , q;
		scanf("%d%d", &p, &q);
		p = (p + last) % n;
		q = (q + last) % n;
		last = get(st[p] - 1 , ft[p] , q + h[p] , 0 , n , 1);
		printf("%d\n", last);
	}
}


#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define ld long double
#define pll pair <ll , ll>

#define pb push_back
#define pf push_front
#define pob pop_back
#define pof pop_front
#define mp make_pair

#define X first
#define Y second

#define LB(x) ((x) & -(x))
#define BIT(a , b) (((a)>>(b)) & 1)

#define ll int

const ll MAXN=1e5+10;
const ll INF=1e9+7;

struct node
{
    vector <pll> m;
    vector <ll> e;
};

node segt[4*MAXN];

vector <ll> vertex[MAXN];

ll a[MAXN];
ll b[MAXN];
ll h[MAXN];
ll st[MAXN];
ll ft[MAXN];

bool check[MAXN];

ll t=0;

void set_time_dfs(ll v , ll z)
{
    check[v]=true;
    st[v]=++t;
    h[v]=z;

    for(ll i=0;i<vertex[v].size();i++)
    {
        if (!check[vertex[v][i]])
        {
            set_time_dfs(vertex[v][i] , z+1);
        }
    }

    ft[v]=t;
}

void build(ll id , ll l , ll r)
{
    if (l==r)
    {
        segt[id].m.pb(mp(h[b[l]] , b[l]));
        segt[id].e.pb(b[l]);

        return;
    }

    ll mid=(l+r)/2;

    build(2*id+1 , l , mid);
    build(2*id+2 , mid+1 , r);

    segt[id].m.reserve(r-l+1);
    segt[id].e.reserve(r-l+1);

    for(ll i=0;i<segt[2*id+1].m.size();i++)
    {
        segt[id].m.pb(segt[2*id+1].m[i]);
    }

    for(ll i=0;i<segt[2*id+2].m.size();i++)
    {
        segt[id].m.pb(segt[2*id+2].m[i]);
    }

    sort(segt[id].m.begin() , segt[id].m.end());

    for(ll i=0;i<segt[id].m.size();i++)
    {
        if (i==0)
        {
            segt[id].e.pb(segt[id].m[i].Y);
        }
        else
        {
            segt[id].e.pb(min(segt[id].e.back() , segt[id].m[i-1].Y));
        }
    }
}

ll get_res(ll id , ll L , ll R , ll l , ll r , ll val)
{
    if (L>r || R<l)
    {
        return INF;
    }

    if (L>=l && R<=r)
    {
        ll k=upper_bound(segt[id].m.begin() , segt[id].m.end() , mp(val , INF))-segt[id].m.begin();

        if (k==0)
        {
            return INF;
        }
        else
        {
            return segt[id].e[k-1];
        }
    }

    ll mid=(L+R)/2;

    return min(get_res(2*id+1 , L , mid , l , r , val) , get_res(2*id+2 , mid+1 , R , l , r , val));
}

int main()
{
	ll n , r;
	scanf("%d%d" , &n , &r);

	for(ll i=1;i<=n;i++)
    {
        cin>>a[i];
    }

	for(ll i=0;i<(n-1);i++)
    {
        ll v , u;
        cin>>v>>u;

        vertex[v].pb(u);
        vertex[u].pb(v);
    }

    set_time_dfs(r , 0);

    for(ll i=1;i<=n;i++)
    {
        b[st[i]]=i;
    }

    build(0 , 1 , n);

    ll q , last=0;
    scanf("%d" , &q);

    for(ll i=0;i<q;i++)
    {
        ll f , g;
        scanf("%d%d" , &f , &g);

        ll x=((f+last)%n)+1 , k=(g+last)%n;
        k+=h[x];

        last=get_res(0 , 1 , n , st[x] , ft[x] , k);
        printf("%d\n" , last);
    }
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back

const int maxn = 1e5 + 20;

vector<int> adj[maxn] , pm[maxn];

vector<pair<int , int> > seg[maxn * 4];

int h[maxn] , st[maxn] , ft[maxn] , rst[maxn] , a[maxn] , t;

void dfs(int v , int p = -1)
{
	st[v] = ++t;
	rst[t] = v;
	for(auto u : adj[v])
		if(u != p)
		{
			h[u] = h[v] + 1;
			dfs(u , v);
		}
	ft[v] = t;
}

void build(int s , int e , int v)
{
	if(e - s < 2)
	{
		pm[v].pb(maxn);
		pm[v].pb(a[rst[s + 1]]);
		seg[v].pb({h[rst[s + 1]] , a[rst[s + 1]]});
		return;
	}
	int m = (s + e) / 2;
	build(s , m , 2 * v);
	build(m , e , 2 * v + 1);
	for(auto x : seg[2 * v])
		seg[v].pb(x);
	for(auto x : seg[2 * v + 1])
		seg[v].pb(x);
	sort(seg[v].begin() , seg[v].end());
	pm[v].pb(maxn);
	for(auto x : seg[v])
		pm[v].pb(min(pm[v].back() , x.second));
}

int get(int l , int r , int val , int s , int e , int v)
{
	if(l <= s && e <= r)
	{
		int k = upper_bound(seg[v].begin() , seg[v].end() , make_pair(val , maxn)) - seg[v].begin();
		return pm[v][k];
	}
	if(r <= s || e <= l)
		return maxn;
	int m = (s + e) / 2;
	return min(get(l , r , val , s , m , 2 * v) , get(l , r , val , m , e , 2 * v + 1));
}

int main()
{
	int n , r;
	scanf("%d%d", &n, &r);
	r--;
	for(int i = 0; i < n; i++)
		scanf("%d", &a[i]);
	for(int i = 0; i < n - 1; i++)
	{
		int a , b;
		scanf("%d%d", &a, &b);
		adj[--a].pb(--b);
		adj[b].pb(a);
	}

	dfs(r);

	build(0 , n , 1);

	int q;
	scanf("%d", &q);
	int last = 0;
	while(q--)
	{
		int p , q;
		scanf("%d%d", &p, &q);
		p = (p + last) % n;
		q = (q + last) % n;
		last = get(st[p] - 1 , ft[p] , q + h[p] , 0 , n , 1);
		cout << last << endl;
	}
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back

const int maxn = 1e5 + 20;
const int maxa = 1e9 + 20;

vector<int> adj[maxn] , pm[maxn];

vector<pair<int , int> > seg[maxn * 4];

int h[maxn] , st[maxn] , ft[maxn] , rst[maxn] , a[maxn] , t;

void dfs(int v , int p = -1)
{
	st[v] = ++t;
	rst[t] = v;
	for(auto u : adj[v])
		if(u != p)
		{
			h[u] = h[v] + 1;
			dfs(u , v);
		}
	ft[v] = t;
}

void build(int s , int e , int v)
{
	if(e - s < 2)
	{
		pm[v].pb(maxa);
		pm[v].pb(a[rst[s + 1]]);
		seg[v].pb({h[rst[s + 1]] , a[rst[s + 1]]});
		return;
	}
	int m = (s + e) / 2;
	build(s , m , 2 * v);
	build(m , e , 2 * v + 1);
	for(auto x : seg[2 * v])
		seg[v].pb(x);
	for(auto x : seg[2 * v + 1])
		seg[v].pb(x);
	sort(seg[v].begin() , seg[v].end());
	pm[v].pb(maxa);
	for(auto x : seg[v])
		pm[v].pb(min(pm[v].back() , x.second));
}

int get(int l , int r , int val , int s , int e , int v)
{
	if(l <= s && e <= r)
	{
		int k = upper_bound(seg[v].begin() , seg[v].end() , make_pair(val , maxa)) - seg[v].begin();
		return pm[v][k];
	}
	if(r <= s || e <= l)
		return maxa;
	int m = (s + e) / 2;
	return min(get(l , r , val , s , m , 2 * v) , get(l , r , val , m , e , 2 * v + 1));
}

int main()
{
	int n , r;
	scanf("%d%d", &n, &r);
	r--;
	for(int i = 0; i < n; i++)
		scanf("%d", &a[i]);
	for(int i = 0; i < n - 1; i++)
	{
		int a , b;
		scanf("%d%d", &a, &b);
		adj[--a].pb(--b);
		adj[b].pb(a);
	}

	dfs(r);

	build(0 , n , 1);

	int q;
	scanf("%d", &q);
	int last = 0;
	while(q--)
	{
		int p , q;
		scanf("%d%d", &p, &q);
		p = (p + last) % n;
		q = (q + last) % n;
		last = get(st[p] - 1 , ft[p] , q + h[p] , 0 , n , 1);
		cout << last << endl;
	}
}


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

const int N=100010;
const int M=1000010;
int n,m,tot,cnt,rt,head[N],d[N],root[N],a[N];
struct Edge{
	int suiv,ver;
}e[M<<1];

struct SegmentTree{
	int l,r,mini;
	SegmentTree(){mini=1000000010;}
	#define l(x) t[x].l
	#define r(x) t[x].r
	#define mini(x) t[x].mini
}t[N*50];

inline void lnk(int x,int y)
{
	e[++tot].ver=y;
	e[tot].suiv=head[x];
	head[x]=tot;
}

inline void insert(int &p,int l,int r,int d,int val)
{
	if(!p)p=++cnt;
	if(l==r)return mini(p)=min(mini(p),val),void();
	int mid=l+r>>1;
	if(d<=mid)insert(l(p),l,mid,d,val);
	if(d>mid)insert(r(p),mid+1,r,d,val);
	mini(p)=min(mini(l(p)),mini(r(p)));
}

inline int merge(int x,int y,int l,int r)
{
	if(!x || !y)return x^y;
	int p=++cnt;
	if(l==r)return mini(p)=min(mini(x),mini(y)),p;
	int mid=l+r>>1;
	l(p)=merge(l(x),l(y),l,mid);
	r(p)=merge(r(x),r(y),mid+1,r);
	mini(p)=min(mini(l(p)),mini(r(p)));
	return p;
}

inline int query(int p,int l,int r,int L,int R)
{
    if((L<=l && r<=R)||!p)return mini(p);
    int mid=l+r>>1,res=1ll<<60;
    if(L<=mid)res=min(res,query(l(p),l,mid,L,R));
    if(R>mid)res=min(res,query(r(p),mid+1,r,L,R));
    return res;
}

inline void dfs(int x,int fa)
{
    d[x]=d[fa]+1;
    insert(root[x],1,n,d[x],a[x]);
	for(int i=head[x];i;i=e[i].suiv)
	{
		int y=e[i].ver;
		if(y==fa)continue;
		dfs(y,x);
        root[x]=merge(root[x],root[y],1,n);
	}
}

signed main()
{
	scanf("%lld%lld",&n,&rt);
	for(int i=1;i<=n;i++)
		scanf("%lld",&a[i]);
	for(int i=1,x,y;i<n;i++)
		scanf("%lld%lld",&x,&y),lnk(x,y),lnk(y,x);
	dfs(rt,0);
    scanf("%lld",&m);
    int x,y,ans=0;
    while(m--)
    {
        scanf("%lld%lld",&x,&y);
        x=(x+ans)%n+1,y=(y+ans)%n;
		printf("%lld\n",ans=query(root[x],1,n,d[x],min(d[x]+y,n)));
    }
	return 0;
}


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

const int N=100010;
const int M=1000010;
int n,m,tot,tim,cnt,rt,head[N],dfn[N],low[N],d[N],root[N],siz[N],id[N],a[N],mini[N*40];
struct Edge{
	int suiv,ver;
}e[M<<1];

struct SegmentTree{
	int l,r
//	,mini
	;
//	SegmentTree(){mini=1000000010;}
	#define l(x) t[x].l
	#define r(x) t[x].r
//	#define mini(x) t[x].mini
	#define mini(x) mini[x]
}t[N*40];

//struct Node{
//	int val,id,dep;
//	inline bool operator <(const Node& that)const{
//		return dep<that.dep;
//	}
//}a[N];
inline bool cmp(int x,int y){return d[x]<d[y];}
inline void lnk(int x,int y)
{
	e[++tot].ver=y;
	e[tot].suiv=head[x];
	head[x]=tot;
}

inline void dfs(int x,int fa)
{
	d[x]=d[fa]+1;
	dfn[x]=++tim;siz[x]=1;
	for(int i=head[x];i;i=e[i].suiv)
	{
		int y=e[i].ver;
		if(y==fa)continue;
		dfs(y,x);siz[x]+=siz[y];
	}
	low[x]=tim;
}

inline void insert(int p,int &q,int l,int r,int d,int val)
{
	if(!q)q=++cnt;
	if(l==r)return mini(q)=min(mini(p),val),void();
	int mid=l+r>>1;
	if(d<=mid)insert(l(p),l(q),l,mid,d,val),r(q)=r(p);
	if(d>mid)insert(r(p),r(q),mid+1,r,d,val),l(q)=l(p);
	mini(q)=min(mini(l(q)),mini(r(q)));
}

inline int query(int p,int l,int r,int L,int R)
{
	if(L<=l && r<=R)return mini(p);
	int mid=l+r>>1,res=1ll<<60;
	if(L<=mid)res=min(res,query(l(p),l,mid,L,R));
	if(R>mid)res=min(res,query(r(p),mid+1,r,L,R));
	return res;
}

signed main()
{
//	memset(mini,0x3f,sizeof(mini));
	for(int i=0;i<=N*40-1;i++)
		mini[i]=1ll<<60;
	scanf("%lld%lld",&n,&rt);
	for(int i=1;i<=n;i++)
		scanf("%lld",&a[i]),id[i]=i;
	for(int i=1,x,y;i<n;i++)
		scanf("%lld%lld",&x,&y),lnk(x,y),lnk(y,x);
	dfs(rt,0);
//	sort(a+1,a+1+n);
	sort(id+1,id+1+n,cmp);
	for(int i=1;i<=n;i++)
		insert(root[d[id[i-1]]],root[d[id[i]]],1,n,dfn[id[i]],a[id[i]]);
	scanf("%lld",&m);
	int x,y,ans=0;
	while(m--)
	{
		scanf("%lld%lld",&x,&y);
		x=(x+ans)%n+1,y=(y+ans)%n;
		printf("%lld\n",ans=query(root[min(d[x]+y,d[id[n]])],1,n,dfn[x],dfn[x]+siz[x]-1));
	}
	return 0;
}


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

const int N=100010;
const int M=1000010;
int n,m,tot,tim,cnt,rt,head[N],dfn[N],low[N],d[N],root[N],siz[N],id[N],a[N],mini[N*40];
struct Edge{
	int suiv,ver;
}e[M<1];

struct SegmentTree{
	int l,r
//	,mini
	;
//	SegmentTree(){mini=1000000010;}
	#define l(x) t[x].l
	#define r(x) t[x].r
//	#define mini(x) t[x].mini
	#define mini(x) mini[x]
}t[N*40];

//struct Node{
//	int val,id,dep;
//	inline bool operator <(const Node& that)const{
//		return dep<that.dep;
//	}
//}a[N];
inline bool cmp(int x,int y){return d[x]<d[y];}
inline void lnk(int x,int y)
{
	e[++tot].ver=y;
	e[tot].suiv=head[x];
	head[x]=tot;
}

inline void dfs(int x,int fa)
{
	d[x]=d[fa]+1;
	dfn[x]=++tim;siz[x]=1;
	for(int i=head[x];i;i=e[i].suiv)
	{
		int y=e[i].ver;
		if(y==fa)continue;
		dfs(y,x);siz[x]+=siz[y];
	}
	low[x]=tim;
}

inline void insert(int p,int &q,int l,int r,int d,int val)
{
	if(!q)q=++cnt;
	if(l==r)return mini(q)=min(mini(p),val),void();
	int mid=l+r>>1;
	if(d<=mid)insert(l(p),l(q),l,mid,d,val),r(q)=r(p);
	if(d>mid)insert(r(p),r(q),mid+1,r,d,val),l(q)=l(p);
	mini(q)=min(mini(l(q)),mini(r(q)));
}

inline int query(int p,int l,int r,int L,int R)
{
	if(L<=l && r<=R)return mini(p);
	int mid=l+r>>1,res=1000000000;
	if(L<=mid)res=min(res,query(l(p),l,mid,L,R));
	if(R>mid)res=min(res,query(r(p),mid+1,r,L,R));
	return res;
}

int main()
{
	memset(mini,0x3f,sizeof(mini));
	scanf("%d%d",&n,&rt);
	for(int i=1;i<=n;i++)
		scanf("%d",&a[i]),id[i]=i;
	for(int i=1,x,y;i<n;i++)
		scanf("%d%d",&x,&y),lnk(x,y),lnk(y,x);
	dfs(rt,0);
//	sort(a+1,a+1+n);
	sort(id+1,id+1+n,cmp);
	for(int i=1;i<=n;i++)
		insert(root[d[id[i-1]]],root[d[id[i]]],1,n,dfn[id[i]],a[id[i]]);
	scanf("%d",&m);
	int x,y,ans=0;
	while(m--)
	{
		scanf("%d%d",&x,&y);
		x=(x+ans)%n+1,y=(y+ans)%n;
		printf("%d\n",ans=query(root[min(d[x]+y,d[id[n]])],1,n,dfn[x],dfn[x]+siz[x]-1));
	}
	return 0;
}


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

const int N=100010;
const int M=1000010;
int n,m,tot,tim,cnt,rt,head[N],dfn[N],low[N],d[N],root[N],siz[N],id[N],a[N],mini[N*50];
struct Edge{
	int suiv,ver;
}e[M<<1];

struct SegmentTree{
	int l,r
//	,mini
	;
//	SegmentTree(){mini=1000000010;}
	#define l(x) t[x].l
	#define r(x) t[x].r
//	#define mini(x) t[x].mini
	#define mini(x) mini[x]
}t[N*50];

//struct Node{
//	int val,id,dep;
//	inline bool operator <(const Node& that)const{
//		return dep<that.dep;
//	}
//}a[N];
inline bool cmp(int x,int y){return d[x]<d[y];}
inline void lnk(int x,int y)
{
	e[++tot].ver=y;
	e[tot].suiv=head[x];
	head[x]=tot;
}

inline void dfs(int x,int fa)
{
	d[x]=d[fa]+1;
	dfn[x]=++tim;siz[x]=1;
	for(int i=head[x];i;i=e[i].suiv)
	{
		int y=e[i].ver;
		if(y==fa)continue;
		dfs(y,x);siz[x]+=siz[y];
	}
	low[x]=tim;
}

inline void insert(int p,int &q,int l,int r,int d,int val)
{
	if(!q)q=++cnt;
	if(l==r)return mini(q)=min(mini(p),val),void();
	int mid=l+r>>1;
	if(d<=mid)insert(l(p),l(q),l,mid,d,val),r(q)=r(p);
	if(d>mid)insert(r(p),r(q),mid+1,r,d,val),l(q)=l(p);
	mini(q)=min(mini(l(q)),mini(r(q)));
}

inline int query(int p,int l,int r,int L,int R)
{
	if(L<=l && r<=R)return mini(p);
	int mid=l+r>>1,res=1ll<<60;
	if(L<=mid)res=min(res,query(l(p),l,mid,L,R));
	if(R>mid)res=min(res,query(r(p),mid+1,r,L,R));
	return res;
}

signed main()
{
//	memset(mini,0x3f,sizeof(mini));
	for(int i=0;i<=N*40-1;i++)
		mini[i]=1ll<<60;
	scanf("%lld%lld",&n,&rt);
	for(int i=1;i<=n;i++)
		scanf("%lld",&a[i]),id[i]=i;
	for(int i=1,x,y;i<n;i++)
		scanf("%lld%lld",&x,&y),lnk(x,y),lnk(y,x);
	dfs(rt,0);
//	sort(a+1,a+1+n);
	sort(id+1,id+1+n,cmp);
	for(int i=1;i<=n;i++)
		insert(root[d[id[i-1]]],root[d[id[i]]],1,n,dfn[id[i]],a[id[i]]);
	scanf("%lld",&m);
	int x,y,ans=0;
	while(m--)
	{
		scanf("%lld%lld",&x,&y);
		x=(x+ans)%n+1,y=(y+ans)%n;
		printf("%lld\n",ans=query(root[min(d[x]+y,d[id[n]])],1,n,dfn[x],dfn[x]+siz[x]-1));
	}
	return 0;
}


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

#define ll long long
#define ld long double
#define F  first
#define S second
#define sf(x) scanf("%d",&x)
#define pf(x) printf("%d\n",x)
#define ALL(x) x.begin(),x.end()
#define pb push_back
#define mp make_pair
#define mod1 1000000007LL
#define mod2 998244353LL

void solve()
{
int n;
cin>>n;

cout<<0<<" "<<0<<endl;
string s,ls;
cin>>s;
ls=s;
ll mx,my;
if(s=="black")
{
    ll bx=0,by=0,wx=1e9,wy=1e9;
     mx = (wx+bx+1)/2;
     my = (wy+by+1)/2;

    for(int i=2;i<=n;i++)
    {
         cout<<mx<<" "<<my<<endl;
          cin>>s;

        if(s[0]=='b')
        {
           bx=mx,by=my;
        }
        else
        {
            wx=mx,wy=my;
        }
        mx = (wx+bx+1)/2;
       my = (wy+by+1)/2;

      // cout<<bx<<" "<<by<<" "<<wx<<" "<<wy<<endl;
    }
}
else
{
    ll wx=0,wy=0,bx=1e9,by=1e9;
     mx = (wx+bx+1)/2;
     my = (wy+by+1)/2;

       for(int i=2;i<=n;i++)
    {
        cout<<mx<<" "<<my<<endl;
        cin>>s;

        if(s[0]=='b')
        {
           bx=mx,by=my;
        }
        else
        {
            wx=mx,wy=my;
        }
        mx = (wx+bx+1)/2;
        my = (wy+by+1)/2;
        //cout<<bx<<" "<<by<<" "<<wx<<" "<<wy<<endl;
    }
}

cout<<mx-11<<" "<<my+11<<" "<<mx<<" "<<my;

}

signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
   //freopen("input.txt","r",stdin);
    int t=1 ;
 //   cin>>t;

   /* for(int i=2;i<=1000000;i++)
    {

        if(visited[i]==0)
        {
            prime.pb(i);
            for(int j=2*i;j<=1000000;j+=i)
                visited[j]=1;
        }
    }*/

    while(t--)
     {
      solve();
     }
    return 0;
}


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

#define ll long long
#define ld long double
#define F  first
#define S second
#define sf(x) scanf("%d",&x)
#define pf(x) printf("%d\n",x)
#define ALL(x) x.begin(),x.end()
#define pb push_back
#define mp make_pair
#define mod1 1000000007LL
#define mod2 998244353LL

void solve()
{
int n;
cin>>n;

cout<<0<<" "<<0<<endl;
string s,ls;
cin>>s;
ls=s;
ll mx,my;
if(s=="black")
{
    ll bx=0,by=0,wx=1e9-1,wy=1e9-1;
     mx = (wx+bx)/2;
     my = (wy+by)/2;

    for(int i=2;i<=n;i++)
    {
         cout<<mx<<" "<<my<<endl;
          cin>>s;

        if(s[0]=='b')
        {
           bx=mx,by=my;
        }
        else
        {
            wx=mx,wy=my;
        }
        mx = (wx+bx)/2;
       my = (wy+by)/2;

      // cout<<bx<<" "<<by<<" "<<wx<<" "<<wy<<endl;
    }
}
else
{
    ll wx=0,wy=0,bx=1e9-1,by=1e9-1;
     mx = (wx+bx)/2;
     my = (wy+by)/2;

       for(int i=2;i<=n;i++)
    {
        cout<<mx<<" "<<my<<endl;
        cin>>s;

        if(s[0]=='b')
        {
           bx=mx,by=my;
        }
        else
        {
            wx=mx,wy=my;
        }
        mx = (wx+bx)/2;
        my = (wy+by)/2;
        //cout<<bx<<" "<<by<<" "<<wx<<" "<<wy<<endl;
    }
}

cout<<mx-11<<" "<<my+11<<" "<<mx<<" "<<my;

}

signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
   //freopen("input.txt","r",stdin);
    int t=1 ;
 //   cin>>t;

   /* for(int i=2;i<=1000000;i++)
    {

        if(visited[i]==0)
        {
            prime.pb(i);
            for(int j=2*i;j<=1000000;j+=i)
                visited[j]=1;
        }
    }*/

    while(t--)
     {
      solve();
     }
    return 0;
}


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

#define ll long long
#define ld long double
#define F  first
#define S second
#define sf(x) scanf("%d",&x)
#define pf(x) printf("%d\n",x)
#define ALL(x) x.begin(),x.end()
#define pb push_back
#define mp make_pair
#define mod1 1000000007LL
#define mod2 998244353LL

void solve()
{

int n;
cin>>n;
map< pair<int,int> , int > co;
map< pair<int,int> , int > :: iterator it,it1;

co[{1e9+1,1e9+1}]=2;
cout<<0<<" "<<0<<endl;
string s,ls;
cin>>s;
ls=s;
ll mx,my;

if(s=="black")
{
    co[{0,0}]=0;
    ll bx=0,by=0,wx=1e9-1,wy=0;
     mx = (wx+bx+1)/2;
     my = (wy+by+1)/2;

    for(int i=2;i<=n;i++)
    {
         cout<<mx<<" "<<my<<endl;
          cin>>s;

        if(s[0]=='b')
        {
            co[{mx,0}]=0;
           bx=mx,by=my;
        }
        else
        {
            co[{mx,0}]=1;
            wx=mx,wy=my;
        }
        mx = (wx+bx+1)/2;
       my = (wy+by+1)/2;

       //cout<<bx<<" "<<by<<" "<<wx<<" "<<wy<<endl;
    }

}
else
{
    co[{0,0}]=1;
    ll wx=0,wy=0,bx=1e9-1,by=0;
     mx = (wx+bx)/2;
     my = (wy+by)/2;

       for(int i=2;i<=n;i++)
    {
        cout<<mx<<" "<<my<<endl;
        cin>>s;

        if(s[0]=='b')
        {
            co[{mx,0}]=0;
           bx=mx,by=my;
        }
        else
        {
            co[{mx,0}]=1;
            wx=mx,wy=my;
        }
        mx = (wx+bx+1)/2;
        my = (wy+by+1)/2;
        //cout<<bx<<" "<<by<<" "<<wx<<" "<<wy<<endl;
    }

}
it=co.begin();
it1=++it;
it--;

while(it->S == it1->S)
{
    it1++;
    it++;
}

cout<<(it->F).F+1<<" "<<0<<" "<<(it->F).F+1<<" "<<1;

}

signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
   //freopen("input.txt","r",stdin);
    int t=1 ;
 //   cin>>t;

   /* for(int i=2;i<=1000000;i++)
    {

        if(visited[i]==0)
        {
            prime.pb(i);
            for(int j=2*i;j<=1000000;j+=i)
                visited[j]=1;
        }
    }*/

    while(t--)
     {
      solve();
     }
    return 0;
}


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

#define ll long long
#define ld long double
#define F  first
#define S second
#define sf(x) scanf("%d",&x)
#define pf(x) printf("%d\n",x)
#define ALL(x) x.begin(),x.end()
#define pb push_back
#define mp make_pair
#define mod1 1000000007LL
#define mod2 998244353LL

void solve()
{

int n;
cin>>n;

cout<<0<<" "<<1<<endl;
string s,ls;
cin>>s;
ls=s;
ll mx,left=0,rgt=1e9;
mx = 0;

if(s=="black")
{
    left=mx+1;
    mx= (left+rgt)/2;
    for(int i=2;i<=n;i++)
    {
         cout<<mx<<" "<<1<<endl;
          cin>>s;

        if(s[0]=='b')
        {

        left=mx+1;
        }
        else
        {
            rgt=mx-1;
        }
       mx = (left+rgt)/2;
      //  cout<<left<<" "<<rgt<<" "<<mx<<endl;
    }

}
else
{
    left=mx+1;
    mx= (left+rgt)/2;
    for(int i=2;i<=n;i++)
    {
         cout<<mx<<" "<<1<<endl;
          cin>>s;

        if(s[0]=='w')
        {
        left=mx+1;
        }
        else
        {
            rgt=mx-1;
        }
       mx = (left+rgt)/2;
     //  cout<<left<<" "<<rgt<<" "<<mx<<endl;
    }
}

if(left<=rgt)
cout<<left<<" "<<0<<" "<<left<<" "<<1;
else
cout<<rgt<<" "<<2<<" "<<left<<" "<<0;
}

signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
   //freopen("input.txt","r",stdin);
    int t=1 ;
 //   cin>>t;

   /* for(int i=2;i<=1000000;i++)
    {

        if(visited[i]==0)
        {
            prime.pb(i);
            for(int j=2*i;j<=1000000;j+=i)
                visited[j]=1;
        }
    }*/

    while(t--)
     {
      solve();
     }
    return 0;
}


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

#define ll long long
#define ld long double
#define F  first
#define S second
#define sf(x) scanf("%d",&x)
#define pf(x) printf("%d\n",x)
#define ALL(x) x.begin(),x.end()
#define pb push_back
#define mp make_pair
#define mod1 1000000007LL
#define mod2 998244353LL

void solve()
{
int n;
cin>>n;

cout<<0<<" "<<0<<endl;
string s,ls;
cin>>s;
ls=s;
ll mx,my;
if(s=="black")
{
    ll bx=0,by=0,wx=1e9-1,wy=1e9-1;
     mx = (wx+bx)/2;
     my = (wy+by)/2;

    for(int i=2;i<=n;i++)
    {
         cout<<mx<<" "<<my<<endl;
          cin>>s;

        if(s[0]=='b')
        {
           bx=mx,by=my;
        }
        else
        {
            wx=mx,wy=my;
        }
        mx = (wx+bx)/2;
       my = (wy+by)/2;
    }
}
else
{
    ll wx=0,wy=0,bx=1e9-1,by=1e9-1;
     mx = (wx+bx)/2;
     my = (wy+by)/2;

       for(int i=2;i<=n;i++)
    {
        cout<<mx<<" "<<my<<endl;
        cin>>s;

        if(s[0]=='b')
        {
           bx=mx,by=my;
        }
        else
        {
            wx=mx,wy=my;
        }
        mx = (wx+bx)/2;
        my = (wy+by)/2;
    }
}

cout<<0<<" "<<2*my<<" "<<2*mx<<" "<<0;

}

signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
   //freopen("input.txt","r",stdin);
    int t=1 ;
 //   cin>>t;

   /* for(int i=2;i<=1000000;i++)
    {

        if(visited[i]==0)
        {
            prime.pb(i);
            for(int j=2*i;j<=1000000;j+=i)
                visited[j]=1;
        }
    }*/

    while(t--)
     {
      solve();
     }
    return 0;
}


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

#define ll long long
#define ld long double
#define F  first
#define S second
#define sf(x) scanf("%d",&x)
#define pf(x) printf("%d\n",x)
#define ALL(x) x.begin(),x.end()
#define pb push_back
#define mp make_pair
#define mod1 1000000007LL
#define mod2 998244353LL

void solve()
{

int n;
cin>>n;
map< pair<int,int> , int > co;
map< pair<int,int> , int > :: iterator it,it1;

co[{1e9+1,1e9+1}]=2;
cout<<0<<" "<<0<<endl;
string s,ls;
cin>>s;
ls=s;
ll mx,my;

if(s=="black")
{
    co[{0,0}]=0;
    ll bx=0,by=0,wx=1e9-1,wy=0;
     mx = (wx+bx+1)/2;
     my = (wy+by+1)/2;

    for(int i=2;i<=n;i++)
    {
         cout<<mx<<" "<<my<<endl;
          cin>>s;

        if(s[0]=='b')
        {
            co[{mx,0}]=0;
           bx=mx,by=my;
        }
        else
        {
            co[{mx,0}]=1;
            wx=mx,wy=my;
        }
        mx = (wx+bx)/2;
       my = (wy+by)/2;

       //cout<<bx<<" "<<by<<" "<<wx<<" "<<wy<<endl;
    }

}
else
{
    co[{0,0}]=1;
    ll wx=0,wy=0,bx=1e9-1,by=0;
     mx = (wx+bx)/2;
     my = (wy+by)/2;

       for(int i=2;i<=n;i++)
    {
        cout<<mx<<" "<<my<<endl;
        cin>>s;

        if(s[0]=='b')
        {
            co[{mx,0}]=0;
           bx=mx,by=my;
        }
        else
        {
            co[{mx,0}]=1;
            wx=mx,wy=my;
        }
        mx = (wx+bx+1)/2;
        my = (wy+by+1)/2;
        //cout<<bx<<" "<<by<<" "<<wx<<" "<<wy<<endl;
    }

}
it=co.begin();
it1=++it;
it--;

while(it->S == it1->S)
{
    it1++;
    it++;
}

cout<<(it->F).F+1<<" "<<0<<" "<<(it->F).F+1<<" "<<1;

}

signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
   //freopen("input.txt","r",stdin);
    int t=1 ;
 //   cin>>t;

   /* for(int i=2;i<=1000000;i++)
    {

        if(visited[i]==0)
        {
            prime.pb(i);
            for(int j=2*i;j<=1000000;j+=i)
                visited[j]=1;
        }
    }*/

    while(t--)
     {
      solve();
     }
    return 0;
}


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

#define ll long long
#define ld long double
#define F  first
#define S second
#define sf(x) scanf("%d",&x)
#define pf(x) printf("%d\n",x)
#define ALL(x) x.begin(),x.end()
#define pb push_back
#define mp make_pair
#define mod1 1000000007LL
#define mod2 998244353LL

void solve()
{
int n;
cin>>n;

cout<<0<<" "<<0<<endl;
string s,ls;
cin>>s;
ls=s;
ll mx,my;
if(s=="black")
{
    ll bx=0,by=0,wx=1e9-1,wy=1e9-1;
     mx = (wx+bx)/2;
     my = (wy+by)/2;

    for(int i=2;i<=n;i++)
    {
         cout<<mx<<" "<<my<<endl;
          cin>>s;

        if(s[0]=='b')
        {
           bx=mx,by=my;
        }
        else
        {
            wx=mx,wy=my;
        }
        mx = (wx+bx)/2;
       my = (wy+by)/2;

      // cout<<bx<<" "<<by<<" "<<wx<<" "<<wy<<endl;
    }
}
else
{
    ll wx=0,wy=0,bx=1e9-1,by=1e9-1;
     mx = (wx+bx)/2;
     my = (wy+by)/2;

       for(int i=2;i<=n;i++)
    {
        cout<<mx<<" "<<my<<endl;
        cin>>s;

        if(s[0]=='b')
        {
           bx=mx,by=my;
        }
        else
        {
            wx=mx,wy=my;
        }
        mx = (wx+bx)/2;
        my = (wy+by)/2;
        //cout<<bx<<" "<<by<<" "<<wx<<" "<<wy<<endl;
    }
}

cout<<mx-11<<" "<<my-11<<" "<<mx<<" "<<my;

}

signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
   //freopen("input.txt","r",stdin);
    int t=1 ;
 //   cin>>t;

   /* for(int i=2;i<=1000000;i++)
    {

        if(visited[i]==0)
        {
            prime.pb(i);
            for(int j=2*i;j<=1000000;j+=i)
                visited[j]=1;
        }
    }*/

    while(t--)
     {
      solve();
     }
    return 0;
}


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

#define ll long long
#define ld long double
#define F  first
#define S second
#define sf(x) scanf("%d",&x)
#define pf(x) printf("%d\n",x)
#define ALL(x) x.begin(),x.end()
#define pb push_back
#define mp make_pair
#define mod1 1000000007LL
#define mod2 998244353LL

void solve()
{

int n;
cin>>n;
map< pair<int,int> , int > co;
map< pair<int,int> , int > :: iterator it,it1;

co[{1e9+1,1e9+1}]=2;
cout<<0<<" "<<0<<endl;
string s,ls;
cin>>s;
ls=s;
ll mx,my;

if(s=="black")
{
    co[{0,0}]=0;
    ll bx=0,by=0,wx=1e9-1,wy=0;
     mx = (wx+bx+1)/2;
     my = (wy+by+1)/2;

    for(int i=2;i<=n;i++)
    {
         cout<<mx<<" "<<my<<endl;
          cin>>s;

        if(s[0]=='b')
        {
            co[{mx,0}]=0;
           bx=mx,by=my;
        }
        else
        {
            co[{mx,0}]=1;
            wx=mx,wy=my;
        }
        mx = (wx+bx)/2;
       my = (wy+by)/2;

       //cout<<bx<<" "<<by<<" "<<wx<<" "<<wy<<endl;
    }

}
else
{
    co[{0,0}]=1;
    ll wx=0,wy=0,bx=1e9,by=0;
     mx = (wx+bx)/2;
     my = (wy+by)/2;

       for(int i=2;i<=n;i++)
    {
        cout<<mx<<" "<<my<<endl;
        cin>>s;

        if(s[0]=='b')
        {
            co[{mx,0}]=0;
           bx=mx,by=my;
        }
        else
        {
            co[{mx,0}]=1;
            wx=mx,wy=my;
        }
        mx = (wx+bx)/2;
        my = (wy+by)/2;
        //cout<<bx<<" "<<by<<" "<<wx<<" "<<wy<<endl;
    }

}
it=co.begin();
it1=++it;
it--;

while(it->S == it1->S)
{
    it1++;
    it++;
}

cout<<(it->F).F+1<<" "<<0<<" "<<(it->F).F+1<<" "<<1;

}

signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
   //freopen("input.txt","r",stdin);
    int t=1 ;
 //   cin>>t;

   /* for(int i=2;i<=1000000;i++)
    {

        if(visited[i]==0)
        {
            prime.pb(i);
            for(int j=2*i;j<=1000000;j+=i)
                visited[j]=1;
        }
    }*/

    while(t--)
     {
      solve();
     }
    return 0;
}