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):
S = input()
Q = int(input())
Query = [input().split() for _ in range(Q)]

muki = 1
a = ''
b = ''
for i in Query:
    if i[0] == '1':
        muki *= -1
    else:
        if i[1] == '1':
            if muki == 1:
                a += i[2]
                
            else:
                
                b += i[2]
        else:
            if muki == -1:
                
                a += i[2]
            else:
                
                b += i[2]
S = a[::-1] + S + b
if muki == -1:
    print(S[::-1])
else:
    print(S)

Solution (Python):

import sys

def INT(): return int(input())
def MAP(): return map(int,input().split())
def LI(): return list(map(int,input().split()))

from collections import deque
def main():
    S = input()
    queue = deque(list(S))
    Q = INT()
    reverse_flag = False

    for _ in range(Q):
        query = list(input().split())
        if len(query) == 3:
            F = int(query[1])
            C = query[2]
            if F == 1:
                if reverse_flag:
                    queue.append(C)
                else:
                    queue.appendleft(C)
            elif F == 2:
                if reverse_flag:
                    queue .appendleft(C)
                else:
                    queue.append(C)
        else:
            if reverse_flag:
                reverse_flag = False
            else:
                reverse_flag = True
    answer = ''.join(queue)
    if reverse_flag:
        answer = answer[::-1]
    print(answer)
    return
        
if __name__ == '__main__':
    main()

Solution (Python):
S = input()
Q = int(input())
Query = [input() for _ in range(Q)]

is_reversed = False
prefix = ""
suffix = ""

for query in Query:
    if query == "1":
        is_reversed = not is_reversed
    else:
        _, f, c = query.split()
        if is_reversed ^ (f == "1"):
            prefix += c
        else:
            suffix += c

if is_reversed:
    print(suffix[::-1] + S[::-1] + prefix)
else:
    print(prefix[::-1] + S + suffix)

Solution (Python):
from collections import deque

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


rev=0
for i in range(q):
  l=list(input().split())
  if len(l)==1:

    rev+=1
  else:
    t,f,c=l[0],l[1],l[2]

    if rev%2==0:
      if f=='1':
        s.appendleft(c)
      else:
        s.append(c)
    else:
      if f=='1':
        s.append(c)
      else:
        s.appendleft(c)

s=list(s)
if rev%2==1:
  s.reverse()
print(''.join(s))

Solution (Python):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
from collections import deque

S = deque(readline().rstrip().decode())
Q = int(readline())
query = readlines()

rev = False
for q in query:
    if q[0] == ord('1'):
        rev = not rev
    else:
        _, f, c = q.decode().split()
        f = int(f)
        if rev ^ (f == 1):
            S.appendleft(c)
        else:
            S.append(c)

T = ''.join(S)
if rev:
    T = T[::-1]
print(T)

Solution (Python):
S = input()
Q = int(input())

is_reverse = False
sento = []
matubi = []
for i in range(Q):
    s = input().split()
    if s[0] == '1':
        is_reverse = not is_reverse
    else:
        if (s[1] == '1' and not is_reverse) or (s[1] == '2' and is_reverse):
            sento.append(s[2])
        else:
            matubi.append(s[2])

ans = ''.join(sento[::-1]) + S + ''.join(matubi)
if is_reverse:
    ans = ans[::-1]
print(ans)

Solution (Python):
s = input()
q = int(input())
appendix = [[], []]
reverse = False
for _ in range(q):
  query = input().split()
  if query[0] == '1':
    reverse ^= True
  else:
    f = bool(int(query[1]) - 1)
    appendix[int(f ^ reverse)].append(query[2])
if reverse:
  print(''.join(appendix[1][::-1]) + s[::-1] + ''.join(appendix[0]))
else:
  print(''.join(appendix[0][::-1]) + s + ''.join(appendix[1]))

