peterverebics/CodeNet_Python_118 huggingface dataset:
S = input()
Q = int(input())

S_list = []
S_list.append(S)
step_list = []

for i in range(Q):
    step = list(input().split())
    step_list.append(step)
#print(step_list)

for i in range(Q):
    if step_list[i][0] == '1':
        S_list.reverse()
    elif step_list[i][0] == '2':
        if step_list[i][1] == '1':
            S_list.insert(0, step_list[i][2])
        elif step_list[i][1] == '2':
            S_list.append(step_list[i][2])

ans = "".join(S_list)
print(ans)
from collections import deque
s=input().split()
s=deque(s)
a=int(input())
for i in range(a):
  n=input().split()
  if(n[0]=="1"):
    s.reverse()
  else:
    if(n[1]=="1"):
      s.appendleft(n[2])
    else:
      s.append(n[2])
s=''.join(s)
print(s)
S = input()
Q = int(input())
flag = 1
for _ in range(Q):
    query = input()
    if len(query) == 1:
        flag = flag ^ 1
    else:
        F, C = int(query[2])-1, query[4]
        S = [C+S, S+C][flag^F]
print([S, S[::-1]][flag])
from collections import deque
a = list(input())
Q = int(input())
d = deque(a)
ans = 0
for i in range(Q):
  b = list(input().split())
  if len(b) == 1:
    ans += 1
  elif b[1] == "1" and ans % 2 == 1:
    d.appendleft(b[2])
  elif b[1] == "1" and ans % 2 == 0:
    d.append(b[2])
  elif b[1] == "2" and ans % 2 == 1:
    d.append(b[2])
  elif b[1] == "2" and ans % 2 == 0:
    d.appendleft(b[2])
print("".join(d))
import numpy as np
s = input()
q = int(input())
sgn = 1
for i in range(q):
    qi = np.array(input().split(),dtype=str)
    if qi[0] == "1":
        sgn *= -1
    else:
        if (qi[1] == "1" or sgn == -1) and (qi[1] == "2" or sgn == 1):
            s = qi[2] + s
        else:
            s = s + qi[2]

'''
if sgn == 1:
    print(s)
else:
    news = ''.join(list(reversed(s)))
    print(news)
'''
from collections import deque
t=list(input().split())
s=deque(t)
q=int(input())
flag=True
for i in range(q):
  ques=list(input().split())
  if ques[0]=="1":
    flag=not flag
    continue
  else:
    if ques[1]=="1":
      if flag:
        s.appendleft(ques[2])
      else:
        s.append(ques[2])
    else:
      if flag:
        s.append(ques[2])
      else:
        s.appendleft(ques[2])
if flag==False:
    print(''.join(reversed(list(s))))
else:
    print(''.join(list(s)))
S=input()
Q=int(input())
query=[]
for _ in range(Q):
    query.append(input().split())

for i in query:
    if i[0]=="1":
        S=S[::-1]
    else:
        if i[1]==1:
            S=i[2]+S
        else:
            S=S+i[2]
print(S)
s = list(input())
q = int(input())
for i in range(q):
  t = input().split()
  if t[0] == "1":
    h = s[-1]
    s[-1] = s[0]
    s[0] = h
  else:
    if t[1] == "1":
      s = [t[2]]+s
    else:
      s = s+[t[2]]
print("".join(s))
S = list(input())
Q = int(input())

flag = 0

for _ in range(Q):
    q = input().split()
    if q[0] == '1':
        flag ^= -1
    elif q[1] == '1':
        S.insert(flag, q[2])
    else:
        S.insert(flag ^ -1, q[2])

if flag:
    S.reverse()

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


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

for _ in range(Q):
    Query = input().split()
    if Query[0] == "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))
s=str(input())
s=list(s)
st=["A"]
q=int(input())
t=0
ta=0
for i in range(q):
  temp=str(input())
  temp=list(temp)
  if temp[0]=="1":
    t=t+1
    ta=ta+1
  else:
    if t%2!=0:
      st.reverse()
      t=0
    if temp[2]=="1":
      st.insert(0,temp[4])
    else:
      st.append(temp[4])
if t%2!=0:
  st.reverse()
if ta%2!=0:
  s.reverse()
