peterverebics/CodeNet_Python_118 huggingface dataset:
from collections import deque

S = deque(input())
Q = int(input())
is_reversed = False
for _ in range(Q):
    q = input().split()
    if q[0] == "1":
        is_reversed = not is_reversed
    else:
        f = int(q[1]) - 1
        if is_reversed:
            f ^= 1
        c = q[2]
        if f:
            S.append(c)
        else:
            S.appendleft(c)
if is_reversed:
    S = reversed(S)
print(*S, sep="")
from collections import deque

s = deque(input())
q = int(input())
counter = 1
lista = [list(input().split()) for i in range(q)]

def rever(i):
    if i == '1':
        return 1
    else:
        return -1
for i in lista:
    if i[0] == '1':
        counter = -1 * counter
    else:
        if counter * rever(i[1]) > 0:
            s.appendleft(i[2])
        else:
            s.append(i[2])
s = "".join(list(s))
if counter > 0:
    print(s)
else:
    print(s[::-1])
S=input()
N=int(input())

from collections import*
D=deque(S)
count=0
for _ in range(N):
    *Q,=input().split()
    if len(Q)==1:
        count+=1
    else:
        if (count+int(Q[1]))%2:
            D.appendleft(Q[2])
        else:
            D.append(Q[2])
ans=''.join(D)
print(ans[::-1] if count%2 else ans)
s = [_ for _ in input()]
qn = int(input())
rev = False
prevs = ['']*200000
nexts = ['']*200000
previ = 0
nexti = 0

for _ in range(qn):
  q = [_ for _ in input().split()]
  if q[0] == '1':
    rev = not rev
  else:
    if (rev and q[1] == '2') or (not rev and q[1] == '1'):
      prevs[previ] = q[2]
      previ += 1
    else:
      nexts[nexti] = q[2]
      nexti += 1

prevs.reverse()
s = prevs + s + nexts
if rev:
  s.reverse()
print("".join(s))
from collections import deque

S = deque(list(input()))
Q = int(input())
q = [list(input().split()) for _ in range(Q)]
r = 0


for i in range(Q):
    if q[i][0] == "1":
        r += 1
    else:
        if (q[i][1] == "1" and r%2==0)or (q[i][1] == "2" and r%2==1 ):
            S.appendleft(q[i][2])
        else:
            S.append(q[i][2])

S = "".join(S)
if r%2==1:
    S = S[::-1]
print(S)
S = input()
N = int(input())
Q = tuple([_ for _ in input().split()] for _ in range(N))
C = 0
F = ""
R = ""
for q in Q:
    if q[0] == "1":
        C = (C+1)%2
    else:
        if (int(q[1])+C)%2 != 0:
            F = q[2] + F
        else:
            R = R + q[2]
S = F + S + R
if C != 0:
    S = S[::-1]
print(S)
S = input()
Q = int(input())

S_front, S_back = [], []
reverse = False

for i in range(Q):
    query = input().split()
    if query[0] == "1":
        reverse = not reverse
    else:
        T = False if (query[1] == "1") else True
        if reverse ^ T is False:
            S_front += query[2]
        else:
            S_back += query[2]

if reverse is True:
    S = S[::-1]
    ans = "".join(S_back)[::-1] + S + "".join(S_front)
else:
    ans = "".join(S_front)[::-1] + S + "".join(S_back)
print(ans)
from collections import deque
s = deque(input())
q = int(input())
query = [list(map(str, input().split())) for _ in range(q)]

order = True
for i in query:
    if i[0] == "1":
        order = not order
    elif i[1] == "1":
        if order:
            s.appendleft(i[2])
        else:
            s.append(i[2])
    else:
        if order:
            s.append(i[2])
        else:
            s.appendleft(i[2])

if not order:
    s.reverse()
print("".join(s))
from collections import deque

s_list = list(input())
d = deque(s_list)
q = int(input())
reverse = False

for _ in range(q):
	query= input().split()
	if int(query[0]) == 1:
		if reverse: reverse = False
		else: reverse = True
	elif int(query[0]) == 2:
		if int(query[1]) == 1:
			if reverse:
				d.append(query[2])
			else:
				d.appendleft(query[2])
		elif int(query[1]) == 2:
			if reverse:
				d.appendleft(query[2])
			else:	
				d.append(query[2])


