

Given graph may be not connected.
Nodes are numbered from [1, 2 ….. N] in input data.
Output data must be sorted by ‘source’ nodes.
The first line contains 'T', denoting the number of tests.
For each Test :
The first line contains two space-separated integers, 'N' and 'M', denoting the number of nodes and number of edges, respectively.
Next 'M' lines contain three integers 'x[i]', 'y[i]', 'w[i]' denoting there is a directed edge from node 'x[i]' to node 'y[i]', and having cost 'w[i]'.
For each Test :
The first line contains an integer 'P', denoting the number of simple paths.
Next 'P' lines contain three integers 'a[i]', 'b[i]', 'c[i]' denoting there is a path starting from node 'a[i]', ending at node 'b[i]' and having cost 'c[i]'.
You are not required to print the expected output. It has already been taken care of. Just implement the function.
1 <= T <= 5
1 <= N, M <= 10^5
1 <= x[i], y[i] <= N i ∈ (1, M)
0 <= w[i] <= 10^9 i ∈ (1, M)
Note - Sum of N and sum of M over all test cases does not exceed 10^5.
Time Limit: 1 sec
A ‘source’ node can be easily identified by checking the presence of incoming and outgoing edges to/from it. From each ‘source’ node, run a DFS and keep track of the minimum weighted edge. The DFS will run until it finds a node for which no outgoing node is found; it’s our ‘destination’ node.
Algorithm: