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

    s = input()
    q = int(input())

    flag = 0
    query = []
    for _ in range(q):
        temp = input().split()
        if len(temp) == 1:
            flag += 1
        else:
            query.append(temp + [flag%2])

    for q in query:
        if q[-1] == 0:
            if q[1] == "1":
                s = q[2] + s
            else:
                s += q[2]
        else:
            if q[1] == "1":
                s += q[2]
            else:
                s = q[2] + s

    if flag % 2 == 1:
        print(s[::-1])
    else:
        print(s)

main()

Fixed submission:
from collections import deque

def main():

    s = deque(input())
    q = int(input())

    flag = 0
    query = []

    for _ in range(q):
        temp = input().split()
        if len(temp) == 1:
            flag += 1
        else:
            query.append(temp + [flag%2])

    for q in query:
        if q[-1] == 0:
            if q[1] == "1":
                s.appendleft(q[2])
            else:
                s.append(q[2])
        else:
            if q[1] == "1":
                s.append(q[2])
            else:
                s.appendleft(q[2])

    if flag % 2 == 1:
        print("".join(list(s)[::-1]))
    else:
        print("".join(s))

main()

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

    s = input()
    q = int(input())
    query = [input().split() for _ in range(q)]
    ctn = query.count(["1"]) % 2 == 1

    new_query = []
    flag = 0
    for q in query:
        if len(q) == 1:
            flag += 1
        else:
            q += [flag%2]
            new_query.append(q)

    for q in new_query:
        if q[-1] == 0:
            if q[1] == "1":
                s = q[2] + s
            else:
                s += q[2]
        else:
            if q[1] == "1":
                s += q[2]
            else:
                s = q[2] + s

    if ctn:
        print(s[::-1])
    else:
        print(s)

main()

Fixed submission:
from collections import deque

def main():

    s = deque(input())
    q = int(input())

    flag = 0
    query = []

    for _ in range(q):
        temp = input().split()
        if len(temp) == 1:
            flag += 1
        else:
            query.append(temp + [flag%2])

    for q in query:
        if q[-1] == 0:
            if q[1] == "1":
                s.appendleft(q[2])
            else:
                s.append(q[2])
        else:
            if q[1] == "1":
                s.append(q[2])
            else:
                s.appendleft(q[2])

    if flag % 2 == 1:
        print("".join(list(s)[::-1]))
    else:
        print("".join(s))

main()

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

    s = input()
    q = int(input())
    query = [input().split() for _ in range(q)]
    ctn = query.count(["1"]) % 2 == 1

    new_query = []
    flag = 0
    for q in query:
        if len(q) == 1:
            if flag == 0:
                flag = 1
            else:
                flag = 0
        else:
            q += [flag]
            new_query.append(q)

    for q in new_query:
        if q[-1] == 0:
            if q[1] == "1":
                s = q[2] + s
            else:
                s += q[2]
        else:
            if q[1] == "1":
                s += q[2]
            else:
                s = q[2] + s

    if ctn:
        print(s[::-1])
    else:
        print(s)

main()

Fixed submission:
from collections import deque

def main():

    s = deque(input())
    q = int(input())

    flag = 0
    query = []

    for _ in range(q):
        temp = input().split()
        if len(temp) == 1:
            flag += 1
        else:
            query.append(temp + [flag%2])

    for q in query:
        if q[-1] == 0:
            if q[1] == "1":
                s.appendleft(q[2])
            else:
                s.append(q[2])
        else:
            if q[1] == "1":
                s.append(q[2])
            else:
                s.appendleft(q[2])

    if flag % 2 == 1:
        print("".join(list(s)[::-1]))
    else:
        print("".join(s))

main()

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

    s = input()
    q = int(input())
    query = [input().split() for _ in range(q)]
    ctn = query.count(["1"]) % 2 == 1

    new_query = []
    flag = 0
    for q in query:
        if len(q) == 1:
            if flag == 0:
                flag = 1
            else:
                flag = 0
        else:
            new_query.append(q + [flag])

    for q in new_query:
        if q[-1] == 0:
            if q[1] == "1":
                s = q[2] + s
            else:
                s = s + q[2]
        else:
            if q[1] == "1":
                s = s + q[2]
            else:
                s = q[2] + s

    if ctn:
        print(s[::-1])
    else:
        print(s)

