Buggy python submission (verdict: Time Limit Exceeded):
#input
S = input()
Q = int(input())
P = [[]] * Q
for i in range(Q):
    P[i] = list(map(str, input().split()))

#output
T = "AB"
for i in range(Q):
    if len(P[i]) == 1:
        T = T[-1::-1]
    else:
        v = P[i][2]
        if int(P[i][1]) == 1:
            T = v + T
        else:
            T = T + v

if "AB" in T:
    S = T.replace("AB", S)
else:
    S = T.replace("BA", S[-1::-1])
                        
print(S)

Fixed submission:
#input
S = input()
Q = int(input())
P = [[]] * Q
for i in range(Q):
    P[i] = list(map(str, input().split()))

#output
flip = 0
cap = ""
tail = ""

for i in range(Q):
    if int(P[i][0]) == 1:
        flip += 1
    else:
        n = int(P[i][1])
        v = P[i][2]
        if flip % 2 == 0:
            if n == 1:
                cap = v + cap
            else:
                tail = tail + v
        else:
            if n == 1:
                tail = tail + v
            else:
                cap = v + cap

answer = ""
if flip % 2 == 0:
    answer = cap + S + tail
else:
    answer = tail[-1::-1] + S[-1::-1] + cap[-1::-1]

print(answer)

Buggy python submission (verdict: Time Limit Exceeded):
#input
S = input()
Q = int(input())
P = [[]] * Q
for i in range(Q):
    P[i] = list(map(str, input().split()))

#output
T = "AB"
for i in range(Q):
    if len(P[i]) == 1:
        T = T[-1::-1]
    else:
        v = P[i][2]
        if int(P[i][1]) == 1:
            T = v + T
        else:
            T = T + v

if "AB" in T:
    S = T.replace("AB", S)
else:
    S = T.replace("BA", S[-1::-1])
                        
print(S)

Fixed submission:
#input
S = input()
Q = int(input())
P = [[]] * Q
for i in range(Q):
    P[i] = list(map(str, input().split()))

#output
flip = 0
cap = ""
tail = ""

for i in range(Q):
    if int(P[i][0]) == 1:
        flip += 1
    else:
        n = int(P[i][1])
        v = P[i][2]
        if flip % 2 == 0:
            if n == 1:
                cap = v + cap
            else:
                tail = tail + v
        else:
            if n == 1:
                tail = tail + v
            else:
                cap = v + cap

answer = ""
if flip % 2 == 0:
    answer = cap + S + tail
else:
    answer = tail[-1::-1] + S[-1::-1] + cap[-1::-1]

print(answer)

Buggy python submission (verdict: Time Limit Exceeded):
#input
S = input()
Q = int(input())
P = []
for i in range(Q):
    P.append(list(map(str, input().split())))

#output
T = "AB"
for i in range(Q):
    if len(P[i]) == 1:
        T = T[-1::-1]
    else:
        v = P[i][2]
        if int(P[i][1]) == 1:
            T = v + T
        else:
            T = T + v

if "AB" in T:
    S = T.replace("AB", S)
else:
    S = T.replace("BA", S[-1::-1])
                        
print(S)

Fixed submission:
#input
S = input()
Q = int(input())
P = [[]] * Q
for i in range(Q):
    P[i] = list(map(str, input().split()))

#output
flip = 0
cap = ""
tail = ""

for i in range(Q):
    if int(P[i][0]) == 1:
        flip += 1
    else:
        n = int(P[i][1])
        v = P[i][2]
        if flip % 2 == 0:
            if n == 1:
                cap = v + cap
            else:
                tail = tail + v
        else:
            if n == 1:
                tail = tail + v
            else:
                cap = v + cap

answer = ""
if flip % 2 == 0:
    answer = cap + S + tail
else:
    answer = tail[-1::-1] + S[-1::-1] + cap[-1::-1]

print(answer)

Buggy python submission (verdict: Time Limit Exceeded):
#input
S = input()
Q = int(input())
P = []
for i in range(Q):
    P.append(list(map(str, input().split())))

#output
for i in range(Q):
    if len(P[i]) == 1:
        S = S[-1::-1]
    else:
        v = P[i][2]
        if int(P[i][1]) == 1:
            S = v + S
        else:
            S = S + v
                        
print(S)

Fixed submission:
#input
S = input()
Q = int(input())
P = [[]] * Q
for i in range(Q):
    P[i] = list(map(str, input().split()))

#output
flip = 0
cap = ""
tail = ""

for i in range(Q):
    if int(P[i][0]) == 1:
        flip += 1
    else:
        n = int(P[i][1])
        v = P[i][2]
        if flip % 2 == 0:
            if n == 1:
                cap = v + cap
            else:
                tail = tail + v
        else:
            if n == 1:
                tail = tail + v
            else:
                cap = v + cap

answer = ""
if flip % 2 == 0:
    answer = cap + S + tail
else:
    answer = tail[-1::-1] + S[-1::-1] + cap[-1::-1]

print(answer)

Buggy python submission (verdict: Time Limit Exceeded):
#input
S = input()
Q = int(input())
P = []
for i in range(Q):
    P.append(list(map(str, input().split())))

#output
for i in range(Q):
    if len(P[i]) == 1:
        S = S[-1::-1]
    else:
        v = P[i][2]
        if int(P[i][1]) == 1:
            T = v + S
            S = T
        else:
            T = S + v
            S = T
                        
print(S)

Fixed submission:
#input
S = input()
Q = int(input())
P = [[]] * Q
for i in range(Q):
    P[i] = list(map(str, input().split()))

#output
flip = 0
cap = ""
tail = ""

for i in range(Q):
    if int(P[i][0]) == 1:
        flip += 1
    else:
        n = int(P[i][1])
        v = P[i][2]
        if flip % 2 == 0:
            if n == 1:
                cap = v + cap
            else:
                tail = tail + v
        else:
            if n == 1:
                tail = tail + v
            else:
                cap = v + cap

answer = ""
if flip % 2 == 0:
    answer = cap + S + tail
else:
    answer = tail[-1::-1] + S[-1::-1] + cap[-1::-1]

print(answer)

Buggy python submission (verdict: Time Limit Exceeded):
#input
S = input()
Q = int(input())
P = []
for i in range(Q):
    P.append(list(map(str, input().split())))

#output
for i in range(Q):
    if len(P[i]) == 1:
        S = S[-1::-1]
    else:
        v = P[i][2]
        if int(P[i][1]) == 1:
            T = v + S
            S = T
        else:
            T = S + v
            S = T
                        
print(S)

Fixed submission:
#input
S = input()
Q = int(input())
P = [[]] * Q
for i in range(Q):
    P[i] = list(map(str, input().split()))

#output
flip = 0
cap = ""
tail = ""

for i in range(Q):
    if int(P[i][0]) == 1:
        flip += 1
    else:
        n = int(P[i][1])
        v = P[i][2]
        if flip % 2 == 0:
            if n == 1:
                cap = v + cap
            else:
                tail = tail + v
        else:
            if n == 1:
                tail = tail + v
            else:
                cap = v + cap

answer = ""
if flip % 2 == 0:
    answer = cap + S + tail
else:
    answer = tail[-1::-1] + S[-1::-1] + cap[-1::-1]

print(answer)