peterverebics/CodeNet_Python_118 huggingface dataset:
from collections import deque
S = deque(input())
N = int(input())
is_inversed = False
for i in range(N):
    Q = input()
    if Q[0] == '1':
        is_inversed = not (is_inversed)
    else:
        if (Q[2] == '1' and is_inversed) or (Q[2] == '2' and not (is_inversed)):
            S.append(Q[4])
        else:
            S.appendleft(Q[4])
if is_inversed:
    print(''.join(list(S)[::-1]))
else:
    print(''.join(S))
s = input()
q = int(input())

reverse_flag = 0
head = ""
back = ""
for _ in range(q):
    query = input()
    if query[0] == "1":
        reverse_flag ^= 1
    else:
        t,f,c = query.split()
        f = int(f) + reverse_flag
        if f%2 == 1:
            head = c + head
        else:
            back = back + c
s = head + s + back
print(s if reverse_flag%2 == 0 else s[::-1])
from collections import deque
s=input()
n = int(input())

f=False
q= deque(s)

for _ in range(n):
    i = input()

    if i[0] == '1': f=False if f else True
    else:
        _,a,b =i.split()
        
        if f :
            if a == '1':q.append(b)
            else:q.appendleft(b)
        else:
            if a == '1':q.appendleft(b)
            else:q.append(b)

if f: q.reverse()

for i in q:print(i,end='')
from collections import deque
import sys

input = lambda: sys.stdin.readline().rstrip()

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

is_reverse = False
for i in range(q):
    query = input().split()
    if query[0] == '2':
        if (query[1] == '1' and not is_reverse) or (query[1] == '2' and is_reverse):
            s.appendleft(query[2])
        else:
            s.append(query[2])
    else:
        is_reverse = not is_reverse

result = ''.join(s)
if is_reverse:
    print(result[::-1])
else:
    print(result)
from collections import deque

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

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

S = list(S)
if flip == 0:
    print(*S, sep="")
else:
    print(*S[::-1], sep="")
from collections import deque
S=deque(input())
q=int(input())
rev=False
for i in range(q):
  c=list(input().split())
  if c[0]=='1':
    rev=not rev
  elif c[1]=='1' and rev==False:
    S.appendleft(c[2])
  elif c[1]=='2' and rev==True:
    S.appendleft(c[2])
  else:
    S.append(c[2])
if rev:
  S.reverse()
ans=''.join(S)
print(ans)
from collections import deque
s=deque(list(input()))
q=int(input())
l = [list(input().split()) for i in range(q)]
c=0
for i in range(q):
  if l[i][0]=="1":
    c+=1
  elif c%2==0 and l[i][1]=="1":
    s.appendleft(l[i][2])
  elif c%2==0:
    s.append(l[i][2])
  elif c%2==1 and l[i][1]=="1":
    s.append(l[i][2])
  else:
    s.appendleft(l[i][2])
s=list(s)
if c%2==1:
  s=s[::-1]
print(*s, sep="")
from collections import deque
s=deque(input())
q=int(input())
cnt=0
for i in range(q):
    query=list(input().split())
    if query[0]=="1":
        cnt+=1
    else:
        if (cnt+int(query[1]))%2==1:
            s.appendleft(query[2])
        else:
            s.append(query[2])
if cnt%2==1:
    s=reversed(s)
print("".join(s))
from collections import deque
s=deque(input())
q=int(input())
flag = True

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

if not flag:
    s.reverse()

print("".join(s))
from collections import deque

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

l_q = []
for i in range(q):
  l_q.append( list(input().split())  ) 

#クエリ処理
c = 0
for i in l_q:

  if i[0] == "1":
    c = (c+1)%2

  else:
    if (c+int(i[1]))%2 ==1:
      s.appendleft(i[2])
    else:
      s.append(i[2])

s=list(s)

if c == 1:
  s.reverse()

s = ''.join(s)
print(s)
from collections import deque
S = deque(input())
Q = int(input())

reverse = False
for _ in range(Q):
    query = input().split()
    if query[0] == "1":
        reverse = not reverse
    else:
        tail = bool(int(query[1]) - 1) ^ reverse
        c = query[2]
        if tail:
            S.append(c)
        else:
            S.appendleft(c)

print("".join(reversed(S)) if reverse else "".join(S))
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)
# coding: utf-8
import sys
from collections import deque

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

