#include <bits/extc++.h>
#define endl '\n'
typedef long long ll;
#define int ll
using namespace std;
using namespace __gnu_cxx;
using namespace __gnu_pbds;
struct ly {
int to, nxt;
};
void Main() {
int n;
cin >> n;
vector<int> he(n + 1), fa(n + 1), dep(n + 1), l(n + 1), r(n + 1);
vector<vector<int>> dep_mp(n + 1);
vector<ly> edge(1);
auto add = [&](const int &u, const int &v) {
edge.emplace_back(v, he[u]);
he[u] = edge.size() - 1;
};
fa[1] = 1;
for (int i = 2; i <= n; ++i) {
cin >> fa[i];
add(fa[i], i);
}
int t = 0;
function<void(int)> dfs = [&](const int &u) {
l[u] = ++t;
dep_mp[dep[u]].push_back(l[u]);
for (int i = he[u]; i; i = edge[i].nxt) {
const int &v = edge[i].to;
fa[v] = u;
dep[v] = dep[u] + 1;
dfs(v);
}
r[u] = t;
};
dfs(1);
int q;
cin >> q;
while (q--) {
int d, x;
cin >> d >> x;
auto u = ranges::lower_bound(dep_mp[d], l[x]);
auto v = ranges::upper_bound(dep_mp[d], r[x]);
cout << v - u << endl;
}
cout << "Debug:dep_mp" << endl;
for (const auto &_: dep_mp) {
for (int i = 0; i < _.size(); ++i) {
cout << _[i] << " \n"[i == _.size() - 1];
}
}
}
// #define __CP_MULTI_TEST_CASES
signed main() {
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
int t = 1;
#ifdef __CP_MULTI_TEST_CASES
cin >> t;
#endif
while (t--) {
Main();
}
return cout << flush, fflush(stdout), 0;
}