In an internet cafe, there are 'N' computers numbered from 1 to ‘N’. These computers are connected by 'M' wires. Each wire connects two different computers. Information can be transmitted between two computers if there exists a path between the two computers by travelling through some ( possibly Zero ) intermediate computers. The cafe owner wants to remove some of the existing wires and then use those wires to connect some other pair of computers in such a manner that after the rearrangement, it is possible to transmit information between all the pairs of computers.
You are given the description of each of the 'M' wires, i.e., the two computers that the wire connects. Your task is to find the minimum number of wires that the cafe owner needs to remove and rearrange to satisfy the above conditions.
If there is no possible way to remove and rearrange wires to satisfy the above conditions, then return -1.
The first line of the input contains an integer, 'T,’ denoting the number of test cases.
The first line of each test case contains two space-separated integers, 'N' and 'M', denoting the number of computers and the number of wires, respectively.
The next 'M' lines of each test case contain the description of the 'M' wires
The 'i'th' line contains two space-separated integers, 'Computer1', and 'Computer2' . 'Computer1'' and 'Computer2' denote the two computers that are connected by the 'i'th' wire.
Output Format :
The only line of output of each test case should contain the minimum number of wires that should be removed and rearranged so that it is possible to transmit information between all pairs of computers.
Print the output of each test case in a new line.
Note :
You do not need to print anything. It has already been taken care of. Just implement the given function.
1 <= T <= 10
1 <= N <= 10^5
0 <= M <= min(10^5, (N*(N-1))/2)
1 <= Computer1, Computer2 <= N
Any two computers are directly connected by at most one wire.
Time Limit: 1 sec
2
4 3
1 2
1 3
2 3
4 2
1 2
3 4
1
-1
For the first test case :
We can remove the wire that connects Computer 1 and Computer 3, and use that wire to connect Computer 1 and Computer 4. Now, all the computers are connected. Hence, the answer is 1 in this case.

For the second test case:
For the above network, we can see that it is impossible to replace the wires so that information can be transmitted between all computers. Hence, the answer is -1 in this case.
2
4 4
1 2
3 1
2 3
3 4
4 3
3 4
3 2
2 4
0
1
Try to think of remodelling the problem as a graph problem.
Let us first find the condition for which it is impossible to replace wires to connect all computers. The only necessary and sufficient condition to check is that the number of wires should always be greater than or equal to the number of computers - 1, i.e., M > = ( N - 1 ). If this condition holds true, then the answer can never be -1. Otherwise, the answer will always be -1. This condition is necessary because to form a connected network of computers using minimum wires, we need to form a tree-like structure, and the number of edges in a tree having N nodes is N - 1.
Now we will remodel the above problem as a graph problem. The computers will act as the graph vertices, and the wires will act as the graph edges. The problem now reduces to find the number of connected components in the graph. If the number of connected components in the graph is K, then our answer will be K - 1. This works because here, a connected component represents a network of computers that can transmit information among them. We already have checked that there are sufficient wires available, so we can take a wire from a connected component having an extra wire and use it to connect to another component to make a bigger connected component. We will need to do this step until all the connected components are not joined together. If we simulate the above process for some small graphs. We can observe that our answer will always be numberOfConnectedComponents - 1.
We can find the connected components and the number of connected components in a graph using Depth-First search (DFS). Reference.
Algorithm
As we are using DFS to find the number of connected components in the graph, and we will visit each edge of the graph exactly once, hence the time complexity of doing that will be O(M). We also need to create and traverse the visited array for DFS having N elements. Hence, the overall time complexity is O(N+M).
The visited array required for DFS has N elements, and the number of edges in the built graph is M. Hence, the overall space complexity is O(N+M).