l=st.index("A")
ans=st[0:l]+s+st[l+1:len(st)-1]
print("".join(ans))
import sys
fastinput = sys.stdin.readline
from collections import deque
S = input()
Q = int(input())
ans = deque([S])
Reverse = False
for q in range(Q):
    query = fastinput().split()
    if query[0]=="1":
        Reverse = not Reverse
    elif (query[1]=="1") ^ Reverse:#左側にくっつく
        ans.appendleft(query[2])
    else:
        ans.append(query[2])
SS = "".join(list(ans))
if Reverse:
    SS = "".join(reversed(ans))
print(SS)
s = str(input())
q = int(input())
for i in range(q):
    query = list(map(str, input().split()))
    query[0]=int(query[0])
    if len(query)!=1:
        query[1]=int(query[1])
    if query[0] == 1:
        if len(s) != 1:
            s = s[len(s)-1]+s[1:len(s)-1]+s[0]
    else:
        if query[1] == 1:
            s = query[2] + s
        else:
            s += query[2]

print(s)
from collections import deque
from functools import reduce
from operator import add
import sys

input = sys.stdin.readline

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

rev_cnt = 0
for i in range(q):
    query = input().split()
    if query[0] == '2':
        if (query[1] == '1' and rev_cnt % 2 == 0) or ((query[1] == '2' and rev_cnt % 2 == 1)):
            s.appendleft(query[2])
        else:
            s.append(query[2])
    else:
        rev_cnt += 1

res = reduce(add, s)
if rev_cnt % 2 == 1:
    res = res[::-1]

print(res)
def q1(s):
    #reverse
    return s[::-1]
def q2(s,f,char):
    if int(f) == 1:
        s = char + s
    else:
        s= s + char
    return s
 

s = input()
q = int(input())
for i in range(q):
    query = input().split()
    if len(query)==1:
        s=q1(s)
    else:
        print(query)
        s=q2(s,query[1],query[2])
print(s)
s = [input()]
q = int(input())

for _ in range(q):
    a = list(input().split())
    if len(a) == 1:
        s.reverse()
    else:
        if a[1] == "1":
            s.reverse()
            s += a[-1]
            s.reverse()
        else:
            s += a[-1]
print("".join(s))
S = input()
Q = int(input())
L = [list(input().split()) for i in range(Q)]

hanten = 0
t = [S]
for l in L:
    if len(l) == 1:
        hanten += 1
    elif (l[1] == '1' and hanten % 2 == 0) or (l[1] == '2' and hanten % 2 == 1):
        t.insert(0, l[2])
    else:
        t.append(l[2])
if hanten % 2 == 1:
    t.reverse()
ans = ''.join(t)

print(ans)
import sys
S = str(input())
Q = int(input())
Query = [list(map(str, input().split())) for _ in range(Q)]

sArry = S.split()
for i in range(Q):
#    print(i,Query[i])
    if Query[i][0] == '1':
        tmp_first = sArry[0]
        sArry[0] = sArry[-1]
        sArry[-1] = tmp_first
    else:
        if Query[i][1] == '1':
            sArry.insert(0, Query[i][2])
        else:
            sArry.append(Query[i][2])
            
print("".join(sArry))
# coding: utf-8
# Your code here!

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

mae = []
usiro = []
count = 0
for i in range(Q):
    query = input().rstrip().split()
    if len(query) == 3:
        if (count + int(query[1])) % 2 == 0:
            mae.append(query[2])
        else:
            usiro.append(query[2])
    else:
        count += 1
        
output = ''.join(mae[::-1]) + S + ''.join(usiro)
if count % 2 == 0:
    print(output[::-1])
else:
    print(output)
flag = True
S = input()
Q = int(input())
for i in range(Q):
  T = input().split()
  if T[0] == "1" :
    flag = not flag
  else:
    if flag:
      if T[1] == "1":
        S = T[-1] + S
      else:
        S = S + T[-1]
    else:
      if T[1] == "1":
        S = S + T[-1]
      else:
        S = T[-1] + S
print(S)
S = input()
Q = int(input())
T = 0
for i in range(Q):
    flag, *inp = input().split()
    if flag == "1":
        T += 1
    elif int(inp[0]) == T % 2:
        S = inp[1] + S
    else:
        S = S + inp[1]

if T % 2:
    print(S)
else:
    print(S[::-1])
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 = "".join(s)[::-1]
print(s)
s=[i for i in input()]
q=int(input())
t=0
for i in range(q):
  qu=list(input().split())
  if qu[0]=='2':
    if qu[1]=='1' and t==0:
      s=[qu[2]]+s
    elif qu[1]=='1' and t==1:
      s=s+[qu[2]]
    elif qu[1]=='2' and t==0:
      s=s+[qu[2]]
    elif qu[1]=='2' and t==1:
      s=[qu[2]]+s
  else:
    if t==0:
      t=1
    else:
      t=0
