Problem:
 Score : 400 points    Problem Statement 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.      Constraints  1 \leq |S| \leq 10^5  S consists of lowercase English letters.  1 \leq Q \leq 2 \times 10^5  T_i = 1 or 2 .  F_i = 1 or 2 , if provided.  C_i is a lowercase English letter, if provided.         Input Input is given from Standard Input in the following format:  S  Q  Query_1  :  Query_Q   In the 3 -rd through the (Q+2) -th lines, Query_i is one of the following:  1   which means T_i = 1 , and:  2  F_i  C_i   which means T_i = 2 .      Output Print the resulting string.        Sample Input 1 a
4
2 1 p
1
2 2 c
1      Sample Output 1 cpa  There will be Q = 4 operations. Initially, S is a .    Operation 1 : Add p at the beginning of S . S becomes pa .    Operation 2 : Reverse S . S becomes ap .    Operation 3 : Add c at the end of S . S becomes apc .    Operation 4 : Reverse S . S becomes cpa .    Thus, the resulting string is cpa .       Sample Input 2 a
6
2 2 a
2 1 b
1
2 2 c
1
1      Sample Output 2 aabc  There will be Q = 6 operations. Initially, S is a .    Operation 1 : S becomes aa .    Operation 2 : S becomes baa .    Operation 3 : S becomes aab .    Operation 4 : S becomes aabc .    Operation 5 : S becomes cbaa .    Operation 6 : S becomes aabc .    Thus, the resulting string is aabc .       Sample Input 3 y
1
2 1 x      Sample Output 3 xy

Solution (Python):
from collections import deque

S = input()
Q = int(input())
Queries = [input().split() for i in range(Q)]

def start(S):
    state = {
        "S": S,
        "is_reversed": False,
        "left": deque(),
        "right": deque(),
    }

    return state


def T1(state, Query):
    state['is_reversed'] = not state['is_reversed']


def T2(state, Query):
    F = Query[1]
    C = Query[2]

    if not state['is_reversed']:
        if F == '1':
            state['left'].appendleft(C)
        else:
            state['right'].append(C)
    else:
        if F == '1':
            state['right'].append(C)
        else:
            state['left'].appendleft(C)


def output(state):
    output = ''.join(state['left']) + state['S'] + ''.join(state['right'])
    if state['is_reversed']:
        output = ''.join(list(reversed(output)))

    return output

def fetch(Query):
    operations = {
        '1': T1,
        '2': T2,
    }

    index = Query[0]
    operation = operations[index]

    return operation

def operate(state, Query):
    T = fetch(Query)
    T(state, Query)


def execute(state, Queries):
    for Query in Queries:
        operate(state, Query)

    return state


state = start(S)
execute(state, Queries)

answer = output(state)

print(answer)

Solution (Python):
from collections import deque

S = input()
Q = int(input())
Queries = [input().split() for i in range(Q)]

def start(S):
    state = {
        "S": S,
        "is_reversed": False,
        "left": deque(),
        "right": deque(),
    }

    return state


def T1(state, Query):
    state['is_reversed'] = not state['is_reversed']


def T2(state, Query):
    F = Query[1]
    C = Query[2]

    if not state['is_reversed']:
        if F == '1':
            state['left'].appendleft(C)
        else:
            state['right'].append(C)
    else:
        if F == '1':
            state['right'].append(C)
        else:
            state['left'].appendleft(C)


def output(state):
    output = ''.join(state['left']) + state['S'] + ''.join(state['right'])
    if state['is_reversed']:
        output = ''.join(list(reversed(output)))

    return output

def fetch(Query):
    operations = {
        '1': T1,
        '2': T2,
    }

    index = Query[0]
    operation = operations[index]

    return operation

def operate(state, Query):
    T = fetch(Query)
    T(state, Query)


state = start(S)
for Query in Queries:
    operate(state, Query)

answer = output(state)

print(answer)

Solution (Python):
from collections import deque

S = input()
Q = int(input())
Queries = [input().split() for i in range(Q)]

state = {
    "S": S,
    "is_reversed": False,
    "left": deque(),
    "right": deque(),
}

