program main
   implicit none
   integer n,m,coun,i,j,k
   integer,allocatable,dimension(:)::p,y,counter,numb
   integer,allocatable,dimension(:,:)::dummy
   read(*,*)n,m
   allocate(p(m),y(m),counter(n),numb(m),dummy(n,m))
   dummy=0
   do i=1,m
      read(*,*)p(i),y(i)
   enddo
   counter=0
   do i=1,m
      counter(p(i))= counter(p(i))+1
      dummy(p(i),counter(p(i)))=y(i)
   enddo
   do i=1,n
      if(counter(p(i))>0)then
         call heapsort(counter(p(i)),dummy(p(i),:))
      endif
   enddo
 ! write(*,*)dummy
   !!!!!!!!!!!!!!ここまで並び替え
   do k=1,m
      do j=1,m
         do i=1,n
           if(dummy(i,j)==y(k))write(*,*)itoa(1000000*p(k)+j)
         enddo
      enddo
   enddo
contains


subroutine heapsort(n,array)
  implicit none
  integer,intent(in) :: n
  integer,intent(inout) :: array(1:n)
  
  integer ::i,k,j,l
  integer :: t
  
  if(n.le.0)then
     write(6,*)"Error, at heapsort"; stop
  endif
  if(n.eq.1)return

  l=n/2+1
  k=n
  do while(k.ne.1)
     if(l.gt.1)then
        l=l-1
        t=array(L)
     else
        t=array(k)
        array(k)=array(1)
        k=k-1
        if(k.eq.1) then
           array(1)=t
           exit
        endif
     endif
     i=l
     j=l+l
     do while(j.le.k)
        if(j.lt.k)then
           if(array(j).lt.array(j+1))j=j+1
        endif
        if (t.lt.array(j))then
           array(i)=array(j)
           i=j
           j=j+j
        else
           j=k+1
        endif
     enddo
     array(i)=t
  enddo

  return
end subroutine heapsort

  function itoa(i) result(a)
    integer,intent(in) :: i
    character(len=12) :: a
    integer :: ii,num,ibuf
    ibuf=i
    do ii=12,1,-1
       num=modulo(ibuf,10)
       a(ii:ii)=char(ichar("0")+num)
       ibuf=ibuf/10
    end do
  end function itoa
end program main

