AtCoder Beginner Contest 158 - String Formation

Takahashi has a string S consisting of lowercase English letters.
Starting with this string, he will produce a new one in the procedure given as follows.
The procedure consists of Q operations. In Operation i (1 \leq i \leq Q), an integer T_i is provided, which means the following:
 - If T_i = 1: reverse the string S.
 - If T_i = 2: An integer F_i and a lowercase English letter C_i are additionally provided.
 - If F_i = 1 : Add C_i to the beginning of the string S.
 - If F_i = 2 : Add C_i to the end of the string S.
Help Takahashi by finding the final string that results from the procedure.

-----Constraints-----
 - 1 \leq |S| \leq 10^5
 - S consists of lowercase English letters.
 - 1 \leq Q \leq 2 \times 10^5
 - T_i = 1 or 2.
 - F_i = 1 or 2, if provided.
 - C_i is a lowercase English letter, if provided.

-----Input-----
Input is given from Standard Input in the following format:
S
Q
Query_1
:
Query_Q

In the 3-rd through the (Q+2)-th lines, Query_i is one of the following:
1

which means T_i = 1, and:
2 F_i C_i

which means T_i = 2.

-----Output-----
Print the resulting string.

-----Sample Input-----
a
4
2 1 p
1
2 2 c
1

-----Sample Output-----
cpa

There will be Q = 4 operations. Initially, S is a.
 - Operation 1: Add p at the beginning of S. S becomes pa.
 - Operation 2: Reverse S. S becomes ap.
 - Operation 3: Add c at the end of S. S becomes apc.
 - Operation 4: Reverse S. S becomes cpa.
Thus, the resulting string is cpa.

Difficulty: MEDIUM

Starter code:

Solutions:
s1 = list(input())
q = int(input())
s0 = []
f = 1
for i in range(q):
	a = list(input().split())
	if int(a[0]) == 1:
		f = (f + 1) % 2
	elif (f + int(a[1])) % 2 == 0:
		s0.append(a[2])
	else:
		s1.append(a[2])
if f == 1:
	print(''.join(s0[::-1]) + ''.join(s1))
else:
	print(''.join(s1[::-1]) + ''.join(s0))


from collections import deque
S = deque(input())
q = int(input())
rev = False
for i in range(q):
	c = list(input().split())
	if c[0] == '1':
		rev = not rev
	elif c[1] == '1' and rev == False:
		S.appendleft(c[2])
	elif c[1] == '2' and rev == True:
		S.appendleft(c[2])
	else:
		S.append(c[2])
if rev:
	S.reverse()
ans = ''.join(S)
print(ans)


from collections import deque
s = deque(list(input()))
n = int(input())
Q = []
for i in range(n):
	Q.append(list(input().split()))
count = 0
for q in Q:
	if q[0] == '1':
		count += 1
	elif q[0] == '2' and count % 2 == 0:
		if q[1] == '1':
			s.appendleft(q[2])
		else:
			s.append(q[2])
	elif q[1] == '1':
		s.append(q[2])
	else:
		s.appendleft(q[2])
ans = ''.join(s)
if count % 2 == 0:
	print(ans)
else:
	print(ans[::-1])


from collections import deque
s = deque(input())
rev = 0
q = int(input())
for i in range(q):
	query = list(map(str, input().split()))
	if query[0] == '1':
		rev += 1
		rev %= 2
	else:
		(f, c) = query[1:]
		if f == '1':
			if rev:
				s.append(c)
			else:
				s.appendleft(c)
		elif rev:
			s.appendleft(c)
		else:
			s.append(c)
s = ''.join(s)
if rev:
	print(s[::-1])
else:
	print(s)


from collections import deque
import sys
read = sys.stdin.read
readlines = sys.stdin.readlines
input = sys.stdin.readline
S = list(input().rstrip())
S = deque(S)
input()
rev = 0
for line in readlines():
	(q, *query) = line.split()
	if q == '1':
		rev ^= 1
	else:
		(f, c) = query
		if f == '1':
			if not rev:
				S.appendleft(c)
			else:
				S.append(c)
		elif not rev:
			S.append(c)
		else:
			S.appendleft(c)
print(''.join(S) if not rev else ''.join(S)[::-1])


s = input()
q = int(input())
t = ''
for i in range(q):
	que = input().split()
	if que[0] == '1':
		(s, t) = (t, s)
	elif que[1] == '1':
		t += que[2]
	else:
		s += que[2]
print(t[::-1] + s)


S = input()
Q = int(input())
T = ''
for i in range(Q):
	q = input().split()
	if q[0] == '1':
		(T, S) = (S, T)
	elif q[1] == '1':
		T += q[2]
	else:
		S += q[2]
print(T[::-1] + S)


f = input
from collections import deque
s = deque(f())
n = int(f())
t = 0
for i in range(n):
	q = f()
	if q[0] == '1':
		t ^= 1
	elif int(q[2]) + t == 2:
		s.append(q[4])
	else:
		s.appendleft(q[4])
print(''.join([s, list(s)[::-1]][t]))


from collections import deque
s = deque(input())
q = int(input())
l = list((list(input().split()) for _ in range(q)))
t = 0
for i in l:
	if len(i) == 1:
		t += 1
	else:
		(a, f, c) = i
		if f == '1' and t % 2 == 0 or (f == '2' and t % 2 == 1):
			s.appendleft(c)
		else:
			s.append(c)
if t % 2 == 1:
	s.reverse()
print(*s, sep='')


from collections import deque

def main():
	s = deque(input())
	q = int(input())
	flag = 0
	query = []
	for _ in range(q):
		temp = input().split()
		if len(temp) == 1:
			flag += 1
		else:
			query.append(temp + [flag % 2])
	for q in query:
		if q[-1] == 0:
			if q[1] == '1':
				s.appendleft(q[2])
			else:
				s.append(q[2])
		elif q[1] == '1':
			s.append(q[2])
		else:
			s.appendleft(q[2])
	if flag % 2 == 1:
		print(''.join(list(s)[::-1]))
	else:
		print(''.join(s))
main()


S = input()
Q = int(input())
added = ''
inv = 0
for i in range(Q):
	query = input().split()
	if query == ['1']:
		inv = (inv + 1) % 2
	else:
		(f, c) = (query[1], query[2])
		if f == '1' and inv or (f == '2' and (not inv)):
			S += c
		else:
			added += c
if inv:
	print(S[::-1] + added)
else:
	print(added[::-1] + S)


import sys
from collections import deque, Counter, defaultdict
import bisect
import heapq
import math
import itertools
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 8)
inf = 10 ** 18
MOD = 1000000007
ri = lambda : int(input())
rs = lambda : input().strip()
rl = lambda : list(map(int, input().split()))
mod = 998244353
s = rs()
q = ri()
cnt = 0
(head, tail) = ([], [])
for i in range(q):
	qry = list(input().split())
	if len(qry) == 3:
		if int(qry[1]) % 2 ^ cnt % 2:
			head.append(qry[2])
		else:
			tail.append(qry[2])
	else:
		cnt += 1
ans = ''.join(head[::-1]) + s + ''.join(tail)
print(ans if cnt % 2 == 0 else ans[::-1])


(s, _, *q) = open(0)
a = '%s'
p = 1
for q in q:
	try:
		(q, f, c) = q.split()
		if (f < '2') ^ p:
			a += c
		else:
			a = c + a
	except:
		p ^= 1
print((a % s[:-1])[::p or -1])


import sys
input = sys.stdin.readline
rev = 0
ST = ''
EN = ''
S = input()
Q = int(input())
for i in [0] * Q:
	A = list(input().split())
	if int(A[0]) == 1:
		rev += 1
	elif (int(A[1]) + rev) % 2 == 1:
		ST += A[2]
	else:
		EN += A[2]
if rev % 2 == 0:
	print(ST[::-1].strip() + S.strip() + EN.strip())
else:
	print(EN[::-1].strip() + S[::-1].strip() + ST.strip())


from collections import deque
S = str(input())
s = []
for i in range(len(S)):
	s.append(S[i])
s = deque(s)
Q = int(input())
eo = 0
for query in range(Q):
	temp = list(map(str, input().split()))
	if temp[0] == '1':
		eo += 1
	elif temp[1] == '1':
		if eo % 2 == 0:
			s.appendleft(temp[2])
		else:
			s.append(temp[2])
	elif eo % 2 == 0:
		s.append(temp[2])
	else:
		s.appendleft(temp[2])
s = list(s)
if eo % 2 == 0:
	ans = ''.join(s)
else:
	s = s[::-1]
	ans = ''.join(s)
print(ans)


S = input()
Q = int(input())
lS = ''
for _ in range(Q):
	query = input().split()
	if query[0] == '1':
		(lS, S) = (S, lS)
	elif query[1] == '1':
		lS += query[2]
	else:
		S += query[2]
print(lS[::-1] + S)


import sys
s = input()
q = int(input())
a = ['', '']
r = 0
for l in sys.stdin:
	if l[0] == '1':
		r ^= 1
	else:
		a[0 if l[2] == '21'[r] else 1] += l[4]
if r:
	print(a[0][::-1] + s[::-1] + a[1])
else:
	print(a[1][::-1] + s + a[0])


from collections import deque
d = deque()
d.extend(input())
q = int(input())
r = False
for i in range(q):
	qu = input().split()
	if qu[0] == '1':
		r = not r
	elif qu[1] == '1' and (not r) or (qu[1] == '2' and r):
		d.extendleft(qu[2])
	else:
		d.extend(qu[2])
if r:
	d.reverse()
print(''.join(list(d)))


from collections import deque
(s, j) = (deque(input()), False)
for _ in range(int(input())):
	t = input().split()
	if t[0] == '1':
		j = not j
		continue
	if t[1] == '1':
		if j:
			s.append(t[2])
		else:
			s.appendleft(t[2])
	elif j:
		s.appendleft(t[2])
	else:
		s.append(t[2])
if j:
	s.reverse()
print(*s, sep='')


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))


from collections import deque
S = input()
Q = int(input())
cnt = 0
D = deque()
D.append(S)
for _ in range(Q):
	q = input()
	if q == '1':
		cnt += 1
	else:
		(q, f, c) = list(map(str, q.split()))
		f = int(f)
		if cnt % 2 == 0:
			if f == 2:
				D.append(c)
			else:
				D.appendleft(c)
		elif f == 2:
			D.appendleft(c)
		else:
			D.append(c)
X = ''.join(D)
print(X if cnt % 2 == 0 else X[::-1])


s = input()
n = int(input())
t = True
a = str()
b = str()
for i in range(n):
	q = input().split()
	if len(q) == 1:
		t = not t
		continue
	if int(q[1]) == 1:
		if t:
			a = q[2] + a
		else:
			b = b + q[2]
	if int(q[1]) == 2:
		if t:
			b = b + q[2]
		else:
			a = q[2] + a
if t:
	print(a + s + b)
else:
	print(b[::-1] + s[::-1] + a[::-1])


s = input()
q = int(input())
(h, t) = ([], [])
p = 0
for i in range(q):
	c = input().split()
	if c[0] == '1':
		(h, t) = (t, h)
		p = 1 - p
	elif c[1] == '1':
		h.append(c[2])
	else:
		t.append(c[2])
if p == 1:
	s = s[::-1]
print(''.join(h[::-1]) + s + ''.join(t))


from collections import deque
s = deque(input())
q = int(input())
flag = 0
for _ in range(q):
	query = input().split()
	if len(query) == 1:
		flag = (flag + 1) % 2
	elif (int(query[1]) + flag) % 2:
		s.appendleft(query[2])
	else:
		s.append(query[2])
print(''.join(s) if flag == 0 else ''.join(reversed(s)))


from collections import deque
s = input()
q = int(input())
r = False
S = deque(s)
for _ in range(q):
	d = list(input().split())
	if len(d) == 1:
		r = ~r
	elif d[1] == '1' and ~r or (d[1] == '2' and r):
		S.appendleft(d[2])
	else:
		S.append(d[2])
s = ''.join(S)
if r:
	s = s[::-1]
print(s)


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
	elif (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)


from queue import deque
S = input()
Q = int(input())
Query = list((input().split() for _ in range(Q)))
count = 0
(L, R) = (deque(), deque())
for i in range(Q):
	if Query[i][0] == '1':
		count += 1
	elif Query[i][1] == '1':
		if count % 2 == 0:
			L.appendleft(Query[i][2])
		else:
			R.append(Query[i][2])
	elif count % 2 == 0:
		R.append(Query[i][2])
	else:
		L.appendleft(Query[i][2])
(L, R) = (''.join(L), ''.join(R))
if count % 2 == 0:
	print(L + S + R)
else:
	print(R[::-1] + S[::-1] + L[::-1])


import sys
from collections import deque

def LS():
	return list(input().split())
S = deque(input())
Q = int(input())
rev = 0
for i in range(Q):
	A = LS()
	if A[0] == '1':
		rev += 1
		rev %= 2
	elif A[1] == '1':
		if rev == 0:
			S.appendleft(A[2])
		else:
			S.append(A[2])
	elif rev == 0:
		S.append(A[2])
	else:
		S.appendleft(A[2])
if rev == 0:
	print(''.join(S))
else:
	S.reverse()
	print(''.join(S))


from collections import deque
s = deque(input())
q = int(input())
flag = 2
for i in range(q):
	query = input().split()
	if query[0] == '1':
		flag = 3 - flag
	elif int(query[1]) - flag:
		s.appendleft(query[2])
	else:
		s.append(query[2])