Solution (Python):
S=input()
Q=int(input())
mode=0
sento=''
matubi=''
for i in range(Q):
  query=list(input().split())
  if query[0]=='1':
    mode=(mode+1)%2
  if query[0]=='2':
    if query[1]=='1':
      if mode==0:
        sento=query[2]+sento
      else:
        matubi=matubi+query[2]
    else:
      if mode==0:
        matubi=matubi+query[2]
      else:
        sento=query[2]+sento
if mode==1:
  S=matubi[::-1]+S[::-1]+sento[::-1]
else:
  S=sento+S+matubi
print(S)

Solution (Python):
'''
　 ∧_,,∧    究極奥義「WAがACになーれ！！」
　（`・ω・)つ━☆・*。
　⊂　　 ノ 　　　・゜+.
　 し’´Ｊ　　*・ 
'''
s=input()
a=[]
b=[]
now=1
q=int(input())
for i in range(q):
    x=list(input().split())
    if x[0]=="1":
        now*=-1
    else:
        if now==1 and x[1]=="2" or now==-1 and x[1]=="1":
            b.append(x[2])
        else:
            a.append(x[2])
if now==-1:
    ans=b[::-1]+list(s[::-1])+a
    print(*ans,sep="")
else:
    ans=a[::-1]+list(s)+b
    print(*ans,sep="")

Solution (Python):
def d_string_formation():
    from collections import deque
    S = input()
    Q = int(input())
    Query = [input().split() for _ in range(Q)]

    ans = deque(list(S))
    turn = False
    for q in Query:
        if q[0] == '1':
            turn = not turn
        else:
            _, f, c = q
            if f == '1':
                if turn:
                    ans.append(c)
                else:
                    ans.appendleft(c)
            else:
                if turn:
                    ans.appendleft(c)
                else:
                    ans.append(c)
    return ''.join(ans) if not turn else ''.join(reversed(ans))

print(d_string_formation())

Solution (Python):
S = list(input())
Q = int(input())
mae = []
usi = []
R = 0
for _ in range(Q):
    q = list(map(str,input().split()))
    if q[0] == '1':
        R += 1
        R %= 2
    elif q[0] == '2':
        if R == 0:
            if q[1] == '1':
                mae.append(q[2])
            elif q[1] == '2':
                usi.append(q[2])
        elif R == 1:
            if q[1] == '1':
                usi.append(q[2])
            elif q[1] == '2':
                mae.append(q[2])
mae.reverse()
ans = mae + S + usi
if R == 1:
    ans.reverse()
print(''.join(ans))

Solution (Python):
S=input()
Q=int(input()) 






rev=False
head=[] 
tail=[] 

for _ in range(Q):
    query = input().split()

    if query[0] == "1":
        
        rev = not rev
    else:
        
        F = query[1]
        char = query[2]
        if F == "1":
            
            if rev:
                
                tail.append(char)
            else:
                head.append(char)
        else:
            
            if rev:
                
                head.append(char)
            else:
                tail.append(char)

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

if rev:
    
    print(tail_str[::-1]+S[::-1]+head_str)
else:
    print(head_str[::-1]+S+tail_str)

Solution (Python):
S = input()
Q = int(input())

tep = 1
top = ""
bottom = ""
for t in range(Q):
    query = [i for i in input().split()]
    if query[0]=="1":
       tep =tep * (-1) 
    if query[0]=="2":
        F = int(query[1])
        C = query[2]
        if F==1:
            if tep ==1:
                top = C + top
            else:
                bottom = bottom + C
        if F==2:
            if tep ==-1:
                top = C + top
            else:
                bottom = bottom + C


ans = top + S + bottom
if tep==1:
    print(ans)

else:
    print(ans[::-1])

Solution (Python):
printn = lambda x: print(x,end='')
inn = lambda : int(input())
inl   = lambda: list(map(int, input().split()))
inm   = lambda:      map(int, input().split())
ins = lambda : input().strip()
DBG = True 
BIG = 10**18
R = 10**9 + 7

def ddprint(x):
  if DBG:
    print(x)

