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):
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):
import heapq
from collections import deque
s = input()
s_list = list(s)
s_list = deque(s_list)
q = int(input())
q_list = []
for i in range(q):
  q_list.append(list(map(str,input().split())))
check = 0
for list_ in q_list:
  if int(list_[0]) == 1:
    check += 1
  if int(list_[0]) == 2:
    if (int(list_[1]) + check) % 2 ==1:
      s_list.appendleft(list_[2])
    else:
      s_list.append(list_[2])
s_list = list(s_list)
if check % 2 == 1:
  s_list = s_list[::-1]
print("".join(s_list))

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())
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())


is_reverse = False
forward = dict()  
backward = dict()  


forward_num = 0
backward_num = 0

for _ in range(q):
    query = input().split()
    if len(query) == 1:
        if is_reverse:
            is_reverse = False
        else:
            is_reverse = True
    else:
        _, f, c = query
        if f == '1':
            if is_reverse:
                backward[backward_num] = c
                backward_num += 1
                
                
            else:
                forward[forward_num] = c
                forward_num += 1
                
                
        else:
            if is_reverse:
                forward[forward_num] = c
                forward_num += 1
                
                
            else:
                backward[backward_num] = c
                backward_num += 1
                
                



forward = list(forward.values())[::-1]
backward = list(backward.values())




if is_reverse:
    s = s[::-1]
    print(''.join(backward)[::-1] + s + ''.join(forward)[::-1])
else:
    print(''.join(forward) + s + ''.join(backward))

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

ans = [" " for n in range(2*q + 1)]
ans[q] = s
frontIndex = q - 1
endIndex = q + 1
isReverse = -1

for i in range(q):
    query = input().split()
    if(query[0] == "1"):
        isReverse *= -1
    else:
        if(query[1] == "1"):
            if(isReverse == 1):
                ans[endIndex] = query[2]  
                endIndex += 1
            else:
                ans[frontIndex] = query[2]
                frontIndex -= 1
        else:
            if(isReverse == 1):
                ans[frontIndex] = query[2] 
                frontIndex -= 1
            else:
                ans[endIndex] = query[2]
                endIndex += 1
ans = "".join(ans).strip()
if(isReverse == -1):
    print(ans)
else:
    print(ans[::-1])

Solution (Python):
import sys
import math
import collections
from collections import deque


s = input()

q = int(input())
d = deque([])
for i in range(len(s)):
    d.append(s[i])

rev = 1
for i in range(q):
    que = sys.stdin.readline().split()
    if que[0] == "1":
        rev *= -1

    else:
        if rev == 1:
            if que[1] == "1":
                d.appendleft(que[2])
            else:
                d.append(que[2])

        else:
            if que[1] == "1":
                d.append(que[2])
            else:
                d.appendleft(que[2])


if rev == 1:
    for i in range(len(d)):
        print(d.popleft(), end="")
else:
    for i in range(len(d)):
        print(d.pop(), end="")

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())
t = 0
f = -1
x = ["",""]
for i in range(q):
    que = input().split()
    if que[0] == "1":
        t += 1
    else:
        x[(int(que[1])-1+t)%2] += que[2]

s = x[0][::-1] + s + x[1]
if t%2 == 1:
    s = s[::-1]

print(s)

Solution (Python):
s = input()
N = int(input())
Q = [input().split() for _ in range(N)]

head = ''
tail = ''
reverse = 0

for q in Q:
    if int(q[0]) == 1:
        reverse = 1 - reverse
    elif int(q[1]) == 1:
        if reverse:
            tail += q[2]
        else:
            head += q[2]
    else:
        if reverse:
            head += q[2]
        else:
            tail += q[2]

if reverse:
    print(tail[::-1] + s[::-1] + head)
else:
    print(head[::-1] + s + tail)

Solution (Python):
ii = lambda : int(input())
mi = lambda : map(int,input().split())
li = lambda : list(map(int,input().split()))


s = input()

n = ii()
r = 0
head = ''
tail = ''
for i in range(n):
    q = input().split()

    if(q[0] == '1'):
        
        r += 1
    else:
        if(q[1] == '1'):
            if(r%2 ==0):
                
                head = q[2] + head
            else:
                
                tail = tail + q[2]
        else:
            if(r%2 ==0):
                
                tail = tail + q[2]
            else:
                
                head = q[2] + head

    
    

if(r%2==1):
    
    
    print( tail[::-1] + s[::-1]+ head[::-1])
else:

    print(head +  s + tail)

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())

