In the event of technical difficulties with Szkopuł, please contact us via email at [email protected].
If you would like to talk about tasks, solutions or technical problems, please visit our Discord servers. They are moderated by the community, but members of the support team are also active there.
Byteotian Ministry of Infrastructure has decided to create a computer program that helps to find quickly the lengths of routes between arbitrary towns. It would be small wonder if the inhabitants of Byteotia always wanted to find the shortest route. However, it happens that they want to know the -th shortest route. Moreover, cycles in routes are possible, i.e. routes that have recurring towns.
If there are 4 routes between two towns and their lengths are 2, 4, 4 and 5, then the length of the shortest connection is 2, the second shortest is 4, the third is 4, and the fourth is 5.
Write a program which:
In the first line of the standard input there are two positive integers and , separated by a single space, , . They are the number of towns in Byteotia and the number of roads connecting the towns, respectively. The towns are numbered from to .
In each of successive lines there are three integers separated by single spaces: , and , , . Each triple describes one one-way road of length enabling to move from the town to . For each two towns there exist at most one road that enables to move in the given direction.
In the following line there is one integer , , denoting the number of queries. In the successive lines there are queries written, one per line. Each query has a form of three integers separated by single spaces: , and , . Such a query refers to the length of the -th shortest route from the town to the town .
Your program should write to the standard output the answers to the queries read, one answer per line. In the -th line the answer to the -th query should be written: one integer equal to the length of the route being sought or , when such a route does not exist.
For the input data:
5 5 1 2 3 2 3 2 3 2 1 1 3 10 1 4 1 8 1 3 1 1 3 2 1 3 3 1 4 2 2 5 1 2 2 1 2 2 2 1 1 2
the correct result is:
5 8 10 -1 -1 3 6 -1
Task author: Krzysztof Sikora.