peterverebics/CodeNet_Python_118 huggingface dataset:
import sys
sys.setrecursionlimit(10**6)
readline = sys.stdin.readline
s = readline().rstrip()
q = int(readline())
queries = [[i for i in readline().split()] for _ in range(q)]

t = ''

for query in queries:
    if query[0] == '1':
        s,t = t,s
    else:
        if query[1] == '1':
            t += query[2]
        else:
            s += query[2]
            
print(t[::-1]+s)
from collections import deque
s = input()

q = int(input())
s = deque(s)
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) or (f=='1' and r):
            s.append(c)
        else:
            s.appendleft(c)
if r:
    s.reverse()
print(''.join(s))
from collections import deque
S = input()
Q = int(input())

st = True
deq = deque(S)
for _ in range(Q):
    Query = input().split()
    if Query[0] == '1':
        st = not st
    else:
        if (Query[1] == '1') ^ st:
            deq.append(Query[2])
        else:
            deq.appendleft(Query[2])
if st:
    print(''.join(deq))
else:
    print(''.join(reversed(deq)))
from collections import deque

s = deque(input())
q = int(input())
qu = [[0] * 3] * q
count = 0

for i in range(q):
    qu[i] = input().split()

for i in range(q):
    if qu[i][0] == '1':
        count += 1
        continue

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

if count % 2:
    s.reverse()

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

S = input()
S = deque(S)
Q = int(input())
Query1 = 0
flag = False
for i in range(Q):
    Queryi = input().split()
    if Queryi[0] == '1':
        Query1 += 1
        flag = not flag
    else:
        if (Queryi[1] == '1') ^ flag:
            S.appendleft(Queryi[2])
        else:
            S.append(Queryi[2])

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

ans = ''.join(S)
print(ans)
#!/usr/bin/env python3
s = input()
Q = int(input())
q = [input().split() for _ in range(Q)]
reverse = 0
start = ""
end = ""
for i in q:
    if i[0] == "1":
        reverse ^= 1
    else:
        if (int(i[1]) + reverse) % 2:
            start += i[2]
        else:
            end += i[2]
s = start[::-1] + s + end
print(s[::1 - reverse * 2])
from collections import deque
S = deque(input())
Q = int(input())
rev = False
for i in range(Q):
  q = input()
  if len(q) == 1:
    rev = False if rev else True
  else:
    q, f, c = q.split()
    if rev:
      if f == "1":
        S.append(c)
      else:
        S.appendleft(c)
    else:
      if f == "1":
        S.appendleft(c)
      else:
        S.append(c)
print("".join(reversed(S)) if rev else "".join(S))
S = input()
Q = int(input())
reverse = False
front = ""
end = S

for _ in range(Q):
    I = input().split()
    if I[0] == "1":
        reverse = not reverse
    else:
        if (I[1] == "1") ^ reverse:
            front =front + I[2]
        else:
            end = end + I[2]


if reverse:
    print("".join(list(reversed(end)))+front)
else:
    print("".join(list(reversed(front)))+end)
s = input()
q_num = int(input())
head = []
tail = []
flip = 0
for i in range(q_num):
    q = tuple(input().split())
    if q[0] == '1':
        flip ^= 1
    else:
        f = int(q[1]) - 1
        c = q[2]
        if f ^ flip == 0:
            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)
S = input()
Q = int(input())
rev = False
head = []
tail = []
for i in range(Q):
    q = input()
    if q == '1':
        rev = not rev
    else:
        F, C = q.split()[1:]
        if (F == '1' and not rev) or (F == '2' and rev):
            head.append(C)
        else:
            tail.append(C)

head = ''.join(head)
tail = ''.join(tail)
S = head[::-1] + S + tail
if rev:
    S = S[::-1]
print(S)
from collections import deque

s = deque(input())
query_num = int(input())
query_li = list()
for _ in range(query_num):
    query_li.append(input().split())

rev_flag = False
for query in query_li:
    if query[0] == '1':
        rev_flag = not rev_flag
    elif (query[1] == '1' and rev_flag) or (query[1] == '2' and not rev_flag):
        s.append(query[2])
    else:
        s.appendleft(query[2])

if rev_flag:
    print(''.join(deque(reversed(s))))
else:
    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[::-1]+left)
else:
  print(left[::-1]+s+right)
from collections import deque
s = input()
q = int(input())

