#include <iostream>
#include <vector>
using namespace std;

const int INF = 123456789;

int main() {
    int N, K, M;
    cin >> N >> K >> M;
    vector< vector<int> > G(N, vector<int>(N, INF));
    for (int n=0; n<N; ++n) G[n][n] = 0;
    vector<int> stupen(N, 0);
    for (int m=0; m<M; ++m) {
        int x, y;
        cin >> x >> y;
        G[x][y] = G[y][x] = 1;
        ++stupen[x];
        ++stupen[y];
    }

    if (K == 0) { cout << (N == 1 ? 0 : -1) << endl; return 0; }
    if (K == 1 && N == 2 && M == 0) { cout << 1 << endl << "0 1" << endl; return 0; }
    if (K == 1) { cout << (N <= 2 ? 0 : -1) << endl; return 0; }

    for (int k=0; k<N; ++k) for (int i=0; i<N; ++i) for (int j=0; j<N; ++j) G[i][j] = min( G[i][j], G[i][k]+G[k][j] );

    vector<bool> ofarbene(N, false);
    vector< vector<int> > komponenty;
    for (int n=0; n<N; ++n) if (!ofarbene[n]) {
        vector<int> curr;
        for (int i=0; i<N; ++i) if (G[n][i] < INF) { curr.push_back(i); ofarbene[i] = true; }
        komponenty.push_back(curr);
    }
    
    cout << komponenty.size()-1 << endl;
    for (int k=0; k+1<int(komponenty.size()); ++k) {
        int x = -1, y = -1;
        for (int xx : komponenty[k]) if (stupen[xx] < K) { x = xx; break; }
        for (int yy : komponenty[k+1]) if (stupen[yy] < K) { y = yy; break; }
        cout << x << " " << y << endl;
        ++stupen[y];
    }
}