print(*s,sep='')
s = input()
q_ = int(input())

for q in range(q_):
  t = input()
  if t[0]==1: s=s[::-1]
  elif t[0]=='2' and t[2]=='1': s=t[4]+s
  elif t[0]=='2' and t[2]=='2': s+=t[4]
print(s)
S = list(input())
Q = int(input())

for i in range(Q):
  q = list(input().split())
  if int(q[0]) == 1:
    S[0], S[-1] = S[-1], S[0]
  else:
    if int(q[1]) == 1:
      S.insert(0, q[2])
    else:
      S.append(q[2])

print(''.join(S))
s = input()
q = int(input())
r = False#ひっくり返されたか
for i in range(q):
  t=list(input().split())
  if len(t) == 1:#一つしか入力されていない
    r = not(r)
  else:
    if (t[1] == '1' and r == False):
      s = t[2] + s
    elif (t[1] == '2' and r == True):
      s = t[2] + s
    else:
      s = s + t[2]
print(s)
from collections import deque
s=input()
Q=int(input())
hanten = 0
mae = deque()
ushiro = deque()
for i in range(Q):
	q=input()
	if q=="1":
		hanten ^= 1
	else:
		if q[2] == "1":
			if hanten == 0:
				mae.append(q[4])
			else:
				ushiro.append(q[4])
		else:
			if hanten == 0:
				ushiro.appendleft(q[4])
			else:
				mae.appendleft(q[4])
if hanten == 0:
	print("".join(mae) + s + "".join(ushiro))
else:
	print(("".join(mae) + s + "".join(ushiro))[::-1])
S=list(input())
Q=int(input())
ls=[list(input().split()) for _ in range(Q)]
a=1
for i in range(Q):
  if ls[i][0]=="1":
    a*=(-1)
  else:
    if (ls[i][1]=="1" and a==1) or (ls[i][1]=="2" and a==-1):
      S.insert(0,ls[i][2])
    else:
      S.append(ls[i][2])
print("".join(S) if a==1 else reversed("".join(S)))
s = input()
q = int(input())
t = []
 
def F(f,fr,ba,c):
  if f == '1':
     return c + fr, ba
  if f == '2':
    return fr, ba + c
 
def R(r):
  if r == '1':return '2'
  if r == '2':return '1'

fr = ''
ba = ''
r = '1'
for n in range(q):
  t = input().split()
  if t[0] == '1':
    r = R(r)
  else:
    if r =='2':
      t[1] = R(t[1])
    fr,ba = F(t[1], fr, ba,t[2])
 
    
if r == '1': 
   print(fr+s+ba)
else:
  print(fr+s+ba[::-1])
S = list(input())
Q = int(input())

for _ in range(Q):
  item = list(input().split())
  if item[0] == '1':
    S[0], S[-1] = S[-1], S[0]

  else:
    if item[1] == '1':
      S.insert(0, item[2])

    else:
      S.append(item[2])

print(''.join(S))
import sys
input = sys.stdin.readline
s = input()
q = int(input())
for _ in range(q):
    qi = input()
    if len(qi) <= 2:
        s = s[::-1]
    else:
        _,F,C = qi.split()
        if F == "1":
            s = C + s
        else:
            s = s + C           
print(s)
s = input()
q = int( input())

for query in range( q ):
  qi = input()
  if len( qi ) == 1:
    s = s[ len( s ) - 1 ] + s[ 1 : len( s ) - 1 ] + s[ 0 ]
  else:
    n, f, c = qi.split()
    if f == "1":
      s = c + s
    else:
      s = s + c

print( s )
S1=S2=input()
N=int(input())
count=0
for _ in range(N):
    *Q,=input().split()
    if len(Q)==1:
        count+=1
    else:
        if (count+int(Q[1]))%2:
            S1=Q[2]+S1
            S2=S2+Q[2]
        else:
            S2=S2+Q[2]
            S1=Q[2]+S1
print(S2 if count%2 else S1)
def main():
    s = input()
    n = int(input())
    pre = ''
    suf = ''
    r = 1
    for _ in range(n):
        q = input().split()
        if len(q) == 1:
            r *= -1
            continue
        if r * int(q[1]) in (1, -2):
            pre = q[2] + pre
        else:
            suf += q[2]
    ans = pre + s + suf
    if r == -1:
        ans = s[::-1]
    print(ans)


