peterverebics/CodeNet_Python_118 huggingface dataset:
s = input()
t = ''
q = int(input())
Q = [list(map(str, input().split())) for _ in range(q)]

for qi in Q:
    if int(qi[0]) == 1:
        s, t = t, s
    else:
        if int(qi[1])  == 1:
            t += qi[2]
        else:
            s += qi[2]
t[::-1]
t += s
print(t)
S = input()
Q = int(input())
reverse = False
front = ""
end = ""

for i in range(Q):
    inp = input()
    if inp[0] == "1":
        reverse = ~reverse
    else:
        _, F, C = inp.split()
        if (F == "1") ^ reverse:
            front =front+C
        else:
            end =end+C

if reverse:
    print(end[::-1]+S[::-1]+front)
else:
    print(front[::-1]+S+end)
s = input()
q = int(input())

flag = 1
for i in range(q):
  st = input().split()
  if int(st[0]) == 1:
    flag *= -1
  else:
    tmp = int(st[1])*flag
    if tmp == 1 or -2:
      s = st[2] + s
    else:
      s = s + st[2]

if flag == -1:
  s = s[::-1]
print(s)
# -*- coding: utf-8 -*-
# TLE
s = input()
q = int(input())
reverse_flag = False
prefix = ''
suffix = ''
for _ in range(q):
  query = input()
  if query == '1':
    if reverse_flag:
      reverse_flag = False
    else:
      reverse_flag = True
  else:
    if reverse_flag and query[2] == '1':
      suffix += query[4]
    elif not reverse_flag and query[2] == '2':
      suffix += query[4]
    else:
      prefix += query[4]

if reverse_flag:
  print(suffix[::-1] + s + prefix)
else:
  print(prefix[::-1] + s + suffix)
from collections import deque
S = deque([s for s in input()])
Q = int(input())
for i in range(Q):
    query = input().split()
    t = int(query[0])
    if t == 1:
        S[0], S[-1] = S[-1], S[0]
    else:
        f = int(query[1])
        c = query[2]
        if f == 1:
            S.appendleft(c)
        else:
            S.append(c)
print(''.join(S))
list=[]
list.append(input())
q=int(input())
for i in range(q):
	a=input()
	if a=="1":
		list.reverse()
	else:
		a,f,c = a.split()
		if f=="1":
			list.insert(0,c)
		else:
			list.append(c)
c="".join(list)
print(c)
s=input()
a=[]
b=[]
d=0
for i in range(int(input())):
  q=input()
  if q=="1":d^=1
  else:
    _,f,c=q.split()
    f=int(f)-1
    if d^f==0:a.append(c)
    else:b.append(c)
if d==0:print("".join(a)[::-1]+s+"".join(b))
else:print("".join(b)[::-1]+s+"".join(a))
s = list(input())
n=int(input())
l = [input().split(" ") for _ in range(n)]

for i in range(n):
    if l[i][0]=='1':
        s=s[::-1]
    elif l[i][0]=='2':
        if l[i][1]==1:
            s = s+[l[i][2]]
        else:
            s.append(l[i][2])
print(''.join(s))
s = input()
qn = int(input())
right = []
left = [s]
for i in range(qn):
    q = input()
    if q[0] == '1':
        right, left = left, right
    else:
        _, f, c = q.split()
        if f == '1':
            left.append(c)
        else:
            right.append(c)
print(''.join(left[::-1]) + ''.join(right))
s=list(input())
n=int(input())
count=0
for i in range(n):
  a=list(input().split())
  if len(a)==1:
    count+=1
  else:
    if count%2==0:
      if a[1]=='1':
        s.insert(0,a[2])
      else:
        s.insert(-1,a[2])
    else:
      if a[1]=='1':
        s.insert(-1,a[2])
      else:
        s.insert(0,a[2])
if count%2==0:
  print(''.join(s))
else:
  print(''.join(reversed(s)))
s=input()
n=int(input())
r=-1
for i in range(n):
    q=input().split()
    if q[0]=='1':
        r*=-1
    else:
        t=int(q[1])
        if t*r==(-1 or 2):
            s=q[2]+s
        else:
            s=s+q[2]
if r==1:
    s=s[::-1]
print(s)
S = input()
char_S = list(S)
Q = int(input())
for i in range(Q):
    query = input().split()
    if query[0] == "1":
        char_S.reverse()
    else:
        if query[1] == "1":
            char_S.insert(0,query[2])
        else:
            char_S.insert(len(char_S),query[2])
