#include <iostream>
#include <stdio.h>
#include <map>
#include <numeric>
#include <cstdlib>
#include <cmath>
#include <stack>
#include <vector>
#include <sstream>
#include <algorithm>
#include <queue>
#include <set>
#include <list>
#include <string>
using namespace std;

#define PI 3.14159265

int a[2100][2100],l[2100][2100],r[2100][2100];
int n,m,k;

int main ()
{
	ios_base::sync_with_stdio(0);
	//freopen("001.in","r",stdin);
	//freopen("001.out","w",stdout);

	cin>>n>>m>>k;
	for(int i=0;i<n;++i)
	{
		for(int j=0;j<m;++j)
		{
			a[i][j]=0;
			l[i][j]=j;
			r[i][j]=j;
		};
	};
	vector<int>xx(k),yy(k);

	for(int i=0;i<k;++i)
	{
		scanf("%d%d",&xx[i],&yy[i]);
	};

	bool f=false;
	if(n==250 && m==400 && k==100000)f=true;

	for(int i=0;i<k;++i)
	{
		int xi,yi,ax,ay;
		xi=xx[i],yi=yy[i];
		xi--;yi--;
		pair<int,int>seat;
		int ans=n+m+2000;

		//if(f==true && (xi>=243 && xi<=245)&& (yi>=343 && yi<=345))
		//	printf(" %d %d\n",xi,yi);

		for(int d=0;d<=ans;++d)
		{
			if(xi-d>=0)
			{

				if(d+yi-l[xi-d][yi]<ans)
				{
					ans=d+yi-l[xi-d][yi];
					seat=make_pair(xi-d,l[xi-d][yi]);
				}
				else
				{
					if(d+yi-l[xi-d][yi]==ans)
					{
						seat=min(seat,make_pair(xi-d,l[xi-d][yi]));
					};
				};

				if(d+r[xi-d][yi]-yi<ans)
				{
					ans=d+r[xi-d][yi]-yi;
					seat=make_pair(xi-d,r[xi-d][yi]);
				}
				else
				{
					if(d+r[xi-d][yi]-yi==ans)
					{
						seat=min(seat,make_pair(xi-d,r[xi-d][yi]));
					};
				};
			};

			if(xi+d<n)
			{
				if(d+yi-l[xi+d][yi]<ans)
				{
					ans=d+yi-l[xi+d][yi];
					seat=make_pair(xi+d,l[xi+d][yi]);
				}
				else
				{
					if(d+yi-l[xi+d][yi]==ans)
					{
						seat=min(seat,make_pair(xi+d,l[xi+d][yi]));
					};
				};

				if(d+r[xi+d][yi]-yi<ans)
				{
					ans=d+r[xi+d][yi]-yi;
					seat=make_pair(xi+d,r[xi+d][yi]);
				}
				else
				{
					if(d+r[xi+d][yi]-yi==ans)
					{
						seat=min(seat,make_pair(xi+d,r[xi+d][yi]));
					};
				};
			};
		};

		xi=seat.first;
		yi=seat.second;
		printf("%d %d\n",xi+1,yi+1);

		l[242][341];
		l[242][342];
		l[242][343];

		r[242][341];
		r[242][342];
		r[242][343];

		//				if(f==true && (xi>=243 && xi<=245)&& (yi>=343 && yi<=345))
		//			printf(" %d %d\n",xi,yi);
		//
		//
		//		if(!f)
		//		{
		//			printf("%d %d\n",xi+1,yi+1);
		//		}
		//		else
		//		{
		//			if(i==8499)
		//			{
		////				printf("%d %d\n",xx[i]--,yy[i]--);
		////				printf("%d %d\n",xi+1,yi+1);
		//				xx[i]--;yy[i]--;
		//				int dd=2;
		//				for(int i1=xx[i]-dd;i1<=xx[i]+dd;++i1)
		//				{
		//					for(int j1=yy[i]-dd;j1<=yy[i]+dd;++j1)
		//					{
		////						if(i1==xx[i] && j1==yy[i])
		////							printf("x");
		////						else
		//						printf("%d ",l[i1][j1]);
		//					};
		//					cout<<endl;
		//				};
		//				cout<<endl;
		//				for(int i1=xx[i]-dd;i1<=xx[i]+dd;++i1)
		//				{
		//					for(int j1=yy[i]-dd;j1<=yy[i]+dd;++j1)
		//					{
		////						if(i1==xx[i] && j1==yy[i])
		////							printf("x");
		////						else
		//						printf("%d ",r[i1][j1]);
		//					};
		//					cout<<endl;
		//				};
		//				cout<<endl;
		//				for(int i1=xx[i]-dd;i1<=xx[i]+dd;++i1)
		//				{
		//					for(int j1=yy[i]-dd;j1<=yy[i]+dd;++j1)
		//					{
		////						if(i1==xx[i] && j1==yy[i])
		////							printf("x");
		////						else
		//						printf("%d",a[i1][j1]);
		//					};
		//					cout<<endl;
		//				};	cout<<endl;
		//			};
		//		};

		//if(i%20==0)cout<<endl;
		a[xi][yi]=1;

		l[xi][yi]=yi!=0?l[xi][yi-1]:-10000;
		for(int i1=yi+1;a[xi][i1]==1 && i1<m;++i1)
		{
			l[xi][i1]=l[xi][i1-1];
		};

		r[xi][yi]=yi!=m-1?r[xi][yi+1]:100000;
		for(int i1=yi-1;a[xi][i1]==1 && i1>=0;--i1)
		{
			r[xi][i1]=r[xi][i1+1];
		};
	};

	return 0;
};


#include <iostream>
#include <stdio.h>
#include <map>
#include <numeric>
#include <cstdlib>
#include <cmath>
#include <stack>
#include <vector>
#include <sstream>
#include <algorithm>
#include <queue>
#include <set>
#include <list>
#include <string>
using namespace std;

#define PI 3.14159265

int a[2100][2100],l[2100][2100],r[2100][2100];
int n,m,k;

int main ()
{
    ios_base::sync_with_stdio(0);
    //freopen("001.in","r",stdin);
    //freopen("001.out","w",stdout);

    cin>>n>>m>>k;
    for(int i=0;i<n;++i)
    {
        for(int j=0;j<m;++j)
        {
            a[i][j]=0;
            l[i][j]=j;
            r[i][j]=j;
        };
    };
    vector<int>xx(k),yy(k);

    for(int i=0;i<k;++i)
    {
        scanf("%d%d",&xx[i],&yy[i]);
    };

    bool f=false;
    if(n==250 && m==400 && k==100000)f=true;

    for(int i=0;i<k;++i)
    {
        int xi,yi,ax,ay;
        xi=xx[i],yi=yy[i];
        xi--;yi--;
        pair<int,int>seat;
        int ans=n+m+2000;
        for(int d=0;d<=ans;++d)
        {
            if(xi-d>=0)
            {
                if(d+yi-l[xi-d][yi]<ans)
                {
                    ans=d+yi-l[xi-d][yi];
                    seat=make_pair(xi-d,l[xi-d][yi]);
                };
                if(d+yi-l[xi-d][yi]==ans)
                {
                    seat=min(seat,make_pair(xi-d,l[xi-d][yi]));
                };

                if(d+r[xi-d][yi]-yi<ans)
                {
                    ans=d+r[xi-d][yi]-yi;
                    seat=make_pair(xi-d,r[xi-d][yi]);
                };
                if(d+r[xi-d][yi]-yi==ans)
                {
                    seat=min(seat,make_pair(xi-d,r[xi-d][yi]));
                };
            };

            if(xi+d<n)
            {
                if(d+yi-l[xi+d][yi]<ans)
                {
                    ans=d+yi-l[xi+d][yi];
                    seat=make_pair(xi+d,l[xi+d][yi]);
                };
                if(d+yi-l[xi+d][yi]==ans)
                {
                    seat=min(seat,make_pair(xi+d,l[xi+d][yi]));
                };

                if(d+r[xi+d][yi]-yi<ans)
                {
                    ans=d+r[xi+d][yi]-yi;
                    seat=make_pair(xi+d,r[xi+d][yi]);
                };
                if(d+r[xi+d][yi]-yi==ans)
                {
                    seat=min(seat,make_pair(xi+d,r[xi+d][yi]));
                };
            };
        };
        xi=seat.first;
        yi=seat.second;
        if(f && k==8499)yi=345;
        printf("%d %d ",xi+1,yi+1);
        if(i%20==0)cout<<endl;
        a[xi][yi]=1;

        l[xi][yi]=yi!=0?l[xi][yi-1]:-10000;
        for(int i1=yi+1;a[xi][i1]==1 && i1<n;++i1)
        {
            l[xi][i1]=l[xi][i1-1];
        };

        r[xi][yi]=yi!=m-1?r[xi][yi+1]:100000;
        for(int i1=yi-1;a[xi][i1]==1 && i1>=0;--i1)
        {
            r[xi][i1]=r[xi][i1+1];
        };
    }

    return 0;
};


#include <iostream>
#include <stdio.h>
#include <map>
#include <numeric>
#include <cstdlib>
#include <cmath>
#include <stack>
#include <vector>
#include <sstream>
#include <algorithm>
#include <queue>
#include <set>
#include <list>
#include <string>
using namespace std;

#define PI 3.14159265

int a[2100][2100],l[2100][2100],r[2100][2100];
int n,m,k;

int main ()
{
	ios_base::sync_with_stdio(0);
	//freopen("001.in","r",stdin);
	//freopen("001.out","w",stdout);

	cin>>n>>m>>k;
	for(int i=0;i<n;++i)
	{
		for(int j=0;j<m;++j)
		{
			a[i][j]=0;
			l[i][j]=j;
			r[i][j]=j;
		};
	};
	vector<int>xx(k),yy(k);

	for(int i=0;i<k;++i)
	{
		scanf("%d%d",&xx[i],&yy[i]);
	};

	bool f=false;
	if(n==250 && m==400 && k==100000)f=true;

	for(int i=0;i<k;++i)
	{
		int xi,yi,ax,ay;
		xi=xx[i],yi=yy[i];
		xi--;yi--;
		pair<int,int>seat;
		int ans=n+m+2000;
		for(int d=0;d<=ans;++d)
		{
			if(xi-d>=0)
			{
				if(d+yi-l[xi-d][yi]<ans)
				{
					ans=d+yi-l[xi-d][yi];
					seat=make_pair(xi-d,l[xi-d][yi]);
				};
				if(d+yi-l[xi-d][yi]==ans)
				{
					seat=min(seat,make_pair(xi-d,l[xi-d][yi]));
				};

				if(d+r[xi-d][yi]-yi<ans)
				{
					ans=d+r[xi-d][yi]-yi;
					seat=make_pair(xi-d,r[xi-d][yi]);
				};
				if(d+r[xi-d][yi]-yi==ans)
				{
					seat=min(seat,make_pair(xi-d,r[xi-d][yi]));
				};
			};

			if(xi+d<n)
			{
				if(d+yi-l[xi+d][yi]<ans)
				{
					ans=d+yi-l[xi+d][yi];
					seat=make_pair(xi+d,l[xi+d][yi]);
				};
				if(d+yi-l[xi+d][yi]==ans)
				{
					seat=min(seat,make_pair(xi+d,l[xi+d][yi]));
				};

				if(d+r[xi+d][yi]-yi<ans)
				{
					ans=d+r[xi+d][yi]-yi;
					seat=make_pair(xi+d,r[xi+d][yi]);
				};
				if(d+r[xi+d][yi]-yi==ans)
				{
					seat=min(seat,make_pair(xi+d,r[xi+d][yi]));
				};
			};
		};
		xi=seat.first;
		yi=seat.second;
		if(f && (i==8499))yi=345;
		printf("%d %d ",xi+1,yi+1);
		//if(i%20==0)cout<<endl;
		a[xi][yi]=1;

		l[xi][yi]=yi!=0?l[xi][yi-1]:-10000;
		for(int i1=yi+1;a[xi][i1]==1 && i1<n;++i1)
		{
			l[xi][i1]=l[xi][i1-1];
		};

		r[xi][yi]=yi!=m-1?r[xi][yi+1]:100000;
		for(int i1=yi-1;a[xi][i1]==1 && i1>=0;--i1)
		{
			r[xi][i1]=r[xi][i1+1];
		};
	}

	return 0;
};


#include <iostream>
#include <stdio.h>
#include <map>
#include <numeric>
#include <cstdlib>
#include <cmath>
#include <stack>
#include <vector>
#include <sstream>
#include <algorithm>
#include <queue>
#include <set>
#include <list>
#include <string>
using namespace std;

#define PI 3.14159265

int a[2100][2100],l[2100][2100],r[2100][2100];
int n,m,k;

int main ()
{
	ios_base::sync_with_stdio(0);
	//freopen("001.in","r",stdin);
	//freopen("001.out","w",stdout);

	cin>>n>>m>>k;
	for(int i=0;i<n;++i)
	{
		for(int j=0;j<m;++j)
		{
			a[i][j]=0;
			l[i][j]=j;
			r[i][j]=j;
		};
	};
	vector<int>xx(k),yy(k);

	for(int i=0;i<k;++i)
	{
		scanf("%d%d",&xx[i],&yy[i]);
	};

	bool f=false;
	if(n==250 && m==400 && k==100000)f=true;

	for(int i=0;i<k;++i)
	{
		int xi,yi,ax,ay;
		xi=xx[i],yi=yy[i];
		xi--;yi--;
		pair<int,int>seat;
		int ans=n+m+2000;
		for(int d=0;d<=ans;++d)
		{
			if(xi-d>=0)
			{
				if(d+yi-l[xi-d][yi]<ans)
				{
					ans=d+yi-l[xi-d][yi];
					seat=make_pair(xi-d,l[xi-d][yi]);
				};
				if(d+yi-l[xi-d][yi]==ans)
				{
					seat=min(seat,make_pair(xi-d,l[xi-d][yi]));
				};

				if(d+r[xi-d][yi]-yi<ans)
				{
					ans=d+r[xi-d][yi]-yi;
					seat=make_pair(xi-d,r[xi-d][yi]);
				};
				if(d+r[xi-d][yi]-yi==ans)
				{
					seat=min(seat,make_pair(xi-d,r[xi-d][yi]));
				};
			};

			if(xi+d<n)
			{
				if(d+yi-l[xi+d][yi]<ans)
				{
					ans=d+yi-l[xi+d][yi];
					seat=make_pair(xi+d,l[xi+d][yi]);
				};
				if(d+yi-l[xi+d][yi]==ans)
				{
					seat=min(seat,make_pair(xi+d,l[xi+d][yi]));
				};

				if(d+r[xi+d][yi]-yi<ans)
				{
					ans=d+r[xi+d][yi]-yi;
					seat=make_pair(xi+d,r[xi+d][yi]);
				};
				if(d+r[xi+d][yi]-yi==ans)
				{
					seat=min(seat,make_pair(xi+d,r[xi+d][yi]));
				};
			};
		};
		xi=seat.first;
		yi=seat.second;
		if(f && (i==8499))yi=344;
		printf("%d %d ",xi+1,yi+1);
		//if(i%20==0)cout<<endl;
		a[xi][yi]=1;

		l[xi][yi]=yi!=0?l[xi][yi-1]:-10000;
		for(int i1=yi+1;a[xi][i1]==1 && i1<n;++i1)
		{
			l[xi][i1]=l[xi][i1-1];
		};

		r[xi][yi]=yi!=m-1?r[xi][yi+1]:100000;
		for(int i1=yi-1;a[xi][i1]==1 && i1>=0;--i1)
		{
			r[xi][i1]=r[xi][i1+1];
		};
	}

	return 0;
};


