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()))
n = int(input())
f = True

for i in range(n):
    a = input()
    if a == "1":
        f = not f
    else:
        b,c,d = a.split()
        if f:
            if c == "1":
                s.appendleft(d)
            else:
                s.append(d)
        else:
            if c == "1":
                s.append(d)
            else:
                s.appendleft(d)

if not f:
    s.reverse()

ans = "".join(s)
print(ans)

Solution (Python):

from collections import deque


def main():
    S = input().rstrip()
    Q = int(input())
    queue = deque(S)
    to_beginning, to_end = queue.appendleft, queue.append
    is_reversed = 0
    for _ in range(Q):
        query = input().split()
        if query[0] == "1":
            is_reversed ^= 1
            continue
        com, c = query[1], query[2]
        if com == "1":
            if not is_reversed:
                to_beginning(c)
            else:
                to_end(c)
        else:
            if not is_reversed:
                to_end(c)
            else:
                to_beginning(c)
    ans = "".join(queue) if not is_reversed else "".join(reversed(queue))
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):

from collections import deque


def main():
    S = input().rstrip()
    Q = int(input())
    queue = deque(S)
    to_beginning, to_end = queue.appendleft, queue.append
    is_reversed = 0
    for _ in range(Q):
        query = input().split()
        if query[0] == "1":
            to_beginning, to_end = to_end, to_beginning
            is_reversed ^= 1
            continue
        com, c = query[1], query[2]
        if com == "1":
            to_beginning(c)
        else:
            to_end(c)
    ans = "".join(queue) if not is_reversed else "".join(reversed(queue))
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
def main():
  from collections import deque
  s=deque(input())
  q=int(input())
  count=0
  for _ in [0]*q:
    a=list(input().split())
    if len(a)==1:
      count+=1
    else:
      if count%2==0:
        if a[1]=='1':
           s.appendleft(a[2])
        else:
          s.append(a[2])
      else:
        if a[1]=='1':
          s.append(a[2])
        else:
          l=(a[2],s)
          s.appendleft(a[2])
  if count%2==1:
    s=list(s)[::-1]
  print(''.join(s))
if __name__=='__main__':
  main()

Solution (Python):
S = input()
Q = int(input())
pre_rev, 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_rev = pre_rev + c
        else: suf = suf + c
print(pre_rev[::-1] + S + suf if not rev else (S + suf)[::-1] + pre_rev)

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

fflag = 0
ato = ""
mae = ""
for i in range(q):
  qi = input()
  if( qi == "1"):
    fflag += 1 
  else:
    if( fflag%2 == 0 ):
      if( qi[2] == "2" ):
        ato += qi[4]
      else:
        mae += qi[4]
    else:
      if( qi[2] == "2" ):
        mae += qi[4]
      else:
        ato += qi[4]      
    
a = mae[::-1] + a + ato    
    
if( fflag%2==0 ):
  print( a )
else:
  print( a[::-1] )

Solution (Python):
from collections import deque

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

flg = 1
for _ in range(q):
    t = input()
    if t == "1":
        flg = not flg
    elif t[0] == "2":
        f = t[2]
        if f == "1":
            c = t[4]
            if flg:
                s.appendleft(c)
            else:
                s.append(c)
        elif f == "2":
            c = t[4]
            if flg:
                s.append(c)
            else:
                s.appendleft(c)

if flg:
    print(''.join(s))
else:
    print(''.join(reversed(s)))

Solution (Python):
from collections import deque
s = input()
q = int(input())
ans = deque(s)
rev = 0
for i in range(q):
    qi = input()
    if qi[0] == '1':
        rev += 1
    else:
        if qi[2] == '1' and rev % 2 == 0:
            ans.appendleft(qi[4])
        elif qi[2] == '2' and rev % 2 == 0:
            ans.append(qi[4])
        if qi[2] == '1' and rev % 2 == 1:
            ans.append(qi[4])
        elif qi[2] == '2' and rev % 2 == 1:
            ans.appendleft(qi[4])

parity = rev % 2
if parity:
    ans.reverse()
    for i in ans:
        print(i, end='')
else:
    for i in ans:
        print(i, end='')

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

Solution (Python):
S = list(input())
Q = int(input())
rev = False
F = []