print(char_S)
import sys
s = sys.stdin.readlines()
S = s[0]
Q = s[1]
Qs = s[2:]
 
for q in Qs:
  if len(q.strip()) == 1:
    S = S[::-1]
  else:
    F, C = q.strip().split()[1:3]
    if F == "1":
      S = C + S
    else:
      S += C
print(S)
import sys
s = str(sys.stdin.readline())
Q = int(sys.stdin.readline())

for _ in range(Q):
	qu = input()
	if qu == '1':
		s = s[::-1]
	else:
		_, fi, ci = qu.split()
		if fi == '1':
			s = ci + s
		if fi == '2':
			s += ci
print(s)
s = input()
n = int(input())

for _ in range(n):
  q = input().split()
  if q[0] == "1":
    s = s[-1] + s[1:-1] + s[0]
  else:
    if q[1] == "1":
      s = q[2] + s
    else:
      s += q[2]
print(s)
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:
            S.append(c)

if flag == -1:
    S = S[::-1]

print(''.join(S))
s = input()
q = int(input())
rev = 1
for i in range(q):
  t = input()
  
  if t == '1':
    rev *= -1
  else:
    x,y,a = t.split()
    if rev == 1 and y == '2':
      s = s + a
    else:
      s = a + s
if rev == -1:
  s = s[::-1]
  
print(s)
s = input()
q = int(input())

rev = 0
onenum = 0
for i in range(q):
	g = input()
	if len(g) == 1:
		t = int(g)
	else:
		t = int(g[0])
		f = int(g[2])
		c = g[4]
	if t == 1:
		onenum += 1
		rev += 1
	else:
		if f == 2:
			if rev%2 == 1:
				s = c + s
			else:
				s = s + c
		else:
			if rev%2 == 0:
				s = c + s
			else:
				s = s + c
		rev = 0
if onenum%2 == 1:
	s = s[::-1]
print(s)
S = input()
Q = int(input())
queries = list(list(input().split()) for _ in range(Q))

for query in queries:
    if len(query) == 1:
        S_temp = S[len(S) - 1]
        S_temp += S[1:len(S) - 1] + S[0]
        S = S_temp
    else:
        if int(query[1]) == 1:
            S = query[2] + S
        else:
            S += query[2]

print(S)
from collections import deque

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

DQ = deque()
DQ.appendleft(S)


for i in range(Q):
    # q = list(input().split())
    q = deque(input().split())
    

    if (len(q) == 1):
        # S = S[::-1]
        DQ.reverse()
    else:
        cha = q.pop()
        judge = q.pop()
        
        if (int(judge) == 1):
            DQ.appendleft(cha)
        else:
            DQ.append(cha)


ans = list(DQ)
print(''.join(ans))
from collections import deque
d = deque(input())
flag = 0
for T_F_C in (input().split() for _ in range(int(input()))):
    if T_F_C[0] == '1':
        flag ^= 1
        continue
    _, f, c = T_F_C
    if (flag+int(f)) % 2:
        d.append(c)
    else:
        d.appendleft(c)
if flag:
    print("".join(d))
else:
    print("".join(reversed(d)))
from collections import deque
S = deque(input().split())
Q = int(input())
f = 0
for _ in range(Q):
  a, *b = input().split()
  f += int(a)
  if a == "2":
    if int(b[0]) + f % 2 ==0 :
      S.leftappend(b[1])
    else:
      S.append(b[1])
if f % 2 == 0:
  for i in range(len(S)):
    print (S[-i-1], end="")
else:
  print (*S, sep="")
words=[]
words.append(input())
order_amount=int(input())
for i in range(order_amount):
    order=input().split()
    if order[0]=='2':
        if order[1]=='1':
            words.insert(0,order[2])
        elif order[1]=='2':
            words.append(order[2])
    else:
        a=words[0]
        words[0]=words[len(words)-1]
        words[len(words)-1]=a
word=''
for i in words:
    word+=i
print(word)
from collections import deque
s=deque([input()])
q=int(input())

for _ in range(q):
	t=input()
	if t[0]=="1":
		s.reverse()
	else:
		t,f,c=t.split()
		if f=="1":
			s.appendleft(c)
		else:
			s.append(c)
print(*s,sep="")
S = input()
Q = int(input())
flag = False
for i in range(Q):
    T = input()
    if len(T)==1:
        flag = not flag
        continue
    if flag:
        S = S[::-1]

    if T[2]=="1":
        S = T[4]+S
    else:
        S = S+T[4]
    
