#include <algorithm>
#include <iomanip>
#include <array>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <deque>
#include <functional>
#include <iostream>
#include <iterator>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <sstream>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <numeric>
#include <vector>
using namespace std;
#if __has_include(<atcoder/all>)
#include <atcoder/all>
using namespace atcoder;
#endif
#define GET_MACRO(_1, _2, _3, NAME, ...) NAME
#define _rep(i, n) _rep2(i, 0, n)
#define _rep2(i, a, b) for(int i = (int)(a); i < (int)(b); i++)
#define rep(...) GET_MACRO(__VA_ARGS__, _rep2, _rep)(__VA_ARGS__)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
using i64 = long long;
template<class T, class U>
bool chmin(T& a, const U& b) { return (b < a) ? (a = b, true) : false; }
template<class T, class U>
bool chmax(T& a, const U& b) { return (b > a) ? (a = b, true) : false; }

template<typename T>istream& operator>>(istream&i,vector<T>&v){rep(j,v.size())i>>v[j];return i;}
template<typename T>string join(vector<T>&v){stringstream s;rep(i,v.size())s<<' '<<v[i];return s.str().substr(1);}
template<typename T>ostream& operator<<(ostream&o,vector<T>&v){if(v.size())o<<join(v);return o;}
template<typename T>string join(vector<vector<T>>&vv){string s="\n";rep(i,vv.size())s+=join(vv[i])+"\n";return s;}
template<typename T>ostream& operator<<(ostream&o,vector<vector<T>>&vv){if(vv.size())o<<join(vv);return o;}
template<class T> using pq = priority_queue<T, vector<T>, greater<T>>;

const int MAX_V = 200005;

vector<int> to[MAX_V];
int dep[MAX_V];
int in[MAX_V];
int out[MAX_V];
int k;
vector<int> ls[MAX_V]; // 深さごとの頂点リスト

void dfs(int v, int d)
{
    in[v] = k; // 来た時に何番だったか
    k++;
    dep[v] = d;
    for(auto i : to[v]) dfs(i, d + 1);
    out[v] = k; // 帰ってきたときに何番だったか
}

int calc(int d, int r)
{
    // ls[d]内で，rより大きい要素数を数える．
    return lower_bound(all(ls[d]), r) - ls[d].begin();
}

int main()
{
    int n;
    cin >> n;
    rep(i, n - 1)
    {
        int p;
        cin >> p;
        to[p - 1].push_back(i + 1);
    }
    dfs(0, 0);
    rep(i, n) ls[dep[i]].push_back(in[i]);
    rep(i, n) sort(all(ls[i]));

    int q;
    cin >> q;
    while(q--)
    {
        int u, d;
        cin >> u >> d;
        --u;
        int ans = calc(d, out[u]) - calc(d, in[u]); // ls[d] の中で [in[u], out[u]) である部分を数える
        cout << ans << endl;
    }

    return 0;
}
/**
 ____ ____ ____ ____ ____
||a |||t |||o |||d |||o ||
||__|||__|||__|||__|||__||
|/__\|/__\|/__\|/__\|/__\|

**/

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int N_MAX = 200000;

int n;

int a[N_MAX * 2 + 2];

int p[N_MAX * 2 + 2];

bool cmp (const int &x, const int &y)
{
    return a[x] < a[y];
}

bool color[N_MAX * 2 + 2];

set <int> sPos;

set <pair <int, int>> sPair;

bool answer[N_MAX * 2 + 2];

void del (int i)
{
    set <int> :: iterator it = sPos.find(i);
    set <int> :: iterator itl, itr;
    bool L = false, R = false;
    itl = it;
    if(itl != sPos.begin())
    {
        L = true;
        itl--;
        if(color[*itl] != color[i])
            sPair.erase(make_pair(*itl, i));
    }
    itr = it;
    itr++;
    if(itr != sPos.end())
    {
        R = true;
        if(color[i] != color[*itr])
            sPair.erase(make_pair(i, *itr));
    }
    if(L == true && R == true)
    {
        if(color[*itl] != color[*itr])
            sPair.insert(make_pair(*itl, *itr));
    }
    sPos.erase(i);
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> n;
    for(int i = 1; i <= n * 2; i++)
        cin >> a[i];
    for(int i = 1; i <= n * 2; i++)
        p[i] = i;
    sort(p + 1, p + n * 2 + 1, cmp);
    for(int i = 1; i <= n; i++)
        color[p[i]] = 1;
    for(int i = 1; i <= n * 2; i++)
        sPos.insert(i);
    for(int i = 1; i < n * 2; i++)
        if(color[i] != color[i + 1])
            sPair.insert(make_pair(i, i + 1));
    while(sPair.empty() == false)
    {
        pair <int, int> p = *sPair.begin();
        answer[p.first] = 0;
        answer[p.second] = 1;
        del(p.first);
        del(p.second);
    }
    assert(sPos.empty() == true);
    for(int i = 1; i <= n * 2; i++)
        if(answer[i] == 0)
            cout << "(";
        else
            cout << ")";
    cout << "\n";
    return 0;
}