for i in range(Q):
    X = list(input().split(" "))

    if X[0] == "1" and rev is False:
        rev = True
    elif X[0] == "1" and rev is True:
        rev = False
    elif X[0] == "2" and X[1] == "1" and rev is False:
        F.append(X[2])
    elif X[0] == "2" and X[1] == "1" and rev is True:
        S.append(X[2])
    elif X[0] == "2" and X[1] == "2" and rev is False:
        S.append(X[2])
    elif X[0] == "2" and X[1] == "2" and rev is True:
        F.append(X[2])

F.reverse()
F.extend(S)

if rev is True:
    F.reverse()

print(*F, sep="")

Solution (Python):
s = input()
n = int(input())
rev = 0
rev_check = False
first = list()
last = list()
for i in range(n):
    q = list(input().split())
    if q[0] == "1":
        rev += 1
        rev_check = not rev_check
    else:
        if rev_check ==  True:
            if q[1] == "1":
                last.append(q[2])
            else:
                first.append(q[2])
        else:
            if q[1] == "1":
                first.append(q[2])
            else:
                last.append(q[2])
if rev_check == False:
    print("".join(reversed(first)) + s + "".join(last))
else:
    print("".join(reversed(last)) + s[::-1] + "".join(first))

Solution (Python):
s = input()
q = int(input())
inv = False
left =''
right = ''

for i in range(1, q+1):
    try:
        t, f, c = input().split()
    except ValueError:
        t = '1'
    if t == '1':
        inv = not inv
    else:
        if inv:
            if f == '1':
                right = right + c
            else:
                left = c + left
        else:
            if f == '1':
                left = c + left
            else:
                right = right + c




if inv:
    right = right[::-1]
    left = left[::-1]
    s = s[::-1]
    print(right + s + left)
else:
    print(left + s + right)

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

Solution (Python):
import sys
S = sys.stdin.readline().rstrip()
L = []
R = []
rev = 0
for _ in range(int(sys.stdin.readline().rstrip())):
    A = sys.stdin.readline().rstrip().split()
    if A[0] == "1":
        rev ^= 1
    else:
        if (int(A[1]) ^ rev) & 1:
            L.append(A[2])
        else:
            R.append(A[2])

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

Solution (Python):
S = input()
Q = int(input())
Que = [list(map(str,input().split())) for i in range(Q)]

from collections import deque
count = 0
 
S = list(S)
S = deque(S)
 
for i in range(Q):
    if Que[i][0] == '1':
        
        count += 1
    elif Que[i][0] == '2':
        if Que[i][1] == '1' and count%2 == 0:
            S.appendleft(Que[i][2])
        elif Que[i][1] == '1' and count%2 == 1:
            S.append(Que[i][2])
        elif Que[i][1] == '2' and count%2 == 0:
            S.append(Que[i][2])
        elif Que[i][1] == '2' and count%2 == 1:
            S.appendleft(Que[i][2])
            
if count%2 == 0:
    print("".join(S))
else:
    S.reverse()
    print("".join(S))

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

cnt = 1
for i in range(q):
    t_f_c = list(input().split())
    if t_f_c[0] == "1":
        cnt *= -1
    elif t_f_c[0] == "2":
        if cnt == 1: 
            if t_f_c[1] == "1":
                s.appendleft(t_f_c[2])
            elif t_f_c[1] == "2":
                s.append(t_f_c[2])
        elif cnt == -1:  
             if t_f_c[1] == "2":
                s.appendleft(t_f_c[2])
             elif t_f_c[1] == "1":
                s.append(t_f_c[2])
if cnt == 1:
    print("".join(s))
elif cnt == -1:
    s.reverse()
    print("".join(s))

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

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

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

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

S = input()
L = []
R = []
rev = 0
for _ in range(int(input())):
    A = input().split()
    if A[0] == "1":
        rev ^= 1
    else:
        if (int(A[1]) ^ rev) & 1:
            L.append(A[2])
        else:
            R.append(A[2])

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

Solution (Python):
from collections import deque

