Problem:
 Score : 400 points    Problem Statement 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 1 a
4
2 1 p
1
2 2 c
1      Sample Output 1 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 .       Sample Input 2 a
6
2 2 a
2 1 b
1
2 2 c
1
1      Sample Output 2 aabc  There will be Q = 6 operations. Initially, S is a .    Operation 1 : S becomes aa .    Operation 2 : S becomes baa .    Operation 3 : S becomes aab .    Operation 4 : S becomes aabc .    Operation 5 : S becomes cbaa .    Operation 6 : S becomes aabc .    Thus, the resulting string is aabc .       Sample Input 3 y
1
2 1 x      Sample Output 3 xy

Solution (Python):
from collections import deque
s = deque(list(input()))
q = int(input())
flg = True
for _ in range(q):
    try:
        t,f,c = input().split()
        if flg:
            if f == "1":
                s.appendleft(c)
            else:
                s.append(c)
        else:
            if f == "1":
                s.append(c)
            else:
                s.appendleft(c)
    except ValueError:
        if flg:
            flg = False
        else:
            flg = True
if flg:
    print(''.join(s))
else:
    s = list(reversed(s))
    print(''.join(s))

Solution (Python):
from collections import deque
s = deque(list(input()))
q = int(input())
flg = True
for _ in range(q):
    try:
        t,f,c = input().split()
        if flg:
            if f == "1":
                s.appendleft(c)
            else:
                s.append(c)
        else:
            if f == "1":
                s.append(c)
            else:
                s.appendleft(c)
    except ValueError:
        if flg:
            flg = False
        else:
            flg = True
if flg:
    print(''.join(s))
else:
    s = list(reversed(s))
    print(''.join(s))

Solution (Python):
S=input()
Q=int(input())
Q=[input().split() for i in range(Q)]
Forward=[]
Backward=[]
count=0
for i in Q:
    if i[0]=='1':
        count+=1
    else:
        if (int(i[1])+count)%2==1:
            Forward.append(i[2])
        else:
            Backward.append(i[2])  
if count%2==0:
    ans = (''.join(Forward))[::-1]+S+(''.join(Backward))
else:
    ans = (''.join(Backward)[::-1]+S[::-1]+(''.join(Forward)))
print(ans)

Solution (Python):
S = input()
Q = int(input())
q = []
for i in range(Q):
    a = input().split(" ")
    if len(a) == 1:
        q.append([int(a[0])])
    else:
        q.append([int(a[0]), int(a[1]), a[2]])




def switch(s):
    d = ""
    for i in range(len(s)):
        d += s[len(s) - i - 1]
    return d

H = ""
B = ""

direction = 1 

for t in range(len(q)):
    T = q[t]
    if T[0] == 1:
        direction *= -1
    else:
        F = T[1]
        C = T[2]
        if F == 1:
            if direction == 1:
                H += C
            else:
                B += C
        else:
            if direction == 1:
                B += C
            else:
                H += C

if direction == 1:
    print(switch(H) + S + B)
else:
    print(switch(B) + switch(S) + H)

Solution (Python):









import os
import sys




def main():
    S = input()
    Q = int(input())
    appendix_1 = ''
    appendix_2 = ''
    reverse = 0
    for _ in range(Q):
        q = list(input().split())
        if q[0] == '1':
            reverse += 1
        else:
            if q[1] == '1' and reverse % 2 == 0:
                appendix_1 += q[2]
            elif q[1] == '1' and reverse % 2 == 1:
                appendix_2 += q[2]
            if q[1] == '2' and reverse % 2 == 0:
                appendix_2 += q[2]
            elif q[1] == '2' and reverse % 2 == 1:
                appendix_1 += q[2]

    if reverse % 2 == 0:
        print(appendix_1[::-1] + S + appendix_2)
    else:
        print(appendix_2[::-1] + S[::-1] + appendix_1)



if __name__ == "__main__":
    main()

Solution (Python):
import sys
from collections import deque


