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 java.io.*;
  import java.sql.Array;
  import java.text.DateFormat;
  import java.text.ParseException;
  import java.text.SimpleDateFormat;
  import java.time.Instant;
  import java.util.*;
  import java.util.regex.Matcher;
  import java.util.regex.Pattern;
  
  
  public class Main {
      public static class UnionFind {
          private int[] parent;
          private int[] sizes;
          private int[] maxEl;
  
          public UnionFind(int N) {
              // Set parent of each element to itself
              // (N array accesses)
              parent = new int[N];
              sizes = new int[N];
              maxEl = new int[N];
              for (int i=0; i<N; i++) {
                  parent[i] = maxEl[i] = i;
                  sizes[i] = 1;
              }
          }
  
          int root(int p) {
              // Check parent pointers until reach root
              // (Depth of i array accesses)
              while (parent[p] != p) {
  
                  //PATH COMPRESSION
                  //No need to traverse path all time
                  //Just make it point to grandparent instead of parent
                  //Halving the length to traverse, as we only care about root
                  parent[p] = parent[parent[p]];
  
                  p = parent[p];
              }
              return p;
          }
  
          public boolean connected(int p, int q) {
              // Check if p and q have the same root
              // (Depth of p and q array accesses)
              return this.root(p) == this.root(q);
          }
  
          public void union(int p, int q) {
              // Change root of p to point to q
              // (Depth of p and q array accesses)
              int p_root = this.root(p);
              int q_root = this.root(q);
  
              // If they already under same parent, no need to do anything
              if(p_root == q_root) return;
  
              // If size of p is small, attach it to q, otherwise opposite
              if(sizes[p_root] < sizes[q_root]) {
                  this.parent[p_root] = q_root;
                  sizes[q_root] += sizes[p_root];
                  maxEl[q_root] = Math.max(maxEl[q_root], maxEl[p_root]);
              } else {
                  this.parent[q_root] = p_root;
                  sizes[p_root] += sizes[q_root];
                  maxEl[p_root] = Math.max(maxEl[q_root], maxEl[p_root]);
              }
          }
  
          public int findMax(int p) {
              int p_root = this.root(p);
              return this.maxEl[p_root];
          }
      }
  
      static long inverse(int n,int MD) {
          return n == 1 ? 1 : inverse(n - MD % n, MD) * (MD / n + 1) % MD;
      }
  
      static long choose(int n, int k, int MD) {
          return k == 0 ? 1 : choose(n - 1, k - 1, MD) * n % MD * inverse(k, MD) % MD;
      }
  
      public static class Node {
          int a;
          int b;
          Node(int a, int b) {
              this.a = a;
              this.b = b;
          }
      }
  
  
  
      /************************ SOLUTION STARTS HERE ************************/
      private static void solve() {
          int n = nextInt();
          int m1 = nextInt();
          int m2 = nextInt();
  
          UnionFind uf1 = new UnionFind(n);
          UnionFind uf2 = new UnionFind(n);
          for(int i=0;i<m1;i++) {
              int u1 = nextInt();
              int u2 = nextInt();
              uf1.union(u1-1,u2-1);
          }
  
          for(int i=0;i<m2;i++) {
              int u1 = nextInt();
              int u2 = nextInt();
              uf2.union(u1-1,u2-1);
          }
  
          ArrayList<Node> list = new ArrayList<>();
          // Do it for node 0
          for(int j=1;j<n;j++) {
              boolean c1 = uf1.connected(0, j);
              boolean c2 = uf2.connected(0, j);
              if(!c1 && !c2) {
                  list.add(new Node(0, j));
                  uf1.union(0, j);
                  uf2.union(0, j);
              }
          }
  
          int left = 0, right = 0;
          Set<Integer> set1 = new HashSet<>();
          Set<Integer> set2 = new HashSet<>();
          set1.add(uf1.root(0));
          set2.add(uf2.root(0));
          while(left < n && right < n) {
              while(left < n && set1.contains(uf1.root(left)))left++;
              while(right < n && set2.contains(uf2.root(right)))right++;
  
              if(left >= n || right >= n)
                  break;
  
              uf1.union(0, left);
              uf2.union(0, left);
              set1.add(uf1.root(left));
              set2.add(uf2.root(right));
  
              list.add(new Node(left, right));
          }
  
          println(list.size());
          for(int i=0;i<list.size();i++) {
              Node node = list.get(i);
              println((node.a+1) + " " +(node.b+1));
          }
      }
  
  
      /************************ SOLUTION ENDS HERE ************************/
  
  
  
      /************************ TEMPLATE STARTS HERE **********************/
  
      public static void main(String[] args) throws IOException, ParseException {
          reader = new BufferedReader(new InputStreamReader(System.in));
          writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)), false);
          st = null;
          solve();
          reader.close();
          writer.close();
      }
  
      static BufferedReader  reader;
      static PrintWriter     writer;
      static StringTokenizer st;
  
      static String next() {
          while (st == null || !st.hasMoreTokens()) {
              try {
                  String line = reader.readLine();
                  if (line == null) {
                      return null;
                  }
                  st = new StringTokenizer(line);
              } catch (Exception e) {
                  throw new RuntimeException();
              }
          }
          return st.nextToken();
      }
  
      static String nextLine() {
          String s = null;
          try {
              s = reader.readLine();
          } catch (IOException e) {
              e.printStackTrace();
          }
          return s;
      }
  
      static int nextInt() {
          return Integer.parseInt(next());
      }
  
      static long nextLong() {
          return Long.parseLong(next());
      }
  
      static double nextDouble() {
          return Double.parseDouble(next());
      }
  
      static char nextChar() {
          return next().charAt(0);
      }
  
      static int[] nextIntArray(int n) {
          int[] a = new int[n];
          int i = 0;
          while (i < n) {
              a[i++] = nextInt();
          }
          return a;
      }
  
      static long[] nextLongArray(int n) {
          long[] a = new long[n];
          int i = 0;
          while (i < n) {
              a[i++] = nextLong();
          }
          return a;
      }
  
      static int[] nextIntArrayOneBased(int n) {
          int[] a = new int[n + 1];
          int i = 1;
          while (i <= n) {
              a[i++] = nextInt();
          }
          return a;
      }
  
      static long[] nextLongArrayOneBased(int n) {
          long[] a = new long[n + 1];
          int i = 1;
          while (i <= n) {
              a[i++] = nextLong();
          }
          return a;
      }
  
      static void print(Object o) {
          writer.print(o);
      }
  
      static void println(Object o) {
          writer.println(o);
      }
  
      static void printArr(int[] arr) {
          for(int i = 0 ; i < arr.length; i++)
              System.out.print(arr[i] + " ");
          System.out.println();
      }
  
      public static void exchange(int[] arr, int i, int j) {
          int temp = arr[i];
          arr[i] = arr[j];
          arr[j] = temp;
      }
  
      public static void shuffle(int arr[], int n) {
          for (int i = 0; i < n; i++) {
              int r = (int)Math.floor(Math.random() * (i+1));
              exchange(arr, i, r);
          }
      }
  
      /************************ TEMPLATE ENDS HERE ************************/
  }
  