from collections import deque
ss = ins()
s = deque([ss[i] for i in range(len(ss))])
dir = 0
q = inn()
for qq in range(q):
    a = input().split()
    if a[0]=='1':
        dir = 1-dir
    else:
        f = a[1]
        c = a[2]
        if f=='1' and dir==0 or f=='2' and dir==1:
            s.appendleft(c)
        else:
            s.append(c)
    
if dir==1:
    s.reverse()
print(''.join(s))

Solution (Python):
import sys

from collections import deque

def main():
    s = list(input())
    s = deque(s)
    q = int(input())
    count,b = 0,1
    for _ in range(q):
        x = list(input().split())
        if x[0] == "1":
            b *= -1
            count += 1
        else:
            if x[1] == "1":
                if b == 1:
                    s.appendleft(x[2])
                else:
                    s.append(x[2])
            else:
                if b == 1:
                    s.append(x[2])
                else:
                    s.appendleft(x[2])
    
    if count%2 != 0:
        s.reverse()
        
    print("".join(s))

if __name__ == "__main__":
    main()

Solution (Python):
from collections import deque

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

t = deque(S)
reverse = False
for _ in range(Q):
    query = input()
    if query[0] == '1':
        reverse = not reverse
    elif query[0] == '2':
        _, F, C = query.split()
        if F == '1':
            if reverse:
                t.append(C)
            else:
                t.appendleft(C)
        elif F == '2':
            if reverse:
                t.appendleft(C)
            else:
                t.append(C)
result = ''.join(t)
if reverse:
    result = result[::-1]
print(result)

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

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

d=deque(S)
rev=False
for q in range(Q):
  q=input()
  if q=="1":
    rev=not rev
  else:
    qlist=q.split()
    if qlist[1]=="1":
      if not rev:
        d.appendleft(qlist[2])
      else:
        d.append(qlist[2])
    elif qlist[1]=="2":
      if not rev:
        d.append(qlist[2])
      else:
        d.appendleft(qlist[2])

d2=deque()
if not rev:
  d2=deque(d)
else:
  d2=deque(reversed(d))
  
print("".join(d2))

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

Solution (Python):
from collections import deque

def resolve():
    S = deque(input())
    Q = int(input())
    query = [ input().split() for _ in range(Q)]

    sw = True
    for q in query:
        if q[0] == "2":
            if q[1] == "1":
                if sw:
                    S.appendleft(q[2])
                else:
                    S.append(q[2])
            else:
                if sw:
                    S.append(q[2])
                else:
                    S.appendleft(q[2])
        else:
            sw = not sw
    
    if not sw:
        S.reverse()
    print("".join(S))

if __name__ == "__main__":
    resolve()

Solution (Python):
S=input()
Q=int(input())
inv = 0
s0 = ""
s1 = ""
for i in range(Q):
  q = input()
  if q[0] == "1":
    if inv == 0: inv = 1
    else: inv = 0
  else:
    q,f,c = q.split()
    if f == "1":
      if inv == 0:
        s0 += c
      else:
        s1 += c
    else:
      if inv == 1:
        s0 += c
      else:
        s1 += c
if inv == 1:
  S = s1[::-1] + S[::-1] + s0
else:
  S = s0[::-1] + S + s1

print(S)

Solution (Python):
def solve(s, q, query_list):
    flag = 1
    add_right = []
    add_left = []
    for i in range(q):
        query = query_list[i]
        if len(query) == 1:
            flag *= -1
        else:
            if query[1] == "1":
                if flag == 1:
                    add_left.append(query[2])
                else:
                    add_right.append(query[2])
            else:
                if flag == 1:
                    add_right.append(query[2])
                else:
                    add_left.append(query[2])
    s = "".join(list(reversed(add_left))) + s + "".join(add_right)
    
    if flag == 1:
        return s
    else:
        return "".join(list(reversed(s)))


def main():
    s = input()
    q = int(input())
    query_list = [list(input().split()) for _ in range(q)]
    res = solve(s, q, query_list)
    print(res)


