description: This is the hard version of the problem. The only difference between the two versions is the constraint on n. You can make hacks only if all versions of the problem are solved.
  
  A forest is an undirected graph without cycles (not necessarily connected).
  
  Mocha and Diana are friends in Zhijiang, both of them have a forest with nodes numbered from 1 to n, and they would like to add edges to their forests such that: 
  
    * After adding edges, both of their graphs are still forests. 
    * They add the same edges. That is, if an edge (u, v) is added to Mocha's forest, then an edge (u, v) is added to Diana's forest, and vice versa. 
  
  
  
  Mocha and Diana want to know the maximum number of edges they can add, and which edges to add.
  
  Input
  
  The first line contains three integers n, m_1 and m_2 (1 ≤ n ≤ 10^5, 0 ≤ m_1, m_2 < n) — the number of nodes and the number of initial edges in Mocha's forest and Diana's forest.
  
  Each of the next m_1 lines contains two integers u and v (1 ≤ u, v ≤ n, u ≠ v) — the edges in Mocha's forest.
  
  Each of the next m_2 lines contains two integers u and v (1 ≤ u, v ≤ n, u ≠ v) — the edges in Diana's forest.
  
  Output
  
  The first line contains only one integer h, the maximum number of edges Mocha and Diana can add.
  
  Each of the next h lines contains two integers u and v (1 ≤ u, v ≤ n, u ≠ v) — the edge you add each time.
  
  If there are multiple correct answers, you can print any one of them.
  
  Examples
  
  Input
  
  
  3 2 2
  1 2
  2 3
  1 2
  1 3
  
  
  Output
  
  
  0
  
  
  Input
  
  
  5 3 2
  5 4
  2 1
  4 3
  4 3
  1 4
  
  
  Output
  
  
  1
  2 4
  
  
  Input
  
  
  8 1 2
  1 7
  2 6
  1 5
  
  
  Output
  
  
  5
  5 2
  2 3
  3 4
  4 7
  6 8
  
  Note
  
  In the first example, we cannot add any edge.
  
  In the second example, the initial forests are as follows.
  
  <image>
  
  We can add an edge (2, 4).
  
  <image>
