Buggy python submission (verdict: Time Limit Exceeded):
s = input()
q = int(input())

t = [0] * q
f = [0] * q
c = [""] * q

for i in range(q):
    lis = list(input().split())
    t[i] = int(lis[0])
    if t[i] == 2:
        f[i] = int(lis[1])
        c[i] = lis[2]

idx = 0
chs = ""
s_flip = False
ins = 0
while idx < q:
    if t[idx] == 1:
        flip = True
        while idx + 1 < q and t[idx + 1] == 1:
            idx += 1
            flip = not flip
        if flip:
            chs = chs[::-1]
            ins = len(chs) - ins
            s_flip = not s_flip
    else:
        if f[idx] == 1:
            ch = c[idx]
            while idx + 1 < q and t[idx + 1] == 2 and f[idx + 1] == 1:
                idx += 1
                ch = c[idx] + ch
            chs = ch + chs
            ins += len(ch)
        else:
            ch = c[idx]
            while idx + 1 < q and t[idx + 1] == 2 and f[idx + 1] == 2:
                idx += 1
                ch = ch + c[idx]
            chs = chs + ch
    idx += 1

ans = chs[:ins]
ans += s[::-1] if s_flip else s
ans += chs[ins:]
print(ans)



Fixed submission:
s = input()
q = int(input())

t = [0] * q
f = [0] * q
c = [""] * q

for i in range(q):
    lis = list(input().split())
    t[i] = int(lis[0])
    if t[i] == 2:
        f[i] = int(lis[1])
        c[i] = lis[2]

idx = 0

p = ""
while idx < q:
    if t[idx] == 1:
        s, p = p, s
    else:
        if f[idx] == 1:
            p += c[idx]
        else:
            s += c[idx]
    idx += 1

p = p[::-1]
s = p + s
print(s)

Buggy python submission (verdict: Time Limit Exceeded):
s = input()
q = int(input())

t = [0] * q
f = [0] * q
c = [""] * q

for i in range(q):
    lis = list(input().split())
    t[i] = int(lis[0])
    if t[i] == 2:
        f[i] = int(lis[1])
        c[i] = lis[2]

idx = 0
chs = ""
s_flip = False
ins = 0
while idx < q:
    if t[idx] == 1:
        flip = True
        while idx + 1 < q and t[idx + 1] == 1:
            idx += 1
            flip = not flip
        if flip:
            chs = chs[::-1]
            ins = len(chs) - ins
            s_flip = not s_flip
    else:
        if f[idx] == 1:
            ch = c[idx]
            while idx + 1 < q and t[idx + 1] == 2 and f[idx + 1] == 1:
                idx += 1
                ch = c[idx] + ch
            chs = ch + chs
            ins += len(ch)
        else:
            ch = c[idx]
            while idx + 1 < q and t[idx + 1] == 2 and f[idx + 1] == 2:
                idx += 1
                ch = ch + c[idx]
            chs = chs + ch
    idx += 1

ans = chs[:ins]
ans += s[::-1] if s_flip else s
ans += chs[ins:]
print(ans)



Fixed submission:
s = input()
q = int(input())

t = [0] * q
f = [0] * q
c = [""] * q

for i in range(q):
    lis = list(input().split())
    t[i] = int(lis[0])
    if t[i] == 2:
        f[i] = int(lis[1])
        c[i] = lis[2]

idx = 0

p = ""
while idx < q:
    if t[idx] == 1:
        s, p = p, s
    else:
        if f[idx] == 1:
            p += c[idx]
        else:
            s += c[idx]
    idx += 1

p = p[::-1]
s = p + s
print(s)

Buggy python submission (verdict: Time Limit Exceeded):
s = input()
q = int(input())

t = [0] * q
f = [0] * q
c = [""] * q

for i in range(q):
    lis = list(input().split())
    t[i] = int(lis[0])
    if t[i] == 2:
        f[i] = int(lis[1])
        c[i] = lis[2]

idx = 0
while idx < q:
    if t[idx] == 1:
        flip = True
        while idx + 1 < q and t[idx + 1] == 1:
            idx += 1
            flip = not flip
        if flip:
            s = s[::-1]
    else:
        if f[idx] == 1:
            ch = c[idx]
            while idx + 1 < q and t[idx + 1] == 2 and f[idx + 1] == 1:
                idx += 1
                ch = c[idx] + ch
            s = ch + s
        else:
            ch = c[idx]
            while idx + 1 < q and t[idx + 1] == 2 and f[idx + 1] == 2:
                idx += 1
                ch = ch + c[idx]
            s = s + ch
    idx += 1

print(s)



Fixed submission:
s = input()
q = int(input())

t = [0] * q
f = [0] * q
c = [""] * q

for i in range(q):
    lis = list(input().split())
    t[i] = int(lis[0])
    if t[i] == 2:
        f[i] = int(lis[1])
        c[i] = lis[2]

idx = 0

p = ""
while idx < q:
    if t[idx] == 1:
        s, p = p, s
    else:
        if f[idx] == 1:
            p += c[idx]
        else:
            s += c[idx]
    idx += 1

p = p[::-1]
s = p + s
print(s)

Buggy python submission (verdict: Time Limit Exceeded):
s = input()
q = int(input())

t = [0] * q
f = [0] * q
c = [""] * q

for i in range(q):
    lis = list(input().split())
    t[i] = int(lis[0])
    if t[i] == 2:
        f[i] = int(lis[1])
        c[i] = lis[2]

idx = 0
while idx < q:
    if t[idx] == 1:
        flip = True
        while idx + 1 < q and t[idx + 1] == 1:
            idx += 1
            flip = not flip
        if flip:
            s = s[::-1]
    else:
        if f[idx] == 1:
            s = c[idx] + s
        else:
            s = s + c[idx]
    idx += 1

print(s)



Fixed submission:
s = input()
q = int(input())

t = [0] * q
f = [0] * q
c = [""] * q

for i in range(q):
    lis = list(input().split())
    t[i] = int(lis[0])
    if t[i] == 2:
        f[i] = int(lis[1])
        c[i] = lis[2]

idx = 0

p = ""
while idx < q:
    if t[idx] == 1:
        s, p = p, s
    else:
        if f[idx] == 1:
            p += c[idx]
        else:
            s += c[idx]
    idx += 1

p = p[::-1]
s = p + s
print(s)