text = deque(s)
readleft = True


for _ in range(q):
  query = input().rstrip().split()
  if query[0] == "1":
    readleft = not(readleft)
  else:
    addtop = 2 - int(query[1])
    addleft = (addtop == readleft)
    if addleft:
      text.appendleft(query[2])
    else:
      text.append(query[2])

if readleft:
  while text != deque():
    print(text.popleft(), end="")
  print()
else:
  while text != deque():
    print(text.pop(), end="")
  print()
from collections import deque

def main():
  rev = False
  s = deque(input())
  q = int(input())
  query = tuple(input() for i in range(q))

  for i in range(q):
    temp = query[i].split()
    if int(temp[0]) == 1:
      rev = not rev
    else:
      if (int(temp[1]) == 1 and (not rev)) or (int(temp[1]) == 2 and rev):
        s.appendleft(temp[2])
      else:
        s.append(temp[2])

  if rev:
    s.reverse()

  print(''.join(s))

if __name__ == '__main__':
  main()
from collections import deque
d = deque()
S = input()
d.append(S)
N = int(input())
direct = 0
for i in range(N):
  X = input().split()
  if len(X) == 1:
    direct = 1 - direct
  else:
    if (X[1] == "1" and direct == 0) or (X[1] == "2" and direct == 1):
      d.appendleft(X[2])
    else:
      d.append(X[2])
      
S = "".join(list(d))
print(S) if direct == 0 else print(S[::-1])
from collections import deque
S = list(input())
Q = int(input())
num = 1
q = deque(S)
for _ in range(Q):
    L = [i for i in input().split()]
    if len(L) == 1:
        num += 1
        num %= 2
    else:
        if int(L[1])%2 == num:
            q.appendleft(L[2])
        else:
            q.append(L[2])

ans = list(q)
if num == 0:
    ans.reverse()
print("".join(ans))
from collections import deque

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

cont = 0
for i in range(q):
    q_uery = list(map(str, input().split()))
    if int(q_uery[0]) == 1:
        cont += 1
    if int(q_uery[0]) == 2:
        if (int(q_uery[1]) + cont) % 2 == 0:
            s.append(q_uery[2])
        else:
            s.appendleft(q_uery[2])
if cont % 2 == 1:
    s.reverse()

print(''.join(s))
S = input()
N = int(input())

pre = ""
suf = ""
f = 1
for i in range(N):
  Q = input()
  if Q[0] == "1":
    f *= -1
  elif Q[2] == "1":
    if f > 0:
      pre = Q[4] + pre
    else:
      suf = suf + Q[4]
  else:
    if f < 0:
      pre = Q[4] + pre
    else:
      suf = suf + Q[4]

ans = pre + S + suf
if f > 0:
  print(ans)
else:
  print(ans[::-1])
from collections import deque

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

flag = 0
for i in range(Q):
	q = list(map(str, input().split()))
	if q[0] == '1':
		flag = 1 - flag
	else:
		if (q[1] == '1' and flag == 1) or (q[1] == '2' and flag == 0):
			S.append(q[2])
		else:
			S.appendleft(q[2])
if flag == 1:
	S.reverse()
ans = ''.join(S)
print(ans)
S,f=__import__('collections').deque(input()),0
for _ in range(int(input())):
	T=input()
	if'2'>T:f=1-f
	else:(S.appendleft if(T[2]<'2')^f else S.append)(T[4])
if f:S.reverse()
print(*S,sep='')
S = input()
Q = int(input())

front = ''
back = ''

T_count = 0

for i in range(Q):
    command = input().split()
    if command[0] == '1':
        T_count = (T_count+1)%2
    else:
        if (int(command[1]) + T_count)%2 == 1:
            front += command[2]
        else:
            back += command[2]

if T_count == 0:
    print(front[::-1]+S+back)
else:
    print(back[::-1]+S[::-1]+front)
txt = input()
n = (int)(input())
count = 0
sentou = ""
saigo = ""
for i in range(n):
  temp = input()
  if temp[0] == '1':
    count += 1
  else:
    sec, moji = (int)(temp.split(" ")[1]), temp.split(" ")[2]
    if (sec + count) % 2 == 1:
      sentou = moji + sentou
    else:
      saigo += moji
if count % 2 == 1:
  txt = saigo[::-1] + txt[::-1] + sentou[::-1]