inint = lambda: int(sys.stdin.readline())
inintm = lambda: map(int, sys.stdin.readline().split())
inintl = lambda: list(inintm())
instrm = lambda: map(str, sys.stdin.readline().split())
instrl = lambda: list(instrm())

s = input()
q = inint()

S = deque()
f = [0,1]

for i in range(len(s)):
    S.append(s[i])

for i in range(q):
    Q = instrl()
    if Q[0] == "1":
        f = f[::-1]
    else:
        if f[0] == 0:
            if Q[1] == "1":
                S.appendleft(Q[2])
            else:
                S.append(Q[2])
        else:
            if Q[1] == "1":
                S.append(Q[2])
            else:
                S.appendleft(Q[2])

if f[0] == 1:
    S.reverse()

print("".join(S))

Solution (Python):
s = input()
q = int(input())
mae,usiro = [],[]

f = 0
for i in range(q):
    t = input().split()
    if t[0] == "1":
        if f % 2 == 0:
            f = 1
        else:
            f = 0
    elif t[0] == "2":
        if t[1] == "1":
            if f == 1:
                usiro.append(t[2])
            else:
                mae.append(t[2])
        elif t[1] == "2":
            if f == 1:
                mae.append(t[2])
            else:
                usiro.append(t[2])

mae = mae[::-1]
ans = "".join(mae)+s+"".join(usiro)
if f:
    print(ans[::-1])
else:
    print(ans)

Solution (Python):
import sys
input = sys.stdin.readline

def main():
    S = input().replace('\n', '')
    Q = int(input())

    flg = 0
    head = ""
    tail = ""
    for _ in range(Q):
        query = input().split()
        if len(query) == 1:
            head, tail = tail, head
            flg += 1
        else:
            T, F, C = query
            if int(F) == 1:
                head += C
            else:
                tail += C
    if flg % 2:
        print(head[::-1] + S[::-1] + tail)
    else:
        print(head[::-1] + S + tail)
main()

Solution (Python):
from collections import deque
S = list(input())
Q = int(input())
reverse = 1

d = deque()
for i in S:
  d.append(i)


for i in range(Q):
  temp = input().split()
  if temp[0] == "1":
    reverse = -reverse
  else:
    if temp[1] == "1" :
      if reverse == 1:
        d.appendleft(temp[2])
      else:
        d.append(temp[2])
    else:
      if reverse == 1:
        d.append(temp[2])
      else:
        d.appendleft(temp[2])
      
      
length = len(d)
for _ in range(length):
  temp = 0
  if reverse == 1:
    temp = d.popleft()
  else:
    temp = d.pop()
  print(temp, end="")

Solution (Python):
from collections import deque

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

rl=1
fleft=deque()
fright=deque()

for i in qq:
  if i[0]=='1':
    rl*=-1
  else:
    if i[1]=='1':
      if rl==1:
        fleft.appendleft(i[2])
      else:
        fright.append(i[2])
    else:
      if rl==1:
        fright.append(i[2])
      else:
        fleft.appendleft(i[2])

fleft.reverse()

s.extendleft(fleft)
s.extend(fright)

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

print(''.join(s))

Solution (Python):
from collections import deque
import sys

input=sys.stdin.readline

S=input().rstrip()

string = deque([S[i] for i in range(len(S))])

reverse = False

for _ in range(int(input())):
    q = input().rstrip().split()

    if q[0] == "1":
        reverse = not reverse

    else:
        if q[1] == "1":
            if reverse:
                string.append(q[2])
            else:
                string.appendleft(q[2])

        else:
            if reverse:
                string.appendleft(q[2])
            else:
                string.append(q[2])

string = list(string)

if reverse:
    string = string[::-1]

print("".join(string))

Solution (Python):
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
  else:
    if 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()

Solution (Python):
import sys

from collections import deque


def input():
    return sys.stdin.readline().strip()


def input_int():
    return int(input())


def input_int_list():
    return [int(i) for i in input().split()]


