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

direction = 1

for i in range(Q):
    q = list(map(str, input().split())) 
    if q[0] == "1":
        direction *= (-1)
        #S = S[::-1]
    elif q[0] == "2":
        if direction > 0:
            if q[1] == "1":
                S = q[2] + S
            elif q[1] == "2":
                S = S + q[2]
        elif direction < 0:
            if q[1] == "1":
                S = S + q[2]
            elif q[1] == "2":
                S = q[2] + S 

if direction < 0:
    S = S[::-1]           

print(S)

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

head = ''
back = ''
direction = 1

for i in range(Q):
    q = list(map(str, input().split())) 
    if q[0] == "1":
        direction *= (-1)
        #S = S[::-1]
    else:
        if q[1] == "1":
            if direction == 1:
                head = head + q[2]
            else:
                back = back + q[2]
        else:
            if direction == 1:
                back = back + q[2]
            else:
                head = head + q[2]

if direction == 1:
    print(head[::-1]+S+back)
else:
    print(back[::-1]+S[::-1]+head)

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

direction = 1

for i in range(Q):
    q = input()
    if q[0] == "1":
        direction *= (-1)
        #S = S[::-1]
    elif q[0] == "2":
        if direction > 0:
            if q[2] == "1":
                S = q[4] + S
            elif q[2] == "2":
                S = S + q[4]
        elif direction < 0:
            if q[2] == "1":
                S = S + q[4]
            elif q[2] == "2":
                S = q[4] + S 

if direction < 0:
    S = S[::-1]           

print(S)

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

head = ''
back = ''
direction = 1

for i in range(Q):
    q = list(map(str, input().split())) 
    if q[0] == "1":
        direction *= (-1)
        #S = S[::-1]
    else:
        if q[1] == "1":
            if direction == 1:
                head = head + q[2]
            else:
                back = back + q[2]
        else:
            if direction == 1:
                back = back + q[2]
            else:
                head = head + q[2]

if direction == 1:
    print(head[::-1]+S+back)
else:
    print(back[::-1]+S[::-1]+head)

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

for i in range(Q):
    q = input()
    if q[0] == "1":
        S = S[::-1]
    elif q[0] == "2":
        if q[2] == "1":
            S = q[4] + S
        elif q[2] == "2":
            S = S + q[4]

print(S)

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

head = ''
back = ''
direction = 1

for i in range(Q):
    q = list(map(str, input().split())) 
    if q[0] == "1":
        direction *= (-1)
        #S = S[::-1]
    else:
        if q[1] == "1":
            if direction == 1:
                head = head + q[2]
            else:
                back = back + q[2]
        else:
            if direction == 1:
                back = back + q[2]
            else:
                head = head + q[2]

if direction == 1:
    print(head[::-1]+S+back)
else:
    print(back[::-1]+S[::-1]+head)