Two Avenues
Contest: Codeforces Round 775 (Div. 1, based on Moscow Open Olympiad in Informatics)
Difficulty rating: 3500
Tags: data structures, dfs and similar, graphs

In order to make the capital of Berland a more attractive place for tourists, the great king came up with the following plan: choose two streets of the city and call them avenues. Certainly, these avenues will be proclaimed extremely important historical places, which should attract tourists from all over the world.

The capital of Berland can be represented as a graph, the vertices of which are crossroads, and the edges are streets connecting two crossroads. In total, there are $$$n$$$ vertices and $$$m$$$ edges in the graph, you can move in both directions along any street, you can get from any crossroad to any other by moving only along the streets, each street connects two different crossroads, and no two streets connect the same pair of crossroads.

In order to reduce the flow of ordinary citizens moving along the great avenues, it was decided to introduce a toll on each avenue in both directions. Now you need to pay $$$1$$$ tugrik for one passage along the avenue. You don't have to pay for the rest of the streets.

Analysts have collected a sample of $$$k$$$ citizens, $$$i$$$-th of them needs to go to work from the crossroad $$$a_i$$$ to the crossroad $$$b_i$$$. After two avenues are chosen, each citizen will go to work along the path with minimal cost.

In order to earn as much money as possible, it was decided to choose two streets as two avenues, so that the total number of tugriks paid by these $$$k$$$ citizens is maximized. Help the king: according to the given scheme of the city and a sample of citizens, find out which two streets should be made avenues, and how many tugriks the citizens will pay according to this choice.

Input format:
Each test consists of multiple test cases. The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^5$$$) — the number of test cases.

The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$3 \leq n \leq 500\,000$$$, $$$n - 1 \leq m \leq 500\,000$$$, $$$m \le \frac{n (n - 1)}{2}$$$) — the number of crossroads and streets, respectively.

The next $$$m$$$ lines contain the description of streets, $$$i$$$-th line contains two integers $$$s_i$$$ and $$$f_i$$$ ($$$1 \leq s_i, f_i \leq n$$$, $$$s_i \neq f_i$$$) — indexes of crossroads which are connected by $$$i$$$-th street. It is guaranteed that no two streets connect the same pair of crossroads, you can get from any crossroad to any other by moving only along the streets.

The next line contains a single integer $$$k$$$ ($$$1 \leq k \leq 500\,000$$$) — the amount of citizens in the sample.

The next $$$k$$$ lines contain the description of citizens, $$$i$$$-th line contains two integers $$$a_i$$$ and $$$b_i$$$ ($$$1 \leq a_i, b_i \leq n$$$, $$$a_i \neq b_i$$$) — $$$i$$$-th citizen goes to work from crossroad $$$a_i$$$ to crossroad $$$b_i$$$.

Let $$$M$$$ be the sum of $$$m$$$ over all test cases and $$$K$$$ be the sum of $$$k$$$ over all test cases. It is guaranteed that $$$M, K \le 500\,000$$$.

Output format:
For each test case print the answer to the problem.

In the first line print the total amount of tugriks that will be paid by citizens.

In the second line print two integers $$$x_1$$$ and $$$y_1$$$ — the numbers of crossroads that will be connected by the first avenue.

In the third line print two integers $$$x_2$$$ and $$$y_2$$$ — the numbers of crossroads that will be connected by the second avenue.

The numbers of crossroads connected by an avenue can be printed in any order, each of the printed streets should be among $$$m$$$ streets of the city, chosen streets should be different.

Constraints:
Time limit: 8.0 s
Memory limit: 512.0 MB

Example input:
3
6 5
1 2
2 3
2 4
4 5
4 6
3
1 6
5 3
2 5
5 5
1 2
2 3
3 4
4 5
5 1
6
1 5
1 3
1 3
2 4
2 5
5 3
8 10
1 2
2 3
3 4
4 5
5 6
6 7
7 8
7 1
1 8
3 6
4
2 5
3 7
2 5
7 8

Example output:
5
4 2
5 4
5
1 5
3 2
3
7 6
2 3

