peterverebics/CodeNet_Python_118 huggingface dataset:
S = input()
Q = int(input())
b = "" #reversed
a = ""
f = 0
for i in range(Q):
  q = input()
  if q == "1":
    f+=1
  else:
    _,F,C = q.split()
    if (f+int(F))%2==1:
      b+=C
    else:
      a+=C
if f%2==0:
  print(b[::-1]+S+a)
else:
  print(a[::-1]+S[::-1]+b)
import sys
a = sys.stdin.read().split('\n')[:-1]
q = int(a[1])
f = 1
l = [[], []]
for i in range(2, q + 2):
    if a[i][0] == '1':
        f = 1 - f
    else:
        l[0 if a[i][2] == '21'[f] else 1].append(a[i][4])
if f != 1:
    l[1].reverse()
    print(''.join(l[1]) + ''.join(reversed(list(a[0]))) + ''.join(l[0]))
else:
    l[0].reverse()
    print(''.join(l[0]) + a[0] + ''.join(l[1]))
from collections import deque

S=input()
Q=int(input())
r_flag=False
ans=deque((S))
for i in range(Q):
  Qu=input().split()
  if Qu[0]=="1":
    r_flag=not r_flag
  else:
    if Qu[1]=="1":
      if r_flag==True:
        ans.append(Qu[2])
      else:
        ans.appendleft(Qu[2])
    else:
      if r_flag==True:
        ans.appendleft(Qu[2])
      else:
        ans.append(Qu[2])
ans=list(ans)
print("".join(ans) if r_flag==False else "".join(ans[::-1]))
from collections import deque

S = deque(map(str, input().split()))
Q = int(input())
Left = False

for i in range(Q):
    TFC = input()

    if TFC[0] == "1":
        Left = not Left

    else:
        _, f, c = TFC[0], TFC[2], TFC[4]
        f = int(f)
        if Left ^ (f == 1):
            S.appendleft(c)

        else:
            S.append(c)

T = "".join(S)
if Left:
    T = T[::-1]

print(T)
from collections import deque

S = input()
q = int(input())
res = deque([S])
reverseFlag = False
for i in range(q):
  tmp = input()
  t = tmp[0]
  if t == "1":
    reverseFlag ^= True # ひっくり返す
  else:
    f,c = tmp[2:].split()
    if f == "1" and reverseFlag == False or f == "2" and reverseFlag == True:
      res.appendleft(c)
    else:
      res.append(c)
if reverseFlag:
  ans = "".join(res)[::-1]
else:
  ans = "".join(res)

print(ans)
from collections import deque
que=deque(input())
q=int(input())
rev=False
for i in range(q):
    s=input()
    if s=="1":
        rev= not rev
    else:
        t,f,c=s.split()
        if (f=='1' and rev==False) or (f=='2' and rev):
            que.appendleft(c)
        else:
            que.append(c)
A=que
if rev:
    A.reverse()
print("".join(A))
from collections import deque
s = input()
s = deque(s)
n = int(input())
cnt = False
for i in range(n):
  L = input()
  if len(L) == 1:
    cnt  ^=1 
  else:
    a,b,c = L.split()
    if cnt:
      if b == "1":
        s.append(c)
      else:
        s.appendleft(c)
    else:
      if b == "1":
        s.appendleft(c)
      else:
        s.append(c)
if cnt:
    s = reversed(s)
    print("".join(s))
else:
    print("".join(s))
from collections import deque
S = input()
Q = int(input())
R = 1
Top = deque()
Top.append(S)
for _ in range(Q):
  inp = input()
  if inp[0]=='1':
    R *= -1
  else:
    o, f, c = inp.split()
    if f=='1' and R==-1 or f=='2' and R==1:
      Top.append(c)
    else:
      Top.appendleft(c)
S = ''.join(Top)
S = S if R==1 else S[::-1]
print(S)
from collections import deque
S = deque(input())
n = int(input())
Q = [list(map(str, input().split())) for _ in range(n)]

