using System.Diagnostics;

namespace potrubni_posta;
class Program
{
    static void Main(string[] args)
    {
        string[] parts = Console.ReadLine()!.Split(' ');
        Debug.Assert(parts.Length == 3, "Wrong input - first row should be 3 numbers.");
        int[] intParts = parts.Select(int.Parse).ToArray();
        int n = intParts[0], k = intParts[1], p = intParts[2];

        List<List<int>> edges = new ();
        for(int i = 0; i < n; i++){
            edges.Add(new());
        }

        for(int i = 0; i < p; i++){
            parts = Console.ReadLine()!.Split(' ');
            Debug.Assert(parts.Length == 2, "Wrong input - wrong count of numbers (edges)");
            intParts = parts.Select(int.Parse).ToArray();

            int x = intParts[0], y = intParts[1];
            Debug.Assert(0 <= x && x < n && 0 <= 0 && y < n, "Nodes out of range");
            edges[x].Add(y);
            edges[y].Add(x);

            Debug.Assert(edges[x].Count <= p && edges[x].Count <= p, "More edges to node than permitted");
        }

        int[] nodeColors = new int[n];
        int color = 1;
        for(int i = 0; i < n; i++){
            if(nodeColors[i] != 0) continue;

            nodeColors[i] = color;
            Queue<int> q = new Queue<int>();
            q.Enqueue(i);
            while(q.Count > 0){
                int node = q.Dequeue();
                foreach(int next in edges[node]){
                    if(nodeColors[next] != 0) continue;
                    nodeColors[next] = color;
                    q.Enqueue(next);
                }
            }
            color++;
        }

        List<(int count, int color)> freeEdgesPerColor = new();
        List<List<(int node, int numEdges)>> nodesConnectionsPerColor = new();

        for(int i = 0; i < color; i++){
            freeEdgesPerColor.Add((0, i));
            nodesConnectionsPerColor.Add(new());
        }

        for(int i = 0; i < n; i++){
            int nodeColor = nodeColors[i];
            var x = freeEdgesPerColor[nodeColor];
            int numberFreeEdges = k - edges[i].Count;
            x.count += numberFreeEdges;
            freeEdgesPerColor[nodeColor] = x;

            if(numberFreeEdges == 0) continue;
            nodesConnectionsPerColor[nodeColor].Add((i, edges[i].Count));
        }

        freeEdgesPerColor.Sort((x, y) => -x.count.CompareTo(y.count));

        // foreach(var (count, _color) in freeEdgesPerColor){
        //     Console.WriteLine($"{count} {_color}");
        // }
        // foreach(var connnections in nodesConnectionsPerColor){
        //     Console.WriteLine($"Connection: {connnections.Count}");
        // }

        List<(int node, int numEdges)> freeNodes = new();
        freeNodes.AddRange(nodesConnectionsPerColor[freeEdgesPerColor[0].color]);

        List<(int, int)> output = new();
        for(int i = 1; i < freeEdgesPerColor.Count - 1; i++){
            var (count, _color) = freeEdgesPerColor[i];
            if (freeNodes.Count == 0 || count == 0){
                Console.WriteLine("-1");
                return;
            }

            var node1 = freeNodes.Pop();
            var node2 = nodesConnectionsPerColor[_color].Pop();

            output.Add((node1.node, node2.node));
            node1.numEdges++; node2.numEdges++;

            if (node1.numEdges < k) freeNodes.Add(node1);
            if (node2.numEdges < k) nodesConnectionsPerColor[_color].Add(node2);

            freeNodes.AddRange(nodesConnectionsPerColor[_color]);
        }

        Console.WriteLine(output.Count);
        foreach(var (x, y) in output){
            Console.WriteLine($"{x} {y}");
        }
    }
}

static class ListExtensions{
    public static T Pop<T>(this List<T> list){
        T x = list[^1];
        list.RemoveAt(list.Count - 1);
        return x;
    }
}