print(S)
#import time
S = input()
Q = int(input())
f = True
head = []
tail = []
for i in range(Q):
    query = input().split(' ')
    if len(query) == 1:
        f = not f
    else:
        if (query[1] == '1' and f) or (query[1] == '0' and not f):
            head.append(query[2])
        else:
            tail.append(query[2])

S = ''.join(head)+S+''.join(tail)
if not f:
    S = S[::-1]
print(S)

# start = time.time()
# elapsed_time = time.time() - start
# print("elapsed_time:{0}".format(elapsed_time) + "[sec]")
S = input().strip()
Q = int(input())
is_reverse = False
A = ""
B = ""
for i in range(Q):
  query = [j for j in input().strip().split()]
  T = int(query[0])
  if T == 1:
    is_reverse = not is_reverse
  else:
    F, C = int(query[1]), query[2]
    if F == 1:
      if not is_reverse:
        A = A + C
      else:
        B = B + C
    else:
      if not is_reverse:
        B = C + B
      else:
        A = C + A

if not is_reverse:
  print(A + S + B)
else:
  print(B + S + A)
s,_,*q=open(0)
a=['',s[:-1]]
p=1
for q in q:
  if q>'1':
    f=1if q<'2 2'else 0
    a[f^p]+=q[-2]
  else:p^=1
print(a[p^1][::-1]+a[p])
def main():
  s = list( input() )
  q = int(input())
  r = [list(input().split()) for _ in range(q)]
  p = False
  for x in r:
    if len(x)==1:
      p = True
      continue
    if (p == False and x[1]=='1') or (p == True and x[1]=='2'):
      s = [x[2]] + s
    else:
      s = s + [x[2]]
    
  print("".join(s) if p==False else "".join(s[::-1]))
  
if __name__=='__main__':
  main()
S = input().split()
Q = int(input())
switch = 1
for i in range(Q):
    a =input().split()
    if int(a[0])==1:
        switch *=-1
    else:
        if (int(a[1])==1 and switch ==1) or (int(a[1])==2 and switch ==-1):
            S.insert(0,a[2])
        else:
            S.append(a[2])
if switch ==1:
    S = ''.join(S)
    print(S)
else:
    S.reverse()
    S = ''.join(S)
    print(S)
def main():
  s,q,*r = [list(k.split()) for k in open(0)]
  s=s[0]
  p = False
  for x in r:
    if len(x)==1:
      p = True
      continue
    if (p == False and x[1]=='1') or (p == True and x[1]=='2'):
      s = x[2] + s
    else:
      s = s + x[2]
    
  print("".join(s) if p==False else "".join(s)[::-1])
  
if __name__=='__main__':
  main()
from collections import deque

S  = deque()
S.append(input().strip())

N = int(input().strip())

for _ in range(N):
    ope = input().strip()
    if ope[0] == "1":
        S.reverse()
        continue
    i, s, c = ope.split(" ")
    if s == "1":
        S.appendleft(c)
    else:
        S.append(c)

print(''.join(list(S)))
array = []
array.append(input())

Q = int(input())

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

    if query[0]=="1":
        array.reverse()
    else:
        if query[1]=="1":
            array.insert(0, query[2])
        else:
            array.append(query[2])

ans = "".join(array)
print(ans)
s = input()
Q = int(input())
for i in range(Q):
    q = list(map(str, input().split()))
    if q[0] == "1":
        if len(s) > 1:
            s = s[-1] + s[1:-1] + s[0]
    else:
        if q[1] == "1":
            s = q[2] + s 
        else:
            s = s + q[2]
print(s)
from collections import deque
t=list(input())
q=int(input())
query=deque([])
for i in range(q):
  s=list(map(str,input().split()))
  query.append(s)
#print(query)
while query:
  p=query.popleft()
  if len(p)==1:
    t[::-1]
    
  else:
    if p[1]==str(1):
      t.insert(0,p[2])
      
    else:
      t.append(p[2])
      
print(''.join(t))
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.insert(0,q[2])
    else:
      u.append(q[2])
s=t+s+u
if top:
  print(*list(reversed(s)),sep='')
else:
  print(s)
s = input()
n = int(input())
tmp  =['','']
c = 1
for i in range(n):
    t = list(map(str,input().split()))
    if t[0] == '1':
        c+=1
    else:
        C = (c%2==0)
        
        if t[1] == '1':
            C+=1
            C%=2
            tmp[C] = tmp[C] +t[2]
        else:
            tmp[C] = tmp[C] + t[2]
    # print(tmp)