def T1(state):
    state['is_reversed'] = not state['is_reversed']

def T2(state, F, C):
    if not state['is_reversed']:
        if F == '1':
            state['left'].appendleft(C)
        else:
            state['right'].append(C)
    else:
        if F == '1':
            state['right'].append(C)
        else:
            state['left'].appendleft(C)

def output(state):
    output = ''.join(state['left']) + state['S'] + ''.join(state['right'])
    if state['is_reversed']:
        output = ''.join(list(reversed(output)))

    return output


for Query in Queries:
    if Query[0] == '1':
        T1(state)
    else:
        T2(state, Query[1], Query[2])

answer = output(state)

print(answer)

Solution (Python):
from collections import deque

s=input()
q=int(input())
d=deque()
d.append(s)
m=1
for i in range (q):
    a=list(input().split())
    if a[0]=="1":
        m=m*(-1)
    else:
        if m==1:
            if a[1]=="1":
                d.appendleft(a[2])
            else:
                d.append(a[2])
        else:
            if a[1]=="2":
                d.appendleft(a[2])
            else:
                d.append(a[2])
s="".join(d)


print(s[::m])

Solution (Python):
import sys
def input(): return sys.stdin.readline().rstrip()

from collections import deque

def main():
  S = deque(input())
  N = int(input())

  d = True
  for i in range(N):
    list = input().split()
    if list[0] == '1':
      d = not d
      continue
    if list[1] == '1':
      S.appendleft(list[2]) if d else S.append(list[2])
      continue
    S.append(list[2]) if d else S.appendleft(list[2])
  return ''.join(S) if d else ''.join(S)[::-1]

if __name__ == '__main__':
  print(main())

Solution (Python):
from collections import deque

def main():
    ans = deque(input())
    n = int(input())
    vec = 1
    for i in range(n):
        q = input()
        if q == "1":
            vec *= -1
        else:
            _,o,s = q.split()
            if ((o == "1") and (vec == 1)) or ((o == "2") and (vec == -1)):
                ans.appendleft(s)
            else:
                ans.append(s)
    if vec == -1:
        ans.reverse()
    ans = ''.join(ans)
    print(ans)

if __name__ == '__main__':
    main()

Solution (Python):
from collections import deque

d = deque(input())
Q = int(input())

qs = [list(map(str, input().split())) for _ in range(Q)]

vector = True
for q in qs:
    if q[0] == "1":
        vector = not vector
    else:
        if q[1] == "1":
            if vector:
                d.appendleft(q[2])
            else:
                 d.append(q[2])
        else:
            if vector:
                d.append(q[2])
            else:
                 d.appendleft(q[2])

if not vector:
    d.reverse()

ans = ''.join(d)
print(ans)

Solution (Python):
from collections import deque
import copy
s = input()
q_ = int(input())
query = [input().split() for _ in range(q_)]
l = deque(s)
flag = 0

for q in query:
    if q[0] == '1':
        flag += 1
        continue
    elif q[0] == '2':
        if (int(q[1]) + flag) % 2 == 1:
            l.appendleft(q[2])
        else:
            l.append(q[2])

if flag % 2 == 1:
    l.reverse()

print(''.join(l))

Solution (Python):
S = str(input())
Q = int(input())

qs = [list(map(str, input().split())) for _ in range(Q)]

x, y = S, ""
for ls in qs:
    if ls[0] == "1":
        x, y = y, x
    else:
        if ls[1] == "1":
            y += ls[2]
        else:
            x += ls[2]
            
y = ''.join(list(reversed(y)))
print(y+x)

Solution (Python):
def main():
  s = input()
  q = int(input())
  de = collections.deque(s)
  rev = True
  for _ in range(q):
    query = input().split()
    if len(query) == 1:
      rev = False if rev else True
      continue
    typ, f, c = query
    if rev:
      if f == '1':
        de.appendleft(c)
      if f == '2':
        de.append(c)
    else:
      if f == '1':
        de.append(c)
      if f == '2':
        de.appendleft(c)
  ans = list(de)
  if rev is False:
    ans.reverse()
  print(''.join(ans))
      

