Buggy python submission (verdict: Time Limit Exceeded):
S = input()
Q = int(input())

n = S
# r = S
# for i in range(Q):
#     op = input().split()
#     if len(op) == 1:
#         n, r = r, n
#     elif op[1] == "1":
#         n = op[2] + n
#         r = r + op[2]
#     else:
#         n = n + op[2]
#         r = op[2] + r
# print(n)

mode = 0
operations = []
for i in range(Q):
    operations.append(input().split())
for op in operations:
    if op[0] == "1":
        mode += 1
        mode %= 2
    elif int(op[1]) + mode == 2:
        n = "".join([n, op[2]])
    else:
        n = "".join([op[2], n])
if mode == 0:
    print(n)
else:
    print(n[::-1])

Fixed submission:
from collections import deque
S = input()
Q = int(input())

n = S
mode = 0
operations = []
deq = deque(n)
for i in range(Q):
    operations.append(input().split())
for op in operations:
    if op[0] == "1":
        mode += 1
        mode %= 2
    elif int(op[1]) + mode == 2:
        deq.append(op[2])
    else:
        deq.appendleft(op[2])
if mode == 0:
    print("".join(deq))
else:
    print("".join(reversed(deq)))

Buggy python submission (verdict: Time Limit Exceeded):
S = input()
Q = int(input())

n = S
# r = S
# for i in range(Q):
#     op = input().split()
#     if len(op) == 1:
#         n, r = r, n
#     elif op[1] == "1":
#         n = op[2] + n
#         r = r + op[2]
#     else:
#         n = n + op[2]
#         r = op[2] + r
# print(n)

mode = 0
operations = []
for i in range(Q):
    operations.append(input().split())
for op in operations:
    if len(op) == 1:
        mode += 1
        mode %= 2
    elif int(op[1]) + mode == 2:
        n = n + op[2]
    else:
        n = op[2] + n
if mode == 0:
    print(n)
else:
    print(n[::-1])

Fixed submission:
from collections import deque
S = input()
Q = int(input())

n = S
mode = 0
operations = []
deq = deque(n)
for i in range(Q):
    operations.append(input().split())
for op in operations:
    if op[0] == "1":
        mode += 1
        mode %= 2
    elif int(op[1]) + mode == 2:
        deq.append(op[2])
    else:
        deq.appendleft(op[2])
if mode == 0:
    print("".join(deq))
else:
    print("".join(reversed(deq)))

Buggy python submission (verdict: Time Limit Exceeded):
S = input()
Q = int(input())

n = S
# r = S
# for i in range(Q):
#     op = input().split()
#     if len(op) == 1:
#         n, r = r, n
#     elif op[1] == "1":
#         n = op[2] + n
#         r = r + op[2]
#     else:
#         n = n + op[2]
#         r = op[2] + r
# print(n)

mode = 0
for i in range(Q):
    op = input().split()
    if len(op) == 1:
        mode += 1
        mode %= 2
    elif int(op[1]) + mode == 2:
        n = n + op[2]
    else:
        n = op[2] + n
if mode == 0:
    print(n)
else:
    print(n[::-1])

Fixed submission:
from collections import deque
S = input()
Q = int(input())

n = S
mode = 0
operations = []
deq = deque(n)
for i in range(Q):
    operations.append(input().split())
for op in operations:
    if op[0] == "1":
        mode += 1
        mode %= 2
    elif int(op[1]) + mode == 2:
        deq.append(op[2])
    else:
        deq.appendleft(op[2])
if mode == 0:
    print("".join(deq))
else:
    print("".join(reversed(deq)))