program Guidebook
    implicit none
    integer(4):: n,i,j
    integer(4),allocatable:: p(:), ind(:)
    character(10),allocatable:: s(:)
    logical:: changed

    read*, n
    allocate(p(n), s(n),ind(n))

    do i=1,n
        read*, s(i),p(i)
        ind(i) = i
    end do

    changed = .true.
    do while(changed)
        changed=.false.
        do i=1, n-1
            if (s(i+1) < s(i)) then
                changed = .true.
                call cswap(s(i),s(i+1))
                call swap(p(i),p(i+1))
                call swap(ind(i),ind(i+1))
            else if (s(i+1) == s(i) .and. p(i+1) > p(i)) then
                changed = .true.
                call swap(p(i),p(i+1))
                call swap(ind(i),ind(i+1))
            end if
        end do
    end do

    do i=1,n
        print*, ind(i)
    end do

contains
subroutine swap(x,y)
    integer(4),intent(inout):: x,y
    integer(4):: tmp

    tmp = x
    x = y
    y = tmp
end subroutine

subroutine cswap(c1, c2)
    character(10),intent(inout)::c1,c2
    character(10):: tmp

    tmp = c1
    c1 = c2
    c2 = tmp
end subroutine
end program Guidebook