#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ª êÎAJ[\ðãÌsÌæª¶ÉÚ®·éB
          ãÉsªÈ¯êÎAJ[\ðæª¶ÉÚ®·éB
        */
        if ( exist_next_upperline() ) {
            move_cursor_up();
            move_cursor_leftmost();
        } else {
            move_cursor_leftmost();
        }
    }
    void command_n() {
        /*
          ºÉsª êÎAJ[\ðºÌsÌæª¶ÉÚ®·éB
          ºÉsªÈ¯êÎAJ[\ðæª¶ÉÚ®·éB
        */
        if ( exist_next_lowerline() ) {
            move_cursor_down();
            move_cursor_leftmost();
        } else {
            move_cursor_leftmost();
        }
    }
    void command_f() {
        /*
          J[\ªsÉÈ¢êAJ[\ðPÂEÉÚ®·éB
          J[\ªsÉ è©ÂºÉsª éêÍAJ[\ðºÌ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[\ªæª¶ÅÈ¢ÈçÎAJ[\ðPÂ¶ÉÚ®·éB
          J[\ªæª¶Å è©ÂãÉsª éêÍAJ[\ðãÌ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¤Ì¶ñÍ¶ÉVtg³êéB
          J[\ªsðwµºÉsª éêÍAºÌsð»ÌÜÜJ[\ÌÊuÉq°éBÈºÌsÍãÉVtg³êé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 Æ¯¶ìðµobt@ÉPÂÌüsðL^·éB
          J[\ªsÉÈ¢êAJ[\ªw·¶ðÜß½E¤·×ÄÌ¶ðØèæè»êðobt@ÉL^·éB
          J[\Í³ÌsÌsðw·æ¤ÉÈéBSÄÌL^É¨¢Äobt@Íã«³êé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() {
        /*
          obt@ªóÌêÍÈÉàµÈ¢B
          obt@ÌàeªüsÌêAJ[\ªw·¶Ì¼OÅüsðs¤BJ[\ÍVµ¢sÌæª¶ÖÚ®·éB
          obt@Ìàeª¶ñÌêAJ[\ÌÊuÉobt@Ìà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;
}