#include <stdio.h>
#include <string.h>
#include <math.h>
int dis(int x1,int y1,int x2,int y2)
{
    return abs(x1-x2)+abs(y1-y2);
}
int set[2005][2005];
struct near
{
    int dis;
    int x;
    int y;
}sit[2005][2005];
struct guanzhong
{
    int x;
    int y;
}gz[100005];
int main()
{
  int n,i,j,k,m,k1;
     /* freopen("in.txt","r",stdin);
    freopen("out.txt","w",stdout);*/
  while (scanf("%d%d%d",&n,&m,&k)!=EOF)
  {

     memset(set,0,sizeof(set));
     memset(sit,0,sizeof(sit));
     for(i=1;i<=k;i++)
     {
         scanf("%d %d",&gz[i].x,&gz[i].y);
     }
     for(k1=1;k1<=k;k1++)
     {
         if(!set[gz[k1].x][gz[k1].y])
         {
             printf("%d %d\n",gz[k1].x,gz[k1].y);
             sit[gz[k1].x][gz[k1].y].dis=1;
             set[gz[k1].x][gz[k1].y]=1;
         }
         else
         {
              int xx,yy,tag,tag1;
              tag1=0;
              int dist1;
loop:    dist1=sit[gz[k1].x][gz[k1].y].dis;
            tag=0;
             for(i=dist1;i>=-dist1;i--)
             {
                 xx=gz[k1].x-i;
                 yy=gz[k1].y-(dist1-abs(i));
                 if(xx>=1&&xx<=n&&yy>=1&&yy<=m)
                 {
                     if(!set[xx][yy])
                     {
                         tag=1;
                         break;
                     }
                 }
                  xx=gz[k1].x-i;
                 yy=gz[k1].y+(dist1-abs(i));
                 if(xx>=1&&xx<=n&&yy>=1&&yy<=m)
                 {
                     if(!set[xx][yy])
                     {
                         tag=1;
                         break;
                     }
                 }
             }
             if(!tag)
             {
                 sit[gz[k1].x][gz[k1].y].dis++;
                     tag1=1;
                 goto loop;
             }
             set[xx][yy]=1;
             printf("%d %d\n",xx,yy);
             if(tag1)
             {
             /*for(i=k1+1;i<=k;i++)
             {
                 int dist2=dis(gz[i].x,gz[i].y,gz[k1].x,gz[k1].y);
                 if(dist2< sit[gz[k1].x][gz[k1].y].dis)
                 {
                     int tmp=sit[gz[k1].x][gz[k1].y].dis-dist2;
                     if(tmp>sit[gz[i].x][gz[i].y].dis)
                         sit[gz[i].x][gz[i].y].dis=tmp;
                 }
             }*/
             }
         }
     }
  }
  return 0;
}


#include <stdio.h>
#include <string.h>
#include <math.h>
int dis(int x1,int y1,int x2,int y2)
{
    return abs(x1-x2)+abs(y1-y2);
}
int set[2005][2005];
struct near
{
    int dis;
    int last;
}sit[2005][2005];
struct guanzhong
{
    int x;
    int y;
}gz[100005];
int main()
{
  int n,i,j,k,m,k1;
      /*freopen("in.txt","r",stdin);
    freopen("out.txt","w",stdout);*/
  while (scanf("%d%d%d",&n,&m,&k)!=EOF)
  {

     memset(set,0,sizeof(set));
     memset(sit,0,sizeof(sit));
     for(i=1;i<=k;i++)
     {
         scanf("%d %d",&gz[i].x,&gz[i].y);
     }
     for(k1=1;k1<=k;k1++)
     {
         if(!set[gz[k1].x][gz[k1].y])
         {
             printf("%d %d\n",gz[k1].x,gz[k1].y);
             sit[gz[k1].x][gz[k1].y].dis=1;
             set[gz[k1].x][gz[k1].y]=1;
         }
         else
         {
              int xx,yy,tag,tag1;
              tag1=0;
              int dist1;
loop:    dist1=sit[gz[k1].x][gz[k1].y].dis;
              if(sit[gz[k1].x][gz[k1].y].dis==1)
                  i=dist1;
              else
                  i=sit[gz[k1].x][gz[k1].y].last;
            tag=0;
             for(i=dist1;i>=-dist1;i--)
             {
                 xx=gz[k1].x-i;
                 yy=gz[k1].y-(dist1-abs(i));
                 if(xx>=1&&xx<=n&&yy>=1&&yy<=m)
                 {
                     if(!set[xx][yy])
                     {
                         tag=1;
                         break;
                     }
                 }
                  xx=gz[k1].x-i;
                 yy=gz[k1].y+(dist1-abs(i));
                 if(xx>=1&&xx<=n&&yy>=1&&yy<=m)
                 {
                     if(!set[xx][yy])
                     {
                         tag=1;
                         break;
                     }
                 }
             }
             if(!tag)
             {
                 sit[gz[k1].x][gz[k1].y].dis++;
                     tag1=1;
                 goto loop;
             }
             set[xx][yy]=1;
             sit[gz[k1].x][gz[k1].y].last=i;
             printf("%d %d\n",xx,yy);
             if(tag1&&(sit[gz[k1].x][gz[k1].y].dis%10==0))
             {
             for(i=k1+1;i<=k;i++)
             {
                 int dist2=dis(gz[i].x,gz[i].y,gz[k1].x,gz[k1].y);
                 if(dist2< sit[gz[k1].x][gz[k1].y].dis)
                 {
                     int tmp=sit[gz[k1].x][gz[k1].y].dis-dist2;
                     if(tmp>sit[gz[i].x][gz[i].y].dis)
                         sit[gz[i].x][gz[i].y].dis=tmp;
                 }
             }
             }
         }
     }
  }
  return 0;
}