def main():
    s = deque(list(input()))
    q = input_int()
    inverse = False
    for _ in range(q):
        data = input().split()
        if len(data) == 1:
            inverse = not inverse
        else:
            if (data[1] == "1" and not inverse) or (data[1] == "2" and inverse):
                s.appendleft(data[2])
            else:
                s.append(data[2])
    if inverse:
        print("".join(reversed(s)))
    else:
        print("".join(s))
    return


if __name__ == "__main__":
    main()

Solution (Python):
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
    else:
        if 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)

Solution (Python):
S = list(input())
Q = int(input())
re = False
top = []
end = []
for i in range(Q):
    line = list(map(str,input().split()))
    if line[0] == '1':
        if re == True:
            re = False
        else:
            re = True
    else:
        if (re == False and line[1] == '1') or (re == True and line[1] == '2'):
            top.append(line[2])
        else:
            end.append(line[2])

if re == True:
    S.reverse()
    end.reverse()
    ans = end + S + top
else:
    top.reverse()
    ans = top + S + end
print(''.join(ans))

Solution (Python):







S = input()
























Q = int(input())
query = [list(input().split()) for _ in range(Q)]
direction = 1
S_front = ""
S_back = ""
for l in query:
    if l[0] == "1":
        direction *= -1
    else:
        if (l[1] == "1" and direction == 1) or (l[1] == "2" and direction == -1):
            S_front += l[2]
        else:
            S_back += l[2]

if direction == 1:
    print(S_front[::-1] + S + S_back)
else:
    print(S_back[::-1] + S[::-1] + S_front)

Solution (Python):
from collections import deque
S = input()
Q = int(input())
R = deque(S)
rev = 0

for i in range(Q):
    T = input().split()
    if T[0] == '1':
        rev += 1
    else:
        if T[1] == '1':
            if rev % 2 == 0:
                R.appendleft(T[2])
            else:
                R.append(T[2])
        else:
            if rev % 2 == 0:
                R.append(T[2])
            else:
                R.appendleft(T[2])
ans = ''.join(R)
if rev % 2 ==0:
    print(ans)
else:
    print(ans[::-1])

Solution (Python):

def main():
    import sys
    from collections import deque

    input = sys.stdin.readline

    S = input().rstrip()
    Q = int(input())

    S = deque(S)
    rev = 0
    for _ in [0] * Q:
        q = input().split()
        if q[0] == '1':
            rev += 1
        else:
            if q[1] == '1':
                S.append(q[2]) if rev % 2 else S.appendleft(q[2])
            else:
                S.appendleft(q[2]) if rev % 2 else S.append(q[2])
    ans = ''.join(S)
    print(ans[::-1] if rev % 2 else ans)


if __name__ == '__main__':
    main()

Solution (Python):


from collections import deque
s=deque(input())
Q=int(input())
count=0
query=[]
for i in range(Q):
    arr=input().split()
    query.append(arr)

for i in range(Q):
    if query[i][0]=="1":
        count+=1
    else:
        if count%2==0:
            if query[i][1]=="1":
                s.appendleft(query[i][2])
            else:
                s.append(query[i][2])
        else:
            if query[i][1]=="1":
                s.append(query[i][2])
            else:
                s.appendleft(query[i][2])
if count%2==0:
    for i in range(len(s)):
        print(s[i],end="")
        
else:
    s.reverse()
    for i in range(len(s)):
        print(s[i],end="")

Solution (Python):
from collections import deque

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

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


rl = 1
rlrl = 1
fleft = deque()
fright = deque()

for i in qq:
  if i[0] == '1':
    rl *= -1
  else:
    if i[1] == '1':
      if rl == 1:
        fleft.appendleft(i[2])
      else:
        fright.append(i[2])
    else:
      if rl == 1:
        fright.append(i[2])
      else:
        fleft.appendleft(i[2])

fleft.reverse()        
        
s.extendleft(fleft)
s.extend(fright)        

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

print(''.join(s))

