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())
flip = 1
for i in range(q):
    T = input().split()
    if T[0] == '1':
        flip *= -1
    else:
        _flip = 1 if T[1]=='1' else -1
        if (flip * _flip)==1:
            s.appendleft(T[2])
        else:
            s.append(T[2])
print("".join(s) if flip==1 else "".join(reversed(s)))

Solution (Python):
s = input()
q = int(input())
querys = [list(map(str,input().split())) for _ in range(q)]

direction = 0
mae = ""
ato = ""

for i,query in enumerate(querys):
    if query[0] == "1":
        direction = (direction+1)%2
    else:
        if (int(query[1])-1 + direction)%2 == 0:
            mae += query[2]
        else:
            ato += query[2]

s = mae[::-1] + s
s = s + ato

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

Solution (Python):
s = str(input())
q = int(input())
cnt = 0
L = []
R = []
for _ in range(q):
    query = list(input().split())
    if query[0] == '1':
        cnt += 1
    else:
        if query[1] == '1':
            if cnt % 2:
                R.append(query[2])
            else:
                L.append(query[2])
        else:
            if cnt % 2:
                L.append(query[2])
            else:
                R.append(query[2])
s = (''.join(L[::-1]) + s + ''.join(R))
if cnt % 2:
    s = s[::-1]
print(s)

Solution (Python):
from collections import deque

def main():
    S = input()
    Q = int(input())
    
    SS = deque()
    for s in S:
        SS.append(s)
    
    cnt = 0
    for i in range(Q):
        query = input().split()
        if(query[0] == "1"):
            cnt += 1
        else:
            if(query[1] == "1"):
                if(cnt%2 == 0):
                    SS.appendleft(query[2])
                else:
                    SS.append(query[2])
            else:
                if(cnt%2 == 0):
                    SS.append(query[2])
                else:
                    SS.appendleft(query[2])
    
    ans = list(SS)
    if(cnt%2 == 0):
        print(''.join(ans))
    else:
        print(''.join(ans[::-1]))
main()

Solution (Python):



from collections import deque













def main():

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

    str_q = deque()

    for s in S:
        str_q.append(s)

    reverse = False
    for _ in range(Q):
        t, *query = input().split()
        if t == '1':
            reverse = not reverse
        else:
            f, c = query
            if f == '1':
                if reverse:
                    str_q.append(c)
                else:
                    str_q.appendleft(c)
            else:
                if reverse:
                    str_q.appendleft(c)
                else:
                    str_q.append(c)

    ans = list(str_q)
    if reverse:
        ans = ans[::-1]
    print(''.join(ans))


if __name__ == '__main__':
    main()

Solution (Python):
import sys

def ep(*params):
    print(*params,file=sys.stderr)

l = ''
r = ''

S = input().rstrip()

Q = int(input())
mode = 0
for _ in range(Q):
    ope = list(input().split())
    if ope[0] == '1':
        mode = (mode + 1)%2
    else:
        op = (int(ope[1]) + mode)%2
        if op == 1:
            l += ope[2]
        else:
            r += ope[2]
    

if mode == 0:
    print(l[::-1],S,r,sep="")    
else:
    print(r[::-1],S[::-1],l,sep="")

Solution (Python):
from collections import deque


def main():
    s = deque(list(input()))
    q = int(input())
    rev = False
    for _ in range(q):
        query = input().split()
        if query[0] == "1":
            if not rev:
                rev = True
            else:
                rev = False
        else:
            if query[1] == '1':
                pass
                if rev:
                    s.append(query[2])
                else:
                    s.appendleft(query[2])
            else:
                if rev:
                    s.appendleft(query[2])
                else:
                    s.append(query[2])
    if rev:
        s.reverse()
    print(*s, sep="")


if __name__ == "__main__":
    main()

Solution (Python):
from collections import deque

s = deque(list(map(str, input().rstrip())))
q = int(input())

state = 1
for _ in range(q):
    li = list(map(str, input().split()))
    if li[0] == "1":
        state *= -1
    elif (li[1] == "1" and state == 1) or (li[1] == "2" and state == -1):
        s.appendleft(li[2])
    else:
        s.append(li[2])

if state == -1:
    s.reverse()

