#include <iostream>
#include <sstream>
#include <vector>
#include <list>
#include <string>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <algorithm>
#include <numeric>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
using namespace std;

typedef vector <string> VS;
istream& myis = cin;
ostream& myos = cout;

const string END_OF_TEXT = "END_OF_TEXT";
const string END_OF_COMMAND = "-";

// prototypes
VS solve( const VS& lines );
void output( const VS& lines );
void split( const VS& lines, VS& T, VS& C );

// editor
class Editor {
public:
    // members
    VS T;
    int r;
    int c;
    string buffer;
    // methods
    Editor( VS T ): T(T) {
        r = 0;
        c = 0;
        buffer.clear();
    }
    VS get_text() { return T; }
    void parse_command( string com ) {
        if ( com == "a" ) command_a();
        if ( com == "e" ) command_e();
        if ( com == "p" ) command_p();
        if ( com == "n" ) command_n();
        if ( com == "f" ) command_f();
        if ( com == "b" ) command_b();
        if ( com == "d" ) command_d();
        if ( com == "k" ) command_k();
        if ( com == "y" ) command_y();
        // debug
        /*
        myos << "::: debug start :::" << endl;
        myos << "r: " << r << endl;
        myos << "c: " << c << endl;
        myos << "b: " << buffer << endl;
        myos << "::: debug end   :::" << endl;
        */
    }
private:
    bool is_cursor_leftmost() {
        return c == 0;
    }
    bool is_cursor_rightmost() {
        return c == (int)T[r].size();
    }
    bool exist_next_upperline() {
        return r - 1 >= 0;
    }
    bool exist_next_lowerline() {
        return r + 1 < (int)T.size();
    }
    void delete_line( int n ) {
        T.erase( T.begin() + n );
    }
    void delete_letter( int n ) {
        T[r].erase( T[r].begin() + n );
    }
    void move_cursor_leftmost() {
        c = 0;
    }
    void move_cursor_rightmost() {
        c = T[r].size();
    }
    void move_cursor_up() {
        r -= 1;
    }
    void move_cursor_down() {
        r += 1;
    }
    void move_cursor_left() {
        c -= 1;
    }
    void move_cursor_right() {
        c += 1;
    }
    void move_cursor_right( int n ) {
        c += n;
    }
    void command_a() {
        /*
          J[\ð»ÝÌsÌæª¶ÉÚ®·éB
        */
        move_cursor_leftmost();
    }
    void command_e() {
        /*
          J[\ð»ÝÌsÌsÉÚ®·éB
        */
        move_cursor_rightmost();
    }
    void command_p() {
        /*
          ãÉsª êÎAJ[\ðãÌsÌæª¶ÉÚ®·éB
          ãÉsªÈ¯êÎAJ[\ðæª¶ÉÚ®·éB
        */
        if ( exist_next_upperline() ) {
            move_cursor_up();
            move_cursor_leftmost();
        } else {
            move_cursor_leftmost();
        }
    }
    void command_n() {
        /*
          ºÉsª êÎAJ[\ðºÌsÌæª¶ÉÚ®·éB
          ºÉsªÈ¯êÎAJ[\ðæª¶ÉÚ®·éB
        */
        if ( exist_next_lowerline() ) {
            move_cursor_down();
            move_cursor_leftmost();
        } else {
            move_cursor_leftmost();
        }
    }
    void command_f() {
        /*
          J[\ªsÉÈ¢êAJ[\ðPÂEÉÚ®·éB
          J[\ªsÉ è©ÂºÉsª éêÍAJ[\ðºÌsÌæª¶ÖÚ®·éB
          »êÈOÌêÍ½àµÈ¢B
        */
        if ( ! is_cursor_rightmost() ) {
            move_cursor_right();
        } else if ( is_cursor_rightmost() && exist_next_lowerline() ) {
            move_cursor_down();
            move_cursor_leftmost();
        }
    }
    void command_b() {
        /*
          J[\ªæª¶ÅÈ¢ÈçÎAJ[\ðPÂ¶ÉÚ®·éB
          J[\ªæª¶Å è©ÂãÉsª éêÍAJ[\ðãÌsÌsÖÚ®·éB
          »êÈOÌêÍ½àµÈ¢B
        */
        if ( ! is_cursor_leftmost() ) {
            move_cursor_left();
        } else if ( is_cursor_leftmost() && exist_next_upperline() ) {
            move_cursor_up();
            move_cursor_rightmost();
        }
    }
    void command_d() {
        /*
          J[\ª¶ðw·êÍ»Ì¶ðí·éBí³ê½¶ÌE¤Ì¶ñÍ¶ÉVtg³êéB
          J[\ªsðwµºÉsª éêÍAºÌsð»ÌÜÜJ[\ÌÊuÉq°éBÈºÌsÍãÉVtg³êéB
          »êÈOÌêÍ½àµÈ¢B
        */
        if ( ! is_cursor_rightmost() ) {
            delete_letter( c );
        } else if ( is_cursor_rightmost() && exist_next_lowerline() ) {
            T[r] += T[r+1];
            delete_line( r+1 );
        }
    }
    void command_k() {
        /*
          J[\ªsÉ éêAºÉsª êÎAd Æ¯¶ìðµobt@ÉPÂÌüsðL^·éB
          J[\ªsÉÈ¢êAJ[\ªw·¶ðÜß½E¤·×ÄÌ¶ðØèæè»êðobt@ÉL^·éB
          J[\Í³ÌsÌsðw·æ¤ÉÈéBSÄÌL^É¨¢Äobt@Íã«³êéB
          »êÈOÌêÍ½àµÈ¢B
        */
        if ( is_cursor_rightmost() && exist_next_lowerline() ) {
            T[r] += T[r+1];
            delete_line( r+1 );
            buffer = "\n";
        } else if ( ! is_cursor_rightmost() ) {
            buffer = T[r].substr( c );
            T[r] = T[r].substr( 0, c );
            move_cursor_rightmost();
        }
    }
    void command_y() {
        /*
          obt@ªóÌêÍÈÉàµÈ¢B
          obt@ÌàeªüsÌêAJ[\ªw·¶Ì¼OÅüsðs¤BJ[\ÍVµ¢sÌæª¶ÖÚ®·éB
          obt@Ìàeª¶ñÌêAJ[\ÌÊuÉobt@Ìàeð}ü·éB
          J[\ÌÊuÍàÆàÆwµÄ¢½¶Ü½ÍsÌêÖÚ®·éB
        */
        if ( buffer == "\n" ) {
            string tmp = T[r].substr( c );
            T[r] = T[r].substr( 0, c );
            T.insert( T.begin() + r + 1, tmp );
            move_cursor_down();
            move_cursor_leftmost();
        } else if ( buffer != "" ) {
            string left = T[r].substr( 0, c );
            string right = T[r].substr( c );
            T[r] = left + buffer + right;
            move_cursor_right( buffer.size() );
        }
    }
};