Solution (Python):
S = input()
Q= int(input())
r = False
h = []
t = []
for i in range(Q):
    Query = input().split()
    if int(Query[0]) == 1:
        r = not r
    else:
        if int(Query[1]) == 1 and not r or int(Query[1]) == 2 and r:
            h.append(Query[2])
        else:
            t.append(Query[2])
if not r:
    print(''.join(reversed(h))+S+''.join(t))
else:
    print(''.join(reversed(t))+S[::-1]+''.join(h))

Solution (Python):
def main():
  S = input()
  Q = int(input())
  Query=[input().split() for q in range(Q)]
  cnt=0
  prefix, suffix = '', ''
  for q in Query:
    if q[0]=='1':
      cnt=1^cnt
      prefix, suffix = suffix, prefix
      
    else:
      if q[1]=='1':
        prefix=prefix+q[2]
      else:
        suffix=suffix+q[2]
  if cnt:
    S=S[::-1]
  print(prefix[::-1]+S+suffix)

main()

Solution (Python):
def resolve():
    S = input()
    Q = int(input())
    reverse = False
    import collections
    left = collections.deque([])
    right = collections.deque([])
    for i in range(Q):
        query = input().split()
        if query[0] == "1":
            reverse = (not reverse)
            continue
        if query[1] == "1":
            if reverse:
                right.append(query[2])
            else:
                left.appendleft(query[2])
        elif query[1] == "2":
            if reverse:
                left.appendleft(query[2])
            else:
                right.append(query[2])
    if reverse:
        ans = "{}{}{}".format("".join(reversed(right)), "".join(reversed(S)), "".join(reversed(left)))
    else:
        ans = "{}{}{}".format("".join(left), S, "".join(right))
    print(ans)


if '__main__' == __name__:
    resolve()

Solution (Python):
s = input()
q = int(input())
rev = False
bef = ""
aft = ""
for _ in range(q):
    t = list(input().split())
    if t[0] == "1":
        rev = not rev
    else:
        f = t[1]
        c = t[2]
        if rev:
            if f == "1":
                aft += c
            else:
                bef += c
        else:
            if f == "1":
                bef += c
            else:
                aft += c
if rev:
    ans = aft[::-1] + s[::-1] + bef
else:
    ans = bef[::-1] + s + aft
print(ans)

Solution (Python):
from collections import deque
import sys

flg1 = 1
s = input()
ll = deque()
lr = deque()
for _ in range(int(input())):
    q = sys.stdin.readline().rstrip()
    if q == "1":
        flg1 *= -1
    else:
        q = list(q.split())
        flg2 = 1 if q[1] == "1" else -1
        if flg1 * flg2 == 1:
            ll.appendleft(q[2])
        else:
            lr.append(q[2])

ans = "".join(ll) + s + "".join(lr)
print((ans[::-1], ans)[flg1 == 1])

Solution (Python):
from collections import deque
S = input()
ans = deque(S)
Q = int(input())
rev = False
for i in range(Q):
    a_list = input().split()
    if a_list[0] == "1":
        if rev == True:
            rev = False
        else:
            rev = True
    elif (a_list[1] == "1" and rev == False) or (a_list[1] == "2" and rev == True):
        ans.appendleft(a_list[2])
    else:
        ans.append(a_list[2])
if rev:
    ans.reverse()
ans = "".join(ans)
print(ans)

Solution (Python):
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)

Solution (Python):
S = input()
Q = int(input())
flip = 0
head = []
tail = []
for i in range(Q):
  Query = input().split()
  if len(Query) == 3:
    T,F,C = Query
    F = int(F)-1
    if F^flip == 1:
      tail.append(C)
    else:
      head.append(C)
  else:
    flip ^= 1
if flip:
  head, tail = tail, head
  S = S[::-1]
head = ''.join(head[::-1])
tail = ''.join(tail)
print(head+S+tail)

Solution (Python):
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)

