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


def order_manegement():
    while True:
        yield 1
        yield 0


def main():
    s = input()
    q = int(input())
    s = deque(s)
    gen = order_manegement()
    state = 0
    for i in range(q):
        order = list(input().split())
        if order[0] == '1':
            state = next(gen)
        elif order[0] == '2':
            if (state + int(order[1])) % 2 == 0:
                s.append(order[2])
            else:
                s.appendleft(order[2])
    s = list(s)
    if state == 1:
        s = s[::-1]

    print(''.join(s))


if __name__ == '__main__':
    main()

Solution (Python):
from collections import deque

s = deque(input())
n = int(input())

memo = 0
for i in range(n):
    q = list(input().split())
    if q[0] == '1':
        if  memo == 0: memo = 1
        else: memo = 0
    else:
        if memo == 0:
            if q[1] == '1': s.appendleft(q[2])
            else: s.append(q[2])
        else:
            if q[1] == '1': s.append(q[2])
            else: s.appendleft(q[2])

if memo == 0: print(''.join(s))
else: print(''.join(list(s)[::-1]))

Solution (Python):
from collections import deque

s = deque(input())
n = int(input())




memo = 0
for i in range(n):
    q = list(input().split())
    if q[0] == '1':
        if  memo == 0: memo = 1
        else: memo = 0
    else:
        if memo == 0:
            if q[1] == '1': s.appendleft(q[2])
            else: s.append(q[2])
        else:
            if q[1] == '1': s.append(q[2])
            else: s.appendleft(q[2])

if memo == 0: print(''.join(s))
else: print(''.join(list(s)[::-1]))

Solution (Python):
MAX_Q = 500_001
OFFSET = 200_000
C = [None] * MAX_Q
S = input()
N = len(S)
for i in range(N):
    C[OFFSET + i] = S[i]
Q = int(input())
left = OFFSET - 1
right = N + OFFSET
flg = False
for _ in range(Q):
    T = input()
    if len(T) == 1:
        flg = not(flg)
    else:
        f, c = T.split()[1:]
        if ((not flg and f == '1') or (flg and f == '2')):
            C[left] = c
            left -= 1
        else:
            C[right] = c
            right += 1
if flg:
    print(''.join(C[right-1:left:-1]))
else:
    print(''.join(C[left+1:right]))

Solution (Python):
s=input()
n=int(input())
cnt=0
left=[]
right=[]
for _ in range(n):
    tmp=input()
    if tmp[0]=="1":
        cnt+=1
    else:
        num,side,st=tmp.split()
        if side=="1":
            if cnt%2==0:
                left.append(st)
            else:
                right.append(st)
        else:
            if cnt%2==0:
                right.append(st)
            else:
                left.append(st)
left="".join(left)
left=left[::-1]
right="".join(right)
s=left+s+right
if cnt%2==1:
    s=s[::-1]
print(s)

Solution (Python):
s=input()
n=int(input())
cnt=0
left=[]
right=[]
for _ in range(n):
    tmp=input()
    if tmp[0]=="1":
        cnt+=1
    else:
        num,side,st=tmp.split()
        if side=="1":
            if cnt%2==0:
                left.append(st)
            else:
                right.append(st)
        else:
            if cnt%2==0:
                right.append(st)
            else:
                left.append(st)
left="".join(left)
left=left[::-1]
right="".join(right)
s=left+s+right
if cnt%2==1:
    s=s[::-1]
print(s)

Solution (Python):
from collections import deque


def main():
    s = input()
    d = deque(s)
    q = int(input())
    reverse = False
    for _ in range(q):
        line = input().split()
        if line[0] == "1":
            reverse = reverse ^ True
        else:
            if (not reverse and line[1] == "1") or (reverse and line[1] == "2"):
                d.appendleft(line[2])
            else:
                d.append(line[2])
    print("".join(list(d)) if not reverse else "".join(list(reversed(d))))


if __name__ == "__main__":
    main()

Solution (Python):
inversion = 1 
s = input()
s1 = ""
s2 = ""
n_query = int(input())
for i in range(n_query):
    q = input().split()
    
    if q[0] == '1':
        inversion *= -1
    elif q[0] == '2':
        if inversion == 1:
            if q[1] == '1':
                s1 = s1 + q[2]
            elif q[1] == '2':
                s2 = s2 + q[2]
        elif inversion == -1:
            if q[1] == '1':
                s2 = s2 + q[2]
            elif q[1] == '2':
                s1 = s1 + q[2]

