Buggy python submission (verdict: Time Limit Exceeded):
S = list(input())
Q = int(input())
flag = 0
for i in range(Q):
    Query = list(map(str, input().split()))
    if Query[0] == "1":
        if flag == 0:
            flag = 1
        else:
            flag = 0
    elif Query[1] == "1" and flag == 0:
        S.insert(0, Query[2])

    elif Query[1] == "1" and flag == 1:
        S.append(Query[2])

    elif Query[1] == "2" and flag == 0:
        S.append(Query[2])

    elif Query[1] == "2" and flag == 1:
        S.insert(0, Query[2])

if flag == 0:
    print("".join(S))

else:
    S.reverse()
    print("".join(S))


Fixed submission:
import collections
S = input()
Q = int(input())
q = collections.deque()
q.append(S)
inversion = True
for i in range(Q):
    Query = list(map(str, input().split()))
    if Query[0] == "1":
        inversion = not(inversion)
    elif Query[1] == "1":
        if inversion:
            q.appendleft(Query[2])
        else:
            q.append(Query[2])
    else:
        if inversion:
            q.append(Query[2])
        else:
            q.appendleft(Query[2])

if inversion:
    print("".join(q))
else:
    print("".join(q)[::-1])

Buggy python submission (verdict: Time Limit Exceeded):
S = list(input())
Q = int(input())
for i in range(Q):
    Query = list(map(str, input().split()))
    if Query[0] == "1":
        S.reverse()
    elif Query[1] == "1":
        S.insert(0, Query[2])
    else:
        S.append(Query[2])
print("".join(S))


Fixed submission:
import collections
S = input()
Q = int(input())
q = collections.deque()
q.append(S)
inversion = True
for i in range(Q):
    Query = list(map(str, input().split()))
    if Query[0] == "1":
        inversion = not(inversion)
    elif Query[1] == "1":
        if inversion:
            q.appendleft(Query[2])
        else:
            q.append(Query[2])
    else:
        if inversion:
            q.append(Query[2])
        else:
            q.appendleft(Query[2])

if inversion:
    print("".join(q))
else:
    print("".join(q)[::-1])

Buggy python submission (verdict: Time Limit Exceeded):
S = input()
Q = int(input())
for i in range(Q):
    Query = list(map(str, input().split()))
    if Query[0] == "1":
        S = S[::-1]
    elif Query[1] == "1":
        S = Query[2] + S
    else:
        S = S + Query[2]
print(S)


Fixed submission:
import collections
S = input()
Q = int(input())
q = collections.deque()
q.append(S)
inversion = True
for i in range(Q):
    Query = list(map(str, input().split()))
    if Query[0] == "1":
        inversion = not(inversion)
    elif Query[1] == "1":
        if inversion:
            q.appendleft(Query[2])
        else:
            q.append(Query[2])
    else:
        if inversion:
            q.append(Query[2])
        else:
            q.appendleft(Query[2])

if inversion:
    print("".join(q))
else:
    print("".join(q)[::-1])

Buggy python submission (verdict: Time Limit Exceeded):
S = input()
Q = int(input())
Query = [list(map(str, input().split())) for i in range(Q)]
for i in range(Q):
    if Query[i][0] == "1":
        S = S[::-1]
    elif Query[i][1] == "1":
        S = Query[i][2] + S

    else:
        S = S + Query[i][2]
print(S)


Fixed submission:
import collections
S = input()
Q = int(input())
q = collections.deque()
q.append(S)
inversion = True
for i in range(Q):
    Query = list(map(str, input().split()))
    if Query[0] == "1":
        inversion = not(inversion)
    elif Query[1] == "1":
        if inversion:
            q.appendleft(Query[2])
        else:
            q.append(Query[2])
    else:
        if inversion:
            q.append(Query[2])
        else:
            q.appendleft(Query[2])

if inversion:
    print("".join(q))
else:
    print("".join(q)[::-1])