Solution (Python):
S = input().rstrip()
Q = int(input().rstrip())
a = -1
before =[]
after =[]
for _ in range(Q):
    stdin = input().rstrip()
    if int(stdin[0]) == 1:
        a = ~a
    else:
        T,F,C = stdin.split()
        if int(F) == 2+a:
            before.append(C)
        else:
            after.append(C)
S = ''.join(before)[::-1] + S + ''.join(after)
if a == 0:
    S = S[::-1]
print(S)

Solution (Python):
s=input()
q=int(input())

cnt=0
left=""
right=""

for i in range(q):
  qq=[x for x in input().rstrip().split()]

  if qq[0]=="2":
    t=int(qq[0])
    f=int(qq[1])
    c=qq[2]
    
    if cnt%2==1 and f==1:
      right=right+c
    
    elif cnt%2==1 and f==2:

      left=c+left
    elif cnt%2==0 and f==2:
  
      right=right+c
    elif cnt%2==0 and f==1:

      left=c+left

  else:
    cnt+=1

s=left+s+right
if cnt%2==1:
  print(s[::-1])
else:
  print(s)

Solution (Python):
s=input()
q=int(input())
h=0
m=''
u=''
for _  in range(q):
  d=input()
  if '2' in d:
    a,b,c=map(str,d.split())
    if b=='1':
      if h%2==0:
        m+=c
      else:
        u+=c
        
    else:
      if h%2==0:
        u+=c
      else:
        m+=c
        
  else:
    h+=1
    
M=list(m)
G=list(reversed(M))
GG=''.join(G)
s=GG+s+u
if h%2==0:
  print(s)
else:
  f=list(s)
  g=list(reversed(f))
  print(''.join(g))

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

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

    rvflg = False

    d_st = deque()
    d_ed = deque()

    for _ in range(n):
        q = input()
        if len(q) == 1:
            if rvflg:
                rvflg = False
            else:
                rvflg = True
        else:
            i,f,c = map(str,q.split())
            if f == "1":
                if rvflg:
                    
                    d_ed.append(c)
                else:
                    
                    d_st.appendleft(c)
            else:
                if rvflg:
                    
                    d_st.appendleft(c)
                else:
                    
                    d_ed.append(c)

    ans = "".join(d_st) + s + "".join(d_ed)
    
    if rvflg:
        ans = ans[::-1]
    print(ans)

Solution (Python):
if __name__ == '__main__':

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

	rvcnt = 0

	tmp = "S"
	tmp_before = ""
	tmp_after = ""
	
	for _ in range(n):
		q = input()
		if len(q) == 1:
			rvcnt += 1
		else:
			i,f,c = map(str,q.split())
			if f == "1":
				if rvcnt % 2 == 0:
					
					tmp_before = c + tmp_before
				else:
					
					tmp_after = tmp_after + c
			else:
				if rvcnt % 2 == 0:
					
					tmp_after = tmp_after + c
				else:
					
					tmp_before = c + tmp_before


	
	tmp1 = tmp.replace("S",s)
	
	tmp2 = tmp_before + tmp1 + tmp_after

	
	if rvcnt % 2 == 1:
		print(tmp2[::-1])
	else:
		print(tmp2)

Solution (Python):
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
  else:
    if 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))

Solution (Python):
from collections import deque
S=input()
Q=int(input())

ct=0
reverse=[]
string=[deque(''), deque('')]




for i in range(Q):
    l=list(input().split())
    if l==['1']:
        reverse.append(i)
        ct+=1
    elif (int(l[1])+ct)%2==1:
        string[0].appendleft(l[2])
    else:
        string[1].append(l[2])



if ct%2==0:
    top=''.join(string[0])
    bottom=''.join(string[1])
else:
    top=''.join(reversed(string[1]))
    bottom=''.join(reversed(string[0]))
    S=S[::-1]
    
print(top+S+bottom)

Solution (Python):
from collections import deque

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

dt = deque()
de = deque()

f1 = 1
f2 = 2

for _ in range(q):
    q = input().split()
    if int(q[0]) == 1:
        f1,f2 = f2,f1
    elif int(q[0]) == 2:
            if int(q[1]) == f1:
                dt.appendleft(q[2])
            elif int(q[1]) == f2:
                de.append(q[2])
                
