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):
import sys
t = input()

flont = []
back = []

if len(t) == 1:
    flont = [t[0]]
else:
    flont = list(reversed(t[:-1]))
    back = [t[-1]]
q = int(input())
for i in range(q):
    s = input().split()
    if int(s[0]) == 1:
        sw = flont
        flont = back
        back = sw
    else:
        f = int(s[1])
        c = s[2]
        if f == 1:
            flont.append(c)
        else:
            back.append(c)
[sys.stdout.write(i) for i in list(reversed(flont))]
[sys.stdout.write(i) for i in back]
print("")

Solution (Python):
S = list(input())
Q = int(input())
Query = [list(input().split()) for i in range(Q)]

ans = S
X = []
Y = []
state = 1

for k in range(Q):
    if Query[k][0]=="1":
        state *= -1
    elif Query[k][0]=="2":
        if Query[k][1]=="1":
            if state == 1:
                X.append(Query[k][2])
            else:
                Y.append(Query[k][2])
        else:
            if state == 1:
                Y.append(Query[k][2])
            else:
                X.append(Query[k][2])

if state == 1:
    X = list(reversed(X))
    ans = X + ans + Y
else:
    Y = list(reversed(Y))
    ans = list(reversed(ans))
    ans = Y + ans + X

print("".join(ans))

Solution (Python):
from collections import deque

def iin(): return int(input())
def iln(): return list(map(int, input().split()))


S = input()

queue = deque()
for c in S:
    queue.append(c)

is_reversed = False

QN = iin()

for i in range(QN):
    operation = input().split()
    if len(operation) == 1:
        is_reversed = not is_reversed
    else:
        _, op, new_char = operation
        if op == "1":
            
            if is_reversed:
                queue.append(new_char)
            else:
                queue.appendleft(new_char)
        else:
            
            if is_reversed:
                queue.appendleft(new_char)
            else:
                queue.append(new_char)

if is_reversed:
    print("".join(reversed(queue)))
else:
    print("".join(queue))

Solution (Python):
from collections import deque

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

cont = 0
for i in range(q):
    q_uery = list(map(str, input().split()))
    if int(q_uery[0]) == 1:
        cont += 1
    if int(q_uery[0]) == 2:
        if (int(q_uery[1]) + cont) % 2 == 0:
            s.append(q_uery[2])
        else:
            s.appendleft(q_uery[2])
if cont % 2 == 1:
    s.reverse()

print(''.join(s))

Solution (Python):


import sys
 
stdin = sys.stdin
inf = 1 << 60
mod = 1000000007
 
ni = lambda: int(ns())
nin = lambda y: [ni() for _ in range(y)]
na = lambda: list(map(int, stdin.readline().split()))
nan = lambda y: [na() for _ in range(y)]
nf = lambda: float(ns())
nfn = lambda y: [nf() for _ in range(y)]
nfa = lambda: list(map(float, stdin.readline().split()))
nfan = lambda y: [nfa() for _ in range(y)]
ns = lambda: stdin.readline().rstrip()
nsn = lambda y: [ns() for _ in range(y)]
ncl = lambda y: [list(ns()) for _ in range(y)]
nas = lambda: stdin.readline().split()

from collections import deque

s = deque(ns())
q = ni()
p = 0
for i in range(q):
    que = nas()
    if int(que[0]) == 1:
        p += 1
    elif int(que[0]) == 2:
        f, c = int(que[1]), que[2]
        if p % 2 == 0:
            if f == 1:
                s.appendleft(c)
            else:
                s.append(c)
        else:
            if f == 1:
                s.append(c)
            else:
                s.appendleft(c)

for i in range(len(s)):
    if p % 2 == 1:
        print(s.pop(), end="")
    else:
        print(s.popleft(), end="")
print()

Solution (Python):
from collections import deque