flg = True
for q in Q:
    if q[0] == "1":
        flg = not flg
    else:
        F = int(q[1])
        if not flg:
            F = 3 - F
        if F == 1:
            S.appendleft(q[2])
        if F == 2:
            S.append(q[2])

if not flg:
    S.reverse()
print("".join(S))
s=input()
q=int(input())
from collections import deque
qq=deque(s)
cnt=0
for i in range(q):
    j=input()
    if j=="1":
        cnt+=1
        continue
    _,k,l=j.split()
    if (k=="2")^cnt%2:
        qq.append(l)
    else:
        qq.appendleft(l)

if cnt%2==1:
    print("".join(reversed(list(qq))))
else:
    print("".join(qq))
s = input()
q = int(input())
query = [list(input().split()) for _ in range(q)]
right = []
left = []
rev_count = 0
for i in range(q):
  if query[i][0] == "1":
    rev_count += 1
    right,left = left,right
  else:
    if query[i][1] == "1":
      left.append(query[i][2])
    else:
      right.append(query[i][2])
if rev_count % 2 == 1:
  s = s[::-1]
print("".join(reversed(left))+s+"".join(right))
S = input()
Q = int(input())

d = 0
X = [[], []]
for i in range(Q):
    q = input()
    if q[0] == "1":
        d = (d + 1) % 2
    else:
        a, f, c = q.split()
        x = (int(f) + d) % 2
        X[x].append(c)

if d == 0:
    v = "".join(X[1])[::-1] + S + "".join(X[0])
else:
    v = "".join(X[0])[::-1] + S[::-1] + "".join(X[1])
print(v)
from collections import deque
S = list(input())
q = int(input())
Q = [list(input().split()) for _ in range(q)]

ds = deque(S)

flg = True
for i in Q:
  if i[0] == "1":
    flg = not flg
  else:
    if flg == True:
      if i[1] == "1":
        ds.appendleft(i[2])
      else:
        ds.append(i[2])
    else:
      if i[1] == "2":
        ds.appendleft(i[2])
      else:
        ds.append(i[2])
if flg:
  print("".join(ds))
else:
  print("".join(ds)[::-1])
S = input()
Q = int(input())
pre, suf = '', ''
rev = False
for _ in range(Q):
    query = input().split()
    if query[0] == '1':
        rev ^= 1
    else:
        f, c = query[1:]
        if (f == '1' and not rev) or (f == '2' and rev): pre = c + pre
        else: suf = suf + c
print(pre + S + suf if not rev else (pre + S + suf)[::-1])
S = input().strip()
Q = int(input())
queries = []
for _ in range(Q):
  q = input().split()
  queries.append(q)

flip = False
s_0 = ''
s_n = ''
for q in queries:
  if q[0] == '1':
    flip = not flip
  else:
    if (q[1] == '1' and not flip) or (q[1] == '2' and flip):
      s_0 += q[2]
    else:
      s_n += q[2]
if flip:
  print(s_n[::-1] + S[::-1] + s_0)
else:
  print(s_0[::-1] + S + s_n)
import collections

x = collections.deque(input())
n = int(input())
rev = False

for i in range(n):
    y = input().split()
    if len(y) == 1:
      rev = not rev
    elif rev:
      if int(y[1]) == 2:
          x.appendleft(y[2])
      else:
          x.append(y[2])
    else:
      if int(y[1]) == 2:
          x.append(y[2])
      else:
          x.appendleft(y[2])

if rev:
  x.reverse()

print("".join(x))
s=input()
a=[]
b=[]
now=True
q=int(input())
for i in range(q):
    x=list(input().split())
    if x[0]=="1":
        now=not now
    else:
        if now and x[1]=="2" or not now and x[1]=="1":
            b.append(x[2])
        else:
            a.append(x[2])
if now:
    ans=a[::-1]+list(s)+b
else:
    ans=b[::-1]+list(s[::-1])+a
