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

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
printV = lambda x: print(*x, sep="\n")
printH = lambda x: print(" ".join(map(str,x)))
def IS(): return sys.stdin.readline()[:-1]
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 LI1(): return list(map(int1, sys.stdin.readline().split()))
def LII(rows_number): return [II() for _ in range(rows_number)]
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def LLI1(rows_number): return [LI1() for _ in range(rows_number)]

def main():
	S = IS()
	Q = II()
	c = 0
	head=[]
	tail=[]
	for _  in range(Q):
		query = IS()
		if(len(query)==1):
			c = (c + 1)%2
		else:
			T,F,C = query.split()
			T = int(T)
			F = int(F)
			if F==1:
				if(c==0):
					head.append(C)
				else:
					tail.append(C)
			else:
				if(c==1):
					head.append(C)
				else:
					tail.append(C)
	if c == 0:
		head = ''.join(head[::-1])
		tail = ''.join(tail)
		ans = head+S+tail
	else:
		head = ''.join(head)
		tail = "".join(tail[::-1])
		ns = S[::-1]
		ans = tail+ns+head
	print(ans)




if __name__ == '__main__':
	main()

Solution (Python):

from collections import deque

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

a = deque(s)
reverse = False

for i in range(q):
    query = input()
    if query == "1":
        if reverse:
            reverse = False
        else:
            reverse = True
    else:
        x, q2_1, q2_2 = query.split()
        if q2_1 == "1":
            if reverse:
                a.append(q2_2)
            else:
                a.appendleft(q2_2)
        else:
            if reverse:
                a.appendleft(q2_2)
            else:
                a.append(q2_2)

s = list(a)
if reverse:
    s = s[::-1]
    print("".join(s))
else:
    print("".join(s))

Solution (Python):

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

lim = 10**5 *2
buf = [None for _ in range(lim)] + S + [None for _ in range(lim)]
forward_i, back_i = lim-1, lim + len(S) + 1
is_reversed = False
for _ in range(Q):
    token = input().split(' ')
    if len(token) == 1:
        is_reversed = ~is_reversed

    else:
        _, f, c = token
        f = int(f)
        if (f == 1 and is_reversed) or (f == 2 and not is_reversed):
            buf[back_i] = c
            back_i += 1
            

        else:
        
            buf[forward_i] = c
            forward_i -= 1
            

S = ''.join([b for b in buf if b is not None])
if is_reversed:
    print(''.join(reversed(list(S))))
    
    

else:
    print(S)

Solution (Python):
from collections import deque

S = deque(input())
Q = int(input())
count = 0
for i in range(Q):
    input_list = input().split()
    if input_list[0] == "1":
        count += 1
        continue

    if count % 2 == 0:  
        if input_list[1] == "1":
            S.appendleft(input_list[2])
        else:
            S.append(input_list[2])
    else:
        if input_list[1] == "2":
            S.appendleft(input_list[2])
        else:
            S.append(input_list[2])

if count % 2 == 0:
    ans = ''.join(S)
    print(ans)
else:
    S.reverse()
    ans = ''.join(S)
    print(ans)

Solution (Python):
s=list(input())
q=int(input())
sc=0
lhs=[]
rhs=[]
for _ in range(q):
  query=input()
  if query=="1":
    sc+=1
  else:
    t,f,c=query.split()
    if f=="1":
      if sc%2==0:
        lhs.append(c)
      else:
        rhs.append(c)
    else:
      if sc%2==0:
        rhs.append(c)
      else:
        lhs.append(c)

lhs.reverse()
s=lhs+s+rhs
if sc%2!=0:
  s.reverse()
print("".join(s))

Solution (Python):
from collections import deque


S = deque(input())
Q = int(input())
queries = []
for _ in range(Q):
    queries.append(input())

is_reverse = False

for q in queries:
    ql = list(q.split())
    if len(ql) == 1:
        is_reverse = not is_reverse
    else:
        if ql[1] == '1': 
            if not is_reverse:
                S.appendleft(ql[2])
            else:
                S.append(ql[2])
        else: 
            if not is_reverse:
                S.append(ql[2])
            else:
                S.appendleft(ql[2])


if not is_reverse:
    print(''.join(list(S)))
else:
    print(''.join(list(S)[::-1]))

