Elders
View as PDFThe goblin kingdom is strictly hierarchical: goblin is the king, and every other
goblin has exactly one superior. Indra is still infiltrating their ranks, and as his fairy
godmother you need to answer questions about who sits above whom.
Each query asks: if Indra is goblin , who is the
-th superior above him? The
-st
superior is his direct boss, the
-nd superior is that boss's boss, and so on. If he does
not have
superiors above him, report that the climb fails.
Input
The first line contains two integers and
: the number of goblins and the number of
queries.
The next lines each contain two integers
and
, meaning that goblin
is the
superior of goblin
.
Finally, the next lines each contain two integers
and
, representing one query.
Output
For each query, output a single integer on its own line: the -th superior of goblin
,
or
-1 if no such superior exists.
Constraints
- The superiors form a tree rooted at goblin
Example 1
Input
5 4
0 1
0 2
1 3
1 4
3 1
3 2
4 3
0 1
Output
1
0
-1
-1
Explanation
- Goblin
's first superior is
.
- Goblin
's second superior is
(the king).
- Goblin
has only two superiors (
then
), so a climb of
fails.
- Goblin
has no superiors at all.
Comments