#P17890. [ICPC 2026 Nanchang I] Tourist
[ICPC 2026 Nanchang I] Tourist
Problem Description
The Construction Kingdom is a famous tourist attraction, attracting thousands of tourists every year. The kingdom consists of towns and directed roads. Each road has a length of . It is guaranteed that among the initial roads, there are no duplicate roads and no road from a town to itself.
Later, due to the continuous prosperity of tourism, the Construction Kingdom decided to expand the roads. Starting from day , over the next days, a new directed road from to will be built each day. Due to improper planning, it may happen that an already existing road is built, or a road from a town to itself is built.
Fengmi is a free-spirited tourist. She has travel wishes and a desired distance . Each wish has a start and an end ( and may be the same). She wants to know, for each wish, the earliest day on which she can start from town , and under the condition that she is allowed to pass through towns and roads multiple times, exactly travel a distance of to reach town , or report that it is impossible. Please help her solve this problem.
Input Format
The first line contains two integers and (, ) --- the number of towns and the number of initial roads.
Each of the next lines contains two integers and (, ), indicating a directed road from to .
The next line contains an integer () --- the number of roads to be built in the following days.
Each of the next lines contains two integers and (), meaning that on day , a new directed road from to is built.
The next line contains two integers and (, ) --- the number of wishes and the desired distance.
Each of the next lines contains two integers and () --- the start and end of the -th wish.
Output Format
For each wish, output a single integer --- the earliest day that the wish can be fulfilled (i.e., after the road built on that day, there exists a walk of length exactly from to ). If it is never possible, output .
6 3
1 3
2 3
3 1
4
4 5
5 5
2 5
5 6
5 8
5 1
5 5
1 1
4 5
4 6
-1
2
0
2
4
Hint
In the sample, the desired distance is .
- For the first wish, it can be proved that it is impossible.
- For the second wish, after the roads and are built, one can walk along the town sequence to reach the destination.
- For the third wish, initially one can walk along the town sequence to the destination.
- For the fourth wish, after the roads , are built, one can walk along the town sequence to the destination.
- For the fifth wish, after the roads , , , are built, one can walk along the town sequence to the destination.