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>
#pragma GCC optimize("O3")
using namespace std;
int n, m, aib[4002][4002], q;
bool pus[2002][2002];
void add(int L, int C) {
  for (int i = L; i <= n + m - 1; i += (i & (-i)))
    for (int j = C; j <= n + m - 1; j += (j & (-j))) aib[i][j]++;
}
int query(int L, int C) {
  int ans = 0;
  for (int i = L; i; i -= (i & (-i)))
    for (int j = C; j; j -= (j & (-j))) ans += aib[i][j];
  return ans;
}
bool tri(int L, int C, int mid) {
  int X = L + C - 1;
  int Y = L - C + m;
  int val = query(min(n + m - 1, X + mid), min(n + m - 1, Y + mid)) -
            query(max(0, X - mid - 1), min(n + m - 1, Y + mid));
  val = val - query(min(n + m - 1, X + mid), max(0, Y - mid - 1)) +
        query(max(0, X - mid - 1), max(0, Y - mid - 1));
  int tot = 0;
  int st = C - mid;
  int dr = C + mid;
  int lstline = max(1, L - mid);
  for (int i = L; i >= lstline; --i) {
    tot += min(m, dr) - max(1, st) + 1;
    ++st;
    --dr;
  }
  st = C - mid + 1;
  dr = C + mid - 1;
  lstline = min(n, L + mid);
  for (int i = L + 1; i <= lstline; ++i) {
    tot += min(m, dr) - max(1, st) + 1;
    ++st;
    --dr;
  }
  return (tot == val);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n >> m >> q;
  for (; q; --q) {
    int L, C;
    cin >> L >> C;
    int st = 0;
    int dr = n + m;
    int ans = 0;
    while (st <= dr) {
      int mid = (st + dr) / 2;
      if (tri(L, C, mid))
        ans = mid + 1, st = mid + 1;
      else
        dr = mid - 1;
    }
    int ansA = 0, ansB = 0;
    int C1 = C;
    int C2 = C;
    for (int first = L - ans; first <= L; ++first) {
      if (first >= 1) {
        if (C1 >= 1 && !pus[first][C1]) {
          pus[first][C1] = 1;
          ansA = first;
          ansB = C1;
          break;
        }
        if (C2 <= m && !pus[first][C2]) {
          pus[first][C2] = 1;
          ansA = first;
          ansB = C2;
          break;
        }
      }
      --C1;
      ++C2;
    }
    C1 = C - ans;
    C2 = C + ans;
    if (ansA == 0)
      for (int first = L; first <= n; ++first) {
        if (first >= 1) {
          if (C1 >= 1 && !pus[first][C1]) {
            pus[first][C1] = 1;
            ansA = first;
            ansB = C1;
            break;
          }
          if (C2 <= m && !pus[first][C2]) {
            pus[first][C2] = 1;
            ansA = first;
            ansB = C2;
            break;
          }
        }
        ++C1;
        --C2;
        if (C1 > C2) break;
      }
    cout << ansA << " " << ansB << '\n';
    add(ansA + ansB - 1, ansA - ansB + m);
  }
  return 0;
}
