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

for i in range(Q):
    query = input()
    if query[0] == '1':
        reverse_index *= -1
    elif query[0] == '2':
        if query[2] == '1':
            if reverse_index == -1:
                S = query[4] + S
            else:
                S = S + query[4]
        else:
            if reverse_index == -1:
                S = S + query[4]
            else:
                S = query[4] + S

if reverse_index == 1:
    print(S[::-1])
else:
    print(S)


Fixed submission:
from collections import deque

S = deque(input())
Q = int(input())
reverse_index = -1

for i in range(Q):
    query = input()
    if query[0] == '1':
        reverse_index *= -1
    elif query[0] == '2':
        if query[2] == '1':
            if reverse_index == -1:
                S.appendleft(query[4])
            else:
                S.append(query[4])
        else:
            if reverse_index == -1:
                S.append(query[4])
            else:
                S.appendleft(query[4])

if reverse_index == 1:
    S.reverse()
    print(*S, sep='')
else:
    print(*S, sep='')

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

for i in range(Q):
    query = input()
    query = query.replace(' ', '')
    if query[0] == '1':
        reverse_index *= -1
    elif query[0] == '2':
        if query[1] == '1':
            if reverse_index == -1:
                S = query[2] + S
            else:
                S = S + query[2]
        else:
            if reverse_index == -1:
                S = S + query[2]
            else:
                S = query[2] + S

if reverse_index == 1:
    print(S[::-1])
else:
    print(S)


Fixed submission:
from collections import deque

S = deque(input())
Q = int(input())
reverse_index = -1

for i in range(Q):
    query = input()
    if query[0] == '1':
        reverse_index *= -1
    elif query[0] == '2':
        if query[2] == '1':
            if reverse_index == -1:
                S.appendleft(query[4])
            else:
                S.append(query[4])
        else:
            if reverse_index == -1:
                S.append(query[4])
            else:
                S.appendleft(query[4])

if reverse_index == 1:
    S.reverse()
    print(*S, sep='')
else:
    print(*S, sep='')

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

for i in range(Q):
    query = input()
    query = query.replace(' ', '')
    if query[0] == '1':
        reverse_index *= -1
    elif query[0] == '2':
        if query[1] == '1':
            if reverse_index == -1:
                S = query[2] + S
            else:
                S = S + query[2]
        else:
            if reverse_index == -1:
                S = S + query[2]
            else:
                S = query[2] + S

if reverse_index == 1:
    S = S[::-1]

print(S)


Fixed submission:
from collections import deque

S = deque(input())
Q = int(input())
reverse_index = -1

for i in range(Q):
    query = input()
    if query[0] == '1':
        reverse_index *= -1
    elif query[0] == '2':
        if query[2] == '1':
            if reverse_index == -1:
                S.appendleft(query[4])
            else:
                S.append(query[4])
        else:
            if reverse_index == -1:
                S.append(query[4])
            else:
                S.appendleft(query[4])

if reverse_index == 1:
    S.reverse()
    print(*S, sep='')
else:
    print(*S, sep='')

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

for i in range(Q):
    query = input()
    query = query.replace(' ', '')
    if query[0] == '1':
        S = S[::-1]
    elif query[0] == '2':
        if query[1] == '1':
            S = query[2] + S
        else:
            S = S + query[2]

print(S)


Fixed submission:
from collections import deque

S = deque(input())
Q = int(input())
reverse_index = -1

for i in range(Q):
    query = input()
    if query[0] == '1':
        reverse_index *= -1
    elif query[0] == '2':
        if query[2] == '1':
            if reverse_index == -1:
                S.appendleft(query[4])
            else:
                S.append(query[4])
        else:
            if reverse_index == -1:
                S.append(query[4])
            else:
                S.appendleft(query[4])

if reverse_index == 1:
    S.reverse()
    print(*S, sep='')
else:
    print(*S, sep='')