#include <bits/stdc++.h>   using namespace std;   typedef pair<int, int> pii;   typedef long long ll;   typedef unsigned long long ull;   template <typename T>   bool umax(T& a, const T& b) {     return b <= a ? false : (a = b, true);   }   template <typename T>   bool umin(T& a, const T& b) {     return b >= a ? false : (a = b, true);   }   template <typename T>   void V2A(T a[], const vector<T>& b) {     for (int i = 0; i < b.size(); i++) a[i] = b[i];   }   template <typename T>   void A2V(vector<T>& a, const T b[]) {     for (int i = 0; i < a.size(); i++) a[i] = b[i];   }   const double PI = acos(-1.0);   const int INF = 1e9 + 7;   const int maxn = 2e3 + 7;   bool plot[maxn][maxn];   int r[maxn][maxn];   int n, m, k;   int check(int row, int col) {     if (col >= 0 && col < m) return true;     return false;   }   int main() {     cin >> n >> m >> k;     for (int i = 0; i < k; i++) {       int x, y;       scanf( %d%d , &x, &y);       x--;       y--;       if (!plot[x][y]) {         printf( %d %d n , x + 1, y + 1);         plot[x][y] = true;         continue;       }       for (int d = 1; d <= 3; d++) {         int Max = min(n, x + d + 1);         for (int row = max(0, x - d); row < Max; row++) {           int t = d - abs(x - row), col1 = y - t, col2 = y + t;           if (check(row, col1)) umax(r[x][y], r[row][col1] - d);           if (check(row, col2)) umax(r[x][y], r[row][col2] - d);         }       }       for (int d = r[x][y] + 1;; d++) {         int Max = min(n, x + d + 1);         bool ok = false;         for (int row = max(0, x - d); row < Max; row++) {           int t = d - abs(x - row), col1 = y - t, col2 = y + t;           if (check(row, col1) && !plot[row][col1]) {             ok = true;             printf( %d %d n , row + 1, col1 + 1);             plot[row][col1] = true;             break;           }           if (check(row, col2) && !plot[row][col2]) {             ok = true;             printf( %d %d n , row + 1, col2 + 1);             plot[row][col2] = true;             break;           }         }         if (ok) {           r[x][y] = d - 1;           break;         }       }     }     return 0;   }   