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
#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline T qmin(const T &a, const T &b) {
  return a < b ? a : b;
}
template <typename T>
inline T qmax(const T &a, const T &b) {
  return a > b ? a : b;
}
template <typename T>
inline void getmin(T &a, const T &b) {
  if (a > b) a = b;
}
template <typename T>
inline void getmax(T &a, const T &b) {
  if (a < b) a = b;
}
inline void fileio(string s) {
  freopen((s + ".in").c_str(), "r", stdin);
  freopen((s + ".out").c_str(), "w", stdout);
}
const int inf = (int)1e9 + 7;
const long long linf = (long long)1e17 + 7;
const int N = 1e5 + 7, S = 2005;
int pre[S][S], nxt[S][S];
int n, m, k;
inline int findpre(int i, int j) {
  if (j < 1) return -inf;
  if (pre[i][j] == -1) return j;
  return pre[i][j] = findpre(i, pre[i][j]);
}
inline int findsuc(int i, int j) {
  if (j > m) return inf;
  if (nxt[i][j] == -1) return j;
  return nxt[i][j] = findsuc(i, nxt[i][j]);
}
inline void solve1() {
  for (register int i = (1); i <= (n); ++i)
    for (register int j = (1); j <= (m); ++j) pre[i][j] = nxt[i][j] = -1;
  while (k--) {
    int x, y;
    scanf("%d%d", &x, &y);
    int res = n + m, resx, resy;
    for (register int i = (x); i >= (1); --i) {
      if (x - i > res) break;
      int l = findpre(i, y), r = findsuc(i, y);
      if (qmin(y - l, r - y) + x - i > res) continue;
      if (y - l <= r - y) {
        res = y - l + x - i;
        resx = i, resy = l;
      } else {
        res = r - y + x - i;
        resx = i, resy = r;
      }
    }
    for (register int i = (x + 1); i <= (n); ++i) {
      if (i - x > res) break;
      int l = findpre(i, y), r = findsuc(i, y);
      if (qmin(y - l, r - y) + i - x >= res) continue;
      if (y - l <= r - y) {
        res = y - l + i - x;
        resx = i, resy = l;
      } else {
        res = r - y + i - x;
        resx = i, resy = r;
      }
    }
    pre[resx][resy] = resy - 1;
    nxt[resx][resy] = resy + 1;
    printf("%d %d\n", resx, resy);
  }
}
inline void solve2() {
  swap(n, m);
  for (register int i = (1); i <= (n); ++i)
    for (register int j = (1); j <= (m); ++j) pre[i][j] = nxt[i][j] = -1;
  while (k--) {
    int x, y;
    scanf("%d%d", &x, &y);
    swap(x, y);
    int res = n + m, resx = inf, resy = inf;
    for (register int i = (x); i >= (1); --i) {
      if (x - i > res) break;
      int l = findpre(i, y), r = findsuc(i, y);
      if (y - l + x - i < res || y - l + x - i == res && l <= resy) {
        res = y - l + x - i;
        resx = i, resy = l;
      }
      if (r - y + x - i < res || r - y + x - i == res && r <= resy) {
        res = r - y + x - i;
        resx = i, resy = r;
      }
    }
    for (register int i = (x + 1); i <= (n); ++i) {
      if (i - x > res) break;
      int l = findpre(i, y), r = findsuc(i, y);
      if (y - l + i - x < res || y - l + i - x == res && l < resy) {
        res = y - l + i - x;
        resx = i, resy = l;
      }
      if (r - y + i - x < res || r - y + i - x == res && r < resy) {
        res = r - y + i - x;
        resx = i, resy = r;
      }
    }
    pre[resx][resy] = resy - 1;
    nxt[resx][resy] = resy + 1;
    printf("%d %d\n", resy, resx);
  }
}
int main() {
  scanf("%d%d%d", &n, &m, &k);
  if (n <= m) {
    solve1();
  } else {
    solve2();
  }
  return 0;
}