def test():
    assert solve("a", 4, [["2", "1", "p"], ["1"], ["2", "2", "c"], ["1"]]) == "cpa"
    assert solve("a", 6, [["2", "2", "a"], ["2", "1", "b"], ["1"], ["2", "2", "c"], ["1"], ["1"]]) == "aabc"
    assert solve("y", 1, [["2", "1", "x"]]) == "xy"


if __name__ == "__main__":
    test()
    main()

Solution (Python):
s = input()
q = int(input())
query = [input().split() for _ in range(q)]

add_texts = ["", ""]
head = 0
for q_i in query:
    if q_i[0] == "1":
        head = ~head
    else:
        f, c = q_i[1:]
        if f == "1":
            add_texts[head] += c
        else:
            add_texts[~head] += c

text = add_texts[0][::-1] + s + add_texts[1]
print(text[::[1, -1][head]])

Solution (Python):


from collections import deque


def submit():
    s = input()

    
    curr = deque()
    for c in s:
        curr.append(c)

    
    front = 1

    
    q = int(input())
    for _ in range(q):
        query = input()
        if query[0] == '1':
            front *= -1
        else:
            _, f, c = query.split()
            if f == '1': 
                if front == 1: 
                    curr.appendleft(c)
                else:
                    curr.append(c)
            else:   
                if front == 1: 
                    curr.append(c)
                else:
                    curr.appendleft(c)

    
    results = ""
    while len(curr) > 0:
        if front == 1:
            results += curr.popleft()
        else:
            results += curr.pop()

    print(results)
        


if __name__ == "__main__":
    submit()

Solution (Python):
S=input()
Q=int(input())
Rev=False
a=[]
b=[]
for _ in range(Q):
    q=list(input().split())
    if len(q)==1:
        Rev=not Rev
    else:
        if q[1]=='1':
            if not Rev:
                a.append(q[2])
            else:
                b.append(q[2])
        else:
            if Rev:
                a.append(q[2])
            else:
                b.append(q[2])
S=''.join(a)[::-1]+S+''.join(b)
if Rev:
    S=S[::-1]
print(S)

Solution (Python):
S = input()
N = int(input())

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

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

Solution (Python):
from collections import deque
aa = list(map(str, input()))
S = deque()
for a in aa:
	S.append(a)
Q = int(input())
ope = []

for _ in range(Q):
	a = list(map(str, input().split()))
	if len(a) == 1:
		ope.append(a)
	else:
		ope.append(a)
	

std = True 

for i in range(Q):
	if ope[i][0] == "1":
		std = not std
	else:
		if ope[i][1] == "1": 
			if std: S.appendleft(ope[i][2])
			else: S.append(ope[i][2])
		else: 
			if std: S.append(ope[i][2])
			else: S.appendleft(ope[i][2])
		
if std:		
	for s in S:
		print(s, end = "")
else:
	for s in reversed(S):
		print(s, end = "")

Solution (Python):
import copy

import math
import fractions

def j(n):
    if n:print("Yes")
    else:print("No")
    exit(0)
rem = 10 ** 9 + 7

"""
def ct(x,y):
    if (x>y):print("+")
    elif (x<y): print("-")
    else: print("?")
"""

def ip():
    return int(input())
def iprow():
    return [int(i) for i in input().split()]
def ips():
    return (int(i) for i in input().split())
def ipmultiplerow(n):
    a = []
    for i in range(n):
        a.append(ip())
    return a
def printrow(a):
    for i in a:
        print(i)

def lcm(x, y):
    return (x * y) // fractions.gcd(x, y)

s = input()
n = ip()
addedleft = []
addedright = []
flip = 0
for i in range(n):
    cmd = input().split()
    
    if len(cmd) == 1:
        flip+=1
        flip%=2
    else:
        if cmd[1] == "1":
            if flip == 0: addedleft.append(cmd[2])
            else: addedright.append(cmd[2])
        else: 
            if flip == 0: addedright.append(cmd[2])
            else: addedleft.append(cmd[2])