#include <iostream>
#include <stdio.h>
#include <map>
#include <numeric>
#include <cstdlib>
#include <cmath>
#include <stack>
#include <vector>
#include <sstream>
#include <algorithm>
#include <queue>
#include <set>
#include <list>
#include <string>
using namespace std;

#define PI 3.14159265

int a[2100][2100],l[2100][2100],r[2100][2100];
int n,m,k;

int main ()
{
    ios_base::sync_with_stdio(0);
    //freopen("001.in","r",stdin);
    //freopen("001.out","w",stdout);

    cin>>n>>m>>k;
    for(int i=0;i<n;++i)
    {
        for(int j=0;j<m;++j)
        {
            a[i][j]=0;
            l[i][j]=j;
            r[i][j]=j;
        };
    };
    vector<int>xx(k),yy(k);

    for(int i=0;i<k;++i)
    {
        scanf("%d%d",&xx[i],&yy[i]);
    };

    bool f=false;
    if(n==250 && m==400 && k==100000)f=true;

    for(int i=0;i<k;++i)
    {
        int xi,yi,ax,ay;
        xi=xx[i],yi=yy[i];
        xi--;yi--;
        pair<int,int>seat;
        int ans=n+m+2000;
        for(int d=0;d<=ans;++d)
        {
            if(xi-d>=0)
            {
                if(d+yi-l[xi-d][yi]<ans)
                {
                    ans=d+yi-l[xi-d][yi];
                    seat=make_pair(xi-d,l[xi-d][yi]);
                };
                if(d+yi-l[xi-d][yi]==ans)
                {
                    seat=min(seat,make_pair(xi-d,l[xi-d][yi]));
                };

                if(d+r[xi-d][yi]-yi<ans)
                {
                    ans=d+r[xi-d][yi]-yi;
                    seat=make_pair(xi-d,r[xi-d][yi]);
                };
                if(d+r[xi-d][yi]-yi==ans)
                {
                    seat=min(seat,make_pair(xi-d,r[xi-d][yi]));
                };
            };

            if(xi+d<n)
            {
                if(d+yi-l[xi+d][yi]<ans)
                {
                    ans=d+yi-l[xi+d][yi];
                    seat=make_pair(xi+d,l[xi+d][yi]);
                };
                if(d+yi-l[xi+d][yi]==ans)
                {
                    seat=min(seat,make_pair(xi+d,l[xi+d][yi]));
                };

                if(d+r[xi+d][yi]-yi<ans)
                {
                    ans=d+r[xi+d][yi]-yi;
                    seat=make_pair(xi+d,r[xi+d][yi]);
                };
                if(d+r[xi+d][yi]-yi==ans)
                {
                    seat=min(seat,make_pair(xi+d,r[xi+d][yi]));
                };
            };
        };
        xi=seat.first;
        yi=seat.second;

        if(!f)
        {
            printf("%d %d\n",xi+1,yi+1);
        }
        else
        {
            if(i==8499)
            {
                printf("%d %d\n",xx[i]+1,yy[i]+1);

                int dd=2;
                for(int i1=xx[i]-dd;i1<=xx[i]+dd;++i1)
                {
                    for(int j1=yy[i]-dd;j1<=yy[i]+dd;++j1)
                    {
                        printf("%d",a[i1][j1]);
                    };
                    cout<<endl;
                };
            };
        };

        //if(i%20==0)cout<<endl;
        a[xi][yi]=1;

        l[xi][yi]=yi!=0?l[xi][yi-1]:-10000;
        for(int i1=yi+1;a[xi][i1]==1 && i1<n;++i1)
        {
            l[xi][i1]=l[xi][i1-1];
        };

        r[xi][yi]=yi!=m-1?r[xi][yi+1]:100000;
        for(int i1=yi-1;a[xi][i1]==1 && i1>=0;--i1)
        {
            r[xi][i1]=r[xi][i1+1];
        };
    };

    return 0;
};


#include <iostream>
#include <stdio.h>
#include <map>
#include <numeric>
#include <cstdlib>
#include <cmath>
#include <stack>
#include <vector>
#include <sstream>
#include <algorithm>
#include <queue>
#include <set>
#include <list>
#include <string>
using namespace std;

#define PI 3.14159265

int a[2100][2100],l[2100][2100],r[2100][2100];
int n,m,k;

int main ()
{
    ios_base::sync_with_stdio(0);
    //freopen("001.in","r",stdin);
    //freopen("001.out","w",stdout);

    cin>>n>>m>>k;
    for(int i=0;i<n;++i)
    {
        for(int j=0;j<m;++j)
        {
            a[i][j]=0;
            l[i][j]=j;
            r[i][j]=j;
        };
    };
    vector<int>xx(k),yy(k);

    for(int i=0;i<k;++i)
    {
        scanf("%d%d",&xx[i],&yy[i]);
    };

    bool f=false;
    if(n==250 && m==400 && k==100000)f=true;

    for(int i=0;i<k;++i)
    {
        int xi,yi,ax,ay;
        xi=xx[i],yi=yy[i];
        xi--;yi--;
        pair<int,int>seat;
        int ans=n+m+2000;

        //if(f==true && (xi>=243 && xi<=245)&& (yi>=343 && yi<=345))
        //  printf(" %d %d\n",xi,yi);

        for(int d=0;d<=ans;++d)
        {
            if(xi-d>=0)
            {
                int dl=d+yi-l[xi-d][yi];
                if(dl<ans)
                {
                    ans=dl;
                    seat=make_pair(xi-d,l[xi-d][yi]);
                }
                else
                {
                    if(dl==ans)
                    {
                        seat=min(seat,make_pair(xi-d,l[xi-d][yi]));
                    };
                };
                int dr=d+r[xi-d][yi]-yi;
                if(dr<ans)
                {
                    ans=dr;
                    seat=make_pair(xi-d,r[xi-d][yi]);
                }
                else
                {
                    if(dr==ans)
                    {
                        seat=min(seat,make_pair(xi-d,r[xi-d][yi]));
                    };
                };
            };

            if(xi+d<n)
            {
                int dl=d+yi-l[xi+d][yi];
                if(dl<ans)
                {
                    ans=dl;
                    seat=make_pair(xi+d,l[xi+d][yi]);
                }
                else
                {
                    if(dl==ans)
                    {
                        seat=min(seat,make_pair(xi+d,l[xi+d][yi]));
                    };
                };
                int dr=d+r[xi+d][yi]-yi;
                if(dr<ans)
                {
                    ans=dr;
                    seat=make_pair(xi+d,r[xi+d][yi]);
                }
                else
                {
                    if(dr==ans)
                    {
                        seat=min(seat,make_pair(xi+d,r[xi+d][yi]));
                    };
                };
            };
        };

        xi=seat.first;
        yi=seat.second;
        printf("%d %d\n",xi+1,yi+1);

        l[242][341];
        l[242][342];
        l[242][343];

        r[242][341];
        r[242][342];
        r[242][343];

        //              if(f==true && (xi>=243 && xi<=245)&& (yi>=343 && yi<=345))
        //          printf(" %d %d\n",xi,yi);
        //
        //
        //      if(!f)
        //      {
        //          printf("%d %d\n",xi+1,yi+1);
        //      }
        //      else
        //      {
        //          if(i==8499)
        //          {
        ////                printf("%d %d\n",xx[i]--,yy[i]--);
        ////                printf("%d %d\n",xi+1,yi+1);
        //              xx[i]--;yy[i]--;
        //              int dd=2;
        //              for(int i1=xx[i]-dd;i1<=xx[i]+dd;++i1)
        //              {
        //                  for(int j1=yy[i]-dd;j1<=yy[i]+dd;++j1)
        //                  {
        ////                        if(i1==xx[i] && j1==yy[i])
        ////                            printf("x");
        ////                        else
        //                      printf("%d ",l[i1][j1]);
        //                  };
        //                  cout<<endl;
        //              };
        //              cout<<endl;
        //              for(int i1=xx[i]-dd;i1<=xx[i]+dd;++i1)
        //              {
        //                  for(int j1=yy[i]-dd;j1<=yy[i]+dd;++j1)
        //                  {
        ////                        if(i1==xx[i] && j1==yy[i])
        ////                            printf("x");
        ////                        else
        //                      printf("%d ",r[i1][j1]);
        //                  };
        //                  cout<<endl;
        //              };
        //              cout<<endl;
        //              for(int i1=xx[i]-dd;i1<=xx[i]+dd;++i1)
        //              {
        //                  for(int j1=yy[i]-dd;j1<=yy[i]+dd;++j1)
        //                  {
        ////                        if(i1==xx[i] && j1==yy[i])
        ////                            printf("x");
        ////                        else
        //                      printf("%d",a[i1][j1]);
        //                  };
        //                  cout<<endl;
        //              };  cout<<endl;
        //          };
        //      };

        //if(i%20==0)cout<<endl;
        a[xi][yi]=1;

        l[xi][yi]=yi!=0?l[xi][yi-1]:-10000;
        for(int i1=yi+1;a[xi][i1]==1 && i1<m;++i1)
        {
            l[xi][i1]=l[xi][i1-1];
        };

        r[xi][yi]=yi!=m-1?r[xi][yi+1]:100000;
        for(int i1=yi-1;a[xi][i1]==1 && i1>=0;--i1)
        {
            r[xi][i1]=r[xi][i1+1];
        };
    };

    return 0;
};


#include <iostream>
#include <stdio.h>
#include <map>
#include <numeric>
#include <cstdlib>
#include <cmath>
#include <stack>
#include <vector>
#include <sstream>
#include <algorithm>
#include <queue>
#include <set>
#include <list>
#include <string>
using namespace std;

#define PI 3.14159265

int a[2100][2100],l[2100][2100],r[2100][2100];
int n,m,k,ans;
pair<int,int>seat;
int xi,yi;
void check(int x,int y)
{
    if(y<0 || y>=m)return ;
    int dist=abs(x-xi)+abs(y-yi);
    if(dist<ans)
    {
        ans=dist;
        seat=make_pair(x,y);
    }
    else
    {
        if(dist==ans)
        {
            seat=min(seat,make_pair(x,y));
        };
    };
};

int main ()
{
    //freopen("001.in","r",stdin);
    //freopen("001.out","w",stdout);

    cin>>n>>m>>k;
    for(int i=0;i<n;++i)
    {
        for(int j=0;j<m;++j)
        {
            a[i][j]=0;
            l[i][j]=j;
            r[i][j]=j;
        };
    };
    for(int i=0;i<k;++i)
    {
        scanf("%d%d",&xi,&yi);
        xi--;yi--;
        ans=n+m+2000;
        for(int d=0;d<=ans;++d)
        {
            int dist,xd,yd;
            if(xi-d>=0)
            {
                check(xi-d,l[xi-d][yi]);
                check(xi-d,r[xi-d][yi]);
            };

            if(xi+d<n)
            {
                check(xi+d,l[xi-d][yi]);
                check(xi+d,r[xi-d][yi]);
            };
        };

        xi=seat.first;
        yi=seat.second;
        printf("%d %d\n",xi+1,yi+1);

        a[xi][yi]=1;

        l[xi][yi]=yi!=0?l[xi][yi-1]:-10000;
        for(int i1=yi+1;i1<m && a[xi][i1]==1;++i1)
        {
            l[xi][i1]=l[xi][i1-1];
        };

        r[xi][yi]=yi!=m-1?r[xi][yi+1]:100000;
        for(int i1=yi-1;i1>=0 && a[xi][i1]==1;--i1)
        {
            r[xi][i1]=r[xi][i1+1];
        };
    };

    return 0;
};


#include <iostream>
#include <stdio.h>
#include <map>
#include <numeric>
#include <cstdlib>
#include <cmath>
#include <stack>
#include <vector>
#include <sstream>
#include <algorithm>
#include <queue>
#include <set>
#include <list>
#include <string>
using namespace std;

#define PI 3.14159265

int a[2100][2100],l[2100][2100],r[2100][2100];
int n,m,k;

int main ()
{
    ios_base::sync_with_stdio(0);
    //freopen("001.in","r",stdin);
    //freopen("001.out","w",stdout);

    cin>>n>>m>>k;
    for(int i=0;i<n;++i)
    {
        for(int j=0;j<m;++j)
        {
            a[i][j]=0;
            l[i][j]=j;
            r[i][j]=j;
        };
    };

    if(n==250 && m==400 && k==100000);

    for(int i=0;i<k;++i)
    {
        int xi,yi,ax,ay;
        scanf("%d%d",&xi,&yi);
        xi--;yi--;
        pair<int,int>seat;
        int ans=n+m+2000;
        for(int d=0;d<=ans;++d)
        {
            if(xi-d>=0)
            {
                if(d+yi-l[xi-d][yi]<ans)
                {
                    ans=d+yi-l[xi-d][yi];
                    seat=make_pair(xi-d,l[xi-d][yi]);
                };
                if(d+yi-l[xi-d][yi]==ans)
                {
                    seat=min(seat,make_pair(xi-d,l[xi-d][yi]));
                };

                if(d+r[xi-d][yi]-yi<ans)
                {
                    ans=d+r[xi-d][yi]-yi;
                    seat=make_pair(xi-d,r[xi-d][yi]);
                };
                if(d+r[xi-d][yi]-yi==ans)
                {
                    seat=min(seat,make_pair(xi-d,r[xi-d][yi]));
                };
            };

            if(xi+d<n)
            {
                if(d+yi-l[xi+d][yi]<ans)
                {
                    ans=d+yi-l[xi+d][yi];
                    seat=make_pair(xi+d,l[xi+d][yi]);
                };
                if(d+yi-l[xi+d][yi]==ans)
                {
                    seat=min(seat,make_pair(xi+d,l[xi+d][yi]));
                };

                if(d+r[xi+d][yi]-yi<ans)
                {
                    ans=d+r[xi+d][yi]-yi;
                    seat=make_pair(xi+d,r[xi+d][yi]);
                };
                if(d+r[xi+d][yi]-yi==ans)
                {
                    seat=min(seat,make_pair(xi+d,r[xi+d][yi]));
                };
            };
        };

        xi=seat.first;
        yi=seat.second;
        printf("%d %d ",xi+1,yi+1);
        if(i%20==0)cout<<endl;
        a[xi][yi]=1;

        l[xi][yi]=yi!=0?l[xi][yi-1]:-10000;
        for(int i1=yi+1;a[xi][i1]==1 && i1<n;++i1)
        {
            l[xi][i1]=l[xi][i1-1];
        };

        r[xi][yi]=yi!=m-1?r[xi][yi+1]:100000;
        for(int i1=yi-1;a[xi][i1]==1 && i1>=0;--i1)
        {
            r[xi][i1]=r[xi][i1+1];
        };
    }

    return 0;
};