if flag - 2:
	s.reverse()
print(''.join(s))


from collections import deque
Q = deque(list(input()))
q = int(input())
cnt = 0
for i in range(q):
	x = input()
	if x[0] == '1':
		cnt += 1
		cnt %= 2
	elif x[0] == '2':
		if x[2] == '1':
			if cnt % 2 == 0:
				Q.appendleft(x[4])
			else:
				Q.append(x[4])
		elif cnt % 2 == 0:
			Q.append(x[4])
		else:
			Q.appendleft(x[4])
if cnt % 2 == 1:
	Q.reverse()
print(''.join(Q))


from collections import deque
Q = deque(list(input()))
rev = 0
for _ in range(int(input())):
	query = input()
	if query[0] == '1':
		rev ^= 1
	else:
		(q, f, c) = query.split()
		if (f == '1') ^ rev:
			Q.appendleft(c)
		else:
			Q.append(c)
if rev:
	Q = reversed(Q)
print(''.join(Q))


from sys import stdin
from collections import deque

def input():
	return stdin.readline().strip()
S = list(input())
q = deque(S)
is_flipped = False
for _ in range(int(input())):
	(q_t, *q_d) = input().split()
	if q_t == '1':
		is_flipped = not is_flipped
	else:
		(f, c) = q_d
		if not is_flipped and f == '1' or (is_flipped and f == '2'):
			q.appendleft(c)
		else:
			q.append(c)
ans = list(q)
print(''.join(ans[::-1])) if is_flipped else print(''.join(ans))


s = ['', input()]
q = int(input())
for _ in range(q):
	que = input().split()
	if que[0] == '1':
		s = s[::-1]
	else:
		s[int(que[1]) - 1] += que[2]
print(''.join(list(s[0])[::-1]) + s[1])


S = [c for c in input()]
N = int(input())
r = 0
B = []
for i in range(N):
	values = input().split()
	if values[0] == '1':
		r ^= 1
	else:
		f = int(values[1]) - 1
		if f == r:
			B.append(values[2])
		else:
			S.append(values[2])
if r == 0:
	print(''.join(B[::-1] + S))
else:
	print(''.join(S[::-1] + B))


from collections import deque
S = deque(input())
Q = int(input())
rev = 0
for i in range(Q):
	query = input()
	if query == '1':
		rev = 1 - rev
	elif int(query[2]) + rev == 2:
		S.append(query[4])
	else:
		S.appendleft(query[4])
if rev == 1:
	S.reverse()
print(''.join(S))


from sys import stdin as s
init = s.readline().rstrip()
state = 1
s1 = ''
s2 = ''
q = int(s.readline())
for i in range(q):
	t = s.readline().rstrip()
	if t == '1':
		state = (state + 1) % 2
	else:
		(a, b, c) = t.split()
		if int(b) + state == 2:
			s1 = c + s1
		else:
			s2 += c
ans = s1 + init + s2
if state:
	print(ans)
else:
	print(ans[::-1])


S = input()
Q = int(input())
b = ''
a = ''
f = 0
for i in range(Q):
	q = input()
	if q == '1':
		f += 1
	else:
		(_, F, C) = q.split()
		if (f + int(F)) % 2 == 1:
			b += C
		else:
			a += C
if f % 2 == 0:
	print(b[::-1] + S + a)
else:
	print(a[::-1] + S[::-1] + b)


from collections import deque
S = deque(list(input()))
sign = 1
for i in range(int(input())):
	ls = input().split()
	if ls[0] == '1':
		sign *= -1
	elif sign * int(ls[1]) == 1 or sign * int(ls[1]) == -2:
		S.appendleft(ls[2])
	else:
		S.append(ls[2])
if sign == -1:
	S.reverse()
print(*S, sep='')


import sys
input = sys.stdin.readline
S = input().strip()
Q = int(input())
f = 0
add = ['', '']
for q in range(Q):
	query = input().strip()
	if query == '1':
		f ^= 1
	else:
		(_, F, C) = query.split(' ')
		add[int(F) - 1 ^ f] += C
print(''.join([add[f][::-1], S[::f * -1 * 2 + 1], add[f * -1 + 1]]))


right = input()
flag = 1
left = ''
for i in range(int(input())):
	t = input()
	if t == '1':
		flag ^= 1
		continue
	(t, f, c) = t.split(' ')
	if not (f == '1') ^ flag:
		left += c
	else:
		right += c
ans = left[::-1] + right
if not flag:
	ans = ans[::-1]
print(ans)


s = input()
q = int(input())
f = 0
(l, r) = ('', '')
for i in range(q):
	a = list(input().split())
	if a[0] == '1':
		f += 1
	elif int(a[1]) - 1 ^ f % 2 == 0:
		l += a[2]
	else:
		r += a[2]
if f % 2 == 1:
	print(r[::-1] + s[::-1] + l)
else:
	print(l[::-1] + s + r)


from collections import deque
S = input()
S = deque(S)
Q = int(input())
flipped = False
for _ in range(Q):
	q = input().split()
	if q[0] == '1':
		flipped = not flipped
	elif (q[1] == '1') == flipped:
		S.append(q[2])
	else:
		S.appendleft(q[2])
print(''.join(reversed(S)) if flipped else ''.join(S))


from collections import deque
s = deque(input())
q = int(input())
odd = 0
for i in range(q):
	(t, *fc) = input().split()
	if t == '1':
		odd ^= 1
	elif odd ^ int(fc[0]) - 1:
		s.append(fc[1])
	else:
		s.appendleft(fc[1])
if odd:
	deque.reverse(s)
print(*s, sep='')


from collections import deque
S = input()
Q = int(input())
R = 1
Top = deque()
Top.append(S)
for _ in range(Q):
	inp = input()
	if inp[0] == '1':
		R *= -1
	else:
		(o, f, c) = inp.split()
		if f == '1' and R == -1 or (f == '2' and R == 1):
			Top.append(c)
		else:
			Top.appendleft(c)
S = ''.join(Top)
S = S if R == 1 else S[::-1]
print(S)


from collections import deque
import sys
input = sys.stdin.readline
Ss = input().rstrip()
Q = int(input())
isRev = False
queue = deque(Ss)
for _ in range(Q):
	ts = input().rstrip()
	if ts[0] == '1':
		isRev = not isRev
	else:
		(tp, f, c) = ts.split()
		if f == '1':
			if isRev:
				queue.append(c)
			else:
				queue.appendleft(c)
		elif isRev:
			queue.appendleft(c)
		else:
			queue.append(c)
anss = list(queue)
if isRev:
	anss.reverse()
print(''.join(anss))


S = input()
Q = int(input())
p = 0
u = 0
l = ''
r = ''
for i in range(Q):
	q = list(input().split())
	if q[0] == '1':
		u += 1
	elif q[1] == '1' and u % 2 == 0 or (q[1] == '2' and u % 2 == 1):
		l = l + q[2]
	else:
		r = r + q[2]
if u % 2 == 0:
	l = l[::-1]
	S = l + S + r
else:
	S = S[::-1]
	r = r[::-1]
	S = r + S + l
print(S)


s = input()
qn = int(input())
left = ''
right = s
for i in range(qn):
	q = input()
	if q[0] == '1':
		(left, right) = (right, left)
	else:
		(_, f, c) = q.split()
		if f == '1':
			left += c
		else:
			right += c
print(left[::-1] + right)


s = input()
q = int(input())
b = True
left = []
right = []
for _ in range(q):
	query = input()
	if query == '1':
		b = not b
	else:
		(t, f, c) = query.split()
		if int(f) + b == 2:
			left.append(c)
		else:
			right.append(c)
s = ''.join(left)[::-1] + s + ''.join(right)
print(s if b else s[::-1])


S = input()
A = ['', '']
Q = int(input())
b = 0
for _ in range(Q):
	query = input()
	if query[0] == '1':
		b ^= 1
	else:
		F = int(query[2])
		A[b ^ F - 1] += query[4]
ans = A[0][::-1] + S + A[1]
if b:
	ans = ans[::-1]
print(ans)


S = input()
Q = int(input())
T = 0
s = ''
for i in range(Q):
	q = list(input().split())
	if q[0] == '1':
		T = (T + 1) % 2
	elif T == 0 and q[1] == '1' or (T == 1 and q[1] == '2'):
		s += q[2]
	else:
		S += q[2]
if T == 0:
	S = s[::-1] + S
	print(S)
else:
	S = S[::-1] + s
	print(S)


import sys
pin = sys.stdin.readline
S = pin()[:-1]
Q = int(pin())
now = 1
ans1 = ''
ans2 = ''
for i in range(Q):
	D = pin().split()
	T = 0
	F = 0
	C = ''
	if len(D) == 1:
		T = int(D[0])
	else:
		T = int(D[0])
		F = int(D[1])
		C = D[2]
	if T == 1:
		now *= -1
	elif F == 1:
		if now == 1:
			ans1 = C + ans1
		else:
			ans2 = ans2 + C
	elif now == 1:
		ans2 = ans2 + C
	else:
		ans1 = C + ans1
if now == 1:
	print(ans1 + S + ans2)
else:
	print(ans2[::-1] + S[::-1] + ans1[::-1])


s = input()
q = int(input())
s1 = ''
s2 = ''
cnt = 1
for i in range(q):
	l1 = list(input().split())
	if len(l1) == 1:
		cnt *= -1
	elif cnt == 1 and l1[1] == '1' or (cnt == -1 and l1[1] == '2'):
		s1 = l1[2] + s1
	else:
		s2 = s2 + l1[2]
if cnt == 1:
	print(s1 + s + s2)
else:
	print(s2[::-1] + s[::-1] + s1[::-1])


S = input()
Q = int(input())
(pre, suf) = ('', '')
rev = False
for _ in range(Q):
	query = input().split()
	if query[0] == '1':
		rev ^= 1
	else:
		(f, c) = query[1:]
		if f == '1' and (not rev) or (f == '2' and rev):
			pre = c + pre
		else:
			suf = suf + c
print(pre + S + suf if not rev else (pre + S + suf)[::-1])


from collections import deque
s = deque(input())
q = int(input())
flag = 0
for _ in range(q):
	query = list(map(str, input().split()))
	if query[0] == '1':
		flag = 1 - flag
	elif query[1] == '1':
		if flag == 0:
			s.appendleft(query[2])
		else:
			s.append(query[2])
	elif flag == 0:
		s.append(query[2])
	else:
		s.appendleft(query[2])
if flag == 1:
	s.reverse()
print(*s, sep='')


s = input()
n = int(input())
t = ''
u = ''
bo = True
for _ in range(n):
	q = input().split()
	if q[0] == '1':
		(t, u) = (u, t)
		if bo == True:
			bo = False
		else:
			bo = True
	elif q[1] == '1':
		t += q[2]
	else:
		u += q[2]
t = t[::-1]
if bo == False:
	s = s[::-1]
print(t + s + u)


import math, string, itertools, fractions, heapq, collections, re, array, bisect, sys, random, time, copy
sys.setrecursionlimit(10 ** 7)
inf = 10 ** 20
mod = 10 ** 9 + 7
stdin = sys.stdin
ni = lambda : int(ns())
na = lambda : list(map(int, stdin.readline().split()))
ns = lambda : stdin.readline().rstrip()
ans = collections.deque(ns())
q = ni()
now = 0
for i in range(q):
	query = ns().split(' ')
	if query[0] == '1':
		now = 1 - now
	else:
		(f, c) = (query[1], query[2])
		if f == '1':
			if now == 1:
				ans.append(c)
			else:
				ans.appendleft(c)
		elif now != 1:
			ans.append(c)
		else:
			ans.appendleft(c)
if now == 1:
	for i in range(len(ans)):
		print(ans[-(i + 1)], end='')
else:
	for i in range(len(ans)):
		print(ans[i], end='')
print()


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
	elif not toright ^ (q[1] == '1'):
		Q[top - 1] = q[2]
		top -= 1
	else:
		Q[end + 1] = q[2]
		end += 1
ans = ''
for i in range(top, end + 1):
	ans += Q[i]
if not toright:
	ans = ans[::-1]
print(ans)


from collections import deque

def main():
	s = deque(input())
	q = int(input())
	flg = 0
	for i in range(q):
		l = list(map(str, input().split()))
		if len(l) == 1:
			flg = 1 - flg
		elif flg == 0 and l[1] == '1' or (flg == 1 and l[1] == '2'):
			s.appendleft(l[2])
		else:
			s.append(l[2])
	if flg == 0:
		print(''.join(s))
	else:
		print(''.join(reversed(s)))
main()


def main():
	S = list(input())
	T = []
	reverse = False
	n_q = int(input())
	for i in range(n_q):
		q = input().split(' ')
		if q[0] == '1':
			reverse = not reverse
		elif q[0] == '2':
			f = q[1]
			c = q[2]
			if f == '1' and (not reverse) or (f == '2' and reverse):
				T.append(c)
			elif f == '1' and reverse or (f == '2' and (not reverse)):
				S.append(c)
	if reverse:
		S.reverse()
		ans = S + T
	elif not reverse:
		T.reverse()
		ans = T + S
	print(''.join(ans))
main()


s = input()
q = int(input())
query = [input() for i in range(q)]
index = 0
str_list = [[], []]
for que in query:
	if len(que) == 1:
		index = not index
	else:
		(_, f, c) = que.split()
		str_list[index ^ int(f) - 1].append(c)