t = input()
s = deque()
for i in range(len(t)): s.append(t[i])
q = int(input())
flg = False
for i in range(q):
    query = input()
    if query[0] == '1': flg = not flg
    else:
        t,f,c = query.split()
        if flg:
            s.appendleft(c) if f=='2' else s.append(c)
        else:
            s.appendleft(c) if f=='1' else s.append(c)
if flg: s = reversed(s)
print(''.join(s))

Solution (Python):
s = str(input())
q = int(input())

x = []
pre = ''
post = ''
isone = False
for i in range(q):
    x = input().split()
    if x[0] == '1':
        if isone:
            isone = False
        else:
            isone = True
    elif x[1] == '1':
        if isone:
            post += x[2]
        else:
            pre += x[2]
    else:
        if isone:
            pre +=x [2]
        else:
            post += x[2]
    if i == q - 1:
        if isone:
            s = post[::-1] + s[::-1] + pre
        else:
            s = pre[::-1] + s + post
print(s)

Solution (Python):

 
import math
import itertools
import sys
import copy

 







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


sl = [] 
el = [] 
f = True 
for _ in range(Q) :
  stmp = input().split()
  
  
  if stmp[0] == "1" :
    f = not f
    tl = sl
    sl = el
    el = tl
    
  else :
    if stmp[1] == '1' :
      sl.append(stmp[2])
    else :
      el.append(stmp[2])

ans = ""
for i in range(len(sl)-1, -1, -1) :
  ans += sl[i]
if f :
  ans += S
else :
  ans += S[::-1]
for i in range(len(el)) :
  ans += el[i]
  
print (ans)

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

num_turn = 0

front = []
end = []

for i in range(Q):
    query = input()
    if query == '1':
        num_turn += 1
    else:
        _, f, c = query.split()
        f = int(f)
        if f == 1:
            if num_turn%2==0:
                front.append(c)
            else:
                end.append(c)
        else:
            if num_turn%2==0:
                end.append(c)
            else:
                front.append(c)

S = list(S)
if num_turn%2==1:
    ans = end[::-1] + S[::-1] + front
else:
    ans = front[::-1] + S + end
print(''.join(ans))

Solution (Python):
from collections import deque
def main():
  d=deque(list(input()))
  Q=int(input())
  rev=0
  for i in range(Q):
    q=list(map(str, input().split()))
    if q[0]=='1':
      rev+=1
    else:
      if (int(q[1])+rev)%2==1:
        d.appendleft(q[2])
      else:
        d.append(q[2])
  if rev%2==0:
    print(''.join(list(d)))
  else:
    d.reverse()
    print(''.join(list(d)))
main()

Solution (Python):
from collections import deque
que = deque()
ans = ''
s = list(input())
for i in range(len(s)):
    que.append(s[i])
q = int(input())
flag = 0
for i in range(q):
    a = list(map(str, input().split()))
    if a[0] == '1':
        if flag == 0:
            flag =1
        else:
            flag = 0
    elif a[0] == '2':
        if a[1] == '1':
            if flag == 0:
                que.appendleft(str(a[2]))
            else:
                que.append(str(a[2]))
        elif a[1] == '2':
            if flag == 1:
                que.appendleft(str(a[2]))
            else:
                que.append(str(a[2]))
if flag == 0:
    for i in range(len(que)):
        ans += str(que.popleft())
elif flag == 1:
    for i in range(len(que)):
        ans += str(que.pop())
print(ans)

Solution (Python):
import math
import sys

m=10**9 + 7 
sys.setrecursionlimit(1000010)
S = input()
Q = int(input())

inv = 0
head=[]
tail=[]
for _ in range(Q):
    q = input()
    if q == '1':
        inv = 1 - inv
    else:
        (T,F,C) = list(q.split())
        
        if inv == 0:
            if F == '1':
                head.append(C)
            else:
                tail.append(C)
        else:
            if F == '1':
                tail.append(C)
            else:
                head.append(C)
    