print("".join(s))

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

from collections import deque 
def main():
    s = deque(readline().rstrip().decode())
    q = int(readline())
    dir = 1
    for _ in range(q):
        txt = readline().rstrip().decode()
        if txt[0] == '1':
            dir += 1
            dir%=2
        elif txt[0] == '2':
            f = txt[2]
            c = txt[4]
            if ((dir==1) and (f=='1')) or ((dir==0) and (f=='2')):
                s.appendleft(c)
            else:
                s.append(c)
    if dir==0:
        print(''.join(reversed(s)))
    else:
        print(''.join(s))
if __name__ == '__main__':
    main()

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

from collections import deque 
def main():
    s = deque(readline().rstrip().decode())
    q = int(readline())
    dir = 1
    for _ in range(q):
        txt = readline().rstrip().decode()
        if txt[0] == '1':
            dir ^= 1
        elif txt[0] == '2':
            f = txt[2]
            c = txt[4]
            if ((dir==1) and (f=='1')) or ((dir==0) and (f=='2')):
                s.appendleft(c)
            else:
                s.append(c)
    if dir!=1:
        print(''.join(reversed(s)))
    else:
        print(''.join(s))
if __name__ == '__main__':
    main()

Solution (Python):
from collections import deque
s = deque(input())
q = int(input())
rev = False
for _ in range(q):
    que = input()
    if que == "1":
        rev = not rev
    else:
        f,c = que[2:].split()
        if (f == "1") ^ rev:
            s.appendleft(c)
        else:
            s.append(c)

if rev: s.reverse()

print("".join(s))

Solution (Python):
S = input()
Q = int(input())
nT = 0
s1 = []
s2 = []
for i in range(Q):
  Query = input()
  if Query == "1":
    nT += 1
    continue
  T, F, C = Query.split()
  if (F == "1") ^ (nT%2 == 1):
    s1.append(C)
  else:
    s2.append(C)

r = "".join(reversed(s1))+S+"".join(s2)
if nT%2 == 1:
  r = "".join(reversed(r))
print(r)

Solution (Python):
s = input()
d = True
f = '' 
b = '' 
q = int(input())
for _ in range(q):
    a = input().split()
    if a[0] == '1':
        d = not d
    else:
        if a[1] == '1':
            if d:
                f = f + a[2]
            else:
                b = b + a[2]
        else:
            if d:
                b = b + a[2]
            else:
                f = f + a[2]

if not d:
    print(''.join([b[::-1], s[::-1], f]))
else:
    print(''.join([f[::-1], s, b]))

Solution (Python):
s = input()
d = True
f = '' 
b = '' 
q = int(input())
for _ in range(q):
    a = input().split()
    if a[0] == '1':
        d = not d
    else:
        if a[1] == '1':
            if d:
                f = ''.join([a[2], f])
            else:
                b = ''.join([b, a[2]])
        else:
            if d:
                b = ''.join([b, a[2]])
            else:
                f = ''.join([a[2], f])

if not d:
    print(''.join([b[::-1], s[::-1], f[::-1]]))
else:
    print(''.join([f, s, b]))

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

tag = False
que = deque([S])

for i in range(Q):
    if query[i][0] == str(1):
        if tag == False:
            tag = True
        else:
            tag = False
    elif query[i][0] == str(2):
        if query[i][1] == str(1):
            if tag == False:
                que.appendleft(query[i][2])
            else:
                que.append(query[i][2])
        else:
            if tag == False:
                que.append(query[i][2])
            else:
                que.appendleft(query[i][2])
ans = "".join(que)
if tag == True:
    ans = ans[::-1]
print(ans)

Solution (Python):
S = input()
Q = int(input())
 
a = []
b = []
rev = 0
 
for _ in range(Q):
    q = input().split()
    if q[0] == '1':
        a, b = b, a
        rev += 1
    else:
        if q[1] == '1':
            a.append(q[2])
        else:
            b.append(q[2])
if rev % 2 == 1:
    S = S[::-1]
ans = ''.join(a[::-1]) + S + ''.join(b)
print(ans)

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