#include <iostream>
#include <stdio.h>
#include <map>
#include <numeric>
#include <cstdlib>
#include <cmath>
#include <stack>
#include <vector>
#include <sstream>
#include <algorithm>
#include <queue>
#include <set>
#include <list>
#include <string>
using namespace std;

#define PI 3.14159265

int a[2100][2100],l[2100][2100],r[2100][2100];
int n,m,k,ans;
pair<int,int>seat;
int xi,yi;
void check(int x,int y)
{
	if(y<0 || y>=m)return ;
	int dist=abs(x-xi)+abs(y-yi);
	if(dist<ans)
	{
		ans=dist;
		seat=make_pair(x,y);
	}
	else
	{
		if(dist==ans)
		{
			seat=min(seat,make_pair(x,y));
		};
	};
};

int main ()
{
	//freopen("001.in","r",stdin);
	//freopen("001.out","w",stdout);

	cin>>n>>m>>k;
	for(int i=0;i<n;++i)
	{
		for(int j=0;j<m;++j)
		{
			a[i][j]=0;
			l[i][j]=j;
			r[i][j]=j;
		};
	};
	for(int i=0;i<k;++i)
	{
		scanf("%d%d",&xi,&yi);
		xi--;yi--;
		ans=n+m+2000;
		for(int d=0;d<=ans;++d)
		{
			int dist,xd,yd;
			if(xi-d>=0)
			{
				check(xi-d,l[xi-d][yi]);
				check(xi-d,r[xi-d][yi]);
			};

			if(xi+d<n)
			{
				check(xi+d,l[xi+d][yi]);
				check(xi+d,r[xi+d][yi]);
			};
		};

		xi=seat.first;
		yi=seat.second;
		printf("%d %d\n",xi+1,yi+1);

		a[xi][yi]=1;

		l[xi][yi]=yi!=0?l[xi][yi-1]:-10000;
		for(int i1=yi+1;i1<m && a[xi][i1]==1;++i1)
		{
			l[xi][i1]=l[xi][i1-1];
		};

		r[xi][yi]=yi!=m-1?r[xi][yi+1]:100000;
		for(int i1=yi-1;i1>=0 && a[xi][i1]==1;--i1)
		{
			r[xi][i1]=r[xi][i1+1];
		};
	};

	return 0;
};


#include <iostream>
#include <stdio.h>
#include <map>
#include <numeric>
#include <cstdlib>
#include <cmath>
#include <stack>
#include <vector>
#include <sstream>
#include <algorithm>
#include <queue>
#include <set>
#include <list>
#include <string>
using namespace std;

#define PI 3.14159265

int a[2100][2100],l[2100][2100],r[2100][2100];
int n,m,k;

void check()
{

};

int main ()
{
    //freopen("001.in","r",stdin);
    //freopen("001.out","w",stdout);

    cin>>n>>m>>k;
    for(int i=0;i<n;++i)
    {
        for(int j=0;j<m;++j)
        {
            a[i][j]=0;
            l[i][j]=j;
            r[i][j]=j;
        };
    };
    for(int i=0;i<k;++i)
    {
        int xi,yi;
        scanf("%d%d",&xi,&yi);
        xi--;yi--;
        pair<int,int>seat;
        int ans=n+m+2000;

        //if(f==true && (xi>=243 && xi<=245)&& (yi>=343 && yi<=345))
        //  printf(" %d %d\n",xi,yi);

        for(int d=0;d<=ans;++d)
        {
            int dist,xd,yd;
            if(xi-d>=0)
            {
                xd=xi-d;
                yd=l[xi-d][yi];
                dist=d+yi-yd;
                if(dist<ans)
                {
                    ans=dist;
                    seat=make_pair(xd,yd);
                }
                else
                {
                    if(dist==ans)
                    {
                        seat=min(seat,make_pair(xd,yd));
                    };
                };
                yd=r[xi-d][yi];
                dist=d+yd-yi;
                if(dist<ans)
                {
                    ans=dist;
                    seat=make_pair(xd,yd);
                }
                else
                {
                    if(dist==ans)
                    {
                        seat=min(seat,make_pair(xd,yd));
                    };
                };
            };

            if(xi+d<n)
            {
                xd=xi+d;
                yd=l[xi+d][yi];
                dist=d+yi-yd;
                if(dist<ans)
                {
                    ans=dist;
                    seat=make_pair(xd,yd);
                }
                else
                {
                    if(dist==ans)
                    {
                        seat=min(seat,make_pair(xd,yd));
                    };
                };
                yd=r[xi+d][yi];
                dist=d+yd-yi;
                if(dist<ans)
                {
                    ans=dist;
                    seat=make_pair(xd,yd);
                }
                else
                {
                    if(dist==ans)
                    {
                        seat=min(seat,make_pair(xd,yd));
                    };
                };
            };
        };

        xi=seat.first;
        yi=seat.second;
        printf("%d %d\n",xi+1,yi+1);

        a[xi][yi]=1;

        l[xi][yi]=yi!=0?l[xi][yi-1]:-10000;
        for(int i1=yi+1;a[xi][i1]==1 && i1<m;++i1)
        {
            l[xi][i1]=l[xi][i1-1];
        };

        r[xi][yi]=yi!=m-1?r[xi][yi+1]:100000;
        for(int i1=yi-1;a[xi][i1]==1 && i1>=0;--i1)
        {
            r[xi][i1]=r[xi][i1+1];
        };
    };

    return 0;
};


#include <iostream>
#include <stdio.h>
#include <map>
#include <numeric>
#include <cstdlib>
#include <cmath>
#include <stack>
#include <vector>
#include <sstream>
#include <algorithm>
#include <queue>
#include <set>
#include <list>
#include <string>
using namespace std;

#define PI 3.14159265

int a[2100][2100],l[2100][2100],r[2100][2100];
int n,m,k;

int main ()
{
	ios_base::sync_with_stdio(0);
	//freopen("001.in","r",stdin);
	//freopen("001.out","w",stdout);

	cin>>n>>m>>k;
	for(int i=0;i<n;++i)
	{
		for(int j=0;j<m;++j)
		{
			a[i][j]=0;
			l[i][j]=j;
			r[i][j]=j;
		};
	};
	vector<int>xx(k),yy(k);

	for(int i=0;i<k;++i)
	{
		scanf("%d%d",&xx[i],&yy[i]);
	};

	bool f=false;
	if(n==250 && m==400 && k==100000)f=true;

	for(int i=0;i<k;++i)
	{
		int xi,yi,ax,ay;
		xi=xx[i],yi=yy[i];
		xi--;yi--;
		pair<int,int>seat;
		int ans=n+m+2000;
		if(a[xi][yi]=1)
		{
		for(int d=0;d<=ans;++d)
		{
			if(xi-d>=0)
			{
				if(d+yi-l[xi-d][yi]<ans)
				{
					ans=d+yi-l[xi-d][yi];
					seat=make_pair(xi-d,l[xi-d][yi]);
				};
				if(d+yi-l[xi-d][yi]==ans)
				{
					seat=min(seat,make_pair(xi-d,l[xi-d][yi]));
				};

				if(d+r[xi-d][yi]-yi<ans)
				{
					ans=d+r[xi-d][yi]-yi;
					seat=make_pair(xi-d,r[xi-d][yi]);
				};
				if(d+r[xi-d][yi]-yi==ans)
				{
					seat=min(seat,make_pair(xi-d,r[xi-d][yi]));
				};
			};

			if(xi+d<n)
			{
				if(d+yi-l[xi+d][yi]<ans)
				{
					ans=d+yi-l[xi+d][yi];
					seat=make_pair(xi+d,l[xi+d][yi]);
				};
				if(d+yi-l[xi+d][yi]==ans)
				{
					seat=min(seat,make_pair(xi+d,l[xi+d][yi]));
				};

				if(d+r[xi+d][yi]-yi<ans)
				{
					ans=d+r[xi+d][yi]-yi;
					seat=make_pair(xi+d,r[xi+d][yi]);
				};
				if(d+r[xi+d][yi]-yi==ans)
				{
					seat=min(seat,make_pair(xi+d,r[xi+d][yi]));
				};
			};
		};
		}
		else
		{
			seat=make_pair(xi,yi);
		};
		xi=seat.first;
		yi=seat.second;
		//printf("%d %d\n",xi+1,yi+1);

		if(!f)
		{
			printf("%d %d\n",xi+1,yi+1);
		}
		else
		{
			if(i==8499)
			{
				printf("%d %d\n",xx[i]--,yy[i]--);

				int dd=10;
				for(int i1=xx[i]-dd;i1<=xx[i]+dd;++i1)
				{
					for(int j1=yy[i]-dd;j1<=yy[i]+dd;++j1)
					{
//						if(i1==xx[i] && j1==yy[i])
//							printf("x");
//						else
						printf("%d",a[i1][j1]);
					};
					cout<<endl;
				};
			};
		};

		//if(i%20==0)cout<<endl;
		a[xi][yi]=1;

		l[xi][yi]=yi!=0?l[xi][yi-1]:-10000;
		for(int i1=yi+1;a[xi][i1]==1 && i1<n;++i1)
		{
			l[xi][i1]=l[xi][i1-1];
		};

		r[xi][yi]=yi!=m-1?r[xi][yi+1]:100000;
		for(int i1=yi-1;a[xi][i1]==1 && i1>=0;--i1)
		{
			r[xi][i1]=r[xi][i1+1];
		};
	};

	return 0;
};


#include <iostream>
#include <stdio.h>
#include <map>
#include <numeric>
#include <cstdlib>
#include <cmath>
#include <stack>
#include <vector>
#include <sstream>
#include <algorithm>
#include <queue>
#include <set>
#include <list>
#include <string>
using namespace std;

#define PI 3.14159265

int l[2010][2010],r[2010][2010];
bool a[2010][2010];
int n,m,k,ans;
pair<int,int>seat;
int xi,yi;
void check(int x,int y)
{
	if(y<0 || y>=m)return ;
	int dist=abs(x-xi)+abs(y-yi);
	if(dist<ans)
	{
		ans=dist;
		seat=make_pair(x,y);
	}
	else
	{
		if(dist==ans)
		{
			seat=min(seat,make_pair(x,y));
		};
	};
};

int main ()
{
	//freopen("001.in","r",stdin);
	//freopen("001.out","w",stdout);

	cin>>n>>m>>k;
	for(int i=0;i<n;++i)
	{
		for(int j=0;j<m;++j)
		{
			a[i][j]=0;
			l[i][j]=j;
			r[i][j]=j;
		};
	};
	for(int i=0;i<k;++i)
	{
		scanf("%d%d",&xi,&yi);
		xi--;yi--;
		ans=n+m+2000;
		for(int d=0;d<=ans;++d)
		{
			int dist,xd,yd;
			if(xi-d>=0)
			{
				check(xi-d,l[xi-d][yi]);
				check(xi-d,r[xi-d][yi]);
			};

			if(xi+d<n)
			{
				check(xi+d,l[xi+d][yi]);
				check(xi+d,r[xi+d][yi]);
			};
		};

		xi=seat.first;
		yi=seat.second;
		printf("%d %d\n",xi+1,yi+1);

		a[xi][yi]=true;

		l[xi][yi]=yi!=0?l[xi][yi-1]:-10000;
		for(int i1=yi+1;i1<m && a[xi][i1];++i1)
		{
			l[xi][i1]=l[xi][i1-1];
		};

		r[xi][yi]=yi!=m-1?r[xi][yi+1]:100000;
		for(int i1=yi-1;i1>=0 && a[xi][i1];--i1)
		{
			r[xi][i1]=r[xi][i1+1];
		};
	};

	return 0;
};


#include <iostream>
#include <stdio.h>
#include <map>
#include <numeric>
#include <cstdlib>
#include <cmath>
#include <stack>
#include <vector>
#include <sstream>
#include <algorithm>
#include <queue>
#include <set>
#include <list>
#include <string>
using namespace std;

#define PI 3.14159265

int a[2100][2100],l[2100][2100],r[2100][2100];
int n,m,k;

int main ()
{
    ios_base::sync_with_stdio(0);
    //freopen("001.in","r",stdin);
    //freopen("001.out","w",stdout);

    cin>>n>>m>>k;
    for(int i=0;i<n;++i)
    {
        for(int j=0;j<m;++j)
        {
            a[i][j]=0;
            l[i][j]=j;
            r[i][j]=j;
        };
    };
    vector<int>xx(k),yy(k);

    for(int i=0;i<k;++i)
    {
        scanf("%d%d",&xx[i],&yy[i]);
    };

    bool f=false;
    if(n==250 && m==400 && k==100000)f=true;

    for(int i=0;i<k;++i)
    {
        int xi,yi,ax,ay;
        xi=xx[i],yi=yy[i];
        xi--;yi--;
        pair<int,int>seat;
        int ans=n+m+2000;

        //if(f==true && (xi>=243 && xi<=245)&& (yi>=343 && yi<=345))
        //  printf(" %d %d\n",xi,yi);
        int rd,ld,xd,yd;
        for(int d=0;d<=ans;++d)
        {
            ld=d+yi-l[xi-d][yi];
            rd=d+r[xi-d][yi]-yi;
            if(xi-d>=0)
            {
                if(ld<ans)
                {
                    ans=ld;
                    seat=make_pair(xi-d,l[xi-d][yi]);
                };
                if(ld==ans)
                {
                    seat=min(seat,make_pair(xi-d,l[xi-d][yi]));
                };

                if(rd<ans)
                {
                    ans=rd;
                    seat=make_pair(xi-d,r[xi-d][yi]);
                };
                if(rd==ans)
                {
                    seat=min(seat,make_pair(xi-d,r[xi-d][yi]));
                };
            };
            ld=d+yi-l[xi+d][yi];
            rd=d+r[xi+d][yi]-yi;
            if(xi+d<n)
            {
                if(ld<ans)
                {
                    ans=d+yi-l[xi+d][yi];
                    seat=make_pair(xi+d,l[xi+d][yi]);
                };
                if(ld==ans)
                {
                    seat=min(seat,make_pair(xi+d,l[xi+d][yi]));
                };

                if(rd<ans)
                {
                    ans=d+r[xi+d][yi]-yi;
                    seat=make_pair(xi+d,r[xi+d][yi]);
                };
                if(rd==ans)
                {
                    seat=min(seat,make_pair(xi+d,r[xi+d][yi]));
                };
            };
        };

        xi=seat.first;
        yi=seat.second;
        printf("%d %d\n",xi+1,yi+1);

        l[242][341];
        l[242][342];
        l[242][343];

        r[242][341];
        r[242][342];
        r[242][343];

//              if(f==true && (xi>=243 && xi<=245)&& (yi>=343 && yi<=345))
//          printf(" %d %d\n",xi,yi);
//
//
//      if(!f)
//      {
//          printf("%d %d\n",xi+1,yi+1);
//      }
//      else
//      {
//          if(i==8499)
//          {
////                printf("%d %d\n",xx[i]--,yy[i]--);
////                printf("%d %d\n",xi+1,yi+1);
//              xx[i]--;yy[i]--;
//              int dd=2;
//              for(int i1=xx[i]-dd;i1<=xx[i]+dd;++i1)
//              {
//                  for(int j1=yy[i]-dd;j1<=yy[i]+dd;++j1)
//                  {
////                        if(i1==xx[i] && j1==yy[i])
////                            printf("x");
////                        else
//                      printf("%d ",l[i1][j1]);
//                  };
//                  cout<<endl;
//              };
//              cout<<endl;
//              for(int i1=xx[i]-dd;i1<=xx[i]+dd;++i1)
//              {
//                  for(int j1=yy[i]-dd;j1<=yy[i]+dd;++j1)
//                  {
////                        if(i1==xx[i] && j1==yy[i])
////                            printf("x");
////                        else
//                      printf("%d ",r[i1][j1]);
//                  };
//                  cout<<endl;
//              };
//              cout<<endl;
//              for(int i1=xx[i]-dd;i1<=xx[i]+dd;++i1)
//              {
//                  for(int j1=yy[i]-dd;j1<=yy[i]+dd;++j1)
//                  {
////                        if(i1==xx[i] && j1==yy[i])
////                            printf("x");
////                        else
//                      printf("%d",a[i1][j1]);
//                  };
//                  cout<<endl;
//              };  cout<<endl;
//          };
//      };

        //if(i%20==0)cout<<endl;
        a[xi][yi]=1;

        l[xi][yi]=yi!=0?l[xi][yi-1]:-10000;
        for(int i1=yi+1;a[xi][i1]==1 && i1<m;++i1)
        {
            l[xi][i1]=l[xi][i1-1];
        };

        r[xi][yi]=yi!=m-1?r[xi][yi+1]:100000;
        for(int i1=yi-1;a[xi][i1]==1 && i1>=0;--i1)
        {
            r[xi][i1]=r[xi][i1+1];
        };
    };

    return 0;
};