bf = ''.join(str_list[index][::-1])
af = ''.join(str_list[not index])
if index:
	s = s[::-1]
print(bf + s + af)


s = input()
n = int(input())
t = ''
for i in range(n):
	q = input().split()
	if len(q) == 1:
		(t, s) = (s, t)
	elif len(q) == 3:
		if q[1] == '1':
			t += q[2]
		elif q[1] == '2':
			s += q[2]
t = t[::-1]
print(t + s)


from collections import deque
S = deque(list(input()))
Q = int(input())
flag = 0
for _ in range(Q):
	q = input()
	if q[0] == '1':
		flag = 1 - flag
	else:
		(_, F, C) = q.split()
		if int(F) - flag == 1:
			S.appendleft(C)
		else:
			S.append(C)
if flag:
	S.reverse()
print(''.join(S))


def main():
	s = str(input())
	q = int(input())
	lst = [list(map(str, input().split())) for _ in range(q)]
	switch = 0
	str_lst = [s]
	front_lst = []
	for i in range(q):
		if lst[i][0] == '1':
			switch = 1 - switch
		else:
			f = lst[i][1]
			c = lst[i][2]
			if f == '1':
				if switch == 0:
					front_lst.append(c)
				else:
					str_lst.append(c)
			elif switch == 0:
				str_lst.append(c)
			else:
				front_lst.append(c)
	front = ''.join(front_lst)
	front = front[::-1]
	after = ''.join(str_lst)
	answer = front + after
	if switch == 1:
		answer = answer[::-1]
	print(answer)

def __starting_point():
	main()
__starting_point()


s = input()
n = int(input())
hanten = 0
s1 = ''
s2 = ''
for i in range(n):
	Q = input()
	if int(Q[0]) == 1:
		hanten += 1
	elif int(Q[0]) == 2:
		if hanten % 2 == int(Q[2]) - 1:
			s1 = s1 + Q[4]
		else:
			s2 = s2 + Q[4]
s1 = s1[::-1]
s = s1 + s + s2
if hanten % 2:
	s = s[::-1]
print(s)


S = input()
Q = int(input())
pre = ''
suf = ''
rev = 0
for i in range(Q):
	inp = input()
	if inp == '1':
		(pre, suf) = (suf, pre)
		rev += 1
	else:
		inp_l = inp.split()
		if inp_l[1] == '1':
			pre = pre + inp_l[2]
		else:
			suf = suf + inp_l[2]
if rev % 2 == 0:
	print(''.join(list(reversed(pre))) + S + suf)
else:
	print(''.join(list(reversed(pre))) + ''.join(list(reversed(S))) + suf)


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])


s = input()
n = int(input())
a = [input() for _ in range(n)]
front = True
f = ''
b = ''
for q in a:
	if q == '1':
		front = not front
		continue
	l = q.split()
	if l[1] == '1':
		if front:
			f = l[2] + f
		else:
			b = b + l[2]
	if l[1] == '2':
		if front:
			b = b + l[2]
		else:
			f = l[2] + f
print(f + s + b if front else (f + s + b)[::-1])


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


from collections import deque
s = input()
a = deque()
for i in s:
	a.append(i)
q = int(input())
rev = 0
for i in range(q):
	s = input()
	if s[0] == '1':
		rev ^= 1
	else:
		b = int(s[2]) - 1 ^ rev
		if b:
			a.append(s[4])
		else:
			a.appendleft(s[4])
if rev:
	a.reverse()
for i in a:
	print(i, end='')


(S, Q) = (input(), int(input()))
(l, r) = ('', '')
reverse = 0
for i in range(Q):
	T = input().split()
	if T[0] == '1':
		reverse = 0 if reverse else 1
	elif T[1] == '1':
		if reverse:
			r = r + T[2]
		else:
			l = T[2] + l
	elif reverse:
		l = T[2] + l
	else:
		r = r + T[2]
if reverse:
	s = list(l + S + r)
	s.reverse()
	print(''.join(s))
else:
	print(l + S + r)


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 == 1:
			S.appendleft(b[1])
		else:
			S.append(b[1])
D = list(S)
print(''.join(D) if f % 2 == 0 else ''.join(D[::-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))


from collections import deque
s = list(input())
q = int(input())
t = [list(map(str, input().split())) for _ in range(q)]
d = deque(s)
cnt = 0
for i in range(q):
	if len(t[i]) == 1:
		cnt += 1
	elif (int(t[i][1]) + cnt) % 2 != 0:
		d.appendleft(t[i][2])
	else:
		d.append(t[i][2])
if cnt % 2 != 0:
	d = list(d)[::-1]
print(''.join(d))


from collections import deque
s = deque(input())
q = int(input())
rev = False
for _ in range(q):
	que = input()
	if que == '1':
		rev = not rev
	else:
		(f, c) = que[2:].split()
		if (f == '1') ^ rev:
			s.appendleft(c)
		else:
			s.append(c)
if rev:
	s.reverse()
print(''.join(s))


from collections import deque
s = deque(list(input()))
n = int(input())
normal = 0
for i in range(n):
	li = input().split()
	if int(li[0]) == 1:
		normal ^= 1
	else:
		k = int(li[1]) - 1
		k ^= normal
		if k == 1:
			s.append(li[2])
		else:
			s.appendleft(li[2])
print(''.join(s) if normal == 0 else ''.join(list(s)[::-1]))


S = input()
Q = int(input())
t = ''
for i in range(Q):
	instructions = list(map(str, input().split()))
	if instructions[0] == '1':
		(S, t) = (t, S)
	elif instructions[1] == '1':
		t += instructions[2]
	else:
		S += instructions[2]
print(t[::-1] + S)


(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())
seq1 = ''
seq2 = ''
flg = 1
for i in range(q):
	ii = input().split()
	if ii[0] == '1':
		(seq1, seq2) = (seq2, seq1)
		flg *= -1
	elif ii[1] == '1':
		seq1 += ii[2]
	else:
		seq2 += ii[2]
print(seq1[::-1] + s[::flg] + seq2)


from collections import deque
s = list(input())
q = int(input())
d = deque()
for i in s:
	d.append(i)
c = 1
for _ in range(q):
	t = list(input().split())
	if t[0] == '1':
		c *= -1
	elif t[1] == '1':
		if c == 1:
			d.appendleft(t[2])
		else:
			d.append(t[2])
	elif c == 1:
		d.append(t[2])
	else:
		d.appendleft(t[2])
print(''.join(list(d)[::c]))


from collections import deque
S = deque(input())
Q = int(input())
d = 0
for i in range(Q):
	I = list(input().split())
	if I[0] == '1':
		d ^= 1
	elif d + int(I[1]) == 2:
		S.append(I[2])
	else:
		S.appendleft(I[2])
if d == 1:
	S.reverse()
for s in S:
	print(s, end='')
print()


from collections import deque
S = deque(input())
Q = int(input())
s = 1
for i in range(Q):
	A = list(map(str, input().split()))
	if A[0] == '1':
		s *= -1
		continue
	if A[1] == '1' and s == 1:
		S.appendleft(A[2])
	elif A[1] == '2' and s == -1:
		S.appendleft(A[2])
	else:
		S.append(A[2])
if s == -1:
	S.reverse()
print(''.join(S))


S = input()
Q = int(input())
query = [input().split() for _ in range(Q)]
switch = 1
top = []
bottom = []
for i in range(Q):
	if query[i][0] == '1':
		switch *= -1
	else:
		f = query[i][1]
		c = query[i][2]
		if switch == 1:
			if f == '1':
				top.append(c)
			else:
				bottom.append(c)
		elif f == '1':
			bottom.append(c)
		else:
			top.append(c)
if switch == 1:
	ans = top[::-1] + list(S) + bottom
else:
	ans = bottom[::-1] + list(S)[::-1] + top
print(''.join(ans))


from collections import deque
s = deque(input())
q = int(input())
turn = True
for i in range(q):
	qq = input().split()
	if qq[0] == '1':
		turn = not turn
	elif turn == bool(int(qq[1]) - 2):
		s.appendleft(qq[2])
	else:
		s.append(qq[2])
text = ''.join(s)
print(text if turn else text[::-1])


s = input()
p = int(input())
dir = 0
mae = []
usi = []
for _ in range(p):
	Q = input().split()
	if Q[0] == '1':
		dir = not dir
	elif int(Q[1]) - 1 == dir:
		mae.append(Q[2])
	else:
		usi.append(Q[2])
ans = ''.join(mae[::-1]) + s + ''.join(usi)
if not dir:
	print(ans)
else:
	print(ans[::-1])


t = list(input())
q = int(input())
s = []
for i in range(q):
	query = list(input().split())
	if query[0] == '1':
		(s, t) = (t, s)
	elif query[1] == '1':
		s.extend(query[2])
	else:
		t.extend(query[2])
if s == []:
	s = t
else:
	s.reverse()
	s.extend(t)
moji = ''.join(s)
print(moji)


def solve():
	S = input()
	q = int(input())
	flg = 0
	li = ['', '']
	for _ in range(q):
		Q = input().split()
		if Q[0] == '1':
			flg = 1 - flg
		else:
			li[(int(Q[1]) - 1 + flg) % 2] += Q[2]
	if flg:
		S = S[::-1]
	print(li[flg][::-1] + S + li[1 - flg])
solve()


I = input
s = I()
q = int(I())
a = ([], [])
i = 0
for _ in range(q):
	x = I()
	if x[0] == '1':
		i ^= 1
	else:
		f = int(x[2]) - 1
		a[i ^ f].append(x[4])
r = ''.join(reversed(a[0])) + s + ''.join(a[1])
print(r[::-1] if i else r)


S = input()
Q = int(input())
rev = False
ends = [[], []]
for _ in range(Q):
	parts = input().split()
	if parts[0] == '1':
		rev = not rev
	else:
		(_, end, s) = parts
		ends[int(end) - 1 ^ rev].append(s)
res = list(reversed(ends[0])) + list(S) + ends[1]
if rev:
	res = reversed(res)
print(''.join(res))


from collections import deque
s = deque(input())
q = int(input())
flag = True
for _ in range(q):
	temp = input().split()
	if len(temp) == 1:
		flag = not flag
	elif flag:
		if temp[1] == '1':
			s.appendleft(temp[2])
		else:
			s.append(temp[2])
	elif temp[1] == '1':
		s.append(temp[2])
	else:
		s.appendleft(temp[2])
if flag:
	print(''.join(s))
else:
	print(''.join(list(s)[::-1]))


s = input()
n = int(input())
front = ''
back = s
reverse = False
for i in range(n):
	t = input().split()
	if t[0] == '1':
		reverse = not reverse
	elif (t[1] == '1') ^ reverse:
		front = front + t[2]
	else:
		back = back + t[2]
if reverse:
	print(back[::-1] + front)
else:
	print(front[::-1] + back)


from collections import deque
s = deque(input())
n = int(input())
status = True
for i in range(n):
	com = input().split()
	if com[0] == '1':
		status = not status
	elif int(com[1]) + status == 2:
		s.appendleft(com[2])
	else:
		s.append(com[2])
if not status:
	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
	elif 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)


s = input()
q = int(input())
inv = False
fro = ''
end = ''
for _ in range(q):
	query = list(map(str, input().split()))
	if query[0] == '1':
		inv = not inv
	else:
		t = int(query[1])
		instr = query[2]
		if not inv:
			if t == 1:
				fro = instr + fro
			else:
				end = end + instr
		elif t == 1:
			end = end + instr[::-1]
		else:
			fro = instr[::-1] + fro
ans = fro + s + end
if not inv:
	print(ans)
else:
	print(ans[::-1])


import sys
S = input()
A = ''
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':
			A = A + i[4]
		else:
			S = S + i[4]
	elif i[2] == '2':
		A = A + i[4]
	else:
		S = S + i[4]
ans = A[::-1] + S
print(ans[::key])


s = input()
n = int(input())
head = ''
tail = s
for _ in range(n):
	q = input()
	if q[0] == '1':
		(head, tail) = (tail, head)
	else:
		(_, f, c) = q.split()
		if f == '1':
			head += c
		else:
			tail += c
print(head[::-1] + tail)


s = input()
q = int(input())
r = 0
S = ['', '']
for _ in range(q):
	d = input().split()
	if d[0] == '1':
		r ^= 1
	else:
		S[(int(d[1]) + r) % 2] += d[2]
s = S[1][::-1] + s + S[0]
if r:
	s = s[::-1]
print(s)


from collections import deque
(S, inv) = (deque(input()), False)
Q = int(input())
for _ in range(Q):
	q = input().split()
	if len(q) == 1:
		inv = not inv
		continue
	(F, C) = (q[1], q[2])
	if (F == '1') ^ inv:
		S.appendleft(C)
	else:
		S.append(C)
print(''.join(S)[::-1] if inv else ''.join(S))


(s, _, *q) = open(0)
a = ['', '']
p = 1
for q in q:
	p ^= q < '2'
	a[(q < '2 2') ^ p] += q[-2] * (q > '2')
print(a[p ^ 1][::-1] + s[:-1][::p or -1] + a[p])


S = input()
Q = int(input())
a = []
b = []
rev = 0
for _ in range(Q):
	q = input().split()
	if q[0] == '1':
		(a, b) = (b, a)
		rev += 1
	elif q[1] == '1':
		a.append(q[2])
	else:
		b.append(q[2])