Solution (Python):
s=list(input())
q=int(input())
ans=0
mae=[]
ato=[]
for i in range(q):
    query=input().split()
    if query==['1']:
        ans+=1
    else:
        if query[1]=='1' and ans%2==0:
            mae.append(query[2])
        elif query[1]=='1' and ans%2==1:
            ato.append(query[2])
        elif query[1]=='2' and ans%2==0:
            ato.append(query[2])
        else:
            mae.append(query[2])
if ans%2==1:
    s.reverse()
    ato.reverse()
    s=ato+s+mae
else:
    mae.reverse()
    s=mae+s+ato
print(''.join(s))

Solution (Python):
from collections import deque

S = list(input())
d = deque(S)
Q = int(input())
r_flag = 0

def top(d,C,r_flag):
    if r_flag:
        d.append(C)
    else:
        d.appendleft(C)

def bottom(d,C,r_flag):
    if r_flag:
        d.appendleft(C)
    else:
        d.append(C)

for i in range(Q):
    tmp = list(input().split())
    if tmp[0] == "1":
        if r_flag == 0:
            r_flag = 1
        else:
            r_flag = 0
    else:
        if tmp[1] == "1":
            top(d,tmp[2],r_flag)
        else:
            bottom(d,tmp[2],r_flag)

d = list(d)
if r_flag:
    d.reverse()
print("".join(d))

Solution (Python):
from collections import deque
S = input()
aaa = list(S)
aaa.reverse()
sss = "".join(aaa)

Q = int(input())
d = deque()
d.append(S)
rev = 1
cnt = 0

for i in range(Q):
  List = list(input().split())
  a = int(List[0])
  if a == 1:
    rev = rev * -1
  else:
    b = int(List[1])
    if (b == 1 and rev == 1) or (b == 2 and rev == -1):
      d.appendleft(List[2])
      cnt = cnt + 1
    else:
      d.append(List[2])

if rev == -1:
  d[cnt] = sss
  d.reverse()
string = "".join(list(d))
print(string)

Solution (Python):
S = input()
Q = int(input())
is_turned = False
head = []
tail = []
for _ in range(Q):
    tmp = input().split()
    if len(tmp) == 1:
        is_turned = not is_turned
        continue
    f, c = int(tmp[1]), tmp[2]
    
    if not is_turned:
        if f == 1:
            head.append(c)
        else:
            tail.append(c)
    else:
        if f == 1:
            tail.append(c)
        else:
            head.append(c)

if not is_turned:
    
    head = ''.join(reversed(head))
    tail = ''.join(tail)
    ans = head + S + tail
else:
    S = ''.join(reversed(S))
    head = ''.join(head)
    tail = ''.join(reversed(tail))
    ans = tail + S + head

print(ans)

Solution (Python):
from collections import deque

S = input()
Q = int(input())
ans = deque()
ans.append(S)
turncnt = 0
for i in range(Q):
    query = input().split()
    if (query[0] == "1"):
        turncnt = turncnt + 1
    if (query[0] == "2"):
        if (query[1] == "1"):
            if (turncnt % 2 == 0):
                ans.appendleft(query[2])
            else:
                ans.append(query[2])
        if (query[1] == "2"):
            if (turncnt % 2 == 0):
                ans.append(query[2])
            else:
                ans.appendleft(query[2])

anstr = "".join(ans)
if (turncnt % 2 == 1):
    anstr = anstr[::-1]
print(anstr)

Solution (Python):
s=list(input())
import collections 
D=collections.deque(s)
counter=0
lastindex=0
q=int(input())
for i in range(q):
    a=list(input().split())
    if int(a[0])==1:
        counter+=1

    else:
        if (counter+int(a[1]))%2==0:
            D.append(a[2])
        else:
            D.appendleft(a[2])
if counter%2==0:
    D=list(D)
    print("".join(D))
else:
    D=list(D)[::-1]
    print("".join(D))

Solution (Python):
import collections

def read():
    S = input().strip()
    Q = int(input().strip())
    queries = list()
    for i in range(Q):
        query = input().strip().split()
        t = int(query[0])
        if t == 1:
            queries.append((t, -1, "*"))
        if t == 2:
            f = int(query[1])
            c = query[2]
            queries.append((t, f, c))
    return S, Q, queries