S = deque(sr())
Q = ir()
rev = 0
for _ in range(Q):
    q = sr().split()
    if q[0] == '1':
        rev ^= 1
    else:
        _, f, c = q
        if (int(f)^rev)&1:
            S.appendleft(c)
        else:
            S.append(c)

if rev&1:
    S.reverse()

print(''.join(S))
# 06
s = input()
q = int(input())
t = 0
form = ""
late = ""
for i in range(q):
  l = list(input().split())
  if l[0] == "1":
    t += 1
  else:
    if t % 2 == 0:
      if l[1] == "1":
        form = l[2] + form
      else:
        late += l[2]
    else:
      if l[1] == "1":
        late += l[2]
      else:
        form = l[2] + form
if t % 2 == 0:
  ans = form + s + late
else:
  ans = late[::-1] + s[::-1] + form[::-1]
print(ans)
S = input()
Q = int(input())

forward = True
que0 = []
que1 = []

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

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

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

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

if forward:
    print(S)
else:
    print(S[::-1])
s = input()
q = int(input())

add_s = ['', ''] #pre, post
rev_flag = 0

for i in range(q):
    query = input()
    if query == '1':
        rev_flag = (rev_flag+1)%2
    else:
        _, f, c = query.split()
        if f == '1':
            add_s[rev_flag] += c
        else:
            add_s[(rev_flag+1)%2] += c

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

print(add_s[rev_flag][::-1]+s+add_s[(rev_flag+1)%2])
s = input()
q = int(input())
r = False#ひっくり返されたか
f = ''#in front of#逆転しているので最後に戻しましょう
b = '' #behind
for i in range(q):
  t=list(input().split())
  if len(t) == 1:#一つしか入力されていない
    r = not(r)
  else:
    if (t[1] == '1' and r == False):
      f += t[2]
    elif (t[1] == '2' and r == True):
      f += t[2]
    else:
      b += t[2]
s = f[::-1] + s + b
if r:
  print(s[::-1])
else:
  print(s)
from collections import deque

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

t = 0
for i in range(q):
    o = list(map(str, input().split()))
    if o[0] == "1":
        t += 1
    else:
        if o[1] == "1" and t%2 == 0 or o[1] == "2" and t%2 == 1:
            s.appendleft(o[2])
        else:
            s.append(o[2])

if t%2 == 1:
    s.reverse()

print("".join(s))
from collections import deque

s = deque(input())
Q = int(input())
    
t = True
    
for i in range(Q):
    query = input().split()
    if query[0] == '1':
        t = not t
    else:
        if(t and query[1] == '1') or (not t and query[1] == '2'):
            s.appendleft(query[2])
        else:
            s.append(query[2])
            
s = ''.join(s)
    
if not t:
    s = s[::-1]
        
print(s)
s = input()
q = int(input())
x = 1
f, b = '', ''
for i in range(q):
  a = input().split()
  if a[0] == '1':
    x = -x
  elif (1.5-int(a[1]))*x > 0:
    f = a[2] + f
  else:
    b = b + a[2]
s = f + s + b
if x == -1:
  s = s[::-1]
print(s)
right = input()
Q = int(input())
flag = 1
left = ""
for i in range(Q):
  q = input()
  if q == "1":
    flag = 1-flag
    continue
  m,f,c = q.split(" ")
  f = int(f)
  left += c*(1-flag)*(f-1)
  right += c*flag*(f-1)
  left += c*flag*(2-f)
  right += c*(1-flag)*(2-f)
if flag == 1:
  print(left[::-1]+right)
else:
  print(right[::-1]+left)
s = input()
back = ""
q = int(input())
rev = False
for i in range(q):
    inpt = input().split( )
    if len(inpt)>1:
        fi,ci = inpt[1],inpt[2]
        if not ((fi == "1") ^ rev):
            s += ci
        else:
            back += ci
    else:
        rev = not rev
back = back[::-1]
ans = back+s
if not rev:
    print(ans)
else:
    ans = ans[::-1]
    print(ans)
from collections import deque

S = input()
Q = int(input())
head = 1
d = deque([S])

for _ in range(Q):
    q = input().split()
    if q[0]=='1':
        head = head*-1
    else:
        if q[1]=='1':
            h = head*1
        else:
            h = head*-1
        
        if h==1:
            d.appendleft(q[2])
        else:
            d.append(q[2])
ans = "".join(list(d))
if head == -1:
    ans = ans[::-1]