if rev % 2 == 1:
	S = S[::-1]
ans = ''.join(a[::-1]) + S + ''.join(b)
print(ans)


S = input()
Q = int(input())
cnt_re = 0
s = ''
for i in range(Q):
	q = list(input().split())
	if q[0] == '1':
		cnt_re += 1
	elif (int(q[1]) + cnt_re) % 2 == 1:
		s = s + q[2]
	else:
		S = S + q[2]
if cnt_re % 2 == 1:
	print(S[::-1] + s)
else:
	print(s[::-1] + S)


from collections import deque
S = deque(list(input()))
q = int(input())
flag = False
for i in range(q):
	query = list(map(str, input().split()))
	if query[0] == '1' and flag == False:
		flag = True
	elif query[0] == '1' and flag == True:
		flag = False
	elif flag == False and query[1] == '1' or (flag == True and query[1] == '2'):
		S.appendleft(query[2])
	else:
		S.append(query[2])
if flag:
	S = list(S)[::-1]
else:
	S = list(S)
print(''.join(map(str, list(S))))


from collections import deque
a = list(input())
a = deque(a)
re = 0
for i in range(int(input())):
	b = input().split()
	if b[0] == '1':
		re = (re + 1) % 2
	elif re and b[1] == '2' or (not re and b[1] == '1'):
		a.appendleft(b[2])
	else:
		a.append(b[2])
a = list(a)
if re:
	a = a[::-1]
print(''.join(a))


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
	elif (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())
head = ''
tail = ''
rev = False
for _ in range(Q):
	q = input().split()
	if len(q) == 1:
		rev = not rev
	elif rev ^ (q[1] == '1'):
		head += q[2]
	else:
		tail += q[2]
if rev:
	print(tail[::-1] + S[::-1] + head)
else:
	print(head[::-1] + S + tail)


s = input()
q = int(input())
cnt = 0
(a1, a2) = ([], [])
for _ in range(q):
	t = list(input().split())
	if t[0] == '1':
		cnt += 1
		(a1, a2) = (a2, a1)
	elif t[1] == '1':
		a1.append(t[2])
	else:
		a2.append(t[2])
if cnt % 2 == 1:
	s = s[::-1]
print(''.join(a1[::-1]) + s + ''.join(a2))


S = input()
Q = int(input())
reverse = 0
back = ''
front = ''
for i in range(Q):
	T = input()
	if T[0] == '1':
		reverse = 1 - reverse
	else:
		(q, F, C) = T.split()
		F = int(F)
		if F - reverse == 1:
			front = C + front
		else:
			back += C
S = front + S + back
if reverse == 0:
	print(S)
else:
	S = S[::-1]
	print(S)


S = input()
b = 1
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:
			x = c + x
		else:
			y = y + c
S = x + S + y
print(S if b else S[::-1])


s = input()
q = int(input())
head = ''
tail = ''
rev = False
for _ in range(q):
	ql = list(input().split())
	if ql[0] == '1':
		(head, tail) = (tail, head)
		if rev:
			rev = False
		else:
			rev = True
	elif ql[1] == '1':
		head += ql[2]
	else:
		tail += ql[2]
if rev:
	ans = head[::-1] + s[::-1] + tail
else:
	ans = head[::-1] + s + tail
print(ans)


import sys
S = input()
Q = int(input())
rev = 0
front = []
end = []
for q in range(Q):
	query = input().split()
	if query[0] == '1':
		rev = 1 - rev
	elif (rev == 0) & (query[1] == '1') | (rev == 1) & (query[1] == '2'):
		front.append(query[2])
	else:
		end.append(query[2])
front = ''.join(front)
end = ''.join(end)
if rev:
	print(end[::-1] + S[::-1] + front)
else:
	print(front[::-1] + S + end)


from collections import deque
S = deque()
S.extend(list(input()))
F = True
for _ in range(int(input())):
	(p, *q) = input().split()
	if p == '1':
		F = not F
	else:
		c = q[1]
		if q[0] == '1' and F or (q[0] == '2' and (not F)):
			S.appendleft(c)
		else:
			S.append(c)
print(''.join(list(S) if F else list(S)[::-1]))


from collections import deque
s = deque(list(input()))
q = int(input())
top = 0
for i in range(q):
	t = input()
	if t[0] == '1':
		top = 1 - top
		continue
	t = t.split()
	if t[1] == '1':
		if top == 0:
			s.appendleft(t[2])
		else:
			s.append(t[2])
	elif top == 0:
		s.append(t[2])
	else:
		s.appendleft(t[2])
if top == 0:
	for i in range(len(s)):
		print(s[i], end='')
else:
	for i in range(len(s) - 1, -1, -1):
		print(s[i], end='')


from collections import deque
S = list(input())
Q = int(input())
answer = deque(S)
reverse = False
for _ in range(Q):
	query = input()
	if query == '1':
		reverse ^= True
	else:
		(ope, types, alphabet) = query.split()
		if types == '1':
			if reverse:
				answer.append(alphabet)
			else:
				answer.appendleft(alphabet)
		elif reverse:
			answer.appendleft(alphabet)
		else:
			answer.append(alphabet)
if reverse:
	result = ''
	for i in range(len(answer)):
		result += answer.pop()
	print(result)
else:
	result = ''
	for i in range(len(answer)):
		result += answer.popleft()
	print(result)


S = list(input())
Q = int(input())
A = []
a = 0
for i in range(Q):
	q = list(input().split())
	if q[0] == '1':
		a += 1
	elif q[1] == '1' and a % 2 == 0 or (q[1] == '2' and a % 2 == 1):
		A.append(q[2])
	else:
		S.append(q[2])
A = A[::-1]
S = A + S
if a % 2 == 1:
	S = S[::-1]
ans = ''.join(S)
print(ans)


(s, _, *q) = open(0)
a = ['', s[:-1]]
p = 1
for q in q:
	if q > '2':
		a[(q < '2 2') ^ p] += q[-2]
	else:
		p ^= 1
print(a[p ^ 1][::-1] + a[p])


A = input()
B = int(input())
C = ''
for i in range(B):
	D = input().split()
	if D[0] == '1':
		(C, A) = (A, C)
	elif D[1] == '1':
		C += D[2]
	else:
		A += D[2]
C = C[::-1]
print(C + A)


S = input()
Q = int(input())
flg = True
(T, U) = ('', '')
for i in range(Q):
	q = list(input().split())
	if len(q) == 1:
		flg = not flg
	elif flg:
		if q[1] == '1':
			T = q[2] + T
		else:
			U = U + q[2]
	elif q[1] == '1':
		U = U + q[2]
	else:
		T = q[2] + T
S = T + S + U
if not flg:
	S = S[::-1]
print(S)


S = input()
N = int(input())
flg = 0
L = []
for i in range(N):
	e = input().split()
	if e[0] == '1':
		flg += 1
		flg = flg % 2
	else:
		L.append(((flg + int(e[1])) % 2, e[2]))
S = ''.join(list(reversed(S))) if flg else S
(s1, s2) = ('', '')
for (f, v) in L:
	if (flg + f) % 2 == 1:
		s1 += v
	else:
		s2 += v
ans = ''.join(list(reversed(s1))) + S + s2
print(ans)


S = input()
n = int(input())
t = 0
L = ''
R = ''
for i in range(n):
	Q = list(input().split())
	if Q[0] == '1':
		if t == 0:
			t = 1
		else:
			t = 0
	elif int(Q[1]) + t == 2:
		R = R + Q[2]
	else:
		L = Q[2] + L
ans = L + S + R
if t == 0:
	print(ans)
else:
	print(ans[::-1])


from collections import deque
S = deque()
f = input
S.extend(f())
F = 1
for _ in range(int(f())):
	q = input()
	if q[0] == '1':
		F = 1 - F
	elif int(q[2]) + F == 2:
		S.appendleft(q[4])
	else:
		S.append(q[4])
print(''.join([list(S)[::-1], S][F]))


s = input()
n = int(input())
d = 1
left = ''
right = ''
for i in range(n):
	q = input()
	if q == '1':
		d *= -1
	else:
		(_, f, c) = q.split()
		if d == 1:
			if f == '1':
				left += c
			else:
				right += c
		elif f == '2':
			left += c
		else:
			right += c
ans = left[::-1] + s + right
print(ans[::d])


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])


s = input()
q = int(input())
cnt = 0
from collections import deque
s = deque(s)
for i in range(q):
	j = tuple(input().split())
	if len(j) == 3:
		if (cnt + int(j[1])) % 2:
			s.appendleft(j[2])
		else:
			s.append(j[2])
	else:
		cnt += 1
if cnt % 2 == 0:
	print(''.join(s))
else:
	print(''.join(reversed(list(s))))


S = input()
Q = int(input())
nT = 0
s1 = []
s2 = []
for i in range(Q):
	Query = input()
	if Query == '1':
		nT += 1
		continue
	(T, F, C) = Query.split()
	if (F == '1') ^ (nT % 2 == 1):
		s1.append(C)
	else:
		s2.append(C)
r = ''.join(reversed(s1)) + S + ''.join(s2)
if nT % 2 == 1:
	r = ''.join(reversed(r))
print(r)


S = input()
Q = int(input())
Query = [input() for _ in range(Q)]
inv = False
add = ['', '']
for query in Query:
	if query[0] == '1':
		inv = not inv
	else:
		add[(query[2] == '1') ^ inv] += query[-1]
ans = add[1][::-1] + S + add[0]
if inv:
	ans = ans[::-1]
print(ans)


import sys
s = input()
q = int(input())
a = []
b = []
c = 0
for i in sys.stdin:
	if '1' in i.split()[0]:
		(a, b) = (b, a)
		c += 1
	elif '1' in i.split()[1]:
		a.append(i.split()[2])
	else:
		b.append(i.split()[2])
if c % 2 == 1:
	s = ''.join(list(s)[::-1])
print(''.join(a[::-1]) + s + ''.join(b))


S = input()
f = ''
b = ''
rev = False
for i in range(int(input())):
	t = input().split()
	if t[0] == '1':
		rev ^= True
		tmp = f
		f = b
		b = tmp
	elif t[1] == '1':
		f += t[2]
	else:
		b += t[2]
if rev:
	S = S[::-1]
f = f[::-1]
print(f + S + b)


from collections import deque
s = deque(list(input()))
q = int(input())
f = 0
for _ in range(q):
	l = input()
	if l == '1':
		f ^= 1
	else:
		(a, b, c) = l.split()
		if f ^ (b == '1'):
			s.appendleft(c)
		else:
			s.append(c)
if f:
	ans = list(s)[::-1]
else:
	ans = list(s)
print(*ans, sep='')


S = input()
Q = int(input())
reverse = False
left = ''
right = ''
for _ in range(Q):
	q = input().split()
	if q[0] == '1':
		reverse = not reverse
	else:
		F = q[1]
		C = q[2]
		if reverse == False:
			if F == '1':
				left = C + left
			else:
				right += C
		elif F == '1':
			right += C
		else:
			left = C + left
S = left + S + right
if reverse == False:
	print(S)
	return
else:
	print(S[::-1])
	return


S = input()
Q = int(input())
bs = ''
fs = ''
hanten = 0
for i in range(Q):
	query = list(input().split())
	if query[0] == '1':
		hanten += 1
	elif query[1] == '1':
		if hanten % 2 == 0:
			fs += query[2]
		else:
			bs += query[2]
	elif hanten % 2 == 0:
		bs += query[2]
	else:
		fs += query[2]
if hanten % 2 == 0:
	print(fs[::-1] + S + bs)
else:
	print(bs[::-1] + S[::-1] + fs)


from collections import deque
que = deque(input())
q = int(input())
rev = False
for i in range(q):
	t = input()
	if t == '1':
		rev = not rev
	else:
		(tt, f, c) = t.split()
		if rev == False and f == '1' or (rev and f == '2'):
			que.appendleft(c)
		else:
			que.append(c)
if rev:
	que.reverse()
print(''.join(que))


import os, sys, re, math
S = input()
Q = int(input())
direction = 1
prefix = ''
suffix = ''
for i in range(Q):
	query = input().split()
	if int(query[0]) == 1:
		direction = 2 if direction == 1 else 1
	elif int(query[1]) ^ direction == 0:
		prefix = query[2] + prefix
	else:
		suffix = suffix + query[2]
S = prefix + S + suffix
if direction == 1:
	print(S)
else:
	print(S[::-1])


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


from collections import deque
S = deque(input())
Q = int(input())
s = 1
for _ in range(Q):
	q = input()
	if q != '1':
		if q[2] == '1' and s == 1 or (q[2] != '1' and s != 1):
			S.extendleft(q[4])
		else:
			S.extend(q[4])
	else:
		s *= -1
S = ''.join(list(S))
if s == 1:
	print(S)
else:
	print(S[::-1])


from collections import deque
deq = deque(input())
rev = False
for _ in range(int(input())):
	query = input().split()
	if query[0] == '1':
		rev = not rev
	elif (query[1] == '1') ^ rev:
		deq.appendleft(query[2])
	else:
		deq.append(query[2])
if rev:
	deq.reverse()
print(''.join(deq))


S = input()
Q = int(input())
upside_down = False
front = ''
for i in range(Q):
	l = list(map(str, input().split()))
	if l[0] == '1':
		if upside_down == False:
			upside_down = True
		else:
			upside_down = False
	else:
		(F, C) = (l[1], l[2])
		if upside_down:
			if F == '1':
				S = S + C
			else:
				front += C
		elif F == '1':
			front += C
		else:
			S = S + C
