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

    for _ in range(q):
        try:
            t, f, a = input().split()
            if f == '1':
                st = a + st
            else:
                st = st + a
        except:
            st = st[::-1]

    print(st)

main()

Fixed submission:
def string():
    s = input()
    q = int(input())
    l = ''
    r = ''
    re = False
    for _ in range(q):
        t = input().split()
        if t[0] == '1':
            re = not re
        else:
            if re:
                if t[1] == '1':
                    r = r + t[2]
                else:
                    l = t[2] + l
            else:
                if t[1] == '1':
                    l = t[2] + l
                else:
                    r = r + t[2]
    s = l + s + r
    if re:
        s = s[::-1]
    print(s)
string()

Buggy python submission (verdict: Time Limit Exceeded):
def string():
    s = input()
    q = int(input())
    while q > 0:
        t = list(input().strip().split())
        if int(t[0]) == 1:
            s = s[::-1]
        else:
            if int(t[1]) == 2:
                s += t[2]
            else:
                s = t[2] + s
        q -= 1
    print(s)
string()

Fixed submission:
def string():
    s = input()
    q = int(input())
    l = ''
    r = ''
    re = False
    for _ in range(q):
        t = input().split()
        if t[0] == '1':
            re = not re
        else:
            if re:
                if t[1] == '1':
                    r = r + t[2]
                else:
                    l = t[2] + l
            else:
                if t[1] == '1':
                    l = t[2] + l
                else:
                    r = r + t[2]
    s = l + s + r
    if re:
        s = s[::-1]
    print(s)
string()

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

for _ in range(Q):
    q = input().split()
    if q[0] == '1':
        S.reverse()
    elif q[1] == '1':
        S.insert(0, q[2])
    else:
        S.append(q[2])

print(''.join(S))


Fixed submission:
def string():
    s = input()
    q = int(input())
    l = ''
    r = ''
    re = False
    for _ in range(q):
        t = input().split()
        if t[0] == '1':
            re = not re
        else:
            if re:
                if t[1] == '1':
                    r = r + t[2]
                else:
                    l = t[2] + l
            else:
                if t[1] == '1':
                    l = t[2] + l
                else:
                    r = r + t[2]
    s = l + s + r
    if re:
        s = s[::-1]
    print(s)
string()

Buggy python submission (verdict: Time Limit Exceeded):
S=input()
Q=int(input())
case=0
for i in range(Q):
    temp=input().split()
    if len(temp)==1:
        case=1-case	
    else:
        if int(temp[1])==1:
            if case==0:
                S=temp[2]+S
            if case==1:
                S=S+temp[2]

        if int(temp[1])==2:
            if case==1:
                S=temp[2]+S
            if case==0:
                S=S+temp[2]
if case==1:
    S=S[::-1]

print(S)


Fixed submission:
def string():
    s = input()
    q = int(input())
    l = ''
    r = ''
    re = False
    for _ in range(q):
        t = input().split()
        if t[0] == '1':
            re = not re
        else:
            if re:
                if t[1] == '1':
                    r = r + t[2]
                else:
                    l = t[2] + l
            else:
                if t[1] == '1':
                    l = t[2] + l
                else:
                    r = r + t[2]
    s = l + s + r
    if re:
        s = s[::-1]
    print(s)
string()

Buggy python submission (verdict: Time Limit Exceeded):
S=input()
Q=int(input())
for i in range(Q):
    temp=input().split()
    if len(temp)==1:
        S=S[::-1]
    else:
        if int(temp[1])==1:
            S=temp[2]+S

        if int(temp[1])==2:
            S=S+temp[2]
print(S)

Fixed submission:
def string():
    s = input()
    q = int(input())
    l = ''
    r = ''
    re = False
    for _ in range(q):
        t = input().split()
        if t[0] == '1':
            re = not re
        else:
            if re:
                if t[1] == '1':
                    r = r + t[2]
                else:
                    l = t[2] + l
            else:
                if t[1] == '1':
                    l = t[2] + l
                else:
                    r = r + t[2]
    s = l + s + r
    if re:
        s = s[::-1]
    print(s)
string()