Buggy python submission (verdict: Time Limit Exceeded):
s = input()
q = int(input())
hanten = False
query = []
for i in range(q):
    query.append(list(input().split()))

for i in range(q):
    if hanten == False:
        if len(query[i]) == 1:
            hanten = True
        else:
            if int(query[i][1]) == 1:
                #先頭にquery[i][2]を追加
                s = query[i][2] + s
            else:
                #末尾にquery[i][2]を追加
                s = s + query[i][2]
        #print(s)
    else:
        if len(query[i]) == 1:
            hanten = False
        else:
            if int(query[i][1]) == 1:
                #末尾にquery[i][2]を追加
                s = s + query[i][2]
            else:
                #先頭にquery[i][2]を追加
                s = query[i][2] + s
        #print(s)
                
if hanten == False:
    print(s)
else:
    print(s[::-1])

Fixed submission:
s = input()
q = int(input())
hanten = False
sentou = ""
matubi = ""
query = []
for i in range(q):
    query.append(list(input().split()))

for i in range(q):
    if hanten == False:
        if len(query[i]) == 1:
            hanten = True
        else:
            if int(query[i][1]) == 1:
                #先頭にquery[i][2]を追加
                sentou = query[i][2] + sentou
            else:
                #末尾にquery[i][2]を追加
                matubi = matubi + query[i][2]
        #print(s)
    else:
        if len(query[i]) == 1:
            hanten = False
        else:
            if int(query[i][1]) == 1:
                #末尾にquery[i][2]を追加
                matubi = matubi + query[i][2]
            else:
                #先頭にquery[i][2]を追加
                sentou = query[i][2] + sentou
        #print(s)

s = sentou + s + matubi
if hanten == False:
    print(s)
else:
    print(s[::-1])

Buggy python submission (verdict: Time Limit Exceeded):
s = input()
q = int(input())
hanten = False
query = []
for i in range(q):
    query.append(list(input().split()))

for i in range(q):
    if hanten == False:
        if len(query[i]) == 1:
            hanten = True
        else:
            if int(query[i][1]) == 1:
                #先頭にquery[i][2]を追加
                s = query[i][2] + s
            else:
                #末尾にquery[i][2]を追加
                s = s + query[i][2]
        #print(s)
    else:
        if len(query[i]) == 1:
            hanten = False
        else:
            if int(query[i][1]) == 1:
                #末尾にquery[i][2]を追加
                s = s + query[i][2]
            else:
                #先頭にquery[i][2]を追加
                s = query[i][2] + s
        #print(s)
                
if hanten == False:
    print(s)
else:
    print(s[::-1])

Fixed submission:
s = input()
q = int(input())
hanten = False
sentou = ""
matubi = ""
query = []
for i in range(q):
    query.append(list(input().split()))

for i in range(q):
    if hanten == False:
        if len(query[i]) == 1:
            hanten = True
        else:
            if int(query[i][1]) == 1:
                #先頭にquery[i][2]を追加
                sentou = query[i][2] + sentou
            else:
                #末尾にquery[i][2]を追加
                matubi = matubi + query[i][2]
        #print(s)
    else:
        if len(query[i]) == 1:
            hanten = False
        else:
            if int(query[i][1]) == 1:
                #末尾にquery[i][2]を追加
                matubi = matubi + query[i][2]
            else:
                #先頭にquery[i][2]を追加
                sentou = query[i][2] + sentou
        #print(s)

s = sentou + s + matubi
if hanten == False:
    print(s)
else:
    print(s[::-1])