#include <iostream>
#include <sstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <climits>
#include <cfloat>
using namespace std;

int main()
{
    list<list<char> > text;
    for(;;){
        string s;
        getline(cin, s);
        if(s == "END_OF_TEXT")
            break;
        text.push_back(list<char>(s.begin(), s.end()));
    }

    list<list<char> >::iterator it1 = text.begin();
    list<char>::iterator it2 = it1->begin();
    list<char> buffer;
    bool newLine = false;
    for(;;){
        char c;
        cin >> c;
        if(c == 'a'){
            it2 = it1->begin();
        }else if(c == 'e'){
            it2 = it1->end();
        }else if(c == 'p'){
            if(it1 != text.begin())
                -- it1;
            it2 = it1->begin();
        }else if(c == 'n'){
            ++ it1;
            if(it1 == text.end())
                -- it1;
            it2 = it1->begin();
        }else if(c == 'f'){
            if(it2 != it1->end()){
                ++ it2;
            }else{
                ++ it1;
                if(it1 != text.end()){
                    it2 = it1->begin();
                }else{
                    -- it1;
                }
            }
        }else if(c == 'b'){
            if(it2 != it1->begin()){
                -- it2;
            }else{
                if(it1 != text.begin()){
                    -- it1;
                    it2 = it1->end();
                }
            }
        }else if(c == 'd'){
            if(it2 != it1->end()){
                it2 = it1->erase(it2);
            }else{
                list<list<char> >::iterator tmp = it1;
                ++ tmp;
                if(tmp != text.end()){
                    it2 = it1->insert(it2, '.');
                    it1->splice(it1->end(), *tmp);
                    text.erase(tmp);
                    it2 = it1->erase(it2);
                }
            }
        }else if(c == 'k'){
            buffer.clear();
            newLine = false;
            if(it2 == it1->end()){
                list<list<char> >::iterator tmp = it1;
                ++ tmp;
                if(tmp != text.end()){
                    it2 = it1->insert(it2, '.');
                    it1->splice(it1->end(), *tmp);
                    text.erase(tmp);
                    it2 = it1->erase(it2);
                    newLine = true;
                }
            }else{
                buffer.splice(buffer.end(), *it1, it2, it1->end());
                it2 = it1->end();
            }
        }else if(c == 'y'){
            if(newLine){
                list<list<char> >::iterator tmp = it1;
                ++ it1;
                it1 = text.insert(it1, list<char>());
                it1->splice(it1->end(), *tmp, it2, tmp->end());
                it2 = it1->begin();
            }else if(buffer.size() > 0){
                list<char> tmp = buffer;
                it1->splice(it2, tmp);
            }
        }else{
            break;
        }
    }

    it1 = text.begin();
    do{
        cout << string(it1->begin(), it1->end()) << endl;
    }while(++it1 != text.end());

    return 0;
}

Measured memory_bytes: 0