def solve(S, Q, queries):
    head = collections.deque()
    tail = collections.deque()
    is_fliped = False
    for t, f, c in queries:
        if t == 1:
            is_fliped = not is_fliped
        if t == 2:
            is_head = (f == 1) ^ is_fliped
            if is_head:
                head.appendleft(c)
            else:
                tail.append(c)
    s = ''.join(head) + S + ''.join(tail)
    if is_fliped:
        s = ''.join(reversed(s))
    return s


if __name__ == '__main__':
    inputs = read()
    print("{}".format(solve(*inputs)))

Solution (Python):
from collections import deque
def solve(S, Q):
    S = deque(S)
    flg = 0
    for _ in range(Q):
        que = input().split()
        if len(que) == 1:
            flg += 1
        else:
            if flg % 2 == 0:
                if que[1] == '1':
                    S.extendleft(que[2])
                elif que[1] == '2':
                    S.extend(que[2])
            elif flg % 2 == 1:
                if que[1] == '1':                 
                    S.extend(que[2])
                elif que[1] == '2':                
                    S.extendleft(que[2])
    S = ''.join(S)
    if flg % 2 == 1:
        S = S[::-1]
    print(S)

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

if __name__ == "__main__":
    main()

Solution (Python):
S=input()
Q=int(input())
inv=False
head=""
last=""
for i in range(Q):
    qu=input().split(" ")
    if int(qu[0])==1:
        inv=not inv
    else:
        F=int(qu[1])
        if inv:
            F=3-F
        if F==1:
            head+=qu[2]
        else:
            last+=qu[2]

if inv:
    print(last[::-1]+S[::-1]+head)
else:
    print(head[::-1]+S+last)

Solution (Python):
from collections import deque

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



pop_flag = True
S_deque = deque(S)
for i in range(Q):
  Querie = [str(j) for j in input().split()]

  if Querie[0] == "1":
    if pop_flag == True:
      pop_flag = False
    elif pop_flag == False:
      pop_flag = True


  elif Querie[0] == "2":
    if Querie[1] == "1":
      if pop_flag == True:
        S_deque.appendleft(Querie[2])
      elif pop_flag == False:
        S_deque.append(Querie[2])
    elif Querie[1] == "2":
      if pop_flag == True:
        S_deque.append(Querie[2])
      elif pop_flag == False:
        S_deque.appendleft(Querie[2])

if pop_flag == False:
  S_deque.reverse()

ans = ''.join(S_deque)

print(ans)

Solution (Python):
import collections
S = collections.deque(list(input()))
N = int(input())
parity = True
for i in range(N):
  query = input().split()
  if query[0] == "1":
    if parity == True:
      parity = False
    else:
      parity = True
  if query[0] == "2":
    if parity:
      if query[1] == "1":
        S.appendleft(query[2])
      else:
        S.append(query[2])
    else:
      if query[1] == "1":
        S.append(query[2])
      else:
        S.appendleft(query[2])

Sstr = ""
if parity:
  for i in range(len(S)):
    Sstr = Sstr + S.popleft()
else:
  for i in range(len(S)):
    Sstr = Sstr + S.pop()
print(Sstr)

Solution (Python):
s=input()
t=int(input())
u=[input().split()for i in range(t)]
a=0
b=''
c=''
for i in range(t):
  if u[i][0]=='1':
    a+=1
  else:
    if u[i][1]=='1':
      if a%2==0:
        b=b+u[i][2]
      else:
        c=c+u[i][2]
    else:
      if a%2==0:
        c=c+u[i][2]
      else:
        b=b+u[i][2]
if a%2==0:
  b=b[::-1]
  s=b+s+c
  print(s)
else:
  s=s[::-1]
  c=c[::-1]
  s=c+s+b
  print(s)

Solution (Python):
import sys
from io import StringIO
import unittest

doTest=0

def yn(n):
    if n==1:
        print("Yes")
    else:
        print("No")
    return