main()

Fixed submission:
from collections import deque

def main():

    s = deque(input())
    q = int(input())

    flag = 0
    query = []

    for _ in range(q):
        temp = input().split()
        if len(temp) == 1:
            flag += 1
        else:
            query.append(temp + [flag%2])

    for q in query:
        if q[-1] == 0:
            if q[1] == "1":
                s.appendleft(q[2])
            else:
                s.append(q[2])
        else:
            if q[1] == "1":
                s.append(q[2])
            else:
                s.appendleft(q[2])

    if flag % 2 == 1:
        print("".join(list(s)[::-1]))
    else:
        print("".join(s))

main()

Buggy python submission (verdict: Time Limit Exceeded):
s = input()
query = [input().split() for _ in range(int(input()))]
ctn = query.count(["1"]) % 2 == 1

new_query = []
flag = 0
for q in query:
    if len(q) == 1:
        if flag == 0:
            flag = 1
        else:
            flag = 0
    else:
        new_query.append(q + [flag])

for q in new_query:
    if q[-1] == 0:
        if q[1] == "1":
            s = q[2] + s
        else:
            s = s + q[2]
    else:
        if q[1] == "1":
            s = s + q[2]
        else:
            s = q[2] + s

if ctn:
    print(s[::-1])
else:
    print(s)

Fixed submission:
from collections import deque

def main():

    s = deque(input())
    q = int(input())

    flag = 0
    query = []

    for _ in range(q):
        temp = input().split()
        if len(temp) == 1:
            flag += 1
        else:
            query.append(temp + [flag%2])

    for q in query:
        if q[-1] == 0:
            if q[1] == "1":
                s.appendleft(q[2])
            else:
                s.append(q[2])
        else:
            if q[1] == "1":
                s.append(q[2])
            else:
                s.appendleft(q[2])

    if flag % 2 == 1:
        print("".join(list(s)[::-1]))
    else:
        print("".join(s))

main()

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

    s = input()
    q = int(input())

    query = []
    flag = 0
    ctn = 0
    for _ in range(q):
        temp = input().split()
        if len(temp) == 1:
            if flag == 0:
                flag = 1
            else:
                flag = 0
            ctn += 1
        else:
            query.append(temp + [flag])

    for q in query:
        if q[-1] == 0:
            if q[1] == "1":
                s = q[2] + s
            else:
                s = s + q[2]
        else:
            if q[1] == "1":
                s = s + q[2]
            else:
                s = q[2] + s

    if ctn % 2 == 1:
        print(s[::-1])
    else:
        print(s)

main()

Fixed submission:
from collections import deque

def main():

    s = deque(input())
    q = int(input())

    flag = 0
    query = []

    for _ in range(q):
        temp = input().split()
        if len(temp) == 1:
            flag += 1
        else:
            query.append(temp + [flag%2])

    for q in query:
        if q[-1] == 0:
            if q[1] == "1":
                s.appendleft(q[2])
            else:
                s.append(q[2])
        else:
            if q[1] == "1":
                s.append(q[2])
            else:
                s.appendleft(q[2])

    if flag % 2 == 1:
        print("".join(list(s)[::-1]))
    else:
        print("".join(s))

main()

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

    s = input()
    q = int(input())

    query = []
    flag = 0
    ctn = 0
    for _ in range(q):
        temp = input().split()
        if len(temp) == 1:
            if flag == 0:
                flag = 1
            else:
                flag = 0
            ctn += 1
        else:
            query.append(temp + [flag])

    for q in query:
        if q[-1] == 0:
            if q[1] == "1":
                s = q[2] + s
            else:
                s = s + q[2]
        else:
            if q[1] == "1":
                s = s + q[2]
            else:
                s = q[2] + s

    if ctn % 2 == 1:
        print(s[::-1])
    else:
        print(s)

main()


Fixed submission:
from collections import deque

def main():

    s = deque(input())
    q = int(input())

    flag = 0
    query = []

    for _ in range(q):
        temp = input().split()
        if len(temp) == 1:
            flag += 1
        else:
            query.append(temp + [flag%2])

    for q in query:
        if q[-1] == 0:
            if q[1] == "1":
                s.appendleft(q[2])
            else:
                s.append(q[2])
        else:
            if q[1] == "1":
                s.append(q[2])
            else:
                s.appendleft(q[2])

    if flag % 2 == 1:
        print("".join(list(s)[::-1]))
    else:
        print("".join(s))

