Buggy python submission (verdict: Time Limit Exceeded):
S = input()
Q = int(input())
k = 1
l = ""
li = ""
r = ""
ri = ""
for _ in range(Q):
    query = input()
    if query.startswith('1'):
        k *= -1
    elif query.startswith('2'):
        t, f, c = query.split()
        if f == '1':
            if k > 0:
                l = c + l
                li = li + c
            else:
                r = r + c
                ri = c + ri
        elif f == '2':
            if k > 0:
                r = r + c
                ri = c + ri
            else:
                l = c + l
                li = li + c

if k > 0:
    print(l + S + r)
else:
    print(ri + S[::-1] + li)


Fixed submission:
from collections import deque
S = input()
Q = int(input())
k = 1
d = deque(S)
for _ in range(Q):
    query = input()
    if query.startswith('1'):
        k *= -1
    elif query.startswith('2'):
        t, f, c = query.split()
        if f == '1':
            if k > 0:
                d.appendleft(c)                
            else:
                d.append(c)
        elif f == '2':
            if k > 0:
                d.append(c)
            else:
                d.appendleft(c)
d = list(d)
if k > 0:
    print(''.join(d))
else:
    print(''.join(d[::-1]))

Buggy python submission (verdict: Time Limit Exceeded):
S = input()
Q = int(input())
k = 1
l = ""
r = ""
for _ in range(Q):
    query = input()
    if query.startswith('1'):
        k *= -1
    elif query.startswith('2'):
        t, f, c = query.split()
        if f == '1':
            if k > 0:
                l += c
            else:
                r += c
        elif f == '2':
            if k > 0:
                r += c
            else:
                l += c

if k > 0:
    l = l[::-1]
    print(l + S + r)
else:
    print(r[::-1] + S[::-1] + l)


Fixed submission:
from collections import deque
S = input()
Q = int(input())
k = 1
d = deque(S)
for _ in range(Q):
    query = input()
    if query.startswith('1'):
        k *= -1
    elif query.startswith('2'):
        t, f, c = query.split()
        if f == '1':
            if k > 0:
                d.appendleft(c)                
            else:
                d.append(c)
        elif f == '2':
            if k > 0:
                d.append(c)
            else:
                d.appendleft(c)
d = list(d)
if k > 0:
    print(''.join(d))
else:
    print(''.join(d[::-1]))

Buggy python submission (verdict: Time Limit Exceeded):
S = input()
Q = int(input())
k = 1
for _ in range(Q):
    query = input()
    if query.startswith('1'):
        k *= -1
    elif query.startswith('2'):
        t, f, c = query.split()
        if f == '1':
            if k > 0:
                S = c + S
            else:
                S = S + c
        elif f == '2':
            if k > 0:
                S = S + c
            else:
                S = c + S
if k > 0:
    print(S)
else:
    print(S[::-1])


Fixed submission:
from collections import deque
S = input()
Q = int(input())
k = 1
d = deque(S)
for _ in range(Q):
    query = input()
    if query.startswith('1'):
        k *= -1
    elif query.startswith('2'):
        t, f, c = query.split()
        if f == '1':
            if k > 0:
                d.appendleft(c)                
            else:
                d.append(c)
        elif f == '2':
            if k > 0:
                d.append(c)
            else:
                d.appendleft(c)
d = list(d)
if k > 0:
    print(''.join(d))
else:
    print(''.join(d[::-1]))