def resolve():
    readline=sys.stdin.readline


    s=readline().strip()
    q=int(readline().strip())
    hed=""
    btm=""
    rev=0
    for i in range(q):
        x=list(map(str, readline().strip().split()))
        if x[0]=="1": 
            if rev==0:
                rev=1
            else:
                rev=0
        elif x[0]=="2":
            if x[1]=="1": 
                if rev==0:
                    hed = x[2] + hed
                else:
                    btm = btm + x[2]
            else: 
                if rev==0:
                    btm = btm + x[2]
                else:
                    hed = x[2] + hed
    if rev==0:
        print(hed,end="")
        print(s,end="")
        print(btm)
    else:
        print(''.join(list(reversed(btm))),end="")
        print(''.join(list(reversed(s))),end="")
        print(''.join(list(reversed(hed))))
    return

if doTest==0:
    resolve()
    sys.exit()

Solution (Python):
s = input()
q = int(input())
st = 0
str1 = ""
str2 = ""
for _ in range(q):
    query = list(input().split())
    if query[0] == "1":
        st = 1 if st == 0 else 0
    else:
        if query[1] == "1":
            if st == 0:
                str1 += query[2]
            else:
                str2 += query[2] 
        else:
            if st == 0:
                str2 += query[2] 
            else:
                str1 += query[2] 

if st == 0:
    ans = str1[::-1] + s + str2
else:
    ans = str2[::-1] + s[::-1] + str1
    
print(ans)

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

rev = 1
ans = deque(list(S))
for i in range(Q):
    if info[i][0] == '1':
        rev *= -1
    else:
        if rev == 1:
            if info[i][1] == '1':
                ans.appendleft(info[i][2])
            else:
                ans.append(info[i][2])
        else:
            if info[i][1] == '1':
                ans.append(info[i][2])
            else:
                ans.appendleft(info[i][2])

if rev == -1:
    ans.reverse()


print(''.join(ans))

Solution (Python):




S = input()

Q = int(input())
hanten = 0

ato = []
mae = []

for i in range(Q):
    li = input().split()

    one = int(li[0])
    if one == 1:
        hanten += 1
    else:
        F = int(li[1])
        C = li[2]
        if (hanten + F) % 2 == 1:
            mae.append(C)
        else:
            ato.append(C)

if hanten % 2 == 0:
    mae.reverse()
    print("".join(mae) + S + "".join(ato))
else:
    ato.reverse()
    S1 = "".join(list(reversed(S)))

    print("".join(ato) + S1 + "".join(mae))

Solution (Python):
S = input()
Q = int(input())
QUERY = []
for i in range(Q):
    
    q = input().split()
    if q[0] == '1':
        QUERY.append([int(q[0])])
    else:
        QUERY.append([int(q[0]), int(q[1]), q[2]])

st = []
en = []
rev = False
rev_c = 0
for q in QUERY:
    if q[0] == 1:
        if rev:
            rev = False
        else:
            rev = True
        rev_c += 1
    else:
        if q[1] == 1:
            if not rev:
                st.append(q[2])
            else:
                en.append(q[2])
        else:
            if not rev:
                en.append(q[2])
            else:
                st.append(q[2])




st_s = "".join(st)
st_s = st_s[::-1]
en_s = "".join(en)

ans = st_s + S + en_s
if rev_c % 2 == 0:
    pass
else:
    ans = ans[::-1]

print(ans)

Solution (Python):
from collections import deque
s = list(input())
s = deque(s)
n = int(input())
cnt = 0
for i in range(n):
    q = list(map(str,input().split()))
    if q[0] == '1':
        cnt += 1
    else:
        if cnt % 2 == 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 cnt % 2 != 0:
    s.reverse()
print(''.join(list(s)))

Solution (Python):
import collections


def resolve():
    s = input()
    n = int(input())
    d = collections.deque()
    d.appendleft(s)

    rev = False
    for _ in range(n):
        q = list(input().split())

        if len(q) == 1:
            rev = not rev

        else:
            if q[1] == '1':
                las = False

            else:
                las = True

            if rev:
                las = not las

            if las:
                d.append(q[2])

            else:
                d.appendleft(q[2])

    dstr = ''.join(d)

    if rev:
        print(dstr[::-1])

    else:
        print(dstr)
        
resolve()

Solution (Python):


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

inputs = []
n_reverse = 0
for i in range(q):
    input_ = input()
    if input_[0] == "1":
        n_reverse += 1
    inputs.append(input_)

if n_reverse % 2 == 0:
    final_rev = False
else:
    final_rev = True

