Problem:
 Score : 400 points    Problem Statement Takahashi has a string S consisting of lowercase English letters.  Starting with this string, he will produce a new one in the procedure given as follows.  The procedure consists of Q operations. In Operation i  (1 \leq i \leq Q) , an integer T_i is provided, which means the following:    If T_i = 1 : reverse the string S .    If T_i = 2 : An integer F_i and a lowercase English letter C_i are additionally provided.   If F_i = 1 : Add C_i to the beginning of the string S .  If F_i = 2 : Add C_i to the end of the string S .     Help Takahashi by finding the final string that results from the procedure.      Constraints  1 \leq |S| \leq 10^5  S consists of lowercase English letters.  1 \leq Q \leq 2 \times 10^5  T_i = 1 or 2 .  F_i = 1 or 2 , if provided.  C_i is a lowercase English letter, if provided.         Input Input is given from Standard Input in the following format:  S  Q  Query_1  :  Query_Q   In the 3 -rd through the (Q+2) -th lines, Query_i is one of the following:  1   which means T_i = 1 , and:  2  F_i  C_i   which means T_i = 2 .      Output Print the resulting string.        Sample Input 1 a
4
2 1 p
1
2 2 c
1      Sample Output 1 cpa  There will be Q = 4 operations. Initially, S is a .    Operation 1 : Add p at the beginning of S . S becomes pa .    Operation 2 : Reverse S . S becomes ap .    Operation 3 : Add c at the end of S . S becomes apc .    Operation 4 : Reverse S . S becomes cpa .    Thus, the resulting string is cpa .       Sample Input 2 a
6
2 2 a
2 1 b
1
2 2 c
1
1      Sample Output 2 aabc  There will be Q = 6 operations. Initially, S is a .    Operation 1 : S becomes aa .    Operation 2 : S becomes baa .    Operation 3 : S becomes aab .    Operation 4 : S becomes aabc .    Operation 5 : S becomes cbaa .    Operation 6 : S becomes aabc .    Thus, the resulting string is aabc .       Sample Input 3 y
1
2 1 x      Sample Output 3 xy

Solution (Python):
from collections import deque
S=deque(input())
Q=int(input())
r='1'
for _ in range(Q):
    target = input()
    if target == '1':
        if r=='1':
            r='2'
        else:
            r='1'
    else:
        a,b,c=target.split()
        if b=='1' and r=='1':
            S.appendleft(c)
        elif b=='2' and r=='1':
            S.append(c)
        elif b=='1' and r=='2':
            S.append(c)
        else:
            S.appendleft(c)
if r=='2':
    S.reverse()
print(''.join(list(S)))

Solution (Python):
from collections import deque

S = deque([x for x in input()])
Q = int(input())
switch = 0
for _ in range(Q):
    inp = input()
    if len(inp) == 1:
        T = int(inp)
        if switch:
            switch = 0
        else:
            switch = 1
    else:
        T, F = map(int, inp.split()[:2])
        C = inp.split()[-1]
        if F == 1:
            if switch:
                S.append(C)
            else:
                S.appendleft(C)
        else:
            if switch:
                S.appendleft(C)
            else:
                S.append(C)
if switch:
    print("".join(list(S)[::-1]))
else:
    print("".join(S))

Solution (Python):
from collections import deque

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

flg = "order"
for i in range(q):
    t, *f = map(str, input().split())
    if t == "1":
        if flg == "order":
            flg = "reverse"
        else:
            flg = "order"
    else:
        if f[0] == "1":
            if flg == "order":
                s.appendleft(f[1])
            else:
                s.append(f[1])
        else:
            if flg == "order":
                s.append(f[1])
            else:
                s.appendleft(f[1])

if flg == "order":
    print("".join(s))
else:
    s.reverse()
    print("".join(s))

Solution (Python):
S = input()
Q = int(input())
L = [list(map(str,input().split())) for i in range(Q)]

cnt = 0
FR = ""
BA = ""

for i in range(Q):
    if len(L[i]) == 1:
        cnt += 1
    else:
        if (int(L[i][1])+cnt)%2 == 0:
            BA = BA + L[i][2]
        else:
            FR = L[i][2]+FR