A=""
Z=""
AZFlag = 0
for i in range(Q):
    Que = input().split()

    if Que[0] == '1':
        if AZFlag == 0:
            AZFlag = 1
            continue

        if AZFlag == 1:
            AZFlag = 0
            continue

    if Que[0] == '2':
        if Que[1] == '1' and AZFlag == 0:
            A = Que[2]+A
            continue

        if Que[1] == '1' and AZFlag == 1:
            Z = Z+Que[2]
            continue
        
        if Que[1] == '2' and AZFlag == 0:
            Z = Z+Que[2]
            continue

        if Que[1] == '2' and AZFlag == 1:
            A = Que[2]+A
            continue


if AZFlag == 0:
    print(A+S+Z)
if AZFlag == 1:
    print(Z[::-1]+S[::-1]+A[::-1])

Solution (Python):
from collections import deque

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

for i in range(Q):
    tmp = list(input().split())
    lst.append(tmp)

reverse = 0
c = 1
for i in range(Q):

    if int(lst[i][0]) == 1:
        reverse += 1
        c += 1
    
    if int(lst[i][0]) == 2:
        if c%2 == 1:
            if int(lst[i][1]) == 1:
                S.appendleft(lst[i][2])
            else:
                S.append(lst[i][2])
        else:
            if int(lst[i][1]) == 2:
                S.appendleft(lst[i][2])
            else:
                S.append(lst[i][2])

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

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

Solution (Python):
from collections import deque

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

d = deque(s)
inv = 0

for _ in range(q):
  Q = list(input().split())
  
  if Q[0]=='1':
    inv += 1
  elif Q[0]=='2':
    if Q[1]=='1':
      if inv%2==0:
        d.appendleft(Q[2])
      else:
        d.append(Q[2])
    elif Q[1]=='2':
      if inv%2==0:
        d.append(Q[2])
      else:
        d.appendleft(Q[2])

if inv%2==0:
  print(*d, sep='')
else:
  print(*deque(reversed(d)), sep='')

Solution (Python):
s=input()
q=int(input())
is_reverse=False
pre=[]
post=[]
for i in range(q):
    qi=list(input().split())
    if(len(qi)==1):
        is_reverse = not is_reverse
    elif(qi[1]=="1"):
        if(is_reverse):
            post.append(qi[2])
        else:
            pre.append(qi[2])
    else:
        if(is_reverse):
            pre.append(qi[2])
        else:
            post.append(qi[2])

if(is_reverse):
    print(''.join(post)[::-1] + s[::-1] + ''.join(pre))
else:
    print(''.join(pre)[::-1] + s + ''.join(post))

Solution (Python):





sl=list(input())
q=int(input())
l=[list(map(str,input().split())) for i in range(q)]



count=0
isR=False
from collections import deque
sl=deque(sl)
for i in range(q):
    order=l[i]
    if order[0]=="1":
        count+=1
        
    if count%2==0:
        isR=False
    else:
        isR=True
    if order[0]!="1":
        f=order[1]
        c=order[2]
        temp_f=f
        if isR:
            if temp_f=="1":
                temp_f="2"
            else:
                temp_f="1"
        if temp_f=="1":
            
            sl.appendleft(c)
            
            
        else:
            sl.append(c)
sl=list(sl)
ln=len(sl)
ansl=[]
if isR:
    for i in range(ln-1,-1,-1):
        ansl.append(sl[i])
else:
    ansl=sl
ans=''.join(ansl)
print(ans)

Solution (Python):


moj = str(input())
kai = int(input())
han = 0
omo = ''
ura = ''
for i in range(kai):
    kyu = []
    kyu = list(input().split(" "))
    if kyu[0] == "1":
        if han == 0:
            han = 1
        elif han == 1:
            han = 0
    elif kyu[0] == "2":
        if han == 0:
            if kyu[1] == "1":
                ura += kyu[2]
            elif kyu[1] == "2":
                omo += kyu[2]
        if han == 1:
            if kyu[1] == "1":
                omo += kyu[2]
            elif kyu[1] == "2":
                ura += kyu[2]
if han == 1:
    moj = moj[::-1]
    omo = omo[::-1]
    moj = omo + moj + ura
elif han == 0:
    ura = ura[::-1]
    moj = ura + moj + omo
    
print(moj)