if inv == 0:
    print(''.join(list(reversed(head))) + S + ''.join(tail))
else:
    print(''.join(list(reversed(tail))) + ''.join(list(reversed(S))) + ''.join(head))

Solution (Python):
s = input()
q = int(input())
jun = 1
f = "";t = ""
for _ in range(q):
  
  tmp = input()
  if len(tmp)==1:
    jun = 1 if jun==0 else 0 
  else:
    _,ff,c = tmp.split()
    if jun+int(ff) ==2:
      f = c+f
    else:
      t += c
if jun==1:
  print(f+s+t)
else:
  print((f+s+t)[::-1])

Solution (Python):
import sys
from collections import deque


def main():
    s = sys.stdin.readline().strip()
    q = int(sys.stdin.readline().strip())
    r = 0
    head = []
    bottom = []
    do_reverse = False
    for _ in range(q):
        command = sys.stdin.readline().strip().split(" ")
        
        
        
        
        
        
        
        if command[0] == "1":
            do_reverse = not do_reverse
            r += 1
        else:
            position_head = command[1] == "1"
            if do_reverse:
                position_head = not position_head
            if position_head:
                head.append(command[2])
            else:
                bottom.append(command[2])
    if r % 2 == 0:
        print("".join(head[::-1]) + s + "".join(bottom))
    else:
        print("".join(bottom[::-1]) + s[::-1] + "".join(head))


if __name__ == '__main__':
    main()

Solution (Python):
S = input()

Q = int(input())

array = ["" for i in range(Q*2+1)]

array[Q] = S
order = 0

for i in range(Q):
    s = input().split()
    
    if s[0] == '1':
        order = 1 - order
    elif s[1] == '1':
        if order == 0:
            array[Q-(i+1)] = s[2]
        else:
            array[Q+(i+1)] = s[2]
    elif s[1] == '2':
        if order == 0:
            array[Q+(i+1)] = s[2]
        else:
            array[Q-(i+1)] = s[2]



if order == 0:
    for i in range(2*Q+1):
        print(array[i],sep="",end="")
else:
    for i in range(2*Q, -1,-1):
        for j in range(len(array[i])):
            print(array[i][len(array[i])-1-j],sep="",end="")

Solution (Python):



import sys
from collections import deque

s2nn = lambda s: [int(c) for c in s.split(' ')]
ss2nn = lambda ss: [int(s) for s in list(ss)]
ss2nnn = lambda ss: [s2nn(s) for s in list(ss)]
i2s = lambda: sys.stdin.readline().rstrip()
i2n = lambda: int(i2s())
i2nn = lambda: s2nn(i2s())
ii2ss = lambda n: [i2s() for _ in range(n)]
ii2nn = lambda n: ss2nn(ii2ss(n))
ii2nnn = lambda n: ss2nnn(ii2ss(n))

def main():
    S = deque(i2s())
    Q = i2n()
    reverse = False
    for _ in range(Q):
        T, *args = i2s().split(' ')
        if T == '1':
            reverse = not reverse
            continue
        F, C = args
        toHead = F == '1'
        if toHead ^ reverse:
            
            S.appendleft(C)
        else:
            
            S.append(C)
    if not reverse:
        print(''.join(S))
    else:
        print(''.join(reversed(S)))
    return

main()

Solution (Python):
s = str(input())
q = int(input())

cnt_t1 = 0
s_1 = ""
s_2 = ""

for i in range(q):
    tmp = str(input())

    if tmp[0] == "1":
        cnt_t1 += 1

    else:
        if tmp[2] == "1":
            if cnt_t1 % 2 == 0:
                s_1 = tmp[4] + s_1
            else:
                s_2 = s_2 + tmp[4]
        else:
            if cnt_t1 % 2 == 1:
                s_1 = tmp[4] + s_1
            else:
                s_2 = s_2 + tmp[4]

s = s_1 + s + s_2
if cnt_t1 % 2 == 1:
    s = s[::-1]