main()

Buggy python submission (verdict: Time Limit Exceeded):
s = input()
q = int(input())

query = []
flag = 0
ctn = 0
for _ in range(q):
    temp = input().split()
    if len(temp) == 1:
        if flag == 0:
            flag = 1
        else:
            flag = 0
        ctn += 1
    else:
        query.append(temp + [flag])

for q in query:
    if q[-1] == 0:
        if q[1] == "1":
            s = q[2] + s
        else:
            s = s + q[2]
    else:
        if q[1] == "1":
            s = s + q[2]
        else:
            s = q[2] + s

if ctn % 2 == 1:
    print(s[::-1])
else:
    print(s)

Fixed submission:
from collections import deque

def main():

    s = deque(input())
    q = int(input())

    flag = 0
    query = []

    for _ in range(q):
        temp = input().split()
        if len(temp) == 1:
            flag += 1
        else:
            query.append(temp + [flag%2])

    for q in query:
        if q[-1] == 0:
            if q[1] == "1":
                s.appendleft(q[2])
            else:
                s.append(q[2])
        else:
            if q[1] == "1":
                s.append(q[2])
            else:
                s.appendleft(q[2])

    if flag % 2 == 1:
        print("".join(list(s)[::-1]))
    else:
        print("".join(s))

main()

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

    s = input()
    q = int(input())

    query = [tuple(input().split()) for _ in range(q)]
    ctn = query.count(("1",))

    flag = 0
    for q in query:
        if q[0] == "1":
            if flag == 1:
                flag = 0
            else:
                flag = 1
        else:
            if flag == 0:
                if q[1] == "1":
                    s = q[2] + s
                else:
                    s = s + q[2]
            else:
                if q[1] == "1":
                    s = s + q[2]
                else:
                    s = q[2] + s

    if ctn % 2 == 1:
        print(s[::-1])
    else:
        print(s)

main()


Fixed submission:
from collections import deque

def main():

    s = deque(input())
    q = int(input())

    flag = 0
    query = []

    for _ in range(q):
        temp = input().split()
        if len(temp) == 1:
            flag += 1
        else:
            query.append(temp + [flag%2])

    for q in query:
        if q[-1] == 0:
            if q[1] == "1":
                s.appendleft(q[2])
            else:
                s.append(q[2])
        else:
            if q[1] == "1":
                s.append(q[2])
            else:
                s.appendleft(q[2])

    if flag % 2 == 1:
        print("".join(list(s)[::-1]))
    else:
        print("".join(s))

main()

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

    s = input()
    q = int(input())

    query = [input().split() for _ in range(q)]
    ctn = query.count(["1"])

    flag = 0
    for q in query:
        if q[0] == "1":
            if flag == 1:
                flag = 0
            else:
                flag = 1
        else:
            if flag == 0:
                if q[1] == "1":
                    s = q[2] + s
                else:
                    s = s + q[2]
            else:
                if q[1] == "1":
                    s = s + q[2]
                else:
                    s = q[2] + s

    if ctn % 2 == 1:
        print(s[::-1])
    else:
        print(s)

main()


Fixed submission:
from collections import deque

def main():

    s = deque(input())
    q = int(input())

    flag = 0
    query = []

    for _ in range(q):
        temp = input().split()
        if len(temp) == 1:
            flag += 1
        else:
            query.append(temp + [flag%2])

    for q in query:
        if q[-1] == 0:
            if q[1] == "1":
                s.appendleft(q[2])
            else:
                s.append(q[2])
        else:
            if q[1] == "1":
                s.append(q[2])
            else:
                s.appendleft(q[2])

    if flag % 2 == 1:
        print("".join(list(s)[::-1]))
    else:
        print("".join(s))

main()

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

    s = input()
    q = int(input())

    ctn = 0
    query = []
    for _ in range(q):
        temp = input().split()
        if len(temp) == 1:
            ctn += 1
        query.append(temp)

    flag = 0
    for q in query:
        if q[0] == "1":
            if flag == 1:
                flag = 0
            else:
                flag = 1
        else:
            if flag == 0:
                if q[1] == "1":
                    s = q[2] + s
                else:
                    s = s + q[2]
            else:
                if q[1] == "1":
                    s = s + q[2]
                else:
                    s = q[2] + s

    if ctn % 2 == 1:
        print(s[::-1])
    else:
        print(s)

