#!/usr/bin/python3

from random import randint
from sys import setrecursionlimit
setrecursionlimit(10**6+47)

# WA riesenie: neosetruje spravne K < 2

def sef(boss, v):
    if boss[v] == v: return v
    tmp = sef(boss, boss[v])
    boss[v] = tmp
    return tmp

def spoj(boss, u, v):
    bu = sef(boss, u)
    bv = sef(boss, v)
    if bu == bv: return
    if randint(0, 1): bu, bv = bv, bu
    boss[bu] = bv

N, K, M = [ int(_) for _ in input().split() ]
G = [ [] for n in range(N) ]
stupen = [ 0 for n in range(N) ]
boss = [ n for n in range(N) ]
for m in range(M):
    x, y = [ int(_) for _ in input().split() ]
    G[x].append(y)
    G[y].append(x)
    stupen[x] += 1
    stupen[y] += 1
    spoj(boss, x, y)

F = 0
farba = [ None for n in range(N) ]
for n in range(N):
    if boss[n] == n:
        farba[n] = F
        F += 1

for n in range(N):
    farba[n] = farba[ sef(boss, n) ]

komponenty = [ [] for f in range(F) ]
for n in range(N):
    komponenty[ farba[n] ].append(n)

print(F-1)
for f in range(F-1):
    for x in komponenty[f]:
        if stupen[x] <= 1: break
    for y in komponenty[f+1]:
        if stupen[y] <= 1: break
    print(x, y)
    stupen[y] += 1

