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


Fixed submission:
def main():
    s = input()
    q = int(input())
    reverse = 0
    head = ""
    tail = ""
    for _ in range(q):
        query = input().split()
        if query[0] == "1":
            reverse = not(reverse)
        else:
            if (reverse^int(query[1]))&1:
                head += query[2]
            else:
                tail += query[2]
    s = head[::-1] + s + tail
    if reverse&1:
        print(s[::-1])
    else:
        print(s)
if __name__ == "__main__":
    main()

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


Fixed submission:
def main():
    s = input()
    q = int(input())
    reverse = 0
    head = ""
    tail = ""
    for _ in range(q):
        query = input().split()
        if query[0] == "1":
            reverse = not(reverse)
        else:
            if (reverse^int(query[1]))&1:
                head += query[2]
            else:
                tail += query[2]
    s = head[::-1] + s + tail
    if reverse&1:
        print(s[::-1])
    else:
        print(s)
if __name__ == "__main__":
    main()

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


Fixed submission:
def main():
    s = input()
    q = int(input())
    reverse = 0
    head = ""
    tail = ""
    for _ in range(q):
        query = input().split()
        if query[0] == "1":
            reverse = not(reverse)
        else:
            if (reverse^int(query[1]))&1:
                head += query[2]
            else:
                tail += query[2]
    s = head[::-1] + s + tail
    if reverse&1:
        print(s[::-1])
    else:
        print(s)
if __name__ == "__main__":
    main()

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


Fixed submission:
def main():
    s = input()
    q = int(input())
    reverse = 0
    head = ""
    tail = ""
    for _ in range(q):
        query = input().split()
        if query[0] == "1":
            reverse = not(reverse)
        else:
            if (reverse^int(query[1]))&1:
                head += query[2]
            else:
                tail += query[2]
    s = head[::-1] + s + tail
    if reverse&1:
        print(s[::-1])
    else:
        print(s)
if __name__ == "__main__":
    main()

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


Fixed submission:
def main():
    s = input()
    q = int(input())
    reverse = 0
    head = ""
    tail = ""
    for _ in range(q):
        query = input().split()
        if query[0] == "1":
            reverse = not(reverse)
        else:
            if (reverse^int(query[1]))&1:
                head += query[2]
            else:
                tail += query[2]
    s = head[::-1] + s + tail
    if reverse&1:
        print(s[::-1])
    else:
        print(s)
if __name__ == "__main__":
    main()