Buggy python submission (verdict: Time Limit Exceeded):
#!/usr/bin/env python3
import sys

# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    S=next(tokens)
    Q=int(next(tokens))
    queries = []
    invert = False
    for i in range(Q):
        t = int(next(tokens))
        if t == 1:
            queries.append((t,))
        else:
            queries.append((t,int(next(tokens)),next(tokens)))
    for query in queries:
        if query[0] == 1:
            invert = not invert
        else:
            if query[1] == 1:
                if invert:
                    S = S+query[2]
                else:
                    S = query[2] + S
            else:
                if not invert:
                    S = S+query[2]
                else:
                    S = query[2] + S
    print(S if not invert else ''.join(list(reversed(S))))
    pass

if __name__ == '__main__':
    main()


Fixed submission:
#!/usr/bin/env python3
import sys

class Node:
    def __init__(self,v):
        self.value = v
        self.next = None
        self.prev = None
    
    def insertHead(self,v):
        self.prev = v
        v.next = self
        return v

    def appendBottom(self,v):
        self.next = v
        v.prev = self
        return v

    def printRecursively(self):
        current = self
        s = ""
        while True:
            if current == None:
                break
            s += current.value
            current = current.next
        print(s)

    def printRecursivelyInvert(self):
        current = self
        s = ""
        while True:
            if current == None:
                break
            s += current.value
            current = current.prev
        print(s)

# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    S=next(tokens)
    Q=int(next(tokens))
    queries = []
    invert = False
    for i in range(Q):
        t = int(next(tokens))
        if t == 1:
            queries.append((t,))
        else:
            queries.append((t,int(next(tokens)),next(tokens)))
    tail = Node(S[0])
    head = tail
    for i in range(1,len(S)):
        tail = tail.appendBottom(Node(S[i]))
    for query in queries:
        if query[0] == 1:
            invert = not invert
        else:
            if query[1] == 1:
                if invert:
                    tail = tail.appendBottom(Node(query[2]))
                else:
                    head = head.insertHead(Node(query[2]))
            else:
                if not invert:
                    tail = tail.appendBottom(Node(query[2]))
                else:
                    head = head.insertHead(Node(query[2]))
    if invert:
        tail.printRecursivelyInvert()
    else:
        head.printRecursively()
    pass

if __name__ == '__main__':
    main()