if cnt%2 == 0:
    print(FR+S+BA)
else:
    new_S = ''.join(list(reversed(FR+S+BA)))
    print(new_S)

Solution (Python):
s = input()
q = int(input())
qs = [input().split() for i in range(q)]
n = False
left= right = ""
for operation in qs:
    if operation[0] == '1':
        n = not n
    if operation[0] == '2':
        t , f, c = operation
        f = int(f)
        if n:
            f = 1 if f != 1 else 2
        if f == 1:
            left=c+left
        elif f == 2:
            right+=c
s = left+s+right
print(s[::-1] if n else s)

Solution (Python):
s=input()
q=int(input())
l=""
r=""
flg=True

for i in range(q):
    p=list(map(str,input().split()))
    if p[0]=="1":
        flg=not flg
    else:
        z,f,c=p
        if f=="1":
            if flg:
                l=c+l
            else:
                r=r+c
        else:
            if not flg:
                l=c+l
            else:
                r=r+c
s=l+s+r

if flg:
    ans=s
else:
    ans=s[::-1]
print(ans)

Solution (Python):
s=input()
q=int(input())
l=""
r=""
flg=True

for i in range(q):
    p=list(map(str,input().split()))
    if p[0]=="1":
        flg=not flg
    else:
        z,f,c=p
        if f=="1":
            if flg:
                l=c+l
            else:
                r=r+c
        else:
            if not flg:
                l=c+l
            else:
                r=r+c
s=l+s+r

if flg:
    ans=s
else:
    ans=s[::-1]
print(ans)

Solution (Python):
from collections import deque

s = input()
a = deque()
for i in s:
  a.append(i)
q = int(input())
rev = 0     
for i in range(q):
  s = input()
  if s[0] == '1':
    rev ^= 1
  else:
    b = (int(s[2]) - 1) ^ rev
    if b:
      a.append(s[4])
    else:
      a.appendleft(s[4])
if rev:
  a.reverse()
for i in a:
  print(i, end="")

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

right=[]
left=[]
flag=0

for i in range(q):
    a = input()
    if int(a[0]) == 1:
        if flag == 0:
            flag=1
        else:
            flag=0
    else:
        if int(a[2]) == 1:
            if flag == 0:
                left.append(a[4])
            else:
                right.append(a[4])
        elif int(a[2]) ==2:
            if flag ==0:
                right.append(a[4])
            else:
                left.append(a[4])

right_result="".join(right)
left_result="".join(left)

if flag==0:
    print(str(left_result[::-1]+s+right_result))
else:
    print(str(right_result[::-1]+s[::-1]+left_result))

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

right=[]
left=[]
flag=0

for i in range(q):
    a = input()
    if int(a[0]) == 1:
        if flag == 0:
            flag=1
        else:
            flag=0
    else:
        if int(a[2]) == 1:
            if flag == 0:
                left.append(a[4])
            else:
                right.append(a[4])
        elif int(a[2]) ==2:
            if flag ==0:
                right.append(a[4])
            else:
                left.append(a[4])

right_result="".join(right)
left_result="".join(left)

if flag==0:
    print(str(left_result[::-1]+s+right_result))
else:
    print(str(right_result[::-1]+s[::-1]+left_result))

Solution (Python):
I = input
s = I()
q = int(I())
a = [], []
i = 0
for _ in range(q):
  x = I()
  if x[0] == '1':
    i ^= 1
  else:
    f = int(x[2]) - 1
    a[i ^ f].append(x[4])
print(''.join(reversed(a[i])) + (s[::-1] if i else s) + ''.join(a[i^1]))

Solution (Python):
I = input
s = I()
q = int(I())
a = [], []
i = 0
for _ in range(q):
  x = I()
  if x[0] == '1':
    i ^= 1
  else:
    f = int(x[2]) - 1
    a[i ^ f].append(x[4])
print(''.join(reversed(a[i])) + (s[::-1] if i else s) + ''.join(a[i^1]))

Solution (Python):
S = list(input())
Q = int(input())
query = [list(input().split()) for _ in range(Q)]
mae = []
ato = []
is_reverse = False
for q in query:
    if q[0] == "1":
        is_reverse = not is_reverse
    else:
        if is_reverse:
            if q[1] == "2":
                mae.append(q[2])
            else:
                ato.append(q[2])
        else:
            if q[1] == "1":
                mae.append(q[2])
            else:
                ato.append(q[2])
