Buggy python submission (verdict: Time Limit Exceeded):
#! /usr/bin/env python3

from fractions import gcd
from collections import Counter, deque, defaultdict
from heapq import heappush, heappop, heappushpop, heapify, heapreplace, merge
from bisect import bisect_left, bisect_right, bisect, insort_left, insort_right, insort
from itertools import accumulate, product, permutations, combinations, combinations_with_replacement

S = input()
Q = int(input())

dire = 0
front = []
back = []

for i in range(Q):
    quary = [_ for _ in input().split()]
    if len(quary) == 1:
        T = quary[0]
        dire += 1
    else:
        T, F, C = quary
        if F == "1":
            if dire % 2 == 0:
                front += [C]
            else:
                back += [C]
        else:
            if dire % 2 == 1:
                front += [C]
            else:
                back += [C]

for f in front:
    S = f + S
for b in back:
    S = S + b

if dire % 2 == 1:
    print(S[::-1])
else:
    print(S)

Fixed submission:
S = input()
Q = int(input())

dire = 0
front = ""
back = ""

for i in range(Q):
    quary = [_ for _ in input().split()]
    if len(quary) == 1:
        T = quary[0]
        dire += 1
    else:
        T, F, C = quary
        if F == "1":
            if dire % 2 == 0:
                front += C
            else:
                back += C
        else:
            if dire % 2 == 1:
                front += C
            else:
                back += C

S = front[::-1] + S + back

if dire % 2 == 1:
    print(S[::-1])
else:
    print(S)

Buggy python submission (verdict: Time Limit Exceeded):
#! /usr/bin/env python3

from fractions import gcd
from collections import Counter, deque, defaultdict
from heapq import heappush, heappop, heappushpop, heapify, heapreplace, merge
from bisect import bisect_left, bisect_right, bisect, insort_left, insort_right, insort
from itertools import accumulate, product, permutations, combinations, combinations_with_replacement

S = input()
Q = int(input())

dire = 0
front = []
back = []

for i in range(Q):
    quary = [_ for _ in input().split()]
    if len(quary) == 1:
        T = quary[0]
        dire += 1
    else:
        T, F, C = quary
        if F == "1":
            if dire % 2 == 0:
                front += [C]
            else:
                back += [C]
        else:
            if dire % 2 == 1:
                front += [C]
            else:
                back += [C]

for f in front:
    S = f + S
for b in back:
    S = S + b

if dire % 2 == 1:
    print(S[::-1])
else:
    print(S)

Fixed submission:
S = input()
Q = int(input())

dire = 0
front = ""
back = ""

for i in range(Q):
    quary = [_ for _ in input().split()]
    if len(quary) == 1:
        T = quary[0]
        dire += 1
    else:
        T, F, C = quary
        if F == "1":
            if dire % 2 == 0:
                front += C
            else:
                back += C
        else:
            if dire % 2 == 1:
                front += C
            else:
                back += C

S = front[::-1] + S + back

if dire % 2 == 1:
    print(S[::-1])
else:
    print(S)

Buggy python submission (verdict: Time Limit Exceeded):
s = input()
n = int(input())

ct = 0

for i in range(n):
    p = input().split()

    if p[0] == '1':
        ct += 1
    else:
        if ct % 2 != 0:
            s = s[::-1]
        ct = 0
        
        if p[1] == "1":
            s = p[2] + s
        else:
            s = s + p[2] 
    
if ct % 2 != 0:
    s = s[::-1]

print(s)

Fixed submission:
S = input()
Q = int(input())

dire = 0
front = ""
back = ""

for i in range(Q):
    quary = [_ for _ in input().split()]
    if len(quary) == 1:
        T = quary[0]
        dire += 1
    else:
        T, F, C = quary
        if F == "1":
            if dire % 2 == 0:
                front += C
            else:
                back += C
        else:
            if dire % 2 == 1:
                front += C
            else:
                back += C

S = front[::-1] + S + back

if dire % 2 == 1:
    print(S[::-1])
else:
    print(S)