import math
import fractions
import itertools
import collections
main()

Solution (Python):
S=input()

def split(word): 
    return [char for char in word]  
def convert(s): 
  
    
    new = "" 
  
    
    for x in s: 
        new += x  
  
    
    return new 
      
      

word = S
splits=split(S)
Q=int(input())
flipcounter=0
prestring=[]
poststring=[]
for i in range(0,Q):
  inputforround=input()
  if inputforround=='1':
    flipcounter=flipcounter+1
  else:
    T,F,C=map(str,inputforround.split())
    if (int(F)+flipcounter)%2==1:
      prestring.append(C)
    else:
      poststring.append(C)

prestring.reverse()
for j in splits:
  prestring.append(j)
for k in poststring:
  prestring.append(k)
if flipcounter%2==1:
  prestring.reverse()
ans=convert(prestring)
print(ans)

Solution (Python):
from collections import deque
import sys
input=sys.stdin.readline
S=input().splitlines()[0]
Q = int(input())
qs = [input().split() for _ in range(Q)]
flipcnt = 0
S =deque(S)
for q in qs:
    if len(q) == 1:
        flipcnt += 1
        continue
    else:
        if (int(q[1]) + flipcnt) % 2 == 1:
            
            S.appendleft(q[2])
        else:
            S.append(q[2])
    

if flipcnt % 2 == 1:
    S.reverse()

print(''.join(S))

Solution (Python):
from collections import deque
import sys

def main():
    s = sys.stdin.readline().strip()
    qn = int(sys.stdin.readline().strip())
    qs = [input().split() for _ in range(qn)]

    s = deque(s)
    r = 1
    for q in qs:
        if len(q) == 1:
            r = r * -1

        else:
            if q[1] == '1':
                if r == 1:
                    s.appendleft(q[2])
                else:
                    s.append(q[2])
            else:
                if r == 1:
                    s.append(q[2])
                else:
                    s.appendleft(q[2])

    if r == 1:
        print(''.join(s))
    else:
        s.reverse()
        print(''.join(s))

if __name__ == "__main__":
    main()

Solution (Python):
from collections import deque

S = input()
Q = int(input())

deque = deque([c for c in S])
flag = 0

for _ in range(Q):
  pack = input().split()

  T = int(pack[0])
  if T == 1:
    flag = flag ^ 1

  if T == 2:
    F, C = int(pack[1]), pack[2]
    if F == 1:
      if flag:
        deque.append(C)
      else:
        
        deque.appendleft(C)
    if F == 2:
      if flag:
        deque.appendleft(C)
      else:
        
        deque.append(C)

print("".join(reversed(deque) if flag else deque))

Solution (Python):
from collections import deque

S = deque(list(input()))
Q = int(input())
reverse = True
for i in range(Q):
    query = input().split()
    if query[0] == '1':
        reverse = not reverse
    else:
        right = query[1] == '1'
        if right == reverse:
            S.appendleft(query[2])
        else:
            S.append(query[2])
if not reverse:
    print(''.join(list(reversed(S))))
else:
    print(''.join(list(S)))

Solution (Python):
from collections import deque
s = deque(input())
q = int(input())
flag = True
for i in range(q):
    x = input()
    if x == '1':
        flag = not flag
    else :
        if x[2] == '1' and flag :
            s.appendleft(x[4])
        elif x[2] == '2' and not flag :
            s.appendleft(x[4])
        else :
            s.append(x[4])
if flag:
    for i in s :
        print(i, end='')
else :
    for i in range(1, len(s)+1):
        print(s[-i], end='')
print('')

Solution (Python):
from collections import deque


reverse = False

string = deque(input().rstrip())
operate_times = int(input().rstrip())

for i in range(operate_times):
    row = input().rstrip()
    if row[0]=='1':
        if reverse:
            reverse = False
        else:
            reverse = True
    
    if row[0]=='2':
        row = row.split()
        head_or_tail = int(row[1])
        add_string = row[2]

        if reverse:
            if head_or_tail==1:
                string.append(add_string)
            else:
                string.appendleft(add_string)
        
        else:
            if head_or_tail==2:
                string.append(add_string)
            else:
                string.appendleft(add_string)