#include <stdio.h>
#include <string.h>
#include <math.h>
int dis(int x1,int y1,int x2,int y2)
{
    return abs(x1-x2)+abs(y1-y2);
}
int set[2005][2005];
struct near
{
    int dis;
    int xx;
}sit[2005][2005];
struct guanzhong
{
    int x;
    int y;
}gz[100005];
int main()
{
  int n,i,j,k,m,k1;
     /* freopen("in.txt","r",stdin);
    freopen("out.txt","w",stdout);*/
  while (scanf("%d%d%d",&n,&m,&k)!=EOF)
  {

     memset(set,0,sizeof(set));
     memset(sit,0,sizeof(sit));
     for(i=1;i<=k;i++)
     {
         scanf("%d %d",&gz[i].x,&gz[i].y);
     }
     for(k1=1;k1<=k;k1++)
     {
         if(!set[gz[k1].x][gz[k1].y])
         {
             printf("%d %d\n",gz[k1].x,gz[k1].y);
             sit[gz[k1].x][gz[k1].y].dis=1;
             set[gz[k1].x][gz[k1].y]=1;
         }
         else
         {
              int xx,yy,tag,tag1;
              tag1=0;
              int dist1;
loop:    dist1=sit[gz[k1].x][gz[k1].y].dis;
            tag=0;
            if(sit[gz[k1].x][gz[k1].y].xx==0)
                i=dist1;
            else
                i=sit[gz[k1].x][gz[k1].y].xx;
             for(;i>=-dist1;i--)
             {
                 xx=gz[k1].x-i;
                 yy=gz[k1].y-(dist1-abs(i));
                 if(xx>=1&&xx<=n&&yy>=1&&yy<=m)
                 {
                     if(!set[xx][yy])
                     {
                         tag=1;
                         break;
                     }
                 }
                  xx=gz[k1].x-i;
                 yy=gz[k1].y+(dist1-abs(i));
                 if(xx>=1&&xx<=n&&yy>=1&&yy<=m)
                 {
                     if(!set[xx][yy])
                     {
                         tag=1;
                         break;
                     }
                 }
             }
             if(!tag)
             {
                 sit[gz[k1].x][gz[k1].y].dis++;
                     tag1=1;
                 goto loop;
             }
             set[xx][yy]=1;
            sit[gz[k1].x][gz[k1].y].xx=i;
             printf("%d %d\n",xx,yy);
             if(tag1)
             {
             for(i=k1+1;i<=k;i++)
             {
                 int dist2=dis(gz[i].x,gz[i].y,gz[k1].x,gz[k1].y);
                 if(dist2< sit[gz[k1].x][gz[k1].y].dis)
                 {
                     int tmp=sit[gz[k1].x][gz[k1].y].dis-dist2;
                     if(tmp>sit[gz[i].x][gz[i].y].dis)
                         sit[gz[i].x][gz[i].y].dis=tmp;
                 }
             }
             }
         }
     }
  }
  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;i1<m && a[xi][i1]==1;++i1)
		{
			l[xi][i1]=l[xi][yi];
		};

		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][yi];
		};
	};

	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+2;
        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;
};