print(*ans,sep="")
s = input()
u, v = [], []
q = int(input())
cur = 0
for _ in range(q):
    t, *c = input().split()
    t = int(t)
    if t == 1:
        cur ^= 1
    else:
        f = (int(c[0]) - 1) ^ cur
        if f:
            v.append(c[1])
        else:
            u.append(c[1])
if cur:
    print(''.join(reversed(v)) + ''.join(reversed(s)) + ''.join(u))
else:
    print(''.join(reversed(u)) + s + ''.join(v))
from collections import deque
S = str(input())
Q = int(input())
Qy = list([input().split() for _ in range(Q)])
rev = False #反転チェック
ans = deque([S])
for t in Qy:
    if t[0] == '1':
        rev = not rev
    else:
        if (t[1]=='1' and rev == False) or (t[1]=='2' and rev == True):
            ans.appendleft(t[2])
        else:
            ans.append(t[2])
if rev == False:
    print(''.join(ans))
else:
    print(''.join(ans)[::-1])
from collections import deque
s = deque(list(input()))
n = int(input())
normal = 0
for i in range(n):
  li = input().split()
  if int(li[0]) == 1:
    normal ^= 1
  else:
    k = int(li[1]) - 1
    k ^= normal
    if k == 1:
      s.append(li[2])
    else:
      s.appendleft(li[2])
print(''.join(s) if normal == 0 else ''.join(list(s)[::-1]))
S = str(input())
Q = int(input())

hanten = 0
front = ""
end = ""

for i in range(Q):
  query = list(map(str, input().split()))
  if query[0] == "1":
    hanten += 1
    hanten = hanten % 2
  else:
    if query[1] == "1":
      if hanten == 0:
        front = query[2] + front
      else:
        end = end + query[2]
    else:
      if hanten == 0:
        end = end + query[2]
      else:
        front = query[2] + front

ans = front + S + end
if hanten == 1:
  ans = ans[::-1]
print(ans)
S = input()
Q = int(input())
now = 1
head = ''
last = ''
for _ in range(Q):
    query = input().split()
    if query[0] == '1':
        now = now * -1
    if query[0] == '2':
        if now * (int(query[1])-1.5) == 0.5:
            last += query[2]
        else:
            head += query[2]

ans = head[::-1] + S + last
if now == 1:
    print(ans)
else:
    print(ans[::-1])
s=input()
q=int(input())
flag=0
t=['' for _ in range(2)]
cnt=0
for _ in range(q):
  a=list(input().split())
  if a[0]=='1':
    flag=1-flag
    cnt+=1
  else:
    if a[1]=='1':t[flag]+=a[2]
    else:t[1-flag]+=a[2]
ans=t[0][::-1]+s+t[1]
if cnt%2:ans=ans[::-1]
print(ans)
S = str(input())
Q = int(input())

qs = [list(map(str, input().split())) for _ in range(Q)]

x, y = S, ""
for ls in qs:
    if ls[0] == "1":
        x, y = y, x
    else:
        if ls[1] == "1":
            y += ls[2]
        else:
            x += ls[2]
            
y = ''.join(list(reversed(y)))
print(y+x)
from collections import deque

S, inv = deque(input()), False
Q = int(input())
for _ in range(Q):
    q = input().split()
    if len(q) == 1:
        inv = not inv
        continue
    F, C = q[1], q[2]
    if (F == '1') ^ inv:
        S.appendleft(C)
    else:
        S.append(C)
print(''.join(S)[::-1] if inv else ''.join(S))
S = input()
Q = int(input())
head, tail = "", ""
reverse = False
for _ in range(Q):
    cmd = input().split()
    if cmd[0] == "1":
        reverse = not reverse
    else:
        top = (cmd[1] == "1") ^ reverse
        if top:
            head += cmd[2]
        else:
            tail += cmd[2]
print(tail[::-1] + S[::-1] + head) if reverse else print(head[::-1] + S + tail)
from collections import deque

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

flip = False