main()


Fixed submission:
from collections import deque

def main():

    s = deque(input())
    q = int(input())

    flag = 0
    query = []

    for _ in range(q):
        temp = input().split()
        if len(temp) == 1:
            flag += 1
        else:
            query.append(temp + [flag%2])

    for q in query:
        if q[-1] == 0:
            if q[1] == "1":
                s.appendleft(q[2])
            else:
                s.append(q[2])
        else:
            if q[1] == "1":
                s.append(q[2])
            else:
                s.appendleft(q[2])

    if flag % 2 == 1:
        print("".join(list(s)[::-1]))
    else:
        print("".join(s))

main()

Buggy python submission (verdict: Time Limit Exceeded):
s = input()
q = int(input())

ctn = 0
query = []
for _ in range(q):
    temp = input().split()
    if len(temp) == 1:
        ctn += 1
    query.append(temp)

flag = 0
for q in query:
    if q[0] == "1":
        if flag == 1:
            flag = 0
        else:
            flag = 1
    else:
        if flag == 0:
            if q[1] == "1":
                s = q[2] + s
            else:
                s = s + q[2]
        else:
            if q[1] == "1":
                s = s + q[2]
            else:
                s = q[2] + s

if ctn % 2 == 1:
    print(s[::-1])
else:
    print(s)

Fixed submission:
from collections import deque

def main():

    s = deque(input())
    q = int(input())

    flag = 0
    query = []

    for _ in range(q):
        temp = input().split()
        if len(temp) == 1:
            flag += 1
        else:
            query.append(temp + [flag%2])

    for q in query:
        if q[-1] == 0:
            if q[1] == "1":
                s.appendleft(q[2])
            else:
                s.append(q[2])
        else:
            if q[1] == "1":
                s.append(q[2])
            else:
                s.appendleft(q[2])

    if flag % 2 == 1:
        print("".join(list(s)[::-1]))
    else:
        print("".join(s))

main()

Buggy python submission (verdict: Time Limit Exceeded):
def main():
    s = list(input())
    q = int(input())

    flag = 1
    ctn = 0
    for _ in range(q):
        query = input().split()
        if flag:
            if query[0] == "1":
                flag = 0
                ctn += 1
            else:
                if query[1] == "1":
                    s = [query[-1]] + s
                else:
                    s = s + [query[-1]]
        else:
            if query[0] == "1":
                flag = 1
                ctn += 1
            else:
                if query[1] == "1":
                    s = s + [query[-1]]
                else:
                    s = [query[-1]] + s

    if ctn % 2 == 1:
        print("".join(s[::-1]))
    else:
        print("".join(s))

main()

Fixed submission:
from collections import deque

def main():

    s = deque(input())
    q = int(input())

    flag = 0
    query = []

    for _ in range(q):
        temp = input().split()
        if len(temp) == 1:
            flag += 1
        else:
            query.append(temp + [flag%2])

    for q in query:
        if q[-1] == 0:
            if q[1] == "1":
                s.appendleft(q[2])
            else:
                s.append(q[2])
        else:
            if q[1] == "1":
                s.append(q[2])
            else:
                s.appendleft(q[2])

    if flag % 2 == 1:
        print("".join(list(s)[::-1]))
    else:
        print("".join(s))

main()

Buggy python submission (verdict: Time Limit Exceeded):
def main():
    s = input()
    q = int(input())

    flag = 1
    ctn = 0
    for _ in range(q):
        query = input().split()
        if flag:
            if query[0] == "1":
                flag = 0
                ctn += 1
            else:
                if query[1] == "1":
                    s = query[-1] + s
                else:
                    s = s + query[-1]
        else:
            if query[0] == "1":
                flag = 1
                ctn += 1
            else:
                if query[1] == "1":
                    s = s + query[-1]
                else:
                    s = query[-1] + s

    if ctn % 2 == 1:
        print(s[::-1])
    else:
        print(s)

main()

Fixed submission:
from collections import deque

