Buggy python submission (verdict: Runtime Error):
from collections import deque

S = input()
Q = int(input())
Query = [input().split() for _ in range(Q)]

S1 = deque('')
S2 = deque('')
rev = False

for TFC in Query:
    if TFC[0] == '1':
        rev = not rev
    else:
        if TFC[1] == '1':
            if rev:
                S2.append(TFC[2])
            else:
                S1.appendleft(TFC[2])
        else:
            if rev:
                S1.appendleft(TFC[2])
            else:
                S2.append(TFC[2])
if rev:
    S2 = ''.join(S2[::-1])
    S1 = ''.join(S1[::-1])
    print(S2 + S + S1)
else:
    S1 = ''.join(S1)
    S2 = ''.join(S2)
    print(S1 + S + S2)

Fixed submission:
S = input()
Q = int(input())
Query = [input().split() for _ in range(Q)]

rev = False
S1 = ''
S2 = ''

for TFC in Query:
    if TFC[0] == '1':
        rev = not rev
    else:
        if TFC[1] == '1':
            if rev:
                S2 += TFC[2]
            else:
                S1 = TFC[2] + S1
        else:
            if rev:
                S1 = TFC[2] + S1
            else:
                S2 += TFC[2]

if rev:
    print(S2[::-1] + S[::-1] + S1[::-1])
else:
    print(S1 + S + S2)

Buggy python submission (verdict: Runtime Error):
from collections import deque

S = input()
Q = int(input())
Query = [input().split() for _ in range(Q)]

S1 = deque('')
S2 = deque('')
rev = False

for TFC in Query:
    if TFC[0] == '1':
        rev = not rev
    else:
        if TFC[1] == '1':
            if rev:
                S2.append(TFC[2])
            else:
                S1.appendleft(TFC[2])
        else:
            if rev:
                S1.appendleft(TFC[2])
            else:
                S2.append(TFC[2])
if rev:
    S2 = ''.join(S2[::-1])
    S1 = ''.join(S1[::-1])
    print(S2 + S + S1)
else:
    S1 = ''.join(S1)
    S2 = ''.join(S2)
    print(S1 + S + S2)

Fixed submission:
S = input()
Q = int(input())
Query = [input().split() for _ in range(Q)]

rev = False
S1 = ''
S2 = ''

for TFC in Query:
    if TFC[0] == '1':
        rev = not rev
    else:
        if TFC[1] == '1':
            if rev:
                S2 += TFC[2]
            else:
                S1 = TFC[2] + S1
        else:
            if rev:
                S1 = TFC[2] + S1
            else:
                S2 += TFC[2]

if rev:
    print(S2[::-1] + S[::-1] + S1[::-1])
else:
    print(S1 + S + S2)

Buggy python submission (verdict: Runtime Error):
from collections import deque

S = input()
Q = int(input())
Query = [input().split() for _ in range(Q)]

S = deque(S)
S1 = deque('')
S2 = deque('')
rev = False

for TFC in Query:
    if TFC[0] == '1':
        rev = not rev
    else:
        if TFC[1] == '1':
            if rev:
                S2.append(TFC[2])
            else:
                S1.appendleft(TFC[2])
        else:
            if rev:
                S1.appendleft(TFC[2])
            else:
                S2.append(TFC[2])
if rev:
    S2.reverse()
    S.reverse()
    S1.reverse()
    print(''.join(S2 + S + S1))
else:
    print(''.join(S1 + S + S2))

Fixed submission:
S = input()
Q = int(input())
Query = [input().split() for _ in range(Q)]

rev = False
S1 = ''
S2 = ''

for TFC in Query:
    if TFC[0] == '1':
        rev = not rev
    else:
        if TFC[1] == '1':
            if rev:
                S2 += TFC[2]
            else:
                S1 = TFC[2] + S1
        else:
            if rev:
                S1 = TFC[2] + S1
            else:
                S2 += TFC[2]

if rev:
    print(S2[::-1] + S[::-1] + S1[::-1])
else:
    print(S1 + S + S2)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline

S = input().strip()
Q = int(input())
QUERY = [input().split() for _ in range(Q)]

rev = False

for query in QUERY:
    # 反転の操作
    if query[0] == '1':
        if rev:
            rev = False
        else:
            rev = True
    else:
        # 先頭に追加する場合
        if query[1] == '1':
            # 反転しているとき
            if rev:
                # 末尾に追加
                S = S + query[2]
            # 反転していないとき
            else:
                # 先頭に追加
                S = query[2] + S
        # 末尾に追加する場合
        else:
            # 反転しているとき
            if rev:
                # 先頭に追加
                S = query[2] + S
            # 反転していないとき
            else:
                # 末尾に追加
                S = S + query[2]
if rev:
    print(S[::-1])
else:
    print(S)

Fixed submission:
S = input()
Q = int(input())
Query = [input().split() for _ in range(Q)]

rev = False
S1 = ''
S2 = ''

for TFC in Query:
    if TFC[0] == '1':
        rev = not rev
    else:
        if TFC[1] == '1':
            if rev:
                S2 += TFC[2]
            else:
                S1 = TFC[2] + S1
        else:
            if rev:
                S1 = TFC[2] + S1
            else:
                S2 += TFC[2]

if rev:
    print(S2[::-1] + S[::-1] + S1[::-1])
else:
    print(S1 + S + S2)