print(s)

Solution (Python):
from collections import deque


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

d = deque(list(S))

left_head = True

for i in range(Q):
    inp = input()

    if inp == '1':
        left_head = not left_head
    else:
        T, F, c = inp.split()

        
        if F == '1':
            if left_head:
                d.appendleft(c)
            else:
                d.append(c)
        else:
            if left_head:
                d.append(c)
            else:
                d.appendleft(c)


if left_head:
    print(''.join(d))
else:
    print(''.join(d)[::-1])

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

left = []
right = []

reversed_ = False

for _ in range(Q):
    INPUT = input()

    if INPUT == '1':
        reversed_ = not(reversed_)
        left, right = right, left
        continue

    INPUT = INPUT.split()
    F = INPUT[1]
    C = INPUT[2]

    if F == '1':
        left.append(C)
    else:
        right.append(C)

for c in reversed(left):
    print(c, end='')

if reversed_:
    for c in reversed(S):
        print(c, end='')
else:
    print(S, end='')

for c in right:
    print(c, end='')

Solution (Python):
s=input()
q=int(input())
a=""
b=""
res=0
for i in range(q):
    l=list(input().split())
    if l[0]=="1":
        res+=1
        a,b=b,a
    else:
        if l[1]=="1":
            a+=l[2]
        else:
            b+=l[2]
if res%2==0:
    print(a[::-1]+s+b)
else:
    print(a[::-1]+s[::-1]+b)

Solution (Python):
s = input()
q = int(input())
rev = 0
fir = ''
end = ''
for i in range(q):
    qvery = input()
    if len(qvery) == 1: 
        rev += 1
    else:
        qq, f, c = qvery.split(' ')
        f = int(f)
        if rev % 2 == 0:
            if f == 1:
                fir += c
            else:
                end += c
        else:
            if f == 2:
                fir += c
            else:
                end += c
fir = fir[::-1]
s = fir + s + end
if rev % 2 == 1:
    s = s[::-1]
print(s)

Solution (Python):
s = input()
q = int(input())
adv = ''
bac = ''
Flag = True
for i in range(q):
    xxx = input()
    if len(xxx) == 1:
        if Flag == True:
            Flag = False
        else:
            Flag = True
    else:
        T,F,C=xxx.split()
        F = int(F)
        if F == 1:
            if Flag == True:
                adv +=C
            else:
                bac +=C
        else:
            if Flag == True:
                bac +=C
            else:
                adv +=C
if Flag == True:
    s = adv[::-1] + s + bac
    print(s)
else:
    s = bac[::-1] + s[::-1] + adv
    print(s)

Solution (Python):
s=input()
q=int(input())
muki=True
a=[[_ for _ in input().split()] for i in range(q)]
front=""
back=""
for i in a:
    if i[0]=="1":
        muki=not muki
        continue
    if (i[1]=="1" and muki) or (i[1]=="2" and not muki):
        front=i[2]+front
    else:
        back+=i[2]
if muki:
    print(front+s+back)
else:
    print((front+s+back)[::-1])

Solution (Python):
moji=input()
times=int(input())

sento=[]
sippo=[]
plus=[sento,sippo]
dictionary={"sento":0,"sippo":1}

hantenkaisuu=0

for i in range(times):
    command=input().split()
    
    
    if command[0]=="1":
        hantenkaisuu+=1
        dictionary["sento"]=(dictionary["sento"]+1)%2
        dictionary["sippo"]=(dictionary["sippo"]+1)%2
        
    
    else:
        if command[1]=="1":
            plus[dictionary["sento"]].append(command[2])
        else:
            plus[dictionary["sippo"]].append(command[2])
           
    
            
    
    
if hantenkaisuu%2==0:
    sento=sento[::-1]
    sento="".join(sento)
    sippo="".join(sippo)
    print(sento+moji+sippo)
