peterverebics/CodeNet_Python_118 huggingface dataset:
S = str(input())
Q = int(input())
Qy = list([input().split() for _ in range(Q)])
ans = [S]
for i in Qy:
    if i[0] == '1':
        ans.reverse()
    elif i[0] == '2':
        if i[1] == '1':
            ans.insert(0,i[2])
        else:
            ans.append(i[2])
print(''.join(ans))
S = input().rstrip()
Q = int(input())
Que = [input().rstrip() for _ in range(Q)]

flag = 0 #0:そのまま 1:反転
for elem in Que:
  if len(elem) == 1:
    flag ^= 1
  else:
    s = int(elem[2])
    if s == 1 and flag == 0:
      S = elem[4] + S
    elif s == 2 and flag == 0:
      S += elem[4]
    elif s == 1 and flag == 1:
      S += S + elem[4]
    else:
      S = elem[4] + S

if flag == 1:
  S = S[::-1]
print(S)
s=input()
q=int(input())
q_list=[input().split() for _ in range(q)]
for i in range(q):
  temp=q_list[i]
  print(temp)
  if int(temp[0])==1:
    temps=s[::-1]
    s=temps
  elif int(temp[0])==2:
    if int(temp[1])==1:
      s=temp[2]+s
    elif int(temp[1])==2:
      s=s+temp[2]
print(s)
string = input()
kai = int(input())
data = [input().split(' ') for i in range(kai)]

for i in range(kai):
    if(data[i][0] == '1'):
        if(len(string) == 2):
            string = string[1] + string[0]
        elif(len(string) >= 3):
            string = string[-1] + string[1:-1] + string[0]
        
    else:
        if(data[i][1] == '1'):
            string = data[i][2] + string
        else:
            string = string + data[i][2]

print(string)
S = str(input())
Q = int(input())

reverseCount = 0
for count in range(Q):
    T = list(map(str,input().split()))
    if int(T[0]) == 1:
        reverseCount +=1
    else:
        if reverseCount > 0:
            if reverseCount % 1  == 0:
                reverseCount = 0
                S = S[::-1]
        if int(T[1]) == 1:
            S = T[2] + S
        else:
            S = S + T[2]
print(S)
import sys
input = sys.stdin.readline
s = input()
q = int(input())
c = []
for i in range(q):
  que = input().split()
  c_append = c.append
  c_append(que)
  
for i in c:
  if i[0] == '1':
    s = s[::-1]
  else:
    if i[1] == '1':
      s = i[2] + s
    else:
      s = s + i[2]
print(s)
s = input()
totalre = 0
gm = []
gu = []

q = int(input())
for _ in range(q):
    query = list(input().split())
    if int(query[0])==1:
        totalre += 1
    else:
        if (totalre + int(query[1]))%2 != 0:
            gm.append(query[2])
        else:
            gu.append(query[2])
if totalre%2==0:
    ans = ''.join(gm)[::-1] + s + ''.join(gu)
else:
    ans = ''.join(gu)[::-1] + s + ''.join(gm)
print(ans)
from collections import deque

S = input()
Q = int(input())
que2 = []
flag = 0
q = deque(que2)
q.append(S)

for _ in range(Q):
    que = list(map(str, input().split()))
    if que[0] == "1":
         q.reverse()
    elif que[1] == "1":
        q.appendleft(que[2])
    elif que[1] == "2":
        q.append(que[2])


ans = "".join(q)
print(ans)
s = input()
q = int(input())
a = [s]

for i in range(q):
    so = input()
    if so == '1':
        a[0], a[-1] = a[-1], a[0]
    else:
        d, f, c = so.split()
        if f == '1':
            a.insert(0, c)
        else:
            a.append(c)


print(''.join(a))
S = input()
Q = int(input())
query = []
for _ in range(Q):
    query.append(input().split(" "))

def calc(q, S):
    global hanten
    if q[0] == "1":
        hanten = not hanten
    else:
        if (q[1] == "1" and (not hanten)) or (q[1] == "2" and (hanten)):
            S = q[2] + S
        else:
            S = S + q[2]
    return S

hanten = False
for q in query:
    S = calc(q, S)
if hanten:
    print(S)
else:
    print(S)
import sys
input = sys.stdin.readline