answer_string = ''
if reverse:
    for _ in range(len(string)):
        right = string.pop()
        answer_string+=right
    print(answer_string)
else:
    print(''.join(string))

Solution (Python):
from collections import deque

def main():
    S = input()
    Q = int(input())

    S = deque(list(S))
    rev = 1  
    for _ in range(Q):
        input_line = input().split()
        if len(input_line) == 1:
            rev *= -1
        else:
            _, f, c = input_line
            if f == '1':
                if rev == 1:
                    S.appendleft(c)
                else:
                    S.append(c)
            else:
                if rev == 1:
                    S.append(c)
                else:
                    S.appendleft(c)

    if rev == 1:
        print(''.join(S))
    else:
        print(''.join(list(S)[::-1]))


if __name__ == '__main__':
    main()

Solution (Python):
from collections import deque

S = input()
Q = int(input())
head = 1
d = deque([S])

for _ in range(Q):
    q = input().split()
    if q[0]=='1':
        head = head*-1
    else:
        if q[1]=='1':
            h = head*1
        else:
            h = head*-1
        
        if h==1:
            d.appendleft(q[2])
        else:
            d.append(q[2])
ans = "".join(list(d))
if head == -1:
    ans = ans[::-1]

print(ans)

Solution (Python):
import collections
S = list(input())
Q = int(input())

ans = collections.deque(S)
rev = False

for _ in range(Q):
    query = input().split()
    if query[0] == '1':
        rev = not rev
    else:
        if query[1] == '1':
            if rev:
                ans.append(query[2])
            else:
                ans.appendleft(query[2])
        else:
            if rev:
                ans.appendleft(query[2])
            else:
                ans.append(query[2])

if rev:
    print("".join(reversed(ans)))
else:
    print("".join(ans))

Solution (Python):
from collections import deque
d = deque(list(input()))
rev = False
for _ in range(int(input())):
    q = list(input().split(" "))
    t = int(q[0])
    if t == 1:
        if rev:
            rev = False
        else:
            rev = True
    else:
        if int(q[1]) == 1:
            if rev:
                d.append(q[2])
            else:
                d.appendleft(q[2])
        elif int(q[1]) == 2:
            if rev:
                d.appendleft(q[2])
            else:
                d.append(q[2])
if rev:
    d.reverse()
for res in d:
    print(res, end="")

Solution (Python):


from collections import deque
S = deque(input())
Q = int(input())





hc = 0

for i in range(Q):
  
  qi = input().split(" ")
  if len(qi)==1:
    
    if hc==0:
      hc = 1
    else:
      hc = 0
  else:
    fi = int(qi[1])
    ci = qi[2]
    if hc ==0:
      if fi==1:
        S.appendleft(ci)
      else:
        S.append(ci)
    else:
      if fi==1:
        S.append(ci)
      else:
        S.appendleft(ci)

S = list(S)
if hc == 1:
  S = S[::-1]
print("".join(S))

Solution (Python):
from collections import deque
S = input()
Q = int(input())
quer = [input().split() for _ in range(Q)]

hanten = 0
t = 0
d = deque(S)
for i in quer:
    quary = i
    if quary[0]=='1':
        
        hanten = hanten ^ 1
    else:
        t = (int(quary[1])-1) ^ hanten
        if t:
            
            d.append(quary[2])
        else:
            
            d.appendleft(quary[2])
    
if hanten:
    d.reverse()
s = ''.join(list(d))
print(s)

Solution (Python):
s=input();q=int(input());query=[input() for i in range(q)]

flag = 1
front = ""
back = ""

for i in range(q):
    t = query[i]
    if t[0] == "1":
        flag *= -1
    else:
        dest = (1 if t[2] == "1" else -1) 
        tmp = t[4]
        if flag*dest == 1:
            front += tmp
        else:
            back += tmp
            
if flag == 1:
    s = front[::-1] + s + back
    print(s)
else:
    s = back[::-1] + s[::-1] + front
    print(s)