else:
    sippo=sippo[::-1]
    sento="".join(sento)
    sippo="".join(sippo)
    moji=moji[::-1]
    print(sippo+moji+sento)

Solution (Python):
S = input()
Q = int(input())
reverse = -1
left = ""
right = ""
for i in range(Q):
  q = list(input().split())
  if q[0] == '1':
    reverse *= (-1)
  else:
    if reverse > 0:
      if q[1] == '1':
        right = right + q[2]
      else:
        left = q[2] + left
    else:
      if q[1] == '1':
        left = q[2] + left
      else:
        right = right + q[2]
if reverse>0:
  print((left+S+right)[::-1])
else:
  print(left+S+right)

Solution (Python):
S, Q = input(), int(input())
rev = False
st = []
ed = []
for _ in range(Q):
  a = input().split(' ')
  if a[0] == '1':
    rev = not rev
    continue
  if not rev:
    if a[1] == '1':
      st.append(a[2])
    else:
      ed.append(a[2])
  else:
    if a[1] == '1':
      ed.append(a[2])
    else:
      st.append(a[2])
if rev:
  print(''.join(reversed(ed)) + S[::-1] + ''.join(st))
else:
  print(''.join(reversed(st)) + S + ''.join(ed))

Solution (Python):
S = input()
Q = int(input())
mode = 0
ans_1 = ""
ans_2 = ""
for i in range(Q):
    K = input().split()
    if K[0] == "1":
        if mode == 0:
            mode = 1
        else:
            mode = 0
    else:
        if K[1] == "1":
            if mode == 0:
                ans_1 += K[2]
            else:
                ans_2 += K[2]
        else:
            if mode == 0:
                ans_2 += K[2]
            else:
                ans_1 += K[2]
ans_1 = ans_1[::-1]
K = ans_1+S+ans_2
if mode == 1:
    print(K[::-1])
else:
    print(K)

Solution (Python):
from collections import deque

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

for i in range(q):
  a = list(map(str, input().split()))
  if(a[0] == "1"):
    top = (top + 1) % 2
    continue
  
  if(top + int(a[1]) - 1 == 1):
    s.append(a[2])
  else:
    s.appendleft(a[2])
if(top == 0):
  print(''.join(s))
else:
  print((''.join(s))[::-1])

Solution (Python):
s=input()
q=int(input())
queri=[]
rv=0
forward=[]
back=[]
for i in range(q):
    queri=list(map(lambda x:x,input().split()))
    if queri[0]=="2":
        rvt=(rv+int(queri[1]))%2
        if rvt==1: forward.append(queri[2])
        else: back.append(queri[2])

    else: rv=(rv+1)%2

if rv==0:
    for i in range(len(forward)-1,-1,-1):
        print(forward[i],end="")
    print(s,end="")
    for i in range(len(back)):
        print(back[i],end="")
        
if rv==1:
    for i in range(len(back)-1,-1,-1):
        print(back[i],end="")
    print(s[::-1],end="")
    for i in range(len(forward)):
        print(forward[i],end="")

Solution (Python):
S = input()
Q = int(input())
revers_count = 0
mae = ""
ushiro = ""

for i in range(Q):
    Q_i = list(input().split())
    if Q_i[0] == "1":
        revers_count += 1
    else:
        if (Q_i[1] == "1" and revers_count % 2 == 0) or (Q_i[1] == "2" and revers_count % 2 == 1):
            mae = Q_i[2] + mae
        else:
            ushiro = ushiro + Q_i[2]

ans = mae + S + ushiro

if revers_count % 2 == 1:
    ans = ans[::-1]

print(ans)

Solution (Python):


import sys
from collections import deque

DEBUG = False

def solve():
    return


def read_int_list(sep = " "):
    return [int(s) for s in sys.stdin.readline().rstrip().split(sep)]

def read_int():
    return int(sys.stdin.readline())

def read_str():
    return sys.stdin.readline().rstrip()
    