Buggy python submission (verdict: Time Limit Exceeded):
S = input()
Q = int(input())
QUERY = [input().split() for _ in range(Q)]

rev = False

for query in QUERY:
    # 反転の操作
    if query[0] == '1':
        if rev:
            rev = False
        else:
            rev = True
    else:
        # 先頭に追加する場合
        if query[1] == '1':
            # 反転しているとき
            if rev:
                # 末尾に追加
                S = S + query[2]
            # 反転していないとき
            else:
                # 先頭に追加
                S = query[2] + S
        # 末尾に追加する場合
        else:
            # 反転しているとき
            if rev:
                # 先頭に追加
                S = query[2] + S
            # 反転していないとき
            else:
                # 末尾に追加
                S = S + query[2]
if rev:
    print(S[::-1])
else:
    print(S)

Fixed submission:
S = input()
Q = int(input())
Query = [input().split() for _ in range(Q)]

rev = False
S1 = ''
S2 = ''

for TFC in Query:
    if TFC[0] == '1':
        rev = not rev
    else:
        if TFC[1] == '1':
            if rev:
                S2 += TFC[2]
            else:
                S1 = TFC[2] + S1
        else:
            if rev:
                S1 = TFC[2] + S1
            else:
                S2 += TFC[2]

if rev:
    print(S2[::-1] + S[::-1] + S1[::-1])
else:
    print(S1 + S + S2)

Buggy python submission (verdict: Time Limit Exceeded):
S = input()
Q = int(input())
QUERY = [input().split() for _ in range(Q)]

rev = False

for query in QUERY:
    # 反転の操作
    if query[0] == '1':
        if rev:
            rev = False
        else:
            rev = True
    else:
        # 先頭に追加する場合
        if query[1] == '1':
            # 反転しているとき
            if rev:
                # 末尾に追加
                S = S + query[2]
            # 反転していないとき
            else:
                # 先頭に追加
                S = query[2] + S
        # 末尾に追加する場合
        else:
            # 反転しているとき
            if rev:
                # 先頭に追加
                S = query[2] + S
            # 反転していないとき
            else:
                # 末尾に追加
                S = S + query[2]
if rev:
    print(S[::-1])
else:
    print(S)

Fixed submission:
S = input()
Q = int(input())
Query = [input().split() for _ in range(Q)]

rev = False
S1 = ''
S2 = ''

for TFC in Query:
    if TFC[0] == '1':
        rev = not rev
    else:
        if TFC[1] == '1':
            if rev:
                S2 += TFC[2]
            else:
                S1 = TFC[2] + S1
        else:
            if rev:
                S1 = TFC[2] + S1
            else:
                S2 += TFC[2]

if rev:
    print(S2[::-1] + S[::-1] + S1[::-1])
else:
    print(S1 + S + S2)

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

rev = False

for i in range(Q):
    # クエリを受け取る
    query = input().split()
    # 反転の操作
    if query[0] == '1':
        if rev:
            rev = False
        else:
            rev = True
    else:
        # 追加する操作
        F = query[1]
        # 先頭に追加する場合
        if F == '1':
            # 反転しているとき
            if rev:
                # 末尾に追加
                S = S + query[2]
            # 反転していないとき
            else:
                # 先頭に追加
                S = query[2] + S
        # 末尾に追加する場合
        else:
            # 反転しているとき
            if rev:
                # 先頭に追加
                S = query[2] + S
            # 反転していないとき
            else:
                # 末尾に追加
                S = S + query[2]
if rev:
    print(S[::-1])
else:
    print(S)

Fixed submission:
S = input()
Q = int(input())
Query = [input().split() for _ in range(Q)]

rev = False
S1 = ''
S2 = ''

for TFC in Query:
    if TFC[0] == '1':
        rev = not rev
    else:
        if TFC[1] == '1':
            if rev:
                S2 += TFC[2]
            else:
                S1 = TFC[2] + S1
        else:
            if rev:
                S1 = TFC[2] + S1
            else:
                S2 += TFC[2]

if rev:
    print(S2[::-1] + S[::-1] + S1[::-1])
else:
    print(S1 + S + S2)

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

rev = False

for i in range(Q):
    # クエリを受け取る
    query = input().split()
    # 反転の操作
    if query[0] == '1':
        if rev == True:
            rev = False
        else:
            rev = True
    else:
        # 追加する操作
        F = int(query[1])
        C = query[2]
        # 先頭に追加する場合
        if F == 1:
            # 反転しているとき
            if rev == True:
                # 末尾に追加
                S = S + C
            # 反転していないとき
            else:
                # 先頭に追加
                S = C + S
        # 末尾に追加する場合
        else:
            # 反転しているとき
            if rev == True:
                # 先頭に追加
                S = C + S
            # 反転していないとき
            else:
                # 末尾に追加
                S = S + C
if rev == True:
    print(S[::-1])
else:
    print(S)

Fixed submission:
S = input()
Q = int(input())
Query = [input().split() for _ in range(Q)]

rev = False
S1 = ''
S2 = ''

for TFC in Query:
    if TFC[0] == '1':
        rev = not rev
    else:
        if TFC[1] == '1':
            if rev:
                S2 += TFC[2]
            else:
                S1 = TFC[2] + S1
        else:
            if rev:
                S1 = TFC[2] + S1
            else:
                S2 += TFC[2]

if rev:
    print(S2[::-1] + S[::-1] + S1[::-1])
else:
    print(S1 + S + S2)