solution: import collections as _collections
  import random as _random
  import sys as _sys
  
  
  def _main():
      [tests_n] = [1]
      for i_test in range(tests_n):
          [n, m_1, m_2] = _read_ints()
          edges_1 = tuple(_read_n_egdes(m_1))
          edges_2 = tuple(_read_n_egdes(m_2))
          random = _random.Random(_random.SystemRandom().getrandbits(256))
          new_edges = add_max_edges_n(n, edges_1, edges_2, random=random)
          print(len(new_edges))
          for u, v in new_edges:
              print(u+1, v+1)
  
  
  def add_max_edges_n(nodes_n, edges_1, edges_2, *, random):
      nodes = list(range(nodes_n))
      colors_1 = DSU.from_equivalence_relations(edges_1, nodes=nodes)
      colors_2 = DSU.from_equivalence_relations(edges_2, nodes=nodes)
      del edges_1, edges_2
      
      new_edges = []
      
      while min(len(colors_1.owners), len(colors_2.owners)) > 1:
          colors_1, colors_2, nodes, new_new_edges = _try_remove_largest_group(nodes, colors_1, colors_2)
          new_edges.extend(new_new_edges)
          colors_2, colors_1, nodes, new_new_edges = _try_remove_largest_group(nodes, colors_2, colors_1)
          new_edges.extend(new_new_edges)
      
      return new_edges
  
  
  def _try_remove_largest_group(nodes, colors_1, colors_2):
      new_edges = []
      
      def consider_edge(u, v):
          if not colors_1.is_same_owner(u, v) and not colors_2.is_same_owner(u, v):
              new_edges.append((u, v))
              colors_1.merge_owners(u, v)
              colors_2.merge_owners(u, v)
      
      largest_owner_1 = max(colors_1.owners, key=lambda owner: len(colors_1.get_group_by_owner(owner)))
      largest_1 = set(colors_1.get_group_by_owner(largest_owner_1))
      from_largest = next(iter(largest_1))
      for node in nodes:
          if node != from_largest:
              consider_edge(from_largest, node)
              if node not in largest_1:
                  # assert colors_2.is_same_owner(node, from_largest)
                  pass
      
      nontrivial_largest = [x for x in largest_1 if not colors_2.is_same_owner(x, from_largest)]
      # if not nontrivial_largest:
      #     assert len(colors_2.owners) <= 1
      #     return colors_1, colors_2, nodes, new_edges
      
      for node in nodes:
          if colors_1.is_same_owner(node, from_largest):
              continue
          while nontrivial_largest and colors_2.is_same_owner(node, nontrivial_largest[-1]):
              nontrivial_largest.pop()
          if nontrivial_largest:
              consider_edge(node, nontrivial_largest.pop())
      
      nodes = [node for node in nodes if node not in largest_1]
      colors_1 = DSU.from_other_dsu(colors_1, nodes=nodes)
      colors_2 = DSU.from_other_dsu(colors_2, nodes=nodes)
      
      return colors_1, colors_2, nodes, new_edges
  
  
  class DSU:
      
      def __init__(self, nodes):
          self._parents = {node: node for node in nodes}
          self._owners = ChoosableSet(self._parents)
          self._groups_by_owners = {owner: [owner] for owner in self._parents}
      
      def _is_owner(self, node):
          return node == self._parents[node]
      
      def _get_owner(self, node):
          to_fix = []
          while not self._is_owner(node):
              to_fix.append(node)
              node = self._parents[node]
          for x in to_fix:
              self._parents[x] = node
          return node
      
      def is_same_owner(self, node_1, node_2):
          return self._get_owner(node_1) == self._get_owner(node_2)
      
      def merge_owners(self, node_1, node_2):
          node_1 = self._get_owner(node_1)
          node_2 = self._get_owner(node_2)
          assert node_1 != node_2
          self._parents[node_2] = node_1
          self._owners.discard(node_2)
          old_groups = [self._groups_by_owners[node_1], self._groups_by_owners[node_2]]
          del self._groups_by_owners[node_1], self._groups_by_owners[node_2]
          lower_group, higher_group = sorted(old_groups, key=len)
          higher_group.extend(lower_group)
          lower_group.clear()
          self._groups_by_owners[node_1] = higher_group
      
      def get_group_by_owner(self, owner):
          return self._groups_by_owners[owner]
      
      @property
      def owners(self):
          return self._owners
      
      @classmethod
      def from_equivalence_relations(cls, equivalence_relations, *, nodes):
          self = cls(nodes)
          for u, v in equivalence_relations:
              if not self.is_same_owner(u, v):
                  self.merge_owners(u, v)
          return self
      
      @classmethod
      def from_other_dsu(cls, other_dsu, *, nodes):
          nodes = set(nodes)
          groups = [other_dsu.get_group_by_owner(owner) for owner in other_dsu.owners]
          new_dsu = cls(nodes)
          for group in groups:
              group = [node for node in group if node in nodes]
              if group:
                  u, *vs = group
                  for v in vs:
                      new_dsu.merge_owners(u, v)
          return new_dsu
  
  
  class ChoosableSet:
      
      def __init__(self, initial_elements=()):
          self._elements_as_set = set(initial_elements)
          self._elements_as_list = list(self._elements_as_set)
          self._removed = set()
      
      def __len__(self):
          return len(self._elements_as_set) - len(self._removed)
      
      def __iter__(self):
          return iter(self._elements_as_set - self._removed)
      
      def add(self, element):
          self._removed.discard(element)
          if element not in self._elements_as_set:
              self._elements_as_set.add(element)
              self._elements_as_list.append(element)
      
      def discard(self, element):
          self._removed.add(element)
          if len(self._removed) * 2 > len(self._elements_as_set):
              self._elements_as_set -= self._removed
              self._elements_as_list = [x for x in self._elements_as_list if x not in self._removed]
              self._removed = set()
      
      def choice(self, random):
          result = random.choice(self._elements_as_list)
          while result in self._removed:
              result = random.choice(self._elements_as_list)
          return result
  
  
  def _read_n_egdes(edges_n):
      edges = (_read_ints() for i_edge in range(edges_n))
      edges = ((u-1, v-1) for u, v in edges)
      return edges
  
  
  def _read_ints():
      return map(int, _read_string().split())
  
  
  def _read_string():
      result = _sys.stdin.readline()
      assert result and result[-1] == '\n'
      return result[:-1]
  
  
  if __name__ == '__main__':
      _main()