addedleft.reverse()
r = ''.join(addedright)
l = ''.join(addedleft)
ans  = ''.join([l,s,r])
if flip==0:
    print(ans)
else:
    for i in range(len(ans)):
        print(ans[-i-1],end='')

Solution (Python):


import sys
from collections import deque

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 18
MOD = 10 ** 9 + 7

S = input()

que = deque(S)
is_rev = 0
for _ in range(INT()):
    q = input().split()
    if q[0] == '1':
        is_rev = 1 - is_rev
    else:
        _, a, c = q
        a = int(a)
        if (a == 1) ^ (not is_rev):
            que.append(c)
        else:
            que.appendleft(c)
ans = ''.join(que)
if is_rev:
    ans = ans[::-1]
print(ans)

Solution (Python):
from collections import deque

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

reverseFlag = 0

for _ in range(q):
    query = input().strip()
    if query[0] == "1":
        reverseFlag = 1 - reverseFlag
    else:
        lf = reverseFlag + int(query[2])
        if lf % 2 == 0:
            s.append(query[4])
        else:
            s.appendleft(query[4])

if reverseFlag == 1:
    s.reverse()
print("".join(s))

Solution (Python):
import sys
from collections import deque
sys.setrecursionlimit(10 ** 7)

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

S = deque(input())

Q = int(input())
TURN = False

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

    _, f, c = q.split()

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

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


if TURN:
    S.reverse()

print(''.join(S))

Solution (Python):
s = input()
q = int(input())
reverse_now = 0
first = ""
latter = ""
for i in range(q):
    k = list(input().split())
    if k[0] == "1":
        reverse_now = 1 - reverse_now
    else:
        f,c = k[1], k[2]
        if f == "1" and reverse_now == 0:
            first = c + first
        if f == "1" and reverse_now == 1:
            latter += c
        if f == "2" and reverse_now == 0:
            latter +=  c
        if f == "2" and reverse_now == 1:
            first = c + first
if reverse_now == 1:
    ans = list(first + s + latter)
    ans.reverse()
    ans = "".join(ans)
else:
    ans = first + s + latter
print(ans)

Solution (Python):
from collections import deque
s = input()
s = deque(list(s))
q = int(input())
inv=False
for _ in range(q):
    query = input()
    if query == '1':
        inv=not(inv)
    else:
        _q,f,c = query.split()
        if inv:
            if f=='2':
                s.appendleft(c)
            else:
                s.append(c)
        else:
            if f=='1':
                s.appendleft(c)
            else:
                s.append(c)

if inv:
    print(''.join(list(s)[::-1]))
else:
    print(''.join(list(s)))

Solution (Python):
s = input()
back = ""
q = int(input())
rev = False
for i in range(q):
    inpt = input().split( )
    if len(inpt)>1:
        fi,ci = inpt[1],inpt[2]
        if not ((fi == "1") ^ rev):
            s += ci
        else:
            back += ci
    else:
        rev = not rev
back = back[::-1]
ans = back+s
if not rev:
    print(ans)
else:
    ans = ans[::-1]
    print(ans)

Solution (Python):
def main():
    import numpy as numpy
    import sys
    from collections import deque

    S=sys.stdin.readline().strip()
    Q=int(sys.stdin.readline())
    rv=False
    S=deque(S)
    for i in range(Q):
        qs=sys.stdin.readline().split()
        if int(qs[0])==1:
            rv=(rv+1)%2
        elif int(qs[0])==2:
            if (int(qs[1])-1+rv)%2==0:
                S.appendleft(qs[2])
            elif (int(qs[1])-1+rv)%2==1:
                S.append(qs[2])
            else:
                print('error')
                return
        else:
            print('error')
            return
    
    if rv:
        S=reversed(list(S))
    else:
        S=list(S)

    S="".join(S)
    
    return S

print(main())

Solution (Python):
from collections import deque

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

