#include<iostream>
#include<string>
#include<vector>

using namespace std;

class Editor {

    private:
        //local variable declaration
        string buffer;
        int cursorX;
        int cursorY;
        vector<string> body;

    public:
        Editor(vector<string> arg) {
            buffer = "";
            cursorX = 1;
            cursorY = 1;
            body = arg;
        }

        //Set a motion for each case
        void acceptCommand(char c) {
            switch (c) {
                case 'a':
                    move_to_head();
                    break;
                case 'e':
                    move_to_tail();
                    break;
                case 'p':
                    move_up();
                    move_to_head();
                    break;
                case 'n':
                    move_down();
                    move_to_head();
                    break;
                case 'f':
                    move_forward();
                    break;
                case 'b':
                    move_backward();
                    break;
                case 'd':
                    delete_char();
                    break;
                case 'k':
                    cut_tails();
                    break;
                case 'y':
                    paste();
                    break;
            }
        }

        void paste() {
            string cl = getCurrentLine();
            if (buffer == "") {
                return;
            } else if (buffer == "!") {
                body.push_back(body[body.size() - 1]);
                for(int i = body.size() - 1; i >= cursorY + 1; i--) {
                    body[i] = body[i - 1];
                }
                body[cursorY] =  cl.substr(cursorX - 1);
                body[cursorY - 1] = cl.substr(0, cursorX - 1);
                cursorY += 1;
                move_to_head();
            } else {
                body[cursorY - 1]  = cl.substr(0, cursorX - 1) + buffer + cl.substr(cursorX - 1);
                cursorX += buffer.length();
            }
        }

        void delete_char() {

            //local variable declaration
            string cl = getCurrentLine();

            if (cursorX <= cl.length()) {
                string tmp1 = cl.substr(0, cursorX - 1);
                string tmp2 = cl.substr(cursorX);
                body[cursorY - 1] = tmp1 + tmp2;
            }else if (cursorY < body.size()) {
                body[cursorY - 1] = cl + body[cursorY];
                for(int i = cursorY; i < body.size() - 1; i++) {
                    body[i] = body[i+1];
                }
                body.pop_back();
            }
        }

        void cut_tails() {
            string cl = getCurrentLine();

            if (cursorX <= cl.length()) {
                buffer = cl.substr(cursorX - 1);
                body[cursorY - 1] = cl.substr(0, cursorX - 1);
            }else {
                buffer = "!";
                delete_char();
            }
        }

        void move_forward() {
            if (cursorX <= getCurrentLine().length()) {
                cursorX += 1;
            }else if (cursorY < body.size()) {
                cursorY += 1;
                move_to_head();
            }
        }

        void move_backward() {
            if (cursorX > 1) {
                cursorX -= 1;
            }else if (cursorY > 1) {
                cursorY -= 1;
                move_to_tail();
            }
        }

        void move_to_head() {
            cursorX = 1;
        }

        void move_to_tail() {
            cursorX = body[cursorY - 1].length() + 1;
        }

        void move_up() {
            if(cursorY > 1)    cursorY -= 1;
        }

        void move_down() {
            if(cursorY < body.size())  cursorY += 1;
        }

        void printBody() {
            for (int i = 0; i < body.size(); i++) {
                cout << body[i] << endl;
            }
        }

        string getCurrentLine() {
            return body[cursorY - 1];
        }

};

int main() {

    vector<string> lines;

    while (true) {
        string line;
        getline(cin, line);
        if (line == "END_OF_TEXT") break;
        lines.push_back(line);
    }

    Editor editor (lines);

    while (true) {
        char c;
        cin >> c;
        if(c == '-')    break;
        editor.acceptCommand(c);
    }
    editor.printBody();

    return 0;
}



Measured memory_bytes: 3168