program falling_asleep
  implicit none
  integer :: n, t(50) = 0, i, m = 0
  character(100) :: s(50) = "", x
  logical :: sleeping = .false.
  read(*,*) n
  do i = 1, n
    read(*,*) s(i), t(i)
  end do
  read(*,*) x
  do i = 1, n
    if (sleeping) m = m+t(i)
    if (s(i) == x) sleeping = .true.
  end do
  write(*,'(i0)') m
end program falling_asleep