reverse=0
Top=[]
Bottom=[]
for i in range(q):
    if Q[i][0]=="1":
        reverse+=1
    elif Q[i][0]=="2" and reverse%2==0:
        if Q[i][1]=="1":
            Top.append(Q[i][2])
        elif Q[i][1]=="2":
            Bottom.append(Q[i][2])
    elif Q[i][0]=="2" and reverse%2==1:
        if Q[i][1]=="2":
            Top.append(Q[i][2])
        elif Q[i][1]=="1":
            Bottom.append(Q[i][2])
if reverse%2==1:
    Top.reverse()
    SS=Top+S+Bottom
    SS.reverse()
elif reverse%2==0:
    Top.reverse()
    SS=Top+S+Bottom
for i in SS:
    print(i,end="")

Solution (Python):


from collections import deque


def main():
    chars = deque(input())
    chars_append = chars.append
    chars_appendleft = chars.appendleft
    n = int(input())
    direction = 1
    for _ in range(n):
        query = input().split()
        if query[0] == '1':
            direction *= (-1)  
        else:
            if query[1] == '1':
                
                head = direction
            else:
                head = (-direction)
            if head == 1:
                chars_appendleft(query[2])
            else:
                chars_append(query[2])

    if direction == -1:
        chars.reverse()
    return ''.join(chars)


if __name__ == '__main__':
    print(main())

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

for _ in range(q):
    buf = input()
    if buf[0]=='1':
        if r:
            r=False
        else :
            r=True
    elif buf[0]=='2':
        c=buf.split()
        if (c[1]=='1' and r) or (c[1]=='2' and r==False):
            d.appendleft(c[2])
        else:
            d.append(c[2])
if r==False:
    d.reverse()
for i in d:
    print(i,end='')

Solution (Python):
from collections import deque
 
s = input()
q = int(input())
 
query = [input().split() for i in range(q)]
 
a = deque([s])
 
flag = True
 
for i in range(q):
    if query[i][0] == "1":
        flag = not(flag)
    else:
        if (query[i][1] == "1" and flag) or (query[i][1] != "1" and not(flag)):
            a.appendleft(query[i][2])
        else:
            a.append(query[i][2])
 
b = "".join(a)
if not(flag):
    b = b[::-1]
 
print(b)

Solution (Python):
from collections import deque
s = deque(input().replace('\n', ''))
q = int(input())
flag = True
for i in range(q):
    query = list(map(str, input().split()))
    if query[0]=='1':
        if flag: flag = False
        else: flag = True
    else:
        
        if (not flag and query[1]=='1') or flag and query[1]=='2':
            s.append(query[2])
        else:
            s.appendleft(query[2])

s = list(s)
if not flag:
    s = s[::-1]

print(''.join(s))

Solution (Python):
from collections import deque
s = deque(input().replace('\n', ''))
q = int(input())
flag = True
for i in range(q):
    query = list(map(str, input().split()))
    if query[0]=='1':
        if flag: flag = False
        else: flag = True
    else:
        
        if (not flag and query[1]=='1') or flag and query[1]=='2':
            s.append(query[2])
        else:
            s.appendleft(query[2])
s = ''.join(s)
print(s if flag else s[::-1])

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

for i in range(Q):
    query = input().split()
    if len(query) == 1:
        parity += 1
    else:
        if int(query[1]) == 1 + (parity % 2):
            S.appendleft(query[2])
        else:
            S.append(query[2])
S = ''.join([c for c in S])
if parity % 2 == 1:
    S = S[::-1]
print(S)

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

tmp = 1
for i in range(q):
    x = input()
    if x[0]==str(1):
        tmp*=-1
    else:
        t,f,c = x.split()
        t,f = int(t),int(f)
        if tmp==-1:
            if f==1:
                queue.append(c)
            if f==2:
                queue.appendleft(c)
        if tmp==1:
            if f==1:
                queue.appendleft(c)
            if f==2:
                queue.append(c)

print(''.join(queue)[::tmp])

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

reverse = 0
b = []
a = []

for i in range(Q):
  if len(Query[i]) == 1:
    reverse += 1
  else:
    if Query[i][1] == '1':
      if reverse % 2 == 0:
        b.append(Query[i][2])
      else:
        a.append(Query[i][2])
    else:
      if reverse % 2 == 0:
        a.append(Query[i][2])
      else:
        b.append(Query[i][2])
        