Solution (Python):
s = input()
Q = int(input())
flag = True  
cnt = 0
query = [list(map(str, input().split())) for _ in range(Q)]
before = []  
after = []  
for i in range(Q):
    a = query[i]
    if len(a) == 1:  
        cnt += 1
        if flag:
            flag = False
        else:
            flag = True
    else:
        f, c = a[1], a[2]
        if f == "1":  
            if flag:
                
                before.append(c)
            else:
                
                after.append(c)
        else:  
            if flag:
                
                after.append(c)
            else:
                
                before.append(c)
before.reverse()



s = "".join(before) + s + "".join(after)
if cnt % 2 == 1 and cnt != 0:
    s = s[::-1]
print(s)

Solution (Python):
from collections import deque

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

queue = deque(list(s))
is_reverse = False

for _ in range(q):
    tfc = input()
    if tfc[0] == '1':
        is_reverse = not is_reverse
        continue
    else:
        if is_reverse == False and tfc[2] == '1':
            queue.appendleft(tfc[4])
        elif is_reverse == False and tfc[2] == '2':
            queue.append(tfc[4])
        elif is_reverse == True and tfc[2] == '1':
            queue.append(tfc[4])
        elif is_reverse == True and tfc[2] == '2':
            queue.appendleft(tfc[4])

if is_reverse:
    ans = list(queue)
    new_ans = ans[::-1]
    print(*new_ans, sep='')
else:
    ans = queue
    print(*ans, sep='')

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

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

    reverse = False
    header = [''] * Q
    footer = [''] * Q
    for i in range(Q):
        query = input()
        if query[0] == '1':
            reverse = not reverse
        else:
            _, f, c = query.split()
            if reverse:
                if f == '1':
                    footer[i] = c
                else:
                    header[i] = c
            else:
                if f == '1':
                    header[i] = c
                else:
                    footer[i] = c
    if reverse:
        print("".join(footer[::-1] + list(S)[::-1] + header))
    else:
        print("".join(header[::-1]) + S + "".join(footer))

if __name__ == '__main__':
    main()

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

day = deque(S)

r = False
for i in range(Q):
    x = input().split()
    
    if x[0] == "1":
        r = not r
        
    else:
        if x[1] == "1":
            if r == False :
                day.appendleft(x[2])
            else:
                day.append(x[2])
        else:
            if r == True :
                day.appendleft(x[2])
            else:
                day.append(x[2])
                
day = list(day)
        
if r:
    print("".join(day[::-1]))
else:
    print("".join(day))

Solution (Python):

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

first = []
end = []
f = 0
for _ in range(q):
    li = list(input().split())
    if len(li) == 1:
        f = ~f
    else:
        if li[1] == '1':
            if f == 0:
                first.append(li[2])
            else:
                end.append(li[2])
        else:
            if f == 0:
                end.append(li[2])
            else:
                first.append(li[2])
s = first[::-1] + s + end
if f == -1:
    s = s[::-1]
    
print(''.join(s))

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

import collections

s = collections.deque(input().strip())
num = int(input())
query = [0] * num
for i in range(num):
    query[i] = input().strip()

reverse_flag = False
for q in query:
    if q[0] == '1':
        reverse_flag = not reverse_flag
    else:
        _, f, c = q.split()
        if (f == '1' and not reverse_flag) or (f == '2' and reverse_flag):
            s.appendleft(c)
        if (f == '2' and not reverse_flag) or (f == '1' and reverse_flag):
            s.append(c)
if reverse_flag:
    s.reverse()
print(''.join(list(s)))

Solution (Python):
from collections import defaultdict
from collections import deque
from string import ascii_uppercase
import sys, bisect, math, heapq

stdin = sys.stdin
read_int = lambda : list(map(int,stdin.readline().split()))
read_str = lambda : stdin.readline().rstrip()

S = read_str()
Q = read_int()[0]
Query = [list(read_str().split()) for _ in range(Q)]

d = deque([S])

def solve():
    reverse = False
    for q in Query:
        if q[0] == '1':
            reverse = not reverse
        else:
            if reverse:
                if q[1] == '1':
                    
                    d.append(q[2])
                else:
                    d.appendleft(q[2])
                    
            else:
                if q[1] == '1':
                    d.appendleft(q[2])
                    
                else:
                    
                    d.append(q[2])
        
    ans = ''
    if reverse:
        ans = ''.join(list(d))[::-1]
    else:
        ans = ''.join(list(d))
    return ans

if __name__ == "__main__":
    print(solve())

