Buggy python submission (verdict: Time Limit Exceeded):
import sys

stdin = sys.stdin

ns = lambda : stdin.readline().rstrip()
ni = lambda : int(ns())
na = lambda : list(map(int, stdin.readline().split()))

def main():
    s = ns()
    Q = ni()

    T1 = 0
    FrontS = ""
    BackS = ""
    for _ in range(Q):
        T = stdin.readline().split()
        if T[0] == "1":
            T1 += 1
        else:
            if T1 % 2 == 0:
                s = FrontS + s + BackS
                FrontS = ""
                BackS = ""

                if T[1] == "1":
                    FrontS = T[2] + FrontS
                else:
                    BackS += T[2]
            else:
                s = FrontS + s + BackS
                FrontS = ""
                BackS = ""
                
                if T[1] == "1":
                    BackS = T[2] + BackS
                else:
                    FrontS = T[2] + FrontS
    
    if BackS != "":
        s += BackS
    if FrontS != "":
        s = FrontS + s

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

if __name__ == '__main__':
    main()


Fixed submission:
import sys

stdin = sys.stdin

ns = lambda : stdin.readline().rstrip()
ni = lambda : int(ns())
na = lambda : list(map(int, stdin.readline().split()))
sys.setrecursionlimit(10 ** 7)

def main():
    s = ns()
    Q = ni()

    T1 = 0
    FrontS = ""
    BackS = ""
    for _ in range(Q):
        T = stdin.readline().split()
        if T[0] == "1":
            FrontS, BackS = BackS, FrontS
            T1 += 1
        else:
            if T[1] == "1":
                FrontS += T[2]
            else:
                BackS += T[2]
                
    FrontS = FrontS[::-1]

    if T1 % 2 == 0:
        print(FrontS + s + BackS)
    else:
        print(FrontS + s[::-1] + BackS)

if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys

stdin = sys.stdin

ns = lambda : stdin.readline().rstrip()
ni = lambda : int(ns())
na = lambda : list(map(int, stdin.readline().split()))

def main():
    s = ns()
    Q = ni()

    T1 = 0
    FrontS = ""
    BackS = ""
    for _ in range(Q):
        T = stdin.readline().split()
        if T[0] == "1":
            T1 += 1
        else:
            if T1 % 2 == 0:
                s = FrontS + s + BackS
                FrontS = ""
                BackS = ""

                if T[1] == "1":
                    FrontS = T[2] + FrontS
                else:
                    BackS += T[2]
            else:
                s = FrontS + s + BackS
                FrontS = ""
                BackS = ""
                
                if T[1] == "1":
                    BackS = T[2] + BackS
                else:
                    FrontS = T[2] + FrontS
    
    if BackS != "":
        s += BackS
    if FrontS != "":
        s = FrontS + s

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

if __name__ == '__main__':
    main()


Fixed submission:
import sys

stdin = sys.stdin

ns = lambda : stdin.readline().rstrip()
ni = lambda : int(ns())
na = lambda : list(map(int, stdin.readline().split()))
sys.setrecursionlimit(10 ** 7)

def main():
    s = ns()
    Q = ni()

    T1 = 0
    FrontS = ""
    BackS = ""
    for _ in range(Q):
        T = stdin.readline().split()
        if T[0] == "1":
            FrontS, BackS = BackS, FrontS
            T1 += 1
        else:
            if T[1] == "1":
                FrontS += T[2]
            else:
                BackS += T[2]
                
    FrontS = FrontS[::-1]

    if T1 % 2 == 0:
        print(FrontS + s + BackS)
    else:
        print(FrontS + s[::-1] + BackS)

if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys

stdin = sys.stdin

ns = lambda : stdin.readline().rstrip()
ni = lambda : int(ns())
na = lambda : list(map(int, stdin.readline().split()))
sys.setrecursionlimit(10 ** 7)

def main():
    s = ns()
    Q = ni()

    T1 = 0
    FrontS = ""
    BackS = ""
    for _ in range(Q):
        T = stdin.readline().split()
        if T[0] == "1":
            T1 += 1
        else:
            if T1 % 2 == 0:
                # これは元通り
                
                s = FrontS + s + BackS
                FrontS = ""
                BackS = ""

                if T[1] == "1":
                    FrontS = T[2] + FrontS
                else:
                    BackS += T[2]
            else:
                # これは逆向き

                s = FrontS + s + BackS
                FrontS = ""
                BackS = ""
                
                if T[1] == "1":
                    BackS = T[2] + BackS
                else:
                    FrontS = T[2] + FrontS
    
    if BackS != "":
        s += BackS
    if FrontS != "":
        s = FrontS + s

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

