Buggy python submission (verdict: Time Limit Exceeded):
s = input()
nq = int(input())
flg = 0
front = []
back = []
for i in range(nq):
    q = input()
    if len(q) == 1:
        #flg += 1
        flg = (flg+1)%2
    else:
        if flg == 0:
            if q[2] == '1':
                front = [q[-1]] + front
            else:
                back = back + [q[-1]]
        else:
            if q[2] == '1':
                back = back + [q[-1]]
            else:
                front = [q[-1]] + front
                
s = ''.join(front) + s + ''.join(back)
if flg % 2 == 1:
    s = s[::-1]
print(s)

Fixed submission:
from sys import stdin
 
def main():
    S = list(stdin.readline().rstrip())
    Q = int(input())
    rev = 0
    head = []
    tail = []
 
    for _ in range(Q):
        q = stdin.readline()
        if q[0] == "1":
            rev = (rev+1)%2
        else:
            if q[2] == "1":
                if rev == 0:
                    head.append(q[4])
                else:
                    tail.append(q[4])
            else:
                if rev ==0:
                    tail.append(q[4])
                else:
                    head.append(q[4])
 
    if rev == 0:
        ans = "".join(head[::-1] + S + tail)
    else:
        ans = "".join(tail[::-1] + S[::-1] + head)
    
    print(ans)
    
main()

Buggy python submission (verdict: Time Limit Exceeded):
s = input()
nq = int(input())
flg = 0
front = []
back = []
for i in range(nq):
    q = input()
    if len(q) == 1:
        flg += 1
    else:
        if flg % 2 == 0:
            if q[2] == '1':
                front = [q[-1]] + front
            else:
                back = back + [q[-1]]
        else:
            if q[2] == '1':
                back = back + [q[-1]]
            else:
                front = [q[-1]] + front
                
s = ''.join(front) + s + ''.join(back)
if flg % 2 == 1:
    s = s[::-1]
print(s)

Fixed submission:
from sys import stdin
 
def main():
    S = list(stdin.readline().rstrip())
    Q = int(input())
    rev = 0
    head = []
    tail = []
 
    for _ in range(Q):
        q = stdin.readline()
        if q[0] == "1":
            rev = (rev+1)%2
        else:
            if q[2] == "1":
                if rev == 0:
                    head.append(q[4])
                else:
                    tail.append(q[4])
            else:
                if rev ==0:
                    tail.append(q[4])
                else:
                    head.append(q[4])
 
    if rev == 0:
        ans = "".join(head[::-1] + S + tail)
    else:
        ans = "".join(tail[::-1] + S[::-1] + head)
    
    print(ans)
    
main()

Buggy python submission (verdict: Time Limit Exceeded):
def try_int(s):
    try:
        return int(s)
    except:
        return s

s = input()
nq = int(input())
flg = 0
for i in range(nq):
    q = input()
    if len(q) == 1:
        flg += 1
    else:
        if flg % 2 == 0:
            if q[2] == '1':
                s = q[-1]+s
            else:
                s = s+q[-1]
        else:
            if q[2] == '1':
                s = s+q[-1]
            else:
                s = q[-1]+s
if flg % 2 == 1:
    s = s[::-1]
print(s)

Fixed submission:
from sys import stdin
 
def main():
    S = list(stdin.readline().rstrip())
    Q = int(input())
    rev = 0
    head = []
    tail = []
 
    for _ in range(Q):
        q = stdin.readline()
        if q[0] == "1":
            rev = (rev+1)%2
        else:
            if q[2] == "1":
                if rev == 0:
                    head.append(q[4])
                else:
                    tail.append(q[4])
            else:
                if rev ==0:
                    tail.append(q[4])
                else:
                    head.append(q[4])
 
    if rev == 0:
        ans = "".join(head[::-1] + S + tail)
    else:
        ans = "".join(tail[::-1] + S[::-1] + head)
    
    print(ans)
    
main()

Buggy python submission (verdict: Time Limit Exceeded):
def try_int(s):
    try:
        return int(s)
    except:
        return s

s = input()
nq = int(input())
flg = 0
for i in range(nq):
    q = [try_int(p) for p in input().split()]

    if len(q) == 1:
        flg += 1
    else:
        if flg % 2 == 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 flg % 2 == 1:
    s = s[::-1]
print(s)

Fixed submission:
from sys import stdin
 
def main():
    S = list(stdin.readline().rstrip())
    Q = int(input())
    rev = 0
    head = []
    tail = []
 
    for _ in range(Q):
        q = stdin.readline()
        if q[0] == "1":
            rev = (rev+1)%2
        else:
            if q[2] == "1":
                if rev == 0:
                    head.append(q[4])
                else:
                    tail.append(q[4])
            else:
                if rev ==0:
                    tail.append(q[4])
                else:
                    head.append(q[4])
 
    if rev == 0:
        ans = "".join(head[::-1] + S + tail)
    else:
        ans = "".join(tail[::-1] + S[::-1] + head)
    
    print(ans)
    
main()

Buggy python submission (verdict: Time Limit Exceeded):
def try_int(s):
    try:
        return int(s)
    except:
        return s

s = input()
nq = int(input())
flg = 0
for i in range(nq):
    q = [try_int(p) for p in input().split()]

    if len(q) == 1:
        flg += 1
    else:
        if flg % 2 == 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 flg % 2 == 1:
    s = s[::-1]
print(s)

Fixed submission:
from sys import stdin
 
def main():
    S = list(stdin.readline().rstrip())
    Q = int(input())
    rev = 0
    head = []
    tail = []
 
    for _ in range(Q):
        q = stdin.readline()
        if q[0] == "1":
            rev = (rev+1)%2
        else:
            if q[2] == "1":
                if rev == 0:
                    head.append(q[4])
                else:
                    tail.append(q[4])
            else:
                if rev ==0:
                    tail.append(q[4])
                else:
                    head.append(q[4])
 
    if rev == 0:
        ans = "".join(head[::-1] + S + tail)
    else:
        ans = "".join(tail[::-1] + S[::-1] + head)
    
    print(ans)
    
main()

Buggy python submission (verdict: Time Limit Exceeded):
def try_int(s):
    try:
        return int(s)
    except:
        return s

s = input()
nq = int(input())
query = []
for i in range(nq):
    query.append([try_int(i) for i in input().split()])
    
for q in query:
    if len(q) == 1:
        s = s[::-1]
    else:
        if q[1] == 1:
            s = q[2]+s
        else:
            s = s+q[2]
print(s)

Fixed submission:
from sys import stdin
 
def main():
    S = list(stdin.readline().rstrip())
    Q = int(input())
    rev = 0
    head = []
    tail = []
 
    for _ in range(Q):
        q = stdin.readline()
        if q[0] == "1":
            rev = (rev+1)%2
        else:
            if q[2] == "1":
                if rev == 0:
                    head.append(q[4])
                else:
                    tail.append(q[4])
            else:
                if rev ==0:
                    tail.append(q[4])
                else:
                    head.append(q[4])
 
    if rev == 0:
        ans = "".join(head[::-1] + S + tail)
    else:
        ans = "".join(tail[::-1] + S[::-1] + head)
    
    print(ans)
    
main()