CF2060E.Graph Composition

传统题 时间 2000 ms 内存 256 MiB 8 尝试 9 已通过 4 标签

Graph Composition

You are given two simple undirected graphs FF and GG with nn vertices. FF has m1m_1 edges while GG has m2m_2 edges. You may perform one of the following two types of operations any number of times:

  • Select two integers uu and vv (1u,vn1 \leq u,v \leq n) such that there is an edge between uu and vv in FF. Then, remove that edge from FF.
  • Select two integers uu and vv (1u,vn1 \leq u,v \leq n) such that there is no edge between uu and vv in FF. Then, add an edge between uu and vv in FF.

Determine the minimum number of operations required such that for all integers uu and vv (1u,vn1 \leq u,v \leq n), there is a path from uu to vv in FF if and only if there is a path from uu to vv in GG.

Input

The first line contains an integer tt (1t1041 \leq t \leq 10^4) — the number of independent test cases.

The first line of each test case contains three integers nn, m1m_1, and m2m_2 (1n21051 \leq n \leq 2\cdot 10^5, 0m1,m221050 \leq m_1,m_2 \leq 2\cdot 10^5) — the number of vertices, the number of edges in FF, and the number of edges in GG.

The following m1m_1 lines each contain two integers uu and vv (1u,vn1 \leq u, v\leq n) — there is an edge between uu and vv in FF. It is guaranteed that there are no repeated edges or self loops.

The following m2m_2 lines each contain two integers uu and vv (1u,vn1 \leq u,v\leq n) — there is an edge between uu and vv in GG. It is guaranteed that there are no repeated edges or self loops.

It is guaranteed that the sum of nn, the sum of m1m_1, and the sum of m2m_2 over all test cases do not exceed 21052 \cdot 10^5.

Output

For each test case, output a single integer denoting the minimum operations required on a new line.

Note

In the first test case you can perform the following three operations:

  1. Add an edge between vertex 11 and vertex 33.
  2. Remove the edge between vertex 11 and vertex 22.
  3. Remove the edge between vertex 22 and vertex 33.

It can be shown that fewer operations cannot be achieved.

In the second test case, FF and GG already fulfill the condition in the beginning.

In the fifth test case, the edges from 11 to 33 and from 22 to 33 must both be removed.

Samples

5
3 2 1
1 2
2 3
1 3
2 1 1
1 2
1 2
3 2 0
3 2
1 2
1 0 0
3 3 1
1 2
1 3
2 3
1 2
3
0
2
0
2

在线编程 IDE

建议全屏模式获得最佳体验