if inversion == 1:
    s = s1[::-1] + s + s2
elif inversion == -1:
    s = s2[::-1] + s[::-1] + s1

print(s)

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

def main():
    s = deque(input())
    q = int(input())
    reverse = False
    
    for _ in range(q):
        query = list(input().split())
        if query[0] == '1':
            reverse ^= True
        else:
            if reverse:
                if query[1] == '1':
                    s.append(query[2])
                else:
                    s.appendleft(query[2])
            else:
                if query[1] == '1':
                    s.appendleft(query[2])
                else:
                    s.append(query[2])
                    
    s = ''.join(s)
    if reverse:
        s = s[::-1]
    print(s)
        
if __name__ == '__main__':
    main()

Solution (Python):
S = input()

from collections import deque
S = deque(S)

Q = int(input())
rev = False

for i in range(Q):
  q = input().split()
  if q[0] == "1":
    rev ^= True
  else:
    F = q[1]
    if (F == "1" and (not rev)) or (F == "2" and rev):
      S.appendleft(q[2])
    else:
      S.append(q[2])

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

Solution (Python):
from collections import deque
s = deque(list(input()))
q = int(input())
cnt = 1
for _ in range(q):
    query = input()
    if len(query)==1:
        cnt = 1-cnt
    else:
        f = query[2]
        c = query[-1]
        if cnt:
            if f =='1':
                s.appendleft(c)
            else:
                s.append(c)
        else:
            if f=='1':
                s.append(c)
            else:
                s.appendleft(c)
print(''.join(s) if cnt else ''.join(s)[::-1])

Solution (Python):
from collections import deque

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

flip = 0
for _ in range(Q):
    q = input().split()
    if q[0] == "1":
        flip += 1
        flip %= 2
    else:
        if (int(q[1]) - 1 + flip)%2 == 0:
            S.appendleft(q[2])
        else:
            S.append(q[2])

S = list(S)
if flip == 0:
    print(*S, sep="")
else:
    print(*S[::-1], sep="")

Solution (Python):
from collections import deque

s = deque(list(input()))

q = int(input())

reverse = 0

for i in range(q):
  query = list(input().split())
  if query[0] == '1':
    reverse ^= 1
    continue
  else:
    if query[1] == '1':
      if reverse:
        s.append(query[2])
      else:
        s.appendleft(query[2])
    else:
      if reverse:
        s.appendleft(query[2])
      else:
        s.append(query[2])
if reverse:
  print(''.join(s)[::-1])
else:
  print(''.join(s))

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

forward = True
que0 = []
que1 = []

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

    if query[0] == '1':
        forward = not forward

    else:
        head = (query[1] == '1')
        if head ^ forward:
            que1.append(query[2])
        else:
            que0.append(query[2])

S = ''.join(que0[::-1]) + S + ''.join(que1)

if forward:
    print(S)
else:
    print(S[::-1])

Solution (Python):
from collections import deque
s,q=deque([*input()]),int(input())
judge=False
for i in range(q):
  t=input().split()
  if len(t)==1:
    judge = not judge
  else:
    t,f,c=t
    if not judge and f=='1': s.appendleft(c)
    elif not judge and f=='2': s.append(c)
    elif judge and f=='1': s.append(c)
    else: s.appendleft(c)
if judge: s.reverse()
print(*s,sep='')

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

S = readline().rstrip()

from collections import deque
S = deque(S)

Q = int(readline())
turn = False
for i in range(Q):
  query = readline().split()
  if query[0] == "1":
    turn ^= True
  elif query[0] == "2":
    if query[1] == "1":
      if turn:
        S.append(query[2])
      else:
        S.appendleft(query[2])
    elif query[1] == "2":
      if turn:
        S.appendleft(query[2])
      else:
        S.append(query[2])

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

Solution (Python):
from collections import deque

s = deque(list(input()))
n = int(input())
q = [list(input().split()) for _ in range(n)]
rev = False

for tfc in q:
    if tfc[0] == "1" and rev is False:
        rev = True
    elif tfc[0] == "1" and rev is True:
        rev = False
    elif rev is False:
        if tfc[1] == "1":
            s.appendleft(tfc[2])
        else:
            s.append(tfc[2])
    elif rev is True:
        if tfc[1] == "1":
            s.append(tfc[2])
        else:
            s.appendleft(tfc[2])