def read_str_list(sep = " "):
    return [s for s in sys.stdin.readline().rstrip().split(sep)]


def dprint(*args, **kwargs):
    if DEBUG:
        print(*args, **kwargs)
    return


def main():
    d = deque(list(read_str()))
    q = read_int()

    reversed_ = False
    for _ in range(0, q):
        query = read_str_list()
        if query[0] == "1":
            reversed_ = not reversed_
            continue
        
        f, c = query[1:]
        if (f == "1" and not reversed_) or (f == "2" and reversed_):  
            d.appendleft(c)
        else:
            d.append(c)
    if reversed_:
        print("".join(reversed(list(d))))
    else:
        print("".join(list(d)))


if __name__ == "__main__":
    main()

Solution (Python):
s = list(input())
q = int(input())
a = []
f = False 
for i in range(q):
    x = list(input().split())
    if x[0]=='1':
        f = not f
    else:
        if f:
            if x[1]=='1': s.append(x[2])
            else: a.append(x[2])
        else:
            if x[1]=='1': a.append(x[2])
            else: s.append(x[2])
a.reverse()
s = a+s
if f:s.reverse()
print(''.join(s))

Solution (Python):
S = input()
reverse = 0
front = []
end = []
n = int(input())
for _ in range(n):
  List = list(input().split())
  if List[0] == '1':
    reverse += 1
  else:
    if List[1] == '1':
      if reverse % 2 == 1:
        end.append(List[2])
      else:
        front.append(List[2])
    else:
      if reverse % 2 == 1:
        front.append(List[2])
      else:
        end.append(List[2])
if reverse %2 == 1:
  S = S[::-1]
  front, end = end, front
Answer = ''.join(front[::-1]) + S + ''.join(end)
print(Answer)

Solution (Python):
def main():
    S = raw_input()
    Q = int(raw_input())

    is_reversed = False
    sentou = []
    owari = []

    for _ in range(Q):
        query = raw_input().split()
        if query[0] == "1":
            is_reversed = not is_reversed
        elif query[0] == "2":
            F = query[1]
            C = query[2]
            if F == "1":
                if not is_reversed:
                    sentou.append(C)
                else:
                    owari.append(C)
            elif F == "2":
                if not is_reversed:
                    owari.append(C)
                else:
                    sentou.append(C)

    S = "".join(sentou[::-1]+list(S)+owari)


    if not is_reversed:
        print S
    else:
        print S[::-1]

if __name__ == "__main__":
    main()

Solution (Python):
from collections import deque


S = list(input())
N = int(input())

d = deque()
d.extend(S)


direction = True

for i in range(N):
    query = input()
    if query[0] == "1":
        t = int(query)
    else:
        t, f, c = query.split()
        t = int(t)
        f = int(f)

    if t == 1:
        
        direction = not direction

    if t == 2:
        if direction == True:
            if f == 1:
                d.appendleft(c)
            else:
                d.append(c)

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

if direction == True:
    print(''.join(d))
else:
    print(''.join(reversed(d)))

Solution (Python):
from collections import deque

S = deque(input())
Q = int(input())
rev = 0

for i in range(Q):
    l = str(input())
    if l[0] == '1':
        rev = (rev + 1) % 2
    else:
        two, F, C = l.split()
        if rev == 0:
            if F == '1':
                S.appendleft(C)
            else:
                S.append(C)

        else:
            if F == '1':
                S.append(C)

            else:
                S.appendleft(C)

if rev == 0:
    print(''.join(S))
else:
    print(''.join(list(reversed(S))))

Solution (Python):
S=input()
Q_num = int(input())
Q=[]
for _ in range(Q_num):
    Q.append(list(input().split()))
