program id
  implicit none
  integer               :: n, m
  integer, allocatable  :: info(:,:), idx(:)
  integer               :: i, p
  read(*,*) n, m
  allocate(info(m,4),idx(n))
  idx = 0
  do i = 1, m
     read(*,*) info(i,1), info(i,2)
     info(i,3) = i
     info(i,4) = 0
  end do
  call merge_sort(m,info,2)
  do i = 1, m
     p = info(i,1)
     idx(p) = idx(p)+1
     info(i,4) = idx(p)
  end do
  call merge_sort(m,info,3)
  do i = 1, m
     write(*,'(i6.6,i6.6)') info(i,1), info(i,4)
  end do
  stop
contains
  subroutine merge_sort(n,c,idx)
    implicit none
    integer, intent(in)    :: n, idx
    integer, intent(inout) :: c(n,4)
    integer                :: number, length, i, ubound
    number = n
    length = 1
    do while (number .gt. 1)
       do i = 1, number/2
          ubound = min(2*i*length,n)
          call merge(c(2*(i-1)*length+1:(2*i-1)*length,:),c((2*i-1)*length+1:ubound,:),idx)
       end do
       length = 2*length
       number = (number+1)/2
    end do
    return
  end subroutine merge_sort
  subroutine merge(c1,c2,idx)
    implicit none
    integer, intent(inout) :: c1(:,:), c2(:,:)
    integer, intent(in)    :: idx
    integer                :: c(size(c1(:,1))+size(c2(:,1)),size(c1(1,:)))
    integer                :: i1, i2, n1, n2
    i1 = 1
    i2 = 1
    n1 = size(c1(:,1))
    n2 = size(c2(:,1))
    do while (i1 .le. n1 .and. i2 .le. n2)
       if (c1(i1,idx) .lt. c2(i2,idx)) then
          c(i1+i2-1,:) = c1(i1,:)
          i1 = i1+1
       else
          c(i1+i2-1,:) = c2(i2,:)
          i2 = i2+1
       end if
    end do
    if (i1 .le. n1) then
       c(i1+i2-1:n1+n2,:) = c1(i1:n1,:)
    else if (i2 .le. n2) then
       c(i1+i2-1:n1+n2,:) = c2(i2:n2,:)
    end if
    c1 = c(1:n1,:)
    c2 = c(n1+1:n1+n2,:)
    return
  end subroutine merge
end program id