for qi in Q:
    if int(qi[0]) == 1:
      flip = not flip
    else:
      f = int(qi[1]) 
      c = qi[2]
      if flip:
        f = 3-f    
      if f == 1:
        S.appendleft(c)
      else:
        S.append(c)

if flip:
    S.reverse()
print(''.join(S))
S = input()
Q = int(input())
T = 0
s = ""
for i in range(Q):
    q = list(input().split())
    if q[0] == "1":
        T = (T+1) % 2
    elif (T == 0 and q[1] == "1") or (T == 1 and q[1] == "2"):
        s += q[2]
    else:
        S += q[2]
if T == 0:
    S = s[::-1] + S
    print(S)
else:
    S = S[::-1] + s
    print(S)
s=input()
l=len(s)
q=int(input())
cnt=1
l=str()
r=str()

for _ in range(q):
	a=list(input().split())
	if a[0]=="1":
		cnt*=(-1)
	else:
		if int(a[1])*cnt==1 or int(a[1])*cnt==-2:
			l+=a[2]
		else:
			r+=a[2]				

s=l[::-1]+s+r
if cnt==-1:
	s=s[::-1] 	

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

import sys
input = sys.stdin.readline

qs = [list(input().split()) for _ in range(q)]

left = ''
right = ''


front = False
for i in qs:
    if(i[0] == '1'):
        front = not front
        continue

    f = int(i[1])
    c = i[2]
    if(front + f == 2):
        right = right + c
    else:
        left = left + c

if(not front):
    ans = left[::-1] + s + right
else:
    ans = right[::-1] + s[::-1] + left

print(ans)
from collections import deque
s = list(input())
d = deque(s)
q = int(input())
flag = True
for i in range(q):
  l = list(map(str,input().split()))
  if l[0] == '1':
    if flag:
      flag = False
    else:
      flag = True
  else:
    if l[1] == '1':
      if flag:
        d.appendleft(l[2])
      else:
        d.append(l[2])
    else:
      if flag:
        d.append(l[2])
      else:
        d.appendleft(l[2])
if flag:
  print(''.join(d))
else:
  d.reverse()
  print(''.join(d))
from collections import deque
s=deque(input().split())
f=False
for i in range(int(input())):
  _=input().split()
  if _[0]=='1':
    f=not f
  else:
    if f:
      [s.append(_[2]) if _[1]=='1'
       else s.appendleft(_[2])]
    else:
      [s.appendleft(_[2]) if _[1]=='1'
       else s.append(_[2])]
print("".join(s)[::-1] if f else "".join(s))
S = input()
N = int(input())
Q = [input().split() for _ in range(N)]
start = ''
end = ''

# Sの向きを定義1:前から0:後ろから
direct = 0
for i in range(N):
  if Q[i][0] =='1':
    direct^=1
  else:
    if direct == int(Q[i][1])-1:
      start += Q[i][2]
    else:
      end += Q[i][2]
      
ans = start[::-1]+S+end
print(ans) if direct == 0 else print(ans[::-1])
from collections import deque

s = deque(input())
q = int(input())
 
me = 1#1:sei  -1:gyaku
for i in range(q):
    t = input().split()
    if t[0] == '1':
        me *= -1
            
    else:# 0=2
        if t[1]=='1':#1=1
            s.appendleft(t[2]) if me == 1 else s.append(t[2])
        else:#1=2
            s.append(t[2]) if me == 1 else s.appendleft(t[2])
s= list(s) 
if me != 1:
    s = s[::-1]
print(''.join(s))
from collections import deque

S = deque(input().strip())
q = int(input())

rev = False
for _ in range(q):
    query = input().split()
    if query[0] == '1':
        rev = not rev
    else:
        if (query[1] == '1') ^ rev:
            S.appendleft(query[2])
        else:
            S.append(query[2])
S = S if not rev else reversed(S)
print(*S, sep='')
s = list(input())
q = int(input()) #int(input())
inv = 1
query = [input() for _ in range(q)]
bef = []
aft = []