print(ans)
from collections import deque
s=deque(input());q=int(input());rev=0
for i in range(q):
    p=list(map(str,input().split()))
    if p[0]=="1":rev^=1
    else:
        z,f,c=p
        if f=="1":
            if rev:s.append(c)
            else:s.appendleft(c)
        else:
            if rev:s.appendleft(c)
            else:s.append(c)
s=list(s)[::1-2*rev]
print("".join(s))
s,_,*q=open(0)
a='%s'
p=1
for q in q:
  try:
    q,f,c=q.split()
    if(f<'2')^p:a+=c
    else:a=c+a
  except:p^=1
print((a%s[:-1])[::p or-1])
from collections import deque

s = input()
Q = int(input())

dq = deque(list(s))

reversed_state = False
  
for q in range(Q):
  query = input().split()
  if len(query) == 1:
    reversed_state = not reversed_state
  else:
    T,F,c = list(query)
    if F == '1':
      if reversed_state == False:
      	dq.appendleft(c)
      else:
        dq.append(c)
    else:
      if reversed_state == False:
        dq.append(c)
      else:
        dq.appendleft(c)
      
print(''.join(dq)) if reversed_state == False else print(''.join(list(dq)[::-1]))
s=input()
q=int(input())

rev = 1
fr = ""
rr = ""

for i in range(q):
    ss=input()
    if int(ss[0])==1:
        rev *= -1
    else:
        f,c=int(ss[2]),ss[4]
        if f == 2:
            f = -1
        
        if f*rev ==1:
            fr+=c
        else:
            rr+=c

if rev == 1:
    ans = fr[::-1]+s+rr
else:
    ans = rr[::-1]+s[::-1]+fr
print(ans)
#２つのリストを使う場合
rL=list(input())#右のリストに入力
lL=[]
Q=int(input())
for i in range(Q):
  N=list(input().split())
  if N[0]=="1":#反転させる場合は右と左を入れ替える
    lL,rL=rL,lL
  elif N[1]=="1":#先頭の場合は左のリストに
    lL.append(N[2])
  else:#末尾の場合は右のリストに
    rL.append(N[2])
lL.reverse()#左のリストの向きを入れ替える
print("".join(lL)+"".join(rL))
import sys
input = lambda: sys.stdin.readline().rstrip()
s=[i for i in input()]
x=0 ; st=[[],[]]
for _ in range(int(input())):
    inp=list(map(str,input().split()))
    if len(inp)==1: x^=1
    else: st[(int(inp[1])-1)^x].append(inp[2])
if x==0:
    st[0].reverse()
    ans=st[0]+s+st[1]
elif x==1:
    st[1].reverse()
    s.reverse()
    ans=st[1]+s+st[0]
print(''.join(ans))
from collections import deque
s = input()
q = int(input())

s = deque(s)
t = 0
for _ in range(q):
  query = list(map(str, input().split()))
  if query[0] == "1": t = 1 - t
  else:
    if t == 0:
      if query[1] == "1": s.appendleft(query[2])
      elif query[1] == "2": s.append(query[2])
    elif t == 1:
      if query[1] == "1": s.append(query[2])
      elif query[1] == "2": s.appendleft(query[2])
if t == 1:print("".join(s)[::-1])
else: print("".join(s))
s = input()
n = int(input())
t = ''
for i in range(n):
    q = input().split()
    if len(q) == 1:
        t, s = s, t
    elif len(q) == 3:
        if q[1] == '1':
            t += q[2]
        elif q[1] == '2':
            s += q[2]

t = t[::-1]
print(t + s)
# coding: utf-8
from collections import deque
S = deque(input())
Q = int(input())
cnt = 0
for i in range(Q):
    q = input().split()
    if q[0] == "1":
        cnt += 1
    else:
        if (int(q[1]) + cnt) % 2 == 1:
            S.appendleft(q[2])
        else:
            S.append(q[2])
ans = "".join(S)
print(ans if cnt % 2 == 0 else ans[::-1])
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)
from collections import deque
s=deque(input())
q=int(input())
cnt=0
for i in range(q):
  t=input().split()
  if t[0]=="1":
    cnt+=1
  else:
    if (int(t[1])+cnt)%2==1:
      s.appendleft(t[2])
    else:
      s.append(t[2])
      
if cnt%2==1:
  s.reverse()
print("".join(s))
s=input()
n=int(input())
front=""
back=s
reverse=False