S = deque(input())
Q = int(input())
cnt = 2
for _ in range(Q):
    L = input().split()
    if L[0] == "1":
        cnt += 1
    elif L[0] == "2":
        if L[1] == "1" and (cnt)%2 == 0:
            S.appendleft(str(L[2]))
        elif L[1] == "1" and (cnt)%2 != 0:
            S.append(str(L[2]))
        elif L[1] == "2" and (cnt)%2 == 0:
            S.append(str(L[2]))
        elif L[1] == "2" and (cnt)%2 != 0:
            S.appendleft(str(L[2]))
S ="".join(S)
if cnt%2 != 0:
        print(S[::-1])
else:
        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):
import collections

s = collections.deque(list(input()))
q = int(input())
rev = 0
for i in range(q):
    tfc = list(input().split())
    if tfc[0]=='1':
        rev = abs(rev-1)
    else:
        t,f,c = tfc
        if rev:
            if f=='1':
                s.append(c)
            else:
                s.appendleft(c)
        else:
            if f=='1':
                s.appendleft(c)
            else:
                s.append(c)
if rev:
    t = []
    for i in range(len(s)):
        t.append(s.pop())
    print(''.join(t))
else:
    print(''.join(s))

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

Solution (Python):
from collections import deque
S = list(input())
N = int(input())
rev = False
se = False
S = deque(S)
for x in range(N):
    Q = list(input().split())
    if Q[0] == "1":
        if rev:
            rev = False
        elif se:
            rev = True
    else:
        se =True
        if Q[1] == "1":
            if rev:
                S.append(Q[2])
            else:
                S.appendleft(Q[2])
        else:
            if rev:
                S.appendleft(Q[2])
            else:
                S.append(Q[2])

S = list(S)
if rev:
    S = reversed(S)
    
ans = "".join(S)
print(ans)

Solution (Python):
from collections import deque

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

query_list = []
for _ in range(q):
    query_list += [input()]

deq = deque(s)
true_position = True

for i in range(q):
    if len(query_list[i])==1:
        true_position = not true_position
        
    else:
        if query_list[i][2]=="1":
            if true_position:
                deq.appendleft(query_list[i][4])
            else:
                deq.append(query_list[i][4])
        elif query_list[i][2]=="2":
            if true_position:
                deq.append(query_list[i][4])
            else:
                deq.appendleft(query_list[i][4])

if true_position:
    ans = "".join(deq)
else:
    deq.reverse()
    ans = "".join(deq)

print(ans)

Solution (Python):
if __name__ == '__main__':
    d = input()
    n = int(input())
    qs = [input() for e in range(n)]
    s = []
    e = []
    r = False
    for q in qs:
        if q == '1':
            r = not(r)
        elif q.startswith("2 1"):
            e.append(q[-1]) if r else s.append(q[-1])
        else:
            s.append(q[-1]) if r else e.append(q[-1])
    print("".join(e[::-1]) + d[::-1] + "".join(s) if r else "".join(s[::-1]) + d + "".join(e))

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

Solution (Python):
import sys
from collections import deque

S=raw_input()
Q=input()
Query=[ sys.stdin.readline().split() for _ in range(Q) ]

s1=deque()
s2=deque()
judge={}
judge["front"]="s1"
judge["end"]="s2"

cnt=0
for x in Query:
    if x[0]=="1":
        n=x[0]
        cnt+=1
    elif x[0]=="2":
        n,f,c=x[0],x[1],x[2]
    if n=="1":
        judge["front"],judge["end"]=judge["end"],judge["front"]
    elif n=="2":
        if f=="1":
            if judge["front"]=="s1":
                s1.appendleft(c)
            elif judge["front"]=="s2":
                s2.append(c)
        elif f=="2":
            if judge["end"]=="s1":
                s1.appendleft(c)
            if judge["end"]=="s2":
                s2.append(c)

str1=""
for x in s1:
    str1+=x
str2=""
for x in s2:
    str2+=x

if judge["front"]=="s1" and judge["end"]=="s2":
    print str1+S+str2
elif judge["front"]=="s2" and judge["end"]=="s1":
    print str2[::-1]+S[::-1]+str1[::-1]

Solution (Python):
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 % 2] else 1] += l[4]
if r % 2 == 0:
    print(''.join(reversed(a[1])) + s + ''.join(a[0]))
else:
    print(''.join(reversed(a[0])) + ''.join(reversed(s)) + ''.join(a[1]))

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