#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][yi-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][yi+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[2100][2100],r[2100][2100];
bool a[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)
        {

            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);

        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]=true;

        l[xi][yi]=yi!=0?l[xi][yi-1]:-10000;
        for(int i1=yi+1;a[xi][i1] && 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] && 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;
        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,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;
		};
	};
	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)
			{
				int dd=2;
				for(int i1=xi-dd;i1<=xi+dd;++i1)
				{
					for(int j1=yi-dd;j1<=yi+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;
		};
	};

	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);
		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]+1,yy[i]+1);

				int dd=20;
				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 <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <climits>
#include <numeric>
#include <vector>
using namespace std;
const int MAX_N = 2000 + 10;
int L[MAX_N][MAX_N], R[MAX_N][MAX_N];
bool used[MAX_N][MAX_N];
int n, m, nQ;
int ans;
pair<int, int> ret;
int x, y;

void check(int r, int c) {
	if (r < 0 || r > n || c < 0 || c > m)
		return;
	int d = abs(r - x) + abs(y - c);
	if (d < ans) {
		ans = d;
		ret = make_pair(r, c);
	} else if (d == ans) {
		ret = min(ret, make_pair(r, c));
	}
}

int main() {
	cin >> n >> m >> nQ;
	for (int r = 0; r < n; ++r) {
		for (int c = 0; c < m; ++c) {
			L[r][c] = c;
			R[r][c] = c;
		}
	}
	for (int i = 0; i < nQ; ++i) {
		scanf("%d%d", &x, &y), --x, --y;
		ans = n + m + 2;
		for (int d = 0; d <= ans; ++d) {
			if (x - d >= 0) {
				check(x - d, L[x - d][y]);
				check(x - d, R[x - d][y]);
			}
			if (x + d < n) {
				check(x + d, L[x + d][y]);
				check(x + d, R[x + d][y]);
			}
		}
		printf("%d %d\n", ret.first + 1, ret.second + 1);
		x = ret.first, y = ret.second;
		used[x][y] = true;
		L[x][y] = y == 0 ? -1 : L[x][y - 1];
		for (int j = y; j < m && used[x][j]; ++j) {
			L[x][j] = L[x][y];
		}
		R[x][y] = y == m - 1 ? -1 : R[x][y + 1];
		for (int j = y; j >= 0 && used[x][j]; --j) {
			R[x][j] = R[x][y];
		}
	}
	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]--);
//              printf("%d %d\n",xi+1,yi+1);
//
//              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;
        };
    };
    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(true)
        {
            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=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;
		};
	};
	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][yi];
		};

		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][yi];
		};
	};

	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]--);
                printf("%d %d\n",xi+1,yi+1);

                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,ans;
pair<int,int>seat;
int xi,yi;
void check(int x,int y)
{
    int dist=abs(x-xi)+(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)cout<<"fghsrt";

	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);
		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);

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

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

	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 ",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],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;
		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]+1,yy[i]+1);

				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;
