- Time limit: 1.00 s
- Memory limit: 512 MB
You are playing a game consisting of planets. Each planet has a teleporter to another planet (or the planet itself).
Your task is to process queries of the form: when you begin on planet and travel through teleporters, which planet will you reach?
Input
The first input line has two integers and : the number of planets and queries. The planets are numbered .
The second line has integers : for each planet, the destination of the teleporter. It is possible that .
Finally, there are lines describing the queries. Each line has two integers and : you start on planet and travel through teleporters.
Output
Print the answer to each query.
Constraints
Example
Input:
4 3 2 1 1 4 1 2 3 4 4 1
Output:
1 2 4