## subroutines

def bol(text, pos)
  while pos > 0 && text[pos - 1] != "\n"
    pos -= 1
  end
  pos
end

def eol(text, pos)
  while pos < text.length && text[pos] != "\n"
    pos += 1
  end
  pos
end

### main

text = ""
buffer = ""

loop do
  line = gets
  break if line == "END_OF_TEXT\n"

  text += line
end

pos = 0

loop do
  cmd = gets.strip
  break if cmd == '-'

  case cmd
  when 'a'
    pos = bol(text, pos)
  when 'e'
    pos = eol(text, pos)
  when 'p'
    pos = bol(text, pos)
    if pos > 0
      pos = bol(text, pos - 1)
    end
  when 'n'
    pos0 = eol(text, pos)
    if pos0 < text.length
      pos0 += 1
    end
    pos = bol(text, pos0)
  when 'f'
    pos += 1 if pos < text.length
  when 'b'
    pos -= 1 if pos > 0
  when 'd'
    if pos < text.length
      text[pos] = ''
    end
  when 'k'
    if text[pos] == "\n"
      text[pos] = ''
      buffer = "\n"
    else
      pos0 = eol(text, pos)
      buffer = text[pos...pos0]
      text[pos...pos0] = ''
    end
  when 'y'
    text[pos, 0] = buffer
    pos += buffer.length
  end
  #p [cmd, cx, cy, buffer]
  #p texts
end

puts text if text != ""