Polycarpus got hold of a family tree. The found tree describes the family relations of n people, numbered from 1 to n. Every person in this tree has at most one direct ancestor. Also, each person in the tree has a name, the names are not necessarily unique.

We call the man with a number a a 1-ancestor of the man with a number b, if the man with a number a is a direct ancestor of the man with a number b.

We call the man with a number a a k-ancestor (k > 1) of the man with a number b, if the man with a number b has a 1-ancestor, and the man with a number a is a (k - 1)-ancestor of the 1-ancestor of the man with a number b.

In the tree the family ties do not form cycles. In other words there isn't a person who is his own direct or indirect ancestor (that is, who is an x-ancestor of himself, for some x, x > 0).

We call a man with a number a the k-son of the man with a number b, if the man with a number b is a k-ancestor of the man with a number a.

Polycarpus is very much interested in how many sons and which sons each person has. He took a piece of paper and wrote m pairs of numbers vi, ki. Help him to learn for each pair vi, ki the number of distinct names among all names of the ki-sons of the man with number vi.

Input

The first line of the input contains a single integer n (1 ≤ n ≤ 105) — the number of people in the tree. Next n lines contain the description of people in the tree. The i-th line contains space-separated string si and integer ri (0 ≤ ri ≤ n), where si is the name of the man with a number i, and ri is either the number of the direct ancestor of the man with a number i or 0, if the man with a number i has no direct ancestor. 

The next line contains a single integer m (1 ≤ m ≤ 105) — the number of Polycarpus's records. Next m lines contain space-separated pairs of integers. The i-th line contains integers vi, ki (1 ≤ vi, ki ≤ n).

It is guaranteed that the family relationships do not form cycles. The names of all people are non-empty strings, consisting of no more than 20 lowercase English letters.

Output

Print m whitespace-separated integers — the answers to Polycarpus's records. Print the answers to the records in the order, in which the records occur in the input.

Examples

Input

6
pasha 0
gerald 1
gerald 1
valera 2
igor 3
olesya 1
5
1 1
1 2
1 3
3 1
6 1


Output

2
2
0
1
0


Input

6
valera 0
valera 1
valera 1
gerald 0
valera 4
kolya 4
7
1 1
1 2
2 1
2 2
4 1
5 1
6 1


Output

1
0
0
0
2
0
0
import static java.lang.System.arraycopy;
import static java.util.Arrays.fill;

import java.io.*;
import java.util.*;

public class E {

	@SuppressWarnings("unchecked")
	private static void solve() throws IOException {
		int n = nextInt();
		int[] p = new int[n];
		for (int i = 0; i < n; i++) {
			p[i] = nextInt() - 1;
		}
		int h = 1;
		while (1 << h < n) {
			h++;
		}
		int[][] up = new int[h][n];
		arraycopy(p, 0, up[0], 0, n);
		for (int it = 0; it < h - 1; it++) {
			int[] cur = up[it];
			int[] next = up[it + 1];
			for (int i = 0; i < n; i++) {
				if (cur[i] < 0) {
					next[i] = -1;
				} else {
					next[i] = cur[cur[i]];
				}
			}
		}
		int q = nextInt();
		int[] inputV = new int[q];
		int[] inputDist = new int[q];
		for (int i = 0; i < q; i++) {
			inputV[i] = nextInt() - 1;
			inputDist[i] = nextInt();
		}
		int[] queryV = new int[q];
		for (int i = 0; i < q; i++) {
			queryV[i] = go(up, inputV[i], inputDist[i] - 1);
		}

		fill(up, null);
		up = null;

		List<Integer>[] edges = new List[n];
		for (int i = 0; i < n; i++) {
			edges[i] = new ArrayList<Integer>();
		}
		for (int i = 0; i < n; i++) {
			if (p[i] >= 0) {
				edges[p[i]].add(i);
			}
		}
		List<Query>[] queries = new List[n];
		for (int i = 0; i < n; i++) {
			queries[i] = new ArrayList<Query>();
		}

		for (int i = 0; i < q; i++) {
			if (queryV[i] < 0 || p[queryV[i]] < 0) {
				continue;
			}
			Query q1 = new Query(i, 1, inputDist[i]);
			// Query q2 = new Query(i, -1, inputDist[i] - 1);
			Query q2 = new Query(i, -1, 0);
			queries[queryV[i]].add(q2);
			queries[p[queryV[i]]].add(q1);
		}

		int[] queryAnswer = new int[q];

		for (int i = 0; i < n; i++) {
			if (p[i] < 0) {
				dfs(edges, queries, queryAnswer, i);
			}
		}

		for (int i = 0; i < q; i++) {
			if (i > 0) {
				out.print(' ');
			}
			out.print(queryAnswer[i]);
		}
		out.println();
	}