front = front[::-1]
S = front + S
if upside_down:
	S = S[::-1]
print(S)


s = input()
q = int(input())
(a, b) = ('', '')
cnt = 0
for _ in range(q):
	que = input().split()
	if que[0] == '1':
		cnt += 1
	elif (cnt + int(que[1])) % 2 == 0:
		a += que[2]
	else:
		b += que[2]
if cnt % 2 == 1:
	s = a[-1::-1] + s[-1::-1] + b
else:
	s = b[-1::-1] + s + a
print(s)


from collections import deque
s = deque(input().split())
f = False
for i in range(int(input())):
	_ = input().split()
	if _[0] == '1':
		f = not f
	elif _[1] == '1':
		if f:
			s.append(_[2])
		else:
			s.appendleft(_[2])
	elif f:
		s.appendleft(_[2])
	else:
		s.append(_[2])
print(''.join(s)[::-1] if f else ''.join(s))


from collections import deque
S = deque(input())
n = int(input())
Q = [list(map(str, input().split())) for _ in range(n)]
flg = True
for q in Q:
	if q[0] == '1':
		flg = not flg
	else:
		F = int(q[1])
		if not flg:
			F = 3 - F
		if F == 1:
			S.appendleft(q[2])
		if F == 2:
			S.append(q[2])
if not flg:
	S.reverse()
print(''.join(S))


from collections import deque
S = deque(input())
q = int(input())
rev = False
for _ in range(q):
	inp = input()
	if inp == '1':
		rev = not rev
	else:
		(t, f, c) = inp.split()
		if rev and f == '1' or (not rev and f == '2'):
			S.append(c)
		else:
			S.appendleft(c)
if rev:
	S.reverse()
print(''.join(S))


s = input()
q = int(input())
flg = 1
(mae, ushiro) = ('', '')
for i in range(q):
	inp = input()
	if inp == '1':
		flg *= -1
	else:
		(f, c) = (inp[2], inp[4])
		if f == '1' and flg == 1:
			mae += c
		elif f == '2' and flg == -1:
			mae += c
		else:
			ushiro += c
s = mae[::-1] + s + ushiro
if flg == -1:
	s = s[::-1]
print(s)


from collections import deque
s = deque(input())
q = int(input())
flag = 0
for i in range(q):
	query = input()
	if len(query) == 1:
		flag += 1
	elif query[2] == '1' and flag % 2 == 0 or (query[2] == '2' and flag % 2 == 1):
		s.appendleft(query[4])
	else:
		s.append(query[4])
if flag % 2 == 1:
	s.reverse()
print(''.join(s))


from collections import deque
s = deque(input())
q = int(input())
rev = False
for _ in range(q):
	ls = list(input().split())
	if ls[0] == '1':
		rev = not rev
	elif rev ^ (ls[1] == '1'):
		s.appendleft(ls[2])
	else:
		s.append(ls[2])
if rev:
	s.reverse()
print(''.join(s))


s = input()
q = int(input())
cnt = 0
front = []
rear = []
for qq in range(q):
	l = list(input().split())
	if l[0] == '1':
		cnt += 1
	elif (cnt + int(l[1])) % 2 == 1:
		front.append(l[2])
	else:
		rear.append(l[2])
front = ''.join(front[::-1])
rear = ''.join(rear)
s = front + s + rear
print(s if cnt % 2 == 0 else s[::-1])


import sys
S = input()
Q = int(input())
pin = 0
left = ''
right = ''
for i in range(Q):
	t = list(sys.stdin.readline().split())
	if int(t[0]) == 1:
		pin += 1
	elif int(t[1]) % 2 == pin % 2:
		right = right + t[2]
	else:
		left = t[2] + left
S = left + S + right
if pin % 2 == 1:
	S = S[::-1]
print(S)


from collections import deque
s = deque(input())
q = int(input())
flag = 1
for i in range(q):
	st = input().split()
	if int(st[0]) == 1:
		flag *= -1
	else:
		tmp = int(st[1]) * flag
		if tmp == 1 or tmp == -2:
			s.appendleft(st[2])
		else:
			s.append(st[2])
if flag == -1:
	s.reverse()
print(''.join(s))


S = input()
f = ''
N = int(input())
direct = 0
for i in range(N):
	X = input().split()
	if len(X) == 1:
		direct = 1 - direct
	elif X[1] == '1' and direct == 0 or (X[1] == '2' and direct == 1):
		f += X[2]
	else:
		S += X[2]
print(f[::-1] + S) if direct == 0 else print(S[::-1] + f)


S = list(input())
q = int(input())
x = 0
appends = [[] for i in range(2)]
for i in range(q):
	s = list(input().split())
	if int(s[0]) == 1:
		x += 1
	else:
		appends[(x + int(s[1]) + 1) % 2].append(s[2])
if x % 2 == 1:
	S = S[::-1]
S = appends[x % 2][::-1] + S + appends[(x + 1) % 2]
for k in S:
	print(k, end='')


from collections import deque
s = deque(input())
q = int(input())
rev = False
for i in range(q):
	query = input().split()
	if query[0] == '1':
		rev = not rev
	elif query[1] == '1':
		if rev:
			s.append(query[2])
		else:
			s.appendleft(query[2])
	elif query[1] == '2':
		if rev:
			s.appendleft(query[2])
		else:
			s.append(query[2])
s = list(s)
if rev:
	s = s[::-1]
print(*s, sep='')


s = input()
n = int(input())
f = 0
l = ''
r = ''
for i in range(n):
	q = input().split()
	if q[0] == '1':
		f ^= 1
	elif f == 0 and q[1] == '1' or (f == 1 and q[1] == '2'):
		l = l + q[2]
	else:
		r = r + q[2]
if f == 0:
	ans = l[::-1] + s + r
else:
	ans = r[::-1] + s[::-1] + l
print(ans)


from collections import deque
s = deque(input())
q = int(input())
flg = 0
for _ in range(q):
	Q = [x for x in input().split()]
	if Q[0] == '1':
		flg += 1
	elif Q[1] == '1' and flg % 2 == 0 or (Q[1] == '2' and flg % 2 == 1):
		s.appendleft(Q[2])
	else:
		s.append(Q[2])
if flg % 2 == 1:
	s.reverse()
print(''.join(s))


S = input()
Q = int(input())
(F, B) = ('', '')
k = 1
for i in range(Q):
	q = list(input().split())
	if int(q[0]) == 1:
		k = abs(k - 1)
	elif int(q[1]) == 1:
		if k:
			F += q[2]
		else:
			B += q[2]
	elif k:
		B += q[2]
	else:
		F += q[2]
if k:
	print(F[::-1] + S + B)
else:
	print(B[::-1] + S[::-1] + F)


from collections import deque
d = deque(input())
q = int(input())
ans = 1
for i in range(q):
	a = input()
	if int(a[0]) == 1:
		ans = ans * -1
	elif ans == 1:
		if int(a[2]) == 1:
			d.appendleft(a[4])
		else:
			d.append(a[4])
	elif int(a[2]) == 1:
		d.append(a[4])
	else:
		d.appendleft(a[4])
if ans == 1:
	for i in range(len(d)):
		print(d[i], end='')
else:
	d.reverse()
	for i in range(len(d)):
		print(d[i], end='')


from collections import deque
S = input()
Q = int(input())
Queries = [input().split() for i in range(Q)]
dq = deque(S)
f = 0
for a in Queries:
	if a[0] == '1':
		f ^= 1
	elif f == int(a[1]) - 1:
		dq.appendleft(a[2])
	else:
		dq.append(a[2])
if f:
	dq.reverse()
print(''.join(dq))


s = input()
q = int(input())
order = 1
header = ''
footer = ''
for _ in range(q):
	tmp = input()
	if tmp == '1':
		order *= -1
	else:
		(t, f, c) = tmp.split()
		if int(f) * order % 3 == 1:
			header = c + header
		else:
			footer = footer + c
s = header + s + footer
print(s) if order == 1 else print(s[::-1])


from collections import deque
S = deque(input())
Q = int(input())
reverse = False
for i in range(Q):
	t = input().split()
	if int(t[0]) == 1:
		if reverse == False:
			reverse = True
		else:
			reverse = False
	else:
		F = int(t[1])
		C = t[2]
		if F == 1:
			if reverse == False:
				S.appendleft(C)
			else:
				S.append(C)
		elif reverse == False:
			S.append(C)
		else:
			S.appendleft(C)
if reverse == False:
	print(''.join(S))
else:
	print(''.join(reversed(S)))


s = input()
q = int(input())
rev = 1
sf = ''
sb = s
for _ in range(0, q):
	n = input().split()
	if n[0] == '1':
		rev *= -1
	elif n[1] == '1' and rev == 1 or (n[1] == '2' and rev == -1):
		sf += n[2]
	else:
		sb += n[2]
s = sf[::-1] + sb
if rev == 1:
	print(s)
else:
	print(s[::-1])


from collections import deque
s = deque(list(input()))
q = int(input())
count = 0
for i in range(q):
	query = input()
	if query == '1':
		count += 1
	elif query[2] == '1':
		if count % 2 == 0:
			s.appendleft(query[-1])
		else:
			s.append(query[-1])
	elif count % 2 == 0:
		s.append(query[-1])
	else:
		s.appendleft(query[-1])
if count % 2 == 1:
	s.reverse()
print(*s, sep='')


from collections import deque
S = deque(input())
Q = int(input())
t = True
for i in range(Q):
	query = input().split()
	if query[0] == '1':
		t = not t
	elif t and query[1] == '1' or (not t and query[1] == '2'):
		S.appendleft(query[2])
	else:
		S.append(query[2])
S = ''.join(S)
if not t:
	S = S[::-1]
print(S)


s = input()
q = int(input())
Q = [list(map(str, input().split())) for _ in range(q)]
t = ''
for qi in Q:
	if int(qi[0]) == 1:
		(s, t) = (t, s)
	elif int(qi[1]) == 1:
		t += qi[2]
	else:
		s += qi[2]
t = t[::-1]
t += s
print(t)


s = input()
q = int(input())
inv = 1
sfb = [0, '', '']
for i in range(q):
	ls = list(input().split())
	if ls[0] == '1':
		inv *= -1
	else:
		(f, c) = ls[1:]
		if f == '1':
			sfb[inv] += c
		else:
			sfb[-inv] += c
ans = sfb[1][::-1] + s + sfb[-1]
print(ans[::inv])


s = input()
a = []
b = []
d = 0
for i in range(int(input())):
	q = input()
	if q == '1':
		d ^= 1
	else:
		(_, f, c) = q.split()
		f = int(f) - 1
		if d == 0:
			if f == 0:
				a.append(c)
			else:
				b.append(c)
		elif f == 1:
			a.append(c)
		else:
			b.append(c)
if d == 0:
	print(''.join(a)[::-1] + s + ''.join(b))
else:
	print(''.join(b)[::-1] + s[::-1] + ''.join(a))


from collections import deque as D
d = D()
s = input()
q = int(input())
ans = 0
d.extend(list(s))
for i in range(q):
	k = list(input().split())
	if len(k) == 1:
		ans = (ans + 1) % 2
	else:
		(x, y, z) = k
		y = (int(y) - 1 + ans) % 2
		d.append(z) if y else d.appendleft(z)
print(''.join(list(d)[::(-1) ** ans]))


B = input()
order = True
F = ''
for q in range(int(input())):
	T = input().rstrip().split()
	if T[0] == '1':
		order ^= True
	else:
		(f, c) = (T[1], T[2])
		if not (f == '1') ^ order:
			F += c
		else:
			B += c
F = F[::-1] + B
if not order:
	F = F[::-1]
print(F)


import sys
i = sys.stdin.read().split('\n')[:-1]
s = i[0]
a = ['', '']
r = 0
for l in i[2:]:
	if l[0] == '1':
		r ^= 1
	else:
		(t, f, c) = l.split()
		a[0 if f == '21'[r] else 1] += c
if r:
	print(a[0][::-1] + s[::-1] + a[1])
else:
	print(a[1][::-1] + s + a[0])


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
	elif 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)


s = input()
q = int(input())
par = 0
pre = ''
pos = ''
for i in range(q):
	t = input().split()
	if t[0] == '1':
		par += 1
	elif (par + int(t[1])) % 2 == 1:
		pre = t[2] + pre
	else:
		pos = pos + t[2]
ans = pre + s + pos
if par % 2 == 0:
	print(ans)
else:
	print(ans[::-1])


from collections import deque
s = input()
q = int(input())
inv = False
(pr, su) = (deque(), deque())
for _ in range(q):
	query = input()
	if query[0] == '1':
		inv = not inv
	else:
		(_, f, c) = query.split()
		if inv:
			if f == '1':
				su.append(c)
			else:
				pr.append(c)
		elif f == '1':
			pr.append(c)
		else:
			su.append(c)
ans = ''.join(pr)[::-1] + s + ''.join(su)
print(ans[::-1] if inv else ans)


right = input()
Q = int(input())
flag = 1
left = ''
for i in range(Q):
	q = input()
	if q == '1':
		flag = 1 - flag
		continue
	(m, f, c) = q.split(' ')
	f = int(f)
	left += c * (1 - flag) * (f - 1)
	right += c * flag * (f - 1)
	left += c * flag * (2 - f)
	right += c * (1 - flag) * (2 - f)