Editorial:
Let's consider two edges from the answer $$$e_1$$$, $$$e_2$$$. At least one of them should lie on dfs tree, otherwise the graph will be connected after removing $$$e_1$$$, $$$e_2$$$ and the answer will be $$$0$$$.
Let the answer be $$$e_1$$$, $$$e_2$$$, where $$$e_1$$$ lies on dfs tree. What cases can be for edges $$$e_1$$$, $$$e_2$$$? The edge $$$e_2$$$ should be a bridge of the graph without edge $$$e_1$$$ (otherwise $$$e_2$$$ can be any). Using this we can highlight possible cases:
1. Both edges $$$e_1$$$, $$$e_2$$$ are bridges of the graph.
2. The edge $$$e_1$$$ is the bridge of the graph, the edge $$$e_2$$$ does not matter.
3. The edge $$$e_1$$$ lies on dfs tree, the edge $$$e_2$$$ is the only outer edge covering the edge $$$e_1$$$.
4. Both edges $$$e_1$$$, $$$e_2$$$ lies on dfs tree, sets of outer edges covering $$$e_1$$$, and covering $$$e_2$$$ are equal.
For each case let's find the maximum answer.
For each of $$$k$$$ pairs of vertices let's consider the path between vertices in the pair.
For each edge $$$e$$$ of dfs tree let's calculate three values:
• $$$c_e$$$ — the number of paths, containing the edge $$$e$$$.
• $$$f_e$$$ — the number of outer edges, covering the edge $$$e$$$.
• $$$h_e$$$ — the hash of all outer edges, covering the edge $$$e$$$. For each outer edge let's give random $$$64$$$-bit integer, the hash will be equal to the sum of values.
In the first case, the answer for two bridges $$$e_1$$$, $$$e_2$$$ is equal to $$$c_{e_1} + c_{e_2}$$$. So we should find two brides with maximum value $$$c_e$$$.
In the second case, the answer for one bridge $$$e_1$$$ is equal to $$$c_{e_1}$$$. So we should find the bridge with maximum value $$$c_e$$$.
In the third case, we consider edges $$$e_1$$$, such that $$$f_{e_1} = 1$$$. The answer is $$$c_{e_1}$$$. So we should find edge $$$e_1$$$ of dfs tree, such that $$$f_{e_1} = 1$$$ with maximum value $$$c_{e_1}$$$.
The fourth case is very hard. We consider two edges $$$e_1$$$, $$$e_2$$$, such that $$$h_{e_1} = h_{e_2}$$$. The answer is equal to the number of paths, containing exactly one edge from $$$e_1$$$, $$$e_2$$$. We can divide each path into two vertical paths, the answer won't change.
The plan of the solution will be: let's make a dfs of the dfs tree and maintain the segment tree with operations add on segment, and maximum on segment. The prefix of the segment tree corresponds to edges on the path from the root to the current edge $$$e_2$$$. The value in the cell corresponding to the edge $$$e_1$$$ is equal to the answer for the pair of edges $$$e_1$$$, $$$e_2$$$.
It is possible to recalculate this segment tree with $$$O(n + k)$$$ updates during the dfs tree traversal.
The unsolved problem now is how to consider only edges $$$e_1$$$, such that $$$h_{e_1} = h_{e_2}$$$ in the maximum on segment. Let's call a cluster all edges with equal hash. All edges from one cluster lie on the vertical path. Let's consider the vertical path for each cluster: from the first occurrence of edge from the cluster to the last occurrence of edge from the cluster. Any two paths for two clusters either do not intersect or nested into each other.
When making a traversal let's add $$$-\infty$$$ on the segment from the last edge with hash $$$h_{e_2}$$$ to the edge $$$e_2$$$. This move will exclude edges from the maximum. This won't interfere the values, because all hashes from the segment won't be found later in the traversal (due to the clusters structure). After that, let's find the maximum on the segment from the first edge with hash $$$h_{e_2}$$$ to the edge $$$e_2$$$. In that maximum all edges with the hash $$$h_{e_2}$$$ will participate.
The total solution has the complexity $$$O(m + (n + k) \log{n})$$$.