print(tmp[0][::-1]+s+tmp[1])
S = input()
Q = int(input())
before_chr = []
after_chr = []
reverse_status = 1
for i in range(Q):
    query = list(input().split())
    if query[0] == "1":
        reverse_status *=  -1
    elif reverse_status * (1.5-int(query[1])) == 0.5:
        before_chr += query[2]
    else:
        after_chr += query[2]
if reverse_status == 1:
    print("".join(before_chr[::-1])+S+"".join(after_chr))
else:
    print("".join(after_chr[::-1])+S+"".join(before_chr))
s = input() # start string
str_list = [s]
q = input() # count
q = int(q)
for i in range(q):
  qq = input().split()
  if qq[0] == "1":
    str_list.reverse()
  else:
    if qq[1] == "1":
      str_list = qq[2:] + str_list
    else:
      str_list.append(qq[2])
print("".join(str_list))
s = input()
q = int(input())
ht = 1
count = 0
for i in range(2, q):
    txt3 = input().split()
    if txt3[0] == '1':
        ht = 3 - ht
        count += 1
    else:
        if int(txt3[1]) == ht:
            s = txt3[2] + s
        else:
            s = s + txt3[2]
if count % 2 == 1:
    s = s[::-1]
print(s)
from collections import deque

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

for _ in range(q):
    query = input().split()
    if query[0] == '1':
        S[0], S[-1] = S[-1], S[0]
    else:
        if query[1] == '1':
            S.appendleft(query[2])
        else:
            S.append(query[2])
print(*S, sep='')
S = input()

Q = int(input())

A = [S]

for i in range(Q):
    s = input().split()
    if len(s) == 1:
        A = A[::-1]
    else:
        if s[1] == '1':
            A.insert(0,s[2])
        else:
            A.append(s[2])

print('%s' % ''.join(A))
def main():
  S = input()
  Q = int(input())
  import sys
  que = [list(sys.stdin.readline().split()) for _ in range(Q)]
  judge = False
  for l in que:
    if int(l[0])==1:
      if judge:
        judge = False
      else:
        judge = True

    elif int(l[0])==2:
      f,c = int(l[1]),l[2]
  if judge:
    S=S[::-1]
  print(S)
  
main()
s = str(input())
q = int(input())
query = [[str(i) for i in input().split()] for i in range(q)]
direction = 0
for i in range(q):
    if query[i] == ['1']:
        direction += 1
    else:
        if (int(query[i][1]) + direction) % 2 == 0:
            s += query[i][2]
        else:
            s = query[i][2] + s
if direction % 2 == 1:
    s1 = ''
    for i in range(len(s)):
        s1 += s[-i-1]
else:
    print(s)
s = input()
q = int(input())
s1 = ""
s2 = ""
cnt = 1
for i in range(q):
  l1 = list(input().split())
  if len(l1) == 1:
    cnt *= -1
  else:
    if (cnt == 1 and l1[1] == "1") or (cnt == -1 and l1[1] == "2"):
      s1 = l1[2] + s1
    else:
      s2 = s2 + l1[2]
if cnt == 1:
  print(s1+s+s2)
else:
  print(s2[::-1]+s+s1[::-1])
from collections import deque

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

for _ in range(q):
    x = input().split()
    
    if(x[0]=='1'):
        reflag = not reflag
        #s.reverse()
    else:
        if(reflag):
            s.reverse()
            reflag = False
        
        s.appendleft(x[2]) if(x[1]=='1') else s.append(x[2])

if(reflag):
    s.reverse()

print(''.join(s))
from collections import deque
S=input()
Q=int(input())
query=[list(map(str,input().split())) for _ in range(Q)]

ans=deque([S])
for q in query:
  if len(q)==1:
    ans.reverse()
  else:
    _,f,c=q
    if f=='1':
      ans.appendleft(c)
    else:
      ans.append(c)
print(*ans,sep='')
s = list(input())
q = int(input())
judge = 1
front = []
back = []
for i in range(q):
  d = input()
  if d == '1':
    judge *= -1
  else:
    a, b, c = list(d.split())
    b = int(b)
    if (b == 1 and judge ==1) or (b == 2 and judge == -1):
      front.append(c)
    else:
      back.append(c)
ans = front + s + back
if judge == 1:
  print(*ans,sep='')
else:
  print(*ans[::-1], sep='')
