The capital of Berland has the only movie theater in the country. Besides, it consists of only one room. The room is divided into n rows, each row consists of m seats.

There are k people lined up to the box office, each person wants to buy exactly one ticket for his own entertainment. Before the box office started selling tickets, each person found the seat that seemed best for him and remembered it as a pair of coordinates (xi, yi), where xi is the row number, and yi is the seat number in this row.

It is possible that some people have chosen the same place, then when some people see their favorite seat taken in the plan of empty seats in the theater, they choose and buy a ticket to another place. Each of them has the following logic: let's assume that he originally wanted to buy a ticket to seat (x1, y1), then when he comes to the box office, he chooses such empty seat (x2, y2), which satisfies the following conditions: 

  * the value of |x1 - x2| + |y1 - y2| is minimum 
  * if the choice is not unique, then among the seats that satisfy the first condition, this person selects the one for which the value of x2 is minimum 
  * if the choice is still not unique, among the seats that satisfy the first and second conditions, this person selects the one for which the value of y2 is minimum 



Your task is to find the coordinates of a seat for each person.

Input

The first input line contains three integers n, m, k (1 ≤ n, m ≤ 2000, 1 ≤ k ≤ min(n·m, 105) — the number of rows in the room, the number of seats in each row and the number of people in the line, correspondingly. Each of the next k lines contains two integers xi, yi (1 ≤ xi ≤ n, 1 ≤ yi ≤ m) — the coordinates of the seat each person has chosen. Numbers on the same line are separated by a space. The pairs of coordinates are located in the order, in which people stand in the line, starting from the head (the first person in the line who stands in front of the box office) to the tail (the last person in the line).

Output

Print k lines, each containing a pair of integers. Print on the i-th line xi, yi — the coordinates of the seat, for which the person who stands i-th in the line will buy the ticket. 

Examples

Input

3 4 6
1 1
1 1
1 1
1 2
1 3
1 3


Output

1 1
1 2
2 1
1 3
1 4
2 3


Input

4 3 12
2 2
2 2
2 2
2 2
2 2
2 2
2 2
2 2
2 2
2 2
2 2
2 2


Output

2 2
1 2
2 1
2 3
3 2
1 1
1 3
3 1
3 3
4 2
4 1
4 3
import java.io.*;
import java.util.*;

import java.util.Arrays;

/**
 * Created by jizhe on 2015/12/21.
 */
public class Cinema {
    public static class FasterScanner {
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;

        public int read() {
            if (numChars == -1)
                throw new InputMismatchException();
            if (curChar >= numChars) {
                curChar = 0;
                try {
                    numChars = System.in.read(buf);
                } catch (IOException e) {
                    throw new InputMismatchException();
                }
                if (numChars <= 0)
                    return -1;
            }
            return buf[curChar++];
        }

        public 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;
        }

        public int[] nextIntArray(int n) {
            int[] arr = new int[n];
            for (int i = 0; i < n; i++) {
                arr[i] = nextInt();
            }
            return arr;
        }

        public long[] nextLongArray(int n) {
            long[] arr = new long[n];
            for (int i = 0; i < n; i++) {
                arr[i] = nextLong();
            }
            return arr;
        }

        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;
        }
    }

    static class InputReader {
        private InputStream stream;
        private byte[] buf = new byte[1000];
        private int curChar;
        private int numChars;

        public InputReader(InputStream stream) {
            this.stream = stream;
        }

        private int read() {
            if (numChars == -1)
                throw new InputMismatchException();
            if (curChar >= numChars) {
                curChar = 0;
                try {
                    numChars = stream.read(buf);
                } catch (IOException e) {
                    throw new InputMismatchException();
                }
                if (numChars <= 0)
                    return -1;
            }
            return buf[curChar++];
        }

        public int readInt() {
            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;
        }
    }

    static class OutputWriter {
        private final PrintWriter writer;

        public OutputWriter(OutputStream outputStream) {
            writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
        }

        public OutputWriter(Writer writer) {
            this.writer = new PrintWriter(writer);
        }

        public void print(Object... objects) {
            for (int i = 0; i < objects.length; i++) {
                if (i != 0)
                    writer.print(' ');
                writer.print(objects[i]);
            }
        }

        public void printLine(Object... objects) {
            print(objects);
            writer.println();
        }

        public void close() {
            writer.close();
        }
    }




    private static boolean[][] marked;
    /*private static int[][] nextSeatRow;
    private static int[][] nextSeatCol;*/

    private static int[][] nextSeatDRow;
    private static int[][] nextSeatDCol;

    /*private static boolean[][] marked = new boolean[2001][2001];
    private static int[][] nextSeatRow = new int[2001][2001];
    private static int[][] nextSeatCol = new int[2001][2001];*/

    private static final int[][] u = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1}};
    private static int[][] dists;

    //private static void getNextSeat(int rRow, int rCol, int cRow, int cCol, int N, int M, StringBuilder out)
    //private static void getNextSeat(int rRow, int rCol, int cRow, int cCol, int N, int M, BufferedWriter out)
    //private static void getNextSeat(int rRow, int rCol, int cRow, int cCol, int N, int M, PrintWriter out)
    //private static void getNextSeat(int rRow, int rCol, int cRow, int cCol, int N, int M, OutputWriter out)
    private static void getNextSeat(int rRow, int rCol, int dRow, int dCol, int N, int M, OutputWriter out)
    {
        int dist = Math.abs(dRow) + Math.abs(dCol);

        while( true ) {
            for (int j = 0; j < 8; j++) {
                int t1 = rRow + u[j][0];
                int t2 = rCol + u[j][1];
                if (0 > t1 || t1 >= N || 0 > t2 || t2 >= M) continue;
                dist = Math.max(dist, dist - (Math.abs(u[j][0]) + Math.abs(u[j][1])));
            }
            for (dRow = -dist; dRow <= dist; dRow++) {
                int row = rRow + dRow;
                dCol = dist - Math.abs(dRow);
                int col = rCol - dCol;

                //System.out.printf("Checking seat: (%d, %d), diff: (%d, %d)\n", row, col, dRow, dCol);

                if (row >= 1 && row <= N && col >= 1 && col <= M && !marked[row][col]) {
                    //System.out.printf("Sell seat: (%d, %d)\n", row, col);
                    marked[row][col] = true;

                    out.printLine(row, col);

                    nextSeatDRow[rRow][rCol] = dRow;
                    nextSeatDCol[rRow][rCol] = dCol;
                    return;
                }

                col = rCol + dCol;
                if (row >= 1 && row <= N && col >= 1 && col <= M && !marked[row][col]) {
                    //System.out.printf("Sell seat: (%d, %d)\n", row, col);
                    marked[row][col] = true;

                    out.printLine(row, col);

                    nextSeatDRow[rRow][rCol] = dRow;
                    nextSeatDCol[rRow][rCol] = dCol;
                    return;
                }
            }
            dist++;
        }

        /*while( true )
        {
            int row = rRow+dRow;
            int col = rCol+dCol;

            //System.out.printf("Checking (%d, %d), diff: (%d, %d)\n", row, col, dRow, dCol);

            if( row >= 1 && row <= N && col >=1 && col <= M && !marked[row][col] ) {
                //System.out.printf("Sell seat: (%d, %d)\n", row, col);
                marked[row][col] = true;

                //out.append(row+" "+col+"\n");
                *//*try {
                    out.write(row + " " + col + "\n");
                }catch (IOException e)
                {
                    e.printStackTrace();
                }*//*
                //out.println(row + " " + col);
                out.printLine(row, col);

                nextSeatRow[rRow][rCol] = row;
                nextSeatCol[rRow][rCol] = col;
                return;
            }

            if( dRow == dist )
            {
                dist++;
                dRow = -dist;
                dCol = 0;
            }
            else if( dCol >= 0 )
            {
                dRow++;
                dCol = -dist+Math.abs(dRow);
            }
            else
            {
                dCol = -dCol;
            }
        }*/
    }

    private static void run2()
    {
        //Scanner in = new Scanner(new BufferedInputStream(System.in));
        //FasterScanner in = new FasterScanner();
        InputReader in = new InputReader(System.in);

        /*int N = in.nextInt();
        int M = in.nextInt();
        int K = in.nextInt();*/

        int N = in.readInt();
        int M = in.readInt();
        int K = in.readInt();

        /*nextSeatRow = new int[N+1][M+1];
        nextSeatCol = new int[N+1][M+1];*/
        /*nextSeatDRow = new int[N+1][M+1];
        nextSeatDCol = new int[N+1][M+1];*/
        marked = new boolean[N+1][M+1];
        dists = new int[N+1][M+1];
        for (int i = 0; i < N+1; i++) {
            /*Arrays.fill(nextSeatDRow[i], 0);
            Arrays.fill(nextSeatDCol[i], 0);*/
            Arrays.fill(dists[i], 0);
            Arrays.fill(marked[i], false);
        }

        //StringBuilder out = new StringBuilder();
        //BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out));
        //PrintWriter out = new PrintWriter(System.out);
        OutputWriter out = new OutputWriter(System.out);

        for( int i = 0; i < K; i++ )
        {
            /*int rRow = in.nextInt();
            int rCol = in.nextInt();*/

            int rRow = in.readInt();
            int rCol = in.readInt();

            //getNextSeat(rRow, rCol, nextSeatDRow[rRow][rCol], nextSeatDCol[rRow][rCol], N, M, out);


            /*int dRow = nextSeatDRow[rRow][rCol];
            int dCol = nextSeatDCol[rRow][rCol];
            int dist = Math.abs(dRow) + Math.abs(dCol);*/

            int dRow, dCol;
            int dist = dists[rRow][rCol];

            for (int j = 0; j < 8; j++) {
                int t1 = rRow + u[j][0];
                int t2 = rCol + u[j][1];
                if (0 >= t1 || t1 >= N+1 || 0 >= t2 || t2 >= M+1) continue;
                dist = Math.max(dist, dists[t1][t2] - (Math.abs(u[j][0]) + Math.abs(u[j][1])));
            }

            while( true ) {
                boolean flag = false;

                for (dRow = -dist; dRow <= dist; dRow++) {
                    int row = rRow + dRow;
                    dCol = dist - Math.abs(dRow);
                    int col = rCol - dCol;

                    //System.out.printf("Checking seat: (%d, %d), diff: (%d, %d)\n", row, col, dRow, dCol);

                    if (row >= 1 && row <= N && col >= 1 && col <= M && !marked[row][col]) {
                        //System.out.printf("Sell seat: (%d, %d)\n", row, col);
                        marked[row][col] = true;

                        out.printLine(row, col);

                        /*nextSeatDRow[rRow][rCol] = dRow;
                        nextSeatDCol[rRow][rCol] = dCol;*/
                        flag = true;
                        break;
                    }

                    col = rCol + dCol;
                    if (row >= 1 && row <= N && col >= 1 && col <= M && !marked[row][col]) {
                        //System.out.printf("Sell seat: (%d, %d)\n", row, col);
                        marked[row][col] = true;

                        out.printLine(row, col);

                        /*nextSeatDRow[rRow][rCol] = dRow;
                        nextSeatDCol[rRow][rCol] = dCol;*/
                        flag = true;
                        break;
                    }
                }
                if( flag )
                {
                    break;
                }
                dist++;
            }
            dists[rRow][rCol] = dist;
        }

        //System.out.print(out.toString());
        /*try {
            out.flush();
        }catch (IOException e)
        {
            e.printStackTrace();
        }*/
        out.close();

    }

    public static void main(String[] args) {
        run2();
        //run3();
    }

}