if reverse:
	d.reverse()
for s in d:
  print(s, end ="")
S = input()
top = 0
end = len(S)-1
toright = True
N = int(input())
Q = [S[i] if i <= end else "" for i in range(end+1+N)]

for i in range(N):
  q = input().split()
  if q[0]=="1":
    toright = not toright
  else:
    if not(toright ^ (q[1]=="1")):
      Q[top-1]=q[2]
      top -= 1
    else:
      Q[end+1]=q[2]
      end += 1
ans = ""
#print(Q)
#print(toright)
for i in range(top, end+1):
  ans += Q[i]
if not toright:
  ans = ans[::-1]
print(ans)
from collections import deque
s = input()
s = deque([i for i in s])
q = int(input())
r = False
for _ in range(q):
    qu = input()
    if qu == "1":
        r = not r
    else:
        _, f, c = [i for i in qu.split()]
        if (f != "1") == r:
            s.appendleft(c)
        else:
            s.append(c)

s = "".join(s)
if r:
    s = reversed(s)
print("".join(s))
from collections import deque
S=deque(input())
Q=int(input())
h=0
for i in range(Q):
    q=input().split()
    if q[0]=="1":
        h=1-h
    else:
        t=int(q[1])
        if t==h+1:
            S.appendleft(q[2])
        else:
            S.append(q[2])
if h==0:
    for i in S:
        print(i,end="")
else:
    for i in range(len(S)):
        print(S[-i-1],end="")
from collections import deque
s=deque(list(input()))
q=int(input())
REVERSE=False
for _ in range(q):
  Input=input().split()
  if len(Input)==1:
    REVERSE = not REVERSE
  else:
    f=int(Input[1])
    c=Input[2]
    if not REVERSE:
      if f==1:
        s.appendleft(c)
      else:
        s.append(c)
    else:
      if f==1:
        s.append(c)
      else:
        s.appendleft(c)
if not REVERSE:
  print("".join(s))
else:
  print("".join(reversed(s)))
from collections import deque

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

d = deque(S)

cnt = 0 
for q in Q:
    if q[0] == "1":
        cnt+=1
        #d.reverse()
    elif q[0] == "2":
        if (cnt%2==0 and q[1] == "1") or (cnt%2!=0 and q[1] == "2"):
            d.appendleft(q[2])
        else:
            d.append(q[2])
            
if cnt%2 != 0:
    d.reverse()
print("".join(d))
S = input()
p = 0
head = ''
end = ''
Q = int(input())
for _ in range(Q):
  Qr = list(input().split())
  if int(Qr[0])==1:
    p = not p
  else:
    if int(Qr[1])==1:
      if p:
        end += Qr[2]
      else:
        head = Qr[2]+head
    else:
      if p:
        head = Qr[2]+head
      else:
        end += Qr[2]
S = head + S + end
if p:
  print(S[::-1])
else:
  print(S)
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()
Q = int(input())
Query = [input().split() for i in range(Q)]

mode = True  # True: Normal False: Reverse
pre = []
aft = []
for query in Query:
    if query[0] == '1':
        # モードを反転
        mode = not mode
    else:
        if (mode and (query[1] == '1')) or (not mode and (query[1] == '2')):
            pre.append(query[2])
        else:
            aft.append(query[2])
    
ans = ''.join(pre[::-1]) +  S + ''.join(aft)
    
if not mode:
    ans = ans[::-1]
 
print(ans)
S = list(input())
Q = int(input())
a = 0
A = []
B = []
for i in range(Q):
  q = list(input().split())
  q[0] = int(q[0])
  if q[0] == 1:
    if a == 0:
      a = 1
    else:
      a = 0
  else:
    q[1] = int(q[1])
    if (q[1] == 1 and a == 0) or (q[1] == 2 and a == 1):
      B.append(q[2])
    else:
      A.append(q[2])
B = B[::-1]
ans = B + S + A
if a == 1:
  ans = ans[::-1]
ans = ''.join(ans)
print(ans)
from collections import deque
S = deque(input())
Q = int(input())
reverse_count = 0
for i in range(Q):
    Q_i = list(input().split())
    if Q_i[0] == "1":
        reverse_count += 1
    elif ((Q_i[1] == "1" and reverse_count % 2 == 0) or
          (Q_i[1] == "2" and reverse_count % 2 == 1)):
        S.appendleft(Q_i[2])
    else:
        S.append(Q_i[2])
