## main

texts = []
buffer = ""

loop do
  line = gets.strip
  break if line == 'END_OF_TEXT'

  texts << line
end

cx = cy = 0

#p [cx, cy]
#p texts

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

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

for line in texts
  puts line
end