else:
  txt = sentou + txt + saigo
print(txt)
from collections import deque
d = deque()
s = input()
d.append(s)
rev = 0

Q = int(input())

for q in range(Q):
    ls = input().split()
    #print (ls)
    if ls[0] == '1':
        rev = (rev + 1) % 2
    else:
        if (int(ls[1]) + rev) % 2 == 0:
            d.append(ls[2])
        else:
            d.appendleft(ls[2])

ans = ''.join(list(d))
if rev == 1:
    ans = ans[::-1]
print (ans)
from collections import deque

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

t=deque(S)
flag_r=0
for _ in range(Q):
    Query=input().split()
    if int(Query[0])==1:
        flag_r=(flag_r+1)%2
    elif int(Query[1])==1+flag_r:
        t.appendleft(Query[2])
    else:
        t.append(Query[2])
result=''.join(t)
if flag_r:# reverseは最後にやる
    result=result[::-1]
print(result)
from collections import deque as dq
s = input()
nq = int(input())

s = dq(s)

jun = True
for i in range(nq):
  query = input().split()
  if query[0] == '1':
    jun = not jun
    continue
  if jun:
    if query[1] == '1':
      s.appendleft(query[2])
    else:
      s.append(query[2])
  else:
    if query[1] == '1':
      s.append(query[2])
    else:
      s.appendleft(query[2])
if jun:
  print(''.join(s))
else:
  print(''.join(list(s)[::-1]))
from collections import deque

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


rev_count = 0
for i in range(q):
  query = input()
  if query[0] == "1":
    rev_count += 1
  elif query[0] == "2":
    query = query.strip().split(" ")
    if rev_count & 1 == 1:
      if query[1] == "1":
        S.append(query[2])
      else:
        S.appendleft(query[2])
    else:
      if query[1] == "1":
        S.appendleft(query[2])
      else:
        S.append(query[2])

if rev_count & 1 == 1:
  S.reverse()

print("".join(S))
s=input()
q=int(input())
f=""
b=""
r=0
for i in range(q):
  query=input().split()
  if int(query[0])==1:
    r=1-r
  else:
    if (r^(int(query[1])-1))==0:
      f+=query[2]
    else:
      b+=query[2]
ret=f[::-1]+s+b
if r:
  ret=ret[::-1]
print(ret)
import sys
from collections import deque
sys.setrecursionlimit(10 ** 7)

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

S = deque(input())

Q = int(input())
TURN = False

for _ in range(Q):
    q = input()
    if q[0] == '1':
        TURN = not TURN
        continue

    _, f, c = q.split()

    FIRST = True if f == '1' else False

    if (FIRST and (not TURN)) or ((not FIRST) and TURN):
        S.appendleft(c)
    else:
        S.append(c)


if TURN:
    S.reverse()

print(''.join(S))
S = list(input())
Q = int(input())
Query = [list(input().split()) for i in range(Q)]

from collections import deque

q = deque(S)

r = 0  #反転の有無

for i in range(Q):
    if Query[i][0] == '1':
        r = 1-r
    else:
        if (r + int(Query[i][1])) % 2 == 1:
            q.appendleft(Query[i][2])
        else:
            q.append(Query[i][2])

A = list(q)

if r % 2 == 1:
    A.reverse()

ans = ''
for i in range(len(A)):
    ans += A[i]

print(ans)
s=input()
q=int(input())
c=1
u=''
for i in range(q):
    inp=input()
    if inp[0]=="1":
        c*=-1
    else:
        t,f,p=inp.split()
        if (f=="1" and c!=1) or (f=="2" and c==1):
            s+=p
        else:
        	u+=p
if c!=1:
    print("".join(reversed(s))+u)
else:
    print("".join(reversed(u))+"".join(s))
tailing = [input()]
heading = []
q_num = int(input())
is_reverse = False
for _ in range(q_num):
    query = [x for x in input().split()]
    if query[0] == '1':
        is_reverse = not is_reverse
    else:
        if (not is_reverse and query[1] == '2') or (is_reverse and query[1] == '1'):
            tailing.append(query[2])
        else:
            heading.append(query[2])
ans = ''.join(reversed(heading)) + ''.join(tailing)
print(ans if not is_reverse else ans[::-1])
S = input()

from collections import deque
S = deque(S)

Q = int(input())
rev = False