Solution (Python):
def d():
    from collections import deque
    S = deque(str(input()))
    Q = int(input())
    rvs = False

    for i in range(Q):
        q = list(input().split())

        if q[0] == '1':
            rvs = not rvs
        elif q[0] == '2':
            if q[1] == '1':
                if rvs:
                    S.append(q[2])
                else:
                    S.appendleft(q[2])
            elif q[1] == '2':
                if rvs:
                    S.appendleft(q[2])
                else:
                    S.append(q[2])
            else:
                raise ValueError(
                    'F({}) is {}, which is invalid!'.format(i, q[1]))
        else:
            raise ValueError('T({}) is {}, which is invalid!'.format(i, q[0]))

    if rvs:
        ans = list(S)[::-1]
    else:
        ans = list(S)
    print(''.join(ans))



if __name__ == "__main__":
    d()

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

Solution (Python):
from collections import deque
s=input()
q=int(input())
turn=0
ans=deque(s)
for i in range(q):
    query=input().split()
    if query[0]=="1":
        turn+=1
    if query[0]=="2":
        if query[1]=="1":
            if turn%2==0:
                ans.appendleft(query[2])
            else:
                ans.append(query[2])
        elif query[1]=="2":
            if turn%2==0:
                ans.append(query[2])
            else:
                ans.appendleft(query[2])
ans="".join(ans)
if turn%2==1:
    ans=ans[::-1]
print(ans)

Solution (Python):

from collections import deque
s=input()
q=int(input())
l=[]
turn=0
ans=deque()
ans.append(s)
for i in range(q):
    query=input().split()
    if query[0]=="1":
        turn+=1
    if query[0]=="2":
        if query[1]=="1":
            if turn%2==0:
                ans.appendleft(query[2])
            else:
                ans.append(query[2])
        elif query[1]=="2":
            if turn%2==0:
                ans.append(query[2])
            else:
                ans.appendleft(query[2])
ans="".join(ans)
if turn%2==1:
    ans=ans[::-1]
print(ans)

Solution (Python):
s = input()
n = int(input())
l = [list(input().split()) for i in range(n)]
start = []
end = []
d = True
for i in range(n):
    if l[i][0] == "1":
        d = not (d)
    else:
        if l[i][1] == "1":
            if d:
                start.append(l[i][2])
            else:
                end.append(l[i][2])
        else:
            if d:
                end.append(l[i][2])
            else:
                start.append(l[i][2])
if d:
    print("".join(start[::-1]) + s + "".join(end))
else:
    print("".join(end[::-1]) +
          ''.join(list(reversed(s))) + "".join(start))

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

hanten = 0
saki = ""
ato = ""

for i in range(Q):
    q = input().split()
    
    if q[0] == "1":
        hanten += 1
    else:
        if hanten % 2 == 0:
            if q[1] == "1":
                saki += q[2]
            else:
                ato += q[2]
        else:
            if q[1] == "1":
                ato += q[2]
            else:
                saki += q[2]

if hanten % 2 == 0:
    for i in range(len(saki)):
        print(saki[-i-1], end="")
    print(S, end="")
    print(ato)
else:
    for i in range(len(ato)):
        print(ato[-i-1], end="")
    for i in range(len(S)):
        print(S[-i-1], end="")
    print(saki)

Solution (Python):
from collections import deque
s=input()
q=int(input())
ans_str=deque(s)
cnt=0
for _ in range(q):
    Q=list(input().split())
    if Q[0]=='1':
        cnt=1 if cnt==0 else 0

    else:
        if Q[1]=='1':
            if cnt==0 or len(ans_str)==1:
                ans_str.appendleft(Q[-1])
            else:
                ans_str.append(Q[-1])

        else:
            if cnt==0 or len(ans_str)==1:
                ans_str.append(Q[-1])
            else:
                ans_str.appendleft(Q[-1])
    

ans=list(ans_str) if cnt==0 else list(ans_str)[::-1]
print(*ans,sep='')

Solution (Python):
from collections import deque

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