inverted = False

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

if inverted:
  print("".join(s_queue)[::-1])
else:
  print("".join(s_queue))

Solution (Python):
from collections import deque
s=input()
q=int(input())


check=True
d=deque(list(s))
for i in range(q):
    x=input().split()
    if len(x)==1:
        check=(not check)
    else:
        t,f,c=x
        f=int(f)
        if check:
            if f==1:
                d.appendleft(c)
                
            else:
                d.append(c)
                
        else:
            if f==2:
                d.appendleft(c)
                
            else:
                d.append(c)
                

if check:
    print("".join(list(d)))
else:
    print("".join(list(d)[::-1]))

Solution (Python):
import sys
import collections

sys.setrecursionlimit(10 ** 8)

input = sys.stdin.readline


def main():
    S = input().strip()
    Q = int(input())

    rev = 0
    sdeq = collections.deque()
    for s in S:
        sdeq.append(ord(s) - ord('a'))

    for i in range(Q):
        A = [x for x in input().split()]
        if len(A) == 1:
            rev = (rev + 1) % 2
        else:
            f = int(A[1])
            c = A[2]
            if f == 1:
                if rev == 0:
                    sdeq.appendleft(ord(c) - ord('a'))
                else:
                    sdeq.append(ord(c) - ord('a'))
            else:
                if rev == 0:
                    sdeq.append(ord(c) - ord('a'))
                else:
                    sdeq.appendleft(ord(c) - ord('a'))

    if rev == 0:
        for i in list(sdeq):
            print(chr(i + ord('a')), end="")
    else:
        for i in list(sdeq)[::-1]:
            print(chr(i + ord('a')), end="")
    print()



if __name__ == '__main__':
    main()

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

Solution (Python):
import collections

def solve(S, Q, queries):
    S = collections.deque(S)
    is_reversed = False
    for q in queries:
        if q[0] == "1":
            is_reversed = not is_reversed
        else:
            _, f, c = tuple(q.split(" "))

            if is_reversed:
                f = "2" if f == "1" else "1"

            if f == "1":
                S.appendleft(c)
            else:
                S.append(c)

    if is_reversed:
        S = reversed(S)

    return "".join(S)

if __name__ == "__main__":
    S = input()
    Q = int(input())
    queries = []
    for _ in range(Q):
        queries.append(input())

    print(solve(S, Q, queries))

Solution (Python):
S = input()
r = False
L = []
R = []
for _ in range(int(input())):
    q = input().split()
    if q[0] == "1":
        r = not r
    else:
        if q[1] == "1":
            if r:
                R.append(q[2])
            else:
                L.append(q[2])
        else:
            if r:
                L.append(q[2])
            else:
                R.append(q[2])


if r:
    R.reverse()
    print("".join(R + list(S[::-1]) + L))
else:
    L.reverse()
    print("".join(L + [S] + R))

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

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

cur=True
front=""
back=""
for i in range(Q):
  q=list(readline().rstrip().split())
  if q[0]=="1":
    cur^=True
  elif q[0]=="2":
    f,c=q[1],q[2]
    if f=="1":
      if cur:
        front+=c
      else:
        back+=c
    elif f=="2":
      if cur:
        back+=c
      else:
        front+=c
front=front[::-1]
ans=front+S+back
if not cur:
  ans=ans[::-1]
  
print(ans)

Solution (Python):
s = list(input())
q = int(input())
Q = [list(input().split()) for i in range(q)]

A = [[], []] 
a = 0
for i in range(q):
    b = int(Q[i][0])
    if b == 1:
        a = (a+1) % 2
    else:
        f = int(Q[i][1])
        c = Q[i][2]
        A[(f-1+a)%2].append(c)


ANS = [A[0][i] for i in range(len(A[0])-1,-1,-1)] + s + [A[1][i] for i in range(len(A[1]))]
if a == 0:
    print("".join(ANS))
else:
    print("".join(ANS[::-1]))

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

