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.BufferedReader;
import java.util.Comparator;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Collection;
import java.util.List;
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.StringTokenizer;
import java.math.BigInteger;
import java.util.Collections;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 * @author AlexFetisov
 */
public class Main {
	public static void main(String[] args) {
		InputStream inputStream = System.in;
		OutputStream outputStream = System.out;
		InputReader in = new InputReader(inputStream);
		PrintWriter out = new PrintWriter(outputStream);
		TaskE_BloodyBros solver = new TaskE_BloodyBros();
		solver.solve(1, in, out);
		out.close();
	}
}

class TaskE_BloodyBros {

    Graph g;

    public void solve(int testNumber, InputReader in, PrintWriter out) {
        g = new Graph();
        int n = in.nextInt();
        g.initGraph(n+1, n);
        for (int i = 1; i <= n; ++i) {
            int x = in.nextInt();
            g.addEdge(x, i);
        }
        int[][] parents = Parent.getLevelParents(g, 0);
        int countQ = in.nextInt();
        final DFSOrder dfsOrder = new DFSOrder(g, 0, parents[0]);
        int[] vertexLevels = Parent.getVertexLevels(g, 0, parents[0]);
        int maxLevel = ArrayUtils.getMax(vertexLevels);
        List<Integer>[] verticesOnLevels = new List[maxLevel+1];
        for (int i = 0; i <= maxLevel; ++i) {
            verticesOnLevels[i] = new ArrayList<Integer>();
        }
        for (int i = 0; i < vertexLevels.length; ++i) {
            verticesOnLevels[vertexLevels[i]].add(i);
        }
        for (int i = 0; i <= maxLevel; ++i) {
            Collections.sort(verticesOnLevels[i], new Comparator<Integer>() {
                public int compare(Integer v, Integer u) {
                    return dfsOrder.start(v) - dfsOrder.start(u);
                }
            });
        }
        while (countQ-- > 0) {
            int v = in.nextInt();
            int p = in.nextInt();
            int x = Parent.getParent(v, p, parents);
            if (x == -1 || x == 0) {
                out.print(0 + " ");
                continue;
            } else {

                int startForParent = dfsOrder.start(x);
                int endForParent = dfsOrder.end(x);

                int left = 0, right = verticesOnLevels[vertexLevels[v]].size()-1;
                int begin = -1, end = -1;
                while (left <= right) {
                    int middle = (left + right) / 2;
                    int cv = verticesOnLevels[vertexLevels[v]].get(middle);
                    if (dfsOrder.start(cv) >= startForParent && dfsOrder.end(cv) <= endForParent) {
                        begin = middle;
                        end = middle;
                        right = middle - 1;
                    } else {
                        if (dfsOrder.start(cv) > endForParent) {
                            right = middle - 1;
                        } else {
                            left = middle + 1;
                        }
                    }
                }

                if (begin == -1) throw new AssertionError("!");
                left = 0;
                right = verticesOnLevels[vertexLevels[v]].size()-1;
                while (left <= right) {
                    int middle = (left + right) / 2;
                    int cv = verticesOnLevels[vertexLevels[v]].get(middle);
                    if (dfsOrder.start(cv) >= startForParent && dfsOrder.end(cv) <= endForParent) {
                        end = middle;
                        left = middle + 1;
                    } else {
                        if (dfsOrder.start(cv) > endForParent) {
                            right = middle - 1;
                        } else {
                            left = middle + 1;
                        }
                    }
                }

                out.print(end - begin);
                out.print(" ");
            }
        }
    }

}

class Graph {
    public int[] from;
    public int[] to;
    public int[] first;
    public int[] next;
    public int nVertex;
    public int nEdges;
    public int curEdge;

    public Graph() {}

    public void initGraph(int n, int m) {
        curEdge = 0;
        nVertex = n;
        nEdges = m;
        from = new int[m * 2];
        to = new int[m * 2];
        first = new int[n];
        next = new int[m * 2];
        Arrays.fill(first, -1);
    }

    public void addEdge(int a, int b) {
        next[curEdge] = first[a];
        first[a] = curEdge;
        to[curEdge] = b;
        from[curEdge] = a;
        ++curEdge;

        next[curEdge] = first[b];
        first[b] = curEdge;
        to[curEdge] = a;
        from[curEdge] = b;
        ++curEdge;
    }

}

