
/* CODER::==>ARYAN KANSAGARA
  CF HANDLE:=>Good_Devil
-----------------------------------------------*/


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

#define int long long int
#define endl "\n"

void solve()
{
	string S; cin >> S;
	int ans = 0;
	for (int i = 0; i <= 9999; i++) {
		vector<bool> flag(10);
		int X = i;
		for (int j = 0; j < 4; j++) {
			flag[X % 10] = true;
			X /= 10;
		}
		bool flag2 = true;
		for (int j = 0; j < 10; j++) {
			if (S[j] == 'o' && !flag[j]) flag2 = false;
			if (S[j] == 'x' && flag[j]) flag2 = false;
		}
		ans += flag2;
	}
	cout << ans;
}

int32_t main() {
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
	freopen("error.txt", "w", stderr);
#endif
	ios_base::sync_with_stdio(false);
	cin.tie(NULL); cout.tie(NULL);
	solve();

	return 0;
}




#include "bits/stdc++.h"
using namespace std;
#define FastIO ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define int long long
#define mod 1000000007

const int N = 2e5 + 5;
vector<int> tree[N], depth(N);
vector<int> time_in(N), time_out(N);
vector<int> orders[N];
int timer = 0, n;

void dfs(int s) {
	time_in[s] = timer++;
	orders[depth[s]].push_back(time_in[s]);
	for (int v : tree[s]) {
		depth[v] = 1 + depth[s];
		dfs(v);
	}
	time_out[s] = timer++;
}

void test_case() {
	cin >> n;
	int j;
	for (int i = 2; i <= n; i++) {
		cin >> j;
		tree[j].push_back(i);
	}
	dfs(1);
	int q;
	cin >> q;
	while (q--) {
		int u, d;
		cin >> u >> d;
		auto &arr = orders[d];
		auto y = lower_bound(arr.begin(), arr.end(), time_out[u]);
		auto x = lower_bound(arr.begin(), arr.end(), time_in[u]);
		cout << y - x << "\n";
	}
}

signed main() {
	FastIO;
	test_case();
	return 0;
}
/*
         ▄              ▄
        ▌▒█           ▄▀▒▌
        ▌▒▒█        ▄▀▒▒▒▐
       ▐▄█▒▒▀▀▀▀▄▄▄▀▒▒▒▒▒▐
     ▄▄▀▒▒▒▒▒▒▒▒▒▒▒█▒▒▄█▒▐
   ▄▀▒▒▒░░░▒▒▒░░░▒▒▒▀██▀▒▌
  ▐▒▒▒▄▄▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▀▄▒▌
  ▌░░▌█▀▒▒▒▒▒▄▀█▄▒▒▒▒▒▒▒█▒▐
 ▐░░░▒▒▒▒▒▒▒▒▌██▀▒▒░░░▒▒▒▀▄▌
 ▌░▒▒▒▒▒▒▒▒▒▒▒▒▒▒░░░░░░▒▒▒▒▌
▌▒▒▒▄██▄▒▒▒▒▒▒▒▒░░░░░░░░▒▒▒▐
▐▒▒▐▄█▄█▌▒▒▒▒▒▒▒▒▒▒░▒░▒░▒▒▒▒▌
▐▒▒▐▀▐▀▒▒▒▒▒▒▒▒▒▒▒▒▒░▒░▒░▒▒▐
 ▌▒▒▀▄▄▄▄▄▄▒▒▒▒▒▒▒▒░▒░▒░▒▒▒▌
 ▐▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒░▒░▒▒▄▒▒▐
  ▀▄▒▒▒▒▒▒▒▒▒▒▒▒▒░▒░▒▄▒▒▒▒▌
    ▀▄▒▒▒▒▒▒▒▒▒▒▄▄▄▀▒▒▒▒▄▀
      ▀▄▄▄▄▄▄▀▀▀▒▒▒▒▒▄▄▀
         ▀▀▀▀▀▀▀▀▀▀▀▀
*/