Buggy python submission (verdict: Time Limit Exceeded):
s=input()
q=int(input())
for _ in [0]*q:
	k=input()
	if k[0] == "1":
		s = s[::-1]
	else:
		if k[2] == "1":
			s = "".join([k[4:],s])
		else:
			s = "".join([s,k[4:]])
print(s)

Fixed submission:
from collections import deque
s=input()
Q=int(input())
hanten = 0
mae = deque()
ushiro = deque()
for i in range(Q):
	q=input()
	if q=="1":
		hanten ^= 1
	else:
		if q[2] == "1":
			if hanten == 0:
				mae.appendleft(q[4])
			else:
				ushiro.append(q[4])
		else:
			if hanten == 0:
				ushiro.append(q[4])
			else:
				mae.appendleft(q[4])
if hanten == 0:
	print("".join(mae) + s + "".join(ushiro))
else:
	print(("".join(mae) + s + "".join(ushiro))[::-1])

Buggy python submission (verdict: Time Limit Exceeded):
s=input()
q=int(input())
for _ in [0]*q:
	k=input()
	if k[0] == "1":
		s = s[::-1]
	else:
		if k[2] == "1":
			s = "".join([k[4:],s])
		else:
			s = "".join([s,k[4:]])
print(s)

Fixed submission:
from collections import deque
s=input()
Q=int(input())
hanten = 0
mae = deque()
ushiro = deque()
for i in range(Q):
	q=input()
	if q=="1":
		hanten ^= 1
	else:
		if q[2] == "1":
			if hanten == 0:
				mae.appendleft(q[4])
			else:
				ushiro.append(q[4])
		else:
			if hanten == 0:
				ushiro.append(q[4])
			else:
				mae.appendleft(q[4])
if hanten == 0:
	print("".join(mae) + s + "".join(ushiro))
else:
	print(("".join(mae) + s + "".join(ushiro))[::-1])

Buggy python submission (verdict: Time Limit Exceeded):
s=input()
q=int(input())
for _ in range(q):
	k=input()
	if k[0] == "1":
		s = s[::-1]
	else:
		if k[2] == "1":
			s = "".join([k[4:],s])
		else:
			s = "".join([s,k[4:]])
print(s)

Fixed submission:
from collections import deque
s=input()
Q=int(input())
hanten = 0
mae = deque()
ushiro = deque()
for i in range(Q):
	q=input()
	if q=="1":
		hanten ^= 1
	else:
		if q[2] == "1":
			if hanten == 0:
				mae.appendleft(q[4])
			else:
				ushiro.append(q[4])
		else:
			if hanten == 0:
				ushiro.append(q[4])
			else:
				mae.appendleft(q[4])
if hanten == 0:
	print("".join(mae) + s + "".join(ushiro))
else:
	print(("".join(mae) + s + "".join(ushiro))[::-1])

Buggy python submission (verdict: Time Limit Exceeded):
s=input()
q=int(input())
for _ in range(q):
	k=input()
	if k[0] == "1":
		s = s[::-1]
	else:
		if k[2] == "1":
			s = k[4:]+s
		else:
			s = s+k[4:]
print(s)

Fixed submission:
from collections import deque
s=input()
Q=int(input())
hanten = 0
mae = deque()
ushiro = deque()
for i in range(Q):
	q=input()
	if q=="1":
		hanten ^= 1
	else:
		if q[2] == "1":
			if hanten == 0:
				mae.appendleft(q[4])
			else:
				ushiro.append(q[4])
		else:
			if hanten == 0:
				ushiro.append(q[4])
			else:
				mae.appendleft(q[4])
if hanten == 0:
	print("".join(mae) + s + "".join(ushiro))
else:
	print(("".join(mae) + s + "".join(ushiro))[::-1])

Buggy python submission (verdict: Time Limit Exceeded):
s=input()
q=int(input())
for _ in range(q):
	k=input()
	if k == "1":
		s = s[::-1]
	else:
		if k[2] == "1":
			s = k[4:]+s
		if k[2] == "2":
			s = s+k[4:]
print(s)

Fixed submission:
from collections import deque
s=input()
Q=int(input())
hanten = 0
mae = deque()
ushiro = deque()
for i in range(Q):
	q=input()
	if q=="1":
		hanten ^= 1
	else:
		if q[2] == "1":
			if hanten == 0:
				mae.appendleft(q[4])
			else:
				ushiro.append(q[4])
		else:
			if hanten == 0:
				ushiro.append(q[4])
			else:
				mae.appendleft(q[4])
if hanten == 0:
	print("".join(mae) + s + "".join(ushiro))
else:
	print(("".join(mae) + s + "".join(ushiro))[::-1])