#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")

#include<bits/stdc++.h>
#define itn int
#define ll long long
#define yes {puts("Yes");}
#define no {puts("No");}
using namespace std;

ll read(){
	ll a=0,b=1;char c=getchar();
	while(c>'9'||c<'0'){if(c=='-')b=-1;c=getchar();}
	while(c>='0'&&c<='9')a=a*10+c-48,c=getchar();
	return a*b;
}

int v[31][31],h[31][31],n=30;
char p[1005];int cnt;

struct node{
	int x,y;ll w;int ty;
};

bool operator<(node a,node b){return a.w>b.w;}

ll dis[31][31];
int vis[31][31];

priority_queue<node>q;

namespace test{
	int v[31][31],h[31][31];
	int clac(int sx,int sy,int ex,int ey,char *p,int ct){
		int ans=0;
		for(int i=1;i<=ct;i++){
			if(p[i]=='U')ans+=v[sx-1][sy],sx--;
			else if(p[i]=='D')ans+=v[sx][sy],sx++;
			else if(p[i]=='L')ans+=h[sx][sy-1],sy--;
			else if(p[i]=='R')ans+=h[sx][sy],sy++;
			else exit(1);
			if(sx>=30||sx<0||sy>=30||sy<0)exit(2);
		}
		return ans;
	}
}

int main(){
/*	freopen("0000.txt","r",stdin);
	freopen("test.out","w",stdout);
	for(int i=0;i<30;i++){
		for(int j=0;j<29;j++){
			cin>>test::h[i][j];
		}
	}
	for(int i=0;i<29;i++){
		for(int j=0;j<30;j++){
			cin>>test::v[i][j];
		}
	}*/
	for(int i=0;i<=30;i++){
		for(int j=0;j<=30;j++){
			v[i][j]=h[i][j]=4000;
		}
	}
	for(int i=1;i<=1000;i++){
		int sx,sy,ex,ey;
		cin>>sx>>sy>>ex>>ey;
		
		memset(dis,0x3f,sizeof dis);
		memset(vis,0,sizeof vis);
		
		
		dis[sx][sy]=0;
		q.push(node{sx,sy,0,-1});
		while(!q.empty()){
			node t=q.top();q.pop();
			if(vis[t.x][t.y])continue;
			vis[t.x][t.y]=t.ty;
			//if(t.x==ex&&t.y==ey)break;
			int o=dis[t.x][t.y],x=t.x,y=t.y;
			if(x+1<30&&dis[x+1][y]>o+h[x][y]){
				dis[x+1][y]=o+h[x][y];
				if(!vis[x+1][y])q.push(node{x+1,y,dis[x+1][y],2});//D
			}
			if(y+1<30&&dis[x][y+1]>o+v[x][y]){
				dis[x][y+1]=o+v[x][y];
				if(!vis[x][y+1])q.push(node{x,y+1,dis[x][y+1],3});//R
			}
			if(x-1>=0&&dis[x-1][y]>o+h[x-1][y]){
				dis[x-1][y]=o+h[x-1][y];
				if(!vis[x-1][y])q.push(node{x-1,y,dis[x-1][y],1});//U
			}
			if(y-1>=0&&dis[x][y-1]>o+v[x][y-1]){
				dis[x][y-1]=o+v[x][y-1];
				if(!vis[x][y-1])q.push(node{x,y-1,dis[x][y-1],4});//L
			}
		}
		while(!q.empty())q.pop();
		cnt=0;
		for(int x=ex,y=ey;vis[x][y]!=-1&&vis[x][y];){
			if(vis[x][y]==1)p[++cnt]='U',x++;
			else if(vis[x][y]==2)p[++cnt]='D',x--;
			else if(vis[x][y]==3)p[++cnt]='R',y--;
			else if(vis[x][y]==4)p[++cnt]='L',y++;
		}
		reverse(p+1,p+1+cnt);
		for(int i=1;i<=cnt;i++)cout<<p[i];
		cout<<endl;
		fflush(stdout);
		
		/*for(int i=0;i<30;i++){
			for(int j=0;j<30;j++){
				cout<<dis[i][j]<<' ';
			}
			puts("");
		}
		
		for(int i=0;i<30;i++){
			for(int j=0;j<30;j++){
				cout<<h[i][j]<<' ';
			}
			puts("");
		}for(int i=0;i<30;i++){
			for(int j=0;j<30;j++){
				cout<<v[i][j]<<' ';
			}
			puts("");
		}*/
		
		int b;
		//int aa;double ee;cin>>aa>>ee;
		//b=test::clac(sx,sy,ex,ey,p,cnt)*ee;
		cin>>b;
		int cg=(b-dis[ex][ey])/cnt;
		for(int x=ex,y=ey;vis[x][y]!=-1&&vis[x][y];){
			if(vis[x][y]==1)h[x][y]+=cg,x++;
			else if(vis[x][y]==2)h[x-1][y]+=cg,x--;
			else if(vis[x][y]==3)v[x][y-1]+=cg,y--;
			else if(vis[x][y]==4)v[x][y]+=cg,y++;
		}
	}
	return 0;
}


