Buggy python submission (verdict: Time Limit Exceeded):
def main():
    s = input()
    q = int(input())
    hanten = False
    for i in range(q):
        query = input().split()
        if query[0] == "1":
            hanten = not hanten
        else:
            if query[1] == "1":
                if hanten:
                    s = s + query[2]
                else:
                    s = query[2] + s
            if query[1] == "2":
                if hanten:
                    s = query[2] + s
                else:
                    s = s + query[2]
    print(s if not hanten else s[::-1])



if __name__ == '__main__':
    main()


Fixed submission:
def main():
    s = input()
    q = int(input())
    left = ""
    right = ""
    hanten = False
    for i in range(q):
        query = input().split()
        if query[0] == "1":
            hanten = not hanten
        else:
            c = query[2]
            if query[1] == "1":
                if not hanten:
                    left += c
                else:
                    right += c
            else:
                if not hanten:
                    right += c
                else:
                    left += c
        #print(query)
        #print(s,left,right)
    left = left[::-1]
    answer = left + s + right

    if not hanten:
        print(answer)
    else:
        print(answer[::-1])


if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
def main():
    s = input()
    q = int(input())
    r = s[::-1]
    for i in range(q):
        query = input().split()
        if query[0] == "1":
            s,r = r,s
        else:
            if query[1] == "1":
                s = "".join([query[2],s])
                r = "".join([r,query[2]])
            if query[1] == "2":
                s = "".join([s,query[2]])
                r = "".join([query[2],r])
    print(s)



if __name__ == '__main__':
    main()


Fixed submission:
def main():
    s = input()
    q = int(input())
    left = ""
    right = ""
    hanten = False
    for i in range(q):
        query = input().split()
        if query[0] == "1":
            hanten = not hanten
        else:
            c = query[2]
            if query[1] == "1":
                if not hanten:
                    left += c
                else:
                    right += c
            else:
                if not hanten:
                    right += c
                else:
                    left += c
        #print(query)
        #print(s,left,right)
    left = left[::-1]
    answer = left + s + right

    if not hanten:
        print(answer)
    else:
        print(answer[::-1])


if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
def main():
    s = input()
    q = int(input())
    r = s
    hanten = False
    for i in range(q):
        query = input().split()
        if query[0] == "1":
            s,r = r,s
            hanten = not hanten
        else:
            if query[1] == "1":
                s = query[2] + s
                r = r + query[2]
            if query[1] == "2":
                s = s + query[2]
                r = query[2] + r
    if hanten:
        answer = ""
        for i in range(len(r)-1,-1,-1):
            answer = answer + r[i]
    else:
        answer = s
    print(answer)



if __name__ == '__main__':
    main()


Fixed submission:
def main():
    s = input()
    q = int(input())
    left = ""
    right = ""
    hanten = False
    for i in range(q):
        query = input().split()
        if query[0] == "1":
            hanten = not hanten
        else:
            c = query[2]
            if query[1] == "1":
                if not hanten:
                    left += c
                else:
                    right += c
            else:
                if not hanten:
                    right += c
                else:
                    left += c
        #print(query)
        #print(s,left,right)
    left = left[::-1]
    answer = left + s + right

    if not hanten:
        print(answer)
    else:
        print(answer[::-1])


if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
def main():
    s = input()
    q = int(input())
    r = s[::-1]
    for i in range(q):
        query = input().split()
        if query[0] == "1":
            s,r = r,s
        else:
            if query[1] == "1":
                s = query[2] + s
                r = r + query[2]
            if query[1] == "2":
                s = s + query[2]
                r = query[2] + r
    print(s)



if __name__ == '__main__':
    main()


Fixed submission:
def main():
    s = input()
    q = int(input())
    left = ""
    right = ""
    hanten = False
    for i in range(q):
        query = input().split()
        if query[0] == "1":
            hanten = not hanten
        else:
            c = query[2]
            if query[1] == "1":
                if not hanten:
                    left += c
                else:
                    right += c
            else:
                if not hanten:
                    right += c
                else:
                    left += c
        #print(query)
        #print(s,left,right)
    left = left[::-1]
    answer = left + s + right

    if not hanten:
        print(answer)
    else:
        print(answer[::-1])


if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
def main():
    s = input()
    q = int(input())
    for i in range(q):
        query = input().split()
        if query[0] == "1":
            s = s[::-1]
        else:
            if query[1] == "1":
                s = query[2] + s
            if query[1] == "2":
                s = s + query[2]
    print(s)



if __name__ == '__main__':
    main()


Fixed submission:
def main():
    s = input()
    q = int(input())
    left = ""
    right = ""
    hanten = False
    for i in range(q):
        query = input().split()
        if query[0] == "1":
            hanten = not hanten
        else:
            c = query[2]
            if query[1] == "1":
                if not hanten:
                    left += c
                else:
                    right += c
            else:
                if not hanten:
                    right += c
                else:
                    left += c
        #print(query)
        #print(s,left,right)
    left = left[::-1]
    answer = left + s + right

    if not hanten:
        print(answer)
    else:
        print(answer[::-1])


if __name__ == '__main__':
    main()