#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<cassert>
#include<iostream>
#include<sstream>
#include<string>
#include<vector>
#include<queue>
#include<set>
#include<map>
#include<utility>
#include<numeric>
#include<algorithm>
#include<bitset>
#include<complex>
#include<stack>

using namespace std;

typedef long long Int;
typedef vector<int> vint;
typedef pair<int,int> pint;
typedef vector<string> vstring;
typedef vector<pint> vpint;
typedef stringstream SS;

template<class T> void chmin(T &t, T f) { if (t > f) t = f; }
template<class T> void chmax(T &t, T f) { if (t < f) t = f; }

#define rep(i,n) for(int i=0;i<(n);++i)
#define repn(i,m,n) for(int i=(m);i<(n);++i)
#define repd(i,n) for(int i=(n)-1;i>=0;--i)
#define repnd(i,m,n) for(int i=(n)-1;i>=(m);--i)
#define rep0(i,n) for(i=0;i<(n);++i)
#define repn0(i,m,n) for(i=(m);i<(n);++i)
#define repd0(i,n) for(i=(n)-1;i>=0;--i)
#define repnd0(i,m,n) for(i=(n)-1;i>=(m);--i)
#define repc(i,n) for(int i=0;i<=(n);++i)
#define repcn(i,m,n) for(int i=(m);i<=(n);++i)
#define repcd(i,n) for(int i=(n);i>=0;--i)
#define repcnd(i,m,n) for(int i=(n);i>=(m);--i)
#define repc0(i,n) for(i=0;i<=(n);++i)
#define repcn0(i,m,n) for(i=(m);i<=(n);++i)
#define repcd0(i,n) for(i=(n);i>=0;--i)
#define repcnd0(i,m,n) for(i=(n);i>=(m);--i)
#define all(n) n.begin(),n.end()
#define sz(n) ((int)(n).size())
#define IL for(;;)
#define MP make_pair
#define PB push_back
#define X second
#define Y first
#define p_queue(n) priority_queue<n,vector<n>,greater<n> >
#define PUTLINE cout<<"LINE:"<<__LINE__<<endl;

const int INF = 2147483647/3;
const double EPS = 1e-10;
const double PI = acos(-1.0);

const int dx[]={1,-1,0,0,1,1,-1,-1,0};
const int dy[]={0,0,1,-1,1,-1,1,-1,0};

string str[111];

int main() {
	int h = 0;
	IL {
		getline(cin, str[h]);
		if (str[h][sz(str[h]) - 1] < ' ') str[h] = str[h].substr(0, sz(str[h]) - 1);
		if (str[h] == "END_OF_TEXT") break;
		++h;
	}
	int x = 0, y = 0;
	string com, buf = "";
	IL {
		cin >> com;
		if (com == "a") {
			x = 0;
		} else if (com == "e") {
			x = sz(str[y]);
		} else if (com == "p") {
			x = 0;
			if (y > 0) --y;
		} else if (com == "n") {
			x = 0;
			if (y < h - 1) ++y; 
		} else if (com == "f") {
			if (x < sz(str[y])) ++x;
			else if (y < h - 1) {x = 0; ++y;}
		} else if (com == "b") {
			if (x > 0) --x;
			else if (y > 0) {--y; x = sz(str[y]);}
		} else if (com == "d") {
			if (x < sz(str[y])) str[y] = str[y].substr(0, x) + str[y].substr(x + 1);
			else if (y < h - 1) {
				str[y] += str[y + 1];
				--h;
				repn (i, y + 1, h) str[i] = str[i + 1];
			}
		} else if (com == "k") {
			if (x == sz(str[y])) {
				if (y < h - 1) {
					buf = "\n";
					str[y] += str[y + 1];
					--h;
					repn (i, y + 1, h) str[i] = str[i + 1];
				}
			} else {
				buf = str[y].substr(x);
				str[y] = str[y].substr(0, x);
				x = sz(str[y]);
			}
		} else if (com == "y") {
			if (buf == "\n") {
				++h;
				repnd (i, y + 2, h) str[i] = str[i - 1];
				str[y + 1] = str[y].substr(x);
				str[y] = str[y].substr(0, x);
				x = 0;
				++y;
			} else if (sz(buf)  > 0) {
				str[y] = str[y].substr(0, x) + buf + str[y].substr(x);
				x += sz(buf);
			}
		} else if (com == "-") {
			break;
		}
	}
	rep (i, h) cout << str[i] << endl;
	return 0;
}

Measured memory_bytes: 0