rev = False
mae = ""
ushiro = ""
for input_ in inputs:
    elms = input_.split(" ")
    t = int(elms[0])
    if elms[0] == "1":
        rev = not rev
    else:
        f = int(elms[1])
        c = elms[2]

        if f == 1:
            if final_rev:
                if rev:
                    mae = c + mae
                else:
                    ushiro = ushiro + c
            else:
                if rev:
                    ushiro = ushiro + c
                else:
                    mae = c + mae
        else:
            if final_rev:
                if rev:
                    ushiro = ushiro + c
                else:
                    mae = c + mae
            else:
                if rev:
                    mae = c + mae
                else:
                    ushiro = ushiro + c

if final_rev:
    s = s[::-1]

out = mae + s + ushiro
print(out)

Solution (Python):
S = input()
Q = int(input())
count = 0
right = ''
left = ''
for i in range(Q):
    arr = list(input().split())
    if len(arr) == 1:
        count += 1
    else:
        if arr[1] == '1':
            if count%2==0:
                left = arr[2] + left
            else:
                right = right + arr[2]
        else:
            if count%2!=0:
                left = arr[2] + left
            else:
                right = right + arr[2]
S = left + S + right
if count%2 == 0:
    print(S)
else:
    print(S[::-1])

Solution (Python):
import sys
sys.setrecursionlimit(10**6)
from math import floor,ceil,sqrt,factorial,log
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict,deque
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
from copy import deepcopy
from operator import itemgetter
from fractions import gcd
mod = 10 ** 9 + 7
inf = float('inf')
ninf = -float('inf')
 

def ii(): return int(sys.stdin.readline().rstrip()) 
def mii(): return map(int,sys.stdin.readline().rstrip().split())
def limii(): return list(mii()) 
def lin(n:int): return [ii() for _ in range(n)]
def llint(n: int): return [limii() for _ in range(n)]

def ss(): return sys.stdin.readline().rstrip() 
def mss(): return sys.stdin.readline().rstrip().split()
def limss(): return list(mss()) 
def lst(n:int): return [ss() for _ in range(n)]
def llstr(n: int): return [limss() for _ in range(n)]





s=ss()
q=ii()

arr=deque(list(s))
chk=0
for i in range(q):
    x=input()
    if x=="1":
        if chk==1:
            chk=0
        else:
            chk=1
    
    else:
        j,k,l=map(str,x.split())

        if chk==0 and k=="1":
            arr.extendleft(deque(list(l)))
            
        elif chk==0 and k=="2":
            arr.extend(deque(list(l)))
        elif chk==1 and k=="1":
            arr.extend(deque(list(reversed(list(l)))))
        elif chk==1 and k=="2":
            arr.extendleft(deque(list(reversed(list(l)))))

if chk==0:
    print("".join(list(arr)))
else:
    print("".join(reversed(list(arr))))

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

flag = False
for _ in range(Q):
  query = input().split()
  if query[0] == '1':
    flag = not flag
  elif query[0] == '2':
    left = query[1] == '1'
    if flag:
      left = not left
    if left:
      S.appendleft(query[2])
    else:
      S.append(query[2])

if flag:
  print(''.join(reversed(S)))
else:
  print(''.join(S))

Solution (Python):
def main():
    s = input()
    q = int(input())
    flag = 0
    cnt = 0
    front = []
    back = []

    for i in range(q):
        t = input()
        if len(t) == 1:
            cnt += 1
            if flag == 0:
                flag = 1
            elif flag == 1:
                flag = 0
        else:
            _, a, b = (str(i) for i in t.split())
            if a == "1":
                if flag == 0:
                    front.append(b)
                elif flag == 1:
                    back.append(b)
            elif a == "2":
                if flag == 0:
                    back.append(b)
                elif flag == 1:
                    front.append(b)

    if cnt % 2 == 0:
        front = front[::-1]
        s = ''.join(front) + s + ''.join(back)
    else:
        back = back[::-1]
        s = ''.join(back) + s[::-1] + ''.join(front)

    print(s)

if __name__ == '__main__':
    main()

Solution (Python):
from sys import stdin
sin = stdin.readline().rstrip()
q = int(stdin.readline().rstrip())
li = [list(stdin.readline().rstrip().split()) for _ in range(q)]
point = 0
omote = ""
ura = ""
for i in li:
    if i[0] == "1":
        point += 1
    elif i[0] == "2":
        if i[1] == "1":
            if point%2 == 0:
                omote += i[2]
            else:
                ura += i[2]
        else:
            if point%2 == 0:
                ura += i[2]
            else:
                omote += i[2]
