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.OutputStream;
import java.util.Set;
import java.util.HashMap;
import java.util.InputMismatchException;
import java.io.IOException;
import java.io.FileNotFoundException;
import java.io.File;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.io.Writer;
import java.io.UnsupportedEncodingException;
import java.io.InputStream;

/**
 * Built using CHelper plug-in Actual solution is at the top
 *
 * @author MaxHeap
 */
public class Main {

  public static void main(String[] args) {
    InputStream inputStream = System.in;
    OutputStream outputStream = System.out;
    InputReader in = new InputReader(inputStream);
    OutputWriter out = new OutputWriter(outputStream);
    EBloodCousinsReturn solver = new EBloodCousinsReturn();
    solver.solve(1, in, out);
    out.close();
  }

  static class EBloodCousinsReturn {

    int n;
    BiMap<String, Integer> people;
    List<Integer>[] g;
    Map<Integer, Set<String>>[] count;
    List<IntPair>[] queries;
    int[] size;
    int[] ans;

    void dfs(int u, int p) {
      size[u] = 1;
      for (int v : g[u]) {
        if (v != p) {
          dfs(v, u);
          size[u] += size[v];
        }
      }
    }

    void compute(int u, int p, int depth) {
      int big = 0;
      for (int v : g[u]) {
        if (v != p) {
          compute(v, u, depth + 1);
          if (size[big] < size[v]) {
            big = v;
          }
        }
      }

      if (big > 0) {
        count[u] = count[big];
      }
      Set<String> set = count[u].get(depth);

      if (set == null) {
        set = new HashSet<>();
      }
      set.add(people.getByValue(u));
      count[u].put(depth, set);
      for (int v : g[u]) {
        if (v != p && v != big) {
          for (Entry<Integer, Set<String>> occ : count[v].entrySet()) {
            Set<String> strings = count[u].get(occ.getKey());
            if (strings == null) {
              count[u].put(occ.getKey(), new HashSet<>());
            }
            count[u].get(occ.getKey()).addAll(occ.getValue());
          }
        }
      }

      for (IntPair query : queries[u]) {
        int newDepth = depth + query.first;
        Set<String> val = count[u].get(newDepth);
        if (val != null) {
          ans[query.second] = val.size();
        }
      }
    }

    public void solve(int testNumber, InputReader in, OutputWriter out) {
      n = in.nextInt();
      g = GraphUtils.generateGraph(n + 1);
      people = new BiMap<>();
      List<Integer> roots = new ArrayList<>();
      for (int i = 0; i < n; ++i) {
        String name = in.next();
        people.put(name, i + 1);
        int parent = in.nextInt();
        if (parent != 0) {
          g[i + 1].add(parent);
          g[parent].add(i + 1);
        } else {
          roots.add(i + 1);
        }
      }
      size = new int[n + 1];
      count = new HashMap[n + 1];
      queries = new ArrayList[n + 1];
      for (int i = 0; i <= n; ++i) {
        count[i] = new HashMap<>();
        queries[i] = new ArrayList<>();
      }
      int q = in.nextInt();
      for (int i = 0; i < q; ++i) {
        int u = in.nextInt();
        int lev = in.nextInt();
        queries[u].add(Factories.makeIntPair(lev, i));
      }

      ans = new int[q + 1];
      for (int root : roots) {
        dfs(root, 0);
        compute(root, 0, 1);
      }
      for (int i = 0; i < q; ++i) {
        out.println(ans[i]);
      }
    }

  }

  static interface FastIO {

  }

  static class GraphUtils {

    private GraphUtils() {
    }

    public static <T> List<T>[] generateGraph(int n) {
      List<T>[] graph = new ArrayList[n];
      for (int i = 0; i < n; i++) {
        graph[i] = new ArrayList<>();
      }
      return graph;
    }

  }

  static class BiMap<K, V> {

    private Map<K, V> toValue;
    private Map<V, K> toKey;

    public BiMap() {
      toValue = new HashMap<>();
      toKey = new HashMap<>();
    }