st = "".join(dt)
se = "".join(de)
ans = st + s + se

if f1 == 1:
    print(ans)
else:
    ans = list(ans)
    ans.reverse()
    ans = "".join(ans)
    print(ans)

Solution (Python):
s = input()
q = int(input())

f1 = 1
f2 = 2

st = ""
se = ""

for _ in range(q):
    q = input().split()
    if int(q[0]) == 1:
        f1,f2 = f2,f1
    elif int(q[0]) == 2:
            if int(q[1]) == f1:
                st = q[2] + st
            elif int(q[1]) == f2:
                se += q[2]

ans = st + s + se
if f1 == 1:
    print(ans)
else:
    ans = list(ans)
    ans.reverse()
    ans = "".join(ans)
    print(ans)

Solution (Python):
s = input()
q = int(input())

f1 = 1
f2 = 2
cnt = 0
st = ""
se = ""

for _ in range(q):
    q = input().split()
    if int(q[0]) == 1:
        cnt += 1
    elif int(q[0]) == 2:
        if cnt % 2 == 0:
            if int(q[1]) == 1:
                st = q[2] + st
            elif int(q[1]) == 2:
                se += q[2]
        else:
            if int(q[1]) == 1:
                se += q[2]
            elif int(q[1]) == 2:
                st = q[2] + st


ans = st + s + se
if cnt % 2 == 0:
    print(ans)
else:
    ans = list(ans)
    ans.reverse()
    ans = "".join(ans)
    print(ans)

Solution (Python):
from collections import deque
S=deque(input())
Q=int(input())
count = 0
for i in range(Q):
    q = list(input().split())
    
    if q[0] == "1":
        count += 1
    else:
        if count%2 == 0:
            if q[1] == "1":
                S.appendleft(q[2])
            else:
                S.append(q[2])
        else:
            if q[1] == "1":
                S.append(q[2])
            else:
                S.appendleft(q[2])
if count%2 != 0:
    S = reversed(S)
print("".join(S))

Solution (Python):
ini = lambda : int(input())
inm = lambda : map(int,input().split())
inl = lambda : list(map(int,input().split()))
gcd = lambda x,y : gcd(y,x%y) if x%y else y
def factorization(n):
    arr = []
    temp = n
    for i in range(2, int(-(-n**0.5//1))+1):
        if temp%i==0:
            cnt=0
            while temp%i==0:
                cnt+=1
                temp //= i
            arr.append([i, cnt])
    if temp!=1:
        arr.append([temp, 1])
    if arr==[]:
        arr.append([n, 1])
    return arr
def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)
    divisors.sort()
    return divisors


s = input()
q = ini()
mae = ''
bac = s
for i in range(q):
    a = input()
    if a[0] == '1':
        temp = mae
        mae = bac
        bac = temp
    else:
        if a[2] == '1':
            mae = mae + a[4]
        else:
            bac = bac + a[4]
mae = mae[::-1]
print(mae + bac)

Solution (Python):
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
    else:
        if (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))

Solution (Python):
import os
import sys
from io import BytesIO, IOBase
from collections import deque as dq

BUFSIZE = 8192


class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)


class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")


sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")

from collections import deque as dq
L=dq(list(input()))
C=0
for _ in range(int(input())):
    S=input().split()
    if S == ['1']:
        C+=1
    elif C%2==0:
        if S[1]=='1':
        	L.appendleft(S[2])
        else:
        	L.append(S[2])
    elif C%2!=0:
        if S[1]=='1':
        	L.append(S[2])
        else:
        	L.appendleft(S[2])
if C%2!=0:
    L.reverse()
print(''.join(L))

Solution (Python):
s = list(input())
q = int(input())
l1 = []
l2 = []
rev = False
for i in range(q):
    inp = input()
    if inp[0] == "1":
        if rev:
            rev = False
        else:
            rev = True
    elif inp[2] == "1":
        a = inp[4]
        if rev:
            l2.append(a)
        else:
            l1.append(a)
    else:
        a = inp[4]
        if rev:
            l1.append(a)
        else:
            l2.append(a)