S = input()
Q = int(input())
L = [list(map(str,input().split())) for i in range(Q)]

cnt = 0

for i in range(Q):
    if len(L[i]) == 1:
        cnt += 1
    else:
        if (int(L[i][1])+cnt)%2 == 0:
            S = S+L[i][2]
        else:
            S = L[i][2]+S

if cnt%2 == 0:
    print(S)
else:
    new_S = ''.join(list(reversed(S)))
    print(new_S)
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([S])
cnt = 0
for query in FTC:
    if query[0] == '1':
        cnt ^= 1
        continue

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

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

que = list(que)
ans = ''.join(que[:: (-1)**cnt])
print(ans)
from collections import deque

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

q = deque()
rev = 1

for s in S:
  q.append(s)

for _ in range(Q):
  TFC = input()
  if len(TFC) == 1: # T == 1
    rev ^= 1
  else:
    T, F, C = (i for i in TFC.split())  
    if F == '2' and rev:
      q.append(C)
    else:
      q.appendleft(C)
      
ans = ''.join(q)

if rev:
  print(ans[::-1])
else:
  print(ans)
from collections import deque
s=deque(input())
f=False
for _ in range(int(input())):
  t=list(input().split())
  if t[0]=='1':
    if f is False:
      f = True
  else:
    if t[1]=='1':
      if f is False:
      	s.appendleft(t[2])
      else:
        s.append(t[2])
    else:
      if f is False:
      	s.append(t[2])
      else:
        s.appendleft(t[2])
print(''.join(s))
s = input()
q_number = int(input())
 
queries = [input().split() for dummy in range(q_number)]
flag = 1
count = 0
for i in range(q_number):
  if queries[i][0] == "1": 
    flag = flag * -1
    count += 1
  else:
    if int(queries[i][1])*flag == 1 or -2:
      s = queries[i][2] + s
    else:
      s = s + queries[i][2]      

      
s = s[::-1]

print(s)
s = input()
q = int(input())
l = []
l2 = []
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"):
      l.insert(0, l1[2])
    else:
      l2.append(l1[2])
if cnt == 1:
  print("".join(l)+s+"".join(l2))
else:
  print("".join(l2[::-1])+s+"".join(l[::-1]))
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)
init = input()
num = int(input())

_strings = [''] * num
_strings[0] = init

i = 0
while i < num:
    query = input().split()
    type = int(query[0])

    if type == 1:
        _strings.reverse()
    else:
        mode = int(query[1])
        if mode == 1:
            _strings.insert(0, str(query[2]))
        else:
            _strings.append(str(query[2]))

    i += 1

print(''.join(_strings))
s = input()
q = int(input())

import sys
input = sys.stdin.readline

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

left = ''
right = ''


front = True
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)
s = input()
q = int(input())

for i in range(q):
    q_i = input()
    if len(q_i) == 1:
        s = s[-1] + s[1:len(s)-1] + s[0]
    else:
        lis = [i for i in q_i.split()]
        if lis[1] == "1":
            s = lis[2] + s
        else:
            s += lis[2]

print(s)
from collections import deque
import sys
input = sys.stdin.readline
s = str(input().rstrip())
ans = deque(list(s))

f = True
q = int(input())
for i in range(q):
    q = list(map(str, input().split()))
    if int(q[0]) == 1:
        f = not f
    else:
        f_i = int(q[1])
        if f_i ^ f:
            ans.appendleft(q[2])
        else:
            ans.appendleft(q[2])
ans = "".join(list(ans))
if not f:
  ans = ans[::-1]
print(ans)
s = input()
q_number = int(input())
 
queries = [input().split() for dummy in range(q_number)]
flag = 1
count = 0
for i in range(q_number):
  if queries[i][0] == "1": 
    flag = flag * -1
    count += 1
  else:
    if int(queries[i][1])*flag == 1 or -2:
      s = queries[i][2] + s
    else:
      s = s + queries[i][2]      

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

print(s)
import sys
input = sys.stdin.readline

S = input()
L = []
R = []
rev = 0
for _ in range(int(input())):
    A = input().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())
query = []
for _ in range(q):
  query.append(input().split())