for i in range(n):
  t=input().split()
  if t[0]=="1":
    reverse=not reverse
  else:
    if (t[1]=="1")^reverse:
      front=front+t[2]
    else:
      back=back+t[2]
if reverse:
  print(back[::-1]+front)
else:
  print(front[::-1]+back)
from collections import deque

s = input()
q = int(input())
Q = [input() for _ in range(q)]

s = deque(list(s))
insert = s.appendleft
append = s.append
flag = True

for query in Q:
    if query[0] == '1':
        flag = not flag
    else:
        if (query[2] == '1' and flag) or (query[2] == '2' and not flag):
            insert(query[4])
        else:
            append(query[4])

if not flag:
    s.reverse()

print(*s, sep='')
S = input()
Q = int(input())
cond = 1
pre = ''
epi = ''
for i in range(Q):
	q = list(map(str, input().split()))
	if q[0] == "1":
		cond *= -1
	else:
		if q[1] == "1":
			if cond == 1:
				pre += q[2]
			else:
				epi += q[2]
		else:
			if cond == 1:
				epi += q[2]
			else:
				pre += q[2]

if cond == 1:
	print(pre[::-1]+S+epi)
else:
	print(epi[::-1]+S[::-1]+pre)
from collections import deque

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

reverse_flag = 1

for _ in range(q):
    query = input().split()
    if query[0] == "1":
        reverse_flag *= -1
    else:
        if (query[1] == "1" and reverse_flag == 1) or (query[1] == "2" and reverse_flag == -1):
            s.appendleft(query[2])
        else:
            s.append(query[2])
if reverse_flag == 1:
    print("".join(s))
else:
    s.reverse()
    print("".join(s))
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))
s=[input()]
q=int(input())
turn=True
l=""
for i in range(q):
    qq=input().split()
    if qq[0]=='1':
        turn=not turn
    else:
        if turn==bool(int(qq[1])-2):
            l+=qq[2]
        else:
            s.append(qq[2])

text=l[::-1]+"".join(s)
print(text if turn else text[::-1])
s=input()
q=int(input())
order=1
header=''
footer=''
for _ in range(q):
  tmp=input()
  if tmp=='1':
    order*=-1
  else:
    t, f, c=tmp.split()
    if (int(f)*order)%3==1:
      header=c+header
    else:
      footer=footer+c
s=header+s+footer
print(s) if order==1 else print(s[::-1])
from collections import deque

S = deque(input())
Q = int(input())
flip = False # False: left side is the head
for i in range(Q):
  Query_i = list(map(str, input().split()))
  if Query_i[0]=='1':
    flip = not flip
  elif Query_i[1] == '1' and not flip:
    S.appendleft(Query_i[2])
  elif Query_i[1] == '1' and flip:
    S.append(Query_i[2])
  elif Query_i[1] == '2' and not flip:
    S.append(Query_i[2])
  elif Query_i[1] == '2' and flip:
    S.appendleft(Query_i[2])
if flip:
  S.reverse()
print(''.join(S))
s=list(input())
top=bool()
t=[]
u=[]
n=int(input())
for i in range(n):
  q=list(map(str,input().split()))
  if q[0]=='1':
    top=not top
  else:
    if (q[1]=="1") ^ top:
      t.append(q[2])
    else:
      u.append(q[2])
s=list(reversed(t))+s+u
if top:
  print(*list(reversed(s)),sep='')
else:
  print(*s,sep='')
from collections import deque
import sys
input = sys.stdin.readline

S = input().rstrip()
Q = int(input())
FTC = [tuple(input().split()) for _ in range(Q)]

que = deque(['?'])
cnt = 0
for query in FTC:
    if query[0] == '1':
        cnt ^= 1
        continue

    sign = 0 if query[1] == '1' else 1

    if sign ^ cnt == 0:
        que.appendleft(query[2])
    else:
        que.append(query[2])

que = list(que)
ans = ''.join(que[:: (-1)**cnt])
ans = ans.replace('?', S[:: (-1)**cnt])

print(ans)
s=input();q=int(input());r,a=0,[[],[]]
for _ in range(q):
  t=input()
  if t=='1':r=-(-~r%2)
  else:a[~-r+int(t[2])].append(t[4])
print(''.join(a[~-r+1][::-1])+s[::r or 1]+''.join(a[~-r+2]))
import sys
input = sys.stdin.readline
S = input().strip()
Q = int(input())
f = 0
add = ['', '']
for q in range(Q):
    query = input().strip()
    if query == '1':
        f ^= 1 
    else:
        _, F, C = query.split(' ')
        add[int(F)-1^f] += C