for i in range(Q):
  q = input().split()
  if q[0] == "1":
    rev ^= True
  else:
    F = q[1]
    if (F == "1" and (not rev)) or (F == "2" and rev):
      S.appendleft(q[2])
    else:
      S.append(q[2])

S = "".join(list(S))
if rev:
  S = S[::-1]
  
print(S)
s = input()
q = int(input())

rev = False
before = []
after = []
for i in range(q):
  inp = list(input().split())
  if inp[0] == '1':
    rev = not rev
  else:
    f = inp[1]
    c = inp[2]
    if rev:
      if f == '1':
        f = '2'
      else:
        f = '1'
    if f == '1':
      before.append(c)
    else:
      after.append(c)
ans = ''
if rev:
  after.reverse()
  ans = ''.join(after) + s[::-1] + ''.join(before)
else:
  before.reverse()
  ans = ''.join(before) + s + ''.join(after)
print(ans)
from collections import deque
d = deque()
for x in input():
    d.append(x)
Q = int(input())
f = 0
for _ in range(Q):
    l = list(input().split())
    if l[0] == '1':
        f ^= 1
    else:
        if f == 0: 
            if l[1] == '2': d.append(l[2])
            else: d.appendleft(l[2])
        else:
            if l[1] == '2': d.appendleft(l[2])
            else: d.append(l[2])
if f:
    d.reverse()
print("".join(d))
s = list(input())
n = int(input())
head = []
tail = []

cnt = 0
for _ in range(n):
    li = list(input().split())
    if li[0] == '1':
        head, tail = tail, head
        cnt += 1
    else:
        if li[1] == '1':
            head.append(li[2])
        else:
            tail.append(li[2])

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

ans = head[::-1] + s + tail
print(*ans , sep='')
import sys
s = sys.stdin.readline()
s = s[:-1]
q = int(input())

front = ''
back = ''
rv = False
for i in range(q):
    line = input()
    if line[0] == '1':
        rv = not rv
    else:
        ti, fi, ci = line.split()
        if (fi == '1' and rv) or (fi == '2' and not rv):
            back += ci
        else:
            front += ci

if rv:
    s = back[::-1] + s[::-1] + front
else:
    s = front[::-1] + s + back
print(s)
from collections import deque

s = deque(list(map(str, input().rstrip())))
q = int(input())

state = 1
for _ in range(q):
    li = list(map(str, input().split()))
    if li[0] == "1":
        state *= -1
    elif (li[1] == "1" and state == 1) or (li[1] == "2" and state == -1):
        s.appendleft(li[2])
    else:
        s.append(li[2])

if state == -1:
    s.reverse()

print("".join(s))
from collections import deque
S = input()
ds = deque(S)
Q = int(input())
query = []
for i in range(Q):
  tq = list(input().split())
  if (len(tq) == 1):
    query.append(1)
  if (len(tq) == 3):
    query.append([int(tq[1]),tq[2]])
eo = 1
for q in query:
  if q == 1:
    eo = eo*(-1)
  else:
    eoq = eo*q[0]
    if eoq == 1 or eoq == -2:
      ds.extendleft(q[1])
    else:
      ds.extend(q[1])
jds = "".join(ds)
if eo == -1:
  jds = jds[::-1]
print(jds)
s,_,*q=open(0)
a=['',s[:-1]]
p=1
for q in q:
  try:
    q,f,c=q.split()
    a[(f<'2')^p]+=c
  except:p^=1
print(a[p^1][::-1]+a[p])
s = input()
q = int(input())
a = 0
c = ""
d = ""
for i in range(q):
    b = list(input().split())
    if b[0] == "1":
        if a == 0:
            a = 1
        else:
            a = 0
    else:
        if (a, b[1]) == (0, "1") or (a, b[1]) == (1, "2"):
            c = b[2]+c
        else:
            d += b[2]
s = c+s+d
if a == 0:
    print(s)
else:
    print(s[::-1])
S = input()
Q = int(input())
front = ''
end = ''
reverse = False
for i in range(Q):
    Query = input()
    if Query[0] == '1':
        reverse = not reverse 
    else:
        if (Query[2] == '1' and reverse == False) or (Query[2] == '2' and reverse == True):
            front = Query[4] + front
        else:
            end = end + Query[4]

S = front + S + end
if reverse == True:
    S = S[::-1]