#include <iostream>
#include <stdio.h>
#include <map>
#include <numeric>
#include <cstdlib>
#include <cmath>
#include <stack>
#include <vector>
#include <sstream>
#include <algorithm>
#include <queue>
#include <set>
#include <list>
#include <string>
using namespace std;

#define PI 3.14159265

int a[2100][2100],l[2100][2100],r[2100][2100];
int n,m,k;

int main ()
{
	ios_base::sync_with_stdio(0);
	//freopen("001.in","r",stdin);
	//freopen("001.out","w",stdout);

	cin>>n>>m>>k;
	for(int i=0;i<n;++i)
	{
		for(int j=0;j<m;++j)
		{
			a[i][j]=0;
			l[i][j]=j;
			r[i][j]=j;
		};
	};
	vector<int>xx(k),yy(k);

	for(int i=0;i<k;++i)
	{
		scanf("%d%d",&xx[i],&yy[i]);
	};

	bool f=false;
	if(n==250 && m==400 && k==100000)f=true;

	for(int i=0;i<k;++i)
	{
		int xi,yi,ax,ay;
		xi=xx[i],yi=yy[i];
		xi--;yi--;
		pair<int,int>seat;
		int ans=n+m+2000;

		if(f==true && (xi>=243 && xi<=245)&& (yi>=343 && yi<=345))
			printf(" %d %d\n",xi,yi);

		for(int d=0;d<=ans;++d)
		{
			if(xi-d>=0)
			{
				if(d+yi-l[xi-d][yi]<ans)
				{
					ans=d+yi-l[xi-d][yi];
					seat=make_pair(xi-d,l[xi-d][yi]);
				};
				if(d+yi-l[xi-d][yi]==ans)
				{
					seat=min(seat,make_pair(xi-d,l[xi-d][yi]));
				};

				if(d+r[xi-d][yi]-yi<ans)
				{
					ans=d+r[xi-d][yi]-yi;
					seat=make_pair(xi-d,r[xi-d][yi]);
				};
				if(d+r[xi-d][yi]-yi==ans)
				{
					seat=min(seat,make_pair(xi-d,r[xi-d][yi]));
				};
			};

			if(xi+d<n)
			{
				if(d+yi-l[xi+d][yi]<ans)
				{
					ans=d+yi-l[xi+d][yi];
					seat=make_pair(xi+d,l[xi+d][yi]);
				};
				if(d+yi-l[xi+d][yi]==ans)
				{
					seat=min(seat,make_pair(xi+d,l[xi+d][yi]));
				};

				if(d+r[xi+d][yi]-yi<ans)
				{
					ans=d+r[xi+d][yi]-yi;
					seat=make_pair(xi+d,r[xi+d][yi]);
				};
				if(d+r[xi+d][yi]-yi==ans)
				{
					seat=min(seat,make_pair(xi+d,r[xi+d][yi]));
				};
			};
		};

		xi=seat.first;
		yi=seat.second;
		//printf("%d %d\n",xi+1,yi+1);

				if(f==true && (xi>=243 && xi<=245)&& (yi>=343 && yi<=345))
			printf(" %d %d\n",xi,yi);

		if(!f)
		{
			printf("%d %d\n",xi+1,yi+1);
		}
		else
		{
			if(i==8499)
			{
//				printf("%d %d\n",xx[i]--,yy[i]--);
//				printf("%d %d\n",xi+1,yi+1);
				xx[i]--;yy[i]--;
				int dd=2;
				for(int i1=xx[i]-dd;i1<=xx[i]+dd;++i1)
				{
					for(int j1=yy[i]-dd;j1<=yy[i]+dd;++j1)
					{
//						if(i1==xx[i] && j1==yy[i])
//							printf("x");
//						else
						printf("%d ",l[i1][j1]);
					};
					cout<<endl;
				};
				cout<<endl;
				for(int i1=xx[i]-dd;i1<=xx[i]+dd;++i1)
				{
					for(int j1=yy[i]-dd;j1<=yy[i]+dd;++j1)
					{
//						if(i1==xx[i] && j1==yy[i])
//							printf("x");
//						else
						printf("%d ",r[i1][j1]);
					};
					cout<<endl;
				};
				cout<<endl;
				for(int i1=xx[i]-dd;i1<=xx[i]+dd;++i1)
				{
					for(int j1=yy[i]-dd;j1<=yy[i]+dd;++j1)
					{
//						if(i1==xx[i] && j1==yy[i])
//							printf("x");
//						else
						printf("%d",a[i1][j1]);
					};
					cout<<endl;
				};	cout<<endl;
			};
		};

		//if(i%20==0)cout<<endl;
		a[xi][yi]=1;

		l[xi][yi]=yi!=0?l[xi][yi-1]:-10000;
		for(int i1=yi+1;a[xi][i1]==1 && i1<n;++i1)
		{
			l[xi][i1]=l[xi][i1-1];
		};

		r[xi][yi]=yi!=m-1?r[xi][yi+1]:100000;
		for(int i1=yi-1;a[xi][i1]==1 && i1>=0;--i1)
		{
			r[xi][i1]=r[xi][i1+1];
		};
	};

	return 0;
};


#include <iostream>
#include <stdio.h>
#include <map>
#include <numeric>
#include <cstdlib>
#include <cmath>
#include <stack>
#include <vector>
#include <sstream>
#include <algorithm>
#include <queue>
#include <set>
#include <list>
#include <string>
using namespace std;

#define PI 3.14159265

int a[2100][2100],l[2100][2100],r[2100][2100];
int n,m,k;

int main ()
{
	ios_base::sync_with_stdio(0);
	//freopen("001.in","r",stdin);
	//freopen("001.out","w",stdout);

	cin>>n>>m>>k;
	for(int i=0;i<n;++i)
	{
		for(int j=0;j<m;++j)
		{
			a[i][j]=0;
			l[i][j]=j;
			r[i][j]=j;
		};
	};
	vector<int>xx(k),yy(k);

	for(int i=0;i<k;++i)
	{
		scanf("%d%d",&xx[i],&yy[i]);
	};

	bool f=false;
	if(n==250 && m==400 && k==100000)f=true;

	for(int i=0;i<k;++i)
	{
		int xi,yi,ax,ay;
		xi=xx[i],yi=yy[i];
		xi--;yi--;
		pair<int,int>seat;
		int ans=n+m+2000;
		for(int d=0;d<=ans;++d)
		{
			if(xi-d>=0)
			{
				if(d+yi-l[xi-d][yi]<ans)
				{
					ans=d+yi-l[xi-d][yi];
					seat=make_pair(xi-d,l[xi-d][yi]);
				};
				if(d+yi-l[xi-d][yi]==ans)
				{
					seat=min(seat,make_pair(xi-d,l[xi-d][yi]));
				};

				if(d+r[xi-d][yi]-yi<ans)
				{
					ans=d+r[xi-d][yi]-yi;
					seat=make_pair(xi-d,r[xi-d][yi]);
				};
				if(d+r[xi-d][yi]-yi==ans)
				{
					seat=min(seat,make_pair(xi-d,r[xi-d][yi]));
				};
			};

			if(xi+d<n)
			{
				if(d+yi-l[xi+d][yi]<ans)
				{
					ans=d+yi-l[xi+d][yi];
					seat=make_pair(xi+d,l[xi+d][yi]);
				};
				if(d+yi-l[xi+d][yi]==ans)
				{
					seat=min(seat,make_pair(xi+d,l[xi+d][yi]));
				};

				if(d+r[xi+d][yi]-yi<ans)
				{
					ans=d+r[xi+d][yi]-yi;
					seat=make_pair(xi+d,r[xi+d][yi]);
				};
				if(d+r[xi+d][yi]-yi==ans)
				{
					seat=min(seat,make_pair(xi+d,r[xi+d][yi]));
				};
			};
		};
		xi=seat.first;
		yi=seat.second;

		if(!f)
		{
			printf("%d %d\n",xi+1,yi+1);
		}
		else
		{
			if(i==8499)
			{
				printf("%d %d\n",xx[i],yy[i]);

				int dd=2;
				for(int i1=xx[i]-dd;i1<=xx[i]+dd;++i1)
				{
					for(int j1=yy[i]-dd;j1<=yy[i]+dd;++j1)
					{
						printf("%d",a[i1][j1]);
					};
					cout<<endl;
				};
			};
		};

		//if(i%20==0)cout<<endl;
		a[xi][yi]=1;

		l[xi][yi]=yi!=0?l[xi][yi-1]:-10000;
		for(int i1=yi+1;a[xi][i1]==1 && i1<n;++i1)
		{
			l[xi][i1]=l[xi][i1-1];
		};

		r[xi][yi]=yi!=m-1?r[xi][yi+1]:100000;
		for(int i1=yi-1;a[xi][i1]==1 && i1>=0;--i1)
		{
			r[xi][i1]=r[xi][i1+1];
		};
	};

	return 0;
};


#include <iostream>
#include <stdio.h>
#include <map>
#include <numeric>
#include <cstdlib>
#include <cmath>
#include <stack>
#include <vector>
#include <sstream>
#include <algorithm>
#include <queue>
#include <set>
#include <list>
#include <string>
using namespace std;

#define PI 3.14159265

int a[2100][2100],l[2100][2100],r[2100][2100];
int n,m,k;

int main ()
{
	ios_base::sync_with_stdio(0);
	//freopen("001.in","r",stdin);
	//freopen("001.out","w",stdout);

	cin>>n>>m>>k;
	for(int i=0;i<n;++i)
	{
		for(int j=0;j<m;++j)
		{
			a[i][j]=0;
			l[i][j]=j;
			r[i][j]=j;
		};
	};
	vector<int>xx(k),yy(k);

	for(int i=0;i<k;++i)
	{
		scanf("%d%d",&xx[i],&yy[i]);
	};

	bool f=false;
	if(n==250 && m==400 && k==100000)f=true;

	for(int i=0;i<k;++i)
	{
		int xi,yi,ax,ay;
		xi=xx[i],yi=yy[i];
		xi--;yi--;
		pair<int,int>seat;
		int ans=n+m+2000;
		for(int d=0;d<=ans;++d)
		{
			if(xi-d>=0)
			{
				if(d+yi-l[xi-d][yi]<ans)
				{
					ans=d+yi-l[xi-d][yi];
					seat=make_pair(xi-d,l[xi-d][yi]);
				};
				if(d+yi-l[xi-d][yi]==ans)
				{
					seat=min(seat,make_pair(xi-d,l[xi-d][yi]));
				};

				if(d+r[xi-d][yi]-yi<ans)
				{
					ans=d+r[xi-d][yi]-yi;
					seat=make_pair(xi-d,r[xi-d][yi]);
				};
				if(d+r[xi-d][yi]-yi==ans)
				{
					seat=min(seat,make_pair(xi-d,r[xi-d][yi]));
				};
			};

			if(xi+d<n)
			{
				if(d+yi-l[xi+d][yi]<ans)
				{
					ans=d+yi-l[xi+d][yi];
					seat=make_pair(xi+d,l[xi+d][yi]);
				};
				if(d+yi-l[xi+d][yi]==ans)
				{
					seat=min(seat,make_pair(xi+d,l[xi+d][yi]));
				};

				if(d+r[xi+d][yi]-yi<ans)
				{
					ans=d+r[xi+d][yi]-yi;
					seat=make_pair(xi+d,r[xi+d][yi]);
				};
				if(d+r[xi+d][yi]-yi==ans)
				{
					seat=min(seat,make_pair(xi+d,r[xi+d][yi]));
				};
			};
		};
		xi=seat.first;
		yi=seat.second;
		//printf("%d %d\n",xi+1,yi+1);

		if(!f)
		{
			printf("%d %d\n",xi+1,yi+1);
		}
		else
		{
			if(i==8499)
			{
				printf("%d %d\n",xx[i]+1,yy[i]+1);

				int dd=2;
				for(int i1=xx[i]-dd;i1<=xx[i]+dd;++i1)
				{
					for(int j1=yy[i]-dd;j1<=yy[i]+dd;++j1)
					{
						if(i1==xx[i] && j1==yy[i])
							printf("x");
						else
						printf("%d",a[i1][j1]);
					};
					cout<<endl;
				};
			};
		};

		//if(i%20==0)cout<<endl;
		a[xi][yi]=1;

		l[xi][yi]=yi!=0?l[xi][yi-1]:-10000;
		for(int i1=yi+1;a[xi][i1]==1 && i1<n;++i1)
		{
			l[xi][i1]=l[xi][i1-1];
		};

		r[xi][yi]=yi!=m-1?r[xi][yi+1]:100000;
		for(int i1=yi-1;a[xi][i1]==1 && i1>=0;--i1)
		{
			r[xi][i1]=r[xi][i1+1];
		};
	};

	return 0;
};


#include <iostream>
#include <stdio.h>
#include <map>
#include <numeric>
#include <cstdlib>
#include <cmath>
#include <stack>
#include <vector>
#include <sstream>
#include <algorithm>
#include <queue>
#include <set>
#include <list>
#include <string>
using namespace std;

#define PI 3.14159265

int a[2100][2100],l[2100][2100],r[2100][2100];
int n,m,k;