// functions

VS solve( const VS& lines ) {
    VS T; // text
    VS C; // commands
    split( lines, T, C );

    if ( T.empty() ) return T;

    // edit
    Editor editor( T );
    for ( VS ::iterator it_i = C.begin(); it_i != C.end(); ++ it_i ) {
        string command = *it_i;
        editor.parse_command( command );
    }
    return editor.get_text();
}

void output( const VS& lines ) {
    for ( VS ::const_iterator it_i = lines.begin(); it_i != lines.end(); ++ it_i ) {
        myos << *it_i << endl;
    }
}

void split( const VS& lines, VS& T, VS& C ) {
    T.clear();
    C.clear();
    VS ::const_iterator it_lines = lines.begin();
    for ( ; it_lines != lines.end(); ++ it_lines ) {
        const string& line = *it_lines;
        if ( line == END_OF_TEXT ) {
            ++ it_lines;
            break;
        }
        T.push_back( line );
    }
    for ( ; it_lines != lines.end(); ++ it_lines ) {
        const string& line = *it_lines;
        if ( line == END_OF_COMMAND ) {
            break;
        }
        C.push_back( line );
    }
}

int main() {
    VS lines;
    string line;
    while ( getline( myis, line ) ) lines.push_back( line );

    VS answer = solve( lines );
    output( answer );
    
    return 0;
}

Measured memory_bytes: 0