if flag == 1:
	print(left[::-1] + right)
else:
	print(right[::-1] + left)


s = str(input())
q = int(input())
now = 0
(mae, usiro) = ('', '')
for _ in range(q):
	lis = list(map(str, input().split()))
	if int(lis[0]) == 1:
		now = (now + 1) % 2
	elif (int(lis[1]) + now) % 2 == 1:
		mae = lis[2] + mae
	else:
		usiro = usiro + lis[2]
s = mae + s + usiro
if now == 0:
	print(s)
else:
	print(s[::-1])


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]
	elif i[2] == '2':
		S = i[4] + S
	else:
		S = S + i[4]
print(S[::key])


rL = list(input())
lL = []
Q = int(input())
for i in range(Q):
	N = list(input().split())
	if N[0] == '1':
		(lL, rL) = (rL, lL)
	elif N[1] == '1':
		lL.append(N[2])
	else:
		rL.append(N[2])
lL.reverse()
print(''.join(lL) + ''.join(rL))


S = input()
Q = int(input())
T = ''
for i in range(Q):
	Query = list(input().split())
	if Query[0] == '1':
		(S, T) = (T, S)
	elif Query[0] == '2':
		if Query[1] == '1':
			T = T + Query[2]
		elif Query[1] == '2':
			S = S + Query[2]
T = T[::-1]
T = T + S
print(T)


s = input()
q = int(input())
cnt = 0
right = ''
left = ''
for i in range(q):
	query = list(input().split())
	if query[0] == '1':
		cnt += 1
	elif (cnt + int(query[1])) % 2 == 0:
		right += query[2]
	else:
		left += query[2]
if cnt % 2 == 0:
	print(left[::-1] + s + right)
else:
	print(right[::-1] + s[::-1] + left)


s = list(input())
q = int(input())
(t1, t2) = ([], [])
cnt = 0
for _ in range(q):
	t = list(input().split())
	if len(t) == 1:
		cnt += 1
		(t1, t2) = (t2, t1)
	elif int(t[1]) == 1:
		t1 += [t[2]]
	else:
		t2 += [t[2]]
if cnt % 2 == 1:
	s = s[::-1]
u = t1[::-1] + s + t2
print(''.join(u))


from collections import deque
S = input()
S = deque(S)
N = int(input())
rev = 0
for i in range(N):
	Q = input().split()
	if Q[0] == '1':
		rev += 1
		rev %= 2
	elif (rev + int(Q[1])) % 2 == 1:
		S.appendleft(Q[2])
	else:
		S.append(Q[2])
S = ''.join(S)
if rev == 1:
	S = S[::-1]
print(S)


s = list(input())
from _collections import deque
s = deque(s)
q = int(input())
rev = False
for i in range(q):
	query = input()
	if len(query) == 1:
		rev = not rev
	else:
		(x, f, c) = query.split()
		if f == '1':
			if rev:
				s.append(c)
			else:
				s.appendleft(c)
		elif rev:
			s.appendleft(c)
		else:
			s.append(c)
n = len(s)
if rev:
	ans = [s[n - 1 - i] for i in range(n)]
else:
	ans = [s[i] for i in range(n)]
print(''.join(ans))


S = input()
Q = int(input())
d = 0
X = [[], []]
for i in range(Q):
	q = input()
	if q[0] == '1':
		d = (d + 1) % 2
	else:
		(a, f, c) = q.split()
		x = (int(f) + d) % 2
		X[x].append(c)
if d == 0:
	v = ''.join(X[1])[::-1] + S + ''.join(X[0])
else:
	v = ''.join(X[0])[::-1] + S[::-1] + ''.join(X[1])
print(v)


s = input()
n = int(input())
(a, b) = ([], [])
p = 0
for i in range(n):
	l = input().split()
	if len(l) == 1:
		p += 1
	else:
		k = int(l[1])
		if k % 2 == p % 2:
			b += [l[2]]
		else:
			a += [l[2]]
if p % 2 == 1:
	s = s[::-1]
	(a, b) = (b, a)
print(''.join(a)[::-1] + s + ''.join(b))


S = input()
top = ''
Q = int(input())
t = 0
for i in range(Q):
	strA = input().split()
	if strA[0] == '1':
		t = t + 1
	elif strA[1] == '1':
		if t % 2 == 0:
			top = top + strA[2]
		else:
			S = S + strA[2]
	elif t % 2 == 0:
		S = S + strA[2]
	else:
		top = top + strA[2]
if t % 2 == 0:
	S = top[::-1] + S
else:
	S = S[::-1] + top
print(S)


import sys
s = input()
q = int(input())
a = ['', '']
r = 0
for i in range(q):
	l = sys.stdin.readline()
	if l[0] == '1':
		r ^= 1
	else:
		(t, f, c) = l.split()
		a[0 if f == '21'[r] else 1] += c
if r:
	print(a[0][::-1] + s[::-1] + a[1])
else:
	print(a[1][::-1] + s + a[0])


from collections import deque
import sys

def input():
	return sys.stdin.readline().strip()
q = deque(input())
Q = int(input())
flipped = False
for _ in range(Q):
	(q_t, *q_b) = input().split()
	if q_t == '1':
		flipped = not flipped
	else:
		(F, C) = q_b
		if F == '2' and (not flipped) or (F == '1' and flipped):
			q.append(C)
		else:
			q.appendleft(C)
q = list(q)
if flipped:
	print(''.join(q[::-1]))
else:
	print(''.join(q))


from collections import deque

def main():
	s = list(input())
	q = int(input())
	query = []
	que = deque(s)
	f = False
	for i in range(q):
		query.append(input().split(' '))
	for i in range(q):
		if query[i][0] == '1':
			f = not f
		elif f:
			if query[i][1] == '2':
				que.appendleft(query[i][2])
			else:
				que.append(query[i][2])
		elif query[i][1] == '1':
			que.appendleft(query[i][2])
		else:
			que.append(query[i][2])
	if f:
		que.reverse()
	print(''.join(que))

def __starting_point():
	main()
__starting_point()


s = input()
q = int(input())
(r, a) = (0, [[], []])
for _ in range(q):
	t = input()
	if t == '1':
		r = -(-~r % 2)
	else:
		a[~-r + int(t[2])].append(t[4])
print(''.join(a[~-r + 1][::-1]) + s[::r or 1] + ''.join(a[~-r + 2]))


from collections import deque
poe = deque([])
S = input()
for i in range(len(S)):
	poe.append(S[i])
Q = int(input())
P = []
cnt = 0
for i in range(Q):
	a = list(input().split())
	P.append(a)
for i in range(Q):
	if len(P[i]) == 1:
		cnt += 1
	else:
		P[i][1] = int(P[i][1]) + cnt
		if P[i][1] % 2 == 1:
			poe.appendleft(P[i][2])
		else:
			poe.append(P[i][2])
poe = list(poe)
if cnt % 2 == 1:
	poe = poe[::-1]
print(''.join(poe))


import sys
s = input()
flag = 0
s1 = ''
s2 = ''
for i in range(int(input())):
	X = sys.stdin.readline().split()
	if int(X[0]) == 1:
		flag = flag ^ 1
	elif int(X[1]) - 1 ^ flag == 0:
		s1 += X[2]
	else:
		s2 += X[2]
s3 = s1[::-1] + s + s2
if flag:
	print(s3[::-1])
else:
	print(s3)


S = str(input())
Q = int(input())
qs = [list(map(str, input().split())) for _ in range(Q)]
(x, y) = (S, '')
for ls in qs:
	if ls[0] == '1':
		(x, y) = (y, x)
	elif ls[1] == '1':
		y += ls[2]
	else:
		x += ls[2]
y = ''.join(list(reversed(y)))
print(y + x)


from collections import deque
s = deque(list(input()))
n = int(input())
count = 0
for i in range(n):
	x = input()
	if x[0] == '1':
		count += 1
		count %= 2
	elif x[2] == '1':
		if count % 2 == 0:
			s.appendleft(x[4])
		else:
			s.append(x[4])
	elif count % 2 == 0:
		s.append(x[4])
	else:
		s.appendleft(x[4])
if count % 2 == 1:
	s.reverse()
print(''.join(s))


s = input()
q = int(input())
head = ''
tail = ''
rev = 0
for _ in range(q):
	T = list(input().split())
	if T[0] == '1':
		(head, tail) = (tail, head)
		rev += 1
	elif T[1] == '1':
		head += T[2]
	else:
		tail += T[2]
if rev % 2 == 0:
	print(head[::-1] + s + tail)
else:
	print(head[::-1] + s[::-1] + tail)


s = list(input())
q = int(input())
rv = False
right = []
left = []
for _ in range(q):
	query = input()
	if query[0] == '1':
		rv = not rv
	else:
		(_, f, c) = query.split()
		if f == '1' and rv == False:
			left.append(c)
		elif f == '1' and rv == True:
			right.append(c)
		elif f == '2' and rv == False:
			right.append(c)
		elif f == '2' and rv == True:
			left.append(c)
if not rv:
	left.reverse()
	print(''.join(left) + ''.join(s) + ''.join(right))
else:
	right.reverse()
	s.reverse()
	print(''.join(right) + ''.join(s) + ''.join(left))


import sys
input = sys.stdin.readline
s = list(input().replace('\n', ''))
from collections import deque
dqn = deque(s)
n = int(input())
temp_rev = False
for _ in range(n):
	qfc = input().split()
	q = qfc[0]
	if q == '1':
		temp_rev = not temp_rev
	else:
		f = qfc[1]
		c = qfc[2]
		if f == '1':
			if temp_rev:
				dqn.append(c)
			else:
				dqn.appendleft(c)
		elif temp_rev:
			dqn.appendleft(c)
		else:
			dqn.append(c)
dqn = list(dqn)
if temp_rev:
	print(''.join([dqn[len(dqn) - 1 - ii] for ii in range(len(dqn))]))
else:
	print(''.join(dqn))


from collections import deque
s = deque(input())
q = int(input())
query = [list(input().split()) for _ in range(q)]
reverse = 0
for i in range(q):
	if int(query[i][0]) == 1:
		reverse = abs(reverse - 3)
	elif abs(int(query[i][1]) - reverse) == 1:
		s.appendleft(query[i][2])
	else:
		s.append(query[i][2])
if reverse:
	s = list(s)[::-1]
else:
	s = list(s)
print(''.join(list(map(str, s))))


s = input()
q = int(input())
state = 0
(x, y) = ('', '')
for i in range(q):
	a = input().split()
	if a[0] == '1':
		state = state ^ 1
	elif a[1] == '1' and state == 0 or (a[1] == '2' and state == 1):
		x = a[2] + x
	else:
		y = y + a[2]
out = x + s + y
if state == 0:
	print(out)
else:
	print(out[::-1])


from collections import deque
s = deque([i for i in input()])
q = int(input())
cnt = 0
for _ in range(q):
	s_ = input()
	if len(s_) == 1:
		cnt += 1
	elif (int(s_[2]) + cnt % 2) % 2 == 0:
		s.append(s_[4])
	else:
		s.appendleft(s_[4])
if cnt % 2 == 1:
	print(''.join(list(s)[::-1]))
else:
	print(''.join(s))


S = input()
p = 1
head = ''
tail = ''
Q = int(input())
for _ in range(Q):
	q = input()
	if q == '1':
		p ^= 1
	else:
		(a, f, c) = q.split()
		f = int(f) % 2
		if p == 0:
			f = (f + 1) % 2
		if f:
			head += c
		else:
			tail += c
if p:
	S = head[::-1] + S + tail
else:
	S = tail[::-1] + S[::-1] + head
print(S)


(s, _, *q) = open(0)
g = ''
s = s.strip()
for q in q:
	q = q.strip().split()
	if q[0] == '1':
		(g, s) = (s, g)
	elif q[1] == '1':
		g += q[2]
	else:
		s += q[2]
print(g[::-1] + s)


def resolve():
	s = input()
	query = int(input())
	r = 0
	left = ''
	for i in range(query):
		q = input()
		if q[0] == '2':
			(_, f, c) = q.split()
			if (int(f) + r) % 2 == 0:
				s += c
			else:
				left += c
		else:
			r = (r + 1) % 2
	print(left[::-1] + s if r == 0 else s[::-1] + left)
resolve()


from collections import deque
s = deque(input())
q = int(input())
flag = True
for _ in range(q):
	tfc = input().split()
	if tfc[0] == '1':
		if flag == True:
			flag = False
		else:
			flag = True
	elif flag == True:
		if tfc[1] == '1':
			s.appendleft(tfc[2])
		else:
			s.append(tfc[2])
	elif tfc[1] == '1':
		s.append(tfc[2])
	else:
		s.appendleft(tfc[2])
if flag == True:
	print(''.join(s))
else:
	s.reverse()
	print(''.join(s))


s = str(input())
q = int(input())
inv = False
head = ''
tail = ''
for i in range(q):
	a = input()
	if a == '1':
		inv = not inv
		continue
	a = a.split()
	if (a[1] == '1') ^ inv:
		head = a[2] + head
	else:
		tail += a[2]
s = head + s + tail
if inv:
	print(s[::-1])
else:
	print(s)