if rev:
    s.reverse()
print("".join(s))

Solution (Python):

from collections import deque

S = input()
Q = int(input())
q = deque(list(S))
rev = 0
for _ in range(Q):
    s, *x = input().split()
    if s == "1":
        rev += 1
    else:
        if ((x[0] == "1" and rev % 2 == 0)
                or (x[0] == "2" and rev % 2 == 1)):
            q.appendleft(x[1])
        else:
            q.append(x[1])

if rev % 2 == 0:
    print("".join(q))
else:
    print("".join(q)[::-1])

Solution (Python):
from collections import deque

S = input()

heads = deque([])
tails = deque([])
reverse = False

Q = int(input())
for _ in range(Q):
    cmds = input().split()
    if cmds[0] == '1':
        reverse = not reverse
        heads, tails = tails, heads
    else:
        f, c = cmds[1], cmds[2]
        if f == '1':
            if reverse:
                heads.append(c)
            else:
                heads.appendleft(c)
        else:
            if reverse:
                tails.appendleft(c)
            else:
                tails.append(c)
    
    
    
    
    
    

if reverse:
    heads = list(heads)[::-1]
    tails = list(tails)[::-1]
    S = S[::-1]

heads = ''.join(map(str, heads))
tails = ''.join(map(str, tails))
ans = ''.join([heads, S, tails])
print(ans)

Solution (Python):

import sys
import collections as cl


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


def MI(): return map(int, sys.stdin.readline().split())


def LI(): return list(map(int, sys.stdin.readline().split()))


def main():
    S = input()
    Q = II()
    
    direction = 1
    pre = []
    sur = []

    for i in range(Q):
        query = input()
        if query == '1':
            direction *= -1
        else:
            _, f, c = query.split()
            if direction == 1:
                if f == '1':
                    pre.append(c)
                else:
                    sur.append(c)
            else:
                if f == '1':
                    sur.append(c)
                else:
                    pre.append(c)

    if direction == 1:
        ans = "".join(pre[::-1]) + S + "".join(sur)
    else:
        ans = "".join(sur[::-1]) + "".join(S[::-1]) + "".join(pre)
    print(ans)

main()

Solution (Python):






from collections import deque





S = deque(input())
Q = int(input())
rev_cnt = 0
for _ in range(Q):
    q = input().split()
    if len(q) == 1:
        rev_cnt += 1
    else:
        if (rev_cnt+int(q[1]))%2 == 0:
            S.append(q[2])
        else:
            S.appendleft(q[2])
if rev_cnt%2 == 0:
    print("".join(S))
else:
    print("".join(list(reversed(S))))

Solution (Python):
s=list(input())
top=bool()
t=[]
u=[]
n=int(input())
for i in range(n):
  q=list(map(str,input().split()))
  if q[0]=='1':
    top=not top
  else:
    if (q[1]=="1") ^ top:
      t.append(q[2])
    else:
      u.append(q[2])
s=list(reversed(t))+s+u
if top:
  print(*list(reversed(s)),sep='')
else:
  print(*s,sep='')

Solution (Python):
from collections import deque
s = input()
n = int(input())
q_list = [input().split() for _ in range(n)]
d = deque(list(s))
r_flag = False
for q in q_list:
    if q[0] == "1":
        r_flag = not r_flag
    elif q[0] == "2":
        if q[1] == "1":
            if r_flag:
                d.append(q[2])
            else:
                d.appendleft(q[2])
        elif q[1] == "2":
            if r_flag:
                d.appendleft(q[2])
            else:
                d.append(q[2])
if r_flag:
    d.reverse()
print("".join(d))

Solution (Python):
s = input()
sgn = 1
Q = int(input())
L = '' 
R = '' 
for _ in range(Q):
  i = input()
  if i[0] == '1':
    sgn*=-1
  else:
    if i[2] == '1':
      if sgn == 1:
        L += i[4]
      else:
        R += i[4]
    else:
      if sgn == 1:
        R += i[4]
      else:
        L += i[4]

if sgn == -1:
  s = s[::-1]
  R = R[::-1]
  print(R + s +L)
  
else:
  L = L[::-1]
  print(L + s + R)

Solution (Python):

