program name
    implicit none
    integer(4):: n,m
    integer(4),allocatable:: p(:),y(:),num(:),ind(:)
    integer(4):: i,ni
    integer(4),allocatable:: ans(:)

    read*, n,m
    allocate(p(m),y(m),num(m),ans(m),ind(m))
    do i=1,m
        read*, p(i),y(i)
        ind(i) = i
    end do

    call triple_sort(p,y,ind,1,m)
    call triple_sort(y,p,ind,1,m)

    num(m) = 1
    ans(m) =1000000*p(m)+num(m)
    do i=m-1,1,-1
        if (p(i) == p(i+1)) then
            num(i) = num(i+1)+1
        else
            num(i) = 1
        end if
        ans(i) = 1000000*p(i)+num(i)
    end do

    call triple_sort(ind,num,p,1,m)

    do i=m,1,-1
        print'(2i6.6)',p(i),num(i) 
    end do
    

contains
    recursive subroutine triple_sort(ar1, ar2, ar3, fst, lst)
        integer(4),intent(inout):: ar1(:),ar2(:),ar3(:)
        integer(4),intent(in):: fst,lst
        integer(4):: mdl

        if (lst - fst < 2) then
            if (ar1(fst) < ar1(lst)) then
                call swap(ar1(fst), ar1(lst))
                call swap(ar2(fst), ar2(lst))
                call swap(ar3(fst), ar3(lst))
            end if
            return
        end if

        mdl = (fst+lst)/2

        call triple_sort(ar1,ar2,ar3,fst,mdl)
        call triple_sort(ar1,ar2,ar3,mdl+1,lst)
        call triple_merge_(ar1,ar2,ar3,fst,mdl,lst)
    end subroutine


    subroutine triple_merge_(ar1,ar2,ar3,fst,mdl,lst)
        integer(4),intent(inout):: ar1(:),ar2(:),ar3(:)
        integer(4),intent(in):: fst,mdl,lst
        integer(4),allocatable:: t1(:),t2(:),t3(:)
        integer(4):: li,ri,ti

        allocate(t1(lst-fst+1), t2(lst-fst+1), t3(lst-fst+1))

        li=fst
        ri=mdl+1
        ti=1

        do while(li <= mdl .and. ri <= lst)
            if (ar1(li) >= ar1(ri)) then
                t1(ti) = ar1(li) 
                t2(ti) = ar2(li)
                t3(ti) = ar3(li)
                li=li+1
            else
                t1(ti) = ar1(ri)
                t2(ti) = ar2(ri)
                t3(ti) = ar3(ri)
                ri=ri+1
            end if
            ti=ti+1
        end do

        if (li <= mdl) then
            t1(ti:) = ar1(li:mdl)
            t2(ti:) = ar2(li:mdl)
            t3(ti:) = ar3(li:mdl)
        else
            t1(ti:) = ar1(ri:lst)
            t2(ti:) = ar2(ri:lst)
            t3(ti:) = ar3(ri:lst)
        end if

        ar1(fst:lst) = t1(:)
        ar2(fst:lst) = t2(:)
        ar3(fst:lst) = t3(:)

        deallocate(t1,t2,t3)
    end subroutine


    recursive subroutine double_sort(ar1, ar2, fst, lst)
        integer(4),intent(inout):: ar1(:),ar2(:)
        integer(4),intent(in):: fst,lst
        integer(4):: mdl

        if (lst - fst < 2) then
            if (ar1(fst) < ar1(lst)) then
                call swap(ar1(fst), ar1(lst))
                call swap(ar2(fst), ar2(lst))
            end if
            return
        end if

        mdl = (fst+lst)/2

        call double_sort(ar1,ar2,fst,mdl)
        call double_sort(ar1,ar2,mdl+1,lst)
        call double_merge_(ar1,ar2,fst,mdl,lst)
    end subroutine


    subroutine double_merge_(ar1,ar2,fst,mdl,lst)
        integer(4),intent(inout):: ar1(:),ar2(:)
        integer(4),intent(in):: fst,mdl,lst
        integer(4),allocatable:: t1(:),t2(:)
        integer(4):: li,ri,ti

        allocate(t1(lst-fst+1), t2(lst-fst+1))

        li=fst
        ri=mdl+1
        ti=1

        do while(li <= mdl .and. ri <= lst)
            if (ar1(li) >= ar1(ri)) then
                t1(ti) = ar1(li) 
                t2(ti) = ar2(li)
                li=li+1
            else
                t1(ti) = ar1(ri)
                t2(ti) = ar2(ri)
                ri=ri+1
            end if
            ti=ti+1
        end do

        if (li <= mdl) then
            t1(ti:) = ar1(li:mdl)
            t2(ti:) = ar2(li:mdl)
        else
            t1(ti:) = ar1(ri:lst)
            t2(ti:) = ar2(ri:lst)
        end if

        ar1(fst:lst) = t1(:)
        ar2(fst:lst) = t2(:)

        deallocate(t1,t2)
    end subroutine

    
    subroutine swap(x,y)
        integer(4),intent(inout):: x,y
        integer(4):: tmp
        tmp = x
        x = y
        y = tmp
    end subroutine
end program name