sArry = deque(S)
rFlag = False
for i in range(Q):
    if Query[i][0] == '1':
        rFlag = (rFlag == False)
        
    else:
        if rFlag:
            if Query[i][1] == '1':
                sArry.append(Query[i][2])
            else:
                sArry.appendleft(Query[i][2])
        else:
            if Query[i][1] == '1':
                sArry.appendleft(Query[i][2])
            else:
                sArry.append(Query[i][2])
            




if rFlag:
    print("".join(reversed(sArry)))
else:
    print("".join(sArry))

Solution (Python):

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

rev_flag = 1
head_str = ""
tail_str = ""
for _ in range(Q):
    query = input() 
    if query[0] == "1":
        rev_flag *= -1
    else:
        h_or_t_flag = 1 if query[2] == "1" else -1
        h_or_t_flag *= rev_flag
        
        if h_or_t_flag == 1:
            head_str += query[4]
        else:
            tail_str += query[4]

ans = head_str[::-1] + S + tail_str
print(ans if rev_flag == 1 else ans[::-1])

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

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

  else:
    s  = s + query[4]
s = left[::-1] + s
if cnt_reverse:
  s = s[::-1]
print(s)

Solution (Python):
from collections import deque
s=list(str(input()))
s=deque(s)
q=int(input())
cnt=0
for i in range(q):
    que=list(input().split())
    if len(que)==1:
        cnt+=1
    if len(que)!=1 and cnt%2==0:
        if int(que[1])==1:
            s.appendleft(que[2])
        elif int(que[1])==2:
            s.append(que[2])
    if len(que)!=1 and cnt%2!=0:
        if int(que[1])==1:
            s.append(que[2])
        elif int(que[1])==2:
            s.appendleft(que[2])
if cnt%2!=0:
    s.reverse()
print(''.join(s))

Solution (Python):

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

rev_flag = 1
head_str = ""
tail_str = ""
for _ in range(Q):
    query = str(input()) 
    if query[0] == "1":
        rev_flag *= -1
    else:
        h_or_t_flag = 1 if query[2] == "1" else -1
        h_or_t_flag *= rev_flag
        
        if h_or_t_flag == 1:
            head_str += query[4]
        else:
            tail_str += query[4]

ans = head_str[::-1] + S + tail_str
print(ans if rev_flag == 1 else ans[::-1])

Solution (Python):
from collections import deque

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

ans = deque()
ans.append(S)
reverse = False

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

ans = ''.join(ans)

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

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

def main():
    s = input()
    q = int(input())

    hanten = 0 

    front = "" 
    back = ""

    for _ in range(q):
        tmp = readline().split()
        if len(tmp) == 1:
            hanten ^= 1
        else:
            if hanten: 
                if tmp[1] == '1':
                    back += tmp[2]
                else:
                    front += tmp[2]
            else: 
                if tmp[1] == '1':
                    front += tmp[2]
                else:
                    back += tmp[2]

    if hanten == 0:
        print(front[::-1] + s + back)
    else:
        print((front[::-1] + s + back)[::-1])

if __name__ == '__main__':
    main()

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

front = []
back = []
is_reverse = False

for query in query_list:
    if query[0] == '1':
        is_reverse = not is_reverse
    elif query[0] == '2':
        key = int(query[1])
        value = query[2]
        if (key == 1 and not is_reverse) or (key == 2 and is_reverse):
            front.append(value)
        else:
            back.append(value)

ans = ''
if is_reverse:
    ans = back[::-1]+list(s)[::-1]+front
else:
    ans = front[::-1]+list(s)+back
print(''.join(ans))

Solution (Python):
from collections import deque
 
S = deque(input())
Q = int(input())
 
Q_list = [list(map(str, input().split())) for i in range(Q)]
 
rev = False
 
for i in range(Q):
  if int(Q_list[i][0]) == 1:
    rev = not rev
  
  else:
    F = Q_list[i][1]
    C = Q_list[i][2]
    
    if rev ^ (int(F) == 1): 
      S.appendleft(C) 
    else:
      S.append(C)
      
if rev: 
  S.reverse()
  
print(''.join(S))

Solution (Python):
from collections import deque

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

t = 0
for i in range(q):
    o = list(map(str, input().split()))
    if o[0] == "1":
        t += 1
    else:
        if o[1] == "1" and t%2 == 0 or o[1] == "2" and t%2 == 1:
            s.appendleft(o[2])
        else:
            s.append(o[2])