if reverse_count%2==1:
    S.reverse()
print("".join(S))
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])
s = "".join(s)
if cnt:
    s = s[::-1]
print(s)
from collections import deque
S = deque(input())
Q = int(input())
query = [input().split() for _ in range(Q)]
reverse = False
for q in query:
    if q[0] == '1':
        reverse = not reverse
    else:
        if q[1] == '1':
            S.append(q[2]) if reverse else S.appendleft(q[2])
        else:
            S.appendleft(q[2]) if reverse else S.append(q[2])
if reverse:
    S.reverse()
print(''.join(S))
from collections import deque

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

d = deque(S)
flg = True #Trueが前から,
for i in range(Q):
    t, *a = input().split()
    if t == "1":
        flg = not flg
    else:
        if (flg and a[0]=="1") or ((not flg) and a[0]=="2"):
            d.appendleft(a[1])
        else:
            d.append(a[1])
d = list(d)
if flg:
    print(''.join(d))
else:
    print(''.join(d[::-1]))
from collections import deque
import sys
readline = sys.stdin.readline


def main():
    a = input()
    d = deque(a)
    n = int(input())
    flagr = True
    for i in range(n):
        j = list(readline().split())
        if len(j) == 1:
            flagr = not flagr
        elif int(j[1] == "1") ^ flagr:
            d.append(j[2])
        else:
            d.appendleft(j[2])
    if not flagr:
        d.reverse()
    print("".join(d))


if __name__ == '__main__':
    main()
from collections import deque

S = deque(list(input()))
Q = int(input())
reverse = True
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 not reverse:
    print(''.join(list(reversed(S))))
else:
    print(''.join(list(S)))
"""
    Author: _YL_
"""
s = input()
n = int(input())

flip = 0
left = 0
right = 1
strs = ["", ""]

i = 0
while i < n:
    lst = input().split()

    if lst[0] == "1":
        flip += 1
        left = 0 if left == 1 else 1
        right = 0 if right == 1 else 1
    elif lst[1] == "1":
        strs[left] += lst[2]
    else:
        strs[right] += lst[2]
    i += 1

if flip % 2 == 1:
    s = s[::-1]
    
s = strs[left][::-1] + s + strs[right]

print(s)
from collections import deque

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

conr = 0

for i in range(Q):
  q = list(input().split())
  if q[0] == "1":
    conr += 1
  else:
    s = q[-1]
    f = int(q[1])
    if conr % 2 == 0:
      if f == 1:
        D.appendleft(s)
      else:
        D.append(s)
    else:
      if f == 1:
        D.append(s)
      else:
        D.appendleft(s)

D = list(D)
if conr % 2 == 1:
  D = D[::-1]
ans = "".join(D)
print(ans)
S=input()
Q=int(input())
added=(list(),list())
head=0
for i in range(Q):
    l=input().split()
    if int(l[0])==1:
        head=(head+1) % 2
    else:
        adding=(int(l[1])+head-1)%2
        added[adding].append(l[2])
#    print(added,head)

#print(added)
print(''.join(reversed(added[head])),end="")
if head==1:
    S=S[::-1]
print(S,end="")
print(''.join(added[(head+1)%2]))
from collections import deque
s = deque(input())
q = int(input())

rev = False

for _ in range(q):
    t = [x for x in input().split()]
    if t[0] == "1":
        rev = not rev
    else:
        if t[1] == "1":
            s.append(t[2]) if rev else s.appendleft(t[2])
        else:
            s.appendleft(t[2]) if rev else s.append(t[2])

if rev:
    s.reverse()
print("".join(s))
S = input()
additions = [[],[]]
direction = 0
Q = int(input())
for _ in range(Q):
    query = input().split()
    T = int(query[0])
    if T == 1:
        direction ^= 1
    else:
        F = int(query[1])
        C = query[2]
        additions[(F+direction+1) % 2].append(C)
if direction == 0:
    print(''.join(additions[0][::-1]), end='')
    print(S, end='')
    print(''.join(additions[1]))
else:
    print(''.join(additions[1][::-1]), end='')
    print(S[::-1], end='')
    print(''.join(additions[0]))
