//                        YATIN KWATRA

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#define ll                      long long
#define ar                      array
#define sz(v)                   (int)(v.size())
#define inf                     1e18
#define int                     ll
#define FIO                     ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define ld                      long double
#define ull                     unsigned long long
#define endl                    "\n"
#define fo(i,a,b)               for(int i = a; i<=b ; i++)
#define rfo(i,a,b)              for(int i = a; i>=b ; i--)
#define vii                     vector<int>
#define pq                      priority_queue
#define uomii                   unordered_map<int,int,best_hash>
#define all(v)                  v.begin(),v.end()
#define mp                      make_pair
#define pb                      push_back
#define pob                     pop_back
#define ff                      first
#define ss                      second
#define pii                     pair<int,int>
#define mii                     map<int,int>
#define vvii                    vector<vii>
#define mod                     1000000007
#define MIN                     -1e9
#define pi                      3.1415926535897932384626433832795
#define cz(x)                   63 - __builtin_clzll(x)

using namespace std;
using namespace __gnu_pbds;
template<class T> using oset =  tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;


struct best_hash {
	static uint64_t splitmix64(uint64_t x) {

		x += 0x9e3779b97f4a7c15;
		x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
		x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
		return x ^ (x >> 31);
	}

	size_t operator()(uint64_t x) const {
		static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
		return splitmix64(x + FIXED_RANDOM);
	}
};

void INPUT()
{
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
}

/*
----------------------------------------------------------------
   -> Check For Overflows
   -> Out of Bounds
   -> Initialisations of global arrays and variables
----------------------------------------------------------------
*/

const int N = 2e5 + 5;
vii v[N];
int in[N];
int out[N];
int t;
vii a;

void dfs(int node, int par, int dunga) {
	in[node] = t++;
	a.pb(dunga);
	for (auto &i : v[node]) {
		if (i == par) continue;
		dfs(i, node, dunga + 1);
	}
	out[node] = t++;
	a.pb(dunga);
}

void solve()
{
	int n;
	cin >> n;
	fo(i, 1, n - 1) {
		int x;
		cin >> x;
		x--;
		v[x].pb(i);
		v[i].pb(x);
	}

	// make a euler tree first

	dfs(0, 0, 0);

	// now make occurance array of vectors

	vii pos[n];
	fo(i, 0, 2 * n - 1) pos[a[i]].pb(i);

	int q;
	cin >> q;
	while (q--) {
		int u, d;
		cin >> u >> d;
		--u;

		// find no of values in subtree of u with value d
		// or find no of values in range in[u] to out[u] with value d

		int l = in[u];
		int r = out[u];

		auto up = upper_bound(all(pos[d]), r) - pos[d].begin();
		auto down = lower_bound(all(pos[d]), l) - pos[d].begin();
		int cnt = (up - down) / 2;
		cout << cnt << endl;
	}

}

signed main() {

	FIO
	INPUT();

	int t;
	t = 1;
	//cin >> t;
	fo(i, 0, t - 1) {
		solve();
	}
	return 0;
}

//#include <tourist>
#include <bits/stdc++.h>
//#include <atcoder/all>
using namespace std;
//using namespace atcoder;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<ll, ll> p;
const int INF = 1e9;
const ll LINF = ll(1e18);
const int MOD = 1000000007;
const int dx[4] = {0, 1, 0, -1}, dy[4] = {-1, 0, 1, 0};
const int Dx[8] = {0, 1, 1, 1, 0, -1, -1, -1}, Dy[8] = {-1, -1, 0, 1, 1, 1, 0, -1};
#define yes cout << "Yes" << endl
#define YES cout << "YES" << endl
#define no cout << "No" << endl
#define NO cout << "NO" << endl
#define rep(i, n) for (int i = 0; i < n; i++)
#define ALL(v) v.begin(), v.end()
#define debug(v)          \
    cout << #v << ":";    \
    for (auto x : v)      \
    {                     \
        cout << x << ' '; \
    }                     \
    cout << endl;
template <class T>
bool chmax(T &a, const T &b)
{
    if (a < b)
    {
        a = b;
        return 1;
    }
    return 0;
}
template <class T>
bool chmin(T &a, const T &b)
{
    if (b < a)
    {
        a = b;
        return 1;
    }
    return 0;
}
//cout<<fixed<<setprecision(15);有効数字15桁
//-std=c++14
//g++ yarudake.cpp -std=c++17 -I .
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    string s;
    cin >> s;
    ll k;
    vector<int> bai;
    if(s.size()==1){
        int base = 1;
        while (base * 8 < 10)
        {
            bai.push_back(base * 8);
            base++;
        }
    }
    else if(s.size()==2){
        int base = 2;
        while (base * 8 < 100)
        {
            bai.push_back(base * 8);
            base++;
        }
    }
    else{
        int base = 13;
        while (base * 8 < 1000)
        {
            bai.push_back(base * 8);
            base++;
        }
    }
    //debug(bai);
    vector<map<int, int>> bai_m(bai.size());
    rep(i, bai.size())
    {
        while (bai[i])
        {
            bai_m[i][bai[i] % 10]++;
            bai[i] /= 10;
        }
    }
    map<int, int> sm;
    rep(i, s.size())
    {
        sm[s[i] - '0']++;
    }
    rep(i, bai_m.size())
    {
        bool check = false;
        rep(j, 10)
        {
            if (bai_m[i][j] > sm[j])
                check = true;
        }
        if (check == false)
        {
            yes;
            return 0;
        }
    }
    no;
}