for _ in range(Q):
  q=input().split()
  if q[0]=="1":
    rev*=-1
  else:
    F,C=int(q[1]),q[2]
    if F==1:
      if rev==1:
        S.appendleft(C)
      else:
        S.append(C)
    else:
      if rev==1:
        S.append(C)
      else:
        S.appendleft(C)

if rev==-1:
  S.reverse()
print("".join(list(S)))

Solution (Python):
import sys
input = sys.stdin.readline
s = input().rstrip('\n')
q = int(input())
s1 = []
s2 = []
k = 0
for i in range(q):
    r = input().rstrip("\n")
    if r == "1":
        k = (k + 1) % 2
    else:
        _, f, c = r.split()
        f = int(f) - 1
        if (k + f) % 2:
            s2.append(c)
        else:
            s1.append(c)

s1.reverse()
ans = s1 + list(s) + s2

if k:
    ans.reverse()
print("".join(ans))

Solution (Python):
from collections import deque
s = list(input())
q = int(input())
d = deque()
d.append(0)
now = 1
for i in range(q):
    q1 = list(input())
    if len(q1) == 1:
        now *= -1
    else:
        if q1[2] == "1":
            if now == 1:
                d.appendleft(q1[4])
            else:
                d.append(q1[4])
        else:
            if now == 1:
                d.append(q1[4])
            else:
                d.appendleft(q1[4])
ans = ""

while len(d):
    if now == 1:
        a = d.popleft()
    else:
        a = d.pop()
    if a == 0:
        if now == 1:
            for i in range(len(s)):
                ans += s[i]
        else:
            for i in range(len(s)):
                ans += s[-(i+1)]
    else:
        ans += a
print(ans)

Solution (Python):
import sys
input=sys.stdin.readline
s=input().rstrip()
q=int(input())
count=0
pre=[]
post=[]
for i in range(q):
    l=input().split()
    if len(l)==1:
        count+=1
    else:
        f=int(l[1])
        c=l[2]
        if count%2==0:
            if f==1:
                pre.append(c)
            else:
                post.append(c)
        else:
            if f==1:
                post.append(c)
            else:
                pre.append(c)
if count%2==0:
    pre.reverse()
    for i in pre:
        print(i,end='')
    print(s,end='')
    for i in post:
        print(i,end='')
else:
    post.reverse()
    for i in post:
        print(i,end='')
    n=len(s)
    for i in range(n-1,-1,-1):
        print(s[i],end='')
    for i in pre:
        print(i,end='')

Solution (Python):
from itertools import accumulate, permutations, combinations, product, combinations_with_replacement, groupby
from math import sqrt, factorial, log
from bisect import bisect_left, bisect_right
from collections import Counter, defaultdict, deque
from heapq import heappop, heappush, heappushpop
import sys
stdin = sys.stdin
mod = 10**9 + 7

def ns(): return stdin.readline().rstrip()
def ni(): return int(ns())
def na(): return list(map(int, stdin.readline().split()))

s = deque(ns())
q = ni()
now = 1
for i in range(q):
    qu = ns().split()
    if qu[0] == "1":
        now *= -1
    else:
        if qu[1] == "1":
            r = 1
        else:
            r = -1
        if r * now == 1:
            s.appendleft(qu[-1])
        else:
            s.append(qu[-1])
if now == -1:
    s.reverse()
print(*s, sep="")

Solution (Python):
from collections import deque

import sys
input = sys.stdin.readline

S = input().strip()

is_front = True 
queue = deque(S)

Q = int(input())
for _ in range(Q):
    query = input().strip().split()
    if len(query) == 1:
        is_front = not is_front
    else:
        ti, fi, ci = query
        fi = int(fi)
        if is_front:
            if fi == 1:
                queue.appendleft(ci)
            else:
                queue.append(ci)
        else:
            if fi == 1:
                queue.append(ci)
            else:
                queue.appendleft(ci)

ans = "".join(queue)
if not is_front:
    ans = ans[::-1]
print(ans)