class InputReader {
    private BufferedReader reader;
    private StringTokenizer stt;

    public InputReader(InputStream stream) {
        reader = new BufferedReader(new InputStreamReader(stream));
    }

    public String nextLine() {
        try {
            return reader.readLine();
        } catch (IOException e) {
            return null;
        }
    }

    public String nextString() {
        while (stt == null || !stt.hasMoreTokens()) {
            stt = new StringTokenizer(nextLine());
        }
        return stt.nextToken();
    }

    public int nextInt() {
        return Integer.parseInt(nextString());
    }

}

class Parent {
    public static int[] run(Graph tree, int root) {
        int[] parent = new int[tree.nVertex];
        int[] queue = new int[tree.nVertex];
        int front = 0, back = 0;
        queue[back++] = root;
        parent[root] = -1;
        while (front < back) {
            int v = queue[front++];
            for (int i = tree.first[v]; i != -1; i = tree.next[i]) {
                if (parent[v] != tree.to[i]) {
                    queue[back++] = tree.to[i];
                    parent[tree.to[i]] = v;
                }
            }
        }
        return parent;
    }

    public static int getParent(int v, int dst, int[][] parents) {
        for (int i = 0; i < parents.length; ++i) {
            if (BitUtils.checkBit(dst, i)) {
                if (parents[i][v] == -1) return -1;
                v = parents[i][v];
            }
        }
        return v;
    }

    public static int[][] getLevelParents(Graph tree, int root) {
        int maxLevel = 1;
        while ((1 << maxLevel) <= tree.nVertex) {
            ++maxLevel;
        }
        int[][] parent = new int[maxLevel][tree.nVertex];
        ArrayUtils.fill(parent, -1);
        parent[0] = Parent.run(tree, root);

        for (int level = 1; level < maxLevel; ++level) {
            for (int i = 0; i < tree.nVertex; ++i) {
                if (parent[level-1][i] != -1) {
                    int u = parent[level-1][i];
                    if (parent[level-1][u] != -1) {
                        int v = parent[level-1][u];
                        parent[level][i] = v;
                    }
                }
            }
        }
        return parent;
    }

    public static int[] getVertexLevels(Graph tree, int root, int[] parent) {
        int[] queue = new int[tree.nVertex];
        int front = 0, back = 0;
        queue[back++] = root;
        parent[root] = -1;
        int[] level = new int[tree.nVertex];
        level[root] = 0;
        while (front < back) {
            int v = queue[front++];
            for (int i = tree.first[v]; i != -1; i = tree.next[i]) {
                if (parent[v] != tree.to[i]) {
                    queue[back++] = tree.to[i];
                    level[tree.to[i]] = level[v] + 1;
                }
            }
        }
        return level;
    }
}

class DFSOrder {
    int[] start;
    int[] end;

    public DFSOrder(Graph tree, int root, int[] p) {
        start = new int[tree.nVertex];
        end = new int[tree.nVertex];
        buildDfsOrder(tree, root, p);
    }

    public int start(int v) {
        return start[v];
    }

    public int end(int v) {
        return end[v];
    }

    private void buildDfsOrder(Graph tree, int root, int[] p) {
        int[] stack = new int[2 * tree.nVertex];
        int top = 0;
        stack[top++] = (root + 1);
        int time = 0;
        while (top > 0) {
            --top;
            int v = stack[top];
            if (v < 0) {
                v = (-v) - 1;
                end[v] = time;
                ++time;
            } else {
                --v;
                start[v] = time++;
                stack[top++] = -(v+1);
                for (int i = tree.first[v]; i != -1; i = tree.next[i]) {
                    if (p[v] != tree.to[i]) {
                        stack[top++] = (tree.to[i] + 1);
                    }
                }
            }
        }
    }
}

class ArrayUtils {

    public static void fill(int[][] f, int value) {
        for (int i = 0; i < f.length; ++i) {
            Arrays.fill(f[i], value);
        }
    }

    public static int getMax(int[] a) {
        int result = a[0];
        for (int x : a) result = Math.max(result, x);
        return result;
    }

}

class BitUtils {
    public static boolean checkBit(int mask, int bit) {
        return (mask & (1 << bit)) > 0;
    }

}