for i in query:
  if len(i) == 1:
    s.reverse()
  else:
    _, f, c = i
    if f == "1":
      s.reverse()
      s.append(c)
      s.reverse()
    else:
      s.append(c)
print("".join(s))
S = input()
Q = int(input())
ba = 1
for _ in range(Q):
  q = input().split()
  if len(q) == 1:
    ba *= -1
  else:
    T, F, C = q
    if (F == '1' and ba == 1) or (F=='2' and ba==-1):
      S = C + S
    elif (F == '2' and ba == 1) or (F=='1' and ba ==-1):
      S = S + C
print(S)
S = input()
Q = int(input())
reverse_flag = False

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

  if T[0] == '1':
    reverse_flag = not reverse_flag

  else:
    if (T[1] == '1' and reverse_flag) or (T[1] != '1' and (not reverse_flag)):
      S = T[2]+S

    else:
      S = S+T[2]
    reverse_flag = True 

print(S) if reverse_flag else print(S[::-1])
from collections import deque

S = input()
Q = int(input())
ans = deque()
ans.append(S)

for i in range(Q):
    query = input().split()
    if query[0] == "1":
        ans.reverse()
    if query[0] == "2":
        F, C = query[1:]
        if F == "1":
            ans.appendleft(C)
        else:
            ans.append(C)


print("".join(ans))
S = [input()]
Q = int(input())
for _ in range(Q):
  query = input().split()
  if query[0] == '1':
    S = S[::-1]
  else:  # query[0] == 2
    F, C = query[1:]
    if F == '1':
      S = [C] + S
    else:  # F == '2'
      S += [C]
print(''.join(S))
S = input()
Q = int(input())

r_flg = False
for i in range(Q):
    q = input()
    if q == '1':
        r_flg = True
        continue
    t,f,c = q.split()
    if f == '1':
        S = c + S if not r_flg else S + c
    else:
        S = S + c if not r_flg else c + S
if r_flg:
    print(S[::-1])
else:
    print(S)
s = input()
q = int(input())
count = 0
for i in range (q):
    t = input().split()
    if(t[0]=='2'):
        if((t[1] == '1' and count % 2 == 0 )or(t[1] == '2' and count % 2 == 1)):
            s = t[2] + s
        else:
            s = s + t[2]
    else:
        count+=1
if(count%2 == 1):
    print(s)
else:
    print(s[::-1])
s = list(input())
q = int(input())
i = 0
for i in range(q):
    arr = list(input().split(' '))
    if int(arr[0]) == 2:
        f, c = int(arr[1]), arr[2]
        if f == 1:
            s.insert(0, c)
        else:
            s.append(c) 
    else:
        if len(s) == 1:
            pass
        else:
            s[0], s[-1] = s[-1], s[0]
    i += 1

text = "".join(s)
print(text)
S = [input()]
Q = int(input())
for i in range(Q):
  temp = input().split()
  if temp[0] == "1":
    S.reverse()
  else:
    if temp[1] == "1":
      S.insert(0, temp[2])
    else:
      S.append(temp[2])
for i in S:
  print(i,end="")
from collections import deque
d=deque()
s=input()
d.append(s)
Q=int(input())
for i in range(Q):
    r=input()
    if len(r.split()) == 3:
        T,F,C=r.split()
    else:
        T='1'
    if T == '1':
        d.reverse()
        continue
    if T == '2':
        if F == '1':
            d.appendleft(C)
        if F == '2':
            d.append(C)

print("".join(d))
from collections import deque
x=input()
Q=int(input())
D=deque()
D.append(x)
count=0
for _ in range(Q):
	Y=list(input().split())
	if Y[0]=="1":
		count=(count+1)%2
	else:
		if Y[1]=="1":
			if count==0:
				D.appendleft(Y[2])
			else:
				D.append(Y[2])
		else:
			if count==0:
				D.append(Y[2])
			else:
				D.appendleft(Y[2])
if count==0:
	print("".join(D))
else:
	D.reverse()
	print("".join(D))
from collections import deque

S = deque(reversed(input().split()))
Q = int(input())
reverse = False
for i in range(Q):
    query = input().split()
    if query[0] == '1':
        reverse = not reverse
    else:
        right = query[1] == '1'
        if right == reverse:
            S.appendleft(query[2])
        else:
            S.append(query[2])
