#include <bits/stdc++.h>   using namespace std;   char gc() {     static char buf[100000], *p1 = buf, *p2 = buf;     return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2)                ? EOF                : *p1++;   }   inline int read() {     int x = 0, f = 1;     char ch = getchar();     while (ch <  0  || ch >  9 ) {       if (ch ==  - ) f = -1;       ch = getchar();     }     while (ch >=  0  && ch <=  9 )       x = (x << 3) + (x << 1) + ch -  0 , ch = getchar();     return x * f;   }   inline long long readll() {     long long x = 0, f = 1;     char ch = getchar();     while (ch <  0  || ch >  9 ) {       if (ch ==  - ) f = -1;       ch = getchar();     }     while (ch >=  0  && ch <=  9 )       x = (x << 3) + (x << 1) + ch -  0 , ch = getchar();     return x * f;   }   inline void write(int x) {     if (!x) putchar( 0 );     if (x < 0) x = -x, putchar( - );     static int sta[20];     register int tot = 0;     while (x) sta[tot++] = x % 10, x /= 10;     while (tot) putchar(sta[--tot] + 48);   }   const int maxn = 2007, INF = 0x3f3f3f3f;   const int MOD = 998244353;   const long long LINF = 0x3f3f3f3f3f3f3f3fll;   const long long P = 19260817;   inline int mod1(int x) { return x < MOD ? x : x - MOD; }   inline int mod2(int x) { return x < 0 ? x + MOD : x; }   inline void add(int &x, int y) { x = mod1(x + y); }   inline void sub(int &x, int y) { x = mod2(x - y); }   struct node {     int x, y, dis;     friend bool operator<(const node &a, const node &b) {       if (a.dis != b.dis) return a.dis < b.dis;       if (a.x != b.x) return a.x < b.x;       return a.y < b.y;     }   };   inline void chkmin(node &a, node b) { a = min(a, b); }   int n, m, K;   int fal[maxn][maxn];   int far[maxn][maxn];   int fau[maxn][maxn];   int fad[maxn][maxn];   int find(int x, int *fa) { return x == fa[x] ? x : fa[x] = find(fa[x], fa); }   void solve(int x, int y) {     if (fal[x][y] == y) {       printf( %d %d n , x, y);       fal[x][y] = y - 1;       far[x][y] = y + 1;       fau[y][x] = x - 1;       fad[y][x] = x + 1;       return;     }     node ans = (node){0, 0, INF};     for (int i = x; i <= min(x + (int)sqrt(K), n); i++) {       int t = y;       t = find(t, fal[i]);       if (t) chkmin(ans, (node){i, t, abs(i - x) + abs(t - y)});       t = y;       t = find(t, far[i]);       if (t) chkmin(ans, (node){i, t, abs(i - x) + abs(t - y)});       if (y == fal[i][y]) break;     }     for (int i = x; i >= max(x - (int)sqrt(K), 1); i--) {       int t = y;       t = find(t, fal[i]);       if (t) chkmin(ans, (node){i, t, abs(i - x) + abs(t - y)});       t = y;       t = find(t, far[i]);       if (t) chkmin(ans, (node){i, t, abs(i - x) + abs(t - y)});       if (y == fal[i][y]) break;     }     for (int i = y; i <= min(y + (int)sqrt(K), m); i++) {       int t = x;       t = find(t, fau[i]);       if (t) chkmin(ans, (node){t, i, abs(t - x) + abs(i - y)});       t = x;       t = find(t, fad[i]);       if (t) chkmin(ans, (node){t, i, abs(t - x) + abs(i - y)});       if (x == fau[i][x]) break;     }     for (int i = y; i >= max(y - (int)sqrt(K), 1); i--) {       int t = x;       t = find(t, fau[i]);       if (t) chkmin(ans, (node){t, i, abs(t - x) + abs(i - y)});       t = x;       t = find(t, fad[i]);       if (t) chkmin(ans, (node){t, i, abs(t - x) + abs(i - y)});       if (x == fau[i][x]) break;     }     printf( %d %d n , ans.x, ans.y);     fal[ans.x][ans.y] = ans.y - 1;     far[ans.x][ans.y] = ans.y + 1;     fau[ans.y][ans.x] = ans.x - 1;     fad[ans.y][ans.x] = ans.x + 1;   }   int main() {     scanf( %d%d%d , &n, &m, &K);     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= m; j++) {         fal[i][j] = j, far[i][j] = j;         fau[j][i] = i, fad[j][i] = i;       }     }     for (int i = 1, x, y; i <= K; i++) {       scanf( %d%d , &x, &y);       solve(x, y);     }     return 0;   }