Bandits in the Tree
There are cities, numbered from to . These cities are interconnected by roads, ensuring that any city can be reached from any other city. Each road has a specific length.
It is known that city was captured by the bandit clan numbered () at time . From the moment city is captured (i.e., starting at time inclusive), only representatives of clan are allowed to pass through it.
You need to answer queries of the following type:
u v b T
— Determine if a representative of clan can travel from city to city starting at time . If the journey is not possible, identify the first city on the path from to that cannot be traversed.
Input
The first line contains a single integer () — the number of cities.
Each of the next lines contains two integers and (, ), indicating a road between cities and with a length of . Indexing starts from .
The following line contains integers (), representing the clans that captured each city.
The next line contains integers (), representing the capture times for each city.
The next line contains a single integer () — the number of queries.
The last lines describe the queries. Each line contains four integers , , , (, ) — the starting and ending cities of the path, the traveler's clan number, and the start time of the journey, respectively.
Output
For each query, output a single integer on a separate line — the number of the first city on the path from to that cannot be traversed. If all cities on the path can be traversed, output «-1
».
Note the large volume of input and output data in this problem. It is recommended to use fast input and output methods, such as scanf/printf
instead of cin/cout
in C++
, and sys.stdin.readline
instead of input
in Python
. It is also recommended to use the PyPy
interpreter when solving problems in Python
.
Examples
Scoring
( points): ;
( points): ;
( points): ;
( points): ;
( points): ;
( points): ;
( points): ;
( points): no additional constraints.