def main():

    s = deque(input())
    q = int(input())

    flag = 0
    query = []

    for _ in range(q):
        temp = input().split()
        if len(temp) == 1:
            flag += 1
        else:
            query.append(temp + [flag%2])

    for q in query:
        if q[-1] == 0:
            if q[1] == "1":
                s.appendleft(q[2])
            else:
                s.append(q[2])
        else:
            if q[1] == "1":
                s.append(q[2])
            else:
                s.appendleft(q[2])

    if flag % 2 == 1:
        print("".join(list(s)[::-1]))
    else:
        print("".join(s))

main()

Buggy python submission (verdict: Time Limit Exceeded):
def main():
    s = input()
    q = int(input())

    flag = 1
    ctn = 0
    for _ in range(q):
        query = input().split()
        if flag:
            if query[0] == "1":
                flag = 0
                ctn += 1
            else:
                if query[1] == "1":
                    s = query[2] + s
                else:
                    s = s + query[2]
        else:
            if query[0] == "1":
                flag = 1
                ctn += 1
            else:
                if query[1] == "1":
                    s = s + query[2]
                else:
                    s = query[2] + s

    if ctn % 2 == 1:
        print(s[::-1])
    else:
        print(s)

main()

Fixed submission:
from collections import deque

def main():

    s = deque(input())
    q = int(input())

    flag = 0
    query = []

    for _ in range(q):
        temp = input().split()
        if len(temp) == 1:
            flag += 1
        else:
            query.append(temp + [flag%2])

    for q in query:
        if q[-1] == 0:
            if q[1] == "1":
                s.appendleft(q[2])
            else:
                s.append(q[2])
        else:
            if q[1] == "1":
                s.append(q[2])
            else:
                s.appendleft(q[2])

    if flag % 2 == 1:
        print("".join(list(s)[::-1]))
    else:
        print("".join(s))

main()

Buggy python submission (verdict: Time Limit Exceeded):
def main():
    s = input()
    q = int(input())

    flag = 1
    ctn = 0
    for _ in range(q):
        query = input().split()
        if flag:
            if query[0] == "1":
                flag = 0
                ctn += 1
            else:
                if query[1] == "1":
                    s = query[2] + s
                elif query[1] == "2":
                    s = s + query[2]
        else:
            if query[0] == "1":
                flag = 1
                ctn += 1
            else:
                if query[1] == "1":
                    s = s + query[2]
                elif query[1] == "2":
                    s = query[2] + s

    if ctn % 2 == 1:
        print(s[::-1])
    else:
        print(s)

main()

Fixed submission:
from collections import deque

def main():

    s = deque(input())
    q = int(input())

    flag = 0
    query = []

    for _ in range(q):
        temp = input().split()
        if len(temp) == 1:
            flag += 1
        else:
            query.append(temp + [flag%2])

    for q in query:
        if q[-1] == 0:
            if q[1] == "1":
                s.appendleft(q[2])
            else:
                s.append(q[2])
        else:
            if q[1] == "1":
                s.append(q[2])
            else:
                s.appendleft(q[2])

    if flag % 2 == 1:
        print("".join(list(s)[::-1]))
    else:
        print("".join(s))

main()

Buggy python submission (verdict: Time Limit Exceeded):
def main():
    s = input()
    q = int(input())

    flag = 1
    ctn = 0
    for _ in range(q):
        query = input().split()
        if flag:
            if query[0] == "1":
                flag = 0
                ctn += 1
            else:
                if query[1] == "1":
                    s = query[2] + s
                elif query[1] == "2":
                    s = s + query[2]
        else:
            if query[0] == "1":
                flag = 1
                ctn += 1
            else:
                if query[1] == "1":
                    s = s + query[2]
                elif query[1] == "2":
                    s = query[2] + s

    if ctn % 2 == 1:
        print(s[::-1])
    else:
        print(s)

main()

Fixed submission:
from collections import deque

def main():

    s = deque(input())
    q = int(input())

    flag = 0
    query = []

    for _ in range(q):
        temp = input().split()
        if len(temp) == 1:
            flag += 1
        else:
            query.append(temp + [flag%2])

    for q in query:
        if q[-1] == 0:
            if q[1] == "1":
                s.appendleft(q[2])
            else:
                s.append(q[2])
        else:
            if q[1] == "1":
                s.append(q[2])
            else:
                s.appendleft(q[2])

    if flag % 2 == 1:
        print("".join(list(s)[::-1]))
    else:
        print("".join(s))

