Buggy python submission (verdict: Time Limit Exceeded):
def main():
    S = input()
    Q = int(input())
    ans = S
    rev = False # rev=1で反転した状態
    for i in range(Q):
        query = input()
        if len(query)==1:
            rev = not rev
        else:
            tmp = list(query.split())
            f = int(tmp[1])
            c = tmp[2]
            if rev:
                ans = ans+c if f==1 else c+ans
            else:
                ans = c+ans if f==1 else ans+c

    ans = ans[::-1] if rev else ans
    print(ans)

main()


Fixed submission:
def main():
    S = input()
    Q = int(input())
    rev = False # rev=1で反転した状態
    tail = ""
    head = ""
    for i in range(Q):
        query = input()
        if len(query)==1:
            rev = not rev
        else:
            tmp = list(query.split())
            f = int(tmp[1])
            c = tmp[2]
            if rev:
                if f==1:
                    tail+=c
                else:
                    head=c+head
            else:
                if f==1:
                    head=c+head
                else:
                    tail+=c

    ans = tail[::-1]+S[::-1]+head[::-1] if rev else head+S+tail
    print(ans)

main()

Buggy python submission (verdict: Time Limit Exceeded):
def main():
    S = input()
    Q = int(input())
    t = [-1] * Q
    ans = S
    for i in range(Q):
        query = input()
        if len(query)==1:
            ans = ans[::-1]
        else:
            tmp = list(query.split())
            f = int(tmp[1])
            c = tmp[2]
            ans = c+ans if f==1 else ans+c
            
    print(ans)

main()


Fixed submission:
def main():
    S = input()
    Q = int(input())
    rev = False # rev=1で反転した状態
    tail = ""
    head = ""
    for i in range(Q):
        query = input()
        if len(query)==1:
            rev = not rev
        else:
            tmp = list(query.split())
            f = int(tmp[1])
            c = tmp[2]
            if rev:
                if f==1:
                    tail+=c
                else:
                    head=c+head
            else:
                if f==1:
                    head=c+head
                else:
                    tail+=c

    ans = tail[::-1]+S[::-1]+head[::-1] if rev else head+S+tail
    print(ans)

main()

Buggy python submission (verdict: Time Limit Exceeded):
def main():
    S = input()
    Q = int(input())
    t = [-1] * Q
    f = [-1] * Q
    c = [""] * Q
    for i in range(Q):
        query = input()
        if len(query)==1:
            t[i] = 1
        else:
            tmp = list(query.split())
            t[i] = int(tmp[0])
            f[i] = int(tmp[1])
            c[i] = tmp[2]

    ans = S
    for i in range(Q):
        if t[i]==1:
            ans = ans[::-1]
        else:
            if f[i]==1:
                ans = c[i]+ans
            else:
                ans = ans+c[i]
    print(ans)

main()


Fixed submission:
def main():
    S = input()
    Q = int(input())
    rev = False # rev=1で反転した状態
    tail = ""
    head = ""
    for i in range(Q):
        query = input()
        if len(query)==1:
            rev = not rev
        else:
            tmp = list(query.split())
            f = int(tmp[1])
            c = tmp[2]
            if rev:
                if f==1:
                    tail+=c
                else:
                    head=c+head
            else:
                if f==1:
                    head=c+head
                else:
                    tail+=c

    ans = tail[::-1]+S[::-1]+head[::-1] if rev else head+S+tail
    print(ans)

main()