for t in query:
  if t == '1':
    inv *= -1 #s = s[::-1]
  else:
    _, f, c = map(str, t.split())
    if (f =='1' and inv == 1) or (f =='2' and inv == -1):
      # s = c+s
      bef.append(c)
    else:
      # s = s+c
      aft.append(c)

bef.reverse()
bef.extend(s)
bef.extend(aft)

print(''.join(bef[::inv]))
import collections
S = collections.deque(input())
Q = int(input())
reverse = False
for i in range(Q):
  q = input()
  if q == "1":
    reverse = not reverse
  else:
    q,f,c = q.split()
    if (f == "1" and not reverse) or (f == "2" and reverse):
      S.appendleft(c)
    else:
      S.append(c)
if not reverse:
  print("".join(S))
else:
  print("".join(reversed(S)))
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])

#print(a)
#print(b)
if rev % 2 == 1:
    S = S[::-1]
ans = ''.join(a[::-1]) + S + ''.join(b)
print(ans)
import collections
s = input()
_q = int(input())
q = collections.deque(s)

rev = False
for _ in range(_q):
    *query, = map(str, input().split())

    if query[0] == "1":
        rev = not rev
        continue

    f, c = query[1], query[2]

    if rev ^ (f == "1"):
        q.appendleft(c)
    else:
        q.append(c)

if rev:
    q.reverse()

print("".join(q))
s=input()
q=int(input())

rev=1

sf=""
sb=s

for _ in range(0,q):
  n=input().split()
  if n[0]=="1":
    rev*=-1
  else:
    if (n[1]=="1" and rev==1) or (n[1]=="2" and rev==-1):
      sf+=n[2]
    else:
      sb+=n[2]

s=sf[::-1]+sb

if rev==1:
  print(s)
else:
  print(s[::-1])
s = str(input())
q = int(input())
state = True
front = ""
ending = ""
for _ in range(q):
  qi = str(input())
  if qi[0] == "2":
    fi,ci = map(str,qi[2:].split())
    if state == True:
      if fi == "1":
        front += ci
      else:
        ending += ci
    else:
      if fi == "1":
        ending += ci
      else:
        front += ci
  else:
    state = not state
if state:
  first = front[::-1]
  lastt = ending
else:
  first = ending[::-1]
  lastt = front
  s = s[::-1]
print(first+s+lastt)
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)
import sys
input = lambda: sys.stdin.readline().rstrip() 
s=input()
count=0
s1=str()
s2=str()
for _ in range(int(input())):
  l=list(map(str,input().split()))
  
  if l[0]=='1':
    count+=1
  elif count%2==0:
    if l[1]=='1':
      s1=l[2]+s1
    else:
      s2=s2+l[2]
  else:
    if l[1]=='2':
      s1=l[2]+s1
    else:
      s2=s2+l[2]
s=s1+s+s2
if count%2!=0:
  s=s[::-1]
print(s)
from collections import deque

S = input()
Q = int(input())
X = [input() for _ in range(Q)]

ans = deque(S)
r = 0  # r=0: order, r=1: reverse
for i in range(Q):
    q, *x = X[i].split()
    if q == "1":
        r = abs(r - 1)
    else:
        f, c = x
        if (r == 0 and f == "1") or (r == 1 and f == "2"):
            ans.appendleft(c)
        else:
            ans.append(c)
            
print("".join(ans) if r == 0 else "".join(ans)[::-1])
S = list(input())
Q = int(input())
left, right = [], []
flag = 0
for q in range(Q):
    query = input().split()
    if query[0] == '1':
        flag = (flag + 1) % 2
        left, right = right, left
    else:
        F, C = query[1], query[2]
        if F == '1':
            left.append(C)
        else:
            right.append(C)
if flag == 1:
    S.reverse()
left.reverse()
left = ''.join(left)
right = ''.join(right)
S = ''.join(S)
print(left + S + right)
from collections import deque

