	
 #include <bits/stdc++.h>
 
 #define FIXED_FLOAT(x)  std::fixed <<std::setprecision(20) << (x)
 #define all(v) (v).begin(), (v).end()
 using namespace  std;
 #define forn(i,n) for (int i = 0; i < (n); ++i)
 #define rforn(i, n) for(int i = (n) - 1;i >= 0;--i)
 using ll = long long;
 int mod = (ll)1e9 + 7;
 #define PI acos(-1)
 typedef pair<int, int> pairs;
 
 const int INF = 1e9 + 1;
 const int N = 2e5 + 100;
 const double eps = 1e-7;
 
 template <class T> using V = vector<T>;  // from yosupo 
 template <class T> using VV = V<V<T>>;  // from yosupo
 
 template <typename XPAX>
 void ckma(XPAX &x, XPAX y) {
     x = (x < y ? y : x);
 }
 template <typename XPAX>
 void ckmi(XPAX &x, XPAX y) {
     x = (x > y ? y : x);
 }
 set<int> s;
 void __print(int x) {cerr << x;}
 void __print(long x) {cerr << x;}
 void __print(long long x) {cerr << x;}
 void __print(unsigned x) {cerr << x;}
 void __print(unsigned long x) {cerr << x;}
 void __print(unsigned long long x) {cerr << x;}
 void __print(float x) {cerr << x;}
 void __print(double x) {cerr << x;}
 void __print(long double x) {cerr << x;}
 void __print(char x) {cerr << '\'' << x << '\'';}
 void __print(const char *x) {cerr << '\"' << x << '\"';}
 void __print(const string &x) {cerr << '\"' << x << '\"';}
 void __print(bool x) {cerr << (x ? "true" : "false");}
 
 template<typename T, typename V>
 void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
 template<typename T>
 void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
 void _print() {cerr << "]\n";}
 template <typename T, typename... V>
 void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
 #define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
 const int maxRep = 1e7;
 void solve() {
 
 
 	int n;
 	cin >> n;
 	int iter = 0;
 	VV<int> g(n);
 	forn(i, n - 1) {
 		int x;
 		cin >> x;
 		--x;
 		g[x].push_back(i + 1);
 	}

 	V<int> tin(n), tout(n), ord(n);

 	int T = 0;
 	V<int> dep(n);
 	auto dfs = [&](auto &&self, int v, int de) -> void {
 		ord[T] = v;
 		tin[v] = T++;
 		dep[v] = de;
 		for(auto c : g[v]) {
 			self(self, c, de + 1);
 		}
 		tout[v] = T;
 	};
 	dfs(dfs, 0, 0);
 	VV<int> ps(n);
 	forn(i, n) {
 		int v = ord[i];
 		ps[dep[v]].push_back(i);
 	}

 	int q;
 	cin >> q;
 	while(q--) {
 		int u, d;
 		cin >> u >> d;
 		--u;
 		int L = tin[u];
 		int R = tout[u];
 		auto it1 = lower_bound(all(ps[d]), L);
 		auto it2 = lower_bound(all(ps[d]), R);
 		cout << (it2 - it1) << '\n';
 	}


 
 
 }
 void test_case() {
     int t;
     cin >> t;
     forn(p, t) {
 
         //cout << "Case #" << p + 1 << ": ";
         solve();
     }
 }
 int main() {
 
     ios::sync_with_stdio(false);
     cin.tie(nullptr);
     cout.tie(nullptr);
 
     solve();
 
 }
  

#include <bits/stdc++.h>
#define int long long int
using namespace std;
signed main(){
    ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    map<string  , pair<int , int> > mp;
    int n;
    cin>>n;
    for(int x = 0;x<n;x++){
        string s;
        cin>>s;
        if(s[0] == '!' ){
            string a = "";
            for(int i = 1;i<s.length();i++ ){
                a.push_back(s[i] );
            }
            mp[a].second++;
        }
        else{
            mp[s].first++;
        }
    }
    bool sas = true;string ans;
    map<string  , pair<int , int> > :: iterator it;
    for(it = mp.begin() ; it!= mp.end();it++){
        if((it->second.first)&& (it->second.second) ){
            ans = it->first;sas = false; ;break;
        }
    }
    if(sas ){
        cout<<"satisfiable";
    }
    else{
        cout<<ans;
    }
}