    public void put(K key, V value) {
      toValue.put(key, value);
      toKey.put(value, key);
    }

    public K getByValue(V value) {
      return toKey.get(value);
    }

    public String toString() {
      return "BiMap{" +
          "toValue=" + toValue +
          ", toKey=" + toKey +
          '}';
    }

  }

  static class InputReader implements FastIO {

    private InputStream stream;
    private static final int DEFAULT_BUFFER_SIZE = 1 << 16;
    private static final int EOF = -1;
    private byte[] buf = new byte[DEFAULT_BUFFER_SIZE];
    private int curChar;
    private int numChars;

    public InputReader(InputStream stream) {
      this.stream = stream;
    }

    public int read() {
      if (this.numChars == EOF) {
        throw new UnknownError();
      } else {
        if (this.curChar >= this.numChars) {
          this.curChar = 0;

          try {
            this.numChars = this.stream.read(this.buf);
          } catch (IOException ex) {
            throw new InputMismatchException();
          }

          if (this.numChars <= 0) {
            return EOF;
          }
        }

        return this.buf[this.curChar++];
      }
    }

    public int nextInt() {
      int c;
      for (c = this.read(); isSpaceChar(c); c = this.read()) {
      }

      byte sgn = 1;
      if (c == 45) {
        sgn = -1;
        c = this.read();
      }

      int res = 0;

      while (c >= 48 && c <= 57) {
        res *= 10;
        res += c - 48;
        c = this.read();
        if (isSpaceChar(c)) {
          return res * sgn;
        }
      }

      throw new InputMismatchException();
    }

    public String next() {
      int c;
      while (isSpaceChar(c = this.read())) {
      }

      StringBuilder result = new StringBuilder();
      result.appendCodePoint(c);

      while (!isSpaceChar(c = this.read())) {
        result.appendCodePoint(c);
      }

      return result.toString();
    }

    public static boolean isSpaceChar(int c) {
      return c == 32 || c == 10 || c == 13 || c == 9 || c == EOF;
    }

  }

  static final class Factories {

    private Factories() {
    }

    public static IntPair makeIntPair(int first, int second) {
      return new IntPair(first, second);
    }

  }

  static class OutputWriter extends PrintWriter {

    public OutputWriter(OutputStream os, boolean autoFlush) {
      super(os, autoFlush);
    }

    public OutputWriter(Writer out) {
      super(out);
    }

    public OutputWriter(Writer out, boolean autoFlush) {
      super(out, autoFlush);
    }

    public OutputWriter(String fileName) throws FileNotFoundException {
      super(fileName);
    }

    public OutputWriter(String fileName, String csn)
        throws FileNotFoundException, UnsupportedEncodingException {
      super(fileName, csn);
    }

    public OutputWriter(File file) throws FileNotFoundException {
      super(file);
    }

    public OutputWriter(File file, String csn)
        throws FileNotFoundException, UnsupportedEncodingException {
      super(file, csn);
    }

    public OutputWriter(OutputStream out) {
      super(out);
    }

    public void flush() {
      super.flush();
    }

    public void close() {
      super.close();
    }

  }

  static class IntPair implements Comparable<IntPair> {

    public int first;
    public int second;

    public IntPair() {
      first = second = 0;
    }

    public IntPair(int first, int second) {
      this.first = first;
      this.second = second;
    }

    public int compareTo(IntPair a) {
      if (first == a.first) {
        return Integer.compare(second, a.second);
      }
      return Integer.compare(first, a.first);
    }

    public String toString() {
      return "<" + first + ", " + second + ">";
    }

    public boolean equals(Object o) {
      if (this == o) {
        return true;
      }
      if (o == null || getClass() != o.getClass()) {
        return false;
      }

      IntPair a = (IntPair) o;

      if (first != a.first) {
        return false;
      }
      return second == a.second;
    }

    public int hashCode() {
      int result = first;
      result = 31 * result + second;
      return result;
    }

  }
}

