Buggy python submission (verdict: Time Limit Exceeded):
#!/usr/bin/env python3

def main():
    S = input()
    Q = int(input())
    reflag = False
    for i in range(Q):
        query = list(input())
        if query[0] == '1':
            reflag = not reflag
        elif query[2] == '1':
            if reflag:
                S = S + query[4]
            else:
                S = query[4] + S
        else:
            if reflag:
                S = query[4] + S
            else:
                S = S + query[4]
    if reflag:
        print(S[::-1])
    else:
        print(S)


if __name__ == "__main__":
    main()


Fixed submission:
#!/usr/bin/env python3
from collections import deque


def main():
    d = deque()
    d.extend(list(input()))

    Q = int(input())
    reflag = False

    for i in range(Q):
        query = list(input().split())
        if query[0] == '1':
            reflag = not reflag
        elif (query[1] == '1' and not reflag) or (query[1] == '2' and reflag):
            d.appendleft(query[2])
        else:
            d.append(query[2])

    if reflag:
        print("".join(reversed(d)))
    else:
        print("".join(d))


if __name__ == "__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
#!/usr/bin/env python3

def main():
    S = input()
    Q = int(input())
    reflag = False
    for i in range(Q):
        query = list(input())
        if query[0] == '1':
            reflag = not reflag
        elif query[2] == '1':
            if reflag:
                S = S + query[4]
            else:
                S = query[4] + S
        else:
            if reflag:
                S = query[4] + S
            else:
                S = S + query[4]
    if reflag:
        print(S[::-1])
    else:
        print(S)


if __name__ == "__main__":
    main()


Fixed submission:
#!/usr/bin/env python3
from collections import deque


def main():
    d = deque()
    d.extend(list(input()))

    Q = int(input())
    reflag = False

    for i in range(Q):
        query = list(input().split())
        if query[0] == '1':
            reflag = not reflag
        elif (query[1] == '1' and not reflag) or (query[1] == '2' and reflag):
            d.appendleft(query[2])
        else:
            d.append(query[2])

    if reflag:
        print("".join(reversed(d)))
    else:
        print("".join(d))


if __name__ == "__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
#!/usr/bin/env python3

def main():
    S = input()
    Q = int(input())

    for i in range(Q):
        query = list(input())
        if query[0] == '1':
            S = S[::-1]
        elif query[2] == '1':
            S = query[4] + S
        else:
            S = S + query[4]
    print(S)


if __name__ == "__main__":
    main()


Fixed submission:
#!/usr/bin/env python3
from collections import deque


def main():
    d = deque()
    d.extend(list(input()))

    Q = int(input())
    reflag = False

    for i in range(Q):
        query = list(input().split())
        if query[0] == '1':
            reflag = not reflag
        elif (query[1] == '1' and not reflag) or (query[1] == '2' and reflag):
            d.appendleft(query[2])
        else:
            d.append(query[2])

    if reflag:
        print("".join(reversed(d)))
    else:
        print("".join(d))


if __name__ == "__main__":
    main()