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.util.*;
   
  public class Main extends PrintWriter {
      
      
      void solve() {
          int n = sc.nextInt();
          int[] m = new int[2];
          for(int t = 0; t < 2; t++) m[t] = sc.nextInt();
          DSU[] dsu = new DSU[2];
          for(int t = 0; t < 2; t++) {
              dsu[t] = new DSU(n);
              for(int i = 0; i < m[t]; i++) {
                  int u = sc.nextInt()-1;
                  int v = sc.nextInt()-1;
                  dsu[t].union(u, v);
              }
          }
          int[][][] lists = new int[2][n][];
          int[][] idx = new int[2][n];
          for(int t = 0; t < 2; t++) {
              for(int i = 0; i < n; i++) {
                  int set = dsu[t].find_set(i);
                  if(lists[t][set] == null) lists[t][set] = new int[dsu[t].sz[set]];
                  lists[t][set][idx[t][set]++] = i;
              }
          }
          ArrayDeque<String> ans = new ArrayDeque<>();
          boolean[][] done = new boolean[2][n];
          for(int t = 0; t < 2; t++) {
              int set = dsu[t].find_set(0);
              for(int x : lists[t][set]) {
                  done[t][x] = true;
              }
          }
          for(int i = 1; i < n; i++) {
              if(!done[0][i] && !done[1][i]) {
                  ans.add("1 " + (i+1));
                  for(int t = 0; t < 2; t++) {
                      int set = dsu[t].find_set(i);
                      for(int x : lists[t][set]) {
                          done[t][x] = true;
                      }
                  }
              }
          }
          int[] it = new int[2];
          while(true) {
              for(int t = 0; t < 2; t++) {
                  while(it[t] < n) {
                      if(done[t][it[t]] && !done[1-t][it[t]]) break;
                      it[t]++;
                  }
              }
              if(it[0] == n || it[1] == n) break;
              ans.add((it[0]+1) + " " + (it[1]+1));
              for(int t = 0; t < 2; t++) {
                  int set = dsu[t].find_set(it[1-t]);
                  for(int x : lists[t][set]) {
                      done[t][x] = true;
                  }
              }
          }
          println(ans.size());
          for(String s : ans) println(s);
      }
      
      class DSU{
          int[] p;
          int[] sz;
          
          int n;
          
          void make_set(int i) {
              p[i] = i;
              sz[i] = 1;
          }
          
          public DSU(int n) {
              p = new int[n];
              sz = new int[n];
              for(int i = 0; i < n; i++) {
                  make_set(i);
              }
          }
          
          int find_set(int u) {
              if(u == p[u]) return u;
              else return p[u] = find_set(p[u]);
          }
          
          boolean union(int u, int v) {
              u = find_set(u);
              v = find_set(v);
              if(u == v) return false;
              if(sz[u] > sz[v]) {
                  int temp = u;
                  u = v;
                  v = temp;
              }
              p[u] = v;
              sz[v] += sz[u];
              return true;
          }
      }
      
      
  //  Main() throws FileNotFoundException { super(new File("output.txt")); }
  //  InputReader sc = new InputReader(new FileInputStream("1_08"));
    Main() { super(System.out); }
    InputReader sc = new InputReader(System.in);
    static class InputReader {
        InputReader(InputStream in) { this.in = in; } InputStream in;
        
        private byte[] buf = new byte[16384];
        private int    curChar;
        private int    numChars;
        
   
        public int read() {
            if (numChars == -1)
                throw new InputMismatchException();
            if (curChar >= numChars) {
                curChar = 0;
                try {
                    numChars = in.read(buf);
                } catch (IOException e) {
                    throw new InputMismatchException();
                }
                if (numChars <= 0)
                    return -1;
            }
            return buf[curChar++];
        }
   
        private String nextLine() {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            StringBuilder res = new StringBuilder();
            do {
                res.appendCodePoint(c);
                c = read();
            } while (!isEndOfLine(c));
            return res.toString();
        }
   
        public String nextString() {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            StringBuilder res = new StringBuilder();
            do {
                res.appendCodePoint(c);
                c = read();
            } while (!isSpaceChar(c));
            return res.toString();
        }
   
        public long nextLong() {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            long res = 0;
            do {
                if (c < '0' || c > '9')
                    throw new InputMismatchException();
                res *= 10;
                res += c - '0';
                c = read();
            } while (!isSpaceChar(c));
            return res * sgn;
        }
   
        public int nextInt() {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            int res = 0;
            do {
                if (c < '0' || c > '9')
                    throw new InputMismatchException();
                res *= 10;
                res += c - '0';
                c = read();
            } while (!isSpaceChar(c));
            return res * sgn;
        }
   
        private boolean isSpaceChar(int c) {
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }
   
        private boolean isEndOfLine(int c) {
            return c == '\n' || c == '\r' || c == -1;
        }
    }
   
      public static void main(String[] $) {
          new Thread(null, new Runnable() {
              public void run() {
                  long start = System.nanoTime();
                  try {Main solution = new Main(); solution.solve(); solution.flush();} 
                  catch (Exception e) {e.printStackTrace(); System.exit(1);}
                  System.err.println((System.nanoTime()-start)/1E9);
              }
          }, "1", 1 << 27).start();
   
      }
  }