ans = ''
s = (''.join(b))[::-1] + S + ''.join(a)
if reverse % 2 == 0:
  ans = s
else:
  ans = s[::-1]
  
print(ans)

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

Q = [list(input().split()) for _ in range(N)]

count = 0
c = 0

ans_append = []
ans_insert = []

for q in Q:
    if q[0] == "1":
        count += 1
        c = count % 2
    else:
        if (c == 0 and q[1] == "1") or (c == 1 and q[1] == "2"):
            ans_insert.append(q[2])
        elif (c == 0 and q[1] == "2") or (c == 1 and q[1] == "1"):
            ans_append.append(q[2])


if count % 2  == 1:
    ans = ans_append[::-1] + S[::-1] + ans_insert
    print("".join(ans))
else:
    ans = ans_insert[::-1] + S + ans_append
    print("".join(ans))

Solution (Python):
txt = input()
n = (int)(input())
count = 0
sentou = ""
saigo = ""
for i in range(n):
  temp = input()
  if temp[0] == '1':
    count += 1
  else:
    sec, moji = (int)(temp.split(" ")[1]), temp.split(" ")[2]
    if (sec + count) % 2 == 1:
      sentou = moji + sentou
    else:
      saigo += moji
if count % 2 == 1:
  txt = saigo[::-1] + txt[::-1] + sentou[::-1]
else:
  txt = sentou + txt + saigo
print(txt)

Solution (Python):
def main():
    S = input()
    Q = int(input())
    rev = False 
    tail = ""
    head = ""
    for i in range(Q):
        query = input()
        if len(query)==1:
            rev = not rev
        else:
            tmp = list(query.split())
            f = int(tmp[1])
            c = tmp[2]
            if rev:
                if f==1:
                    tail+=c
                else:
                    head=c+head
            else:
                if f==1:
                    head=c+head
                else:
                    tail+=c

    ans = tail[::-1]+S[::-1]+head[::-1] if rev else head+S+tail
    print(ans)

main()

Solution (Python):
from collections import deque
s = deque(input())
q = int(input())
rev = False
for _ in range(q):
    inp = [x for x in input().split()]
    if inp[0] == "1":
        rev = not rev
    else:
        if inp[1] == "1":
            if rev:
                s.append(inp[2])
            else:
                s.appendleft(inp[2])
        else:
            if rev:
                s.appendleft(inp[2])
            else:
                s.append(inp[2])

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

Solution (Python):
S = input()
Q = int(input())
Query = [list(map(str,input().split())) for _ in range(Q)]
from collections import deque 
flag = True
f = ""
l = ""

for i in Query:
    if i[0]=="1":
        if flag:
            flag = False
        else:
            flag = True
    else:
        if (flag==True and i[1]=="1") or (flag==False and i[1]=="2"):
            f += i[2] 
        elif (flag==True and i[1]=="2") or (flag==False and i[1]=="1"):
            l += i[2]
S = f[::-1]+S+l
if flag:
    print(S)
else:
    print(S[::-1])

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

from collections import deque

deq=deque(s)

cnt=0
for _ in range(q):
    query=input().split()
    if int(query[0])==1:
        cnt+=1
    elif int(query[0])==2:
        if int(query[1])==1:
            if cnt%2==0:
                deq.appendleft(query[2])
            else:
                deq.append(query[2])
        else:
            if cnt%2==0:
                deq.append(query[2])
            else:
                deq.appendleft(query[2])

if cnt%2==0:
    ans=''.join(list(deq))
else:
    ans=''.join(list(deq)[::-1])

print(ans)

Solution (Python):
from collections import deque
S = deque([input()])
Q = int(input())
is_reverse = False

for _ in range(Q):
  inp = input().split()
  if len(inp) == 1:
    if is_reverse:
      is_reverse = False
    else:
      is_reverse = True
  else:
    _, f, c = inp
    if f == '1':
      if is_reverse:
        S.append(c)
      else:
        S.appendleft(c)
    else:
      if is_reverse:
        S.appendleft(c)
      else:
        S.append(c)
if is_reverse:
  ans = "".join(S)
  print(ans[::-1])
else:
  print("".join(S))