if __name__ == '__main__':
    main()


Fixed submission:
import sys

stdin = sys.stdin

ns = lambda : stdin.readline().rstrip()
ni = lambda : int(ns())
na = lambda : list(map(int, stdin.readline().split()))
sys.setrecursionlimit(10 ** 7)

def main():
    s = ns()
    Q = ni()

    T1 = 0
    FrontS = ""
    BackS = ""
    for _ in range(Q):
        T = stdin.readline().split()
        if T[0] == "1":
            FrontS, BackS = BackS, FrontS
            T1 += 1
        else:
            if T[1] == "1":
                FrontS += T[2]
            else:
                BackS += T[2]
                
    FrontS = FrontS[::-1]

    if T1 % 2 == 0:
        print(FrontS + s + BackS)
    else:
        print(FrontS + s[::-1] + BackS)

if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys

stdin = sys.stdin

ns = lambda : stdin.readline().rstrip()
ni = lambda : int(ns())
na = lambda : list(map(int, stdin.readline().split()))
sys.setrecursionlimit(10 ** 7)

def main():
    s = ns()
    Q = ni()

    T1 = 0
    FrontS = ""
    BackS = ""
    for _ in range(Q):
        T = stdin.readline().split()
        if T[0] == "1":
            T1 += 1
        else:
            if T1 % 2 == 0:
                # これは元通り
                
                s = FrontS + s + BackS
                FrontS = ""
                BackS = ""

                if T[1] == "1":
                    FrontS = T[2] + FrontS
                else:
                    BackS += T[2]
            else:
                # これは逆向き

                s = FrontS + s + BackS
                FrontS = ""
                BackS = ""
                
                if T[1] == "1":
                    BackS = T[2] + BackS
                else:
                    FrontS = T[2] + FrontS
    
    if BackS != "":
        s += BackS
    if FrontS != "":
        s = FrontS + s

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

if __name__ == '__main__':
    main()

Fixed submission:
import sys

stdin = sys.stdin

ns = lambda : stdin.readline().rstrip()
ni = lambda : int(ns())
na = lambda : list(map(int, stdin.readline().split()))
sys.setrecursionlimit(10 ** 7)

def main():
    s = ns()
    Q = ni()

    T1 = 0
    FrontS = ""
    BackS = ""
    for _ in range(Q):
        T = stdin.readline().split()
        if T[0] == "1":
            FrontS, BackS = BackS, FrontS
            T1 += 1
        else:
            if T[1] == "1":
                FrontS += T[2]
            else:
                BackS += T[2]
                
    FrontS = FrontS[::-1]

    if T1 % 2 == 0:
        print(FrontS + s + BackS)
    else:
        print(FrontS + s[::-1] + BackS)

if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys

stdin = sys.stdin

ns = lambda : stdin.readline().rstrip()
ni = lambda : int(ns())
na = lambda : list(map(int, stdin.readline().split()))
sys.setrecursionlimit(10 ** 7)

def main():
    s = ns()
    Q = ni()

    for _ in range(Q):
        T = stdin.readline().split()
        if T[0] == "1":
            s = s[::-1]
        else:
            if T[1] == "1":
                s = T[2] + s
            else:
                s += T[2]
    print(s)

if __name__ == '__main__':
    main()

Fixed submission:
import sys

stdin = sys.stdin

ns = lambda : stdin.readline().rstrip()
ni = lambda : int(ns())
na = lambda : list(map(int, stdin.readline().split()))
sys.setrecursionlimit(10 ** 7)

def main():
    s = ns()
    Q = ni()

    T1 = 0
    FrontS = ""
    BackS = ""
    for _ in range(Q):
        T = stdin.readline().split()
        if T[0] == "1":
            FrontS, BackS = BackS, FrontS
            T1 += 1
        else:
            if T[1] == "1":
                FrontS += T[2]
            else:
                BackS += T[2]
                
    FrontS = FrontS[::-1]

    if T1 % 2 == 0:
        print(FrontS + s + BackS)
    else:
        print(FrontS + s[::-1] + BackS)

if __name__ == '__main__':
    main()