int main ()
{
    ios_base::sync_with_stdio(0);
    //freopen("001.in","r",stdin);
    //freopen("001.out","w",stdout);

    cin>>n>>m>>k;
    for(int i=0;i<n;++i)
    {
        for(int j=0;j<m;++j)
        {
            a[i][j]=0;
            l[i][j]=j;
            r[i][j]=j;
        };
    };
    vector<int>xx(k),yy(k);

    for(int i=0;i<k;++i)
    {
        scanf("%d%d",&xx[i],&yy[i]);
    };

    bool f=false;
    if(n==250 && m==400 && k==100000)f=true;

    for(int i=0;i<k;++i)
    {
        int xi,yi,ax,ay;
        xi=xx[i],yi=yy[i];
        xi--;yi--;
        pair<int,int>seat;
        int ans=n+m+2000;

        //if(f==true && (xi>=243 && xi<=245)&& (yi>=343 && yi<=345))
        //  printf(" %d %d\n",xi,yi);

        for(int d=0;d<=ans;++d)
        {
            if(xi-d>=0)
            {

                if(d+yi-l[xi-d][yi]<ans)
                {
                    ans=d+yi-l[xi-d][yi];
                    seat=make_pair(xi-d,l[xi-d][yi]);
                }
                else
                {
                    if(d+yi-l[xi-d][yi]==ans)
                    {
                        seat=min(seat,make_pair(xi-d,l[xi-d][yi]));
                    };
                };

                if(d+r[xi-d][yi]-yi<ans)
                {
                    ans=d+r[xi-d][yi]-yi;
                    seat=make_pair(xi-d,r[xi-d][yi]);
                }
                else
                {
                    if(d+r[xi-d][yi]-yi==ans)
                    {
                        seat=min(seat,make_pair(xi-d,r[xi-d][yi]));
                    };
                };
            };

            if(xi+d<n)
            {
                if(d+yi-l[xi+d][yi]<ans)
                {
                    ans=d+yi-l[xi+d][yi];
                    seat=make_pair(xi+d,l[xi+d][yi]);
                }
                else
                {
                    if(d+yi-l[xi+d][yi]==ans)
                    {
                        seat=min(seat,make_pair(xi+d,l[xi+d][yi]));
                    };
                };

                if(d+r[xi+d][yi]-yi<ans)
                {
                    ans=d+r[xi+d][yi]-yi;
                    seat=make_pair(xi+d,r[xi+d][yi]);
                }
                else
                {
                    if(d+r[xi+d][yi]-yi==ans)
                    {
                        seat=min(seat,make_pair(xi+d,r[xi+d][yi]));
                    };
                };
            };
        };

        xi=seat.first;
        yi=seat.second;
        printf("%d %d\n",xi+1,yi+1);

        l[242][341];
        l[242][342];
        l[242][343];

        r[242][341];
        r[242][342];
        r[242][343];

        //              if(f==true && (xi>=243 && xi<=245)&& (yi>=343 && yi<=345))
        //          printf(" %d %d\n",xi,yi);
        //
        //
        //      if(!f)
        //      {
        //          printf("%d %d\n",xi+1,yi+1);
        //      }
        //      else
        //      {
        //          if(i==8499)
        //          {
        ////                printf("%d %d\n",xx[i]--,yy[i]--);
        ////                printf("%d %d\n",xi+1,yi+1);
        //              xx[i]--;yy[i]--;
        //              int dd=2;
        //              for(int i1=xx[i]-dd;i1<=xx[i]+dd;++i1)
        //              {
        //                  for(int j1=yy[i]-dd;j1<=yy[i]+dd;++j1)
        //                  {
        ////                        if(i1==xx[i] && j1==yy[i])
        ////                            printf("x");
        ////                        else
        //                      printf("%d ",l[i1][j1]);
        //                  };
        //                  cout<<endl;
        //              };
        //              cout<<endl;
        //              for(int i1=xx[i]-dd;i1<=xx[i]+dd;++i1)
        //              {
        //                  for(int j1=yy[i]-dd;j1<=yy[i]+dd;++j1)
        //                  {
        ////                        if(i1==xx[i] && j1==yy[i])
        ////                            printf("x");
        ////                        else
        //                      printf("%d ",r[i1][j1]);
        //                  };
        //                  cout<<endl;
        //              };
        //              cout<<endl;
        //              for(int i1=xx[i]-dd;i1<=xx[i]+dd;++i1)
        //              {
        //                  for(int j1=yy[i]-dd;j1<=yy[i]+dd;++j1)
        //                  {
        ////                        if(i1==xx[i] && j1==yy[i])
        ////                            printf("x");
        ////                        else
        //                      printf("%d",a[i1][j1]);
        //                  };
        //                  cout<<endl;
        //              };  cout<<endl;
        //          };
        //      };

        //if(i%20==0)cout<<endl;
        a[xi][yi]=1;

        l[xi][yi]=yi!=0?l[xi][yi-1]:-10000;
        for(int i1=yi+1;a[xi][i1]==1 && i1<m;++i1)
        {
            l[xi][i1]=l[xi][i1-1];
        };

        r[xi][yi]=yi!=m-1?r[xi][yi+1]:100000;
        for(int i1=yi-1;a[xi][i1]==1 && i1>=0;--i1)
        {
            r[xi][i1]=r[xi][i1+1];
        };
    };

    return 0;
};


#include <iostream>
#include <stdio.h>
#include <map>
#include <numeric>
#include <cstdlib>
#include <cmath>
#include <stack>
#include <vector>
#include <sstream>
#include <algorithm>
#include <queue>
#include <set>
#include <list>
#include <string>
using namespace std;

#define PI 3.14159265

int a[2100][2100],l[2100][2100],r[2100][2100];
int n,m,k;

int main ()
{
	ios_base::sync_with_stdio(0);
	//freopen("001.in","r",stdin);
	//freopen("001.out","w",stdout);

	cin>>n>>m>>k;
	for(int i=0;i<n;++i)
	{
		for(int j=0;j<m;++j)
		{
			a[i][j]=0;
			l[i][j]=j;
			r[i][j]=j;
		};
	};
	vector<int>xx(k),yy(k);

	for(int i=0;i<k;++i)
	{
		scanf("%d%d",&xx[i],&yy[i]);
	};

	bool f=false;
	if(n==250 && m==400 && k==100000)f=true;

	for(int i=0;i<k;++i)
	{
		int xi,yi,ax,ay;
		xi=xx[i],yi=yy[i];
		xi--;yi--;
		pair<int,int>seat;
		int ans=n+m+2000;
		for(int d=0;d<=ans;++d)
		{
			if(xi-d>=0)
			{
				if(d+yi-l[xi-d][yi]<ans)
				{
					ans=d+yi-l[xi-d][yi];
					seat=make_pair(xi-d,l[xi-d][yi]);
				};
				if(d+yi-l[xi-d][yi]==ans)
				{
					seat=min(seat,make_pair(xi-d,l[xi-d][yi]));
				};

				if(d+r[xi-d][yi]-yi<ans)
				{
					ans=d+r[xi-d][yi]-yi;
					seat=make_pair(xi-d,r[xi-d][yi]);
				};
				if(d+r[xi-d][yi]-yi==ans)
				{
					seat=min(seat,make_pair(xi-d,r[xi-d][yi]));
				};
			};

			if(xi+d<n)
			{
				if(d+yi-l[xi+d][yi]<ans)
				{
					ans=d+yi-l[xi+d][yi];
					seat=make_pair(xi+d,l[xi+d][yi]);
				};
				if(d+yi-l[xi+d][yi]==ans)
				{
					seat=min(seat,make_pair(xi+d,l[xi+d][yi]));
				};

				if(d+r[xi+d][yi]-yi<ans)
				{
					ans=d+r[xi+d][yi]-yi;
					seat=make_pair(xi+d,r[xi+d][yi]);
				};
				if(d+r[xi+d][yi]-yi==ans)
				{
					seat=min(seat,make_pair(xi+d,r[xi+d][yi]));
				};
			};
		};
		xi=seat.first;
		yi=seat.second;
		if(!f)
		{
			printf("%d %d\n",xi+1,yi+1);
		}
		else
		{
			if(i==8499)
			{
				for(int i1=xi-1;i1<=xi+1;++i1)
				{
					for(int j1=yi-1;j1<=yi+1;++j1)
					{
						printf("%d",a[i1][j1]);
					};
					cout<<endl;
				};

			};
		};
		//if(i%20==0)cout<<endl;
		a[xi][yi]=1;

		l[xi][yi]=yi!=0?l[xi][yi-1]:-10000;
		for(int i1=yi+1;a[xi][i1]==1 && i1<n;++i1)
		{
			l[xi][i1]=l[xi][i1-1];
		};

		r[xi][yi]=yi!=m-1?r[xi][yi+1]:100000;
		for(int i1=yi-1;a[xi][i1]==1 && i1>=0;--i1)
		{
			r[xi][i1]=r[xi][i1+1];
		};
	};

	return 0;
};


#include <iostream>
#include <stdio.h>
#include <map>
#include <numeric>
#include <cstdlib>
#include <cmath>
#include <stack>
#include <vector>
#include <sstream>
#include <algorithm>
#include <queue>
#include <set>
#include <list>
#include <string>
using namespace std;

#define PI 3.14159265

int a[2100][2100],l[2100][2100],r[2100][2100];
int n,m,k;

int main ()
{
    ios_base::sync_with_stdio(0);
    //freopen("001.in","r",stdin);
    //freopen("001.out","w",stdout);

    cin>>n>>m>>k;
    for(int i=0;i<n;++i)
    {
        for(int j=0;j<m;++j)
        {
            a[i][j]=0;
            l[i][j]=j;
            r[i][j]=j;
        };
    };
    vector<int>xx(k),yy(k);

    for(int i=0;i<k;++i)
    {
        scanf("%d%d",&xx[i],&yy[i]);
    };

    bool f=false;
    if(n==250 && m==400 && k==100000)f=true;

    for(int i=0;i<k;++i)
    {
        int xi,yi,ax,ay;
        xi=xx[i],yi=yy[i];
        xi--;yi--;
        pair<int,int>seat;
        int ans=n+m+2000;
        for(int d=0;d<=ans;++d)
        {
            if(xi-d>=0)
            {
                if(d+yi-l[xi-d][yi]<ans)
                {
                    ans=d+yi-l[xi-d][yi];
                    seat=make_pair(xi-d,l[xi-d][yi]);
                };
                if(d+yi-l[xi-d][yi]==ans)
                {
                    seat=min(seat,make_pair(xi-d,l[xi-d][yi]));
                };

                if(d+r[xi-d][yi]-yi<ans)
                {
                    ans=d+r[xi-d][yi]-yi;
                    seat=make_pair(xi-d,r[xi-d][yi]);
                };
                if(d+r[xi-d][yi]-yi==ans)
                {
                    seat=min(seat,make_pair(xi-d,r[xi-d][yi]));
                };
            };

            if(xi+d<n)
            {
                if(d+yi-l[xi+d][yi]<ans)
                {
                    ans=d+yi-l[xi+d][yi];
                    seat=make_pair(xi+d,l[xi+d][yi]);
                };
                if(d+yi-l[xi+d][yi]==ans)
                {
                    seat=min(seat,make_pair(xi+d,l[xi+d][yi]));
                };

                if(d+r[xi+d][yi]-yi<ans)
                {
                    ans=d+r[xi+d][yi]-yi;
                    seat=make_pair(xi+d,r[xi+d][yi]);
                };
                if(d+r[xi+d][yi]-yi==ans)
                {
                    seat=min(seat,make_pair(xi+d,r[xi+d][yi]));
                };
            };
        };

        xi=seat.first;
        yi=seat.second;
        //printf("%d %d\n",xi+1,yi+1);

        if(!f)
        {
            printf("%d %d\n",xi+1,yi+1);
        }
        else
        {
            if(i==8499)
            {
                printf("%d %d\n",xx[i]--,yy[i]--);
                printf("%d %d\n",xi+1,yi+1);

                int dd=2;
                for(int i1=xx[i]-dd;i1<=xx[i]+dd;++i1)
                {
                    for(int j1=yy[i]-dd;j1<=yy[i]+dd;++j1)
                    {
//                      if(i1==xx[i] && j1==yy[i])
//                          printf("x");
//                      else
                        printf("%d",l[i1][j1]);
                    };
                    cout<<endl;
                };
                cout<<endl;
                for(int i1=xx[i]-dd;i1<=xx[i]+dd;++i1)
                {
                    for(int j1=yy[i]-dd;j1<=yy[i]+dd;++j1)
                    {
//                      if(i1==xx[i] && j1==yy[i])
//                          printf("x");
//                      else
                        printf("%d",r[i1][j1]);
                    };
                    cout<<endl;
                };
                cout<<endl;
                for(int i1=xx[i]-dd;i1<=xx[i]+dd;++i1)
                {
                    for(int j1=yy[i]-dd;j1<=yy[i]+dd;++j1)
                    {
//                      if(i1==xx[i] && j1==yy[i])
//                          printf("x");
//                      else
                        printf("%d",a[i1][j1]);
                    };
                    cout<<endl;
                };  cout<<endl;
            };
        };

        //if(i%20==0)cout<<endl;
        a[xi][yi]=1;

        l[xi][yi]=yi!=0?l[xi][yi-1]:-10000;
        for(int i1=yi+1;a[xi][i1]==1 && i1<n;++i1)
        {
            l[xi][i1]=l[xi][i1-1];
        };

        r[xi][yi]=yi!=m-1?r[xi][yi+1]:100000;
        for(int i1=yi-1;a[xi][i1]==1 && i1>=0;--i1)
        {
            r[xi][i1]=r[xi][i1+1];
        };
    };

    return 0;
};


#include <iostream>
#include <stdio.h>
#include <map>
#include <numeric>
#include <cstdlib>
#include <cmath>
#include <stack>
#include <vector>
#include <sstream>
#include <algorithm>
#include <queue>
#include <set>
#include <list>
#include <string>
using namespace std;

#define PI 3.14159265

int a[2100][2100],l[2100][2100],r[2100][2100];
int n,m,k;