mae.reverse()
mae.extend(S)
mae.extend(ato)
if is_reverse:
    mae.reverse()
    print("".join(mae))
else:
    print("".join(mae))

Solution (Python):


s = input()
n = int(input())
mae_list = []
usiro_list = []
flag = 1
for i in range(0, n):
    q_list = list(map(str, input().split()))
    if len(q_list) == 1:
        flag *= -1
    else:
        if (q_list[1] == "1" and flag == 1) or (q_list[1] == "2" and flag == -1):
            mae_list.append(q_list[2])
        else:
            usiro_list.append(q_list[2])
if flag == 1:
    if len(mae_list) > 0:
        mae_list.reverse()
        print(''.join(mae_list), end="")
    print(''.join(s), end="")
    if len(usiro_list) > 0:
        print(''.join(usiro_list), end="")
else:
    if len(usiro_list) > 0:
        usiro_list.reverse()
        print(''.join(usiro_list), end="")
    for i in range(0, len(s)):
        print(s[len(s)-i-1], end="")
    if len(mae_list) > 0:
        print(''.join(mae_list), end="")
print()

Solution (Python):
from collections import deque
S = input()
Q = int(input())
query = [list(input().split()) for _ in range(Q)]
reversecount = 0
d = deque(S)
for x in query:
    if int(x[0]) == 1:
        reversecount += 1
    else:
        if (int(x[1]) == 1 and (reversecount % 2 == 0)) or (int(x[1]) == 2 and (reversecount % 2 == 1)):
            d.appendleft(x[2])
        else:
            d.append(x[2])
if reversecount % 2 == 1:
    d.reverse()
print(*d, sep='')

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

flag = False
for _ in range(Q):
    q = list(input().split())
    if q[0] == '1':
        flag = True if flag is False else False
    else:
        if flag is False:
            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 flag is True:
    S.reverse()
print(''.join(S))

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

    s = input()
    s = s[0:len(s)-1]
    Q = int(input())
    head = []
    tail = []

    flg = 0 
    for _ in range(Q):
        q = input().split()
        if q[0] == "1":
            if flg == 0:
                flg = 1
            else:
                flg = 0
        elif q[0]=="2":
            index,tmp = q[1],q[2]
            if (index == "1" and flg ==0) or (index == "2" and flg ==1):
                head.append(tmp)
            elif (index == "2" and flg == 0) or (index == "1" and flg == 1):
                tail.append(tmp)

    s1 = "".join(head)
    s1 = s1[::-1]
    s2 = "".join(tail)
    ans = s1 + s + s2
    if flg == 1:
        ans = ans[::-1]

    print(ans)

if __name__ == '__main__':
    main()

Solution (Python):
S = input()
Q = int(input())
qry = [input().split() for _ in range(Q)]
A = ''
B = ''
oq = 0
even = True
for q in qry:
    if q[0] == '1':
        even = not even
        if oq == '1':
            continue
    else:
        if even:
            if q[1] == '1':
                A = q[2] + A
            else:
                B += q[2]
        else:
            if q[1] == '1':
                B += q[2]
            else:
                A = q[2] + A
    oq = q

if even:
    print(A+S+B)
else:
    print(B[::-1]+S[::-1]+A[::-1])

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

right=[]
left=[]
flag=0

for i in range(q):
    a = input()
    if int(a[0]) == 1:
        if flag == 0:
            flag=1
        else:
            flag=0
    else:
        if int(a[2]) == 1:
            if flag == 0:
                left.append(a[4])
            else:
                right.append(a[4])
        elif int(a[2]) ==2:
            if flag ==0:
                right.append(a[4])
            else:
                left.append(a[4])

right_result="".join(right)
left_result="".join(left)

if flag==0:
    print(str(left_result[::-1]+s+right_result))
else:
    print(str(right_result[::-1]+s[::-1]+left_result))

Solution (Python):
def main(s,end,start,perse):
    start.reverse()
    start.append(''.join(s))
    start.append(''.join(end))
    if perse == -1:
        start.reverse()
    print(''.join(start))