if point%2 == 0:
    omote = omote[::-1]
    sin = omote + sin + ura
    print(sin)
else:
    sin = sin[::-1]
    ura = ura[::-1]
    sin = ura + sin + omote
    print(sin)

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

    s_old = list(str(input()))
    q = int(input())
    pl_mi = 1
    pl_mi_2 = 1
    query = [list(input().split()) for i in range(q)]

    s = deque(s_old)
    s.pop()

    for i in range(q):
        if query[i][0] == "1":
            pl_mi = pl_mi * (-1)
            continue
        else:
            if query[i][1] == "1":
                pl_mi_2 = 1
            else:
                pl_mi_2 = -1
            if pl_mi * pl_mi_2 == 1:
                s.appendleft(query[i][2])
            else:
                s.append(query[i][2])
    
    if pl_mi == 1:
        s_new = "".join(list(s))
        print(s_new)
    else:
        s.reverse()
        s_new = "".join(list(s))
        print(s_new)




if __name__ == "__main__":
    main()

Solution (Python):
from collections import deque

s = list(input())
d = deque(s)



Q = int(input())
dir = 1


def func(f, c, dir):
    
    
    if dir == 1:
        if f == 1:
            d.appendleft(c)
        else:
            d.append(c)
    
    if dir == -1:
        if f == 1:
            d.append(c)
        else:
            d.appendleft(c)
    


for i in range(Q):
    q = input()
    if q[0] == "1":
        dir *= -1
    else:
        z, f, c = q.split()
        func(int(f), c, dir)

if dir == -1:
    d.reverse()
print(*d, sep="")

Solution (Python):
se=input()
q=int(input())
h=0
se1=""
se2=""
for i in range(q):
    s=input().split()
    if len(s)==1:
        h+=1
    elif len(s)==3:
        if (h+int(s[1])-1)%2==0:
            se1+=s[2]
        else:
            se2+=s[2]
r=se1[::-1]+se+se2
if h%2==0:
    print(r)
else:
    print(r[::-1])

Solution (Python):
s = input()
q = int(input())
ud = False
ud_count = 0
sub_s = ""
for i in range(q):
    query = list(input().split())
    if int(query[0]) == 1:
        ud = not(ud)
        ud_count += 1
    else:
        if (int(query[1])==1 and ud==False) or (int(query[1])==2 and ud==True):
            sub_s += query[2]
        else:
            s += query[2]
s = sub_s[::-1] + s
if ud_count%2:
    print(s[::-1])
else:
    print(s)

Solution (Python):
import sys
readline = sys.stdin.buffer.readline
from collections import deque
s = readline().rstrip().decode('utf-8')
q = int(readline())
flag = 0
lst1 = deque([])
lst2 = deque([])
for i in range(q):
    t = readline().rstrip().decode('utf-8')
    if t[0] == "1":
        
        flag = 1-flag
    else:
        com,f,c = t.split()
        if f == "1": 
            if not flag:
                lst1.append(c)
            else: 
                lst2.append(c)
            

        else: 
            if not flag:
                lst2.append(c)
            else:
                lst1.append(c)

if not flag:
    lst1.reverse()
    s = "".join(lst1) + s + "".join(lst2) 

    
else:
    lst2.reverse()
    s = s[::-1]
    s = "".join(lst2)+ s + "".join(lst1)



print(s)

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

def resolve():
    S = input()
    Q = int(input())
    Query = [input().split() for _ in range(Q)]

    S_deque = deque()
    for s in S:
        S_deque.append(s)
 
    is_reversed = False
    for q in Query:
        if q[0]=='1':
            is_reversed = not is_reversed
        elif q[0]=='2':
            if (not is_reversed and q[1]=='1') or (is_reversed and q[1]=='2'):
                S_deque.appendleft(q[2])
            else:
                S_deque.append(q[2])

    if is_reversed:
        S_deque.reverse()

    print(*S_deque, sep='')

if __name__ == '__main__':
    resolve()

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

before = []
after = []

