using System;
using System.Collections.Generic;

namespace AOJ1021
{
    class Program
    {
        static void Main()
        {
            var emacs = new Emacs(ReadText());

            foreach (var op in ReadOperation())
            {
                emacs.Operate(op);
            }

            foreach (var line in emacs.Text)
            {
                Console.WriteLine(line);
            }
        }

        public static IEnumerable<string> ReadText()
        {
            string line;
            while ((line = Console.ReadLine()) != null && line != "END_OF_TEXT")
            {
                yield return line;
            }
        }

        public static IEnumerable<Emacs.Operation> ReadOperation()
        {
            string line;
            while ((line = Console.ReadLine()) != null && line != "-")
            {
                switch (line)
                {
                    case "a":
                        yield return Emacs.Operation.Home;
                        break;
                    case "e":
                        yield return Emacs.Operation.End;
                        break;
                    case "p":
                        yield return Emacs.Operation.Up;
                        break;
                    case "n":
                        yield return Emacs.Operation.Down;
                        break;
                    case "f":
                        yield return Emacs.Operation.Right;
                        break;
                    case "b":
                        yield return Emacs.Operation.Left;
                        break;
                    case "d":
                        yield return Emacs.Operation.Delete;
                        break;
                    case "k":
                        yield return Emacs.Operation.Cut;
                        break;
                    case "y":
                        yield return Emacs.Operation.Yank;
                        break;
                }
            }
        }
    }

    public class Emacs
    {
        public List<string> Text { get; private set; }
        public string Buffer { get; private set; }
        public int Line { get; private set; }
        public int Column { get; private set; }

        public Emacs(IEnumerable<string> text)
        {
            Text = new List<string>(text);
            Buffer = null;
            Line = 0;
            Column = 0;
        }

        public void Operate(Operation op)
        {
            switch (op)
            {
                case Operation.Home:
                    Column = 0;
                    break;
                case Operation.End:
                    Column = Text[Line].Length;
                    break;
                case Operation.Up:
                    Column = 0;
                    if (Line != 0)
                    {
                        Line--;
                    }
                    break;
                case Operation.Down:
                    Column = 0;
                    if (Line != Text.Count - 1)
                    {
                        Line++;
                    }
                    break;
                case Operation.Right:
                    if (Column == Text[Line].Length)
                    {
                        if (Line != Text.Count - 1)
                        {
                            Column = 0;
                            Line++;
                        }
                    }
                    else
                    {
                        Column++;
                    }
                    break;
                case Operation.Left:
                    if (Column == 0)
                    {
                        if (Line != 0)
                        {
                            Line--;
                            Column = Text[Line].Length;
                        }
                    }
                    else
                    {
                        Column--;
                    }
                    break;
                case Operation.Delete:
                    if (Column == Text[Line].Length)
                    {
                        if (Line != Text.Count - 1)
                        {
                            var line = Text[Line + 1];
                            Text.RemoveAt(Line + 1);
                            Text[Line] += line;
                        }
                    }
                    else
                    {
                        Text[Line] = Text[Line].Remove(Column, 1);
                    }
                    break;
                case Operation.Cut:
                    if (Column == Text[Line].Length)
                    {
                        if (Line != Text.Count - 1)
                        {
                            var line = Text[Line + 1];
                            Text.RemoveAt(Line + 1);
                            Text[Line] += line;
                            Buffer = "\n";
                        }
                    }
                    else
                    {
                        Buffer = Text[Line].Substring(Column);
                        Text[Line] = Text[Line].Remove(Column);
                    }
                    break;
                case Operation.Yank:
                    if (Buffer == null) break;
                    if (Buffer == "\n")
                    {
                        if (Column == Text[Line].Length)
                        {
                            Text.Insert(Line + 1, "");
                        }
                        else
                        {
                            var temp = Text[Line].Substring(Column);
                            Text[Line] = Text[Line].Remove(Column);
                            Text.Insert(Line + 1, temp);
                        }
                        Line++;
                        Column = 0;
                    }
                    else
                    {
                        Text[Line] = Text[Line].Insert(Column, Buffer);
                        Column += Buffer.Length;
                    }
                    break;
            }
        }

        public enum Operation
        {
            Home,
            End,
            Up,
            Down,
            Right,
            Left,
            Delete,
            Cut,
            Yank
        }
    }
}

Measured memory_bytes: 5848