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

const int MAXN = 2e5 + 5;

int st[MAXN], ed[MAXN], idx;
vector<int> e[MAXN], h[MAXN];
void dfs(int u, int fa, int depth) {
    st[u] = idx++;
    h[depth].push_back(st[u]);
    for (int v: e[u]) {
        if (v == fa) continue;
        dfs(v, u, depth + 1);
    }
    ed[u] = idx;
}
signed main(int argc, char *argv[]) {
	int n;
    cin >> n;
    for (int i = 2; i <= n; ++i) {
        int fa;
        cin >> fa;
        e[fa].push_back(i);
    }
    dfs(1, -1, 0);
    int q;
    cin >> q;
    while (q--) {
        int u, d;
        cin >> u >> d;
        #define ALL(vec) vec.begin(), vec.end()
        cout << lower_bound(ALL(h[d]), ed[u]) - lower_bound(ALL(h[d]), st[u]) << '\n';
    }
	system("pause");
}

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> 
#include <ext/pb_ds/tree_policy.hpp> 
using namespace std;
using namespace __gnu_pbds; 
 
#define int long long int
#define double long double
#define pb push_back
#define fi first
#define se second
#define vi vector<int>
#define pii pair<int,int>
#define vpii vector<pii>
#define pqi priority_queue<int>
#define test int tcase; cin>>tcase; for(int tc = 1; tc <= tcase; tc++)
#define inp(a,n,f) vector<int> a(n+f);for(int hh=f;hh<n+f;hh++)cin>>a[hh];
#define printdecimal(k) cout<<fixed<<setprecision(k);
#define mem(a,k) memset(a,k,sizeof(a))
#define ub upper_bound
#define lb lower_bound
#define all(v) v.begin(),v.end()
#define mod (int)(1e9+7)
#define inf LLONG_MAX
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
int exp(int x,int y){int val=1;x=x%mod;while(y>0){if(y&1)val=(val*x)%mod;y=y>>1;x=(x*x)%mod;}return val;}
int modinv(int x){return exp(x,mod-2);}
int add(int a,int b){a%=mod,b%=mod;a=((a+b)%mod+mod)%mod;return a;}
int sub(int a,int b){a%=mod,b%=mod;a=((a-b)%mod+mod)%mod;return a;}
int mul(int a,int b){a%=mod,b%=mod;a=((a*b)%mod+mod)%mod;return a;}


signed main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);

	int n,ans=0;
	cin>>n;
	inp(a,n,0);
	map<int, int> m;
	for(int i:a)
		ans+=m[i%200],m[i%200]++;
	cout<<ans;
	return 0;
}		