	static SmartStructure dfs(List<Integer>[] edges, List<Query>[] queries, int[] queryAnswer, int u) {
		SmartStructure ret = new SmartStructure();
		for (int v : edges[u]) {
			SmartStructure cur = dfs(edges, queries, queryAnswer, v);
			ret = SmartStructure.union(ret, cur);
		}
		for (Query q : queries[u]) {
			queryAnswer[q.queryIndex] += q.sign * ret.getCount(q.distance);
		}
		ret.add++;
		return ret;
	}

	static class Query {
		int queryIndex;
		int sign;
		int distance;

		private Query(int queryIndex, int sign, int distance) {
			this.queryIndex = queryIndex;
			this.sign = sign;
			this.distance = distance;
		}
	}

	static class SmartStructure {
		Map<Integer, Integer> countForDistance;
		int add;

		SmartStructure() {
			countForDistance = new HashMap<Integer, Integer>();
			countForDistance.put(0, 1);
			add = 0;
		}

		SmartStructure addAll(SmartStructure other) {
			for (Map.Entry<Integer, Integer> entry : other.countForDistance.entrySet()) {
				int key = entry.getKey();
				int val = entry.getValue();
				add(countForDistance, key + other.add - add, val);
			}
			return this;
		}

		static SmartStructure union(SmartStructure a, SmartStructure b) {
			if (a.countForDistance.size() < b.countForDistance.size()) {
				return b.addAll(a);
			} else {
				return a.addAll(b);
			}
		}

		int getCount(int distance) {
			Integer ret = countForDistance.get(distance - add);
			if (ret == null) {
				ret = 0;
			}
			return ret;
		}
	}

	static void add(Map<Integer, Integer> map, Integer key, int add) {
		Integer have = map.get(key);
		if (have == null) {
			have = 0;
		}
		map.put(key, have + add);
	}

	static int go(int[][] up, int u, int go) {
		for (int h = up.length - 1; h >= 0 && u >= 0 && go > 0; --h) {
			if (((1 << h) & go) != 0) {
				go ^= 1 << h;
				u = up[h][u];
			}
		}
		if (go > 0) {
			return -1;
		}
		return u;
	}

	public static void main(String[] args) {
		try {
			br = new BufferedReader(new InputStreamReader(System.in));
			out = new PrintWriter(System.out);
			solve();
			out.close();
		} catch (Throwable e) {
			e.printStackTrace();
			System.exit(239);
		}
	}

	static BufferedReader br;
	static StringTokenizer st;
	static PrintWriter out;

	static String nextToken() throws IOException {
		while (st == null || !st.hasMoreTokens()) {
			String line = br.readLine();
			if (line == null) {
				return null;
			}
			st = new StringTokenizer(line);
		}
		return st.nextToken();
	}

	static int nextInt() throws IOException {
		return Integer.parseInt(nextToken());
	}

	static long nextLong() throws IOException {
		return Long.parseLong(nextToken());
	}

	static double nextDouble() throws IOException {
		return Double.parseDouble(nextToken());
	}
}