S = input()
Q = int(input())
reverse = False
front = ""
end = ""
 
for i in range(Q):
    inp = input().split()
    if inp[0] == "1":
        reverse = ~reverse
    else:
        if (inp[1] == "1") ^ reverse:
            front = front+inp[2]
        else:
            end = end+inp[2]
 
if reverse:
    print(end[::-1]+S[::-1]+front)
else:
    print(front[::-1]+S+end)
from collections import deque
s=input()
q=int(input())

flug=0
ans=deque(list(s))

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

ans="".join(map(str,ans))

if flug%2==1:
    ans=ans[::-1]
print(ans)
from collections import deque
s = input()
q = int(input())
Q = [list(input().split()) for _ in range(q)]

ans = deque([s])
ok = True
for v in Q:
    if v[0] == "1":
        if ok: ok = False
        else: ok = True
    else:
        if v[1] == "1":
            if ok: ans.appendleft(v[2])
            else: ans.append(v[2])
        else:
            if ok: ans.append(v[2])
            else: ans.appendleft(v[2])
if not ok:
    ans = list(ans)[::-1]
print("".join(ans))
a = str(input())
itr_num = int(input())
test =[]
status =1 
R=""
L=""

##type of valiable##
for i in range(itr_num):
  in_val =input().split()  
  test.append(in_val)

for i in test:
  if len(i)==1:
    status = status *(-1)
  else:
    if i[1] =="1":
      if status ==1:
        L += str(i[2])
      else:
        R += str(i[2])
    if i[1] =="2":
      if status ==1:
        R += str(i[2])
      else:
        L += str(i[2])

out = R[::-1]+a+L
print(out)
s = input()
q = int(input())
l = ''
r = ''
for operation in range(q):
    t = input().split()
    if int(t[0]) == 1:
        l = l[::-1]
        r = r[::-1]
        l, r = r, l
    elif int(t[1]) == 1:
        l += t[2]
    else:
        r += t[2]
s = l + s + r
print(s)
s = list(input())
q = int(input())
ans = ""

for i in range(q):
    l = list(map(str,input().split()))
    print(l)
    t = int(l[0])
    if t == 1:
        s.reverse()
    elif t == 2:
        f = int(l[1])
        c = str(l[2])
        if f == 1:
            s.insert(0,c)
        else:
            s.append(c)

print(''.join(s))
S = list(input())
print(S)
Q = int(input())

for _ in range(Q):
  q = list(input())
  if len(q) == 1:
    r = S[0]
    l = S[-1]
    S[0] = l
    S[-1] = r
  else:
    if q[2] == "1": S.insert(0, q[4])
    else: S.append(q[4])

print("".join(S))
import sys

readline = sys.stdin.readline
s = readline().strip()
n = int(readline())
q = sys.stdin.readlines()
for qi in q:
    if qi[0] == '1':
        l = len(s)
        if l >= 3:
            s = s[-1] + s[1:-1] + s[0]
        elif l == 2:
            s = s[-1] + s[0]
    else:
        c = qi[4]
        if qi[2] == '2':
            s = s + c
        else:
            s = c + s
print(s)
s=[input()]
q=int(input())
turn=True
for i in range(q):
    qq=input().split()
    if qq[0]=='1':
        turn=not turn
    else:
        if turn and int(qq[1])-2:
            s.insert(0,qq[2])
        else:
            s.append(qq[2])
text="".join(s)
print(text if turn else text[::-1])
s = input()
q = int(input())
queries = [list(input().split()) for _ in range(q)]

for query in queries:
    if query[0] == "1":
        s = s[-1] + s[1:-1] + s[0]
    else:
        if query[1] == "1":
            s = query[2] + s
        else:
            s = s + query[2]

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

rev = False
head = []
tail = []

for i in range(q):
    query = input()
    if query == "1":
        rev = not rev
    else:
        _, f, c = query.split()
        if (not rev and f == "1") or (rev and f == "2"):
            head.append(c)
        else:
            tail.append(c)

if not rev:
    res = head[::-1] + s + tail
else:
    res = tail[::-1] + s + head

print(*res, sep="")
from collections import deque


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

for _ in range(Q):
    Query = input().split()
    if Query[0] == "1":
        if len(S) > 1:
            S[0], S[-1] = S[-1], S[0]
    else:
        F, C = Query[1], Query[2]
        if F == "1":
            S.appendleft(C)
        else:
            S.append(C)

