#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <cassert>
using namespace std;

#define REP(i,n) for(int i = 0; i < (int)(n); ++i)
#define FOR(i,a,b) for (int i = a; i < (int)(b); ++i)
#define FORD(i,a,b) for (int i = a; i >= (int)(b); --i)
#define FOREACH(it,c) for(typeof(c.begin()) it = c.begin(); it != c.end(); ++it)

int x = 0, y = 0;
vector<string> buf;

int main() {
  vector<string> lines;
  string str;
  while (getline(cin,str)) {
    if (str == "END_OF_TEXT") break; 
    lines.push_back(str);
  }
  char c;
  while (cin>>c, c != '-') {
    switch (c) {
    case 'a': {
      x = 0;
    } break;
    case 'e': {
      x = lines[y].size();
    } break;
    case 'p': {
      if (y != 0) y--;
      x = 0;
    } break;
    case 'n': {
      if (y != lines.size()-1) y++;
      x = 0;
    } break;
    case 'f': {
      if (x != lines[y].size()) x++;
      else if (y != lines.size()-1) {
	y++; x = 0;
      }
    } break;
    case 'b': {
      if (x != 0) x--;
      else if (y != 0) {
	y--; x = lines[y].size();
      }
    } break;
    case 'd': {
      if (x != lines[y].size()) {
	FOR(i,x,lines[y].size()-1) {
	  lines[y][i] = lines[y][i+1];
	}
	lines[y].resize(lines[y].size()-1);
      } else if (y != lines.size()-1) {
	lines[y]+=lines[y+1];
	FOR(i,y+1,lines.size()-1) {
	  lines[i] = lines[i+1];
	}
	lines.resize(lines.size()-1);
      }
    } break;
    case 'k': {
      if (x != lines[y].size()) {
	string tmp;
	FOR(i,x,lines[y].size()) {
	  tmp += lines[y][i];
	}
	lines[y].resize(x);
	buf = vector<string>(1,tmp);
      } else if (y != lines.size()-1) {
	lines[y]+=lines[y+1];
	FOR(i,y+1,lines.size()-1) {
	  lines[i] = lines[i+1];
	}
	lines.resize(lines.size()-1);
	buf = vector<string>(1, "");
      }
    } break;
    case 'y': {
      if (buf.size()) {
	if (buf[0] == "") {
	  string tmp = lines[y].substr(0,x);
	  lines.resize(lines.size()+1);
	  FORD(i,lines.size()-1,y+2) {
	    lines[i] = lines[i-1];
	  }
	  lines[y+1] = lines[y].substr(x);
	  lines[y] = tmp;
	  y++; x = 0;
	} else {
	  string tmp = lines[y].substr(x);
	  lines[y] = lines[y].substr(0,x);
	  lines[y] += buf[0];
	  lines[y] += tmp;
	  x += buf[0].size();
	}
      } 
    } break;
    default: assert(false);
    }
  }
  REP(i,lines.size())
    cout<<lines[i]<<endl;
  return 0;
}