Solution (Python):
s = input()
q = int(input())
turn = 1
start = []
end = []
for i in range(q):
    q_list = list(input().split())
    if q_list[0] == "1":
        turn *= -1
    else:
        if q_list[1] == "1":
            if turn == 1:
                start.append(q_list[2])
            else:
                end.append(q_list[2])
        else:
            if turn == -1:
                start.append(q_list[2])
            else:
                end.append(q_list[2])

dum1 = "".join(start)
dum2 = "".join(end)
if turn == 1:
    print(dum1[::-1]+s+dum2)
else:
    print(dum2[::-1] + s[::-1] + dum1)

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

state = 1 
Stock = deque([S])
for i in range(Q):
    q = input()
    if q == '1':
        state *= -1
    else:
        Y, f, c = q.split()
        if (state == 1 and f == '1') or (state == -1 and f == '2'):
            Stock.appendleft(c)
        elif (state == 1 and f == '2') or (state == -1 and f == '1'):
            Stock.append(c)
S = ''.join(Stock)
if state == -1:
    S = S[::-1]
print(S)

Solution (Python):
s = input()
qn = int(input())
left = ''
right = s
for i in range(qn):
    q = input()
    if q[0] == '1':
        left, right = right, left
    else:
        _, f, c = q.split()
        if f == '1':
            left += c
        else:
            right += c
print(left[::-1] + right)

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

n = int(input())
l = []

base = 1

for i in range(n):
  l=list(input().split())
  if l[0] == '1':
    base = base *-1
  else:
    if l[1] =='1' and base == 1 :
      s.appendleft(l[2])
    elif l[1] =='2' and base == 1 :
      s.append(l[2])
    elif l[1] =='1' and base == -1 :
      s.append(l[2])
    elif l[1] =='2' and base == -1 :
      s.appendleft(l[2])

if base ==-1:
  s=''.join(list(reversed(s)))
      
print("".join(s))

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

a = 0
for i in range(Q):
    t = input()
    if t == "1":
        if a == 0:
            a = 1
        else:
            a = 0
    else:
        t,f,c = t.split()
        if f == "1":
            if a == 0:
                S.appendleft(c)
            else:
                S.append(c)
        else:
            if a == 0:
                S.append(c)
            else:
                S.appendleft(c)
            
if a == 1:
    S = reversed(S)
    
S = ''.join(S)
print(S)

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

a = []
b = []
rev = 0

for _ in range(Q):
    q = input().split()
    if q[0] == '1':
        a, b = b, a
        rev += 1
    else:
        if q[1] == '1':
            a.append(q[2])
        else:
            b.append(q[2])



if rev % 2 == 1:
    S = S[::-1]
ans = ''.join(a[::-1]) + S + ''.join(b)
print(ans)

Solution (Python):
import sys

input_methods=['clipboard','file','key']
using_method=0
input_method=input_methods[using_method]

tin=lambda : map(int, input().split())
lin=lambda : list(IN())
mod=1000000007



def main():
	
	
	s = input()
	q = int(input())
	ff = []
	bb = []
	frip = False
	for _ in range(q):
		ss = input()
		if ss[0] == '1':
			frip = not frip
		else:
			_a, f, c = ss.split()
			is_ff = ((1 if frip else 0) + (1 if f == '1' else 0 )) % 2 == 1
			if is_ff:
				ff.append(c)
			else:
				bb.append(c)
	
	ret = ''.join(ff[::-1]) + s + ''.join(bb)
	if frip:
		ret = ret[::-1]
	print(ret)
			
	
	
	

isTest=False

def pa(v):
	if isTest:
		print(v)
		
def input_clipboard():
	import clipboard
	input_text=clipboard.get()
	input_l=input_text.splitlines()
	for l in input_l:
		yield l

if __name__ == "__main__":
	if sys.platform =='ios':
		if input_method==input_methods[0]:
			ic=input_clipboard()
			input = lambda : ic.__next__()
		elif input_method==input_methods[1]:
			sys.stdin=open('inputFile.txt')
		else:
			pass
		isTest=True
	else:
		pass
		
			
	ret = main()
	if ret is not None:
		print(ret)

Solution (Python):

from collections import deque

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