Solution (Python):
S= input()
q=int(input())
h=0
a=[]
for i in range(q):
    a.append(input())
t=""
w=""
for i in range(1,q+1):
    if a[q-i][0]=="2":
        if a[q - i][2] == "1":
            if h%2==0:
                t=a[q-i][4]+t
            else:
                w =  a[q-i][4]+w
        else:
            if h%2==0:
                w=a[q-i][4]+w
            else:
                t = a[q - i][4]+t
    else:
        h+=1
if h%2==0:
    print(t[::-1]+S+w)
else:
    print(t[::-1]+S[::-1]+w)

Solution (Python):
import numpy as np
import sys

input = sys.stdin.readline

S = input()
Q = int(input())

left = []
right = []
rev = False

for i in range(Q):
    l = input().split()
    if l[0] == '1':
        rev = not rev
    else:
        if (l[1] == '1' and not rev) or (l[1] == '2' and rev):
            left.append(l[2])
        else:
            right.append(l[2])

S = ''.join(left[::-1]) + S + ''.join(right)

if rev:
    print(S[::-1].replace('\n',''))
else:
    print(S.replace('\n',''))

Solution (Python):
S= input()
q=int(input())
h=0
a=[]
for i in range(q):
    a.append(input())
t=""
w=""
for i in range(1,q+1):
    if a[q-i][0]=="2":
        if a[q - i][2] == "1":
            if h%2==0:
                t=t+a[q-i][4]
            else:
                w =  w+a[q - i][4]
        else:
            if h%2==0:
                w=w+a[q-i][4]
            else:
                t = t + a[q - i][4]
    else:
        h+=1
if h%2==0:
    print(t+S+w[::-1])
else:
    print(t+S[::-1]+w[::-1])

Solution (Python):
s = list(input())
q = int(input())
left = []
right = []
rev = False
for i in range(q):
    x = list(input())
    if len(x) == 1:
        rev = not rev
        
    else:
        if (x[2] == "1" and not rev) or (x[2] == "2" and rev):
            left.append(x[4])
        else:
            right.append(x[4])
ans = "".join(left[::-1]) + "".join(s) + "".join(right)
if rev:
    print(ans[::-1])
else:
    print(ans)

Solution (Python):
import sys
input = sys.stdin.readline

def main():
    S = input()[:-1]
    Q = int(input())
    li = [[] for _ in range(2)]
    now = 0
    for _ in range(Q):
        x = input()
        if(x[0] == '2'):
            if(x[2]=='1'):
                li[1-now].append(x[4])
            else:
                li[now].append(x[4])
        else:
            now = 1-now

    s = ''.join(li[1-now][::-1]) + S[::-(now*2-1)] + ''.join(li[now])
    print(s)

if __name__ == '__main__':
    main()

Solution (Python):
S = input()
Q = int(input())
left = []
right = []
rev = False
for i in range(Q):
  l = input().split()
  if l[0] == '1':
    rev = not rev
  else:
    if (l[1] == '1' and not rev) or (l[1] == '2' and rev):
      left.append(l[2])
    else:
      right.append(l[2])
S = ''.join(left[::-1]) + S + ''.join(right)
if rev:
  print(S[::-1])
else:
  print(S)

Solution (Python):
from collections import deque
 
s = deque(input())
q = int(input())
 
direction = 0
for _ in range(q):
    a = input()
    if a[0]=='1':
        direction+=1
    else:
        t,f,c = a.split()
        if f=='1':
            if direction%2==0:
                s.appendleft(c)
            else:
                s.append(c)
        else:
            if direction%2==0:
                s.append(c)
            else:
                s.appendleft(c)
 
if direction%2==1:
    s.reverse()
print(''.join(s))

Solution (Python):
from collections import deque

s = deque(input())
q = int(input())

direction = 0
for _ in range(q):
    a = input()
    if a[0]=='1':
        direction+=1
    else:
        t,f,c = a.split()
        if f=='1':
            if direction%2==0:
                s.appendleft(c)
            else:
                s.append(c)
        else:
            if direction%2==0:
                s.append(c)
            else:
                s.appendleft(c)