S = input()
Q = int(input())
q = deque(list(S))
rev = 0
for _ in range(Q):
    s, *x = input().split()
    if s == "1":
        rev += 1
    else:
        if ((x[0] == "1" and rev % 2 == 0)
                or (x[0] == "2" and rev % 2 == 1)):
            q.appendleft(x[1])
        else:
            q.append(x[1])

if rev % 2 == 0:
    print("".join(q))
else:
    print("".join(q)[::-1])
s = input()
q = int(input())
h,t = [],[]
p = 0

for i in range(q):
    c = input().split()
    if c[0] =='1':
        h,t = t,h
        p = 1-p
    else:
        if c[1] =='1':
            h.append(c[2])
        else:
            t.append(c[2])
if p ==1:
    s = s[::-1]
print("".join(h[::-1])+s+"".join(t))
from collections import deque
S = deque(list(input()))
Q = int(input())
re = 0
for i in range(Q):
    X = input().split()
    if X[0] == "1":
        re += 1
    else:
        if (int(X[1]) + re)%2 == 1:
            S.appendleft(X[2])
        else:
            S.append(X[2])
while S:
    if re % 2:
        print(S.pop(),end = "")
    else:
        print(S.popleft(),end = "")
print()
from collections import deque
S=input()
Q=int(input())
q=deque(list(S))
directed=True
for i in range(Q):
    command=input().split()
    if command[0]=="1":
        directed=not(directed)
    else:
        if (command[1]=="1")==directed:
            q.appendleft(command[2])
        else:
            q.append(command[2])
ans=""
while(len(q)>0):
    if directed:
        t=q.popleft()
        ans+=t
    else:
        t=q.pop()
        ans+=t
print(ans)
S=input()
Q=int(input())
reverse=False
front=""
rear=""
for _ in range(Q):
    inp=input()
    if inp[0]=="1":
        reverse= not reverse
    elif inp[0]=="2":
        if (inp[2]=="1" and not reverse) or (inp[2]=="2" and reverse):
            front=inp[4]+front
        else:
            rear=rear+inp[4]
ans=front+S+rear
if reverse:
    ans=ans[::-1]
print(ans)
from collections import deque
d=deque()
d.append(input())
q=int(input())
r=1
for i in range(q):
  q2=input().split()
  if int(q2[0])==1:
    r*=-1
  else:
    if (r==1 and int(q2[1])==1) or (r==-1 and int(q2[1])==2):
        d.appendleft(q2[2])
    else:
        d.append(q2[2])

print("".join(d) if r==1 else "".join(d)[::-1])
# D - String Formation
from collections import deque
s = deque(input())
q = int(input())
odd = 0
for i in range(q):
    t,*fc = input().split()
    if t=='1': odd ^= 1 #t1
    else: #t2
        if odd ^ (int(fc[0])-1): s.append(fc[1])
        else: s.appendleft(fc[1])
if odd: deque.reverse(s)
print(*s, sep='')
T_REVERT = "1"
T_APPEND = "2"
F_BEFORE = "1"
F_AFTER = "2"

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

before = []
after = []
is_revert = False


for i in range(Q):
  q = input().split(" ")
  if q[0] == T_REVERT:
    is_revert = not is_revert
    continue
  if (q[1] == F_BEFORE) ^ is_revert:
    before.append(q[2])
  else:
    after.append(q[2])


ans_s = ""
if not is_revert:
  print("".join(before[::-1]) + S + "".join(after))
else:
  print("".join(after[::-1]) + S[::-1] + "".join(before))
import sys
from collections import deque
def input(): return sys.stdin.readline().rstrip()
S = deque(input())
rev = False
apdlft = S.appendleft
apd = S.append

for _ in range(int(input())):
    q = input().split()
    if q[0] == '1':
        rev = not(rev)
    else:
        if (q[1]=='1' and rev is False) or (q[1]=='2' and rev is True):
            apdlft(q[2])
        else:
            apd(q[2])

if rev:
    print((''.join(S))[::-1])
else:
    print(''.join(S))
