def edit_word(word):
while True:
command = input()
if command == "end":
break
elif command.startswith("replace"):
x, y = command.split()[1], command.split()[2]
word = word.replace(x, y)
elif command.startswith("remove"):
x = command.split()[1]
word = word.replace(x, "")
elif command.startswith("addhead"):
x = command.split()[1]
word = x + word
elif command.startswith("addtail"):
x = command.split()[1]
word = word + x
else:
print("invalid command",command.split()[0])
return 0
return word
while True:
try:
initial_word = input()
except :
break
else:
stop = initial_word
edited_word = edit_word(initial_word)
if(edited_word == stop):
continue
elif(edited_word != 0):
print(edited_word)
else:
break