if direction%2==1:
    s.reverse()
print(''.join(s))

Solution (Python):
S=str(input())
Q=int(input())

Query=[]
for i in range(Q):
    Query.append(list(input().split()))

N=len(S)//2
S_left_reverse=S[:N+1][::-1]
S_right=S[N+1:]



check=1
for j in range(Q):
    if len(Query[j])==1:
        check*=-1
        
    else:
        if Query[j][1]=='1':
            if check==1:
                S_left_reverse+=Query[j][2]
                
            else:
                S_right+=Query[j][2]
                
        else:
            if check==1:
                S_right+=Query[j][2]
                
            else:
                S_left_reverse+=Query[j][2]
                


ans=S_left_reverse[::-1]+S_right
if check==1:
    print(ans)
else:
    print(ans[::-1])

Solution (Python):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
from collections import deque
 
S = deque(readline().rstrip().decode())
Q = int(readline())
query = readlines()
 
rev = False
for q in query:
    if q[0] == ord('1'):
        rev = not rev
    else:
        _, f, c = q.decode().split()
        f = int(f)
        if rev ^ (f == 1):
            S.appendleft(c)
        else:
            S.append(c)
 
T = ''.join(S)
if rev:
    T = T[::-1]
print(T)

Solution (Python):
S=input()
Q=int(input())





rev=False
front=""
last=""


for _ in range(Q):
    query=input().split()
    if query[0]=="1":
        rev = not rev
        
    else:
        if rev:
            query[1]=str(3-int(query[1]))
        if query[1]=="1":
            
            front="".join([query[2],front])
        else:
            
            last="".join([last,query[2]])







if not rev:
    print(front+S+last)
else:
    print(last[::-1]+S[::-1]+front[::-1])

Solution (Python):
S=input()
Q=int(input())





rev=False
front=""
last=""


for _ in range(Q):
    query=input().split()
    if query[0]=="1":
        rev = not rev
        
    else:
        if rev:
            query[1]=str(3-int(query[1]))
        if query[1]=="1":
            
            front=query[2]+front
        else:
            
            last+=query[2]







if not rev:
    print(front+S+last)
else:
    print(last[::-1]+S[::-1]+front[::-1])

Solution (Python):

from scipy.sparse.csgraph import shortest_path, floyd_warshall, dijkstra, bellman_ford, johnson, minimum_spanning_tree
from scipy.sparse import csr_matrix, coo_matrix, lil_matrix
import numpy as np
from functools import reduce

from collections import deque, Counter, defaultdict
from itertools import product, permutations,combinations
from operator import itemgetter
from heapq import heappop, heappush
from bisect import bisect_left, bisect_right, bisect
from fractions import gcd
from math import ceil,floor, sqrt, cos, sin, pi, factorial
import statistics
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10**8)
INF = float('inf')
MOD = 10**9+7
def lcm(a, b):
  return a*b//gcd(a, b)

def main():
  S = input()
  Q = int(input())
  TFC = list(list(input().split()) for _ in range(Q))
  rev = 0
  q = deque(S)
  for i in range(Q):
    if TFC[i][0] == '1':
      rev ^= 1
    else:
      if TFC[i][1] == '1':
        if rev == 0:
          q.appendleft(TFC[i][2])
        else:
          q.append(TFC[i][2])
      else:
        if rev == 0:
          q.append(TFC[i][2])
        else:
          q.appendleft(TFC[i][2])
  ans = ''.join(q)
  print(ans if rev==0 else ans[::-1])
  
if __name__ == '__main__':
  main()

Solution (Python):
S = input()
Q = int(input())
Query = [input().split() for i in range(Q)]
countA,countB = 0,0
latter = []
former = []
for i in range(Q):
    if(Query[Q-i-1][0] == "1"):
        countA += 1
        countB += 1
    else:
        t = lambda x:1 if x=="1" else 0
        if((t(Query[Q-i-1][1])+countB)%2 == 1):
            former.append(Query[Q-i-1][2])
        else:
            latter.append(Query[Q-i-1][2])