a=input();b='';l='';f=0
for _ in range(int(input())):
    k=list(input().split())
    if k[0]=='1':
        f=(f+1)%2
    else:
        if (k[1]=='1' and f==0) or (k[1]=='2' and f==1):
            b+=k[2]
        else:
            l+=k[2]
p=b[::-1]+a+l
print(p if f==0 else p[::-1])
from collections import deque

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

inverted = False

for _ in range(q):
  query = input()
  if query[0] == "1":
    inverted = not inverted 
  else:
    ti, fi, ci = query.split()
    if fi == "1" and inverted:
      s_queue.append(ci)
    elif fi == "1" and not inverted:
      s_queue.appendleft(ci)
    elif fi == "2" and inverted:
      s_queue.appendleft(ci)
    else:
      s_queue.append(ci)

if inverted:
  print("".join(s_queue)[::-1])
else:
  print("".join(s_queue))
from collections import deque

S = deque(input())
Q = int(input())
Query = [input().split() for _ in range(Q)]

reverse = False
for query in Query:
    if query[0] == '1':
        reverse = not reverse
    else:
        if query[1] == ('2' if reverse else '1'):
            S.appendleft(query[2])
        else:
            S.append(query[2])
print(''.join(reversed(S) if reverse else S))
s = input()
q = int(input())
a = []
b = []
c = 0
for i in range(q):
    init = list(input().split())
    if init[0] == '1':
        a, b = b, a
        c += 1
    else:
        if init[1] == '1':
            a.append(init[2])
        else:
            b.append(init[2])
if c % 2 == 1:
    s = s[::-1]
print(''.join(a[::-1]) + s + ''.join(b))
s = list(input())
q = int(input())

head = []
tail = []

mode = 0
for i in range(q):
  read = list(map(str,input().split()))
  if read[0]=='1':
    mode = (mode+1)%2
  else:
    if (read[1]=='1' and mode == 0) or (read[1]=='2' and mode == 1):
      head.append(read[2])
    else:
      tail.append(read[2])

s = head[::-1] + s
s = s + tail
if mode == 0:
  print(''.join(s))
else:
  print(''.join(s[::-1]))
S=input()
Q=int(input())
A= [input().split() for i in range(Q)]

tmp=0
s=S
t=''

for li in A:
    if li[0]=='1':
        tmp=(tmp+1)%2
    else:
        if (tmp+int(li[1]))%2 == 1:
            t += li[2]
        else:
            s += li[2]

if tmp == 0:
    print(t[::-1]+s)
else:
    print(s[::-1]+t)
import sys
from collections import deque

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

S = deque(input())
Q = int(input())
rev = False
for _ in range(Q):
    query = input().split()
    if len(query) == 1:
        rev = not rev
    else:
        if (not rev and query[1] == '1') or (rev and query[1] == '2'):
            S.appendleft(query[2])
        else:
            S.append(query[2])

if rev:
    print(''.join(reversed(S)))
else:
    print(''.join(S))
S=input()
Q=int(input())
lst=[]
for _ in range(Q):
    lst.append(input().split())

import collections

str=list(S)
d=collections.deque(str)

right=True
for operation in lst:
    if operation[0]=='1':
       right=not right

    else:
        if right==True and operation[1]=='1' or right==False and operation[1]=='2':
            d.appendleft(operation[2])
        else:
            d.append(operation[2])
str=list(d)
if right==True:
    print(''.join(str))
else:
    print(''.join(str[::-1]))
from collections import deque

s = deque(input())
n = int(input())

point = 0
for i in range(n):
  q = input().split()
  if q.pop(0)=='1':
    point = (point+1)%2
  else:
    _q = int(q.pop(0))-1
    if (point+_q)%2 == 0:
      s.appendleft(q.pop(0))
    else:
      s.append(q.pop(0))

if point == 1:
  s.reverse()

print(''.join(s))
s = input()
q = int(input())
s1 = ""
s2 = ""
c = 1
for i in range(q):
  a = input()
  if " " in a:
    l = list(a.split())
    c2 = 3-2*int(l[1])
    if c*c2 == 1:
      s1 = s1 + l[2]
    else:
      s2 = s2 + l[2]
  else:
    c = -c
