#P1970G3. Min-Fund Prison (Hard)
Min-Fund Prison (Hard)
No submission language available for this problem.
Description
In the hard version, and
After a worker's strike organized by the Dementors asking for equal rights, the prison of Azkaban has suffered some damage. After settling the spirits, the Ministry of Magic is looking to renovate the prison to ensure that the Dementors are kept in check. The prison consists of prison cells and bi-directional corridors. The corridor is from cells to . A subset of these cells is called a complex if any cell in is reachable from any other cell in . Formally, a subset of cells is a complex if and are reachable from each other for all , using only cells from on the way. The funding required for a complex consisting of cells is defined as .
As part of your Intro to Magical Interior Design course at Hogwarts, you have been tasked with designing the prison. The Ministry of Magic has asked that you divide the prison into complexes with connecting them, so that the Dementors can't organize union meetings. For this purpose, you are allowed to build bi-directional corridors. The funding required to build a corridor between any cells is .
Due to budget cuts and the ongoing fight against the Death Eaters, you must find the required to divide the prison as per the Ministry's requirements or if no division is possible.
Note: The total funding is the sum of the funding required for the complexes and the corridors built. If after the division, the two complexes have and cells respectively and you have built a total of corridors, the total funding will be . Note that .
The first line contains one integer () — the number of test cases. Then test cases follow.
The first line of each test case consists of three integers and (, , )
lines follow, each consisting of 2 integers — indicating a corridor is present between cells and (, )
It is guaranteed that the sum of over all test cases does not exceed .
It is guaranteed that the sum of over all test cases does not exceed .
It is guaranteed that there exists at most one corridor between any two cells.
Print the required to divide the prison as per the Ministry's requirements or if no division is possible.
Input
The first line contains one integer () — the number of test cases. Then test cases follow.
The first line of each test case consists of three integers and (, , )
lines follow, each consisting of 2 integers — indicating a corridor is present between cells and (, )
It is guaranteed that the sum of over all test cases does not exceed .
It is guaranteed that the sum of over all test cases does not exceed .
It is guaranteed that there exists at most one corridor between any two cells.
Output
Print the required to divide the prison as per the Ministry's requirements or if no division is possible.
Note
In the first test case of the sample input, there is no way to divide the prison according to the Ministry's requirements.
In the second test case, consider the corridor between cells and as the connection between the complexes consisting of and cells respectively. There are no new corridors built. The total funding is . You can verify this is the minimum funding required.
In the third test case, build a corridor between and . Consider the corridor between cells and as the connection between the complexes consisting of and cells respectively. The total funding is . You can verify this is the minimum funding required.
In the fourth test case, build a corridor between and and between and . Consider the corridor between cells and as the connection between the complexes consisting of and cells respectively. The total funding is . You can verify this is the minimum funding required.
Note for all test cases that there may be multiple ways to get the same funding but there is no other division which will have a more optimal minimum funding.