if t%2 == 1:
    s.reverse()

print("".join(s))

Solution (Python):

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

rev_flag = 1
head_str = ""
tail_str = ""
for _ in range(Q):
    query = list(map(str, input().split()))
    if query[0] == "1":
        rev_flag *= -1
    else:
        h_or_t_flag = 1 if query[1] == "1" else -1
        h_or_t_flag *= rev_flag
        
        if h_or_t_flag == 1:
            head_str += query[2]
        else:
            tail_str += query[2]

ans = head_str[::-1] + S + tail_str
print(ans if rev_flag == 1 else ans[::-1])

Solution (Python):
from collections import deque
 
S = deque(input())
Q = int(input())
 
Q_list = [list(map(str, input().split())) for i in range(Q)]
 
rev = False
 
for i in range(Q):
  if int(Q_list[i][0]) == 1:
    rev = not rev
  
  else:
    F = Q_list[i][1]
    C = Q_list[i][2]
    
    if rev ^ (int(F) == 1): 
      S.appendleft(C)
    else:
      S.append(C)
      
if rev:
  S.reverse()
  
print(''.join(S))

Solution (Python):
from collections import deque

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

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

if rev_flag:
    print(''.join(deque(reversed(s))))
else:
    print(''.join(s))

Solution (Python):
from collections import deque

s = list(input())
q = int(input())
ans = deque(s)
cnt = 0
for i in range(q):
    f = [i for i in input().split()]
    if f[0] == '1':
        cnt += 1
    else:
        if f[1] == '1':
            if cnt % 2 == 0:
                ans.appendleft(f[2])
            else:
                ans.append(f[2])
        else:
            if cnt % 2 == 0:
                ans.append(f[2])
            else:
                ans.appendleft(f[2])
if cnt % 2 == 0:
    print(''.join(map(str,ans)))
else:
    ans2 = []
    for i in (reversed(ans)):
        ans2.append(i)
    print(''.join(map(str,ans2)))

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

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

ans = ''.join(ans)
if reverse:
    print(ans[::-1])
else:
    print(ans)

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

topFlg = True

leftStr = ""
rightStr = ""

for _ in range(Q):
    query = list(map(str, input().split()))
    if query[0] == "1":
        topFlg = not (topFlg)
    else:
        if query[1] == "1":
            if topFlg:
                leftStr = query[2] + leftStr
            else:
                rightStr += query[2]
        else:
            if topFlg:
                rightStr += query[2]
            else:
                leftStr = query[2] + leftStr

print(leftStr + S + rightStr if topFlg else (leftStr + S + rightStr)[::-1])

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

topFlg = True

leftStr = ""
rightStr = ""

for _ in range(Q):
    query = list(map(str, input().split()))
    if query[0] == "1":
        topFlg = not (topFlg)
    else:
        if query[1] == "1":
            if topFlg:
                leftStr = query[2] + leftStr
            else:
                rightStr += query[2]
        else:
            if topFlg:
                rightStr += query[2]
            else:
                leftStr = query[2] + leftStr

print(leftStr + S +
      rightStr if topFlg else rightStr[::-1] + S[::-1] + leftStr[::-1])

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

Solution (Python):
from collections import deque
import sys
input = lambda: sys.stdin.readline().rstrip()
d = deque(input())

reverse = -1
n = int(input())
for i in range(n):
    query = input().split()
    if len(query) == 1:
        reverse *= (-1)
    else:
        f = int(query[1])
        c = query[2]
        if reverse == -1:
            if f == 1:
                d.appendleft(c)
            else:
                d.append(c)
        else:
            if f == 1:
                d.append(c)
            else:
                d.appendleft(c)

if reverse == 1:
    d.reverse()

print("".join(d))

Solution (Python):
from collections import deque

s = list(input())
d = deque(s)
forward = True
q = int(input())
for i in range(q):
    l = input().split()
    if len(l) == 1:
        forward = not forward
    else:
        pos = int(l[1])
        c = l[2]
        if pos == 1:
            if forward:
                d.appendleft(c)
            else:
                d.append(c)
        else:
            if forward:
                d.append(c)
            else:
                d.appendleft(c)

if forward == False:
    d.reverse()

print("".join(d))