s = s1[::-1] + s + s2
if c == 1:
  print(s)
else:
  print(s[::-1])
# https://atcoder.jp/contests/abc158/tasks/abc158_d

S = input()
Q = int(input())
T = ""

for _ in range(Q):
    query = input()
    ty = query[0]
    if ty == '1':
        S, T = T, S
    else:
        if query[2] == '1':
            T += query[4]
        else:
            S += query[4]

T = T[::-1]
print(T + S)
from collections import deque


s = deque(list(input()))
n = int(input())
flag = True
for _ in range(n):
    _input = input().split()
    if len(_input) == 1:
        if flag == True:
            flag = False
        else:
            flag = True
    else:
        if (_input[1] == '1' and flag == True) or (_input[1] == '2' and flag == False):
            s.appendleft(_input[2])
        else:
            s.append(_input[2])

if flag == True:
    print("".join(s))
else:
    print("".join([x for x in reversed(s)]))
S=input()
q=int(input())
rev=False
pre=''
for i in range(q):
    inp=input()
    if inp[0]=="1":
        rev=not rev
    else:
        t,f,c=inp.split()
        if (f=='1' and rev) or (f=='2' and not rev):
            S+=c
        else:
            pre+=c
if not rev:
    print(pre[::-1]+S)
else:
    print(S[::-1]+pre)
s = input()
q = int(input())

head = []
tail = []
flip = 0

for _ in range(q):
	query = input().split()
	if query[0] == '1':
		flip ^= 1
	else:
		f = query[1]
		c = query[2]
		if (f == '1' and flip == 0) or (f == '2' and flip == 1):
			head.append(c)
		else:
			tail.append(c)

if flip:
	head, tail = tail, head
	s = s[::-1]

head = ''.join(head)
tail = ''.join(tail)

print(head[::-1] + s + tail)
#!/usr/bin/env python3
from collections import deque

s = input()
q = int(input())
deq = deque(s)
cur = 1
for _ in range(q):
    query = input().split()
    if query[0] == "1":
        cur ^= 1
    else:
        f, c = query[1:]
        f = int(f) - 1
        if cur ^ f:
            deq.appendleft(c)
        else:
            deq.append(c)
ans = ""
while len(deq) > 0:
    ans += deq.popleft() if cur else deq.pop()
print(ans)
s=input()
q=int(input())
t=0
zengo=["",""]
for i in range(q):
    l=input().split()
    if len(l)==1:
        t^=1
    else:
        f,c=int(l[1])-1,l[2]
        zengo[f^t]+=c
if t:
    print(zengo[1][::-1]+s[::-1]+zengo[0])
else:
    print(zengo[0][::-1]+s+zengo[1])
from collections import deque

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

for i in range(q):
  a = list(map(str, input().split()))
  if(a[0] == "1"):
    top = (top + 1) % 2
    continue
  # a[0] == "2"
  if(top + int(a[1]) - 1 == 1):
    s.append(a[2])
  else:
    s.appendleft(a[2])
if(top == 0):
  print(''.join(s))
else:
  print((''.join(s))[::-1])
from collections import deque
s=input()
q=int(input())
n=0
que=deque(s)
for i in range(q):
    l=input()
    if l[0]=='1':
        n+=1
    else:
        if (l[2]=='1' and n%2==0) or (l[2]=='2' and n%2==1):
            que.appendleft(l[4])
        else:
            que.append(l[4])

ans=''.join(que)
if n%2==0:
    print(ans)
else:
    print(ans[::-1])
from collections import deque
s=input()
de=deque(list(s))
rev=0
append,appendleft=de.append,de.appendleft
for _ in range(int(input())):
	t=input()
	if t[0]=="1":
		rev^=1
	else:
		_,f,c=t.split()
		if f=="1":
			if rev:
				append(c)
			else:
				appendleft(c)
		else:
			if rev:
				appendleft(c)
			else:
				append(c)
li=list(de)
if rev:
	li=li[::-1]
print("".join(li))
S = input()
Q = int(input())
queries = [input().split() for i in range(Q)]

head = []
tail = []
forward = True
for query in queries:
    if query[0] == '1':
        forward = not forward
    else:
        if query[1] == '1' and forward or query[1] == '2' and not forward:
            head.append(query[2])
        else:
            tail.append(query[2])

