#P1266H. Red-Blue Graph
Red-Blue Graph
No submission language available for this problem.
Description
There is a directed graph on vertices numbered through where each vertex (except ) has two outgoing arcs, red and blue. At any point in time, exactly one of the arcs is active for each vertex. Initially, all blue arcs are active and there is a token located at vertex . In one second, the vertex with token first switches its active arcs — the inactive arc becomes active and vice versa. Then, the token is moved along the active arc. When the token reaches the vertex , it stops. It is guaranteed that is reachable via arcs from every vertex.
You are given queries. Each query contains a state of the graph — a pair of the following form:
- is the vertex where the token is currently located;
- is a string consisting of characters. The -th character corresponds to the color of the active edge leading from the -th vertex (the character is 'R' if red arc is active, otherwise the character is 'B').
For each query, determine whether the given state is reachable from the initial state and the first time this configuration appears. Note that the two operations (change active arc and traverse it) are atomic — a state is not considered reached if it appears after changing the active arc but before traversing it.
The first line contains a single integer () — the number of vertices.
lines follow, -th of contains two space separated integers and () representing a blue arc and red arc , respectively. It is guaranteed that vertex is reachable from every vertex.
The next line contains a single integer () — the number of queries.
Then lines with queries follow. The -th of these lines contains an integer () and a string of length consiting only of characters 'R' and 'B'. The -th of these characters is 'R' if the red arc going from is active and 'B' otherwise.
Output lines, each containing answer to a single query.
If the state in the -th query is unreachable, output the integer . Otherwise, output — the first time when the state appears (measured in seconds, starting from the initial state of the graph which appears in time ).
Input
The first line contains a single integer () — the number of vertices.
lines follow, -th of contains two space separated integers and () representing a blue arc and red arc , respectively. It is guaranteed that vertex is reachable from every vertex.
The next line contains a single integer () — the number of queries.
Then lines with queries follow. The -th of these lines contains an integer () and a string of length consiting only of characters 'R' and 'B'. The -th of these characters is 'R' if the red arc going from is active and 'B' otherwise.
Output
Output lines, each containing answer to a single query.
If the state in the -th query is unreachable, output the integer . Otherwise, output — the first time when the state appears (measured in seconds, starting from the initial state of the graph which appears in time ).
Samples
Note
The graph in the first example is depticed in the figure below.
The first queries denote the journey of the token. On the -th move the token would reach the vertex . The last two queries show states that are unreachable.