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 java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.FilterInputStream;
import java.io.BufferedInputStream;
import java.util.ArrayList;
import java.io.InputStream;

/**
 * @author khokharnikunj8
 */

public class Main {
    public static void main(String[] args) {
        new Thread(null, new Runnable() {
            public void run() {
                new Main().solve();
            }
        }, "1", 1 << 26).start();
    }

    void solve() {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        ScanReader in = new ScanReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        EBloodCousins solver = new EBloodCousins();
        solver.solve(1, in, out);
        out.close();
    }

    static class EBloodCousins {
        int[] parent;
        int[][] dp;
        ArrayList<Integer>[] heights;
        ArrayList<Integer>[] arrayLists;
        int[] height_node;
        int[] index;

        private void dfs(int node, int height) {
            height_node[node] = height;
            if (node != 0) heights[height].add(node);
            index[node] = heights[height].size() - 1;
            for (int i = 0; i < arrayLists[node].size(); i++) {
                dfs(arrayLists[node].get(i), height + 1);
            }
        }

        public void solve(int testNumber, ScanReader in, PrintWriter out) {
            int n = in.scanInt();
            parent = new int[n + 1];
            arrayLists = new ArrayList[n + 1];
            heights = new ArrayList[n + 1];
            height_node = new int[n + 1];
            index = new int[n + 1];
            for (int i = 0; i <= n; i++) arrayLists[i] = new ArrayList<>();
            for (int i = 0; i <= n; i++) heights[i] = new ArrayList<>();
            int max_k = (int) Math.ceil(Math.log(n) / Math.log(2)) + 1;
            dp = new int[n + 2][max_k + 1];
            for (int i = 1; i <= n; i++) {
                parent[i] = in.scanInt();
                arrayLists[parent[i]].add(i);
            }

            for (int i = 0; i <= n + 1; i++) {
                for (int j = 0; j <= max_k; j++) {
                    dp[i][j] = -1;
                }
            }
            for (int i = 0; i <= n; i++) dp[i][0] = parent[i];
            for (int j = 1; j <= max_k; j++)
                for (int i = 0; i <= n; i++)
                    dp[i][j] = dp[dp[i][j - 1]][j - 1];
            dfs(0, 0);
            int q = in.scanInt();
            while (q-- > 0) {
                int v = in.scanInt();
                int p = in.scanInt();
                int hh = height_node[v];
                int ii = index[v];
                for (int i = max_k; i >= 0; i--) {
                    if (p >= (1 << i)) {
                        v = dp[v][i];
                        p -= (1 << i);
                    }
                }
                if (v == 0) {
                    out.print(0 + " ");
                    continue;
                }

                int qq = findLCA(heights[hh].get(ii), v, max_k);
                if (qq != v) throw new RuntimeException();
                int low = 0;
                int high = ii;
                int index1 = -1;
                while (low <= high) {
                    int mid = (low + high) >> 1;
                    int q1 = findLCA(v, heights[hh].get(mid), max_k);
                    if (q1 == v) {
                        index1 = mid;
                        high = mid - 1;
                    } else {
                        low = mid + 1;
                    }
                }

                low = ii;
                high = heights[hh].size() - 1;
                int index2 = -1;
                while (low <= high) {
                    int mid = (low + high) >> 1;
                    int qq1 = findLCA(v, heights[hh].get(mid), max_k);
                    if (qq1 == v) {
                        index2 = mid;
                        low = mid + 1;
                    } else {
                        high = mid - 1;
                    }
                }
                out.print(index2 - index1 + " ");


            }
        }

        private int findLCA(int u, int v, int max_k) {
            if (height_node[u] < height_node[v]) u = u + v - (v = u);
            for (int i = max_k; i >= 0; i--) {
                if (height_node[u] - (1 << i) >= height_node[v]) {
                    u = dp[u][i];
                }
            }
            if (u == v) return u;
            for (int i = max_k; i >= 0; i--) {
                if (dp[u][i] != -1 && dp[u][i] != dp[v][i]) {
                    u = dp[u][i];
                    v = dp[v][i];
                }
            }
            return parent[u];
        }

    }

    static class ScanReader {
        private byte[] buf = new byte[4 * 1024];
        private int index;
        private BufferedInputStream in;
        private int total;

        public ScanReader(InputStream inputStream) {
            in = new BufferedInputStream(inputStream);
        }

        private int scan() {
            if (index >= total) {
                index = 0;
                try {
                    total = in.read(buf);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                if (total <= 0) return -1;
            }
            return buf[index++];
        }

        public int scanInt() {
            int integer = 0;
            int n = scan();
            while (isWhiteSpace(n)) n = scan();
            int neg = 1;
            if (n == '-') {
                neg = -1;
                n = scan();
            }
            while (!isWhiteSpace(n)) {
                if (n >= '0' && n <= '9') {
                    integer *= 10;
                    integer += n - '0';
                    n = scan();
                }
            }
            return neg * integer;
        }

        private boolean isWhiteSpace(int n) {
            if (n == ' ' || n == '\n' || n == '\r' || n == '\t' || n == -1) return true;
            else return false;
        }

    }
}