main()

Buggy python submission (verdict: Runtime Error):
    def main():
    s = input()
    q = int(input())

    flag = 1
    ctn = 0
    for _ in range(q):
        query = input().split()
        if flag:
            if query[0] == "1":
                flag = 0
                ctn += 1
            else:
                if query[1] == "1":
                    s = query[2] + s
                elif query[1] == "2":
                    s = s + query[2]
        else:
            if query[0] == "1":
                flag = 1
                ctn += 1
            else:
                if query[1] == "1":
                    s = s + query[2]
                elif query[1] == "2":
                    s = query[2] + s

    if ctn % 2 == 1:
        print(s[::-1])
    else:
        print(s)

main()

Fixed submission:
from collections import deque

def main():

    s = deque(input())
    q = int(input())

    flag = 0
    query = []

    for _ in range(q):
        temp = input().split()
        if len(temp) == 1:
            flag += 1
        else:
            query.append(temp + [flag%2])

    for q in query:
        if q[-1] == 0:
            if q[1] == "1":
                s.appendleft(q[2])
            else:
                s.append(q[2])
        else:
            if q[1] == "1":
                s.append(q[2])
            else:
                s.appendleft(q[2])

    if flag % 2 == 1:
        print("".join(list(s)[::-1]))
    else:
        print("".join(s))

main()

Buggy python submission (verdict: Time Limit Exceeded):
s = input()
q = int(input())

flag = 1
ctn = 0
for _ in range(q):
    query = input().split()
    if flag:
        if query[0] == "1":
            flag = 0
            ctn += 1
        else:
            if query[1] == "1":
                s = query[2] + s
            elif query[1] == "2":
                s = s + query[2]
    else:
        if query[0] == "1":
            flag = 1
            ctn += 1
        else:
            if query[1] == "1":
                s = s + query[2]
            elif query[1] == "2":
                s = query[2] + s

if ctn % 2 == 1:
    print(s[::-1])
else:
    print(s)

Fixed submission:
from collections import deque

def main():

    s = deque(input())
    q = int(input())

    flag = 0
    query = []

    for _ in range(q):
        temp = input().split()
        if len(temp) == 1:
            flag += 1
        else:
            query.append(temp + [flag%2])

    for q in query:
        if q[-1] == 0:
            if q[1] == "1":
                s.appendleft(q[2])
            else:
                s.append(q[2])
        else:
            if q[1] == "1":
                s.append(q[2])
            else:
                s.appendleft(q[2])

    if flag % 2 == 1:
        print("".join(list(s)[::-1]))
    else:
        print("".join(s))

main()

Buggy python submission (verdict: Time Limit Exceeded):
s = input()
q = int(input())

flag = 1
ctn = 0
for _ in range(q):
    query = input().split()
    if flag:
        if query[0] == "1":
            flag = 0
            ctn += 1
        else:
            if query[1] == "1":
                s = query[2] + s
            elif query[1] == "2":
                s = s + query[2]
    else:
        if query[0] == "1":
            flag = 1
            ctn += 1
        else:
            if query[1] == "1":
                s = s + query[2]
            elif query[1] == "2":
                s = query[2] + s

if ctn % 2 == 1:
    print(s[::-1])
else:
    print(s)

Fixed submission:
from collections import deque

def main():

    s = deque(input())
    q = int(input())

    flag = 0
    query = []

    for _ in range(q):
        temp = input().split()
        if len(temp) == 1:
            flag += 1
        else:
            query.append(temp + [flag%2])

    for q in query:
        if q[-1] == 0:
            if q[1] == "1":
                s.appendleft(q[2])
            else:
                s.append(q[2])
        else:
            if q[1] == "1":
                s.append(q[2])
            else:
                s.appendleft(q[2])

    if flag % 2 == 1:
        print("".join(list(s)[::-1]))
    else:
        print("".join(s))

