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.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Stack;
import java.util.StringTokenizer;
import java.util.TreeSet;

public class E {
    static int[] p;
    static ArrayList<Integer> adj[];
    static ArrayList<Integer> roots = new ArrayList<Integer>();
    static ArrayList<Integer> lvls[];
    static int n;

    public static void main(String[] args) throws IOException {
        InputReader in = new InputReader();
        n = in.nextInt();
        p = new int[n];
        adj = new ArrayList[n];
        lvls = new ArrayList[n];
        a = new int[n];
        map = new int[n];
        minId = new int[n];
        maxId = new int[n];
        dp = new int[n][20];
        lvl = new int[n];
        for (int i = 0; i < n; i++) {
            Arrays.fill(dp[i], -1);
            adj[i] = new ArrayList<Integer>();
            lvls[i] = new ArrayList<Integer>();

        }
        for (int i = 0; i < n; i++) {
            p[i] = in.nextInt() - 1;
            if (p[i] != -1) {
                adj[p[i]].add(i);
                dp[i][0] = p[i];
            } else {
                roots.add(i);
            }
        }
        for (int i = 0; i < roots.size(); i++)
            dfs(roots.get(i), 0);
        int x;
        for (int i = 0; i < n; i++) {
            x = map[i];
            for (int j = 0; j < 20; j++)
                if (dp[x][j] != -1)
                    dp[x][j + 1] = dp[dp[x][j]][j];
        }
        int q = in.nextInt();
        StringBuilder sb = new StringBuilder();
        while (q-- > 0) {
            int v = in.nextInt() - 1;
            int p = in.nextInt();
            int pp = p;
            for (int i = 19; i >= 0; i--) {
                if (v != -1 && p - (1 << i) >= 0) {
                    p -= (1 << i);
                    v = dp[v][i];
                }
            }
            if (v == -1)
                sb.append(0 + " ");
            else
                sb.append((query(minId[v], maxId[v], pp + lvl[v]) - 1) + " ");
        }
        System.out.println(sb);
    }

    public static int query(int L, int R, int val) {
        ArrayList<Integer> cur = lvls[val];
        int lo = 0;
        int hi = cur.size()-1;
        int mid;
        while(lo<hi){
            mid = (lo+hi+1)/2;
            if(cur.get(mid)<L)
                lo = mid;
            else
                hi = mid-1;
        }
        int F1 = lo;
        if(cur.get(F1)>=L)F1--;
        
        lo = 0;
        hi = cur.size()-1;
        while(lo<hi){
            mid = (lo+hi)/2;
            if(cur.get(mid)<=R)
                lo = mid+1;
            else
                hi= mid;
        }
        int F2 = lo;
        if(cur.get(F2)<=R)F2++;
        
        return F2-F1-1;
    }

    // public static int query(int L, int R, int val) {
    // int cnt = 0;
    // int i = L / sqrt;
    // int j = R / sqrt;
    // if (i == j) {
    // for (int x = L; x <= R; x++) {
    // if (a[x] == val)
    // cnt++;
    // }
    // return cnt;
    // }
    // i++;
    // j--;
    // for (int x = L; x < i * sqrt; x++)
    // if (a[x] == val)
    // cnt++;
    // for (int x = R; x >= (j + 1) * sqrt; x--)
    // if (a[x] == val)
    // cnt++;
    // while (i <= j)
    // cnt += get(i++, val);
    // return cnt;
    // }
    //
    // public static int get(int i, int v) {
    // int lo = 0;
    // int hi = sqrt - 1;
    // int mid;
    // while (lo < hi) {
    // mid = (lo + hi) / 2;
    // if (dec[i][mid] <= v)
    // lo = mid + 1;
    // else
    // hi = mid;
    // }
    // int F1 = lo;
    // if (dec[i][lo] == v)
    // F1++;
    // lo = 0;
    // hi = sqrt - 1;
    // while (lo < hi) {
    // mid = (lo + hi) / 2;
    // if (dec[i][mid] < v)
    // lo = mid + 1;
    // else
    // hi = mid;
    // }
    // int F2 = lo;
    // if (dec[i][lo] < v)
    // F2++;
    // return Math.max(F1 - F2, 0);
    // }

    static int counter = 0;
    static int[] a;
    static int top = 0;
    static int[] minId;
    static int[] map;
    static int[] maxId;
    static int[][] dp;
    static int[] lvl;

    public static void dfs(int cur, int level) {
        map[top] = cur;
        minId[cur] = top;
        lvl[cur] = level;
        lvls[level].add(top);
        a[top++] = level;
        int size = adj[cur].size();
        for (int i = 0; i < size; i++) {
            int nxt = adj[cur].get(i);
            dfs(nxt, level + 1);
        }
        maxId[cur] = top - 1;
    }

    static class InputReader {
        BufferedReader in;
        StringTokenizer st;
        String cur;

        public InputReader() throws IOException {
            in = new BufferedReader(new InputStreamReader(System.in));
            st = new StringTokenizer(cur = in.readLine());
        }

        public boolean hasNext() throws IOException {
            if (!st.hasMoreElements()) {
                cur = in.readLine();
                if (cur == null)
                    return false;
                st = new StringTokenizer(cur);
            }
            return true;
        }

        public String next() throws IOException {
            while (!st.hasMoreElements())
                st = new StringTokenizer(cur = in.readLine());
            return st.nextToken();
        }

        public int nextInt() throws NumberFormatException, IOException {
            return Integer.parseInt(next());
        }

        public long nextLong() throws NumberFormatException, IOException {
            return Long.parseLong(next());
        }
    }
}
// 10
// 2 3 0 3 4 3 6 3 4 6
// 1
// 9 2