- Time limit: 1.00 s
- Memory limit: 512 MB
A company has employees, who form a tree hierarchy where each employee has a boss, except for the general director.
Your task is to process queries of the form: who is the lowest common boss of employees and in the hierarchy?
Input
The first input line has two integers and : the number of employees and queries. The employees are numbered , and employee is the general director.
The next line has integers : for each employee their boss.
Finally, there are lines describing the queries. Each line has two integers and : who is the lowest common boss of employees and ?
Output
Print the answer for each query.
Constraints
Example
Input:
5 3 1 1 3 3 4 5 2 5 1 4
Output:
3 1 1