b_flag = False
for i in range(q):
    _list = [x for x in input().split()]
    if _list[0] == "1":
        if b_flag:
            b_flag = False
        else:
            b_flag = True
    else:
        if _list[1] == "1":
            if b_flag:
                s.append(_list[2])
            else:
                s.appendleft(_list[2])
        else:
            if b_flag:
                s.appendleft(_list[2])
            else:
                s.append(_list[2])

if b_flag:
    s.reverse()

print(*s,sep="")

Solution (Python):
from collections import deque

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

query = [input().split() for i in range(q)]

a = deque([s])

flag = True

for i in range(q):
    if query[i][0] == "1":
        flag = not(flag)
    else:
        if (query[i][1] == "1" and flag) or (query[i][1] != "1" and not(flag)):
            a.appendleft(query[i][2])
        else:
            a.append(query[i][2])

b = "".join(a)
if not(flag):
    b = b[::-1]

print(b)

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

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

u = ''
t = ''

r = False
for _ in range(Q):
    q = readline().split()
    if q[0] == '1':
        u, t = t, u
        r = not r
    else:
        if q[1] == '1':
            t += q[2]
        else:
            u += q[2]
            
if r:
    print(t[::-1]+S[::-1]+u)
else:
    print(t[::-1]+S+u)

Solution (Python):
s = input()
top = ''
q = int(input())
isReverse = False
for i in range(q):
    que = input().split()
    if(que[0] == '1'):
        isReverse = not isReverse
    else:
        if que[1] == '1':
            if isReverse:
                s += que[2]
            else:
                top += que[2]
        else:
            if isReverse:
                top += que[2]
            else:
                s += que[2]
if isReverse:
    s = s[::-1] + top
else:
    s = top[::-1] + s

print(s)

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

f = ""
l = ""
count = 0
for i in range(Q):
    if q[i][0] == "1":
        count += 1
    elif q[i][0] == "2":
        if count % 2 == 0:
            if q[i][1] == "1":
                f = q[i][2] + f
            else:
                l = l + q[i][2]
        else:
            if q[i][1] == "1":
                l = l + q[i][2]
            else:
                f = q[i][2] + f
        
if count % 2 == 0:
    print(f+S+l)
else:
    print((f+S+l)[::-1])

Solution (Python):
s = input()
q = int(input())
results_forward = str("")
results_back = str("")
qlist = []
for i in range(q):
  qlist.extend(input().split(" "))

amount = 0
for i in range(len(qlist)):
  if qlist[i] == "1" or qlist[i] == "2":
    amount += int(qlist[i])
  elif amount%2 == 1:
    results_forward = qlist[i] + results_forward
    amount -= int(qlist[i-1])
  elif amount%2 == 0:
    results_back = results_back + qlist[i]
    amount -= int(qlist[i-1])

if q%2 == (len(results_forward)+len(results_back))%2:
  print(results_forward+s+results_back)
else:
  print((results_forward+s+results_back)[::-1])

Solution (Python):
from collections import deque
s=deque(input())
q=int(input())
flag=0
for _ in range(q):
  query=input().split()
  if len(query)==1:
    flag=(flag+1)%2
  else:
    if (int(query[1])+flag)%2:
      s.appendleft(query[2])
    else:
      s.append(query[2])
print(''.join(s) if flag==0 else ''.join(reversed(s)))

Solution (Python):
from collections import deque
s = input()
sque = deque(s)
q = int(input())
que = [0] * q
flg = False
for i in range(q):
    que[i] = (list(map(str,input().split())))
    if(len(que[i])==3):
        if que[i][1] == '1':
            if flg:
                sque.append(que[i][2])
            else:
                sque.appendleft(que[i][2])
        else:
            if flg:
                sque.appendleft(que[i][2])
            else:
                sque.append(que[i][2])
    else:
        if flg:
            flg = False
        else:
            flg = True
if flg:
    sque.reverse()
    print("".join(list(sque)))
else:
    print("".join(list(sque)))

Solution (Python):
S = input()
Q = int(input())
rev = 0
top = []
bot = []

for _ in range(Q):
  a = list(input().split())
  if a[0] == "1":
    rev += 1
  else:
    if rev%2 == 0:
      if a[1] == "1":
        top.append(a[2])
      else:
        bot.append(a[2])
    else:
      if a[1] == "2":
        top.append(a[2])
      else:
        bot.append(a[2])
      