def conv(s):
    a = []
    for i in range(len(s)):
        a.append(s[i])
    return a
perse = 1
start = []
end = []
  
s = input()
q = int(input())
for i in range(q):
    query = input().split()
    query[0] = int(query[0])
    if query[0] == 1:
        perse = -perse
    else:
        query[1] = int(query[1])
        if (int(query[1])*2-3)*perse == 1:
            end.append(query[2])
        else:
            start.append(query[2])
s =  conv(s)
if perse == -1:
    s.reverse()
    end.reverse()
main(s,end,start,perse)

Solution (Python):
S1=""
S2=""
S=str(input())
Q=int(input())
flag=1
for i in range(Q):
    T=list(map(str,input().split()))
    if T[0]=="1":
        flag*=-1
    else:
        if T[1]=="1":
            if flag==1:
                S1=T[2]+S1
            else: S2=S2+T[2]
        else:
            if flag==1:
                S2=S2+T[2]
            else:S1=T[2]+S1
if flag==1:
    S=S1+S+S2
else:S=S2[::-1]+S[::-1]+S1[::-1]
print(S)

Solution (Python):
from collections import deque
S = deque(input())
Q = int(input())
query = [input().split() for _ in range(Q)]
reverse = False
for q in query:
    if q[0] == '1':
        reverse = not reverse
    else:
        if q[1] == '1':
            S.append(q[2]) if reverse else S.appendleft(q[2])
        else:
            S.appendleft(q[2]) if reverse else S.append(q[2])
if reverse:
    S.reverse()
print(''.join(S))

Solution (Python):
from collections import deque

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

rev = 0
for i in range(q):
    if len(Q[i]) == 1:
        rev += 1
        rev %= 2
    else:
        if rev == 0:
            if Q[i][1] == "1":
                s.appendleft(Q[i][2])
            else:
                s.append(Q[i][2])
        else:
            if Q[i][1] == "2":
                s.appendleft(Q[i][2])
            else:
                s.append(Q[i][2])

s = list(s)
if rev == 0:
    for i in range(len(s)):
        print(s[i],end="")
else:
    for i in reversed(range(len(s))):
        print(s[i],end="")
print()

Solution (Python):

import sys,os
sys.setrecursionlimit(10**6)
write = sys.stdout.write
dbg = (lambda *something: print(*something)) if 'TERM_PROGRAM' in os.environ else lambda *x: 0
def main(given=sys.stdin.readline):
    input = lambda: given().rstrip()
    LMIIS = lambda: list(map(int,input().split()))
    II = lambda: int(input())
    XLMIIS = lambda x: [LMIIS() for _ in range(x)]
    YN = lambda c : print('Yes') if c else print('No')
    MOD = 10**9+7
    S = input()
    Slist = [S]
    Q = II()
    Slist = ['']*Q+Slist+['']*Q
    left = Q-1
    right = Q+1
    r = False
    for _ in range(Q):
        query = input().split()
        if query[0] == '1':
            r ^= True
        else:
            if query[1] == '1':
                    if r:
                        
                        Slist[right] = query[2]
                        right += 1

                    else:
                        
                        Slist[left] = query[2]
                        left -=1
            else:
                if r:
                    Slist[left] = query[2]
                    left -=1
                else:
                    
                    Slist[right] = query[2]
                    right += 1
    
    if r:
        Slist[Q] = Slist[Q][::-1]

    print(''.join(Slist[::-1]) if r else ''.join(Slist))
                



    

if __name__ == '__main__':
    main()

Solution (Python):


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




mode = False



front = ''
back = ''
for i in range(n):
    qs = input()
    if len(qs) == 1:
        mode = not mode
        
        
    else:
        
        
        
        if not mode:
            if int(qs[2]) == 1:
                front += qs[4]
                
            else:
                back += qs[4]
                
        else:
            if int(qs[2]) == 1:
                back += qs[4]
                
            else:
                front += qs[4]
                




if mode:
    print(back[::-1] + s[::-1] + front)

else:
    print(front[::-1] + s + back)

Solution (Python):
import math

s = input()
q = int(input())
reverse_flag = 0
head = ""
tail = ""

