https://www.acmicpc.net/problem/1406
# 문제1: 시간초과
import sys
from collections import deque
input = sys.stdin.readline
arr = deque([])
li = str(input().rstrip())
num = int(input())
cursor = len(li)
for i in li:
arr.append(i)
for _ in range(num):
temp = list(input().split())
if len(temp) == 2:
cmd = temp[0]
word = temp[1]
else:
cmd = temp[0]
if cmd == 'L':
if cursor > 0:
cursor -= 1
elif cmd == 'D':
if cursor < len(arr):
cursor += 1
elif cmd == 'B':
if cursor > 0:
arr.remove(arr[cursor-1])
elif cmd == 'P':
arr.insert(cursor, word)
cursor += 1
print(''.join(arr))
# 문제2
import sys
input = sys.stdin.readline
str1 = list(input().rstrip())
str2 = []
n = int(input())
for _ in range(n):
cmd = list(input().split())
if cmd[0] == 'L':
if str1:
str2.append(str1.pop())
elif cmd[0] == 'D':
if str2:
str1.append(str2.pop())
elif cmd[0] == 'B':
if str1:
str1.pop()
else:
str1.append(cmd[1])
str1.extend(reversed(str2))
print(''.join(str1))