t = "".join(top)[::-1]
b = "".join(bot)
S = "".join([t,S,b])
if rev%2 == 1:
  S = S[::-1]
print(S)

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

from collections import deque

q = deque(S)

r = 0  

for i in range(Q):
    if Query[i][0] == '1':
        r = 1-r
    else:
        if (r + int(Query[i][1])) % 2 == 1:
            q.appendleft(Query[i][2])
        else:
            q.append(Query[i][2])

A = list(q)

if r % 2 == 1:
    A.reverse()

ans = ''
for i in range(len(A)):
    ans += A[i]

print(ans)

Solution (Python):

import sys
import time
import unittest
from io import StringIO


def resolve():
    S = input()
    Q = int(input())
    reverseFlg = False
    preR = ""
    post = ""
    for i in range(Q):
        QQ = input().split()
        if QQ[0] == "1":
            reverseFlg = not reverseFlg
        else:
            if reverseFlg:
                if QQ[1] == "1":
                    post += QQ[2]
                else:
                    preR += QQ[2]
            else:
                if QQ[1] == "1":
                    preR += QQ[2]
                else:
                    post += QQ[2]

    if reverseFlg:
        print(post[::-1] + S[::-1] + preR)
    else:
        print(preR[::-1] + S + post)


class TestClass(unittest.TestCase):
    def assertIO(self, input, output):
        stdout, stdin = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(input)
        resolve()
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, stdin
        self.assertEqual(out, output)

    def checkTLE(self, input):
        stdout, stdin = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(input)
        resolve()
        sys.stdout.seek(0)
        sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, stdin

    def debugIO(self, input):
        stdin = sys.stdin
        sys.stdin = StringIO(input)
        resolve()
        sys.stdin = stdin

    def setUp(self):
        self.startTime = time.time()

    def tearDown(self):
        t = time.time() - self.startTime
        print('%s: %.3f' % (self.id(), t), flush=True)

    def test_入力例_1(self):
        input = """a
4
2 1 p
1
2 2 c
1"""
        output = """cpa"""
        self.assertIO(input, output)

    def test_入力例_2(self):
        input = """a
6
2 2 a
2 1 b
1
2 2 c
1
1"""
        output = """aabc"""
        self.assertIO(input, output)

    def test_入力例_3(self):
        input = """y
1
2 1 x"""
        output = """xy"""
        self.assertIO(input, output)


if __name__ == "__main__":
    if "IS_LOCAL" in locals():
        suite = unittest.TestLoader().loadTestsFromTestCase(TestClass)
        unittest.TextTestRunner(verbosity=0).run(suite)
    else:
        resolve()

Solution (Python):
from collections import deque

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

rev_cnt = 0
rev = 0
for i in range(Q):
  query = input().split()
  if query[0]=="1":
    rev_cnt += 1
    rev = rev_cnt % 2
  elif query[0]=="2":
    
    s=query[2]
    if query[1] == "1" and rev == 0:
      queue.appendleft(s)
    elif query[1] == "1" and rev == 1:
      queue.append(s)
    elif query[1] == "2" and rev == 0:
      queue.append(s)
    elif query[1] == "2" and rev == 1:
      queue.appendleft(s)
if rev == 0:
  print(''.join(list(queue)))
else:
  print(''.join(reversed(list(queue))))

Solution (Python):
s=input()
q_=int(input())
q=[input() for _ in range(q_)]
reverse=False
pre=[]
fin=[]
for qi in q:
    if qi[0]=='1':
        reverse=not reverse
    elif qi[2]=='1' and reverse==False:
        pre.append(qi[4])
    elif qi[2]=='1' and reverse==True:
        fin.append(qi[4])
    elif qi[2]=='2' and reverse==False:
        fin.append(qi[4])
    elif qi[2]=='2' and reverse==True:
        pre.append(qi[4])
if reverse==False:
    print(''.join([p for p in pre[::-1]])+s+''.join(fin))
else:
    print(''.join([f for f in fin[::-1]])+''.join([si for si in list(s)[::-1]])+''.join(pre))