for i in range(q):
  query = input().split(" ")
  if(len(query) == 1):
    reverse_flag += 1
  else:
    if((reverse_flag + int(query[1])) % 2 == 0):
      tail += query[2]
    else:
      head = query[2] + head

ans_str = head + s + tail

if(reverse_flag % 2 == 1): print(ans_str[::-1])
else: print(ans_str)

Solution (Python):
s = input()
Q = int(input())
count = 0
right = []
left = []
for i in range(Q):
    q = input().split()
    if int(q[0]) == 1:
        count += 1
    else:
        if count%2 == 0:
            if int(q[1]) == 1:
                left.append(q[2])
            else:
                right.append(q[2])
        else:
            if int(q[1]) == 1:
                right.append(q[2])
            else:
                left.append(q[2])

left = "".join(left)
right = "".join(right)
if count%2 == 0:
    left = left[::-1]
    print(left+s+right)
else:
    s = s[::-1]
    right = right[::-1]
    print(right+s+left)

Solution (Python):
*s, = input()
_q = int(input())
rev = 0
l, r = [], []
for _ in range(_q):
    *q, = map(str, input().split())

    if q[0] == "1":
        rev += 1
        continue

    t = q[0]
    f = q[1]
    c = q[2]

    if rev%2 == 0:
        if f == "1":
            l.append(c)
        else:
            r.append(c)
    else:
        if f == "1":
            r.append(c)
        else:
            l.append(c)

*l, = reversed(l)

s = l + s + r

if rev%2 == 1:
    *s, = reversed(s)

print("".join(s))

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

header = []
footer = []
flag = 0
for i in range(q):
    x = list(input().split())
    if int(x[0]) == 1:
        if flag ==0:
            flag = -1
        else:
            flag = 0
        continue
    if int(x[1]) +flag == 1:
        header.append(x[2])
        
        
        
        
    else:
        
        footer.append(x[2])

head = ''.join(reversed(header))
ans = head +s + ''.join(footer)
if flag == -1:
    print(ans[::-1])
else:
    print(ans)

Solution (Python):


ss = input()
s = []
for i in range(0, len(ss)):
    s.append(ss[i])
n = int(input())
mae_list = []
usiro_list = []
flag = 1
for i in range(0, n):
    q_list = list(map(str, input().split()))
    if len(q_list) == 1:
        flag *= -1
    else:
        if (q_list[1] == "1" and flag == 1) or (q_list[1] == "2" and flag == -1):
            mae_list.append(q_list[2])
        else:
            usiro_list.append(q_list[2])
if flag == 1:
    if len(mae_list) > 0:
        mae_list.reverse()
        print(''.join(mae_list), end="")
    print(''.join(s), end="")
    if len(usiro_list) > 0:
        print(''.join(usiro_list), end="")
else:
    if len(usiro_list) > 0:
        usiro_list.reverse()
        print(''.join(usiro_list), end="")
    s.reverse()
    print(''.join(s), end="")
    if len(mae_list) > 0:
        print(''.join(mae_list), end="")
print()

Solution (Python):
from collections import deque

S = input()
Q = int(input())
flag_reversed = False
ret = deque(S)

for _ in range(Q):
    temp = input()
    if (len(temp) < 2):
        flag_reversed = not flag_reversed
    else:
        T, F, C = [x for x in temp.split(" ")]
        F = int(F)
        add_begin = True
        if (F == 2):
            add_begin = False
        if (flag_reversed):
            add_begin = not add_begin
        if (add_begin):
            ret.appendleft(C)
        else:
            ret.append(C)

ret = ''.join(ret)

if (flag_reversed):
    ret = ''.join(reversed(ret))
print(ret)

Solution (Python):
from collections import deque

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

roll = False
ans = deque([s])
preNum = 0

for i in range(q) :
    inp = input()
    if inp[:1] == '1' :
        roll = not roll
        
    else :
        t, f, c = inp.split()
        
        
        if (f == '1' and (not roll)) or (f == '2' and roll) :
            ans.appendleft(c)
            preNum += 1
        else :
            ans.append(c)

ans = list(ans)
if roll :
    ans[preNum] = ans[preNum][::-1]
    ans = ans[::-1]



