#include <bits/stdc++.h>
//#include <atcoder/all>
//using namespace atcoder;
//#include <boost/multiprecision/cpp_int.hpp>
//#include <boost/multiprecision/cpp_dec_float.hpp>
//using namespace boost::multiprecision;
using namespace std;

#pragma region Macros
using ll = long long;
#define int ll
using pii = pair<int, int>;
using tiii = tuple<int, int, int>;
template<class T = int> using V = vector<T>;
template<class T = int> using VV = V<V<T>>;
#define IOS\
    ios::sync_with_stdio(false);\
    cin.tie(0);\
    cout.tie(0);
#define FOR(i,l,r) for(int i=(l);i<int(r);++i)
#define REP(i,n) FOR(i,0,n)
#define REPS(i,n) FOR(i,1,n+1)
#define RFOR(i,l,r) for(int i=(l);i>=int(r);--i)
#define RREP(i,n) RFOR(i,n-1,0)
#define RREPS(i,n) RFOR(i,n,1)
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define eb emplace_back
#define all(x) (x).begin(),(x).end()
#define SORT(name) sort(name.begin(), name.end())
#define RSORT(name)\
    SORT(name);\
    reverse(all(name));
#define ZERO(p) memset(p, 0, sizeof(p))
#define MINUS(p) memset(p, -1, sizeof(p))
inline void Yes(bool b = true) {cout << (b ? "Yes" : "No") << '\n';}
inline void YES(bool b = true) {cout << (b ? "YES" : "NO") << '\n';}
template <class T> inline void print(T x){ cout << x << '\n';}
template<typename T1,typename T2> inline void chmin(T1 &a, T2 b){ if(a > b) a = b; }
template<typename T1,typename T2> inline void chmax(T1 &a, T2 b){ if(a < b) a = b; }

const ll LLINF = (1LL<<60);
const int INF = (1LL<<30);
const double DINF = std::numeric_limits<double>::infinity();
#pragma endregion

const int MOD = 1000000007;

#if 1
#  define DBG(fmt, ...) printf(fmt, ##__VA_ARGS__)
#else
#  define DBG(fmt, ...)
#endif

const int MAX_N = 100010;

signed main() {
    IOS;
    string S;
    cin >> S;
    REP(i, int(S.length())) {
        if(i % 2) {
            if(S[i] >= 'a' && S[i] <= 'z') {
                print("No");
                return 0;
            }
        }
        else {
            if(S[i] >= 'A' && S[i] <= 'Z') {
                print("No");
                return 0;
            }
        }
    }
    print("Yes");

    return 0;
}

#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define REP(i,m,n) for (int i = (m); i < (n); ++i)
#define rrep(i,n) for (int i = (n)-1; i >= 0; --i)
#define RREP(i,m,n) for (int i = (n)-1; i >= (m); ++i)
#define all(a) (a).begin(),(a).end()
#define rall(a) (a).rbegin(),(a).rend()
using namespace std;
using ll = long long int;
using vec = vector<int>;
using P = pair<int,int>;
const int INF = 1e9+7;

void pv(vec v) {
  for (int e : v) {
    cout << e << " ";
  }
  cout << endl;
}

void dfs(vector<vec> &dis, vec &is, vec &os, vector<vec> &tree, int v, int d, int &now) {
  is[v] = now;
  dis[d].push_back(now);
  now++;
  for (int w : tree[v]) {
    dfs(dis, is, os, tree, w, d+1, now);
  }
  os[v] = now;
  now++;
} 

int main() {
  int n; cin >> n;
  vector<vec> tree(n);
  vector<vec> dis(n);
  vec is(n), os(n);
  rep(i,n-1) {
    int p; cin >> p; p--;
    tree[p].push_back(i+1);
  }

  int now = 0;
  dfs(dis, is, os, tree, 0, 0, now);

  for (vec &di : dis) {
    sort(all(di));
  }

  int q; cin >> q;
  rep(i, q) {
    int u, d; cin >> u >> d; u--;
    vec &di = dis[d];
    cout << lower_bound(all(di), os[u]) - lower_bound(all(di), is[u]) << endl;
  }
}
