// time-limit: 2000
// problem-url: https://atcoder.jp/contests/abc202/tasks/abc202_e
#include <bits/stdc++.h>
using namespace std;

#define nl "\n"
#define big int

#ifdef DEBUG
#define ds(statement) cerr << statement << endl
#define dv(var) cerr << (#var) << ": " << var << endl
#else
#define ds(statement)
#define dv(var)
#endif

vector<vector<big>> child;
vector<pair<big, big>> ts;
vector<vector<big>> depth_ent;
big tc = 0;

void dfs(big node, big depth)
{
    ts[node].first = tc++;
    depth_ent[depth].push_back(ts[node].first);

    for (auto c : child[node])
    {
        dfs(c, depth + 1);
    }

    ts[node].second = tc++;
}

void solve()
{
    big n, t, q;
    cin >> n;
    child = vector<vector<big>>(n);
    depth_ent = vector<vector<big>>(n);
    ts = vector<pair<big, big>>(n);
    for (big i = 1; i < n; i++)
    {
        cin >> t;
        child[t - 1].push_back(i);
    }

    dfs(0, 0);

    cin >> q;
    while (q-- > 0)
    {
        big d, u, ans;
        cin >> u >> d;
        u--;
        
        vector<big> v = depth_ent[d];
        ans = (lower_bound(v.begin(),v.end(),ts[u].second) - lower_bound(v.begin(),v.end(),ts[u].first)); 

        cout << ans << nl;
    }
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
#ifdef DEBUG
    (void)!freopen("/home/sarvjot/error.txt", "w", stderr);
#endif
    solve();
    return 0;
}
// g++ -std=c++11 a.cpp
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<map>
#include<set>
#include<unordered_map>
#include<utility>
#include<cmath>
#include<random>
#include<cstring>
#include<queue>
#include<stack>
#include<bitset>
#include<cstdio>
#include<sstream>
#include<random>
#include<iomanip>
#include<assert.h>
#include<typeinfo>
#define loop(i,a,b) for(int i=a;i<b;i++)
#define rep(i,a) loop(i,0,a)
#define FOR(i,a) for(auto i:a)
#define pb push_back
#define all(in) in.begin(),in.end()
#define shosu(x) fixed<<setprecision(x)
#define show1d(v) {rep(_,v.size())cout<<" "<<v[_];cout<<endl;}
#define show2d(v) {rep(__,v.size())show1d(v[__]);}
using namespace std;
//kaewasuretyuui
typedef long long ll;
#define int ll
typedef int Def;
typedef pair<Def,Def> pii;
typedef vector<Def> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef vector<string> vs;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef pair<Def,pii> pip;
typedef vector<pip>vip;
#define mt make_tuple
typedef tuple<int,int,int> tp;
typedef vector<tp> vt;
typedef vector<vt>vvt;
template<typename A,typename B>bool cmin(A &a,const B &b){return a>b?(a=b,true):false;}
template<typename A,typename B>bool cmax(A &a,const B &b){return a<b?(a=b,true):false;}
const double PI=acos(-1);
const long double EPS=1e-9;
Def inf = sizeof(Def) == sizeof(long long) ? 2e18 : 1e9+10;
int dx[]={-1,0,1,0};
int dy[]={0,1,0,-1};
#define yes cout<<"Yes\n"
#define no cout<<"No\n"

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

	int n;
	cin>>n;
	vi a(n),b(n);
	rep(i,n)cin>>a[i];
	rep(i,n)cin>>b[i];
	int s=0;
	rep(i,n)s+=a[i]*b[i];
	if(s)no;
	else yes;
}
