#include<stdio.h>
#include<stdlib.h>
#include<string.h>

char inputString[1000][1000];
char listOfCommands[305];
char buffer[1000] = "";

int numberOfLines = 0;
int numberOfCommands = 0;

int cursorXCordinate = 0;
int cursorYCordinate = 0;


void textInput() {

    int indexOfString = 0;
    int indexOfCommand = 0;
    char dummyString[1000];
    char dummyCommand;
   // freopen("input.txt", "r", stdin);
    while ( scanf("%s",dummyString)!=EOF) {
        getchar();
        if ( !strcmp(dummyString, "END_OF_TEXT") ) break;
        for ( int i = 0; i<=strlen(dummyString); i++ ) {
            inputString[indexOfString][i] = dummyString[i];

        }
        indexOfString++;
    }

    while( scanf("%c", &dummyCommand)!=EOF ) {
        getchar();
        if( dummyCommand == '-' ) break;
        listOfCommands[indexOfCommand] = dummyCommand;
        indexOfCommand++;
    }
    numberOfLines = indexOfString - 1;
    numberOfCommands = indexOfCommand;
    return;
}


void goToFirstPosition() { // a
    cursorYCordinate = 0;
}


void goToLastPosition() { // e
    cursorYCordinate = strlen(inputString[cursorXCordinate]);
}


void moveToUpperLine() {

    if (cursorXCordinate > 0) {
        cursorXCordinate -= 1;
    }
    // cursorYCordinate = 0;
}

void moveTOLowerLine() {

    if (cursorXCordinate < numberOfLines) {
        cursorXCordinate += 1;
    }
    //cursorYCordinate = 0;

}


void moveByOnePlaceToTheRight() {

    int currentStringLen = strlen(inputString[ cursorXCordinate ]);
    if( cursorYCordinate == currentStringLen ) {
        if ( numberOfLines - cursorXCordinate > 0 ) {
            cursorXCordinate += 1;
            cursorYCordinate  = 0;
        }
    } else {
        if( cursorYCordinate < currentStringLen )
            cursorYCordinate += 1;
    }
}


void moveByOnePlaceToTheLeft() {

    int upperStringLen;
    int currentStringLen = strlen(inputString[ cursorXCordinate ]);
    if ( cursorYCordinate == 0 ) {
        if ( cursorXCordinate > 0 ) {
            cursorXCordinate -= 1;
            upperStringLen = strlen( inputString[cursorXCordinate] );
            cursorYCordinate = upperStringLen;
        }
    } else {
        if ( cursorYCordinate >  0 ) {
            cursorYCordinate -= 1;
        }
    }
}


void deleteSingleCharacter() {

    int currentStringLen = strlen(inputString[cursorXCordinate]);
    char referenceString[1000] = "";
    int temp = 0;
    if ( cursorYCordinate < currentStringLen ) {
        for( int i = 0; i < currentStringLen; i++) {
            if ( i != cursorYCordinate) {
                referenceString[temp] = inputString[cursorXCordinate][i];
                temp++;
            }

        }
        strcpy(inputString[cursorXCordinate], referenceString);
    } else if( cursorYCordinate == currentStringLen ) {
        if ( numberOfLines - cursorXCordinate > 0 ) {
            strcat( inputString[cursorXCordinate], inputString[cursorXCordinate+1]);
            for(int i = cursorXCordinate+1; i<numberOfLines; i++) strcpy(inputString[i], inputString[i+1]);
            strcpy(inputString[numberOfLines], "");
        }
    }
}


void copyText() {

    int currentStringLen = strlen(inputString[cursorXCordinate]);
    if ( currentStringLen == cursorYCordinate ) {
        if ( numberOfLines - cursorXCordinate > 0 ) {
            strcat( inputString[cursorXCordinate], inputString[cursorXCordinate+1]);
            for(int i = cursorXCordinate+1; i<numberOfLines; i++) strcpy(inputString[i], inputString[i+1]);
            strcpy(inputString[numberOfLines], "");
        }
    } else if ( cursorYCordinate < currentStringLen ) {
        strcpy(buffer, "");
        for ( int i = cursorYCordinate; i<=currentStringLen; i++)
            buffer[i-cursorYCordinate] = inputString[cursorXCordinate][i];
        for ( int i = cursorYCordinate; i<=currentStringLen; i++)
            inputString[cursorXCordinate][i] = NULL;

    }
}


void yank() {

    int currentStringLen = strlen( inputString[cursorXCordinate]);

    if ( strlen(buffer) ) {
        if( cursorYCordinate < currentStringLen) {
            char firstPart[1000] = "";
            char secondPart[1000] = "";
            int j = 0;
            for(int i = 0; i<cursorYCordinate; i++) firstPart[i] = inputString[cursorXCordinate][i];
            strcat(firstPart, buffer);
            for(int i = cursorYCordinate; i<currentStringLen; i++)
                secondPart[j++] = inputString[cursorXCordinate][i];
            strcat(firstPart, secondPart);
            strcat(inputString[cursorXCordinate], firstPart);
            cursorYCordinate += strlen(buffer);
        }
        else {
        strcat(inputString[cursorXCordinate], buffer);
        cursorYCordinate += strlen(buffer);
        }
    }
}


int main() {

    textInput();
    goToFirstPosition();
    for(int i = 0; i<numberOfCommands; i++) {
        if( listOfCommands[i]  == 'a') goToFirstPosition();
        if ( listOfCommands[i] == 'e') goToLastPosition();
        if ( listOfCommands[i] == 'p') moveToUpperLine();
        if ( listOfCommands[i] == 'n') moveTOLowerLine();
        if ( listOfCommands[i] == 'f') moveByOnePlaceToTheRight();
        if ( listOfCommands[i] == 'b') moveByOnePlaceToTheLeft();
        if ( listOfCommands[i] == 'd') deleteSingleCharacter();
        if ( listOfCommands[i] == 'k') copyText();
        if ( listOfCommands[i] == 'y') yank();
       // printf("%c_____________________\n",listOfCommands[i]);
        //printf("%s\n",inputString[0]);
        //printf("%s\n",inputString[1]);
        //printf("buffer-- %s\n",buffer);
        //printf("(%d,%d)\n", cursorXCordinate, cursorYCordinate);

    }
    for(int i = 0; i<=numberOfLines; i++) if(strlen(inputString[i]))printf("%s\n",inputString[i]);
    //printf("%c", inputString[cursorXCordinate][cursorYCordinate]);
    //goToLastPosition();

    return 0;
}