An interesting tour
In Flatland, there are n cities connected by m one-way roads.
A tourist company wants to create a scenic cyclic route using these roads. The route should start and end in the same city, following the roads in their given directions and visiting intermediate cities. While a city can be visited multiple times, no road should be traversed more than once.
Each road has a landscape type, represented by a number from 1 to m. For the route to be considered scenic, any two consecutive roads must have different landscape types. This rule also applies to the first and last road of the route, ensuring the journey can begin from any city on the route.
Your task is to help the company find such a route or determine if it's impossible.
Input
The input consists of multiple test cases. The first line contains the integer T — the number of test cases (1 ≤ T ≤ 10^5
).
For each test case, the first line contains two integers n and m — the number of cities and roads (2 ≤ n, m ≤ 2 * 10^5)
. The next m lines each describe a road with three integers u[i]
, v[i]
, and c[i]
— the starting city, the destination city, and the landscape type of the i-th road (1 ≤ u[i], v[i] ≤ n; 1 ≤ c[i] ≤ m; u[i] ≠ v[i])
.
The total sum of n across all test cases does not exceed 2 * 10^5
. Similarly, the total sum of m across all test cases does not exceed 2 * 10^5
.
Output
For each test case, output the result.If no suitable route exists, print "-1". If a route is possible, print the integer k — the length of the route (2 ≤ k ≤ m). On the next line, print k integers e1, e2, ..., ek
— the road numbers in the order they appear in the route. All numbers ei must be distinct. If multiple routes are possible, any valid route can be printed.