from collections import deque
sento = deque()
matsubi = deque()
mirror_num=0
for q in Q:
    t=q[0]
    if t=='1':
        mirror_num+=1
        continue

    f=int(q[1])
    c=q[2]
    
    if int(mirror_num%2)==0:
        if f==1:
            sento.appendleft(c)
            continue
        matsubi.append(c)
        continue

    if f==1:
        matsubi.append(c)
        continue
    sento.appendleft(c)
    continue

sento = ''.join(sento)
matsubi = ''.join(matsubi)
S_result = sento+S+matsubi
if mirror_num%2==1:
    S_result = S_result[::-1]
print(S_result)

Solution (Python):
from sys import stdin
s = list(stdin.readline().rstrip())
q = int(stdin.readline().rstrip())
t = []
k = []
for i in range(q):
    t.append([x for x in stdin.readline().rstrip().split()])
r = 0
for i in range(q):
    if int(t[i][0]) == 1:
        if r == 1:
            r = 0
        else:
            r = 1
    else:
        if (int(t[i][1]) + r) % 2 == 0:
            s.append(t[i][2])
        else:
            k.append(t[i][2])
k = list(reversed(k))
k.extend(s)
if r == 1:
    k = list(reversed(k))
k = ''.join(k)
print(k)

Solution (Python):
s = str(input())
q = int(input())
Q = [list(input().split()) for i in range(q)]
head = ''
bottom = ''
reverse = False
for i in range(q):
    if Q[i][0] == '1':
        reverse = not reverse
    else: 
        if Q[i][1] == '1':
            if reverse == False:
                head = Q[i][2] + head
            else:
                bottom = bottom + Q[i][2]
        if Q[i][1] == '2':
            if reverse == False:
                bottom = bottom + Q[i][2]
            else:
                head = Q[i][2] + head
s = head + s + bottom
ans = ''
if reverse == True:
    for i in range(len(s)):
        ans += s[-1 * (i + 1)]
else:
    ans = s
print(ans)

Solution (Python):
from collections import deque

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

cur = 1
d = deque([S])

for _ in range(Q):
    ins = input().split()
    if len(ins) == 1:
        cur = 2 if cur == 1 else 1
    else:
        _, f, c = ins
        f = int(f)
        if cur == f == 1 or cur == f == 2:
            d.appendleft(c)
        else:
            d.append(c)

if cur == 1:
    print("".join(d))
else:
    temp = "".join(d)
    print(temp[::-1])

Solution (Python):
from collections import deque
S = list(input())
S = deque(S)
rev = False
Q = int(input())
for _ in range(Q):
    T = list(map(str,input().split()))
    if T[0] == '1':
        if rev:
            rev = False
        else:
            rev = True
    if T[0] == '2':
        if T[1] == '1':
            if rev:
                S.append(T[2])
            else:
                S.appendleft(T[2])
        if T[1] == '2':
            if rev:
                S.appendleft(T[2])
            else:
                S.append(T[2])
if rev:
    S = list(reversed(S))
    print("".join(S))
else:
    S = list(S)
    print("".join(S))

Solution (Python):
from collections import deque
s = deque(input())
Q = int(input())
c = 0
for _ in range(Q):
  q = input()
  if (q[0] == "1"):
    c += 1
  else:
    if (q[2] == "1"):
      if (c % 2 == 0):
        s.appendleft(q[4])
      else:
        s.append(q[4])
    else:
      if (c % 2 == 0):
        s.append(q[4])
      else:
        s.appendleft(q[4])
if (c % 2 == 0):
  print("".join(list(s)))
else:
  print("".join(list(s))[::-1])

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

reverseFlag = False
prefix = ""
suffix = ""

for i in range(NQ):
    query = input().split()
    if (int(query[0]) == 1):
        reverseFlag = not reverseFlag
    else:
        if (reverseFlag):
            if (int(query[1]) == 1):
                suffix = suffix + query[2]
            else:
                prefix = query[2] + prefix
        else:
            if (int(query[1]) == 1):
                prefix = query[2] + prefix
            else:
                suffix = suffix + query[2]