Solution (Python):
S=input()
Q=int(input())
x=0
A=[]
B=[]
for i in range(0,Q):
    query=list(input().split())
    if query[0]=='1':
        x+=1
    else:
        if query[1]=='1':
            if x%2==1:
                B.append(query[2])
            else:
                A.append(query[2])
        else:
            if x%2==1:
                A.append(query[2])
            else:
                B.append(query[2])

if x%2==1:
    B=[B[len(B)-1-i] for i in range(0,len(B))]
    S=[S[len(S)-1-i] for i in range(0,len(S))]
    a=''.join(A)
    b=''.join(B)
    S=''.join(S)
    print(b+S+a)
else:
    A=[A[len(A)-1-i] for i in range(0,len(A))]
    a=''.join(A)
    b=''.join(B)
    print(a+S+b)

Solution (Python):
from collections import deque

def main():
    s = input()
    q = int(input())

    Q = deque(s)
    rev = False
    for i in range(q):
        tfc = input().split()

        if tfc[0] == '1':
            rev = not rev
        else:
            f = tfc[1]
            c = tfc[2]
            if (f == '1' and not rev) or (f != '1' and rev):
                Q.appendleft(c)
            else:
                Q.append(c)

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

if __name__ == "__main__":
    main()

Solution (Python):
import collections
s=input()
q=int(input())
qv=[list(input().split()) for _ in range(q)]
cnt=0
ans=collections.deque()
for i in range(len(s)):
  ans.append(s[i])
for i in range(q):
  if qv[i][0]=="1":
    cnt+=1
  else:
    t,f,c = qv[i]
    if f=="1":
      if cnt%2==0:
        ans.appendleft(c)
      else:
        ans.append(c)
    elif f=="2":
      if cnt%2==0:
        ans.append(c)
      else:
        ans.appendleft(c)
if cnt%2!=0:
  ans.reverse()
print("".join(ans))

Solution (Python):
import sys
input = sys.stdin.readline
s=input().replace('\n','')
q=int(input())
num1=0
sento=''
matsubi=''

for i in range(q):
    query=input()
    if query[0]=='1':
        num1+=1
    else:
        query=query.replace('\n','')
        _,f,c=map(str,query.split())
        if f=='1':
            if num1%2==0:
                sento=c+sento
            else:
                matsubi+=c
        else:
            if num1%2==0:
                matsubi+=c
            else:
                sento=c+sento
ans=sento+s+matsubi
if num1%2==0:
    print(ans)
else:
    print(ans[::-1])

Solution (Python):
S = str(input())
Q = int(input())
s1 = ''
s2 = ''
reverse = False
for i in range(Q):
    query = list(map(str,input().split()))
    if query[0] == '1':
        if reverse == True:
            reverse = False
        else:
            reverse = True
    if query[0] == '2':
        if query[1] == '1':
            if reverse == False:
                s1 = query[2] + s1
            else:
                s2 = s2 + query[2]
        else:
            if reverse == True:
                s1 = query[2] + s1
            else:
                s2 = s2 + query[2]
S = s1 + S + s2
if reverse == True:
    S = S[::-1]
print(S)

Solution (Python):
def main():
    s=input()
    q=int(input())
    rot = 0
    A = ''
    B = ''
    for _ in range(q):
        t=input()
        if t=='1':
            rot += 1
        else:
            t = t.split()
            if (int(t[1])+rot)%2==1:
                A += t[2] 
            else:
                B  += t[2]
    if not rot%2:
        print(''.join(A[::-1]+s+B))
    else:
        print(''.join(B[::-1]+s[::-1]+A))        
main()

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())
li=[list(map(str,input().split())) for _ in range(q)]

moji = "0"
frag = True

for i in range(q):
    que = li[i]
    if que[0] == "1":
        if frag:
            frag = False
        else:
            frag = True
    else:
        f = que[1]
        c = que[2]
        if frag:
            if f == "1":
                moji = c+moji
            else:
                moji = moji+c
        else:
            if f == "1":
                moji = moji+c
            else:
                moji = c+moji


if frag:
    print(moji.replace("0",s))
else:
    moji = moji[::-1]
    print(moji.replace("0",s[::-1]))

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

is_reverse = False
b = ''
a = ''

for i in range(Q):
    q = input().split()
    if len(q) == 1:
        is_reverse = not is_reverse

    else:
        if (q[1] == '2' and not is_reverse) or (q[1] == '1' and is_reverse):
            a += q[2]
        else:
            b += q[2]

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

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

