Problem: Takahashi has a string S consisting of lowercase English letters.  Starting with this string, he will produce a new one in the procedure given as follows.  The procedure consists of Q operations. In Operation i (1 \leq i \leq Q), an integer T_i is provided, which means the following:    * If T_i = 1: reverse the string S.    * If T_i = 2: An integer F_i and a lowercase English letter C_i are additionally provided.      * If F_i = 1 : Add C_i to the beginning of the string S.     * If F_i = 2 : Add C_i to the end of the string S.  Help Takahashi by finding the final string that results from the procedure.

Submission attempts (in order):
# -*- coding:utf-8 -*-
from collections import deque

def solve():
    d = deque(input())
    Q = int(input())

    is_reverse = False 

    for _ in range(Q):
        query = input()
        if query == "1":
            is_reverse = not is_reverse
        else:
            _, f, c = query.split()

            if f == "1":
                if is_reverse:
                    
                    d.append(c)
                else:
                    
                    d.appendleft(c)
            else:
                if is_reverse:
                    
                    d.appendleft(c)
                else:
                    
                    d.append(c)

    if is_reverse:
        print("".join(reversed(list(d))))
    else:
        print("".join(d))


def solve2():
    S = list(input())
    Q = int(input())
    M = 2*10**5+10
    data = [None]*M
    data.extend(S)
    data.extend([None]*M)
    left, right = M-1, M+len(S)

    is_reverse = False

    for _ in range(Q):
        query = input()
        if query == "1":
            is_reverse = not is_reverse
        else:
            t, f, c = query.split()
            if f == "1":
                if is_reverse:
                    
                    data[right] = c
                    right += 1
                else:
                    
                    data[left] = c
                    left -= 1
            else:
                if is_reverse:
                    
                    data[left] = c
                    left -= 1
                else:
                    
                    data[right] = c
                    right += 1

    
    if is_reverse:
        print("".join(data[right-1:left:-1])
        
        #     print(data[i], end="")
    else:
        
        #     print(data[i], end="")
        print("".join(data[left+1:right]))
    # print()


if __name__ == "__main__":
    solve()


# -*- coding:utf-8 -*-
from collections import deque

def solve():
    d = deque(input())
    Q = int(input())

    is_reverse = False 

    for _ in range(Q):
        query = input()
        if query == "1":
            is_reverse = not is_reverse
        else:
            _, f, c = query.split()

            if f == "1":
                if is_reverse:
                    
                    d.append(c)
                else:
                    
                    d.appendleft(c)
            else:
                if is_reverse:
                    
                    d.appendleft(c)
                else:
                    
                    d.append(c)

    if is_reverse:
        print("".join(reversed(list(d))))
    else:
        print("".join(d))


def solve2():
    S = list(input())
    Q = int(input())
    M = 2*10**5+10
    data = [None]*M
    data.extend(S)
    data.extend([None]*M)
    left, right = M-1, M+len(S)

    is_reverse = False

    for _ in range(Q):
        query = input()
        if query == "1":
            is_reverse = not is_reverse
        else:
            t, f, c = query.split()
            if f == "1":
                if is_reverse:
                    
                    data[right] = c
                    right += 1
                else:
                    
                    data[left] = c
                    left -= 1
            else:
                if is_reverse:
                    
                    data[left] = c
                    left -= 1
                else:
                    
                    data[right] = c
                    right += 1

    
    if is_reverse:
        print("".join(data[right-1:left:-1]))
        
        #     print(data[i], end="")
    else:
        
        #     print(data[i], end="")
        print("".join(data[left+1:right]))
    # print()


if __name__ == "__main__":
    solve2()


Verdicts: Runtime Error

Accepted