Problem: 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

Solution (in JAVA):

Solution:
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.io.FilterInputStream;
import java.io.BufferedInputStream;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
    public static void main(String[] args) {
        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 {
        ArrayList<Integer>[] arrayList;
        ArrayList<Integer>[] atlevel;
        int[] index;
        int[] parent;
        int D;
        int[] depth;
        int[][] dp;

        int walk(int i, int k) {
            for (int d = 0; d <= D && i != -1; d++) {
                if (((1 << d) & k) > 0) {
                    i = dp[d][i];
                }
            }
            return i;
        }

        int lca(int i, int j) {
            if (depth[i] > depth[j])
                i = walk(i, depth[i] - depth[j]);
            if (depth[j] > depth[i])
                j = walk(j, depth[j] - depth[i]);
            if (i == j)
                return i;


            for (int d = D; d >= 0; d--) {
                if (dp[d][i] != dp[d][j]) {
                    i = dp[d][i];
                    j = dp[d][j];
                }
            }
            return dp[0][i];
        }

        public void solve(int testNumber, ScanReader in, PrintWriter out) {
            int n = in.scanInt();
            arrayList = new ArrayList[n + 1];
            atlevel = new ArrayList[n + 5];
            parent = new int[n + 1];
            index = new int[n + 1];
            for (int i = 0; i <= n; i++) {
                arrayList[i] = new ArrayList<>();
                atlevel[i] = new ArrayList();
            }

            for (int i = 1; i <= n; i++) {
                int p = in.scanInt();
                parent[i] = p;
                arrayList[p].add(i);
            }


            D = (int) Math.ceil((double) Math.log(n) / Math.log(2)) + 1;
            dp = new int[D + 1][n + 1];
            for (int[] dd : dp) Arrays.fill(dd, -1);

            ArrayDeque<Integer> q = new ArrayDeque<>();
            boolean visited[] = new boolean[n + 1];
            depth = new int[n + 1];

            q.add(0);
            visited[0] = true;

            while (q.size() > 0) {
                int i = q.poll();
                atlevel[depth[i]].add(i);
                index[i] = atlevel[depth[i]].size() - 1;

                for (int tt : arrayList[i]) {
                    if (!visited[tt]) {
                        visited[tt] = true;
                        q.add(tt);
                        dp[0][tt] = i;
                        depth[tt] = depth[i] + 1;
                    }
                }
            }
            for (int k = 1; k <= D; k++) {
                for (int i = 0; i <= n; i++) {
                    int mid = dp[k - 1][i];
                    if (mid != -1) {
                        dp[k][i] = dp[k - 1][mid];
                    }
                }
            }


            int qq = in.scanInt();
            while (qq-- > 0) {
                int v = in.scanInt();
                int p = in.scanInt();
                int u = walk(v, p);
                if (u == 0 || u == -1) {
                    out.print(0 + " ");
                    continue;
                }


                int lev = depth[v];
                int ll = 0;
                int hh = index[v];
                int lb = -1;

                while (ll <= hh) {
                    int mid = (ll + hh) / 2;
                    int a = u;
                    int b = atlevel[lev].get(mid);
                    int lca = lca(a, b);
                    if (lca == u) {
                        lb = mid;
                        hh = mid - 1;
                    } else {
                        ll = mid + 1;
                    }
                }


                ll = index[v];
                hh = atlevel[lev].size() - 1;
                int ub = -1;

                while (ll <= hh) {
                    int mid = (ll + hh) / 2;
                    int a = u;
                    int b = atlevel[lev].get(mid);
                    int lca = lca(a, b);
                    if (lca == u) {
                        ub = mid;
                        ll = mid + 1;
                    } else {
                        hh = mid - 1;
                    }
                }

                out.print(ub - lb + " ");
            }


        }

    }

    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 I = 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') {
                    I *= 10;
                    I += n - '0';
                    n = scan();
                }
            }
            return neg * I;
        }

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

    }
}