Buggy python submission (verdict: Time Limit Exceeded):
s = input()
q = int(input())
Q = [input() for _ in range(q)]

s = list(s)
insert = s.insert
append = s.append
flag = True

for query in Q:
    if len(query) == 1:
        flag = not flag
    else:
        query = query.split()
        if query[1] == '1':
            if flag:
                insert(0, query[2])
            else:
                append(query[2])
        else:
            if flag:
                append(query[2])
            else:
                insert(0, query[2])

if not flag:
    s.reverse()

print(*s, sep='')


Fixed submission:
from collections import deque

s = input()
q = int(input())
Q = [input() for _ in range(q)]

s = deque(list(s))
insert = s.appendleft
append = s.append
flag = True

for query in Q:
    if query[0] == '1':
        flag = not flag
    else:
        if (query[2] == '1' and flag) or (query[2] == '2' and not flag):
            insert(query[4])
        else:
            append(query[4])

if not flag:
    s.reverse()

print(*s, sep='')

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

s = list(s)
flag = True
for query in Q:
    if len(query) == 1:
        flag = not flag
    else:
        if query[1] == '1':
            if flag:
                s.insert(0, query[2])
            else:
                s.append(query[2])
        elif query[1] == '2':
            if flag:
                s.append(query[2])
            else:
                s.insert(0, query[2])

if not flag:
    s.reverse()

print("".join(s))


Fixed submission:
from collections import deque

s = input()
q = int(input())
Q = [input() for _ in range(q)]

s = deque(list(s))
insert = s.appendleft
append = s.append
flag = True

for query in Q:
    if query[0] == '1':
        flag = not flag
    else:
        if (query[2] == '1' and flag) or (query[2] == '2' and not flag):
            insert(query[4])
        else:
            append(query[4])

if not flag:
    s.reverse()

print(*s, sep='')

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

s = list(s)
flag = True
for query in Q:
    if len(query) == 1:
        flag = not flag
    else:
        if query[1] == '1':
            if flag:
                s.insert(0, query[2])
            else:
                s.append(query[2])
        elif query[1] == '2':
            if flag:
                s.append(query[2])
            else:
                s.insert(0, query[2])

if not flag:
    s.reverse()

print("".join(s))


Fixed submission:
from collections import deque

s = input()
q = int(input())
Q = [input() for _ in range(q)]

s = deque(list(s))
insert = s.appendleft
append = s.append
flag = True

for query in Q:
    if query[0] == '1':
        flag = not flag
    else:
        if (query[2] == '1' and flag) or (query[2] == '2' and not flag):
            insert(query[4])
        else:
            append(query[4])

if not flag:
    s.reverse()

print(*s, sep='')

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

s = list(s)
for query in Q:
    if len(query) == 1:
        s.reverse()
    else:
        if query[1] == '1':
            s.insert(0, query[2])
        elif query[1] == '2':
            s.append(query[2])

print("".join(s))

Fixed submission:
from collections import deque

s = input()
q = int(input())
Q = [input() for _ in range(q)]

s = deque(list(s))
insert = s.appendleft
append = s.append
flag = True

for query in Q:
    if query[0] == '1':
        flag = not flag
    else:
        if (query[2] == '1' and flag) or (query[2] == '2' and not flag):
            insert(query[4])
        else:
            append(query[4])

if not flag:
    s.reverse()

print(*s, sep='')