s = input()
q = int(input())
cnt = 0
l = ''
r = ''
for i in range(q):
	k = input()
	if k[0] == '1':
		cnt += 1
	elif k[2] == '1':
		if cnt % 2 == 0:
			l = k[4] + l
		else:
			r += k[4]
	elif k[2] == '2':
		if cnt % 2 != 0:
			l = k[4] + l
		else:
			r += k[4]
if cnt % 2 == 0:
	s = l + s + r
else:
	s = l + s + r
	s = s[::-1]
print(s)


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
	elif (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 = 0
for _ in range(Q):
	q = input()
	if q == '1':
		rev = 1 - rev
	else:
		(q1, f, c) = q.split()
		if rev:
			if f == '1':
				S.append(c)
			else:
				S.appendleft(c)
		elif f == '1':
			S.appendleft(c)
		else:
			S.append(c)
if rev:
	S.reverse()
print(''.join(S))


s = input()
q = int(input())
jun = 1
f = ''
t = ''
for _ in range(q):
	tmp = input()
	if len(tmp) == 1:
		jun = 1 if jun == 0 else 0
	else:
		(_, ff, c) = tmp.split()
		if jun + int(ff) == 2:
			f = c + f
		else:
			t += c
if jun == 1:
	print(f + s + t)
else:
	print((f + s + t)[::-1])


s = input()
n = int(input())
flag = 1
left = ''
right = ''
for i in range(n):
	l = list(input().split())
	if len(l) == 1:
		flag *= -1
	elif l[1] == '1' and flag == 1 or (l[1] == '2' and flag == -1):
		left += l[2]
	else:
		right += l[2]
s = left[::-1] + s + right
if flag == -1:
	print(s[::-1])
else:
	print(s)


s = input()
q = int(input())
a = ''
b = ''
res = 0
for i in range(q):
	l = list(input().split())
	if l[0] == '1':
		res += 1
		(a, b) = (b, a)
	elif l[1] == '1':
		a += l[2]
	else:
		b += l[2]
if res % 2 == 0:
	print(a[::-1] + s + b)
else:
	print(a[::-1] + s[::-1] + b)


from collections import deque
s = deque(input())
num = int(input())
cnt = 0
for _ in range(num):
	t = input().split()
	if t[0] == '1':
		cnt += 1
	elif (int(t[1]) + cnt) % 2 == 1:
		s.appendleft(t[2])
	else:
		s.append(t[2])
if cnt % 2 == 1:
	s.reverse()
print(''.join(s))


s = input()
q = int(input())
f = ''
b = ''
odd = 0
for i in range(q):
	(t, *fc) = input().split()
	if t == '1':
		odd ^= 1
	elif odd ^ int(fc[0]) - 1:
		b += fc[1]
	else:
		f += fc[1]
if odd:
	print(b[::-1] + s[::-1] + f)
else:
	print(f[::-1] + s + b)


from collections import deque
s = input()
q = int(input())
a = 1
p = deque([s])
for _ in range(q):
	tfc = list(input().split())
	if tfc[0] == '1':
		a *= -1
	else:
		c = tfc[2]
		if tfc[1] == '1':
			if a == 1:
				p.appendleft(c)
			else:
				p.append(c)
		elif a == 1:
			p.append(c)
		else:
			p.appendleft(c)
print(''.join(p) if a == 1 else ''.join(p)[::-1])


from collections import deque
d = deque(input())
n = int(input())
p = []
flg = 0
for i in range(n):
	p = list(input().split())
	if p[0] == '1':
		flg ^= 1
	elif p[1] == '1':
		if flg == 1:
			d.append(p[2])
		else:
			d.appendleft(p[2])
	elif flg == 1:
		d.appendleft(p[2])
	else:
		d.append(p[2])
if flg == 1:
	d.reverse()
a = ''
for i in d:
	a += i
print(a)


import collections
import sys
S = collections.deque(input())
Q = int(input())
r = 0
for _ in range(Q):
	fs = next(sys.stdin).split()
	T = int(fs[0])
	if T == 1:
		r ^= 1
	else:
		(F, C) = (int(fs[1]) - 1, fs[2])
		if F == r:
			S.appendleft(C)
		else:
			S.append(C)
ans = ''.join(reversed(S) if r else S)
print(ans)


s = input()
q = int(input())
que = [list(map(str, input().split())) for i in range(q)]
count = 0
for i in que:
	if i[0] == '1':
		count += 1
ans = [[], []]
ak = count
for i in que:
	if i[0] == '1':
		count -= 1
	elif i[1] == '1':
		ans[count % 2].append(i[2])
	else:
		ans[(count + 1) % 2].append(i[2])
if ak % 2 == 0:
	print(''.join(reversed(ans[0])) + s + ''.join(ans[1]))
else:
	print(''.join(reversed(ans[0])) + ''.join(reversed(list(s))) + ''.join(ans[1]))


def N():
	return int(input())

def L():
	return list(map(int, input().split()))

def NL(n):
	return [list(map(int, input().split())) for i in range(n)]
mod = pow(10, 9) + 7
import sys
import math
import collections
s = input()
n = N()
f = False
h = ''
t = ''
for i in range(n):
	q = input().split()
	if len(q) == 1:
		f = not f
	elif f and q[1] == '1' or (not f and q[1] == '2'):
		t += q[2]
	else:
		h += q[2]
if f:
	ans = t[::-1] + s[::-1] + h
else:
	ans = h[::-1] + s + t
print(ans)


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


from collections import deque
d = deque()
d.append(input())
q = int(input())
r = 1
for i in range(q):
	q2 = input().split()
	if int(q2[0]) == 1:
		r *= -1
	elif r == 1 and int(q2[1]) == 1 or (r == -1 and int(q2[1]) == 2):
		d.appendleft(q2[2])
	else:
		d.append(q2[2])
print(''.join(d) if r == 1 else ''.join(d)[::-1])


s = input()
q = int(input())
h = 0
t = ''
b = ''
for i in range(q):
	x = list(input().split())
	if len(x) == 1:
		h += 1
	elif h % 2 == 0:
		if x[1] == '1':
			t = x[-1] + t
		else:
			b += x[-1]
	elif h % 2 == 1:
		if x[1] == '1':
			b += x[-1]
		else:
			t = x[-1] + t
if h % 2 == 0:
	print(t + s + b)
else:
	s = t + s + b
	s = s[::-1]
	print(s)


s = input()
q = int(input())
flag = 0
t = ['' for _ in range(2)]
cnt = 0
for _ in range(q):
	a = list(input().split())
	if a[0] == '1':
		flag = 1 - flag
		cnt += 1
	elif a[1] == '1':
		t[flag] += a[2]
	else:
		t[1 - flag] += a[2]
ans = t[0][::-1] + s + t[1]
if cnt % 2:
	ans = ans[::-1]
print(ans)


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
	elif (int(qi[2]) + rev % 2) % 2:
		ans.appendleft(qi[4])
	else:
		ans.append(qi[4])
if rev % 2:
	ans.reverse()
for i in ans:
	print(i, end='')


s = [input()]
q = int(input())
turn = True
l = ''
for i in range(q):
	qq = input().split()
	if qq[0] == '1':
		turn = not turn
	elif turn == bool(int(qq[1]) - 2):
		l += qq[2]
	else:
		s.append(qq[2])
text = l[::-1] + ''.join(s)
print(text if turn else text[::-1])


import sys
import math
import itertools
import collections
from collections import deque
from collections import defaultdict
sys.setrecursionlimit(1000000)
MOD = 10 ** 9 + 7
MOD2 = 998244353
INF = float('inf')
input = lambda : sys.stdin.readline().strip()
NI = lambda : int(input())
NMI = lambda : map(int, input().split())
NLI = lambda : list(NMI())
SI = lambda : input()

def main():
	S = SI()
	Q = NI()
	que = deque(list(S))
	reverse = False
	for q in range(Q):
		query = input()
		if query == '1':
			if reverse == False:
				reverse = True
			else:
				reverse = False
		else:
			(x, y, z) = map(str, query.split())
			if reverse == False:
				if y == '1':
					que.appendleft(z)
				else:
					que.append(z)
			elif y == '1':
				que.append(z)
			else:
				que.appendleft(z)
	if reverse == True:
		print(''.join(reversed(list(que))))
	else:
		print(''.join(list(que)))

def __starting_point():
	main()
__starting_point()


S = input()
Q = input()
L = ''
R = ''
rev = -1
for i in range(int(Q)):
	tmp = input().split()
	if tmp[0] == '1':
		rev = abs(rev) - 1
	elif rev + int(tmp[1]) == 1:
		R = R + tmp[2]
	else:
		L = L + tmp[2]
if rev == -1:
	print(L[::-1] + S + R)
else:
	print(R[::-1] + S[::-1] + L)


S = input()
Q = int(input())
Query = list((input().split() for _ in range(Q)))
count = 0
(L, R) = ('', '')
for i in range(Q):
	if Query[i][0] == '1':
		count += 1
	elif Query[i][1] == '1':
		if count % 2 == 0:
			L = Query[i][2] + L
		else:
			R += Query[i][2]
	elif count % 2 == 0:
		R += Query[i][2]
	else:
		L = Query[i][2] + L
if count % 2 == 0:
	print(L + S + R)
else:
	print(R[::-1] + S[::-1] + L[::-1])


s = input()
t = ''
Q = int(input())
flag = 0
for _ in range(Q):
	A = list(map(str, input().split()))
	if A[0] == '1':
		flag += 1
	elif (int(A[1]) + flag) % 2:
		t += A[2]
	else:
		s += A[2]
ans = t[::-1] + s
if flag % 2:
	print(ans[::-1])
else:
	print(ans)


from collections import deque
S = deque(input())
Q = int(input())
cnt = 0
for i in range(Q):
	x = list(input().split())
	if int(x[0]) == 1:
		cnt += 1
	elif (cnt + int(x[1])) % 2 == 1:
		S.appendleft(x[2])
	else:
		S.append(x[2])
S = list(S)
if cnt % 2 == 1:
	S = S[::-1]
print(''.join(S))


se = input()
q = int(input())
h = 0
se1 = ''
se2 = ''
for i in range(q):
	s = input().split()
	if len(s) == 1:
		h += 1
	elif len(s) == 3:
		if (h + int(s[1]) - 1) % 2 == 0:
			se1 += s[2]
		else:
			se2 += s[2]
r = se1[::-1] + se + se2
if h % 2 == 0:
	print(r)
else:
	print(r[::-1])


s = input()
n = int(input())
r = -1
f = []
b = []
for i in range(n):
	q = input().split()
	if q[0] == '1':
		r *= -1
	else:
		t = int(q[1])
		if t * r == -1 or t * r == 2:
			f.append(q[2])
		else:
			b.append(q[2])
f = ''.join(f)
b = ''.join(b)
s = f[::-1] + s + b
if r == 1:
	s = s[::-1]
print(s)


import math
import collections
from itertools import product
ii = lambda : int(input())
mi = lambda : map(int, input().split())
li = lambda : list(map(int, input().split()))
s = input()
q = ii()
query = [input().split() for _ in range(q)]
front = []
back = []
cnt1 = 0
for i in range(q):
	if query[i][0] == '1':
		cnt1 += 1
	elif cnt1 % 2 == 0:
		if query[i][1] == '1':
			front.append(query[i][2])
		else:
			back.append(query[i][2])
	elif query[i][1] == '1':
		back.append(query[i][2])
	else:
		front.append(query[i][2])
if cnt1 % 2 == 0:
	print(''.join(front[::-1]) + s + ''.join(back))
else:
	print(''.join(back[::-1]) + s[::-1] + ''.join(front))


from collections import deque
S = deque(input())
Q = int(input())
mode = 0
for _ in range(Q):
	(T, *FC) = input()
	if T == '1':
		mode ^= 1
		continue
	(_, F, _, C) = FC
	if int(F) + mode == 2:
		S.append(C)
	else:
		S.appendleft(C)
print(''.join(reversed(S) if mode else S))


from collections import deque
d = deque()
S = input()
d.append(S)
Q = int(input())
query = [list(input().split()) for _ in range(Q)]
rev = 0
for i in range(Q):
	if query[i][0] == str(1):
		rev += 1
	elif rev % 2 == 0:
		if query[i][1] == str(1):
			d.appendleft(query[i][2])
		else:
			d.append(query[i][2])
	elif query[i][1] == str(1):
		d.append(query[i][2])
	else:
		d.appendleft(query[i][2])
ans = ''.join(list(d))
if rev % 2 != 0:
	ans = ans[::-1]
print(ans)


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)


from collections import deque
s = deque(input())
n = int(input())
f = 1
for i in range(n):
	l = list(map(str, input().split()))
	if l[0] == '2':
		tmp = int(l[1]) * f
		if tmp == 1 or tmp == -2:
			s.appendleft(l[2])
		else:
			s.append(l[2])
	else:
		f *= -1
if f == -1:
	s.reverse()
print(''.join(s))


s = input()
count = 0
s1 = str()
s2 = str()
for _ in range(int(input())):
	l = list(map(str, input().split()))
	if l[0] == '1':
		count += 1
	elif count % 2 == 0:
		if l[1] == '1':
			s1 = l[2] + s1
		else:
			s2 = s2 + l[2]
	elif l[1] == '2':
		s1 = l[2] + s1
	else:
		s2 = s2 + l[2]
s = s1 + s + s2
if count % 2 != 0:
	s = s[::-1]
print(s)


