#include <bits/stdc++.h>

using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int N;
  cin >> N;
  vector<vector<int>> G(N);
  for (int i = 1; i < N; i++) {
    int p;
    cin >> p;
    p--;
    G[i].emplace_back(p);
    G[p].emplace_back(i);
  }
  vector<int> in(N), out(N);
  vector<vector<int>> dist(N);
  int timer = 0;
  function<void(int, int, int)> dfs = [&](int u, int p, int d) {
    in[u] = timer++;
    dist[d].emplace_back(in[u]);
    for (int v : G[u]) {
      if (v == p) continue;
      dfs(v, u, d + 1);
    }
    out[u] = timer++;
  };
  dfs(0, 0, 0);
  int Q;
  cin >> Q;
  for (int i = 0; i < Q; i++) {
    int u, d;
    cin >> u >> d;
    u--;
    const auto& v = dist[d];
    cout << lower_bound(v.cbegin(), v.cend(), out[u]) - lower_bound(v.cbegin(), v.cend(), in[u])
         << '\n';
  }
  return 0;
}

#include "bits/stdc++.h"
using namespace std;
using ll=int64_t;
using ld=long double;
using ull=unsigned long long;
template <class T>
using grid=vector<vector<T>>;
#define ALL(x) x.begin(),x.end()
#define rep(iter,from,to) for(ll iter=from;iter<to;++iter)

const ll MOD=1e9+7;
const ll INF=1e17;
//#######################################################################
vector<vector<ll>> input(ll N, ll width){
    string str;
    vector<vector<ll>> vec(N,vector<ll>(width));
    for(ll i=0;i<N;++i){
        cin>>str;
        reverse(ALL(str));
        for(ll j=0;j<width;++j){
            vec[i][j]=str.back();
            str.pop_back();
        }
    }
    return vec;
}
void op(vector<ll> vec){
    ll size=(ll)vec.size();
    for(ll i=0;i<size-1;++i) cout<<vec[i]<<" ";
    cout<<vec.back()<<endl;
}

void op(vector<vector<ll>> vec){
    ll height=(ll)vec.size();
    ll width=(ll)vec[0].size();
    for(ll i=0;i<height;++i) {
        for(ll j=0;j<width-1;++j) cout<<vec[i][j]<<" ";
        cout<<vec[i].back()<<endl;
    }
}
//########################################################################





int solve(){
    ll N,K;
    cin>>N>>K;
    ll sum=0;
    rep(i,1,N+1) rep(j,1,K+1) sum+=i*100+j;
    cout<<sum<<endl;  
    return 0;
}


int main(void){
    std::cin.tie(nullptr);
	std::ios_base::sync_with_stdio(false);
	std::cout << std::fixed << std::setprecision(15);
	solve();
}