#include "bits/stdc++.h"
#define int long long
#define endl '\n'
using namespace std;
 
typedef long long ll;
typedef long double ld;
#define db(x) cerr << #x << ": " << x << '\n';
#define read(a) int a; cin >> a;
#define reads(s) string s; cin >> s;
#define readb(a, b) int a, b; cin >> a >> b;
#define readc(a, b, c) int a, b, c; cin >> a >> b >> c;
#define readarr(a, n) int a[(n) + 1] = {}; FOR(i, 1, (n)) {cin >> a[i];}
#define readmat(a, n, m) int a[n + 1][m + 1] = {}; FOR(i, 1, n) {FOR(j, 1, m) cin >> a[i][j];}

#define print(a) cout << a << endl;
#define printarr(a, n) FOR (i, 1, n) cout << a[i] << " "; cout << endl;
#define printv(v) for (int i: v) cout << i << " "; cout << endl;
#define printmat(a, n, m) FOR (i, 1, n) {FOR (j, 1, m) cout << a[i][j] << " "; cout << endl;}
#define all(v) v.begin(), v.end()
#define sz(v) (int)(v.size())
#define rz(v, n) v.resize((n) + 1);
#define pb push_back
#define fi first
#define se second
#define vi vector <int>
#define pi pair <int, int>
#define vpi vector <pi>
#define vvi vector <vi>
#define setprec cout << fixed << showpoint << setprecision(20);
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define FORD(i, a, b) for (int i = (a); i >= (b); i--)
const ll inf = 1e18;
const ll mod = 1e9 + 7;
const ll N = 2e5 + 10;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

int power (int a, int b = mod - 2)
{
    int res = 1;
    while (b > 0) {
        if (b & 1)
            res = res * a % mod;
        a = a * a % mod;
        b >>= 1;
    }
    return res;
}

int n, tim = 0;
vi adj[N];
vpi query[N];
int cnt[N], siz[N], heavy[N], dep[N], euler[N], tin[N], tout[N], ans[N];

void dfs1 (int cur, int prev)
{
    tin[cur] = ++tim;
    euler[tim] = cur;
    
    dep[cur] = dep[prev] + 1;
    siz[cur] = 1;
    pi mx = {0, 0};
    
    for (int i: adj[cur])
        if (i != prev)
        {
            dfs1(i, cur);
            siz[cur] += siz[i];
            mx = max(mx, {siz[i], i});
        }
    
    heavy[cur] = mx.se;
    tout[cur] = tim;
}

void dfs (int cur, int prev, bool keep)
{
    for (int i: adj[cur])
        if (i != prev && i != heavy[cur])
            dfs(i, cur, 0);
        
    if (heavy[cur]) dfs(heavy[cur], cur, 1);
    
    cnt[dep[cur]]++;
    for (int i: adj[cur])
        if (i != prev && i != heavy[cur])
            FOR (j, tin[i], tout[i]) cnt[dep[euler[j]]]++;
    
    for (pi i: query[cur]) ans[i.se] = cnt[i.fi + 1];
    
    if (!keep)
    {
        FOR (i, dep[cur], n)
        {
            if (!cnt[i]) break;
            cnt[i] = 0;
        }
    }
}

signed main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	
	cin >> n;
	readarr(p, n - 1);
	FOR (i, 2, n) adj[i].pb(p[i - 1]), adj[p[i - 1]].pb(i);
	
	read(q);
	FOR (i, 1, q)
	{
	    readb(a, b);
	    query[a].pb({b, i});
	}
	
	dfs1(1, 0);
	dfs(1, 0, 1);
	FOR (i, 1, q) cout << ans[i] << endl;
}