for s in ans :
    print(s, end='')

Solution (Python):
from collections import deque
S = input()
Q = int(input())
pre = deque()
suf = deque()
rev = False
for i in range(Q):
    q = input().split()
    if len(q) == 1:
        rev = not rev
    else:
        if (not rev and q[1] == '1') or (rev and q[1] == '2'):
            pre.appendleft(q[2])
        else:
            suf.append(q[2])
if rev:
    S = S[::-1]
    p = list(suf)[::-1]
    s = list(pre)[::-1]
else:
    p = list(pre)
    s = list(suf)
print(''.join(p) + S + ''.join(s))

Solution (Python):
S=str(input())
Q=int(input())
which=0
front=""
back=""
for i in range(Q):
    a=list(map(str,input().split()))
    if a==["1"]:
        which+=1
        which%=2
        continue
    else:
        if which==0:
            if a[1]=="1":
                front=a[2]+front
            if a[1]=="2":
                back+=a[2]
        if which==1:
            if a[1]=="1":
                back+=a[2]
            if a[1]=="2":
                front=a[2]+front

S=front+S+back
if which==1:
    print(S[::-1])
if which==0:
    print(S)

Solution (Python):
s=input()
q=int(input())
mode=1
h=[]
t=[]
for i in range(q):
    x=input()
    if x=="1":
        if mode==1:
            mode=0
        else:
            mode=1
    else:
        a=int(x[0])
        b=int(x[2])
        c=x[4]
        if b==1:
            if mode==1:
                h.append(c)
            else:
                t.append(c)
        elif b==2:
            if mode==0:
                h.append(c)
            else:
                t.append(c)
s=list(s)
if mode==1:
    ans=h[::-1]+s+t
elif mode==0:
    ans=t[::-1]+s[::-1]+h
print("".join(ans))

Solution (Python):
def main():
    from sys import stdin
    from collections import deque
    S = input()
    Q = int(input())
    reverse = 0
    head = []
    tail = []
    head_deque = deque(head)
    tail_deque = deque(tail)
    
    for _ in [0] * Q:
        readline = stdin.readline
        s = readline().split()
        if int(s[0]) == 1:
            if reverse == 1:
                reverse = 0
            else:
                reverse = 1
        else:
            if int(s[1]) == 1:
                if reverse == 1:
                    tail.append(s[2])
                else:
                    head_deque.appendleft(s[2])
            else:
                if reverse == 1:
                    head_deque.appendleft(s[2])
                else:
                    tail.append(s[2])
    head = list(head_deque)
    head = ''.join(head)
    tail = ''.join(tail)
    if reverse == 1:
        print(tail[::-1]+S[::-1]+head[::-1])
    else:
        print(head+S+tail)
        
main()

Solution (Python):
S = list(input())
Q = int(input())
is_reversed = False
append_first = []
my_first = append_first.append
append_end = []
my_end = append_end.append

for i in range(Q):
  q = list(input())
  if q[0] == '1':
    is_reversed = True if is_reversed == False else False
  else:
    if q[2] == '1' and is_reversed:
      append_end.append(q[4])
    elif q[2] == '1': 
      append_first.append(q[4])
    elif is_reversed:
      append_first.append(q[4])   
    else:
      append_end.append(q[4])
      
append_first.reverse()
append_first.extend(S) 
append_first.extend(append_end)

if is_reversed:
  append_first.reverse()
print(''.join(append_first))

Solution (Python):

import sys
import math
from bisect import bisect_left
from bisect import bisect_right
import collections
import copy
import heapq
from collections import defaultdict
from heapq import heappop, heappush
import itertools
input = sys.stdin.readline

























def inputInt(): return int(input())
def inputMap(): return map(int, input().split())
def inputList(): return list(map(int, input().split()))

inf = float('inf')
mod = 1000000007




