Buggy python submission (verdict: Time Limit Exceeded):
S = input()
Q = int(input())
query = [list(input().split()) for _ in range(Q)]
forward = 1
for i in range(Q):
    if query[i][0] == '1':
        forward *= -1
    else:
        if query[i][1] == '1':
            if forward == 1:
                S = query[i][2] + S
            else:
                S = S + query[i][2] 
        else:
            if forward == 1:
                S = S + query[i][2] 
            else:
                S = query[i][2] + S

if forward == -1:
    ans = ''
    for i in range(len(S)):
        ans += S[-i-1]
else:
    ans = S
print(ans)


Fixed submission:
S = input()
Q = int(input())
query = [list(input().split()) for _ in range(Q)]
forward = 1
f = ''
b = ''
for i in range(Q):
    if query[i][0] == '1':
        forward *= -1
    else:
        if query[i][1] == '1':
            if forward == 1:
                f += query[i][2]
            else:
                b += query[i][2] 
        else:
            if forward == 1:
                b += query[i][2] 
            else:
                f += query[i][2]
S = ''.join(list(reversed(f))) + S + b

if forward == -1:
    ans = ''.join(list(reversed(S)))
else:
    ans = S
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
S = input()
Q = int(input())
query = []
for _ in range(Q):
    q = list(input().split())
    if len(q) != 1:
        q = [int(x) for x in q[:2]] + [q[2]]
    else:
        q = list(map(int, q)) + [0, '']
    query.append(q)

forward = 1
for t, f, c in query:
    if t == 1:
        forward *= -1
    else:
        if f == 1:
            if forward == 1:
                S = c + S
            else:
                S = S + c
        else:
            if forward == 1:
                S = S + c
            else:
                S = c + S

if forward == -1:
    ans = ''
    for i in range(len(S)):
        ans += S[-i-1]
else:
    ans = S
print(ans)


Fixed submission:
S = input()
Q = int(input())
query = [list(input().split()) for _ in range(Q)]
forward = 1
f = ''
b = ''
for i in range(Q):
    if query[i][0] == '1':
        forward *= -1
    else:
        if query[i][1] == '1':
            if forward == 1:
                f += query[i][2]
            else:
                b += query[i][2] 
        else:
            if forward == 1:
                b += query[i][2] 
            else:
                f += query[i][2]
S = ''.join(list(reversed(f))) + S + b

if forward == -1:
    ans = ''.join(list(reversed(S)))
else:
    ans = S
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
S = input()
Q = int(input())
query = []
for _ in range(Q):
    q = list(input().split())
    if len(q) != 1:
        q = [int(x) for x in q[:2]] + [q[2]]
    else:
        q = list(map(int, q)) + [0, '']
    query.append(q)

S = list(S)
forward = 1
for t, f, c in query:
    if t == 1:
        forward *= -1
    else:
        if f == 1:
            if forward == 1:
                S = [c] + S
            else:
                S = S + [c]
        else:
            if forward == 1:
                S = S + [c]
            else:
                S = [c] + S
if forward == -1:
    S = list(reversed(S))
print(''.join(S))

Fixed submission:
S = input()
Q = int(input())
query = [list(input().split()) for _ in range(Q)]
forward = 1
f = ''
b = ''
for i in range(Q):
    if query[i][0] == '1':
        forward *= -1
    else:
        if query[i][1] == '1':
            if forward == 1:
                f += query[i][2]
            else:
                b += query[i][2] 
        else:
            if forward == 1:
                b += query[i][2] 
            else:
                f += query[i][2]
S = ''.join(list(reversed(f))) + S + b

if forward == -1:
    ans = ''.join(list(reversed(S)))
else:
    ans = S
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
S = input()
Q = int(input())
query = []
for _ in range(Q):
    q = list(input().split())
    if len(q) != 1:
        q = [int(x) for x in q[:2]] + [q[2]]
    else:
        q = list(map(int, q)) + [0, '']
    query.append(q)

S = list(S)
forward = 1
for t, f, c in query:
    if t == 1:
        forward *= -1
    else:
        if f == 1:
            if forward == 1:
                S.insert(0, c)
            else:
                S.append(c)
        else:
            if forward == 1:
                S.append(c)
            else:
                S.insert(0, c)
if forward == -1:
    S = list(reversed(S))
print(''.join(S))


Fixed submission:
S = input()
Q = int(input())
query = [list(input().split()) for _ in range(Q)]
forward = 1
f = ''
b = ''
for i in range(Q):
    if query[i][0] == '1':
        forward *= -1
    else:
        if query[i][1] == '1':
            if forward == 1:
                f += query[i][2]
            else:
                b += query[i][2] 
        else:
            if forward == 1:
                b += query[i][2] 
            else:
                f += query[i][2]
S = ''.join(list(reversed(f))) + S + b

if forward == -1:
    ans = ''.join(list(reversed(S)))
else:
    ans = S
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
S = input()
Q = int(input())
query = []
for _ in range(Q):
    q = list(input().split())
    if len(q) != 1:
        q = [int(x) for x in q[:2]] + [q[2]]
    else:
        q = list(map(int, q)) + [0, '']
    query.append(q)

S = list(S)
for t, f, c in query:
    if t == 1:
        S = list(reversed(S))
    else:
        if f == 1:
            S.insert(0, c)
        else:
            S.append(c)
print(''.join(S))

Fixed submission:
S = input()
Q = int(input())
query = [list(input().split()) for _ in range(Q)]
forward = 1
f = ''
b = ''
for i in range(Q):
    if query[i][0] == '1':
        forward *= -1
    else:
        if query[i][1] == '1':
            if forward == 1:
                f += query[i][2]
            else:
                b += query[i][2] 
        else:
            if forward == 1:
                b += query[i][2] 
            else:
                f += query[i][2]
S = ''.join(list(reversed(f))) + S + b

if forward == -1:
    ans = ''.join(list(reversed(S)))
else:
    ans = S
print(ans)