

An edge with type 1 can only be traversed by a person ‘A’.
An edge with type 2 can only be traversed by a person ‘B’.
An edge with type 3 can be traversed by both persons ‘A’ and ‘B’.
The first line contains an integer ‘T’, which denotes the number of test cases to be run. Then, the ‘T’ test cases follow.
The first line of each test case contains two space-separated integers, ‘N’, denoting the total number of nodes, and ‘M’, denoting the total number of edges.
Then ‘M’ lines follow. Each of which has three space-separated integers, ‘X’ ‘Y’, and ‘Z’.
‘X’ denotes the type of edge. ‘Y’ and ‘Z’ denote the two nodes between which the edge is present.
For each test case, print a single line containing a single integer denoting the maximum number of edges that can be removed.
The output for each test case will be printed in a separate line.
You do not need to print anything. It has already been taken care of. Just implement the given function.
1 <= T <= 10
1 <= N,M < 10 ^ 4
edges[i].length = 3
1 <= edges[i][0] <= 3
1 <= edges[i][1], edges[i][1] <= N
Where ‘T’ is the number of test cases, ‘N’ is the total number of nodes, ‘M’ is the total number of edges and edges[i] describes the i-th edge
Time Limit: 1 sec.
The approach is to use Union-Find to find out the minimum number of edges that are required to make the graph a single connected component for both ‘A’ and ‘B’. Since using an edge of type 3 can minimize the total number of edges needed, we will consider these first. Finally, after considering all the edges, if the entire graph can be traversed by both A and B, we print the number of edges that were not needed, else we print “-1”.