l1.reverse()
ans = l1 + s + l2
if rev:
    ans.reverse()
print("".join(ans))

Solution (Python):
s = input()
n = int(input())
c = 0
left_l = ['', '']
right_l = ['', '']
s_l = [s, s[::-1]]
for _ in range(n):
  lst = [i for i in input().split()]
  if lst[0] == '1':
    if c == 1:
      s_l[0], s_l[1] = s_l[1], s_l[0]
      
    else:
      s_l = [right_l[1] + s_l[1] + left_l[1], left_l[0] + s_l[0] + right_l[0]]
      left_l[0], left_l[1] = '', ''
      right_l[0], right_l[1] = '', ''
      c = 1
      
  elif lst[0] == '2':
    c = 0
    if lst[1] == '1':
      left_l[0] = lst[2] + left_l[0]
      left_l[1] = left_l[1] + lst[2]
    elif lst[1] == '2':
      right_l[0] = right_l[0] + lst[2]
      right_l[1] = lst[2] + right_l[1]
w = left_l[0] + s_l[0] + right_l[0]
print(w)

Solution (Python):
S = input()
Q = int(input())
flip = 0
head = []
tail = []
for i in range(Q):
  Query = input().split()
  if len(Query) == 3:
    T,F,C = Query
    F = int(F)-1
    if F^flip == 1:
      tail.append(C)
    else:
      head.append(C)
  else:
    flip ^= 1
if flip:
  head, tail = tail, head
  S = S[::-1]
head = ''.join(head[::-1])
tail = ''.join(tail)
print(head+S+tail)

Solution (Python):
s= input();
q = int(input());
flag = 0;
fornt = "";
back = "";
while(q>0):
    q-=1;
    ls = list(map(str,input().split(" ")));
    if(ls[0]=='1'):
        flag +=1;
    else:
        if(flag%2 == 1):
            if(ls[1]=='2'):
                fornt=ls[2]+fornt;
            else:
                back=back+ls[2];
        else:
            
            if(ls[1]=='1'):
                fornt=ls[2]+fornt;
            else:
                back=back+ls[2];
s = fornt+s+back
if(flag%2==0):
    print(s);
else:
    print(s[::-1])

Solution (Python):
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])

Solution (Python):
def main():
    S=input()
    Q=int(input())
    Left=[]
    Right=[]

    reverse_flag=0
    for i in range(Q):
        query=input().split()
        if query[0]=="1":
            if reverse_flag==1:
                reverse_flag=0
            else:
                reverse_flag=1
        else:
            if query[1]=="1":
                if reverse_flag==0:
                    Left.append(query[2])
                else:
                    Right.append(query[2])
            else:
                if reverse_flag==0:
                    Right.append(query[2])
                else:
                    Left.append(query[2])

    res=""
    if reverse_flag==0:
        Left.reverse()
        res+="".join(Left)
        res+=S
        res+="".join(Right)

    else:
        Right.reverse()
        res+="".join(Right)
        S=list(S)
        S.reverse()
        res+="".join(S)
        res+="".join(Left)
    print(res)

if __name__=="__main__":
    main()

Solution (Python):
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)

Solution (Python):
S = str(input())
Q = int(input())
flip = 0
front,back = '',''
for i in range(Q):
  j = input()
  if len(j) == 1:
    flip += 1
    continue
  else:
    if j[2] == '1' and flip % 2 == 0:
      front = j[4] + front
      continue
    elif j[2] == '2' and flip % 2 == 0:
      back = back + j[4]
      continue
    elif j[2] == '1' and flip % 2 ==1:
      back= back + j[4]
      continue
    elif j[2] == '2' and flip % 2 ==1:
      front = j[4] + front
      continue
S = front + S + back
if flip % 2 == 1:
  S = S[::-1]
print(S)