print(''.join([add[f][::-1], S[::(f*-1*2)+1], add[f*-1+1]]))
s=input()
q=int(input())
q_=[input() for i in range(q)]
l=[]
r=list(s)
reverse=False
for i in q_:
  if i=="1":
    reverse=not reverse
  elif (i.split()[1]=="1" and not reverse) or (i.split()[1]=="2" and reverse):
    l.append(i.split()[2])
  else:
    r.append(i.split()[2])
#print(l)
#print(r)
if reverse:
  print(''.join(r[::-1]+l))
else:print(''.join(l[::-1]+r))
from collections import deque

S = deque(input())

Q = int(input())

c = False
for i in range(Q):
  *T, = map(str,input().split())
  if len(T) == 1:
    c = not c  
  elif (int(T[1]) == 1 and not c) or (int(T[1]) == 2 and c):
    S.appendleft(T[2])
  else:
    S.append(T[2])

print("".join(S) if not c else "".join(list(reversed(S))))
S=input()
Q=int(input())
query=[]
for _ in range(Q):
    query.append(input().split())

flag = True
t=""
u=""

for i in query:
    if i[0]=="1":
        u,t=t,u
        flag = not flag
#        S=S[::-1]
    else:
        if i[1]=="1":
            t+=i[2]
        else:
            u+=i[2]
if flag:
    print(t[::-1]+S+u)
else:
    print(t[::-1]+S[::-1]+u)
from collections import deque
s = input()
n = int(input())
d = deque(s)

now_reverse = False

for _ in range(n):
  query = input()

  if query == '1':
    now_reverse = not now_reverse
    continue

  
  _,f,c = query.split()
  if f == '1' and now_reverse:
    d.append(c)
  elif f == '1' and not now_reverse:
    d.appendleft(c)
  elif f == '2' and now_reverse:
    d.appendleft(c)
  else:
    d.append(c)
  
if now_reverse:
  d = reversed(list(d))
else:
  d =list(d)

for c in d:
  print(c,end='')
print()
def solve():
    S = input()
    q = int(input())
    flg = 0
    li = ["", ""]
    for _ in range(q):
        Q = input().split()
        if Q[0] == "1":
            flg = 1 - flg
        else:
            li[(int(Q[1]) - 1 + flg) % 2] += Q[2]
    if flg:
        S = S[::-1]
    print(li[flg][::-1] + S + li[1 - flg])
solve()
S=input()
Q=int(input())
c=0
left=""
right=""
for i in range(Q):
    T=list(map(str,input().split()))
    if len(T)==1:
        c+=1
    else:
        if (T[1]=="1" and c%2==0) or (T[1]=="2" and c%2==1):
            left+=T[2]
        else:
            right+=T[2]

if c%2==0:
    ans=left[::-1]+S+right
else:
    ans=right[::-1]+S[::-1]+left
print(ans)
import sys
range = xrange
input = raw_input

S = input()
start = []
end = []
par = 1

q = int(input())
for _ in range(q):
    quer = input()
    if quer == '1':
        par ^= 1
    else:
        _, f, c = quer.split()
        f = int(f) - 1
        if par ^ f:
            start.append(c)
        else:
            end.append(c)

T = ''.join([''.join(reversed(start)), S, ''.join(end)])

if par:
    print T
else:
    print T[::-1]
from collections import deque
S=input()
Q=int(input())
query=[list(map(str,input().split())) for _ in range(Q)]

d=deque(S)
reverse=False
for q in query:
  if len(q)==1:
    reverse=not reverse
  else:
    _,f,c=q
    if f=='1':
      d.append(c) if reverse else d.appendleft(c)
    else:
      d.appendleft(c) if reverse else d.append(c)
print(*reversed(d),sep='') if reverse else print(*d,sep='')
from collections import deque

S=deque(str((input())))
Q=int(input())
t=0
for _ in range(Q):
    T=list(map(str,input().split()))
    if T[0]=="1":
        t+=1
    else:
        if (T[1]=="1" and t%2 == 0) or (T[1]=="2" and t%2 != 0):
            S.appendleft(T[2])
        else:
            S.append(T[2])


if t%2 != 0:
    S.reverse()

print("".join(S))
s = input()
n = int(input())