from collections import deque


def main():
    d = deque()
    d.extend(list(input()))

    Q = int(input())
    reflag = False

    for i in range(Q):
        query = list(input().split())
        if query[0] == '1':
            reflag = not reflag
        elif (query[1] == '1' and not reflag) or (query[1] == '2' and reflag):
            d.appendleft(query[2])
        else:
            d.append(query[2])

    if reflag:
        print("".join(reversed(d)))
    else:
        print("".join(d))


if __name__ == "__main__":
    main()

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

s = deque(input().strip())
q=int(input())
count = 0
for i in range(q):
    L = list(input().rstrip().split())    
    if L[0]=='1':
        count += 1
    else:
        a,b,c = L
        b = int(b)
        if count&1==1:
            b=3-b
        if b==2:
            s.append(c)
        else:
            s.appendleft(c)
if count&1==1:
    print("".join(list(s)[::-1])) 
else:
    print("".join(s))

Solution (Python):
from collections import deque

S = input()
d = deque(S)
Q = int(input())
is_order = True
for i in range(Q):
    tmp = input()
    if len(tmp) == 1:
        t = 1
        is_order = not is_order
    else:
        t,f,c = tmp.split()
        f = int(f)
        if f == 1:
            if is_order:
                d.appendleft(c)
            else:
                d.append(c[::-1]) 
        else:
            if is_order:
                d.append(c)
            else:
                d.appendleft(c[::-1])
if is_order:
    print("".join(d))
else:
    print("".join(reversed(d)))

Solution (Python):
from collections import deque

s=deque(list(input()))
flag=0
for i in range(int(input())):
  dir=input().split()
  if int(dir[0])==1:flag+=1
  else:
    if int(dir[1])==1:
      if flag%2==0:s.appendleft(dir[2])
      else:s.append(dir[2])
    else:
      if flag%2==0:s.append(dir[2])
      else:s.appendleft(dir[2])
s=list(s)
if flag%2==1:s.reverse()
print(*s,sep='')

Solution (Python):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines


s = readline().decode().rstrip()
q = int(readline())

cnt1 = 0
t2 = []
for _ in range(q):
    t = list(readline().decode().rstrip().split())
    t2.append(t)
    if t[0] == '1':
        cnt1 += 1

if cnt1 % 2 != 0:
    s = s[::-1]
s1 = []
s2 = []

for t in t2:
    if t[0] == '1':
        cnt1 -= 1
    else:
        if cnt1 % 2 == 0 and t[1] == '1':
            s1.append(t[2])
        elif cnt1 % 2 != 0 and t[1] == '2':
            s1.append(t[2])
        elif cnt1 % 2 == 0 and t[1] == '2':
            s2.append(t[2])
        else:
            s2.append(t[2])

print(''.join(s1[::-1])+s+''.join(s2))

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

st = True
deq = deque(S)
for _ in range(Q):
    Query = input().split()
    if Query[0] == '1':
        st = not st
    else:
        if (Query[1] == '1') ^ st:
            deq.append(Query[2])
        else:
            deq.appendleft(Query[2])
if st:
    print(''.join(deq))
else:
    print(''.join(reversed(deq)))

Solution (Python):
from collections import deque

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

deq = deque(s)
pol = 0
for _ in range(q):
    query = input()
    if query[0] == '1':
        pol ^= 1
        continue
    _, f, c = query.split()
    op = int(f) - 1
    if op ^ pol:
        deq.append(c)
    else:
        deq.appendleft(c)

if pol:
    print(''.join(reversed(deq)))
else:
    print(''.join(deq))

Solution (Python):
from collections import deque

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

queue = deque(list(S))

r = False
for _ in range(Q):
    query = input()
    if query == '1':
        r ^= True
    else:
        _, f, c = query.split()
        front = f == '1'

        if r ^ front:
            queue.appendleft(c)
        else:
            queue.append(c)

if r:
    ans = ''.join(list(queue)[::-1])
else:
    ans = ''.join(queue)

print(ans)

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

r = False
prefix = []
suffix = []
for _ in range(Q):
    query = input()
    if query == '1':
        r ^= True
    else:
        _, f, c = query.split()
        front = f == '1'

        if r ^ front:
            prefix.append(c)
        else:
            suffix.append(c)

if r:
    S = ''.join(suffix[::-1]) + S[::-1] + ''.join(prefix)
