#P1970G3. Min-Fund Prison (Hard)

    ID: 9160 Type: RemoteJudge 1000ms 256MiB Tried: 0 Accepted: 0 Difficulty: (None) Uploaded By: Tags>bitmasksdfs and similardpgraphstrees

Min-Fund Prison (Hard)

No submission language available for this problem.

Description

In the hard version, 2n1052 \leq \sum n \leq 10^5 and 1m5×1051 \leq \sum m \leq 5 \times 10^{5}

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 nn prison cells and mm bi-directional corridors. The ithi^{th} corridor is from cells uiu_i to viv_i. A subset of these cells SS is called a complex if any cell in SS is reachable from any other cell in SS. Formally, a subset of cells SS is a complex if xx and yy are reachable from each other for all x,ySx, y \in S, using only cells from SS on the way. The funding required for a complex SS consisting of kk cells is defined as k2k^2.

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 22 complexes with exactly one corridor\textbf{exactly one corridor} 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 22 cells is cc.

Due to budget cuts and the ongoing fight against the Death Eaters, you must find the minimum total funding\textbf{minimum total funding} required to divide the prison as per the Ministry's requirements or 1-1 if no division is possible.

Note: The total funding is the sum of the funding required for the 22 complexes and the corridors built. If after the division, the two complexes have xx and yy cells respectively and you have built a total of aa corridors, the total funding will be x2+y2+c×ax^2 + y^2 + c \times a. Note that x+y=nx+y=n.

The first line contains one integer tt (1t1051 \leq t \leq 10^5) — the number of test cases. Then tt test cases follow.

The first line of each test case consists of three integers n,mn, m and cc (2n1052 \leq n \leq 10^5, 1m5×1051 \leq m \leq 5 \times 10^{5}, 1c1091 \leq c \leq 10^9)

mm lines follow, each consisting of 2 integers — ui,viu_i, v_i indicating a corridor is present between cells uiu_i and viv_i (1ui,vin1 \leq u_i, v_i \leq n, uiviu_i \neq v_i)

It is guaranteed that the sum of nn over all test cases does not exceed 10510^5.

It is guaranteed that the sum of mm over all test cases does not exceed 5×1055 \times 10^5.

It is guaranteed that there exists at most one corridor between any two cells.

Print the minimum funding\textbf{minimum funding} required to divide the prison as per the Ministry's requirements or 1-1 if no division is possible.

Input

The first line contains one integer tt (1t1051 \leq t \leq 10^5) — the number of test cases. Then tt test cases follow.

The first line of each test case consists of three integers n,mn, m and cc (2n1052 \leq n \leq 10^5, 1m5×1051 \leq m \leq 5 \times 10^{5}, 1c1091 \leq c \leq 10^9)

mm lines follow, each consisting of 2 integers — ui,viu_i, v_i indicating a corridor is present between cells uiu_i and viv_i (1ui,vin1 \leq u_i, v_i \leq n, uiviu_i \neq v_i)

It is guaranteed that the sum of nn over all test cases does not exceed 10510^5.

It is guaranteed that the sum of mm over all test cases does not exceed 5×1055 \times 10^5.

It is guaranteed that there exists at most one corridor between any two cells.

Output

Print the minimum funding\textbf{minimum funding} required to divide the prison as per the Ministry's requirements or 1-1 if no division is possible.

Sample Input 1

4
4 6 5
4 3
2 3
2 4
1 2
4 1
3 1
6 6 2
1 4
2 5
3 6
1 5
3 5
6 5
6 5 7
1 4
2 5
3 6
3 5
6 5
7 5 4
1 4
3 6
3 5
6 5
2 7

Sample Output 1

-1
20
25
33

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 11 and 55 as the connection between the 22 complexes consisting of {2,3,5,6}\{2, 3, 5, 6\} and {1,4}\{1, 4\} cells respectively. There are no new corridors built. The total funding is 42+22=204^2 + 2^2 = 20. You can verify this is the minimum funding required.

In the third test case, build a corridor between 22 and 44. Consider the corridor between cells 11 and 55 as the connection between the 22 complexes consisting of {3,5,6}\{3, 5, 6\} and {1,2,4}\{1, 2, 4\} cells respectively. The total funding is 32+32+7×1=253^2 + 3^2 + 7 \times 1 = 25. You can verify this is the minimum funding required.

In the fourth test case, build a corridor between 22 and 44 and between 55 and 77. Consider the corridor between cells 55 and 77 as the connection between the 22 complexes consisting of {1,2,4,7}\{1, 2, 4, 7\} and {3,5,6}\{3, 5, 6\} cells respectively. The total funding is 42+32+4×2=334^2 + 3^2 + 4 \times 2 = 33. 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.