def main():
	SS = input()
	SS = SS[:-1]
	Q = inputInt()

	mae = ""
	mae_to = []
	usiro = ""
	usiro_to = []

	flg = True
	for _ in range(Q):
		inp = input()
		inp = inp[:-1]
		inpt = inp.split()

		if len(inpt) == 1:
			if flg == True:
				flg = False
			else:
				flg = True
		else:
			oder = inpt[1]
			if oder == "1":
				if flg == True:
					mae = inpt[2] + mae
					if len(mae) > 10000:
						mae_to = [mae] + mae_to
						mae = ""
				else:
					usiro = usiro + inpt[2]
					if len(usiro) > 10000:
						usiro_to.append(usiro)
						usiro = ""
			else:
				if flg == True:
					usiro = usiro + inpt[2]
					if len(usiro) > 10000:
						usiro_to.append(usiro)
						usiro = ""
				else:
					mae = inpt[2] + mae
					if len(mae) > 10000:
						mae_to = [mae] + mae_to
						mae = ""

	
	
	usi = ""
	if len(usiro_to) > 0:
		usi = "".join(usiro_to) + usiro
	else:
		usi = usiro

	me = ""
	if len(mae_to) > 0:
		me = mae + "".join(mae_to)
	else:
		me = mae

	S = me + SS + usi

	if flg == True:
		print(S)
	else:
		print(S[::-1])









def combination(n, r, mod=10**9+7):
    r = min(r, n-r)
    res = 1
    for i in range(r):
        res = res * (n - i) * modinv(i+1, mod) % mod
    return res


def modinv(a, mod=10**9+7):
    return pow(a, mod-2, mod)

