Connections

No attempts yetTime limit3sMemory limit128 MB

Problem

The Byteotian Ministry of Infrastructure wants a program that quickly reports the lengths of routes between towns. People usually look for the shortest route, but here they want the kk-th shortest route. Routes may contain cycles, so a town can be visited more than once.

For example, if there are four routes between two towns with lengths 22, 44, 44, and 55, then the shortest route has length 22, the second shortest has length 44, the third has length 44, and the fourth has length 55. Routes of equal length are counted separately.

A route always uses at least one road, so a route that starts and ends at the same town must follow a cycle.

Write a program that:

  • reads a description of the Byteotian road network and a list of route-length queries from standard input,
  • computes the answer to each query and writes it to standard output.

Input

The first line contains two integers nn and mm separated by a single space, where 1n1001 \le n \le 100 and 0mn2n0 \le m \le n^2 - n. They are the number of towns and the number of roads. Towns are numbered from 11 to nn.

Each of the next mm lines contains three integers aa, bb, and ll separated by single spaces, where aba \ne b and 1l5001 \le l \le 500. Each line describes a one-way road of length ll that leads from town aa to town bb. For any ordered pair of towns there is at most one road in that direction.

The next line contains one integer qq, where 1q100001 \le q \le 10000, the number of queries. Each of the following qq lines contains three integers cc, dd, and kk separated by single spaces, where 1k1001 \le k \le 100. Each query asks for the length of the kk-th shortest route from town cc to town dd.

Output

Write the answers to the queries, one per line, in the order the queries are given. For the ii-th query, print a single integer: the length of the requested route, or 1-1 if fewer than kk such routes exist.