print("".join(S))
def main():
  s=input()
  q=int(input())
  turn = 1

  for _ in [0]*q:
      query = input()
      try:
          t = int(query)
          turn = 3 - turn
      except ValueError:
          t,f,c = map(str,query.split())
          if (int(f) + turn)% 2  == 0:
              s = s + c
          else:
              s = c + s
  if turn == 2:
      print(s)
  else:
      print(s[::-1])
main()
from collections import deque
S = deque([input()])
Q = int(input())

for _ in range(Q):
  inp = input().split()
  if len(inp) == 1:
    S.reverse()
  else:
    _, f, c = inp
    if f == '1':
      S.appendleft(c)
    else:
      S.append(c)

print("".join(S))
from collections import deque
S = deque(input().split())
Q = int(input())
f = 0
for _ in range(Q):
  a, *b = input().split()
  f += int(a)
  if a == "2":
    if (int(b[0]) + f) % 2 == 0 :
      S.appendleft(b[1])
    else:
      S.append(b[1])
for i in range(len(S)):
  if f % 2 == 0:
    print (S[-i-1], end="")
  else:
    print (S[i], end="")
s = input()
n = int(input())

ct = 0

for i in range(n):
    p = input().split()

    if p[0] == '1':
        ct += 1
    else:
        if p[1] == "2":
            ct += 1
        
        if ct % 2 :
            s = s + p[2]
        else:
            s = p[2] + s
        ct = 0

print(s)
import sys
S = sys.stdin.readline()
Q = int(sys.stdin.readline())

mode = True

for _ in range(Q):
  TFC = list(map(str, sys.stdin.readline().split()))
  if "1" in TFC[0]:
    mode = not mode
    continue
  if mode:
    if "1" in TFC[1]:
      S = ''.join([TFC[2],S])
    else:
      S = ''.join([S,TFC[2]])
  else:
    if "1" in TFC[1]:
      S = ''.join([S,TFC[2]])
    else:
      S = ''.join([TFC[2],S])
        
if mode:
  print(S)
  exit()
print(S[::-1])
s = input()
q = int(input())
que = [list(input().split()) for _ in range(q)]
counter = 0
for i in que:
    if i[0] == "1":
        counter += 1
    else:
        if (counter+int(i[1]))%2==1:
            s = s+i[2]
        else:
            s = i[2]+s
if counter%2==1:
    print(s)
else:
    print(s[::-1])
s = input()
qn = int(input())
right = [s]
left = []
for i in range(qn):
    q = input()
    if q[0] == '1':
        right, left = left, right
    else:
        _, f, c = q.split()
        if f == '1':
            left.append(c)
        else:
            right.append(c)
print(''.join(left[::-1]) + ''.join(right))
S = input()
N = int(input())
Q = []
for _ in range(N):
  Q.append([i for i in input().split()])

from collections import deque
ans = deque([S])

asc = True
for q in Q:
  if q[0] == "2" and ((q[1] == "1" and asc) or (q[1] == "2" and not asc)):
    ans.appendleft(q[2])
  elif q[0] == "2" and ((q[1] == "2" and asc) or (q[1] == "1" and not asc)):
    ans.append(q[2])
  else:
    asc = not asc
if not asc:
  ans.reverse()
print(*ans,sep="")
S = input()
Q = int(input())

prefix = ''
suffix = ''
mode = 0
for _ in range(Q) :
  Query = list(input().split())
  if Query[0] == '1' :
    mode ^= 1
  else :
    _, F, C = Query
    F = int(F) - 1
    
    if mode != F :
      suffix += C
    else :
      prefix += C
      

if mode == 0 :
  print(prefix[::-1] + S + suffix)
else :
  print(suffix[::-1] + S + prefix)
from collections import deque

def main():
    S = deque(input().split())
    Q = int(input())

    for i in range(Q):
        QList = deque(input().split())
    
        if QList[0] == '1':
            continue
            #S.reverse()
        else:
            if QList[1] == '1':
                S.appendleft(QList[2])
            else:
                S.append(QList[2])
    print("".join(list(S)))


if __name__ == '__main__':
    main()
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.append(c)
        else:
            right.append(c)
print(''.join(left[::-1]) + ''.join(right))
s=list(input())
q=int(input())
for i in range(q):
    query=input().split()
    print(query)
    if query==['1']:
        s.reverse()
    else:
        if query[1]=='1':
            s.insert(0,query[2])
        else:
            s.append(query[2])
print(''.join(s))
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(reversed(a[~-r+1]))+s+''.join(a[~-r+2]))