if __name__ == '__main__':
    main()
import numpy as np
import copy

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

Que = [0]* Q
for i in range(Q):
    Que[i] = list(map(str, input().split()))

answer = copy.copy(S)
check = 1
for i in range(Q):
    T = Que[i][0]
    if int(T) == 1:
        check *= -1
    else:
        F = Que[i][1]
        if (int(F) == 1 and check == 1) or (int(F) == 2 and check == -1):
            answer = ''.join([Que[i][2], answer])
        else:
            answer = ''.join([answer, Que[i][2]])
print(answer)
s = str(input())
q = int(input())

head = ''
back = ''
flg = 1
for _ in range(q):
    i = list(map(str, input().split()))
    if i[0] == '1':
        head, back = back, head
        flg *= -1
    else:
        if i[1] == '1':
            head = i[2] + head
        else:
            back = back + i[2]

if flg == 1:
    print(head[::-1]+s+back[::-1])
else:
    print(head + s+ back)
from collections import deque
s = input()
q = int(input())
ans = deque(s)
rev = 0
for i in range(q):
    qi = input()
    if qi[0] == '1':
        rev += 1
    else:
        parity = rev % 2
        if (int(qi[2]) + parity) % 2:
            ans.appendleft(qi[4])
        else:
            ans.append(qi[4])

if parity:
    ans.reverse()
for i in ans:
    print(i, end='')
s = list(input())
q = int(input())

for i in range(q):
    t = input()
    if len(t) == 1:
        tmp = s[0]
        s[0] = s[-1]
        s[-1] = tmp
    else:
        q2, f, c = t.split()
        if int(f) == 1:
            s = [c] + s
        else:
            s.append(c)

print("".join(s))
ans = [str(input())]
n = int(input())
num = [str(input()).split() for _ in range(n)] 
for i in num:
    if int(i[0]) == 1:
        ans.reverse()
    else:
        if int(i[1]) == 1:
            ans[0:0]=[i[2]]
        else:
            ans.append(i[2])
print("".join(ans))
from collections import deque

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

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

a = deque([s])

flag = True

for i in range(q):
    if query[i][0] == "1":
        flag = not(flag)
    else:
        if (query[i][1] == "1" and flag) or (query[i][1] != "1" and not(flag)):
            a.appendleft(query[i][2])
        else:
            a.append(query[i][2])

b = list(a)
if not(flag):
    b.reverse()

print("".join(b))
import sys
input = sys.stdin.readline
s=str(input())
q=int(input())
for i in range(q):
    que=list(input().split())
    if len(que)==1:
        s=s[::-1]
    else:
        if int(que[1])==1:
            s=que[2]+s
        elif int(que[1])==2:
            s=s+que[2]
print(s)
S = list(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(S)
print(''.join(S))
s = input()
Q = int(input())
reverse = True

for _ in range(Q):
    query = list(map(str, input().split()))
    if len(query) == 1:
        reverse = not reverse
    else:
        if (reverse and query[1] == "1") or (not reverse and query[1] == "2"):
            s = s + query[2]
        else:
            s = query[2] + s
if reverse:
    print(s[::-1])
else:
    print(s)
S = list(input())
Q = int(input())
a = 0
b = 0
for i in range(Q):
  q = list(input().split())
  if q[0] == '1':
      a += 1
      b += 1
    
  elif (q[1] == '1' and a%2 == 0) or (q[1] == '2' and a%2 == 1):
    S= [q[2]]+S
    a = 0
    
    
  else:
    S = S+[q[2]]
    a = 0
    
if b%2 == 1:
  S.reverse()
    
S="".join(S)
print(S)
message = [input().strip()]
N = int(input().strip())

for _ in range(N):
    ope = input().strip()
    if ope[0] == "1":
        message.reverse()
        continue
    i, s, c = ope.split(" ")
    if s == "1":
        message.insert(0, c)
    else:
        message.append(c)
print(''.join(message))
_strings = []
_strings.append(input())
num = int(input())
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))
from collections import deque

S = deque(input().split())
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 reverse:
    print(''.join(list(S)))
else:
    print(''.join(list(reversed(S))))
S = [input()]
q = int(input().strip())