else:
    S = ''.join(prefix[::-1]) + S + ''.join(suffix)

print(S)

Solution (Python):
from collections import deque



S = input()

q = deque()
for s in list(S):
    q.append(s)

Q = int(input())

direction = True

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

    
    if line == "1":
        direction = not direction

    else:
        if (direction and line[2] == '1') or (not direction and line[2] == '2'):
            
            q.appendleft(line[4])
        else:
            
            q.append(line[4])

if direction:
    print("".join(q))
elif not direction:
    q.reverse()
    print("".join(q))

Solution (Python):
S = input()
Q = int(input())
now = 1
head = ''
last = ''
for _ in range(Q):
    query = input().split()
    if query[0] == '1':
        now = now * -1
    if query[0] == '2':
        if now * (int(query[1])-1.5) == 0.5:
            last += query[2]
        else:
            head += query[2]

ans = head[::-1] + S + last
if now == 1:
    print(ans)
else:
    print(ans[::-1])

Solution (Python):
s = input()
q = int(input())
from collections import deque
f_s = deque([])
b_s = deque([])
fwd = 1
for i in range(q):
    qs = list(input().split())
    if qs[0] == '1':
        fwd ^= 1
    elif qs[1] == '1':
        if fwd:
            f_s.appendleft(qs[2])
        else:
            b_s.append(qs[2])
    else:
        if fwd:
            b_s.append(qs[2])
        else:
            f_s.appendleft(qs[2])
print("".join(f_s)+s+"".join(b_s) if fwd else ("".join(f_s)+s+"".join(b_s))[::-1])

Solution (Python):
from collections import deque
S = input()
q = deque()
for s in list(S):
    q.append(s)
Q = int(input())
direction = True
for _ in range(Q):
    line = input()
    if line[0] == '1':
        direction = not direction
    else:
        if (direction and line[2] == '1') or (not direction and line[2] == '2'):
            q.appendleft(line[4])
        else:
            q.append(line[4])
print("".join(q) if direction else "".join(reversed(q)))

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

s = input().strip()
qq = int(input())

rev = False
pre = ''
suf = ''
for _ in range(qq):
    q = input().strip()
    if q[0] == '1':
        rev = not rev
    else:
        c = q[4]
        if not rev:
            if q[2] == '1':
                pre += c
            else:
                suf += c
        else:
            if q[2] == '1':
                suf += c
            else:
                pre += c
ans = pre[::-1] + s + suf
if rev:
    ans = ans[::-1]
print(ans)

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

c = input()
c = c[:len(c)-1]
q = int(input())

num = len(c)
c_mae = ''
c_ato = ''

hantei = 1

for i in range(q):
    q_i= list(input())
    
    if q_i[0] == '1':
        hantei *= -1
    elif (q_i[2] =='1' and hantei == 1) or (q_i[2] == '2' and hantei == -1):
        c_mae = q_i[4] + c_mae
    else:
        c_ato += q_i[4]

print(c_mae+c+c_ato if hantei == 1 else (c_mae+c+c_ato)[::-1])

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

SS =deque(list(s))
rev_count = 0

for i in range(Q):
    que = input()
    if que[0] == '1':
        rev_count +=1
    elif que[0] == '2':
        if (rev_count + int(que[2]))%2 == 0:
            SS.append(que[4])
        else:
            SS.appendleft(que[4])

print("".join(SS) if rev_count %2 == 0 else ("".join(SS))[-1::-1])

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

rev_count = 0
sbf=''
saf=''
for i in range(Q):
    que = input()
    if que[0] == '1':
        rev_count +=1
    elif que[0] == '2':
        if (rev_count + int(que[2]))%2 == 0:
            saf = saf + que[4]
        else:
            sbf = que[4] + sbf

print(sbf+s+saf if rev_count %2 == 0 else (sbf+s+saf)[-1::-1])

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

rev_count = 0
sbf=''
saf=''
for i in range(Q):
    que = input()
    if que[0] == '1':
        rev_count +=1
    elif que[0] == '2':
        if (rev_count + int(que[2]))%2 == 0:
            saf = saf + que[4]
        else:
            sbf = que[4] + sbf

print(sbf+s+saf if rev_count %2 == 0 else (sbf+s+saf)[-1::-1])

Solution (Python):

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

from collections import deque