for i in former:
    print(i,end="")
if(countA%2 == 0):
    print(S,end="")
else:
    for j in range(len(S)):
        print(S[len(S)-j-1],end="")
for i in range(len(latter)):
    print(latter[len(latter)-i-1],end="")

Solution (Python):
from collections import deque

s = deque(input())
q = int(input())
query = [input().split() for _ in range(q)]

reverse = False
for i in query:
    if len(i) == 1:
        if reverse:
            reverse = False
        else:
            reverse = True
    else:
        if reverse:
            if i[1] == '1':
                s.append(i[2])
            else:
                s.appendleft(i[2])
        else:
            if i[1] == '1':
                s.appendleft(i[2])
            else:
                s.append(i[2])
if reverse:
    s = reversed(s)
    s = ''.join(s)
    print(s)
else:
    s = ''.join(s)
    print(s)

Solution (Python):
S = input()
Q = int(input())
top_str = ""
last_str = ""
s_parity = 0
for i in range(Q):
    q_list=input().split(" ")
    if q_list[0] == "1":
        s_parity += 1
    elif (s_parity%2) == 0:
        if q_list[1] == "1":
            top_str = q_list[2] + top_str
        else:
            last_str = last_str + q_list[2]
    else:
        if  q_list[1] == "1":
            last_str = last_str + q_list[2]
        else:
            top_str = q_list[2] + top_str


if s_parity % 2 ==1:
    S = S[::-1]
    S = last_str[::-1] + S + top_str[::-1]
else :
    S = top_str + S + last_str
print(S)

Solution (Python):
from collections import deque
a = list(input())
Q = int(input())
d = deque(a)
ans = 0
for i in range(Q):
  b = list(input().split())
  if len(b) == 1:
    ans += 1
  elif b[1] == "1" and ans % 2 == 0:
    d.appendleft(b[2])
  elif b[1] == "1" and ans % 2 == 1:
    d.append(b[2])
  elif b[1] == "2" and ans % 2 == 0:
    d.append(b[2])
  elif b[1] == "2" and ans % 2 == 1:
    d.appendleft(b[2])
if ans % 2 == 1:
  d.reverse()
print("".join(d))

Solution (Python):
from collections import deque
a = list(input())
Q = int(input())
d = deque(a)
ans = 1
for i in range(Q):
  b = list(input().split())
  if len(b) == 1:
    ans += 1
  elif b[1] == "1" and ans % 2 == 1:
    d.appendleft(b[2])
  elif b[1] == "1" and ans % 2 == 0:
    d.append(b[2])
  elif b[1] == "2" and ans % 2 == 1:
    d.append(b[2])
  elif b[1] == "2" and ans % 2 == 0:
    d.appendleft(b[2])
if ans % 2 == 0:
  d.reverse()
print("".join(d))

Solution (Python):
def s0():return input()
def s1():return input().split()
def s2(n):return [input() for x in range(n)]
def n0():return int(input())
def n1():return [int(x) for x in input().split()]
def n2(n):return [int(input()) for _ in range(n)]
def n3(n):return [[int(x) for x in input().split()] for _ in range(n)]

from collections import deque 
s=s0()
n=n0()
q=s2(n)

d1=deque()
d2=deque()

f=False
for a in q:
    if a[0]=="1":
        f=not(f)
    else:
        if a[2]=="1":
            if f==False:
                d1.appendleft(a[4])
            else:
                d2.append(a[4])                
        else:
            if f==False:
                d2.append(a[4])
            else:
                d1.appendleft(a[4])
ss=''.join(list(d1))+s+''.join(list(d2))
print(ss[::-1] if f else ss)

Solution (Python):
import math
import random
import sys
from bisect import bisect_left, bisect_right
from collections import Counter, defaultdict, deque
from functools import lru_cache, reduce
from heapq import heapify, heappop, heappush
from itertools import (chain, combinations, combinations_with_replacement,
                       permutations)

MAX_INT = 2**62-1
sys.setrecursionlimit(10**7)

input = sys.stdin.readline


def read_n_int():
    return map(int, input().split())