main()

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

    s = input()
    q = int(input())

    query = []
    ctn = 0
    flag = 0

    for _ in range(q):
        temp = input().split()
        if len(temp) == 3:
            if flag and ctn % 2 == 1:
                query.append(["1"])
                query.append(temp)
                ctn = 0
            else:
                query.append(temp)
        else:
            if flag == 1:
                ctn += 1
            else:
                flag = 1
                ctn += 1
                
    if ctn % 2 == 1:
        query.append(["1"])

    for q in query:
        if q[0] == "1":
            s = s[::-1]
        else:
            if q[1] == "1":
                s = q[2] + s
            else:
                s = s + q[2]

    print(s)

main()

Fixed submission:
from collections import deque

def main():

    s = deque(input())
    q = int(input())

    flag = 0
    query = []

    for _ in range(q):
        temp = input().split()
        if len(temp) == 1:
            flag += 1
        else:
            query.append(temp + [flag%2])

    for q in query:
        if q[-1] == 0:
            if q[1] == "1":
                s.appendleft(q[2])
            else:
                s.append(q[2])
        else:
            if q[1] == "1":
                s.append(q[2])
            else:
                s.appendleft(q[2])

    if flag % 2 == 1:
        print("".join(list(s)[::-1]))
    else:
        print("".join(s))

main()

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

    s = input()
    q = int(input())

    query = []
    ctn = 0
    flag = 0
    for _ in range(q):
        temp = input().split()
        if len(temp) == 3 and flag:
            if ctn % 2 == 1:
                query.append(["1"])
            query.append(temp)
            ctn = 0
        elif len(temp) == 3:
            query.append(temp)
        else:
            if flag == 1:
                ctn += 1
            else:
                flag = 1
                ctn += 1
    if flag and ctn % 2 == 1:
        query.append(["1"])

    for q in query:
        if q[0] == "1":
            s = s[::-1]
        else:
            if q[1] == "1":
                s = q[2] + s
            else:
                s = s + q[2]

    print(s)

main()

Fixed submission:
from collections import deque

def main():

    s = deque(input())
    q = int(input())

    flag = 0
    query = []

    for _ in range(q):
        temp = input().split()
        if len(temp) == 1:
            flag += 1
        else:
            query.append(temp + [flag%2])

    for q in query:
        if q[-1] == 0:
            if q[1] == "1":
                s.appendleft(q[2])
            else:
                s.append(q[2])
        else:
            if q[1] == "1":
                s.append(q[2])
            else:
                s.appendleft(q[2])

    if flag % 2 == 1:
        print("".join(list(s)[::-1]))
    else:
        print("".join(s))

main()

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

    s = input()
    q = int(input())

    query = []
    ctn = 0
    flag = 0
    for _ in range(q):
        temp = input().split()
        if len(temp) == 3 and flag:
            if ctn % 2 == 1:
                query.append(["1"])
            query.append(temp)
            ctn = 0
        elif len(temp) == 3:
            query.append(temp)
        else:
            if flag == 1:
                ctn += 1
            else:
                flag = 1
                ctn += 1
    if flag and ctn % 2 == 1:
        query.append(["1"])

    for q in query:
        if q[0] == "1":
            s = s[::-1]
        else:
            if q[1] == "1":
                s = q[2] + s
            else:
                s = s + q[2]

    print(s)

main()

Fixed submission:
from collections import deque

def main():

    s = deque(input())
    q = int(input())

    flag = 0
    query = []

    for _ in range(q):
        temp = input().split()
        if len(temp) == 1:
            flag += 1
        else:
            query.append(temp + [flag%2])

    for q in query:
        if q[-1] == 0:
            if q[1] == "1":
                s.appendleft(q[2])
            else:
                s.append(q[2])
        else:
            if q[1] == "1":
                s.append(q[2])
            else:
                s.appendleft(q[2])

    if flag % 2 == 1:
        print("".join(list(s)[::-1]))
    else:
        print("".join(s))

main()

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

main()

Fixed submission:
from collections import deque

def main():

    s = deque(input())
    q = int(input())

    flag = 0
    query = []

    for _ in range(q):
        temp = input().split()
        if len(temp) == 1:
            flag += 1
        else:
            query.append(temp + [flag%2])

    for q in query:
        if q[-1] == 0:
            if q[1] == "1":
                s.appendleft(q[2])
            else:
                s.append(q[2])
        else:
            if q[1] == "1":
                s.append(q[2])
            else:
                s.appendleft(q[2])

    if flag % 2 == 1:
        print("".join(list(s)[::-1]))
    else:
        print("".join(s))

main()