if reverse:
    print(''.join(list(S)))
else:
    print(''.join(list(reversed(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.split()[1:3]
    if F == "1":
      S = C + S
    else:
      S += C
print(S)
S = input()
n= int(input())

for i in range(n):
  Q = [i for i in input().split()]
  if Q[0] =='1':
    S = S[-1] + S[1:-1]+S[0]
    #print(S)
  else:
    if Q[1]=='1':
      S = Q[2]+S
    else:
      S =S+Q[2]
    #print(S)
print(S)
s=input()
q=int(input())
trans=0
aft=""
bef=""
for i in range(q):
  que=input()
  if que[0]=="1":
    trans=1-trans
  else:
    if (que[2]=="1" and trans==0) or (que[2]=="2" and trans==1):
      bef=bef+que[4]
    else:
      aft=aft+que[4]
if trans==0:
  print("".join(bef[::-1])+s+"".join(aft))
else:
  print("".join(aft[::-1])+s+"".join(bef))
S = list(input())
Q = int(input())
query = [input().split() for i in range(Q)]
print(query)
for i in query:
    if i[0] == '1':
        S = list(reversed(S))
    elif i[0]=='2' and i[1]=='1':
        S.insert(0, i[2])
    elif i[0]=='2' and i[1]=='2':
        S.append(i[2])
print(''.join(S))
#import time
S = input()
Q = int(input())
f = True
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):
            S = query[2]+S
        else:
            S = S+query[2]

print(S)

# start = time.time()
# elapsed_time = time.time() - start
# print("elapsed_time:{0}".format(elapsed_time) + "[sec]")
s = input()
N = int(input())
Q = [input().split() for _ in range(N)]

head = ''
tail = ''
reverse = 0

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

if reverse:
    print(tail[::-1] + s + head)
else:
    print(head[::-1] + s + tail)
from collections import deque


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

deq = deque(s)


flag = 1
for i in range(q):
    if query[i][0] == "1":
        flag ^= 1
    else:
        _, f, c = query[i]
        f = int(f) % 2
        if flag ^ f:
            deq.appendleft(c)
        else:
            deq.append(c)

ans = "".join(deq)
if not flag:
    print(ans)
else:
    print(ans[::-1])
from collections import deque
s = input()

q = int(input())
s = deque(s, maxlen=len(s) + q)
r = False
for _ in range(q):
    cmd = input()
    if cmd[0] == '1':
        r = not r
    else:
        cmd, f, c = cmd.split()
        if (f == '2' and not r) and (f=='1' and r):
            s.append(c)
        else:
            s.appendleft(c)
if r:
    s.reverse()
print(''.join(s))
s=input()
q=int(input())

while(q > 0):
    a=input().split()
    if len(a)&1:
        s = s[::-1]
    else:
        if int(a[1])&1:
            s = a[2] + s
        else:
            s = s + a[2]
    q-=1
print(s)
S=str(input())
Q=int(input())
for i in range(Q):
    T=str(input().split())
    if T[0]=="1":
        S.reverse()
    elif T[0]=="2":
        if T[1]=="1":
            S=T[2:]+S
        elif T[1]=="2":
            S=S+T[2:]
print(S)
import bisect
s=input().split()
a=int(input())
for i in range(a):
  n=input().split()
  if(n[0]=="1"):
    s =s[::-1]
  else:
    if(n[1]=="1"):
      s.insert(0,n[2])
    else:
      s.append(n[2])
s=''.join(s)
print(s)
s = input()
q = int(input())
l = [s]

for i in range(q):
    c = list(map(str, input().split()))
    if c[0] == '1':
        s = s[::-1]
    if c[0] == '2':
        if c[1] == '1':
            l.insert(0,c[2])
            s =''.join(l)
        if c[1] == '2':
            l.append(c[2])
            s =''.join(l)

print(s)
ans = [str(input())]
n = int(input())
num = [str(input()).split() for _ in range(n)] 
for i in num:
    if len(i) == 1:
        ans.reverse()
    else:
        if i[1] == "1":
            ans[0:0]=i[2]
        else:
            ans.append(i[2])
print("".join(ans))
inital_string = input()
num_of_q = int(input())
flag = True

for i in range(num_of_q):
  q = input()
  if q[0] == '1':
    flag = not flag
  else:
    if q[2] == "1":
      if flag:
        inital_string = inital_string + q[4]
      else:
      	inital_string = q[4] + inital_string
    else:
      if flag:
        inital_string = q[4] + inital_string
      else:
      	inital_string = inital_string + q[4]
      
print(inital_string)
s = input()
q = int(input())

sg = 1
fr = []
ba = []
for x in range(q):
  query = [s for s in input().split()]
  if query[0] == '1':
    sg *= -1
  else:
    if int(query[1])*2 + sg == 3:
      fr.append(query[2])
    else:
      ba.append(query[2])
        
if sg == 1:
  fr.reverse()
  frn = ''.join(fr)
  ban = ''.join(ba)
  print(frn + s + ban)
else:
  ba.reverse()
  frn = ''.join(fr)
  ban = ''.join(ba)
  print(ban + s + frn)
s = input()
q = int(input())

for n in range(q):
  query = input()
  # add new alphabet
  if (len(query) > 1):
    f, c = query[2], query[4]
    if (f == 1):
      s = c + s
    else:
      s = s + c
	# reverse
  else:
    s = s[::-1]
    
print(s)
import numpy as np
import sys

input = sys.stdin.readline

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

left = []
right = []
rev = False
for i in range(Q):
    l = input().split()
    if l[0] == '1':
        rev = not rev
    else:
        if ( l[1] == '1' and not rev) or (l[1] == '2' and rev):
            left.append(l[2])
        else:
            right.append(l[2])

S = ''.join(left[::-1]) + S + ''.join(right)

if rev:
    print(S[::-1])
else:
    print(S)
S=input()
b=0
x=''
y=''
for i in range(int(input())):
    q=input()
    if q=='1':b+=1
    else:
        a,f,c=q.split()
        if (int(f)-1)^(b%2):x=c+x
        else:y=y+c
S=x+S+y
print(S if b^1==0 else S[::-1])
from collections import deque
S=deque(input())
Q=int(input())
Reverse=0

for i in range(Q):
    a=input()
    if len(a)==1:
        Reverse+=1
    else:
        if int(a[2])+Reverse%2==1 or 3:
            S.appendleft(a[4])
        else:
            S.append(a[4])
if Reverse%2==0:
    ans="".join(S)
else:
    ans="".join(S)[::-1]
print(ans)
from collections import deque
S = list(input())
S = deque(S)
Q = int(input())
qlis = []
for _ in range(Q):
    qlis.append(input().split())

for i in range(Q):
    if len(qlis[i]) == 1:
        mae = S[0]
        usiro = S[-1]
        S[0] = usiro
        S[-1] = mae
    else:
        if int(qlis[i][1]) == 2:
            S.append(qlis[i][2])
        else:
            S.appendleft(qlis[i][2])

answer = "".join(S)
print(answer)
S = input()
for x in range(200000):
    try:
        q = input().split()
        if len(q) == 1:
            S = S[::-1]

        elif len(q) == 3:
            f = int(q[1])
            c = q[2]
            if f == 1:
                S = c + S
            elif f == 2:
                S += c
    except EOFError:
        break
print(S)
s=input()
q=int(input())

a=0
for i in range(q):
  t=input()
  if t=='1':
    a=a^1
  else:
    t, f, c = map(str, t.split())
    f=int(f)
    if f^a==0:
      s=s+c
    else:
      s=c+s
if a==1:
  s=s[::-1]
print(s)
s = list(input())
Q = int(input())
for i in range(Q):
    q = input().split()
    if int(q[0]) == 1:
        s[0], s[-1] = s[-1], s[0]
    else:
        if int(q[1]) == 1:
            s = list(q[2]) + s
        else:
            s = s + list(q[2])
print("".join(s))
s = list(input())
len_queries = int(input())
queries = [input().split() for _ in range(len_queries)]

is_forward = True
for query in queries:
    if "1" == query[0]:
        is_forward = not is_forward
    else:
        if is_forward:
            insert_index = 0 if "1" == query[1] else -1
            s.insert(insert_index, query[2])
        else:
            insert_index = -1 if "1" == query[1] else 0
            s.insert(insert_index, query[2])
if not is_forward:
    s.reverse()
print("".join(s))
string = input()
kai = int(input())
data = [input().split(' ') for i in range(kai)]

for i in range(kai):
    if(data[i][0] == '1'):
        if(len(string) >= 2):
            string = string[-1] + string[1:-1] + string[0]
    else:
        if(data[i][1] == '1'):
            string = data[i][2] + string
        else:
            string = string + data[i][2]

print(string)
Sr = list(input())
Sl = []
Q = int(input())


for i in range(Q):
    s = list(input().split())
    if len(s) == 1:
        Sr,Sl = Sl,Sr
    else:
        if s[1] == '1':
            Sl.append(s[2])
        else:
            Sr.append(s[2])

print('%s' % (''.join(Sl) + ''.join(Sr)))
import sys
from collections import deque

s = deque([sys.stdin.readline().rstrip()])
q = int(input())
cnt = 0
for i in range(q):
    query = sys.stdin.readline().split()
    if query[0] == '1':
        cnt = (cnt + 1)%2
    else:
        if (int(query[1]) + cnt)%2 == 1:
            s.appendleft(query[2])
        else:
            s.append(query[2])

if cnt:
    s.reverse()
print("".join(s))
S = list(input())
Q = int(input())
qlis = []
for _ in range(Q):
    qlis.append(input().split())

for i in range(Q):
    if len(qlis[i]) == 1:
        mae = S[0]
        usiro = S[-1]
        S[0] = usiro
        S[-1] = mae
    else:
        if int(qlis[i][1]) == 2:
            S.append(qlis[i][2])
        else:
            S.insert(0,qlis[i][2])

answer = "".join(S)
print(answer)
from collections import deque

S = deque(input().split())
Q = int(input())
reverse = False
for i in range(Q):
    query = input().split()
    if query[0] == '1':
        reverse = not reverse
    else:
        right = query[1] == '1'
        if right == reverse:
            S.appendleft(query[2])
        else:
            S.append(query[2])
if reverse:
    print(''.join(list(S)))
else:
    print(''.join(list(reversed(S))))
# -*- coding: utf-8 -*-


def main():
    from collections import deque

    s = input()
    q = int(input())
    d = deque()
    d.append(s)

    for i in range(q):
        qi = input().split()

        if qi[0] == '1':
            d.reverse()
        else:
            if qi[1] == '1':
                d.appendleft(qi[2])
            else:
                d.append(qi[2])

    print(''.join(map(str, d)))


if __name__ == '__main__':
    main()
from collections import deque
s = input()
q = int(input())
Q = [list(map(str, input().split())) for _ in range(q)]

S = deque([s])
S_reverse = S.reverse
S_appendleft = S.appendleft
S_append = S.append

for i in Q:
  if int(i[0]) == 1:
    S_reverse()
  else:
    if int(i[1]) == 1:
      S_appendleft(i[2])
    else:
      S_append(i[2])
      
print(''.join(S))
s = input()
q = int(input())
count = 0
a = []
b = []

for _ in range(q):
    Q = list(map(str, input().split()))
    if Q[0] == "1":
        count += 1
    else:
        if (int(Q[1])+count)%2 == 1:
            a.append(Q[2])
        else:
            b.append(Q[2])
        count = 0
a = "".join(a[::-1])
b = "".join(b)
s = a + s + b
if count%2 == 0 and count != 0:
    s = s[::-1]
print("".join(s))
s = input()
q = int(input())
left = ""
right = ""
flag_swap = False
for i in range(q):
  query = list(input().split())
  if query[0] == "1":
    flag_swap = not flag_swap
  else:
    if (query[1] == "1" and not flag_swap) or (query[1] == "2" and flag_swap):
      left += query[2]
    else:
      right += query[2]

if flag_swap:
  print(right[::-1]+s+left)
else:
  print(left[::-1]+s+right)
S = input()
Q = int(input())

f, l = S[0],S[-1]

for _ in range(Q):
    Query = input()
    if Query[0] == "1":
        f, l = l, f #swap
        S = f + S[1:-1] + l
    else:
        if Query[2] == "1":
            f = Query[4]
            S = f + S
        else:
            l = Query[4]
            S = S + l
    #print("Qans",S)

print(S)