def main():
    s = input().strip()
    q = int(input())
    queue = deque(s)
    is_reversed = 0

    for _ in range(q):
        t, *x = input().strip().split()
        
        if t == '1':
            is_reversed ^= 1
        else:
            where, c = x
            where = int(where) & 1
            if where ^ is_reversed:
                queue.appendleft(c)
            else:
                queue.append(c)
    if is_reversed:
        queue.reverse()

    print(''.join(queue))


if __name__ == "__main__":
    main()

Solution (Python):
s = input()
q = int(input())
 
count = 0
s_f = ""
for i in range(q):
    q_i = input()
    if len(q_i) == 1:
        count += 1
    else:
        lis = [i for i in q_i.split()]
        x = count
        x += int(lis[1])
        if x % 2 == 0:
            s += lis[2]
        else:
            s_f += lis[2]

ans = ""
if count % 2 == 1:
    for i in range(len(s)):
        ans += s[-i-1]
    ans += s_f
else:
    s_f_ = ""
    for i in range(len(s_f)):
        s_f_ += s_f[-i-1]
    ans = s_f_ + s
print(ans)

Solution (Python):

S = str(input())
Q = int(input())
query=[]
a=""
b=""


t=True

for i in range(Q):
	q=list(input().split())
	if int(q[0])==1:
		t= not t
	else:
		if int(q[1])==1:
			if t:
				a=q[2]+a
			else:
				b=b+q[2]

		else:
			if t:
				b=b+q[2]
			else:
				a=q[2]+a

if t:
	print(a+S+b)
else:
	print(b[::-1]+S[::-1]+a[::-1])

Solution (Python):
from collections import deque
def main():
  s = deque(list(input()))
  q = int(input())
  f = 0
  for _ in range(q):
    l = input()
    if l == "1":
      f ^= 1
    else:
      a, b, c = l.split()
      if f ^ (b=="1"):
        s.appendleft(c)
      else:
        s.append(c)
  if f:
    ans = list(s)[::-1]
  else:
    ans = list(s)
  print(*ans, sep="")
if __name__ == "__main__":
  main()

Solution (Python):
s=input()
n=int(input())
t=True
a=str()
b=str()
for i in range(n):
    q=input().split()
    if(len(q)==1):
        t=not t
        continue
    if(int(q[1])==1):
        if(t):a=q[2]+a
        else:b=b+q[2]
    if(int(q[1])==2):
        if(t):b=b+q[2]
        else:a=q[2]+a
if(t):print(a+s+b)
else:print(b[::-1]+s[::-1]+a[::-1])

Solution (Python):
S = input()
Q = int(input())

head = ''
back = ''
direction = 1

for i in range(Q):
    q = list(map(str, input().split())) 
    if q[0] == "1":
        direction *= (-1)
        
    else:
        if q[1] == "1":
            if direction == 1:
                head = head + q[2]
            else:
                back = back + q[2]
        else:
            if direction == 1:
                back = back + q[2]
            else:
                head = head + q[2]

if direction == 1:
    print(head[::-1]+S+back)
else:
    print(back[::-1]+S[::-1]+head)

Solution (Python):
from collections import deque

S, inv = deque(input()), False
Q = int(input())
for _ in range(Q):
    q = input().split()
    if len(q) == 1:
        inv = not inv
        continue
    F, C = q[1], q[2]
    if (F == '1') ^ inv:
        S.appendleft(C)
    else:
        S.append(C)
print(''.join(S)[::-1] if inv else ''.join(S))

Solution (Python):
from collections import deque
s = input().split()
sd = deque()
for i in s:
    sd.append(i)
N = int(input())
S = []
order = 1
for _ in range(N):
    res = input().split()
    if res[0] == "1":
        order = order * - 1
    elif res[1] == "1":
        if order == 1:
            sd.appendleft(res[2])
        else:
            sd.append(res[2])
    else:
        if order == 1:
            sd.append(res[2])
        else:
            sd.appendleft(res[2])

sd_str="".join(list(sd))
if order == -1:
    print(sd_str[::-1])
else:
    print(sd_str)