for i in range(q):
  query = input()
  if query[0] == "1":
    S.reverse()
  elif query[0] == "2":
    query = query.strip().split(" ")
    if query[1] == "1":
      S.insert(0, query[2])
    else:
      S.append(query[2])

print("".join(S))
def main():
    from collections import deque
    import sys
    input = sys.stdin.readline
    s = deque(input().rstrip().split())
    q = int(input())
    for i in range(q):
        l = input().split()
        if int(l[0]) == 1:
            s.reverse()
        else:
            if int(l[1]) == 1:
                s.appendleft(l[2])
            else:
                s.append(l[2])
    print("".join(s))

if __name__ == "__main__":
    main()
from collections import deque
s = deque()
s.append(input())
q = int(input())
lines = [list(input().split()) for i in range(q)]
def q1(s):
    for i in range(len(s) // 2):
        s[i],s[-1-i] = s[-1-i],s[i]
    return s

def q2(s, l):
    if int(l[1]) == 1:
        s.appendleft(l[2])
    else:
        s.append(l[2])
    return s

for l in lines:
    if int(len(l)) == 1:
        q1(s)
    else:
        q2(s, l)

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

if rev == -1:
  s = s[::-1]
  
print(s)
S = input()
Q = int(input())
prev_t = 0
cnt = 0
for _ in range(Q):
    q = input()
    if len(q)>1:
        _, f, c = q.split()
        if (f=='1' and prev_t==0) or (f=='2' and prev_t==1):
            S = c+S
        else:
            S = S+c
        prev_t = 0
    else:
        prev_t = 1
        cnt+=1
if cnt%2==0:
    print(S)
else:
    print(S[::-1])
str = input()
N = int(input())
q = list([input().split()])
for i in range(N-1):
    q_next = input().split()
    if len(q[-1]) == 1 and len(q_next) == 1:
        continue
    else:
        q.append(q_next)
for i in q:
    if (i[0] == '1'):
        str = str[::-1]
    else:
        if (i[1] == '1'):
            str = i[2]+str
        else:
            str = str + i[2]
print(str)
S = list(input())
Q = int(input())
for q in range(Q):
    # 入力
    Q_list = list(input().split())
    
    # 操作
    if len(Q_list)==1:
        S[0], S[-1] = S[-1], S[0]
    elif (len(Q_list)==3) & (Q_list[1]=='1'):
        S.insert(0, Q_list[2])
    else:
        S.append(Q_list[2])

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

q_list = []

rev_c = 0
count = 0

for _ in range(q):
  v = input()
  q_list.append(v)
  if v == '1':
    rev_c += 1

if rev_c % 2 == 0:
  s = s[::-1]
  
for v in q_list:
  if v == '1':
    count += 1
  else:
    num = v[2]
    char = v[4]
    if ((rev_c-count)%2==0 and num=='1') or  ((rev_c-count)%2==1 and num == '2'):
      s = char + s
    else:
      s = s + char
print(s)
S=input()
q=int(input())
s=[]
for i in S:
    s.append(i)
t=1
for i in range(q):
    qv=list(input().split())
    if qv[0]=='1':
        t^=1
    else:
        if int(qv[1])^t==0:
            s.insert(0,qv[2])
        else:
            s.append(qv[2])
            
if t==0:
    ss=s.copy()
    s=[]
    for j in range(len(s)):
        s.append(ss[-1-j])
ans=''.join(s)
print(ans)
S = []
S.append(input("S"))
Q = int(input("Q"))
query = []
for i in range(Q) :
    query.append(input())
    if query[i] == "1" : #Ti = 1 reverse
        S[0], S[len(S)-1] = S[len(S)-1], S[0]
    else : #Ti = 2 str +
        T, F, C = query[i].split()
        if F == "1" :#Fi = 1 str + top
            S.insert(0, C)
        else :#Fi = 2 str + end
            S.append(C)
print(''.join(S))
from collections import deque

d = deque()
s = input()
d.append(s)
q = int(input())
for _ in range(q):
    t = input().split()
    if t[0] == "1":
        d.reverse()
    else:
        if t[1] == "1":
            d.appendleft(t[2])
        else:
            d.append(t[2])

print("".join(d))
s = input()
n = int(input())
print(n)
for i in range(n):
    li = input().split()
    if(li[0] == '1'):
        s = s[::-1]
    else:
        if(li[1] == '1'):
            s = li[2] + s
        else:
            s = s + li[2]
print(s)
word = input()
words = [word]
number = int(input())
for i in range(number):
    a = input().split(" ")
    if a[0] == "1":
        words[0], words[-1] = words[-1], words[0]
    else:
        if a[1] == "1":
            words.insert(0, a[2])
        else:
            words.append(a[2])
for j in words:
    print(j,end="")
S=input()
Q=int(input())
left=[]
right=[]
rev=0

for _ in range(Q):
    q=list(input().split())
    if int(q[0])==1:
        rev+=1
    else:
        if (int(q[1])+rev)%2==1:
            left.append(q[2])
            rev=0
        else:
            right.append(q[2])
            rev=0

S=left[::-1]+list(S)+right
if  rev % 2==0:
    print(*S[::-1],sep="")            
else:
    print(*S,sep="")
s=input()
q=int(input())
c=0
for _ in range(q):
  L=list(input().split())
  if L[0]=='1':
    c+=1
  else:
    if L[1]=='2':
      s+=L[2]
    else:
      t=L[2]+s
      s=t

if c//2==0:
  print(s)
else:
  s=s[::-1]
  print(s)
s = input()
q = int(input())

for i in range(q):
    t = input()
    if len(t) == 1:
        tmp = list(s)
        tmp[0] = s[-1]
        tmp[-1] = s[0]
        s = "".join(tmp)
    else:
        q2, f, c = t.split()
        if int(f) == 1:
            s = c + s
        else:
            s = s + c

print(s)
from collections import deque
S=deque([input()])
q=int(input())
rev=False
for i in range(q):
   s=input()
   if s[0]=="1":
      rev= not rev
   else:
      t,f,c=s.split()
      if (rev and f=="1") or (not rev and f=="2"):
         S.append(c)
      else:
         S.appendleft(c)
if not rev:
   print("".join(S))
else:
   print("".join(reversed(S)))
s = input()
n = int(input())
tmp  =['','']
t = 0
for i in range(n):
    t = list(map(str,input().split()))
    if t[0] == '1':
        a = tmp[1]
        tmp[1] = tmp[0]
        tmp[0] = a

    else:
        if t[1] == '1':
            tmp[0] = tmp[0] +t[2]
        else:
            tmp[1] = tmp[1] + t[2]
    # print(tmp)
print(tmp[0][::-1]+s+tmp[1])
s = input()
q_num = int(input())

t = True
for _ in range(q_num):
  q = input().split()
  if q[0] == "1":
    t = not t
  else:
    if not t:
      if q[1] == "1":
        s = q[2] + s
      else:
        s = s + q[2]
    else:
      if q[1] == "1":
        s = s + q[2]
      else:
        s = q[2] + s
        
if t:
  s = s[::-1]
print(s)
s = input()
n = int(input())

t = 0
for i in range(n):
  a = input().split()
  if int(a[0]) == 1:
    t += 1
    #s = s[::-1]
  else:
    if t % 2 == 1:
      s = s[::-1]
    t = 0
    if int(a[1]) == 1:
      s = a[2] + s
    if int(a[1]) == 2:
      s = s + a[2]
    
print(s)
S = input()
Q = int(input())

a = []
b = []

for _ in range(Q):
    q = input().split()
    if q[0] == '1':
        a, b = b, a
    else:
        if q[1] == '1':
            a.append(q[2])
        else:
            b.append(q[2])

#print(a)
#print(b)
ans = ''.join(a[::-1]) + S + ''.join(b)
print(ans)
import math

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

for _ in range(0, q):
    c = input().split()

    if len(c) == 3:
        d = int(c[1])
        e = c[2]
        s = e + s if d == 1 else s + e
    else:
        if len(s) == 1:
            s = s
        elif len(s) == 2:
            s = s[1] + s[0]
        else:
            s = s[-1] + s[1:-1] + s[0]

print(s)
S = list(input())
Q = int(input())

rev = False
for i in range(Q):
  q = input()
  if q[0] == '1':
    rev = not rev
  else:
    if q[2] == '1' and rev or q[2] == '2' and not rev:
      S.append(q[4])
    else:
      S.insert(0, q[4])
      
print(S[::-1] if rev else S)
s = str(input())
q = int(input())
inv = False
for i in range(q):
    a = input()
    if a == '1':
        inv = ~inv
        continue
    a = a.split()
    if (a[1] == '1')^inv:
        s = a[2] + s
    else:
        s += a[2]
if inv:
    print(s[::-1])
else:
    print(s)