int main ()
{
    //freopen("in.in","r",stdin);
    int n,m,k;
    cin>>n>>m>>k;
    //vector<vector<int> >v(n,vector<int>(m,0));
    vector<vector<int> >t(n,vector<int>(m,-1));
    vector<pair<int,int> >v1,v2;

    for(int i1=0;i1<k;++i1)
    {
        int x,y;
        scanf("%d%d",&x,&y);
        --x;--y;

        if(t[x][y]!=-1)
        {
            v1.clear();
            v2.clear();
            v1.push_back(make_pair(x,y));
            t[x][y]=i1;
            bool f=false;
            for(int i=0;!f;++i)
            {
                f=false;
                int x1,y1;
                for(int j=0;j<v1.size();++j)
                {
                    x1=v1[j].first;
                    y1=v1[j].second;
                    if(x1>0)
                    {
                        if(t[x1-1][y1]==-1)
                        {
                            x=x1-1;y=y1;
                            f=true;
                            break;
                        }
                        else
                        {
                            if(t[x1-1][y1]!=i1)
                            {
                                v2.push_back(make_pair(x1-1,y1));
                                t[x1-1][y1]=i1;
                            };
                        };
                    };

                    if(y1>0)
                    {
                        if(t[x1][y1-1]==-1)
                        {
                            x=x1;y=y1-1;
                            f=true;
                            break;
                        }
                        else
                        {

                            if(t[x1][y1-1]!=i1)
                            {
                                v2.push_back(make_pair(x1,y1-1));
                                t[x1][y1-1]=i1;
                            };

                        };
                    };

                    if(y1+1<m)
                    {
                        if(t[x1][y1+1]==-1)
                        {
                            x=x1;y=y1+1;
                            f=true;
                            break;
                        }
                        else
                        {

                            if(t[x1][y1+1]!=i1)
                            {
                                v2.push_back(make_pair(x1,y1+1));
                                t[x1][y1+1]=i1;
                            };

                        };
                    };

                    if(x1+1<n)
                    {
                        if(t[x1+1][y1]==-1)
                        {
                            x=x1+1;y=y1;
                            f=true;
                            break;
                        }
                        else
                        {
                            if(t[x1+1][y1]!=i1)
                            {
                                v2.push_back(make_pair(x1+1,y1));
                                t[x1+1][y1]=i1;
                            };

                        };
                    };

                };
                v1.clear();
                swap(v1,v2);
            };

        };

        t[x][y]=i1;
        printf("%d %d\n",x+1,y+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;
		};
	};

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

	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;

		//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);

		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<<endist;
		//				};
		//				cout<<endist;
		//				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<<endist;
		//				};
		//				cout<<endist;
		//				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<<endist;
		//				};	cout<<endist;
		//			};
		//		};

		//if(i%20==0)cout<<endist;
		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)
			{
				int dd=2;
				for(int i1=xi-dd;i1<=xi+dd;++i1)
				{
					for(int j1=yi-dd;j1<=yi+dd;++j1)
					{
						if(i1==xi && j1==yi)
						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(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]+1,yy[i]+1);

				int dd1=10,dd2=20;
				for(int i1=xx[i]-dd1;i1<=xx[i]+dd1;++i1)
				{
					for(int j1=yy[i]-dd2;j1<=yy[i]+dd2;++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]);
                };
                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);

        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)printf("%d\n",ans);
        };
        //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;
        };
    };

    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\n",xi+1,yi+1);
        if(k==20)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[2100][2100],r[2100][2100];
bool a[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]=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;

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;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;
int main ()
{
    freopen("in.in","r",stdin);
    int n,m,k;
    cin>>n>>m>>k;
    //vector<vector<int> >v(n,vector<int>(m,0));
    vector<vector<int> >t(n,vector<int>(m,-1));
    vector<pair<int,int> >v1,v2;

    for(int i1=0;i1<k;++i1)
    {
        int x,y;
        scanf("%d%d",&x,&y);
        --x;--y;

        if(t[x][y]!=-1)
        {
            v1.clear();
            v2.clear();
            v1.push_back(make_pair(x,y));
            t[x][y]=i1;
            bool f=false;
            for(int i=0;!f;++i)
            {
                f=false;
                int x1,y1;
                for(int j=0;j<v1.size();++j)
                {
                    x1=v1[j].first;
                    y1=v1[j].second;
                    if(x1>0)
                    {
                        if(t[x1-1][y1]==-1)
                        {
                            x=x1-1;y=y1;
                            f=true;
                            break;
                        }
                        else
                        {
                            if(t[x1-1][y1]!=i1)
                            {
                                v2.push_back(make_pair(x1-1,y1));
                                t[x1-1][y1]=i1;
                            };
                        };
                    };

                    if(y1>0)
                    {
                        if(t[x1][y1-1]==-1)
                        {
                            x=x1;y=y1-1;
                            f=true;
                            break;
                        }
                        else
                        {

                            if(t[x1][y1-1]!=i1)
                            {
                                v2.push_back(make_pair(x1,y1-1));
                                t[x1][y1-1]=i1;
                            };

                        };
                    };

                    if(y1+1<m)
                    {
                        if(t[x1][y1+1]==-1)
                        {
                            x=x1;y=y1+1;
                            f=true;
                            break;
                        }
                        else
                        {

                            if(t[x1][y1+1]!=i1)
                            {
                                v2.push_back(make_pair(x1,y1+1));
                                t[x1][y1+1]=i1;
                            };

                        };
                    };

                    if(x1+1<n)
                    {
                        if(t[x1+1][y1]==-1)
                        {
                            x=x1+1;y=y1;
                            f=true;
                            break;
                        }
                        else
                        {
                            if(t[x1+1][y1]!=i1)
                            {
                                v2.push_back(make_pair(x1+1,y1));
                                t[x1+1][y1]=i1;
                            };

                        };
                    };

                };
                v1.clear();
                swap(v1,v2);
            };

        };

        t[x][y]=i1;
        printf("%d %d\n",x+1,y+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(true)
		{
			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;
        };
    };

    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(k%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;
};