s = deque(s)

rev = 1
for _ in range(q):
    Q = list(input().split())
    if Q[0] == "1":
        rev *= -1
    elif Q[0] == "2":
        if (Q[1]=="1" and rev==1) or (Q[1]=="2" and rev==-1):
            s.appendleft(Q[2])
        elif (Q[1]=="2" and rev==1) or (Q[1]=="1" and rev==-1):
            s.append(Q[2])
            
s = "".join(s)
            
if rev == -1:
    s = s[::-1]

print(s)

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

is_forward = True

q = int(input())
for i in range(q):
    query = input()
    if query[0] == "1":
        is_forward = is_forward ^ 1
    elif query[0] == "2":
        _, f, c = query.split()
        f = int(f)
        f %= 2
        is_end = f ^ is_forward
        if is_end:
            moji.append(c)
        else:
            moji.appendleft(c)

if is_forward == False:
    moji = list(moji)[::-1]
print("".join(moji))

Solution (Python):
def main():
    s = input()
    q = int(input())
    is_fwd = True
    before = ''
    after = ''

    for i in range(q):
        fields = [i for i in input().split()]
        t = int(fields[0])
        if t == 1:
            is_fwd = not is_fwd
        elif t == 2:
            f = int(fields[1])
            c = fields[2]
            if (f == 1) == is_fwd:
                before += c
            else:
                after += c

    res = before[::-1] + s + after
    if is_fwd:
        print(res)
    else:
        print(res[::-1])

main()

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

from collections import*
D=deque(S)
count=0
for _ in range(N):
    *Q,=input().split()
    if len(Q)==1:
        count+=1
    else:
        if (count+int(Q[1]))%2:
            D.appendleft(Q[2])
        else:
            D.append(Q[2])
ans=''.join(D)
print(ans[::-1] if count%2 else ans)

Solution (Python):
s=input()
l1=[]
l2=[]
num1=0 
def ap1(str1):
    l1.append(str1)
def ap2(str2):
    l2.append(str2)
nq=int(input())
for i in range(nq):
    q=list(input().split())
    if q[0]=="1":
        num1=(num1+1)%2
    else:
        f=int(q[1])
        if (num1+f)%2==1: 
            ap1(q[2])
        else:
            ap2(q[2])
if num1==0:
    ans=""
    for i in range(len(l1)):
        ans+=l1[-1-i]
    ans+=s
    for i in range(len(l2)):
        ans+=l2[i]
else:
    ans=""
    for i in range(len(l2)):
        ans+=l2[-1-i]
    for i in range(len(s)):
        ans+=s[-1-i]
    for i in range(len(l1)):
        ans+=l1[i]
print(ans)

Solution (Python):
S=input()
Q=int(input())
rev = 0
front, back=[],[]
for i in range(Q):
  q = input()
  if len(q)==1:rev = 1- rev
  else:
    n,f,c=q.split()
    if f=='1' and rev==0:
      front.append(c)
    elif f=='2' and rev==1:
      front.append(c)
    elif f=='1' and rev==1:
      back.append(c)
    elif f=='2' and rev == 0:
      back.append(c)

if rev==0:
  S = ''.join(front[::-1]) + S + ''.join(back)
else:
  S = ''.join(back[::-1]) + S[::-1] + ''.join(front)
print(S)

Solution (Python):
s=input()
n = int(input())
s1=""
s2=""
t=0
for i in range(n):
    m=input().split()
    if len(m)==1:
        t=1-t
    else:
        if t==0:
            if int(m[1])==2:
                s2+=m[2]
            else:
                s1+=m[2]
        else:
            if int(m[1])==2:
                s1+=m[2]
            else:
                s2+=m[2]
    
if t==0:
    s3=s1[::-1]
    print(s3+s+s2)
else:
    s=s[::-1]
    s2=s2[::-1]
    print(s2+s+s1)

Solution (Python):
s=input()
q=int(input())
f=""
b=""
r=0
for i in range(q):
  query=input().split()
  if int(query[0])==1:
    r=1-r
  else:
    if (r^(int(query[1])-1))==0:
      f+=query[2]
    else:
      b+=query[2]
ret=f[::-1]+s+b
if r:
  ret=ret[::-1]
print(ret)

Solution (Python):
import sys
from collections import deque

input = sys.stdin.readline