from collections import deque
S = input()
q = int(input())
Query = [list(map(str, input().split())) for _ in range(q)]
flg = True
D = deque(S)
for query in Query:
	if query[0] == '1':
		flg = not flg
	else:
		(F, C) = (query[1], query[2])
		if F == '1' and flg is True:
			D.appendleft(C)
		elif F == '1' and flg is False:
			D.append(C)
		elif F == '2' and flg is True:
			D.append(C)
		elif F == '2' and flg is False:
			D.appendleft(C)
if flg:
	print(''.join(D))
else:
	print(''.join(list(reversed(D))))


a = input()
b = ''
l = ''
f = 0
for _ in range(int(input())):
	k = list(input().split())
	if k[0] == '1':
		f = (f + 1) % 2
	elif 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])


s = input()
n = int(input())
t = ''
for _ in range(n):
	q = input().split()
	if q[0] == '1':
		(t, s) = (s, t)
	elif q[1] == '1':
		t += q[2]
	else:
		s += q[2]
t = t[::-1]
print(t + 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
	elif 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))


from collections import deque
s = deque(input())
num = int(input())
cnt = 0
for i in range(num):
	a = input().split()
	if a[0] == '2':
		if (int(a[1]) + cnt) % 2 == 1:
			s.appendleft(a[2])
		else:
			s.append(a[2])
	else:
		cnt += 1
if cnt % 2 == 1:
	s.reverse()
print(''.join(s))


s = input()
q = int(input())
flag = 1
f = ''
b = ''
for i in range(q):
	query = list(map(str, input().split()))
	if query[0] == '1':
		flag *= -1
	elif flag == 1:
		if query[1] == '1':
			f += query[2]
		else:
			b += query[2]
	elif query[1] == '1':
		b += query[2]
	else:
		f += query[2]
ans = f[::-1] + s + b
if flag == -1:
	ans = ans[::-1]
print(ans)


S = input()
Q = int(input())
rev = False
ad = ''
for _ in range(Q):
	A = input().split()
	if len(A) == 1:
		rev ^= True
	elif A[1] == '1' and rev == False or (A[1] == '2' and rev == True):
		ad += A[2]
	else:
		S += A[2]
ans = (ad[::-1] + S)[::1 - 2 * rev]
print(ans)


S = input()
Q = int(input())
I = 0
L = ''
R = ''
for i in range(Q):
	Query = input().split()
	if Query[0] == '1':
		I = I + 1
	elif (int(Query[1]) + I) % 2 == 0:
		R = R + Query[2]
	else:
		L = L + Query[2]
L = L[::-1]
S = L + S + R
if I % 2:
	S = S[::-1]
print(S)


from collections import deque
s = str(input())
q = int(input())
stat = 0
l = []
a = deque()
b = deque()
for i in range(q):
	l = list(map(str, input().split()))
	t = int(l[0])
	if t == 1:
		stat += 1
	else:
		f = int(l[1])
		c = l[2]
		if stat % 2 == 0:
			if f == 1:
				a.appendleft(c)
			else:
				b.append(c)
		elif f == 1:
			b.append(c)
		else:
			a.appendleft(c)
ans = ''.join(a) + s + ''.join(b)
if stat % 2 == 1:
	ans = ans[::-1]
print(ans)


ans = input()
q = int(input())
r = 0
p = ''
for i in range(q):
	d = input().split()
	if len(d) == 1:
		r += 1
	else:
		f = int(d[1])
		c = d[2]
		if (f + r) % 2 == 1:
			p += c
		else:
			ans += c
ans = p[::-1] + ans
if r % 2:
	ans = ans[::-1]
print(ans)


s = str(input())
q = int(input())
b = 1
x = ''
y = ''
for i in range(q):
	la = input()
	if la == '1':
		b ^= 1
	else:
		(a, f, c) = la.split()
		if f == '1':
			if b == 1:
				x = c + x
			else:
				y = y + c
		elif b == 1:
			y = y + c
		else:
			x = c + x
s = x + s + y
print(s if b == 1 else s[::-1])


s = input()
q = int(input())
head = ''
foot = ''
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) & 1 == 0:
			foot += c
		else:
			head = c + head
s = head + s + foot
if a == 1:
	s = s[::-1]
print(s)


S = input()
Q = int(input())
query = [list(input().split()) for _ in range(Q)]
forward = 1
f = ''
b = ''
for i in range(Q):
	if query[i][0] == '1':
		forward *= -1
	elif query[i][1] == '1':
		if forward == 1:
			f += query[i][2]
		else:
			b += query[i][2]
	elif forward == 1:
		b += query[i][2]
	else:
		f += query[i][2]
S = ''.join(list(reversed(f))) + S + b
if forward == -1:
	ans = ''.join(list(reversed(S)))
else:
	ans = S
print(ans)


s = list(input())
top = bool()
t = []
u = []
n = int(input())
for i in range(n):
	q = list(map(str, input().split()))
	if q[0] == '1':
		top = not top
	elif (q[1] == '1') ^ top:
		t.append(q[2])
	else:
		u.append(q[2])
s = list(reversed(t)) + s + u
if top:
	print(*list(reversed(s)), sep='')
else:
	print(*s, sep='')


s = input()
T = ''
n = int(input())
for i in range(n):
	t = list(map(str, input().split()))
	if t[0] == '1':
		(s, T) = (T, s)
	elif t[1] == '1':
		T += t[2]
	else:
		s += t[2]
print(T[::-1] + s)


Sr = list(input())
Sl = []
Q = int(input())
for i in range(Q):
	s = list(input().split())
	if len(s) == 1:
		(Sr, Sl) = (Sl, Sr)
	elif s[1] == '1':
		Sl.append(s[2])
	else:
		Sr.append(s[2])
Sl.reverse()
print('%s' % (''.join(Sl) + ''.join(Sr)))


S = input()
Q = int(input())
fb = True
forward = ''
back = ''
for i in range(Q):
	query = input()
	if query == '1':
		fb = False if fb else True
	else:
		(query, F, C) = query.split()
		if (F == '1') == fb:
			forward = C + forward
		else:
			back = back + C
print((forward + S + back)[::1 if fb else -1])


from collections import *
(s, _, *q) = open(0)
d = deque(s[:-1])
p = 1
for q in q:
	try:
		(q, f, c) = q.split()
		if (f < '2') ^ p:
			d += (c,)
		else:
			d.appendleft(c)
	except:
		p ^= 1
print(''.join(d)[::p or -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
	elif (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)


s = input()
q = int(input())
rev = False
top = ''
for i in range(q):
	query = input().split()
	if query[0] == '1':
		rev = not rev
	elif query[1] == '1':
		if rev:
			s += query[2]
		else:
			top += query[2]
	elif query[1] == '2':
		if rev:
			top += query[2]
		else:
			s += query[2]
if rev:
	s = s[::-1] + top
else:
	s = top[::-1] + s
print(s)


S = input()
Q = int(input())
flag = 1
s1 = ''
for i in range(Q):
	q = input()
	if len(q) == 1:
		(s1, S) = (S, s1)
	else:
		(T, F, C) = q.split(' ')
		if int(F) == 1:
			s1 = s1 + C
		else:
			S = S + C
print(s1[::-1] + S)


s = input()
q = int(input())
from collections import deque
qq = deque(s)
cnt = 0
for i in range(q):
	j = input()
	if j == '1':
		cnt += 1
		continue
	(_, k, l) = j.split()
	if (k == '2') ^ cnt % 2:
		qq.append(l)
	else:
		qq.appendleft(l)
if cnt % 2 == 1:
	print(''.join(reversed(list(qq))))
else:
	print(''.join(qq))


S = input()
Q = int(input())
flip = False
head = ''
tail = ''
for _ in range(Q):
	q = input()
	if q == '1':
		flip = not flip
		(head, tail) = (tail, head)
	else:
		(_, f, c) = q.split()
		if f == '1':
			head += c
		else:
			tail += c
if flip:
	S = S[::-1]
print(head[::-1] + S + tail)


from collections import deque
S = deque(input())
Q = int(input())
Query = [list(input().split()) for _ in range(Q)]
rev = 0
for Q in Query:
	if Q[0] == '1':
		if rev == 0:
			rev = 1
		else:
			rev = 0
		continue
	if Q[1] == '1':
		if rev == 0:
			S.appendleft(Q[2])
		else:
			S.append(Q[2])
	elif rev == 0:
		S.append(Q[2])
	else:
		S.appendleft(Q[2])
if rev == 1:
	ans = list(reversed(list(S)))
else:
	ans = list(S)
print(''.join(ans))


import re
import sys
import math
import itertools
import bisect
from copy import copy
from collections import deque, Counter
from decimal import Decimal
import functools

def v():
	return input()

def k():
	return int(input())

def S():
	return input().split()

def I():
	return list(map(int, input().split()))

def X():
	return list(input())

def L():
	return list(input().split())

def l():
	return list(map(int, input().split()))

def lcm(a, b):
	return a * b // math.gcd(a, b)
sys.setrecursionlimit(10 ** 9)
mod = 10 ** 9 + 7
cnt = 0
ans = 0
inf = float('inf')
al = 'abcdefghijklmnopqrstuvwxyz'
AL = al.upper()
s = deque(X())
n = k()
normal = 0
for i in range(n):
	a = input().split()
	if int(a[0]) == 1:
		normal ^= 1
	else:
		k = int(a[1]) - 1
		k ^= normal
		if k == 1:
			s.append(a[2])
		else:
			s.appendleft(a[2])
print(''.join(s) if normal == 0 else ''.join(list(s)[::-1]))


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
	elif a[1] == '1':
		x.append(a[2])
	else:
		y.append(a[2])
if cnt % 2 == 1:
	s = s[::-1]
s = ''.join(x[::-1]) + s + ''.join(y)
print(s)


import collections
S = list(input())
deq = collections.deque(S)
Q = int(input())
mode = 1
for i in range(Q):
	f = input()
	f1 = f[0]
	if f1 == '1':
		mode = mode * -1
	else:
		(f1, F, C) = map(str, f.split())
		if F == '1' and mode == 1 or (F == '2' and mode == -1):
			deq.appendleft(C)
		if F == '2' and mode == 1 or (F == '1' and mode == -1):
			deq.append(C)
if mode == -1:
	deq.reverse()
ans = ''.join(deq)
print(ans)


import sys
from collections import deque
r = False
d = deque(list(input()))
for i in range(int(input())):
	Q = input().split()
	if Q[0] == '1':
		r = not r
	elif Q[1] == '1' and (not r) or (Q[1] == '2' and r):
		d.appendleft(Q[2])
	else:
		d.append(Q[2])
if r:
	d.reverse()
print(''.join(list(d)))


s = input()
n = int(input())
rev = 1
pre = ''
suf = ''
for i in range(n):
	q = input().split()
	if q[0] == '1':
		rev += 1
	elif (rev + int(q[1])) % 2 == 0:
		pre = q[2] + pre
	else:
		suf = suf + q[2]
ans = pre + s + suf
if rev % 2 == 0:
	print(ans[::-1])
else:
	print(ans)


S = input()
Q = int(input())
T = 0
ins = ''
apd = ''
for i in range(Q):
	t = input()
	if t == '1':
		T = 1 - T
	else:
		(_, F, C) = t.split()
		F = int(F)
		if (T + F) % 2 == 0:
			apd += C
		else:
			ins += C
ans = ins[::-1] + S + apd
print(ans[::(-1) ** T])


def main():
	S = input()
	Q = int(input())
	order = [list(input().split()) for _ in range(Q)]
	left_flag = 0
	right_flag = 0
	cnt = 0
	for i in range(Q):
		if order[i][0] == '1':
			cnt += 1
		elif cnt % 2 == 0:
			if order[i][1] == '1':
				if left_flag == 1:
					left = order[i][2] + left
				else:
					left = order[i][2]
					left_flag = 1
			elif right_flag == 1:
				right = right + order[i][2]
			else:
				right = order[i][2]
				right_flag = 1
		elif order[i][1] == '2':
			if left_flag == 1:
				left = order[i][2] + left
			else:
				left = order[i][2]
				left_flag = 1
		elif right_flag == 1:
			right = right + order[i][2]
		else:
			right = order[i][2]
			right_flag = 1
	if left_flag == 1 and right_flag == 1:
		S = left + S + right
	elif left_flag == 1 and right_flag == 0:
		S = left + S
	elif left_flag == 0 and right_flag == 1:
		S = S + right
	else:
		S = S
	if cnt % 2 == 0:
		return S
	else:
		S2 = S[-1]
		for i in range(len(S) - 2, -1, -1):
			S2 = S2 + S[i]
		return S2
print(main())


s = input()
q = int(input())
l = []
r = []
c = 0
for _ in range(q):
	t = input().split()
	if t[0] == '1':
		c += 1
		a = l
		l = r
		r = a
	elif t[1] == '1':
		l.append(t[2])
	else:
		r.append(t[2])
l = ''.join(l[::-1])
r = ''.join(r)
if c % 2 == 0:
	print(l + s + r)
else:
	print(l + s[::-1] + r)


s = input()
l = len(s)
q = int(input())
cnt = 1
l = str()
r = str()
for _ in range(q):
	a = list(input().split())
	if a[0] == '1':
		cnt *= -1
	elif int(a[1]) * cnt == 1 or int(a[1]) * cnt == -2:
		l += a[2]
	else:
		r += a[2]
s = l[::-1] + s + r
if cnt == -1:
	s = s[::-1]
print(s)