print(S)
print('\n')
s,_,*q=open(0)
a=['','']
p=1
for q in q:
  try:
    q,f,c=q.split()
    a[(f<'2')^p]+=c
  except:p^=1
print(a[p^1][::-1]+s[:-1][::p or-1]+a[p])
from collections import deque
s = deque(list(input()))
q = int(input())
cnt = 0
for _ in range(q):
    fc = input().split()
    if fc[0] == '1':
        cnt += 1
    else:
        if cnt == 1:
            fc[1] = '1' if fc[1] == '2' else '2'
        if fc[1] == '1':
            s.appendleft(fc[2])
        else:
            s.append(fc[2])
    cnt %= 2
if cnt == 1:
    s.reverse()
print(*s, sep='')
S=input()
Q=int(input())
L=list()
B=True
X=''
Y=''
for i in range(Q):
  L=list(input().split())
  if L[0]=='1':
    B=not(B)
  else:
    if ((L[1]=='1' and B) or (not(L[1]=='1' or B))):
      Y=Y+L[2]
    else:
      X=X+L[2]
S=S[::-1]
S=S+Y
S=S[::-1]
S=S+X
if not(B):
  S=S[::-1]
print(S)
from collections import deque
s = input()
q = int(input())
f = ""
b = ""
forward = True
for _ in range(q):
    que = list(input().split())
    if int(que[0]) == 1:
        if forward:
            forward = False
        else:
            forward = True
    else:
        if (int(que[1]) == 1 and forward) or (int(que[1]) == 2 and not forward):
            f+=que[2]
        else:
            b+=que[2]

if forward:
    print(f[::-1]+s+b)
else:

    print(b[::-1]+s[::-1]+f)
from collections import deque
S = input()
Q = int(input())

state = 1 # 反転していたら-1
Stock = deque([S])
for i in range(Q):
    q = input()
    if q == '1':
        state *= -1
    else:
        Y, f, c = q.split()
        if (state == 1 and f == '1') or (state == -1 and f == '2'):
            Stock.appendleft(c)
        elif (state == 1 and f == '2') or (state == -1 and f == '1'):
            Stock.append(c)
S = ''.join(Stock)
if state == -1:
    S = S[::-1]
print(S)
import sys
S = input()
Q = int(input())
key = 1

for i in sys.stdin:
    if i[0] == '0':
        break

    if i[0] == '1':
        key = -key
    
    elif key == 1:
        if i[2] == '1':
            S = i[4] + S
        else:
            S = S + i[4]

    else:
        if i[2] == '2':
            S = i[4] + S
        else:
            S = S + i[4]


print(S[::key])
s = input()
q = int(input())

cnt_reverse = False
left = ''
for i in range(q):
  query = input()
  
  if query[0] == '1':
    cnt_reverse = not cnt_reverse
  elif (query[2] == '1' and not cnt_reverse) or (query[2] == '2' and cnt_reverse):
    left = left + query[4]

  else:
    s  = s + query[4]
s = left[::-1] + s
if cnt_reverse:
  s = s[::-1]
print(s)
from collections import deque
s,q=deque([*input()]),int(input())
judge=False
for i in range(q):
  t=input().split()
  if len(t)==1:
    judge = not judge
  else:
    t,f,c=t
    if not judge and f=='1': s.appendleft(c)
    elif not judge and f=='2': s.append(c)
    elif judge and f=='1': s.append(c)
    else: s.appendleft(c)
if judge: s.reverse()
print(*s,sep='')
#/usr/bin/env python3
 
s = list(input())
Q = int(input())
 
reverse = False
leading = []
trailing = []
 
for q in range(Q):
    query = input().split()
 
    if len(query) == 1:
        reverse = not reverse
    else:
        direction = int(query[1])
        if (direction == 1 and not reverse) or (direction == 2 and reverse):
            leading.append(query[2])
        else:
            trailing.append(query[2])
 
if reverse:
    print(''.join((leading[::-1] + s + trailing)[::-1]))
else:
    print(''.join(leading[::-1] + s + trailing))
from collections import deque

S = deque(input())
Q = int(input())
Qs = [list(input().split()) for i in range(Q)]

is_reverse = False
for query in Qs:
    if query[0] == '1' and len(S) != 1:
        is_reverse = not is_reverse
    else:
        f = int(query[1])
        c = query[2]
        if (f == 1 and is_reverse) or (f == 2 and not is_reverse):
            S.append(c)
        else:
            S.appendleft(c)