S = deque(input()[:-1])
reverse_flg = 0
Q = int(input())
qs = [input().split() for _ in range(Q)]
for q in qs:
    if q[0] == '1':
        reverse_flg = reverse_flg ^ 1
    else:
        flg = (int(q[1]) + reverse_flg) % 2
        if flg == 1:
            S.appendleft(q[2])
        else:
            S.append(q[2])

if reverse_flg:
    S.reverse()
print(''.join(S))

Solution (Python):
import sys
from collections import deque

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def main():
    S = readline().strip()
    Q = int(readline())
    
    dq = deque(S)
    flipped = False
    for _ in range(Q):
        A = readline().split()
        if A[0] == '1':
            flipped = not flipped
        elif A[1] == '1' and not flipped or A[1] == '2' and flipped:
            dq.appendleft(A[2])
        else:
            dq.append(A[2])
    
    ans = ''.join(reversed(dq) if flipped else dq)
    
    print(ans)
    return


if __name__ == '__main__':
    main()

Solution (Python):
T_REVERT = "1"
T_APPEND = "2"
F_BEFORE = "1"
F_AFTER = "2"

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

before = []
after = []
is_revert = False


for i in range(Q):
  q = input().split(" ")
  if q[0] == T_REVERT:
    is_revert = not is_revert
    continue
  if (q[1] == F_BEFORE) ^ is_revert:
    before.append(q[2])
  else:
    after.append(q[2])


ans_s = ""
if not is_revert:
  print("".join(before[::-1]) + S + "".join(after))
else:
  print("".join(after[::-1]) + S[::-1] + "".join(before))

Solution (Python):
import sys

rr = lambda: sys.stdin.readline().rstrip()
rs = lambda: sys.stdin.readline().split()
ri = lambda: int(sys.stdin.readline())

flag = False
s = rr()
q = ri()
front = ''
end = ''
for _ in range(q):
    query = list(rs())
    if query[0] == '1':
        flag = not flag
        continue
    else:
        f = int(query[1])
        c = query[-1]
        if flag == True:
            if f == 1:
                end += c
            else:
                front = c+front
        else:
            if f == 1:
                front = c+front
            else:
                end += c
if flag:
    print((front+s+end)[::-1])
else:
    print(front+s+end)

Solution (Python):
import sys

rr = lambda: sys.stdin.readline().rstrip()
rs = lambda: sys.stdin.readline().split()
ri = lambda: int(sys.stdin.readline())

flag = False
s = rr()
q = ri()
front = ''
end = ''
for _ in range(q):
    query = list(rs())
    if query[0] == '1':
        flag = not flag
        continue
    else:
        f = int(query[1])
        c = query[-1]
        if flag == True:
            if f == 1:
                end += c
            else:
                front = c+front
        else:
            if f == 1:
                front = c+front
            else:
                end += c
if flag:
    print((front+s+end)[::-1])
else:
    print(front+s+end)

Solution (Python):
import sys
from collections import deque

sys.setrecursionlimit(10**7)
input = sys.stdin.readline

s = input().rstrip()
q = int(input())
d = deque()
d.append(s)

flag = False
for i in range(q):
    qq = input().split()
    if qq[0] == '1':
        flag = not(flag)
    else:
        if flag:
            if qq[1] == '1':
                d.append(qq[2])
            else:
                d.appendleft(qq[2])
        else:
            if qq[1] == '1':
                d.appendleft(qq[2])
            else:
                d.append(qq[2])

ans = ''.join(list(d))
if flag:
    print(ans[::-1])
else:
    print(ans)

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

Solution (Python):
from collections import deque

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

d = deque(S)
R = 0

for i in range(Q):
    Query = str(input())
    if len(Query) == 1:
        R = (R+1)%2
    else:
        T,F,C = map(str,Query.split())

        if (R + int(F)) % 2 == 1:
            d.appendleft(C)
        else:
            d.append(C)

if R == 1:
    d.reverse()

for i in range(len(d)):
    print(d[i],end="")

Solution (Python):
S = input()
Q = int(input())
Query = [input() for _ in range(Q)]
inv = False
add = ['','']
for query in Query:
    if query[0]=='1':
        inv = not inv
    else:
        add[(query[2] == '1') ^ inv] += query[-1]
ans = add[1][::-1] + S + add[0]
if inv:
    ans = ans[::-1]
print(ans)