t = ''
for i in range(n):
    q = input().split()
    if len(q) == 1:
        t, s = s, t
    elif len(q) == 3:
        if q[1] == '1':
            t += q[2]
        elif q[1] == '2':
            s += q[2]

t = t[::-1]
print(t + s)
from collections import *
from itertools import *
from bisect import *
from heapq import *
import copy
import math
from fractions import gcd

mod=10**9+7
#N=int(input())
S=input()
Q=int(input())
Query=[list(input().split()) for i in range(Q)]


lst=deque(S)

flag=0
for query in Query:
    if len(query)==1:
        flag+=1
        flag%=2
    else:
        _,F,C=query
        if (int(F)+flag)%2:
            lst.appendleft(C)
        else:
            lst.append(C)

if flag:
    lst.reverse()
print("".join(lst))
s  = input()
n  = int(input())

s_mae = ''
s_ato = s

for _ in range(n):
    ipt = input()

    if ipt[0] == '1':
        s_mae, s_ato = s_ato, s_mae
    else:
        _, id_, s_ = ipt.split()
        is_head = True if id_ == '1' else False
        if is_head:
            s_mae += s_
        else:
            s_ato += s_

print(s_mae[::-1] + s_ato)
S = input()
Q = int(input())
#query = [input().split() for _ in range(Q)]

rev = False
forw = ""
back = ""
for i in range(Q):
  query = input().split()
  if query[0] == "1":
    rev = not rev
  else:
    if (not rev and query[1] == "1") or (rev and query[1] == "2"):
      forw = query[2] + forw
    else:
      back += query[2]
#S = forw + S + back
if rev:
  S = back[::-1] + S[::-1] + forw[::-1]
else:
  S = forw + S + back
print(S)
from collections import deque

s = deque(list(input()))
q = int(input())
flg = 1
for i in range(q):
    array = input().split(' ')
    if array[0] == '1':
        flg *= -1
    elif array[0] == '2':
        if array[1] == '1' and flg == 1 or array[1] == '2' and flg == -1:
            s.appendleft(array[2])
        else:
            s.append(array[2])

s = list(s)
if flg == -1:
    s = s[::-1]

s = ''.join(s)
print(s)
s = input()
q = int(input())
appendix = [[], []]
reverse = False
for _ in range(q):
  query = input().split()
  if query[0] == '1':
    reverse ^= True
  else:
    f = bool(int(query[1]) - 1)
    appendix[int(f ^ reverse)].append(query[2])
if reverse:
  print(''.join(appendix[1][::-1]) + s[::-1] + ''.join(appendix[0]))
else:
  print(''.join(appendix[0][::-1]) + s + ''.join(appendix[1]))
import sys
S = sys.stdin.readline().rstrip()
L = []
R = []
rev = 0
for _ in range(int(sys.stdin.readline().rstrip())):
    A = sys.stdin.readline().rstrip().split()
    if A[0] == "1":
        rev ^= 1
    else:
        if (int(A[1]) ^ rev) & 1:
            L.append(A[2])
        else:
            R.append(A[2])

if rev:
    print("".join(R[::-1]) + S[::-1] + "".join(L))
else:
    print("".join(L[::-1]) + S + "".join(R))
S = input()

Q = int(input())

from collections import deque
ans = deque()

for s in S:
    ans.append(s)

rev = False
for _ in range(Q):
    q = input().split()
    if q[0] == "1":
        rev = not rev
    else:
        is_right = (q[1] =="2") ^ rev
        if is_right:
            for s in q[2]:
                ans.append(s)
        else:
            for s in q[2]:
                ans.appendleft(s)
if rev:
    ans.reverse()
print("".join(ans))
ans = input()
q = int(input())
r = 0
p = ""
for i in range(q):
    d = input().split()
    if len(d)==1:
        r += 1
    else:
        f = int(d[1])
        c = d[2]
        if (f+r)%2 == 1:
            p += c
        else:
            ans += c
ans = p[::-1] + ans
if r%2:
    ans = ans[::-1]
print(ans)
from collections import deque
s = deque(input())
q = int(input())

cnt = 0
for _ in range(q):
    i = input().split()
    t = int(i[0])

    if t == 1:
        cnt += 1
    else:
        f = int(i[1])
        c = i[2]
        if (cnt % 2 == 0 and f == 1) or (cnt % 2 == 1 and f == 2):
            s.appendleft(i[2])
        else:
            s.append(i[2])

if cnt % 2 == 1:
    s.reverse()
print(''.join(s))