Solution (Python):
s = input()
q = int(input())
seq1 = ''
seq2 = ''
flg = 1
for i in range(q):
    ii = input().split()
    if ii[0] == '1':
        seq1, seq2 = seq2, seq1
        flg *= -1
    else:
        if ii[1] == '1':
            seq1 += ii[2]
        else:
            seq2 += ii[2]
print(seq1[::-1] + s[::flg] + seq2)

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

a = "0"
a_rev_cnt = 0
b = "0"
b_rev_cnt = 0

for _ in range(Q):
    query = input()
    if query[0]=="1":
        t = 1
        rev_cnt  = -1*rev_cnt+1
        a,b = b,a

    else:
        t,f,c = query.split()
        if f=="1":
            if rev_cnt ==0:
                a = c+a
            else:
                a = a+c

        elif f=="2":
            if rev_cnt ==0:
                b = b+c
            else:
                b = c+b
if a[0]=="0":
    a = a[::-1]
a = a[:-1]
if b[-1]=="0":
    b = b[::-1]
b = b[1:]


if rev_cnt%2==0:
    ans = a+S+b
else:
    ans = a+S[::-1]+b
print(ans)

Solution (Python):
ant = []
post = []
s = list(input())
q = int(input())
c = 0

for _ in range(q):
    l = input().split()
    if l == ['1']:
        c += 1
    elif l[1] == '1':
        if c % 2 == 0:
            ant.append(l[2])
        else:
            post.append(l[2])
    else:
        if c % 2 == 0:
            post.append(l[2])
        else:
            ant.append(l[2])

ans = ''
if c % 2 == 0:
    ant.reverse()
    for i in ant:
        ans += i
    for k in s:
        ans += k
    for j in post:
        ans += j
else:
    post.reverse()
    s.reverse()
    for i in post:
        ans += i
    for k in s:
        ans += k
    for j in ant:
        ans += j
        
print(ans)

Solution (Python):
from collections import deque


s = list(input())
q = int(input())
ans = deque(s)
cnt = 0
for i in range(q):
    arr = list(map(str, input().split()))
    if arr[0] == '1':
        cnt += 1
        cnt %= 2
    else:
        if (arr[1] == '1' and cnt == 0) or (arr[1] == '2' and cnt == 1):
            ans.appendleft(arr[2])
        else:
            ans.append(arr[2])
if cnt == 0:
    print(*ans, sep='')
else:
    ans.reverse()
    print(*ans, sep='')

Solution (Python):
from collections import deque

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

S = deque(S)

Query = [list(input()) for i in range(Q)]

rev_cnt = 0

for i in range(Q):
    if Query[i][0] == "1":
        rev_cnt += 1
    else:
        if Query[i][2] == "1":
            if rev_cnt % 2 == 1:
                S.append(Query[i][4])
            else:
                S.appendleft(Query[i][4])
        else:
            if rev_cnt % 2 == 1:
                S.appendleft(Query[i][4])
            else:
                S.append(Query[i][4])

if rev_cnt % 2 == 1:
    while len(S) != 0:
        print(S.pop(), end="")
else:
    while len(S) != 0:
        print(S.popleft(), end="")


print()

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

reverseNum = 0
initialTopAppend = ""
initialLastAppend = ""
for i in range(Q):
    l = input().split()
    if l[0] == "1":
        reverseNum += 1
        continue
    if reverseNum % 2 == 0:
        if l[1] == "1":
            initialTopAppend = l[2] + initialTopAppend
        else:
            initialLastAppend += l[2]
    else:
        if l[1] == "1":
            initialLastAppend += l[2]
        else:
            initialTopAppend = l[2] + initialTopAppend

ans = initialTopAppend + S + initialLastAppend

if reverseNum % 2 != 0:
    ans = ans[::-1]

print(ans)

Solution (Python):
s = input()
n = int(input())
x = []
y = []
r = 1
for i in range(n):
    q = input().split()
    if q[0] == '1':
        r = -r
    elif q[1] == '1':
        z = q[2][0]
        if r == 1:
            x.append(z)
        else:
            y.append(z)
    else:
        z = q[2][0]
        if r == -1:
            x.append(z)
        else:
            y.append(z)
if r == 1:
    x.reverse()
    print("".join(x) + s + "".join(y))
else:
    y.reverse()
    print("".join(y) + s[::-1] + "".join(x))

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':
            if not reverse:
                S.appendleft(q[2])
            else:
                S.append(q[2])
        else:
            if not reverse:
                S.append(q[2])
            else:
                S.appendleft(q[2])
if reverse:
    S.reverse()