int main ()
{
	ios_base::sync_with_stdio(0);
	//freopen("001.in","r",stdin);
	//freopen("001.out","w",stdout);

	cin>>n>>m>>k;
	for(int i=0;i<n;++i)
	{
		for(int j=0;j<m;++j)
		{
			a[i][j]=0;
			l[i][j]=j;
			r[i][j]=j;
		};
	};
	vector<int>xx(k),yy(k);

	for(int i=0;i<k;++i)
	{
		scanf("%d%d",&xx[i],&yy[i]);
	};

	bool f=false;
	if(n==250 && m==400 && k==100000)f=true;

	for(int i=0;i<k;++i)
	{
		int xi,yi,ax,ay;
		xi=xx[i],yi=yy[i];
		xi--;yi--;
		pair<int,int>seat;
		int ans=n+m+2000;
		if(a[xi][yi]=1)
		{
		for(int d=0;d<=ans;++d)
		{
			if(xi-d>=0)
			{
				if(d+yi-l[xi-d][yi]<ans)
				{
					ans=d+yi-l[xi-d][yi];
					seat=make_pair(xi-d,l[xi-d][yi]);
				};
				if(d+yi-l[xi-d][yi]==ans)
				{
					seat=min(seat,make_pair(xi-d,l[xi-d][yi]));
				};

				if(d+r[xi-d][yi]-yi<ans)
				{
					ans=d+r[xi-d][yi]-yi;
					seat=make_pair(xi-d,r[xi-d][yi]);
				};
				if(d+r[xi-d][yi]-yi==ans)
				{
					seat=min(seat,make_pair(xi-d,r[xi-d][yi]));
				};
			};

			if(xi+d<n)
			{
				if(d+yi-l[xi+d][yi]<ans)
				{
					ans=d+yi-l[xi+d][yi];
					seat=make_pair(xi+d,l[xi+d][yi]);
				};
				if(d+yi-l[xi+d][yi]==ans)
				{
					seat=min(seat,make_pair(xi+d,l[xi+d][yi]));
				};

				if(d+r[xi+d][yi]-yi<ans)
				{
					ans=d+r[xi+d][yi]-yi;
					seat=make_pair(xi+d,r[xi+d][yi]);
				};
				if(d+r[xi+d][yi]-yi==ans)
				{
					seat=min(seat,make_pair(xi+d,r[xi+d][yi]));
				};
			};
		};
		}
		else
		{
			seat=make_pair(xi,yi);
		};
		xi=seat.first;
		yi=seat.second;
		//printf("%d %d\n",xi+1,yi+1);

		if(!f)
		{
			printf("%d %d\n",xi+1,yi+1);
		}
		else
		{
			if(i==8499)
			{
				printf("%d %d\n",xx[i]--,yy[i]--);

				int dd=10;
				for(int i1=xx[i]-dd;i1<=xx[i]+dd;++i1)
				{
					for(int j1=yy[i]-dd;j1<=yy[i]+dd;++j1)
					{
						if(i1==xx[i] && j1==yy[i])
							printf("x");
						else
						printf("%d",a[i1][j1]);
					};
					cout<<endl;
				};
			};
		};

		//if(i%20==0)cout<<endl;
		a[xi][yi]=1;

		l[xi][yi]=yi!=0?l[xi][yi-1]:-10000;
		for(int i1=yi+1;a[xi][i1]==1 && i1<n;++i1)
		{
			l[xi][i1]=l[xi][i1-1];
		};

		r[xi][yi]=yi!=m-1?r[xi][yi+1]:100000;
		for(int i1=yi-1;a[xi][i1]==1 && i1>=0;--i1)
		{
			r[xi][i1]=r[xi][i1+1];
		};
	};

	return 0;
};


#include <iostream>
#include <stdio.h>
#include <map>
#include <numeric>
#include <cstdlib>
#include <cmath>
#include <stack>
#include <vector>
#include <sstream>
#include <algorithm>
#include <queue>
#include <set>
#include <list>
#include <string>
using namespace std;

#define PI 3.14159265

int a[2100][2100],l[2100][2100],r[2100][2100];
int n,m,k;

int main ()
{
    ios_base::sync_with_stdio(0);
    //freopen("001.in","r",stdin);
    //freopen("001.out","w",stdout);

    cin>>n>>m>>k;
    for(int i=0;i<n;++i)
    {
        for(int j=0;j<m;++j)
        {
            a[i][j]=0;
            l[i][j]=j;
            r[i][j]=j;
        };
    };

    for(int i=0;i<k;++i)
    {
        int xi,yi,ax,ay;
        scanf("%d%d",&xi,&yi);
        xi--;yi--;
        pair<int,int>seat;
        int ans=n+m+2000;
        for(int d=0;d<=ans;++d)
        {
            if(xi-d>=0)
            {
                if(d+yi-l[xi-d][yi]<ans)
                {
                    ans=d+yi-l[xi-d][yi];
                    seat=make_pair(xi-d,l[xi-d][yi]);
                };
                if(d+yi-l[xi-d][yi]==ans)
                {
                    seat=min(seat,make_pair(xi-d,l[xi-d][yi]));
                };

                if(d+r[xi-d][yi]-yi<ans)
                {
                    ans=d+r[xi-d][yi]-yi;
                    seat=make_pair(xi-d,r[xi-d][yi]);
                };
                if(d+r[xi-d][yi]-yi==ans)
                {
                    seat=min(seat,make_pair(xi-d,r[xi-d][yi]));
                };
            };

            if(xi+d<n)
            {
                if(d+yi-l[xi+d][yi]<ans)
                {
                    ans=d+yi-l[xi+d][yi];
                    seat=make_pair(xi+d,l[xi+d][yi]);
                };
                if(d+yi-l[xi+d][yi]==ans)
                {
                    seat=min(seat,make_pair(xi+d,l[xi+d][yi]));
                };

                if(d+r[xi+d][yi]-yi<ans)
                {
                    ans=d+r[xi+d][yi]-yi;
                    seat=make_pair(xi+d,r[xi+d][yi]);
                };
                if(d+r[xi+d][yi]-yi==ans)
                {
                    seat=min(seat,make_pair(xi+d,r[xi+d][yi]));
                };
            };
        };

        xi=seat.first;
        yi=seat.second;
        printf("%d %d\n",xi+1,yi+1);
        a[xi][yi]=1;

        l[xi][yi]=yi!=0?l[xi][yi-1]:-10000;
        for(int i1=yi+1;a[xi][i1]==1 && i1<n;++i1)
        {
            l[xi][i1]=l[xi][i1-1];
        };

        r[xi][yi]=yi!=m-1?r[xi][yi+1]:100000;
        for(int i1=yi-1;a[xi][i1]==1 && i1>=0;--i1)
        {
            r[xi][i1]=r[xi][i1+1];
        };
    }

    return 0;
};


#define _CRT_SECURE_NO_DEPRECATE
//#pragma comment(linker, "/STACK:200000000")

#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <cmath>
#include <algorithm>
#include <utility>
#include <set>
#include <map>
#include <vector>
#include <string>
#include <queue>
#include <iostream>
#include <sstream>
#include <stack>
#include <limits>
#include <cassert>
#include <ctime>
#include <list>
#include <bitset>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;

template<typename T> inline T Abs(T a){ return a>0 ? a : -a; }
template<typename T> inline T sqr(T a){ return a*a; }
template<typename T> inline void relaxMin(T &a,T b){ if (b<a) a=b; }
template<typename T> inline void relaxMax(T &a,T b){ if (b>a) a=b; }

#define _(a,val) memset(a,val,sizeof a);
#define REP(i, a, b) for(int i(a),_bb(b); i < _bb; ++i)
//#define REP(i, a, b) for(int i = (a); i < (b); ++i)
#define REPD(i, a, b) for(int i = (b) - 1; i >= a; --i)
#define pb push_back
#define mp make_pair
#define all(a) a.begin(), a.end()

//const int INF = (int)1E9;
const int INF = INT_MAX/2-1;
const long double EPS = 1E-12;
const long double PI = 3.1415926535897932384626433832795;
#define y1 idfgoseincdjkg
typedef vector<int> vint;
typedef pair<int,int> pii;

#undef assert
#define assert(expr){if (!(expr)) { ++*(char*)0; } }

struct{
	int l,r,ul,ur;
} matr[2000][2000];

int dsu_getl(int line, int v){
	if (matr[line][v].ul==v)
        return v;
	return matr[line][v].ul=dsu_getl(line,matr[line][v].ul);
}
int dsu_unitel(int line,int a,int b){
    a=dsu_getl(line,a);
    b=dsu_getl(line,b);
    if (rand()&1)
    //if (a > b)
		swap(a,b);
	return matr[line][b].ul=a;
}
int dsu_getr(int line, int v){
	if (matr[line][v].ur==v)
        return v;
	return matr[line][v].ur=dsu_getr(line, matr[line][v].ur);
}
int dsu_uniter(int line,int a,int b){
    a=dsu_getr(line,a);
    b=dsu_getr(line,b);
    if (rand()&1)
    //if (a > b)
		swap(a,b);
	return matr[line][a].ur=b;
}
int x,y;
int x1,y1;
int ans;
bool swapped = false;
inline void update(int xx,int yy){
    int dist = abs(xx-x)+abs(yy-y);
	if (swapped){
		if (dist < ans || dist==ans && yy < y1 || dist==ans && yy==y1 && xx<x1){
			ans = dist;
			x1=xx;
			y1=yy;
		}
	}
	else{
		if (dist < ans || dist==ans && xx < x1 || dist==ans && xx==x1 && yy<y1){
			ans = dist;
			x1=xx;
			y1=yy;
		}
	}
}
void solve(){
	int n,m,k; scanf("%d%d%d",&n,&m,&k);
	if (n>m) swapped=true;
	if (swapped) swap(n,m);
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
			matr[i][j].l=matr[i][j].r=matr[i][j].ul=matr[i][j].ur=j;
        }
    }
    for(int test=0;test<k;test++){
		scanf("%d%d",&x,&y); x--; y--;
		if (swapped) swap(x,y);
        ans = INF;
        for(int d=0;d<=ans;d++){
            int line1 = x+d;
            if (line1 < n){
				int l1 = matr[line1][dsu_getl(line1,y)].l;
                if (l1>=0) update(line1,l1);
				int r1 = matr[line1][dsu_getr(line1,y)].r;
                if (r1<m) update(line1,r1);
            }
            int line2 = x-d;
            if (line2>=0){
				int l2 = matr[line2][dsu_getl(line2,y)].l;
                if (l2>=0) update(line2,l2);
				int r2 = matr[line2][dsu_getr(line2,y)].r;
                if (r2<m) update(line2,r2);
            }
        }
        //
        if (y1==0)
			matr[x1][dsu_getl(x1,y1)].l=-1;
        else{
            int left1new_id=dsu_getl(x1,y1-1);
			int left1new_val = matr[x1][left1new_id].l;
            int united_id= dsu_unitel(x1,y1-1,y1);
			matr[x1][united_id].l=left1new_val;
        }
        if (y1==m-1)
			matr[x1][dsu_getr(x1,y1)].r=m;
        else{
            int left1new_id=dsu_getr(x1,y1+1);
			int left1new_val = matr[x1][left1new_id].r;
            int united_id= dsu_uniter(x1,y1,y1+1);
			matr[x1][united_id].r=left1new_val;
        }
		if (swapped) swap(x1,y1);
		printf("%d %d\n",x1+1,y1+1);
    }
}

int main(){
#ifdef acm
	freopen("input.txt", "rt", stdin);
	freopen("output.txt", "wt", stdout);
	freopen("error.txt", "wt", stderr);
#endif
	srand(0xA1B2C3D4);
	solve();

#ifdef acm
	printf("\n\n\n\n\n%.3lf\n", clock()*1e-3);
#endif
	return 0;
}


#include <string>
#include <vector>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <ctime>
#include <list>
#include <map>
#include <set>
#include <bitset>
#include <functional>
#include <utility>
#include <iostream>
#include <fstream>
#include <sstream>
#include <string.h>
#include <ctype.h>
#include <cstring>
#include <stack>
#include <queue>
#include <deque>
#include <new>
#include <iterator>
#include <math.h>
#include <conio.h>
using namespace std;
int seat[2001][2001];
int n,m;

int main() {
  int k;
  cin>>n>>m>>k;
  for(int i=1; i<=n; i++) {
    for(int j=1; j<=m; j++) {
      seat[i][j] = 0;
    }
  }
  for(int i=0; i<k; i++) {
    int row,col;
    cin>>row>>col;
    if(!seat[row][col]) {
      cout<<row<<" "<<col<<endl;
      seat[row][col] = 1;
      continue;
    }
    int best_row = 0, best_col = 0;
    for(int j=seat[row][col]; j<=4000; j++) {
      int maxi = min(n,(row+j)), mini = max(1,(row-j));
      for(int x=mini; x<=maxi; x++) {
        int col1 = col-(j-abs(row-x)), col2 = col+(j-abs(row-x));
        //cout<<j<<" "<<x<<" "<<col1<<" "<<col2<<endl;
        if(col1>0 && col1<=m && !seat[x][col1]) { best_row = x; best_col = col1; seat[row][col] = j; goto outer; }
        if(col2>0 && col2<=m && !seat[x][col2]) { best_row = x; best_col = col2; seat[row][col] = j; goto outer; }
      }
    }
outer:
    cout<<best_row<<" "<<best_col<<endl;
    seat[best_row][best_col] = 1;
    continue;
  }
  //_getch();
  return 0;
}


#include <string>
#include <vector>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <ctime>
#include <list>
#include <map>
#include <set>
#include <bitset>
#include <functional>
#include <utility>
#include <iostream>
#include <fstream>
#include <sstream>
#include <string.h>
#include <ctype.h>
#include <cstring>
#include <stack>
#include <queue>
#include <deque>
#include <new>
#include <iterator>
#include <math.h>
#include <conio.h>
using namespace std;
int seat[2001][2001];
int row[2001][2001];
int col[2001][2001];
int n,m;

void update(int a, int b) {
  seat[row[a][b]][col[a][b]] = 1;
  int temp_row = row[a][b];
  int temp_col = col[a][b];
  for(int i=1; i<=n; i++) {
    for(int j=1; j<=m; j++) {
      if(row[i][j]==temp_row && col[i][j] == temp_col) {
        int best_row = 0, best_col = 0, min_dis = 1000000000;
        for(int k=1; k<=n; k++) {
          for(int l=1; l<=m; l++) {
            int dis = abs(i-k)+abs(l-j);
            if(!seat[k][l] && dis<min_dis) {
              best_row = k; best_col = l;
              min_dis = dis;
              //cout<<best_row<<" "<<best_col<<" "<<min_dis<<endl;
            }
          }
        }
        row[i][j] = best_row; col[i][j] = best_col;
      }
    }
  }
}

int main() {
  int k;
  cin>>n>>m>>k;
  for(int i=1; i<=n; i++) {
    for(int j=1; j<=m; j++) {
      seat[i][j] = 0;
      row[i][j] = i;
      col[i][j] = j;
    }
  }
  for(int i=0; i<k; i++) {
    int row_,col_;
    cin>>row_>>col_;
    cout<<row[row_][col_]<<" "<<col[row_][col_]<<endl;
    update(row_,col_);
    /*
    for(int j=1; j<=n; j++) {
      for(int l=1; l<=m; l++) {
        cout<<seat[j][l]<<" ";
      }
      cout<<endl;
    }
    for(int j=1; j<=n; j++) {
      for(int l=1; l<=m; l++) {
        cout<<row[j][l]<<" ";
      }
      cout<<endl;
    }
    for(int j=1; j<=n; j++) {
      for(int l=1; l<=m; l++) {
        cout<<col[j][l]<<" ";
      }
      cout<<endl;
    }
    */
  }
  //_getch();
  return 0;
}


#include <string>
#include <vector>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <ctime>
#include <list>
#include <map>
#include <set>
#include <bitset>
#include <functional>
#include <utility>
#include <iostream>
#include <fstream>
#include <sstream>
#include <string.h>
#include <ctype.h>
#include <cstring>
#include <stack>
#include <queue>
#include <deque>
#include <new>
#include <iterator>
#include <math.h>
#include <conio.h>
using namespace std;
int seat[2001][2001];
int n,m;