def egcd(a, b):
    if a == 0:
        return b, 0, 1
    else:
        g, y, x = egcd(b % a, a)
        return g, x - (b // a) * y, y



def H(n, r, mod=10**9+7):
	
	
    return combination(n+r-1, r, mod)

class Combination:
    """
    O(n)の前計算を1回行うことで，O(1)でnCr mod mを求められる
    n_max = 10**6のとき前処理は約950ms (PyPyなら約340ms, 10**7で約1800ms)
    使用例：
    comb = Combination(1000000)
    print(comb(5, 3))  
    """
    def __init__(self, n_max, mod=10**9+7):
        self.mod = mod
        self.modinv = self.make_modinv_list(n_max)
        self.fac, self.facinv = self.make_factorial_list(n_max)

    def __call__(self, n, r):
        return self.fac[n] * self.facinv[r] % self.mod * self.facinv[n-r] % self.mod

    def make_factorial_list(self, n):
        
        
        fac = [1]
        facinv = [1]
        for i in range(1, n+1):
            fac.append(fac[i-1] * i % self.mod)
            facinv.append(facinv[i-1] * self.modinv[i] % self.mod)
        return fac, facinv

    def make_modinv_list(self, n):
        
        modinv = [0] * (n+1)
        modinv[1] = 1
        for i in range(2, n+1):
            modinv[i] = self.mod - self.mod//i * modinv[self.mod%i] % self.mod
        return modinv

if __name__ == "__main__":
	main()

Solution (Python):

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

count = True
ans = S

ireru_mae = ''
ireru_usiro = ''
for i in range(Q):
    T = input().split()
    if len(T)==1:
        count = not(count)
        ans = ireru_mae + ans + ireru_usiro
        ireru_mae = ''
        ireru_usiro = ''
        continue

    if count:
        if T[1] == '1': ireru_mae = T[2] + ireru_mae
        else: ireru_usiro += T[2]
    else:
        if T[1] == '1': ireru_usiro += T[2]
        else: ireru_mae = T[2] + ireru_mae

ans = ireru_mae + ans + ireru_usiro
if count:
    print(ans)
else:
    print(ans[::-1])

Solution (Python):
from collections import deque

head = deque()
tail = deque()
count_rev = 0
switch = 0

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

for i in range(Q):
    q = input()
    if(q == '1'):
        count_rev += 1
        if(switch == 1):
            switch = 0
        else:
            switch = 1

    else:
        tmp, f, c = q.split()
        if(f == '1'):
            
            if(switch == 0):
                head.appendleft(c)
            else:
                tail.append(c)
        else:
            
            if(switch == 0):
                tail.append(c)
            else:
                head.appendleft(c)

reversed(head)
if(count_rev % 2 == 0):
    ans = list(head) + list(S) + list(tail)
else:
    s = list(S)
    s.reverse()
    ans = list(reversed(tail)) + s + list(reversed(head))

print(''.join(ans))

Solution (Python):

import sys
import math
from bisect import bisect_left
from bisect import bisect_right
import collections
import copy
import heapq
from collections import defaultdict
from heapq import heappop, heappush
import itertools
input = sys.stdin.readline

























def inputInt(): return int(input())
def inputMap(): return map(int, input().split())
def inputList(): return list(map(int, input().split()))

inf = float('inf')
mod = 1000000007




def main():
	SS = input()
	SS = SS[:-1]
	Q = inputInt()

	mae = ""
	mae_to = []
	usiro = ""
	usiro_to = []

	flg = True
	for _ in range(Q):
		inp = input()
		inp = inp[:-1]
		inpt = inp.split()

		if len(inpt) == 1:
			if flg == True:
				flg = False
			else:
				flg = True
		else:
			oder = inpt[1]
			if oder == "1":
				if flg == True:
					mae = inpt[2] + mae
					if len(mae) > 10000:
						mae_to = [mae] + mae_to
						mae = ""
				else:
					usiro = usiro + inpt[2]
					if len(usiro) > 10000:
						usiro_to.append(usiro)
						usiro = ""
			else:
				if flg == True:
					usiro = usiro + inpt[2]
					if len(usiro) > 10000:
						usiro_to.append(usiro)
						usiro = ""
				else:
					mae = inpt[2] + mae
					if len(mae) > 10000:
						mae_to = [mae] + mae_to
						mae = ""

	
	
	usi = ""
	if len(usiro_to) > 0:
		usi = "".join(usiro_to) + usiro
	else:
		usi = usiro

	me = ""
	if len(mae_to) > 0:
		me = mae + "".join(mae_to)
	else:
		me = mae

	S = me + SS + usi

	if flg == True:
		print(S)
	else:
		print(S[::-1])









def combination(n, r, mod=10**9+7):
    r = min(r, n-r)
    res = 1
    for i in range(r):
        res = res * (n - i) * modinv(i+1, mod) % mod
    return res


def modinv(a, mod=10**9+7):
    return pow(a, mod-2, mod)

def egcd(a, b):
    if a == 0:
        return b, 0, 1
    else:
        g, y, x = egcd(b % a, a)
        return g, x - (b // a) * y, y



def H(n, r, mod=10**9+7):
	
	
    return combination(n+r-1, r, mod)

class Combination:
    """
    O(n)の前計算を1回行うことで，O(1)でnCr mod mを求められる
    n_max = 10**6のとき前処理は約950ms (PyPyなら約340ms, 10**7で約1800ms)
    使用例：
    comb = Combination(1000000)
    print(comb(5, 3))  
    """
    def __init__(self, n_max, mod=10**9+7):
        self.mod = mod
        self.modinv = self.make_modinv_list(n_max)
        self.fac, self.facinv = self.make_factorial_list(n_max)

    def __call__(self, n, r):
        return self.fac[n] * self.facinv[r] % self.mod * self.facinv[n-r] % self.mod

    def make_factorial_list(self, n):
        
        
        fac = [1]
        facinv = [1]
        for i in range(1, n+1):
            fac.append(fac[i-1] * i % self.mod)
            facinv.append(facinv[i-1] * self.modinv[i] % self.mod)
        return fac, facinv

    def make_modinv_list(self, n):
        
        modinv = [0] * (n+1)
        modinv[1] = 1
        for i in range(2, n+1):
            modinv[i] = self.mod - self.mod//i * modinv[self.mod%i] % self.mod
        return modinv

if __name__ == "__main__":
	main()

Solution (Python):
s=input()
s1=""
s2=""
count_1=0
l=[]
num =int(input())
 
 
for x in range(num):
    i=input().rstrip().split()
    if i[0]=="1":
        count_1+=1
        
    elif i[1]=="1" and count_1%2==0:
        s1=i[2]+s1
   
    elif i[1]=="1" and count_1%2==1:
        s2=s2+i[2]
 
    elif i[1]=="2" and count_1%2==0:
        s2=s2+i[2]
 
    elif i[1]=="2" and count_1%2==1:
        s1=i[2]+s1
s=s1+s+s2
 
if count_1%2==1:
    s=s[::-1]
print(s)