#include <bits/stdc++.h>

#define pb push_back
#define mp make_pair
#define X first
#define Y second
using namespace std;

typedef long long int ll;
typedef vector <int> vi;
typedef pair <int,int> pii;
const int Size=2e6+10;
const int INF=0x3f3f3f3f;

inline int read()
{
    int x=0, f=1; char ch=getchar();
    while(ch<'0' || ch>'9') {if(ch=='-') f=-1; ch=getchar();}
    while(ch>='0' && ch<='9') {x=x*10+ch-'0'; ch=getchar();}
    return x*f;
}

vi f[Size];
int main()
{
    int n=read(),m=read(),a;
    for (int i=0;i<=n;++i) f[i].pb(0);
    for (int i=1;i<=n;++i) {a=read(); f[a].pb(i);}
    for (int i=0;i<=n;++i) f[i].pb(n+1);

    int res;
    for (int i=0;i<=n;++i)
    {
        int siz=f[i].size();
        bool flag=false;
        for (int j=1;j<=siz-1;++j)
            if (f[i][j]-f[i][j-1]>=m+1) {flag=true; break;}
        if (flag) {res=i; break;}
    }
    printf("%d\n",res);
    return 0;
}

#include<bits/stdc++.h>
using namespace std;


vector<vector<int>>G, dG;
vector<int>in, out;
int cnt = 0, depth = 0;

void dfs(int n){
    in[n] = cnt++;
    dG[depth].push_back(in[n]);

    for(int g : G[n]){
        depth++;
        dfs(g);
        depth--;
    }

    out[n] = cnt++;
}



int main(){
    int n;
    cin >> n;

    in = out = vector <int>(n);
    G = dG = vector <vector <int>>(n+1, vector <int> (0));
    
    int p;
    for(int i=1; i<n; i++){
        cin >> p;
        p--;
        G[p].emplace_back(i);
        
    }

    dfs(0);

    int q, u, d;
    cin >> q;

    for(int i=0; i<q; i++){
        cin >> u >> d;
        u--;
        const auto& v = dG[d];
        cout << lower_bound (v.cbegin(), v.cend(), out[u]) - 
        lower_bound (v.cbegin(), v.cend(), in[u]) << endl;
    }

    return 0;

}