for i in range(Q):
	query = input().split()
	if query == ["1"]:
		flag = (flag+1)%2
	else:
		if flag == 0:
			if query[1] == "1":
				before.append(query[2])
			else:
				after.append(query[2])
		else:
			if query[1] == "1":
				after.append(query[2])
			else:
				before.append(query[2])

ans = ""
if flag == 0:
	for c in reversed(before):
		print(c,end = "")
	print(s,end = "")
	for c in after:
		print(c,end = "")
else:
	for c in reversed(after):
		print(c,end = "")
	for c in reversed(s):
		print(c,end = "")
	for c in before:
		print(c,end = "")

Solution (Python):

import sys
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
read = sys.stdin.buffer.read
sys.setrecursionlimit(10 ** 7)

S = readline().strip().decode('utf-8')
Q = int(readline())

is_reversed = False

import collections
answer = collections.deque(S)
for _ in range(Q):
    l = readline().decode('utf-8').split()
    if len(l) == 1:
        is_reversed = not is_reversed
    else:
        _, F, C = map(str, l)
        if F == "1":
            if is_reversed:
                answer.append(C)
            else:
                answer.appendleft(C)
        else:
            if is_reversed:
                answer.appendleft(C)
            else:
                answer.append(C)

if is_reversed:
    answer.reverse()
print("".join(answer))

Solution (Python):
from collections import deque
a = input()
q = int(input())
key = [0]*len(a)
for i in range(len(a)):
    key[i] = a[i]

p = deque(key)
rev = False
for i in range(q):
    qinput = [str(x) for x in input().split()]
    if len(qinput) == 1:
        if rev:
            rev = False
        else:
            rev = True
    else:
        k = qinput[1]
        if k == "1":
            if rev:
                p.append(qinput[2])
            else:
                p.appendleft(qinput[2])
        else:
            if not rev:
                p.append(qinput[2])
            else:
                p.appendleft(qinput[2])
if rev:
    p.reverse()
print("".join(p))

Solution (Python):
S = input()
Q = int(input())
B = []
C = []
t = 1
for i in range(Q):
    A = input()
    if A=="1":
        t*=-1
    else:
        AL = list(A.split())
        if AL[1]=="1":
            if t==1:
                B.append(AL[2])
            else:
                C.append(AL[2])
        elif AL[1]=="2":
            if t==1:
                C.append(AL[2])
            else:
                B.append(AL[2])

B = ''.join(B)
C = ''.join(C)

if t==-1:
    print(C[::-1]+S[::-1]+B)
else:
    print(B[::-1]+S+C)

Solution (Python):
from collections import deque
S = input()
Q = int(input())
li = []
for i in range(Q):
    li.append(list(input().split()))
flag = False
sq = deque(S)
for i in range(Q):
    if li[i][0] == "1":
        flag = not flag
    else:
        if (li[i][1] == "1" and not flag) or (li[i][1] == "2" and flag):
            sq.appendleft(li[i][2])
        else:
            sq.append(li[i][2])
sq = list(sq)
if flag:
    sq.reverse()
for i in range(len(sq)):
    print(sq[i], end="")

Solution (Python):
import math
import itertools
import fractions
import heapq
import collections
import bisect
import sys
import copy

sys.setrecursionlimit(10**9)
mod = 10**7+9
inf = 10**20

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


sentou = ""
matubi = ""
reverse = False
for i in range(q):
    l = input().split()
    if l[0]=="1":
        reverse = not(reverse)
    else:
        if l[1]=="1":
            if reverse:
                matubi += l[2][::-1]
            else:
                sentou = l[2] + sentou
        else:
            if reverse:
                sentou = l[2][::-1] + sentou
            else:
                matubi += l[2]
    

if reverse:
    print(matubi[::-1] + s[::-1] + sentou[::-1])
else:
    print(sentou + s + matubi)

Solution (Python):

from collections import deque
S=list(input())
Q=int(input())
Sque=deque(S)
re=1
for i in range(Q):
    q=input()
    if q=="1":
        re*=-1
    else:
        two,F,C=q.split()
        if re==1:
            if F=="1":
                Sque.appendleft(C)
            else:
                Sque.append(C)
        else:
            if F=="2":
                Sque.appendleft(C)
            else:
                Sque.append(C)
S=list(Sque)
if re==-1:
    S.reverse()

ans="".join(S)
print(ans)