print(''.join(S))

Solution (Python):
S = input()
Q = int(input())
mae = ''
ushiro = ''
rotate = False
for _ in range(Q):
  query = input()
  if query[0] == '1':
    rotate = not rotate
  else:
    if query[2] == '1':
      if rotate:
        ushiro = ushiro + query[4]
      else:
        mae = query[4] + mae
    else:
      if rotate:
        mae = query[4] + mae
      else:
        ushiro = ushiro + query[4]
print(mae + S + ushiro if not rotate else (mae + S + ushiro)[::-1])

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

mae = ""
usiro =""

r_num=0

for i in range(Q):
    query = (input())
    if query=="1":
        
        
        
        r_num+=1
    else:
        query = query.split(" ")
        if query[1] == "1":
            if r_num%2==0:
                mae = query[2]+mae
            else:
                usiro += query[2]
        else:
            if r_num %2 ==0:
                usiro += query[2]
            else:
                mae = query[2]+mae
if r_num%2 == 1:
    print(usiro[::-1]+S[::-1]+mae[::-1])
else:
    print(mae+S+usiro)

Solution (Python):
from collections import deque

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

reverse = False
count = 0
mae = deque()
ushiro = deque()
for i in range(q):
    qu = input()
    if qu[0] == "1":
        count += 1
        if reverse:
            reverse = False
        else:
            reverse = True
    else:
        _, f, c = qu.split()
        if reverse:
            if f == "1":
                ushiro.append(c)
            else:
                mae.appendleft(c)
        else:
            if f == "1":
                mae.appendleft(c)
            else:
                ushiro.append(c)

s = "".join(mae) + s + "".join(ushiro)

if count % 2:
    print(s[::-1])
else:
    print(s)

Solution (Python):
s = input()
qq = int(input())
qs = []
flag = True
for i in range(qq):
    l = input().split()
    qs.append(l)
    if l[0] == "1":
        
        flag = not(flag)


from collections import deque

preflag = flag
res = ""
pre = deque([])
suf = deque([])
for i in range(qq-1, -1, -1):
    q = qs[i]
    if q[0] == "1":
        flag = not(flag)

    elif q[0] == "2":
        if q[1] == "1":
            if flag:
                pre.append(q[2])
            else:
                suf.appendleft(q[2])

        elif q[1] == "2":
            if flag:
                suf.appendleft(q[2])
            else:
                pre.append(q[2])





if preflag:
    print("".join(pre) + s + "".join(suf))
else:
    print("".join(suf)[::-1] + s[::-1] + "".join(pre)[::-1])

Solution (Python):
s = input()
n = int(input())
query = [0]*n
rev = 0
for i in range(n):
    query[i] = list(map(str, input().split()))
    if query[i][0] == '1':
        rev = rev + 1

begin = ''
end = ''
pal = 0

for i in range(n):
    if int(query[i][0]) == 1:
        pal = (pal+1)%2
    else:
        if pal == 0:
            if int(query[i][1]) == 1:
                begin = query[i][2] + begin 
            else:
                end = end + query[i][2]
        else:
            if int(query[i][1]) == 1:
                end = end + query[i][2] 
            else:
                begin = query[i][2] + begin




if rev%2 == 1:
    print(end[::-1]+s[::-1]+begin[::-1])
else:
    print(begin+s+end)

Solution (Python):
import sys

S = input()

Q = int(input())
flag=0
mae = ''
matsu = ''

for i in range(Q):
    r = sys.stdin.readline().rstrip('\n')
    if r == '1':
        flag = 1 - flag
    else:
        ni, f, c = r.split()
        if f=='1':
            if flag == 0:
                mae += c
            else:
                matsu += c
        else:
            if flag == 0:
                matsu += c
            else:
                mae += c


if flag ==0:
    print(mae[::-1]+S+matsu)
else:
    print(matsu[::-1]+S[::-1]+mae)

Solution (Python):
from collections import deque
S = list(input())
Q = int(input())
que = [list(map(str, input().split())) for _ in range(Q)]

sq = deque(S)
cnt = 0

for i, lst in enumerate(que):
    if lst[0]=='1':
        cnt += 1
        continue
    if cnt%2==1:
        if lst[1]=='1':sq.append(lst[2])
        else:sq.appendleft(lst[2])
    else:
        if lst[1]=='1':sq.appendleft(lst[2])
        else:sq.append(lst[2])

ans = list(sq)
if cnt%2==1:
    ans.reverse()
print(''.join(ans))