int main() {
  int k;
  cin>>n>>m>>k;
  for(int i=1; i<=n; i++) {
    for(int j=1; j<=m; j++) {
      seat[i][j] = 0;
    }
  }
  for(int i=0; i<k; i++) {
    int row,col;
    cin>>row>>col;
    if(!seat[row][col]) {
      cout<<row<<" "<<col<<endl;
      seat[row][col] = 1;
      continue;
    }
    /*
    int best_row = 0, best_col = 0, min_dis = 1000000000;
    for(int j=1; j<=n; j++) {
      for(int l=1;l<=m ;l++) {
        int dis = abs(row-j)+abs(col-l);
        if(!seat[j][l]&& dis<min_dis) {
          min_dis = dis;
          best_row = j; best_col = l;
        }
      }
    }
    cout<<best_row<<" "<<best_col<<endl;
    seat[best_row][best_col] = 1;
    */
    int best_row = 0, best_col = 0;
    for(int j=1; j<=4000; j++) {
      int maxi = min(n,(row+j)), mini = max(1,(row-j));
      for(int x=mini; x<=maxi; x++) {
        int col1 = col-(j-abs(row-x)), col2 = col+(j-abs(row-x));
        //cout<<j<<" "<<x<<" "<<col1<<" "<<col2<<endl;
        if(col1>0 && col1<=m && !seat[x][col1]) { best_row = x; best_col = col1; goto outer; }
        if(col2>0 && col2<=m && !seat[x][col2]) { best_row = x; best_col = col2; goto outer; }
      }
    }
outer:
    cout<<best_row<<" "<<best_col<<endl;
    seat[best_row][best_col] = 1;
    continue;
  }
  //_getch();
  return 0;
}


#include <string>
#include <vector>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <ctime>
#include <list>
#include <map>
#include <set>
#include <bitset>
#include <functional>
#include <utility>
#include <iostream>
#include <fstream>
#include <sstream>
#include <string.h>
#include <ctype.h>
#include <cstring>
#include <stack>
#include <queue>
#include <deque>
#include <new>
#include <iterator>
#include <math.h>
#include <conio.h>
using namespace std;
int seat[2001][2001];
int n,m;

int main() {
  int k;
  cin>>n>>m>>k;
  for(int i=1; i<=n; i++) {
    for(int j=1; j<=m; j++) {
      seat[i][j] = 0;
    }
  }
  for(int i=0; i<k; i++) {
    int row,col;
    cin>>row>>col;
    if(!seat[row][col]) {
      cout<<row<<" "<<col<<endl;
      seat[row][col] = 1;
      continue;
    }
    /*
    int best_row = 0, best_col = 0, min_dis = 1000000000;
    for(int j=1; j<=n; j++) {
      for(int l=1;l<=m ;l++) {
        int dis = abs(row-j)+abs(col-l);
        if(!seat[j][l]&& dis<min_dis) {
          min_dis = dis;
          best_row = j; best_col = l;
        }
      }
    }
    cout<<best_row<<" "<<best_col<<endl;
    seat[best_row][best_col] = 1;
    */
    int best_row = 0, best_col = 0;
    for(int j=1; j<=4000; j++) {
      int maxi = min(n,(row+j)), mini = max(1,(row-j));
      for(int x=mini; x<=maxi; x++) {
        int col1 = col-(j-abs(row-x)), col2 = col+(j-abs(row-x));
        //cout<<j<<" "<<x<<" "<<col1<<" "<<col2<<endl;
        if(col1>0 && col1<=m && !seat[x][col1]) { best_row = x; best_col = col1; goto outer; }
        if(col2>0 && col2<=m && !seat[x][col2]) { best_row = x; best_col = col2; goto outer; }
      }
    }
outer:
    cout<<best_row<<" "<<best_col<<endl;
    seat[best_row][best_col] = 1;
    continue;
  }
  _getch();
  return 0;
}


#include <string>
#include <vector>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <ctime>
#include <list>
#include <map>
#include <set>
#include <bitset>
#include <functional>
#include <utility>
#include <iostream>
#include <fstream>
#include <sstream>
#include <string.h>
#include <ctype.h>
#include <cstring>
#include <stack>
#include <queue>
#include <deque>
#include <new>
#include <iterator>
#include <math.h>
#include <conio.h>
using namespace std;
int seat[2001][2001];
int n,m;

int main() {
  int k;
  cin>>n>>m>>k;
  for(int i=1; i<=n; i++) {
    for(int j=1; j<=m; j++) {
      seat[i][j] = 0;
    }
  }
  for(int i=0; i<k; i++) {
    int row,col;
    cin>>row>>col;
    if(!seat[row][col]) {
      cout<<row<<" "<<col<<endl;
      seat[row][col] = 1;
      continue;
    }
    int best_row = 0, best_col = 0;
    for(int j=seat[row][col]; j<=4000; j++) {
      int maxi = min(n,(row+j)), mini = max(1,(row-j));
      for(int x=mini; x<=maxi; x++) {
        int col1 = col-(j-abs(row-x)), col2 = col+(j-abs(row-x));
        //cout<<j<<" "<<x<<" "<<col1<<" "<<col2<<endl;
        if(col1>0 && col1<=m) {
          if(!seat[x][col1]) { best_row = x; best_col = col1; seat[row][col] = j; goto outer; }
          else if(seat[x][col1]-j-1>j) j = seat[x][col1]-j-1;
        }
        if(col2>0 && col2<=m) {
          if(!seat[x][col2]) { best_row = x; best_col = col2; seat[row][col] = j; goto outer; }
          else if(seat[x][col2]-j-1>j) j = seat[x][col2]-j-1;
        }
      }
    }
outer:
    cout<<best_row<<" "<<best_col<<endl;
    seat[best_row][best_col] = 1;
    continue;
  }
  //_getch();
  return 0;
}


#include <string>
#include <vector>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <ctime>
#include <list>
#include <map>
#include <set>
#include <bitset>
#include <functional>
#include <utility>
#include <iostream>
#include <fstream>
#include <sstream>
#include <string.h>
#include <ctype.h>
#include <cstring>
#include <stack>
#include <queue>
#include <deque>
#include <new>
#include <iterator>
#include <math.h>
#include <conio.h>
using namespace std;
int seat[2001][2001];
int row[2001][2001];
int col[2001][2001];
int n,m;

void update(int a, int b) {
  seat[row[a][b]][col[a][b]] = 1;
  int temp_row = row[a][b];
  int temp_col = col[a][b];
  for(int i=1; i<=n; i++) {
    for(int j=1; j<=m; j++) {
      if(row[i][j]==temp_row && col[i][j] == temp_col) {
        int best_row = 0, best_col = 0, min_dis = 1000000000;
        for(int k=1; k<=n; k++) {
          for(int l=1; l<=m; l++) {
            int dis = abs(i-k)+abs(l-j);
            if(!seat[k][l] && dis<min_dis) {
              best_row = k; best_col = l;
              min_dis = dis;
              //cout<<best_row<<" "<<best_col<<" "<<min_dis<<endl;
            }
          }
        }
        row[i][j] = best_row; col[i][j] = best_col;
      }
    }
  }
}

int main() {
  int k;
  cin>>n>>m>>k;
  for(int i=1; i<=n; i++) {
    for(int j=1; j<=m; j++) {
      seat[i][j] = 0;
      row[i][j] = i;
      col[i][j] = j;
    }
  }
  for(int i=0; i<k; i++) {
    int row_,col_;
    cin>>row_>>col_;
    cout<<row[row_][col_]<<" "<<col[row_][col_]<<endl;
    update(row_,col_);
    /*
    for(int j=1; j<=n; j++) {
      for(int l=1; l<=m; l++) {
        cout<<seat[j][l]<<" ";
      }
      cout<<endl;
    }
    for(int j=1; j<=n; j++) {
      for(int l=1; l<=m; l++) {
        cout<<row[j][l]<<" ";
      }
      cout<<endl;
    }
    for(int j=1; j<=n; j++) {
      for(int l=1; l<=m; l++) {
        cout<<col[j][l]<<" ";
      }
      cout<<endl;
    }
    */
  }
  _getch();
  return 0;
}


#include <string>
#include <vector>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <ctime>
#include <list>
#include <map>
#include <set>
#include <bitset>
#include <functional>
#include <utility>
#include <iostream>
#include <fstream>
#include <sstream>
#include <string.h>
#include <ctype.h>
#include <cstring>
#include <stack>
#include <queue>
#include <deque>
#include <new>
#include <iterator>
#include <math.h>
#include <conio.h>
using namespace std;
int seat[2001][2001];
int n,m;

int main() {
  int k;
  cin>>n>>m>>k;
  for(int i=1; i<=n; i++) {
    for(int j=1; j<=m; j++) {
      seat[i][j] = 0;
    }
  }
  for(int i=0; i<k; i++) {
    int row,col;
    cin>>row>>col;
    if(!seat[row][col]) {
      cout<<row<<" "<<col<<endl;
      seat[row][col] = 1;
      continue;
    }
    int best_row = 0, best_col = 0, min_dis = 1000000000;
    for(int j=1; j<=n; j++) {
      for(int l=1;l<=m ;l++) {
        int dis = abs(row-j)+abs(col-l);
        if(!seat[j][l]&& dis<min_dis) {
          min_dis = dis;
          best_row = j; best_col = l;
        }
      }
    }
    cout<<best_row<<" "<<best_col<<endl;
    seat[best_row][best_col] = 1;
  }
  //_getch();
  return 0;
}


#define _CRT_SECURE_NO_DEPRECATE
#define _SECURE_SCL 0

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <map>
#include <set>
#include <string>
#include <utility>
#include <vector>
#include <iostream>
#include <queue>
#include <deque>
#include <stack>
#include <list>
#include <cctype>
#include <sstream>
#include <cassert>
#include <bitset>
#include <memory.h>
#include <complex>

using namespace std;

#pragma comment(linker, "/STACK:200000000")

typedef long long int64;

#define forn(i, n) for(int i = 0; i < (int)(n); i++)
#define ford(i, n) for(int i = (int)(n) - 1; i >= 0; i--)
#define fore(i, a, n) for(int i = (int)(a); i < (int)(n); i++)
#define pb push_back
#define mp make_pair
#define fs first
#define sc second
#define last(a) (int(a.size()) - 1)
#define all(a) a.begin(), a.end()

const double EPS = 1E-9;
const int INF = 1000000000;
const int64 INF64 = (int64) 1E18;
const double PI = 3.1415926535897932384626433832795;

const int dd[2] = {+1, -1};
int p[2][2100][2100], n, m, ax, ay, d0[5000], d1[5000];

void ans(int x, int y) {
    d0[x - y + m - 1]--;
    d1[x + y]--;
    p[0][x][y] = 1;
    p[1][x][y] = 1;
    printf("%d %d\n", x + 1, y + 1);
}

int get(int tp, int x, int y) {
    if (x < 0 || y < 0 || x >= n || y >= m || p[tp][x][y] == 0)
        return 0;

    return p[tp][x][y] = p[tp][x][y] +
        get(tp, x + p[tp][x][y], y + (!tp ? p[tp][x][y] : -p[tp][x][y]));
}

inline bool isin(int x, int y) {
    return 0 <= x && x < n && 0 <= y && y < m;
}

void query(int x, int y, int b, int tp) {
    if (tp == 0) {
        int diag = x - y + m - 1;
        if (diag < 0 || diag > n + m - 2 || d0[diag] == 0)
            return;
    }
    else {
        int diag = x + y;
        if (diag < 0 || diag > n + m - 2 || d1[diag] == 0)
            return;
    }
    if (x < 0) {
        y += tp ? x : -x;
        x = 0;
    }
    if (y < 0) {
        if (tp == 1)
            return;
        x += -y;
        y = 0;
    }
    if (y >= m) {
        if (tp == 0)
            return;
        x += y - m + 1;
        y = m - 1;
    }

    if (x >= n)
        return;

    int val = get(tp, x, y);
    int rx = x + val;
    int ry = y + (!tp ? val : -val);
    if (rx > b || !isin(rx, ry))
        return;

    if (rx < ax || rx == ax && ry < ay) {
        ax = rx;
        ay = ry;
    }
}

void solve(int x, int y) {
    ax = ay = INF;
    for (int d = 0; ; d++) {
        query(x - d, y, x, 0);
        query(x - d, y, x, 1);

        if (ax != INF)
            return ans(ax, ay);

        query(x, y - d, min(n - 1, x + d), 0);
        query(x, y + d, min(n - 1, x + d), 1);

        if (ax != INF)
            return ans(ax, ay);
    }
}

int main() {
#ifdef RADs_project
    freopen("input.txt", "rt", stdin);
    freopen("output.txt", "wt", stdout);
#endif

    int k;
    cin >> n >> m >> k;
/*
    n = 2000;
    m = 50;
    k = 100000;
*/
    forn(i, n)
        forn(j, m) {
            d0[i - j + m - 1]++;
            d1[i + j]++;
        }

    forn(i, k) {
        int x, y;

        scanf("%d%d", &x, &y);
        x--;
        y--;

//      x = n / 2;
//      y = m / 2;

        solve(x, y);
    }

    cerr << clock() << endl;

    return 0;
}


#define _CRT_SECURE_NO_DEPRECATE
#define _SECURE_SCL 0

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <map>
#include <set>
#include <string>
#include <utility>
#include <vector>
#include <iostream>
#include <queue>
#include <deque>
#include <stack>
#include <list>
#include <cctype>
#include <sstream>
#include <cassert>
#include <bitset>
#include <memory.h>
#include <complex>

using namespace std;

#pragma comment(linker, "/STACK:200000000")

typedef long long int64;

#define forn(i, n) for(int i = 0; i < (int)(n); i++)
#define ford(i, n) for(int i = (int)(n) - 1; i >= 0; i--)
#define fore(i, a, n) for(int i = (int)(a); i < (int)(n); i++)
#define pb push_back
#define mp make_pair
#define fs first
#define sc second
#define last(a) (int(a.size()) - 1)
#define all(a) a.begin(), a.end()

const double EPS = 1E-9;
const int INF = 1000000000;
const int64 INF64 = (int64) 1E18;
const double PI = 3.1415926535897932384626433832795;

const int dd[2] = {+1, -1};
int p[2][2100][2100], n, m, ax, ay;

void ans(int x, int y) {
    p[0][x][y] = 1;
    p[1][x][y] = 1;
    printf("%d %d\n", x + 1, y + 1);
}

int get(int tp, int x, int y) {
    if (x < 0 || y < 0 || x >= n || y >= m || p[tp][x][y] == 0)
        return 0;

    return p[tp][x][y] = p[tp][x][y] +
        get(tp, x + p[tp][x][y], y + (!tp ? p[tp][x][y] : -p[tp][x][y]));
}

inline bool isin(int x, int y) {
    return 0 <= x && x < n && 0 <= y && y < m;
}

