#include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#include<cstdio>
#include<climits>
#include<cmath>
#include<cstring>
#include<string>

#define f first
#define s second
#define mp make_pair

#define REP(i,n) for(int i=0; i<(int)(n); i++)
#define FOR(i,c) for(__typeof((c).begin()) i=(c).begin(); i!=(c).end(); i++)
#define ALL(c) (c).begin(), (c).end()

using namespace std;

typedef unsigned int uint;
typedef long long ll;

int main(){
  vector<string> buff;
  string str,kill;
  int y = 0, x = 0;
  while(getline(cin,str), str!="END_OF_TEXT") buff.push_back(str);
  while(getline(cin,str), str[0] != '-'){
    //printf("command: %c (x=%d, y=%d)\n",str[0],x,y);
    //REP(i,buff.size()) cout << buff[i] << endl;
    switch(str[0]){
    case 'a':
      x = 0;
      break;
    case 'e':
      x = buff[y].size();
      break;
    case 'p':
      if(y==0) x = 0;
      else{
	x = 0;
	y--;
      }
      break;
    case 'n':
      if(y==buff.size()-1) x=0;
      else{
	x = 0;
	y++;
      }
      break;
    case 'f':
      if(x < buff[y].size()) x++;
      else if(y != buff.size()-1){
	x = 0;
	y++;
      }
      break;
    case 'b':
      if(x > 0) x--;
      else if(y != 0){
	y--;
	x = buff[y].size();
      }
      break;
    case 'd':
      if(x != buff[y].size()){
	buff[y].erase(buff[y].begin()+x);
      }else if(y != buff.size()-1){
	buff[y] += buff[y+1];
	buff.erase(buff.begin() + y + 1);
      }
      break;
    case 'k':
      if(x != buff[y].size()){
	kill = buff[y].substr(x);
	buff[y] = buff[y].substr(0,x);
      }else if(y != buff.size()-1){
	buff[y] += buff[y+1];
	buff.erase(buff.begin() + y + 1);
	kill = "\n";
      }
      break;
    case 'y':
      if(kill != ""){
	if(kill == "\n"){
	  string s = buff[y].substr(x);
	  buff[y] = buff[y].substr(0,x);
	  buff.insert(buff.begin() + y + 1, s);
	  y++; x=0;
	}else{
	  buff[y].insert(x, kill);
	  x += kill.size();
	}
      }
      break;
    }
  }
  REP(i,buff.size()) cout << buff[i] << endl;
  return 0;
}