if (reverseFlag):
    print(suffix[::-1] + S[::-1] + prefix[::-1])
else:
    print(prefix + S + suffix)

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



front = ''
back = ''

r_flg = False
for i in range(Q):
    q = input()
    if q == '1':
        r_flg = False if r_flg else True
        continue
        
    t,f,c = q.split()
    if not r_flg:
        if f == '1':
            front += c
        else:
            back += c
    else:
        if f == '1':
            back += c
        else:
            front += c
    
if not r_flg:
    print(front[::-1]+S+back)
else:
    print(back[::-1] + S[::-1] + front)

Solution (Python):
import sys
sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))
from collections import deque
def resolve():
    S = deque(sr())
    Q = ir()
    rev = 1
    for i in range(Q):
        T = list(sr().split())
        if int(T[0]) == 1:
            rev *= -1
        else:
            x, F, C = T
            if int(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:
        S.reverse()
    print(*S, sep='')
resolve()

Solution (Python):
from collections import deque
s = list(input())
q = int(input())

s = deque(s)
rev = 0

for i in range(q):
    t = list(input().split())
    if len(t) == 1:
        rev += 1
        continue
    if t[1] == '1':
        if rev % 2 == 0:
            s.appendleft(t[2])
        else:
            s.append(t[2])
    else:
        if rev % 2 == 0:
            s.append(t[2])
        else:
            s.appendleft(t[2])
if rev % 2 != 0:
    s.reverse()

print(''.join(s))

Solution (Python):
from collections import deque
s = list(input())
q_num = int(input())

s_deq = deque(s)
reversed = False

for _ in range(q_num):
    query = input().split()
    if query[0] == '1':
        reversed = False if reversed else True
    elif query[0] == '2':
        if not reversed:
            if query[1] == '1':
                s_deq.appendleft(query[2])
            elif query[1] == '2':
                s_deq.append(query[2])
        else:
            if query[1] == '2':
                s_deq.appendleft(query[2])
            elif query[1] == '1':
                s_deq.append(query[2])
if reversed:
    s_deq.reverse()
print(''.join(s_deq))

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


ss = list(input())
n = len(ss)-1

s = deque([])
for i in range(n):
    s.append(ord(ss[i]))

p = 1

q = int(input())
for i in range(q):
    inp = list(input())

    if inp[0] == '1':
        p *= -1
    else:
        c = ord(inp[4])
        f = inp[2]
        if (f == '1' and p == 1) or (f == '2' and p == -1):
            s.appendleft(c)
        else:
            s.append(c)


n = len(s)
if p == 1:
    for e in s:
        print(chr(e),end = '')
else:
    for i in range(n-1,-1,-1):
        print(chr(s[i]),end = '')

Solution (Python):
s=input()
q=int(input())
query=[]
for i in range(q):
    query.append(input())
rev=0
bef=""
aft=""
for i in range(q):
    if query[i][0]=="1":
        rev+=1
    else:
        n,f,c=query[i].split()
        if (rev+int(f))%2==0:
            aft=aft+c
        else:
            bef=c+bef
seqfin=bef+s+aft
if rev%2==0:
    print(seqfin)
else:
    print(seqfin[::-1])

Solution (Python):
reverseFlag = False

S = input()
Q = int(input())
S_mae = ''
S_ato = ''

for i in range(Q):
    TFC = input().split(' ')
    if TFC[0] == '1':
        reverseFlag = not reverseFlag
    else:
        if TFC[1] == '1':
            if reverseFlag:
                S_ato = S_ato + TFC[2]
            else:
                S_mae = TFC[2] + S_mae
        else:
            if reverseFlag:
                S_mae = TFC[2] + S_mae
            else:
                S_ato = S_ato + TFC[2]
if reverseFlag:
    print(S_ato[::-1] + S[::-1] + S_mae[::-1])
else:
    print(S_mae + S + S_ato)