Under construction

Still being built. Things may change

The Hungry Muto

Time limit1.5sMemory limit512 MB

Summary
Given an evolution map on n ingredients, answer Q queries: count starting ingredients x where one evolution step gives the same result as c evolution steps.
Level

Medium5 of 10

Topics
Graph, Number theory
Solved
No attempts yet

Problem

Muto often gathers ingredients and cooks meals to eat. Today he was very hungry as usual. Nearby, he saw nn different Pokemon ingredients numbered from 11 to nn. Evolving an ingredient once turns it into another Pokemon ingredient. Bored while cooking, Muto asked you the following question QQ times.

Pidori and Pimi start a game with ingredient xx. Pidori evolves ingredient xx once, and Pimi evolves ingredient xx cc times. Count the number of values of xx for which Pidori's result equals Pimi's result.

Solve the QQ questions.

Input

The first line contains the number of ingredients nn. The second line contains nn integers separated by spaces. The ii-th integer xix_{i} (1≤xi≤n1 \le x_{i} \le n) means that when ingredient ii evolves once, it becomes ingredient xix_{i}. The ingredient before and after evolution may be the same.

The next line contains the number of questions QQ. The next QQ lines each contain a value qq, which means that cc equals qq in that question.

Output

Print the answer to each of Muto's QQ questions on its own line.

Constraints

1≤n≤500 0001 \le n \le 500\,000

1≤Q≤500 0001 \le Q \le 500\,000

1≤q≤2 000 0001 \le q \le 2\,000\,000

Examples2

  1. Example 1

    Input
    3
    1 3 2
    2
    1
    2
    
    Expected output
    3
    1
    
  2. Example 2

    Input
    2
    1 2
    2
    3
    4
    
    Expected output
    2
    2