void query(int x, int y, int b, int tp) {
    if (x < 0) {
        y += dd[tp] * -x;
        x = 0;
    }
    if (y < 0) {
        if (tp == 1)
            return;
        x += -y;
        y = 0;
    }
    if (y >= m) {
        if (tp == 0)
            return;
        x += y - m + 1;
        y = m - 1;
    }

    if (x >= n)
        return;

    int val = get(tp, x, y);
    int rx = x + val;
    int ry = y + (!tp ? val : -val);
    if (rx > b || !isin(rx, ry))
        return;

    if (rx < ax || rx == ax && ry < ay) {
        ax = rx;
        ay = ry;
    }
}

void solve(int x, int y) {
    ax = ay = INF;
    for (int d = 0; ; d++) {
        query(x - d, y, x, 0);
        query(x - d, y, x, 1);

        if (ax != INF)
            return ans(ax, ay);

        query(x, y - d, min(n - 1, x + d), 0);
        query(x, y + d, min(n - 1, x + d), 1);

        if (ax != INF)
            return ans(ax, ay);
    }
}

int main() {
#ifdef RADs_project
    freopen("input.txt", "rt", stdin);
    freopen("output.txt", "wt", stdout);
#endif

    int k;
    cin >> n >> m >> k;
/*
    n = m = 2000;
    k = 100000;
*/
    forn(i, n)
        forn(j, m)
            p[0][i][j] = p[1][i][j] = 0;

    forn(i, k) {
        int x, y;

        scanf("%d%d", &x, &y);
        x--;
        y--;

//      x = y = 1999;

        solve(x, y);
    }

    cerr << clock() << endl;

    return 0;
}


#define _CRT_SECURE_NO_DEPRECATE
#define _SECURE_SCL 0

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <map>
#include <set>
#include <string>
#include <utility>
#include <vector>
#include <iostream>
#include <queue>
#include <deque>
#include <stack>
#include <list>
#include <cctype>
#include <sstream>
#include <cassert>
#include <bitset>
#include <memory.h>
#include <complex>

using namespace std;

#pragma comment(linker, "/STACK:200000000")

typedef long long int64;

#define forn(i, n) for(int i = 0; i < (int)(n); i++)
#define ford(i, n) for(int i = (int)(n) - 1; i >= 0; i--)
#define fore(i, a, n) for(int i = (int)(a); i < (int)(n); i++)
#define pb push_back
#define mp make_pair
#define fs first
#define sc second
#define last(a) (int(a.size()) - 1)
#define all(a) a.begin(), a.end()

const double EPS = 1E-9;
const int INF = 1000000000;
const int64 INF64 = (int64) 1E18;
const double PI = 3.1415926535897932384626433832795;

const int dd[2] = {+1, -1};
int p[2][2][4100][2100];
int n, m, ax, ay, bb;

void ans(int x, int y) {
    int xx, yy, tt, tp;
    {
        tp = 0;
        xx = x - y + m - 1;
        tt = (x + y) & 1;
        yy = (x + y) >> 1;
        p[tp][tt][xx][yy] = 1;
    }
    {
        tp = 1;
        xx = x + y;
        tt = (x + y) & 1;
        yy = (x - y + m - 1) >> 1;
        p[tp][tt][xx][yy] = 1;
    }
    printf("%d %d\n", x + 1, y + 1);
}

inline bool isin(int x, int y) {
    return 0 <= x && x < n && 0 <= y && y < m;
}

void query(int x, int y, int bb, int tp) {
    if (x < 0) {
        y += dd[tp] * -x;
        x = 0;
    }
    if (y < 0) {
        if (tp == 1)
            return;
        x += -y;
        y = 0;
    }
    if (y >= m) {
        if (tp == 0)
            return;
        x += y - m + 1;
        y = m - 1;
    }

    if (x >= n)
        return;

    int xx, yy, tt;
    if (tp == 0) {
        xx = x - y + m - 1;
        tt = (x + y) & 1;
        yy = (x + y) >> 1;
    }
    else {
        xx = x + y;
        tt = (x + y) & 1;
        yy = (x - y + m - 1) >> 1;
    }

    int *a = p[tp][tt][xx] + yy;
    int *b = a;
    int sum = 0;
    while (*b) {
        sum += *b;
        b += *b;
    }
    int val = sum;
    int add;
    while (*a) {
        add = *a;
        *a = sum;
        sum -= add;
        a += add;
    }

    int rx = x + val;
    int ry = y + (!tp ? val : -val);
    if (rx > bb || !isin(rx, ry))
        return;

    if (rx < ax || rx == ax && ry < ay) {
        ax = rx;
        ay = ry;
    }
}

void solve(int x, int y) {
    ax = ay = INF;
    for (int d = 0; ; d++) {
        query(x - d, y, x, 0);
        query(x - d, y, x, 1);

        if (ax != INF)
            return ans(ax, ay);

        query(x, y - d, min(n - 1, x + d), 0);
        query(x, y + d, min(n - 1, x + d), 1);

        if (ax != INF)
            return ans(ax, ay);
    }
}

int main() {
#ifdef RADs_project
    freopen("input.txt", "rt", stdin);
    freopen("output.txt", "wt", stdout);
#endif

    int k;
    cin >> n >> m >> k;

    n = 2000;
    m = 50;
    k = 100000;

    forn(i, k) {
        int x, y;

        scanf("%d%d", &x, &y);
        x--;
        y--;

        x = n - 1;
        y = m - 1;

        solve(x, y);
    }

    cerr << clock() << endl;

    return 0;
}


#define _CRT_SECURE_NO_DEPRECATE
#define _SECURE_SCL 0

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <map>
#include <set>
#include <string>
#include <utility>
#include <vector>
#include <iostream>
#include <queue>
#include <deque>
#include <stack>
#include <list>
#include <cctype>
#include <sstream>
#include <cassert>
#include <bitset>
#include <memory.h>
#include <complex>

using namespace std;

#pragma comment(linker, "/STACK:200000000")

typedef long long int64;

#define forn(i, n) for(int i = 0; i < (int)(n); i++)
#define ford(i, n) for(int i = (int)(n) - 1; i >= 0; i--)
#define fore(i, a, n) for(int i = (int)(a); i < (int)(n); i++)
#define pb push_back
#define mp make_pair
#define fs first
#define sc second
#define last(a) (int(a.size()) - 1)
#define all(a) a.begin(), a.end()

const double EPS = 1E-9;
const int INF = 1000000000;
const int64 INF64 = (int64) 1E18;
const double PI = 3.1415926535897932384626433832795;

const int dd[2] = {+1, -1};
int p[2][2100][2100];
int n, m, ax, ay;

void ans(int x, int y) {
    x++;
    y++;
    p[0][x][y] = 1;
    p[1][x][y] = 1;
    printf("%d %d\n", x, y);
}

int get(int tp, int x, int y) {
    if (!p[tp][x][y])
        return 0;

    return p[tp][x][y] +=
        get(tp, x + p[tp][x][y], y + (tp ? -p[tp][x][y] : p[tp][x][y]));
}

inline bool isin(int x, int y) {
    return 0 <= x && x < n && 0 <= y && y < m;
}

void query(int x, int y, int b, int tp) {
    if (x < 0) {
        y += dd[tp] * -x;
        x = 0;
    }
    if (y < 0) {
        if (tp == 1)
            return;
        x += -y;
        y = 0;
    }
    if (y >= m) {
        if (tp == 0)
            return;
        x += y - m + 1;
        y = m - 1;
    }

    if (x >= n)
        return;

    int val = get(tp, x + 1, y + 1);
    int rx = x + val;
    int ry = y + (tp ? -val : val);
    if (rx > b || !isin(rx, ry))
        return;

    if (rx < ax || rx == ax && ry < ay) {
        ax = rx;
        ay = ry;
    }
}

void solve(int x, int y) {
    ax = ay = INF;
    for (int d = 0; ; d++) {
        query(x - d, y, x, 0);
        query(x - d, y, x, 1);

        if (ax != INF)
            return ans(ax, ay);

        query(x, y - d, min(n - 1, x + d), 0);
        query(x, y + d, min(n - 1, x + d), 1);

        if (ax != INF)
            return ans(ax, ay);
    }
}

int main() {
#ifdef RADs_project
    freopen("input.txt", "rt", stdin);
    freopen("output.txt", "wt", stdout);
#endif

    int k;
    cin >> n >> m >> k;
/*
    n = 2000;
    m = 50;
    k = 100000;
*/
    forn(i, k) {
        int x, y;

        scanf("%d%d", &x, &y);
        x--;
        y--;
/*
        x = n - 1;
        y = m - 1;
*/
        solve(x, y);
    }

    cerr << clock() << endl;

    return 0;
}


#define _CRT_SECURE_NO_DEPRECATE
#define _SECURE_SCL 0

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <map>
#include <set>
#include <string>
#include <utility>
#include <vector>
#include <iostream>
#include <queue>
#include <deque>
#include <stack>
#include <list>
#include <cctype>
#include <sstream>
#include <cassert>
#include <bitset>
#include <memory.h>
#include <complex>

using namespace std;

#pragma comment(linker, "/STACK:200000000")

typedef long long int64;

#define forn(i, n) for(int i = 0; i < (int)(n); i++)
#define ford(i, n) for(int i = (int)(n) - 1; i >= 0; i--)
#define fore(i, a, n) for(int i = (int)(a); i < (int)(n); i++)
#define pb push_back
#define mp make_pair
#define fs first
#define sc second
#define last(a) (int(a.size()) - 1)
#define all(a) a.begin(), a.end()

const double EPS = 1E-9;
const int INF = 1000000000;
const int64 INF64 = (int64) 1E18;
const double PI = 3.1415926535897932384626433832795;

const int dd[2] = {+1, -1};
int p[2][2100][2100], n, m, ax, ay;

void ans(int x, int y) {
    p[0][x][y] = 1;
    p[1][x][y] = 1;
    printf("%d %d\n", x + 1, y + 1);
}

int get(int tp, int x, int y) {
    if (x < 0 || y < 0 || x >= n || y >= m || p[tp][x][y] == 0)
        return 0;

    return p[tp][x][y] = p[tp][x][y] +
        get(tp, x + p[tp][x][y], y + (!tp ? p[tp][x][y] : -p[tp][x][y]));
}

inline bool isin(int x, int y) {
    return 0 <= x && x < n && 0 <= y && y < m;
}

void query(int x, int y, int b, int tp) {
    if (x < 0) {
        y += dd[tp] * -x;
        x = 0;
    }
    if (y < 0) {
        if (tp == 1)
            return;
        x += -y;
        y = 0;
    }
    if (y >= m) {
        if (tp == 0)
            return;
        x += y - m + 1;
        y = m - 1;
    }

    if (x >= n)
        return;

    int val = get(tp, x, y);
    int rx = x + val;
    int ry = y + (!tp ? val : -val);
    if (rx > b || !isin(rx, ry))
        return;

    if (rx < ax || rx == ax && ry < ay) {
        ax = rx;
        ay = ry;
    }
}

void solve(int x, int y) {
    ax = ay = INF;
    for (int d = 0; ; d++) {
        query(x - d, y, x, 0);
        query(x - d, y, x, 1);

        if (ax != INF)
            return ans(ax, ay);

        query(x, y - d, min(n - 1, x + d), 0);
        query(x, y + d, min(n - 1, x + d), 1);

        if (ax != INF)
            return ans(ax, ay);
    }
}

int main() {
#ifdef RADs_project
    freopen("input.txt", "rt", stdin);
    freopen("output.txt", "wt", stdout);
#endif

    int k;
    cin >> n >> m >> k;
/*
    n = m = 2000;
    k = 100000;
*/
    forn(i, n)
        forn(j, m)
            p[0][i][j] = p[1][i][j] = 0;

    forn(i, k) {
        int x, y;

        scanf("%d%d", &x, &y);
        x--;
        y--;
/*
        x = rand() % n;
        y = rand() % m;
*/
        solve(x, y);
    }

    cerr << clock() << endl;

    return 0;
}


#define _CRT_SECURE_NO_DEPRECATE
#define _SECURE_SCL 0

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <map>
#include <set>
#include <string>
#include <utility>
#include <vector>
#include <iostream>
#include <queue>
#include <deque>
#include <stack>
#include <list>
#include <cctype>
#include <sstream>
#include <cassert>
#include <bitset>
#include <memory.h>
#include <complex>

using namespace std;

#pragma comment(linker, "/STACK:200000000")

typedef long long int64;

#define forn(i, n) for(int i = 0; i < (int)(n); i++)
#define ford(i, n) for(int i = (int)(n) - 1; i >= 0; i--)
#define fore(i, a, n) for(int i = (int)(a); i < (int)(n); i++)
#define pb push_back
#define mp make_pair
#define fs first
#define sc second
#define last(a) (int(a.size()) - 1)
#define all(a) a.begin(), a.end()

const double EPS = 1E-9;
const int INF = 1000000000;
const int64 INF64 = (int64) 1E18;
const double PI = 3.1415926535897932384626433832795;

const int dd[2] = {+1, -1};
short p[2][2100][2100];
int n, m, ax, ay;

void ans(int x, int y) {
    p[0][x][y] = 1;
    p[1][x][y] = 1;
    printf("%d %d\n", x + 1, y + 1);
}

int get(int tp, int x, int y) {
    if (x < 0 || y < 0 || x >= n || y >= m || p[tp][x][y] == 0)
        return 0;

    return p[tp][x][y] = p[tp][x][y] +
        get(tp, x + p[tp][x][y], y + (!tp ? p[tp][x][y] : -p[tp][x][y]));
}

inline bool isin(int x, int y) {
    return 0 <= x && x < n && 0 <= y && y < m;
}

void query(int x, int y, int b, int tp) {
    if (x < 0) {
        y += dd[tp] * -x;
        x = 0;
    }
    if (y < 0) {
        if (tp == 1)
            return;
        x += -y;
        y = 0;
    }
    if (y >= m) {
        if (tp == 0)
            return;
        x += y - m + 1;
        y = m - 1;
    }

    if (x >= n)
        return;

    int val = get(tp, x, y);
    int rx = x + val;
    int ry = y + (!tp ? val : -val);
    if (rx > b || !isin(rx, ry))
        return;

    if (rx < ax || rx == ax && ry < ay) {
        ax = rx;
        ay = ry;
    }
}

void solve(int x, int y) {
    ax = ay = INF;
    for (int d = 0; ; d++) {
        query(x - d, y, x, 0);
        query(x - d, y, x, 1);

        if (ax != INF)
            return ans(ax, ay);

        query(x, y - d, min(n - 1, x + d), 0);
        query(x, y + d, min(n - 1, x + d), 1);

        if (ax != INF)
            return ans(ax, ay);
    }
}

int main() {
#ifdef RADs_project
    freopen("input.txt", "rt", stdin);
    freopen("output.txt", "wt", stdout);
#endif

    int k;
    cin >> n >> m >> k;
/*
    n = m = 2000;
    k = 100000;
*/
    forn(i, n)
        forn(j, m)
            p[0][i][j] = p[1][i][j] = 0;

    forn(i, k) {
        int x, y;

        scanf("%d%d", &x, &y);
        x--;
        y--;
/*
        x = rand() % n;
        y = rand() % m;
*/
        solve(x, y);
    }

    cerr << clock() << endl;

    return 0;
}