#include<iostream>
#include<string>
#include<algorithm>
#include<map>
#include<vector>
#include<deque>
using namespace std;

class Emacs {

    private:
    
        string buffer;
        int cursorX;
        int cursorY;
        vector<string> body;

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

        void acceptCommand(char c) {
            switch (c) {
                case 'a':
                    moveCursorMostLeft();
                    break;
                case 'e':
                    moveCursorMostRight();
                    break;
                case 'p':
                    moveCursorUp();
                    moveCursorMostLeft();
                    break;
                case 'n':
                    moveCursorDown();
                    moveCursorMostLeft();
                    break;
                case 'f':
                    moveCursorForward();
                    break;
                case 'b':
                    moveCursorBackward();
                    break;
                case 'd':
                    deleteChar();
                    break;
                case 'k':
                    killChar();
                    break;
                case 'y':
                    yankBuffer();
                    break;
            }
        }

        void yankBuffer() {
            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;
                moveCursorMostLeft();
            } else {
                body[cursorY - 1]  = cl.substr(0, cursorX - 1) + buffer + cl.substr(cursorX - 1);
                cursorX += buffer.length();
            }
        }

        void deleteChar() {

            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 killChar() {
            string cl = getCurrentLine();

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

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

        void moveCursorBackward() {
            if (cursorX > 1) {
                cursorX -= 1;
            } else if (cursorY > 1) {
                cursorY -= 1;
                moveCursorMostRight();
            }
        }

        void moveCursorMostLeft() {
            cursorX = 1;
        }

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

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

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

        void printBody() {
            // System.out.printf("Cursor: (%d, %d)\n", cursorX, cursorY);
            // System.out.printf("Buffer: %s\n",   buffer);
            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);
    }

    Emacs emacs (lines);

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

    emacs.printBody();
     
    return 0;
}

Measured memory_bytes: 0