Buggy python submission (verdict: Time Limit Exceeded):
def main():

    S = input()
    Q = int(input())

    reverse = False
    for _ in range(Q):
        query= input()
        if query[0] == '1':
            reverse = not reverse
        elif query[2] == '1':
            if reverse:
                S = ''.join([S,query[4]])
            else:
                S = ''.join([query[4],S])
        elif query[2] == '2':
            if reverse:
                S = ''.join([query[4],S])
            else:
                S = ''.join([S,query[4]])

    if reverse:
        S = S[::-1]

    print(S)

if __name__ == "__main__":
    main()

Fixed submission:
def main():

    S = input()
    Q = int(input())

    reverse = False
    head = ''
    tail = ''

    for _ in range(Q):
        query= input()
        if query == '1':
            reverse = not reverse
            head, tail = tail, head
        else:
            _, f, c = query.split()
            if f == '1':
                head = ''.join([head,c])
            else:
                tail = ''.join([tail,c])

    if reverse:
        S = S[::-1]

    print(''.join([head[::-1], S, tail]))

if __name__ == "__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
S = input()
Q = int(input())

reverse = False
for _ in range(Q):
    query= list(input().split()) 
    if query[0] == '1':
        reverse = not reverse
    elif query[0] == '2':
        if query[1] == '1':
            if reverse:
                S = ''.join([S,query[2]])
            else:
                S = ''.join([query[2],S])
        elif query[1] == '2':
            if reverse:
                S = ''.join([query[2],S])
            else:
                S = ''.join([S,query[2]])

if reverse:
    S = S[::-1]

print(S)

Fixed submission:
def main():

    S = input()
    Q = int(input())

    reverse = False
    head = ''
    tail = ''

    for _ in range(Q):
        query= input()
        if query == '1':
            reverse = not reverse
            head, tail = tail, head
        else:
            _, f, c = query.split()
            if f == '1':
                head = ''.join([head,c])
            else:
                tail = ''.join([tail,c])

    if reverse:
        S = S[::-1]

    print(''.join([head[::-1], S, tail]))

if __name__ == "__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
S = input()
Q = int(input())

for _ in range(Q):
    query= list(input().split()) 
    if query[0] == '1':
        S = S[::-1]
    elif query[0] == '2':
        if query[1] == '1':
            S = ''.join([query[2],S])
        elif query[1] == '2':
            S = ''.join([S,query[2]])

print(S)

Fixed submission:
def main():

    S = input()
    Q = int(input())

    reverse = False
    head = ''
    tail = ''

    for _ in range(Q):
        query= input()
        if query == '1':
            reverse = not reverse
            head, tail = tail, head
        else:
            _, f, c = query.split()
            if f == '1':
                head = ''.join([head,c])
            else:
                tail = ''.join([tail,c])

    if reverse:
        S = S[::-1]

    print(''.join([head[::-1], S, tail]))

if __name__ == "__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
S = input()
Q = int(input())
query_q = [input().split() for _ in range(Q)]

for query in query_q:
    if query[0] == '1':
        S = S[::-1]
    elif query[0] == '2':
        if query[1] == '1':
            S = ''.join([query[2],S])
        elif query[1] == '2':
            S = ''.join([S,query[2]])

print(S)

Fixed submission:
def main():

    S = input()
    Q = int(input())

    reverse = False
    head = ''
    tail = ''

    for _ in range(Q):
        query= input()
        if query == '1':
            reverse = not reverse
            head, tail = tail, head
        else:
            _, f, c = query.split()
            if f == '1':
                head = ''.join([head,c])
            else:
                tail = ''.join([tail,c])

    if reverse:
        S = S[::-1]

    print(''.join([head[::-1], S, tail]))

if __name__ == "__main__":
    main()

Buggy python submission (verdict: Runtime Error):
S = input()
Q = int(input())
query_q = [input().split() for _ in range(Q)]

for query in query_q:
    if query[0] == '1':
        S = S[::-1]
    elif query[0] == '2':
        if query[1] == '1':
            S = ''.join([query[2],*S])
        elif query[1] == '2':
            S = ''.join([*S,query[2]])

print(S)

Fixed submission:
def main():

    S = input()
    Q = int(input())

    reverse = False
    head = ''
    tail = ''

    for _ in range(Q):
        query= input()
        if query == '1':
            reverse = not reverse
            head, tail = tail, head
        else:
            _, f, c = query.split()
            if f == '1':
                head = ''.join([head,c])
            else:
                tail = ''.join([tail,c])

    if reverse:
        S = S[::-1]

    print(''.join([head[::-1], S, tail]))

if __name__ == "__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
S = input()
Q = int(input())
query_q = [input().split() for _ in range(Q)]

for query in query_q:
    if query[0] == '1':
        S = S[::-1]
    if query[0] == '2':
        if query[1] == '1':
            S = query[2] + S
        elif query[1] == '2':
            S = S + query[2]

print(S)

Fixed submission:
def main():

    S = input()
    Q = int(input())

    reverse = False
    head = ''
    tail = ''

    for _ in range(Q):
        query= input()
        if query == '1':
            reverse = not reverse
            head, tail = tail, head
        else:
            _, f, c = query.split()
            if f == '1':
                head = ''.join([head,c])
            else:
                tail = ''.join([tail,c])

    if reverse:
        S = S[::-1]

    print(''.join([head[::-1], S, tail]))

if __name__ == "__main__":
    main()