if forward:
    ans = ''.join(head[::-1]) + S + ''.join(tail)
else:
    ans = ''.join(tail[::-1]) + S[::-1] + ''.join(head)
print(ans)
from collections import deque
S = deque(input())
Q = int(input())
sousa = []
for i in range(Q):
  sousa.append(input().split())

for j in range(Q):
  rev = 0
  if sousa[j][0] == '1':
    rev += 1
  else:
    if rev%2 == 1:
      S.reverse()
    if sousa[j][1] == '1':
      S.appendleft(sousa[j][2])
    else:
      S.append(sousa[j][2])
Ans = ''.join(list(S))
print(Ans)
import sys; input = sys.stdin.readline
def main():
  S = input()
  Q = int(input())
  for i in range(Q):
    TFC = input().split()
    S = S[::-1] if TFC[0]=="1" else (TFC[2]+S if TFC[1]=="1" else S+TFC[2])
  print(S)

if __name__=="__main__":
  main()
from collections import deque
S = deque(list(input()))
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.append(b[1])
    else:
      S.appendleft(b[1])
for i in range(len(S)):
  if f % 2 == 1:
    print (S[-i-1], end="")
  else:
    print (*S, sep="")
S=input()
Q=int(input())
query=[list(map(str,input().split())) for _ in range(Q)]

ans=[S]
for q in query:
  if len(q)==1:
    ans=ans[::-1]
  else:
    _,f,c=q
    if f=='1':
      ans.insert(0,c)
    else:
      ans.append(c)
print(*ans,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([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 == 0:
        que.appendleft(query[2])
    else:
        que.append(query[2])

ans = ''
while que:
    ans += que.popleft() if cnt == 0 else que.pop()

print(ans)
S=input()
Q=int(input())
l=list(input().split() for i in range(Q))
for j in l:
  if j[0]=="1":
    S=S[::-1]
  else:
    if j[1]=="1":
      S=S+j[2]
    else:
      S=j[2]+S

print(S)
from collections import deque

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

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

S = deque([s])
for i in Q:
  if int(i[0]) == 1:
    S.reverse()
  else:
    if int(i[1]) == 1:
      S.appendleft(i[2])
    elif int(i[1]) == 2:
      S.append(i[2])
      
print(''.join(S))
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 query[0] == "1":print("".join(s)[::-1])
else: print("".join(s))
S = list(input())
Q = int(input())
x = 1
for i in range(Q):
    a, *b = input().split()
    if a == "1":
        x *= -1
    elif a == "2":
        c, d = b
        if c == "2":
            x *= -1 
        if x == 1:
            S.insert(0, d)
        elif x == -1:
            S.append(d)
print(*S, sep="")
s = input()
q = int(input())

x = []
y = []
cnt = 0

for i in range(q):
    a = list(input().split())
    if a[0] == "1":
        x, y = y, x
        cnt += 1
    else:
        if a[1] == "1":
            x.append(a[2])
        else:
            y.append(a[2])

if cnt % 2 == 1:
    s = s[::-1]

print(''.join(x[::-1]) + s + ''.join(y))

print(s)
from sys import stdin
S = stdin.readline()[:-1]
Q = int(stdin.readline())
for _ in range(Q):
    command = stdin.readline()[::-1]
    if command[0] == '2':
        a, b, char = command.split()
        if b == '1': S = char + S
        else: S = S + char
    else: S = S[::-1]
print(S)
import sys 
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
S = readline().decode().rstrip()
Q = int(readline())
cnt = 0
start = ''
end = ''
for i in range(Q):
    A = list(map(str,readline().decode().split()))
    if A[0] == '1':
        cnt +=1
    else:
        if (cnt + int(A[1])) % 2:
            end += A[2]
        else:
            start += A[2]
ans = start + S + end
if cnt % 2:
    print(ans)
else:
    print(ans[::-1])
S=input()
Q=int(input())
def rev(s):
    l = len(s)
    return s[-1]+s[1:l-1]+s[0]
    
s=S[0]
for i in range(Q):
    T = input().split()
    if T[0]=='1':
        s=rev(s)
    elif T[0]=='2':
        F,C=T[1],T[2]
        if F=='1':
            s=str(C)+s
        else:
            s=s+str(C)
print(s)