S=input()
Q=int(input())
rev = 0
front, back=[],[]
for i in range(Q):
  q = input()
  if len(q)==1:rev = 1- rev
  else:
    n,f,c=q.split()
    if f=='1' and rev==0:
      front.append(c)
    elif f=='2' and rev==1:
      front.append(c)
    elif f=='1' and rev==1:
      back.append(c)
    elif f=='2' and rev == 0:
      back.append(c)

if rev==0:
  S = ''.join(front[::-1]) + S + ''.join(back)
else:
  S = ''.join(back[::-1]) + S[::-1] + ''.join(front)
print(S)
from collections import deque
words=deque(input())

order_amount=int(input())
flg=True
for _ in range(order_amount):
    orders=input()
    if orders[0]=='1':
        flg=not flg
    else:
        _,f,c=orders.split()
        if (f=='1' and flg) or (f=='2' and not flg):
            words.appendleft(c)
        elif (f=='2' and flg) or (f=='1' and not flg):
            words.append(c)
if not flg:
    words.reverse()
word=''.join(words)
print(word)
from collections import deque
s=list(input())
d=deque(s)
q=int(input())
ct=0
for i in range(q):
  l=list(input().split())
  if l[0]=='1':
    ct+=1
  else:
    if ct%2==0:
      if l[1]=='1':
        d.appendleft(l[2])
      else:
        d.append(l[2])
    else:
      if l[1]=='2':
        d.appendleft(l[2])
      else:
        d.append(l[2])
d=list(d)
if ct%2==1:
  d.reverse()
print(''.join(d))
from collections import deque

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

for _ in range(q):
  query = input()
  if query == '1':
    reverse = 1 - reverse
  else:
    _,f,c = query.split()
    if f == '1':
      if reverse:
        s.append(c)
      else:
        s.appendleft(c)
    else:
      if reverse:
        s.appendleft(c)
      else:
        s.append(c)
        
  
if reverse:
  s.reverse()

print(''.join(s))
S=input()
Q=int(input())
mode=0
sento=''
matubi=''
for i in range(Q):
  query=list(input().split())
  if query[0]=='1':
    mode=(mode+1)%2
  if query[0]=='2':
    if query[1]=='1':
      if mode==0:
        sento=query[2]+sento
      else:
        matubi=matubi+query[2]
    else:
      if mode==0:
        matubi=matubi+query[2]
      else:
        sento=query[2]+sento
if mode==1:
  S=matubi[::-1]+S[::-1]+sento[::-1]
else:
  S=sento+S+matubi
print(S)
s,_,*q=open(0)
a=['','']
p=1
for q in q:
  p^=q<'2'
  a[(q<'2 2')^p]+=q[-2]*(q>'2')
print(a[p^1][::-1]+s[:-1][::p or-1]+a[p])
from collections import deque

S = list(input())
Q = int(input())
d = deque(S)
flag = -1
for _ in range(Q):
    q = list(input().split())
    if q[0] == '1':
        flag *= -1
    else:
        f = int(q[1])
        c = q[2]
        if flag == -1 and f == 1 or flag == 1 and f == 2:
            d.appendleft(c)
        else:
            d.append(c)
d = list(d)
if flag == 1:
    d = d[::-1]

print(''.join(d))
S = input()
Q = int(input())
Query = [input() for _ in range(Q)]
inv = False
add = ['','']
for query in Query:
    if query[0]=='1':
        inv = not inv
    else:
        add[(query[2] == '1') ^ inv] += query[-1]
ans = add[1][::-1] + S + add[0]
if inv:
    ans = ans[::-1]
print(ans)
from collections import deque
que1 = deque([])
que2 = deque([])
s = input()
q = int(input())
rev = 1
for i in range(q):
  t = input()
  
  if t == '1':
    rev *= -1
  else:
    x,y,sr = t.split()
    if (rev == 1 and y == '2') or (rev == -1 and y == '1'):
      que2.append(sr)
    else:
      que1.appendleft(sr)

s = ''.join(que1+deque([s])+que2)     
if rev == -1:
  s = s[::-1]
  
print(s)