if is_reverse:
    S.reverse()

print(''.join(S))
from collections import deque
s = deque(input())
q = int(input())
lis = [list(input().split()) for _ in range(q)]
rev = 0
z = 0

for i in lis:
    if i[0] == "2":
        if (rev + int(i[1])) % 2 == 1:
            s.appendleft(i[2])
        else:
            s.append(i[2])
        z = 1
    else:
        if z == 1:
            rev += 1
        
if rev % 2 == 1:
    s = reversed(s)
print("".join(s))
from collections import deque
S = deque(list(input()))
Q = int(input())
cnt = 0
for i in range(Q):
    query = list(input().split())
    if query[0] == '1':
        cnt += 1
    elif query[1] == '1':
        if cnt % 2 == 0:
            S.appendleft(query[2])
        else:
            S.append(query[2])
    else:
        if cnt % 2 == 0:
            S.append(query[2])
        else:
            S.appendleft(query[2])
if cnt % 2 != 0:
    S.reverse()
print(''.join(S))
from collections import deque
S = list(input())
Q = int(input())

S = deque(S)

rev = False
for i in range(Q):
  query = list(input())
  if query[0] == '1':
    rev = not rev
  else:
    c = query[4]
    if query[2] == '1' and not rev or (rev and query[2] == '2'):
      S.appendleft(c)
    else:
      S.append(c)
      
if rev:
  S.reverse()


print(''.join(S))
s = input()
q = int(input())
Q = [input().split() for _ in range(q)]

import collections
dq = collections.deque(s)

rev = 1
for i in Q:
    if i == ['1']:
        rev *= -1
    else:
        if (i[1] == '1' and rev == 1) or (i[1] == '2'and rev == -1):
            dq.appendleft(i[2])
        else:
            dq.append(i[2])

if rev == 1:
    print(''.join(dq))
else:
    print(''.join(dq)[::-1])
from collections import deque
S=input()
Q=int(input())
q=deque()
for i in S:
  q.append(i)
cnt=0
for i in range(Q):
  A=list(input().split())
  if int(A[0])==1:
    cnt+=1
  else:
    if int(A[1])==cnt%2+1:
      q.appendleft(A[-1])
    else:
      q.append(A[-1])
if (cnt%2)==0:
  for i in range(len(q)):
    print(q.popleft(),end='')
else:
  for i in range(len(q)):
    print(q.pop(),end='')
from collections import deque
s = deque(list(input()))
n = int(input())
flag = True
for _ in range(n):
    q = list(map(str, input().split()))
    if len(q) == 1:
        if flag:
            flag = False
        else:
            flag = True
    if len(q) == 3:
        if (q[1] == "1" and flag == False) or (q[1] == "2" and flag):
            s.append(q[2])
        else:
            s.appendleft(q[2])
if flag == False:
    s.reverse()
print("".join(s))
#D
s_list=list(input())
q=int(input())
r=0
sl_list=[]
for i in range(q):
    ope=input().split()
    if ope[0]=='1':
        r+=1
    else:
        if (int(ope[1])+r)%2 == 1:
            sl_list.append(ope[2])
        else:
            s_list.append(ope[2])
sl_list.reverse()
s_list=sl_list + s_list
if r%2 !=0:
    s_list.reverse()
print(''.join(s_list))
from collections import deque
s = input()
q = int(input())

ans = deque()
ans.append(s)
reverse = False
for i in range(q):
    x = list(input().split())
    if int(x[0]) == 1:
        reverse = not reverse
        continue
    if int(x[1]) == 1 and not reverse:
        ans.appendleft(x[2])
    elif int(x[1]) == 2 and reverse:
        ans.appendleft(x[2])
    else:
        ans.append(x[2])

ans = ''.join(ans)
if reverse:
    print(ans[::-1])
else:
    print(ans)
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[::-1]+"".join(before_chr))
s = input()
q = int(input())

from collections import deque

flip = False
deq = deque(s)

for _ in range(q):
    query = input().split(' ')
    if len(query)==1:
        flip = not flip
    else:
        f,c = int(query[1])-1,query[2]
        if flip ^ f:
            deq.append(c)
        else:
            deq.appendleft(c)

if flip:
    deq.reverse()

res =""
for c in deq:
    res+=c

print(res)