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())
qs = [input().split() for i in range(q)]
n = False
left= right = ""
for operation in qs:
    if operation[0] == '1':
        n = not n
    if operation[0] == '2':
        t , f, c = operation
        f = int(f)
        if n:
            f = 1 if f != 1 else 2
        if f == 1:
            left=c+left
        elif f == 2:
            right+=c
s = left+s+right
print(s[::-1] if n else s)

Solution (Python):
alph="abcdefghijklmnopqrstuvwxyz"
from collections import deque
import sys
input=sys.stdin.readline
moji=deque([])
s=input().rstrip()
for i in range(len(s)):
	for j in range(26):
		if s[i]==alph[j]:
			moji.append(j)
			break
q=int(input())
rev=0
for _ in range(q):
	qw=input().split()
	if qw[0]=="1":
		rev+=1
	else:
		f=qw[1]
		c=qw[2]
		for j in range(26):
			if c==alph[j]:
				x=j
				break
		if f=="1":
			if rev%2==0:
				moji.appendleft(x)
			else:
				moji.append(x)
		else:
			if rev%2==0:
				moji.append(x)
			else:
				moji.appendleft(x)
ans=""
r=[]
for i in range(len(moji)):
	r.append(moji.popleft())
for i in range(len(r)):
	ans+=alph[r[i]]
if rev%2==0:
	print(ans)
else:
	print(ans[::-1])

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

buf = [0] * (3*(10**5)+10)
for i, s in enumerate(S):
    buf[i] = s

is_reverse = False
first = 0
last = len(S) - 1

def add_top(c):
    global buf
    global first
    if is_reverse:
        first += 1
    else:
        first -= 1
    buf[first] = c

def add_tail(c):
    global buf
    global last
    if is_reverse:
        last -= 1
    else:
        last += 1
    buf[last] = c

for i in range(Q):
    Query = input().split()
    t = int(Query[0])
    if t == 1:
        is_reverse = not is_reverse
        first, last = last, first
    else:
        f = int(Query[1])
        c = Query[2]
        if f == 1:
            add_top(c)
        else:
            add_tail(c)

ans_list = []
p = first
while True:
    ans_list.append(buf[p])
    if p == last:
        break
    if is_reverse:
        p -= 1
    else:
        p += 1
ans = ''.join(ans_list)
print(ans)

Solution (Python):
from collections import deque

S = input()
Q = int(input())
L = [list(input().split()) for i in range(Q)]

hanten = 0
t = deque([S])

for l in L:
    if len(l) == 1:
        hanten += 1
    elif (l[1] == '1' and hanten % 2 == 0) or (l[1] == '2' and hanten % 2 == 1):
        t.appendleft(l[2])
    else:
        t.append(l[2])

ans = ''.join(t)
if hanten % 2 == 1:
    ans = ''.join(list(reversed(ans)))
print(ans)

Solution (Python):
from collections import deque
import copy

S = list(input())
Q = int(input())
Q_list = [input().split() for i in range(Q)]

sn = 0
s = 0

Ans_list1 = deque([])

for k in range(len(S)):
    Ans_list1.append(S[k])

for j in range(Q):
    if Q_list[j][0] == "1":
        s += 1

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

Ans_list2 = copy.deepcopy(Ans_list1)

for i in range(Q):
    if Q_list[i][0] == "1":
        sn += 1
    else:
        if (sn % 2 == 0 and Q_list[i][1] == "1") or (sn % 2 == 1 and Q_list[i][1] == "2"):
            Ans_list1.appendleft(Q_list[i][2])
            Ans_list2.append(Q_list[i][2])
        else:
            Ans_list1.append(Q_list[i][2])
            Ans_list2.appendleft(Q_list[i][2])


if sn % 2 == 0:
    print(''.join(Ans_list1))
else:
    print(''.join(Ans_list2))

Solution (Python):
s = str(input())
q = int(input())
state = True
front = ""
ending = ""
for _ in range(q):
  qi = str(input())
  if qi[0] == "2":
    fi,ci = map(str,qi[2:].split())
    if state == True:
      if fi == "1":
        front += ci
      else:
        ending += ci
    else:
      if fi == "1":
        ending += ci
      else:
        front += ci
  else:
    state = not state
if state:
  first = front[::-1]
  lastt = ending
else:
  first = ending[::-1]
  lastt = front
  s = s[::-1]
print(first+s+lastt)

Solution (Python):


start = str(input())
Q = int(input())
S = "X"
cnt = 0
q = []
for i in range(Q):
    q.append(input().split(' '))
    if(q[i][0] == '1'):
        
        cnt += 1

cntq = 0
for i in range(Q):
    
    if(q[i][0] == '1'):
        
        cntq += 1
 
    else:
        if((q[i][1] == '1' and (cnt-cntq)%2 == 0) or(q[i][1] == '2' and (cnt-cntq)%2 == 1)):
            S = q[i][2] + S
        else:
            S = S + q[i][2]

if(cnt%2 == 1):
    start = start[::-1]



print(S.replace("X",start))

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

Solution (Python):
from collections import deque

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

flg = "order"
for i in range(q):
    t, *f = map(str, input().split())
    if t == "1":
        if flg == "order":
            flg = "reverse"
        else:
            flg = "order"
    else:
        if f[0] == "1":
            if flg == "order":
                s.appendleft(f[1])
            else:
                s.append(f[1])
        else:
            if flg == "order":
                s.append(f[1])
            else:
                s.appendleft(f[1])

if flg == "order":
    print("".join(s))
else:
    s.reverse()
    print("".join(s))

Solution (Python):
from collections import deque
S=input()
Q=int(input())
q=deque()
for i in S:
  q.append(i)
cnt=0
for i in range(Q):
  A=list(input().split())
  if int(A[0])==1:
    cnt+=1
  else:
    if int(A[1])==cnt%2+1:
      q.appendleft(A[-1])
    else:
      q.append(A[-1])
if (cnt%2)==0:
  for i in range(len(q)):
    print(q.popleft(),end='')
else:
  for i in range(len(q)):
    print(q.pop(),end='')

Solution (Python):


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

    for i in range(q):
        q_list.append(input())

    r_flg = True
    s_str = ''
    e_str = ''

    for query in q_list:
        
        if query[0] == '1':
            r_flg = not r_flg
        
        
        else:
            
            if query[2] == '1':
                if r_flg:
                    s_str = query[4] + s_str
                else:
                    e_str = e_str + query[4]
            
            else:
                if r_flg:
                    e_str = e_str + query[4]
                else:
                    s_str = query[4] + s_str

        

    s = s_str + s + e_str

    if not r_flg:
        s = ''.join(reversed(s))

    print(s)

if __name__ == "__main__":
    main()

Solution (Python):
from itertools import chain

def swap(head, tail):
    return (tail, head)

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

head = []
tail = []
count = 0

for _i in range(q):
    query = input()
    if query == "1":
        count += 1
        head, tail = swap(head, tail)
    else:
        _n, f, c = tuple(query.split())
        if f == "1":
            head.append(c)
        else:
            tail.append(c)

s = s if count % 2 == 0 else reversed(s)
print("".join(tuple(chain(reversed(head), s, tail))))

Solution (Python):
import array
def main():
  n=2*10**5
  s=input()
  q=int(input())
  rev=0
  l=n-1
  r=n+len(s)
  s=array.array('b',[0]*n+[ord(c) for c in s]+[0]*n)
  for _ in range(q):
    query=input().split()
    if(query[0]=='1'):
      rev=1-rev
    elif((int(query[1])+rev)%2==1):
      s[l]=ord(query[2])
      l-=1
    else:
      s[r]=ord(query[2])
      r+=1
  s=[chr(c) for c in s if c!=0]
  if rev==1:
    return ''.join(s[::-1])
  return ''.join(s)
print(main())

Solution (Python):
from collections import deque


S = deque(list(input()))
Q = int(input())
queries = []
for _ in range(Q):
    query = input().split()
    if len(query) == 3:
        queries.append([int(x) for x in query[:2]] + [query[2]])
    else:
        if len(queries[-1]) == 1:
            queries.pop()
        else:
            queries.append([int(x) for x in query])
p = True
count = 0
for query in queries:
    if len(query) == 3:
        if p:
            if query[1] == 1:
                S.appendleft(query[2])
            else:
                S.append(query[2])
        else:
            if query[1] == 1:
                S.append(query[2])
            else:
                S.appendleft(query[2])
    else:
        p = not p
        count += 1

if count % 2:
    S.reverse()
print(''.join(S))

Solution (Python):
from collections import deque

s = deque(list(input()))
n = int(input())
h = 1
hantensuu = 0

def change(flag):
    if flag==1:
        return 2
    else:
        return 1

for i in range(n):
    inputs = list(input().split())
    t = int(inputs[0])

    if len(inputs) == 3:
        f = int(inputs[1])
        c = inputs[2]
    if t == 1:
        h = change(h)
        hantensuu += 1
    if t == 2:
        if (f == 1 and h == 1) or (f==2 and h==2):
            s.appendleft(c)
        if (f == 2 and h == 1) or (f==1 and h ==2):
            s.append(c)
    
if hantensuu%2 !=0:
    s.reverse()

print("".join(s))

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


def main():
    a = input()
    d = deque(a)
    n = int(input())
    flagr = 1
    for i in range(n):
        j = list(readline().split())
        if len(j) == 3 and j[1] == "1" and flagr == 1:
            d.appendleft(j[2])
        elif len(j) == 3 and j[1] == "2" and flagr == 1:
            d.append(j[2])
        elif len(j) == 3 and j[1] == "2" and flagr == -1:
            d.appendleft(j[2])
        elif len(j) == 3 and j[1] == "1" and flagr == -1:
            d.append(j[2])
        else:
            flagr *= -1
    if flagr == -1:
        d.reverse()
    ans = ""
    for j in d:
        ans = ans + j
    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
import sys
if __name__ == '__main__':
    lines = sys.stdin.readlines()
    line = [a.strip('\n').split(' ') for a in lines]
    
    right = []
    left = []
    count = 0

    for i,v in enumerate(line):
        if i == 0:
            st = v
        if i == 1:
            q = v
        if i >= 2:
            if len(v)==3:
                for j,k in enumerate(v):
                    if j == 1:
                        ichi = int(k)
                    if j==2:
                        alp = k
                if count%2 == 0:
                    if ichi == 1:
                        left.append(alp)
                        
                    if ichi == 2:
                        right.append(alp)
                        
                else:
                    if ichi == 1:
                        right.append(alp)
                        
                    if ichi == 2:
                        left.append(alp)
                        

            if len(v)==1:
                count += 1
    
    
    
    b = list(st[0])
    
    
    
    if count%2 == 0:
        d = list(reversed(left))+b+right
    if count%2 == 1:
        d = list(reversed(right))+list(reversed(b))+left
    print(''.join(d))

Solution (Python):
S = input()
Q = int(input())
f = True
l = []
r = []
for _ in range(Q):
    q = input()
    if q == "1":
        f = not f
    else:
        _, F, C = q.split()
        if F == "1":
            t = l if f else r
            t.append(C)
        else:
            t = r if f else l
            t.append(C)
if f:
    l.reverse()
    ans = "".join(l) + S + "".join(r)
else:
    r.reverse()
    ans = "".join(r) + S[::-1] + "".join(l)
print(ans)

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

sb=""
sa=""
rev=1
for i in range(q):
    qu=input()
    if qu=="1":
        rev=-rev
    else:
        t,f,c=qu.split()
        if f=="1" and rev==1:

            sb=c+sb

        elif f=="2" and rev==1:

            sa=sa+c

        elif f=="1" and rev==-1:

            sa=sa+c

        elif f=="2" and rev==-1:

            sb=c+sb

s=sb+s+sa
if rev==-1:
    s=s[::-1]

print(s)

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

direction = 1  
output_string = s
string1 = ''
string2 = ''

change_direction_count = 0
for query in q_datas :
    if query[0] == '1':
        direction *= -1
        change_direction_count += 1
    else:
        if query[1] == '1' and direction == 1:
            string1 = query[2] + string1
        elif  query[1] == '1' and direction == -1:
            string2 = string2 + query[2]
        elif  query[1] == '2' and direction == 1:
            string2 = string2 + query[2]
        else: 
            string1 = query[2] + string1

if change_direction_count % 2 == 0:
    print(string1 + s + string2)
else:
    print(string2[::-1] + s[::-1] + string1[::-1])

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

S = deque([S])

for _ in range(Q):
    Query += [input()]

reversed = False
for i, q in enumerate(Query):
    if q == "1":
        reversed = not reversed
        
    else:
        Ti, Fi, Ci = list(q.split())
        
        if Fi == "1":
            if not reversed:
                S.appendleft(Ci)
                
            else:
                S.append(Ci)
                
        elif Fi == "2":
            
            if not reversed:
                S.append(Ci)
                
            else:
                S.appendleft(Ci)
                
    

s = "".join(S)

if reversed:
    print(s[::-1])
else:
    print(s)

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

Solution (Python):
s = input()
q = int(input())
hanten = False
sentou = ""
matubi = ""
query = []
for i in range(q):
    query.append(list(input().split()))

for i in range(q):
    if hanten == False:
        if len(query[i]) == 1:
            hanten = True
        else:
            if int(query[i][1]) == 1:
                
                sentou = query[i][2] + sentou
            else:
                
                matubi = matubi + query[i][2]
        
    else:
        if len(query[i]) == 1:
            hanten = False
        else:
            if int(query[i][1]) == 1:
                
                matubi = matubi + query[i][2]
            else:
                
                sentou = query[i][2] + sentou
        

s = sentou + s + matubi
if hanten == False:
    print(s)
else:
    print(s[::-1])

Solution (Python):
from collections import deque

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

Solution (Python):
from collections import deque
s = list(input())
q = int(input())
rev = 0
revcnt = 0
s_q = deque(s)
for _ in range(q):
    query = list(map(str, input().split()))
    if query[0] == "1":
        revcnt += 1
        if rev == 0:
            rev = 1
        elif rev == 1:
            rev = 0

    elif query[0] == "2":
        if rev == 0:
            if query[1] == "1":
                s_q.appendleft(query[2])
            elif query[1] == "2":
                s_q.append(query[2])
        else:
            if query[1] == "1":
                s_q.append(query[2])
            elif query[1] == "2":
                s_q.appendleft(query[2])
s = list(s_q)
if revcnt%2 == 0:
    print("".join(s))
else:
    print("".join(s[::-1]))

Solution (Python):
S = input()
len_s = len(S)
rev = False
max_ = 2*10**5
index = max_ + len_s
rev_index = max_ - 1
s = ["." for _ in range(max_*2 + len_s)]
for i in range(len_s):
    s[max_+i] = S[i]
for _ in range(int(input())):
  t = input()
  if t == "1":
    rev = not rev
  else:
    t = t.split()
    if (t[1] == "1" and not rev) or (t[1] == "2" and rev):
      s[rev_index] = t[2]
      rev_index -= 1
    else:
      s[index] = t[2]
      index += 1
if rev:
  for i in range((max_ + max_ + len_s - 1), 0, -1):
    if s[i] != ".":
      print(s[i], end="")
else:
  for i in range((max_ + max_ + len_s)):
    if s[i] != ".":
      print(s[i], end="")

Solution (Python):
from collections import deque

s = input()
d = deque(s)
n = int(input())
flag = 0
for _ in range(n):
    q = input().split()
    if (len(q)==1):
        if(flag == 0):
            flag = 1
        else:
            flag = 0
    else:
        a = int(q[1])
        b = q[2]
        if (a == 1):
            if flag==0:
                d.appendleft(b)
            else:
                d.append(b)
        else:
            if flag==0:
                d.append(b)
            else:
                d.appendleft(b)

if flag==1:
    d.reverse()
ans = list(d)
xxx = "".join(ans)
print(xxx)

Solution (Python):
s = input()
q = int(input())
qs = [input().split() for i in range(q)]
n = False
left= right = ""
for operation in qs:
    if operation[0] == '1':
        n = not n
    if operation[0] == '2':
        t , f, c = operation
        f = int(f)
        if n:
            f = 1 if f != 1 else 2
        if f == 1:
            left=c+left
        elif f == 2:
            right+=c
s = left+s+right
print(s[::-1] if n else s)

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

S=list(input()[:-1])
Q=int(input()[:-1])
f=0
back=[]
forward=[]

for _ in range(Q):
    q=input().split()
    if int(q[0])==1:
        f = (f+1)%2
    if int(q[0])==2:
        if int(q[1])==1:
            if f==0:
                forward.append(q[2])
            else:
                back.append(q[2])
        else:
            if f==0:
                back.append(q[2])
            else:
                forward.append(q[2])

if f==0:
    print("".join(forward[::-1]+S+back))
else:
    print("".join((forward[::-1]+S+back)[::-1]))

Solution (Python):
from collections import deque

S = deque(list(str(input())))
Q = int(input())
flag = False

for i in range(Q):
    Query =  [str(p) for p in input().split()]
    if(int(Query[0]) == 1):
        flag = not(flag)
    else:
        if(int(Query[1]) == 1):
            if(flag):
                S.append(Query[2])
            else:
                S.appendleft(Query[2])
        else:
            if(flag):
                S.appendleft(Query[2])
            else:
                S.append(Query[2])

if(flag):
    S.reverse()

print("".join(list(S)))

Solution (Python):
s=input()
n=int(input())
q=[list(map(str,input().split())) for i in range(n)]
atama=0
x=0
for i in range(n):
  if q[i][0]=='2':
    x+=1

oshiri=len(s)+x-1
ans=[]
for i in range(len(s)+x):
    ans.append('x')
x=0
for i in range(n)[::-1]:
  if q[i][0]=='1':
    x+=1
  else:
    if (int(q[i][1])+x)%2==0:
      ans[oshiri]=q[i][2]
      oshiri-=1
    else:
      ans[atama]=q[i][2]
      atama+=1

if x%2==1:
  s=s[::-1]

for i in range(len(s)):
  ans[i+atama]=s[i]

answer=ans[0]
for i in range(1,len(ans)):
    answer+=ans[i]
    
print(answer)

Solution (Python):
S = input()
Q = int(input())
lst_query = [input().split() for i in range(Q)]

def make_reverse(S):

    if len(S) == 1:
        return S

    reversedS = ""
    for i in range(len(S)-1, -1, -1):
        reversedS += S[i]
    else: return reversedS

def main(S, Q, lst_query):

    cnt_reverse = 0
    leftS  = ""
    rightS = ""

    for i in range(Q-1, -1, -1):
        
        query = lst_query[i]

        if int(query[0]) == 1:
            cnt_reverse += 1
        else:
            if cnt_reverse % 2 == 0:
                if int(query[1]) == 1: leftS  = leftS + query[2]
                else                 : rightS = query[2] + rightS
            else:
                if int(query[1]) == 1: rightS = query[2] + rightS
                else                 : leftS  = leftS + query[2]

    if cnt_reverse % 2 == 1: S = make_reverse(S)
    return leftS + S + rightS


if __name__ == "__main__":

    answer = main(S, Q, lst_query)
    print(answer)

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

for _ in range(q):
    Q = list(map(str,input().split()))
    if int(Q[0]) == 1:
        if k == 0:
            k = 1
        else:
            k = 0
    else:
        if k == 0:
            if int(Q[1]) == 1:
                s.appendleft(Q[2])
            else:
                s.append(Q[2])
        else:
            if int(Q[1]) == 1:
                s.append(Q[2])
            else:
                s.appendleft(Q[2])


if k == 1:
    s = list(s)[::-1]
    
print("".join(s))

Solution (Python):
from collections import deque
s = input()
q = int(input())
tmp = deque()
for i in range(len(s)):
    tmp.append(s[i])
flag = 0
for _ in range(q):
    _s = input()
    if _s == "1":
        flag += 1
    else:
        t, f, c = _s.split(" ")
        r = flag % 2
        if f == "1" and r == 0:
            
            tmp.appendleft(c)
        elif f == "1" and r == 1:
            
            tmp.append(c)
        elif f == "2" and r == 0:
            
            tmp.append(c)
        elif f == "2" and r == 1:
            
            tmp.appendleft(c)
if flag % 2 == 0:
    for i in range(len(tmp)):
        print(tmp.popleft(), end="")
else:
    for i in range(len(tmp)):
        print(tmp.pop(), end="")

Solution (Python):
s = input()
n = int(input())
re = 1
head = ''
tail = ''

for i in range(n):
    q = input().split()
    if q[0] == '1':
        re = re * -1
    else:
        if re == 1:
            if q[1] == '1':
                head = q[2] + head
            else:
                tail = tail + q[2]
        else:
            if q[1] == '1':
                tail = tail + q[2]
            else:
                head = q[2] + head

print(head+s+tail) if re == 1 else print((head+s+tail)[::-1])

Solution (Python):
def main():
    from collections import deque
    S = input()
    Sd = deque(list(S))
    Q = int(input())
    Query = [input().split() for i in range(Q)]
    flag = 0
    for q in Query:
        if q[0] == "1":
            flag += 1
        elif q[0] == "2":
            if q[1] == "1":
                if flag % 2 == 1:
                    Sd.append(q[2])
                elif flag % 2 == 0:
                    Sd.appendleft(q[2])
            elif q[1] == "2":
                if flag % 2 == 1:
                    Sd.appendleft(q[2])
                elif flag % 2 == 0:
                    Sd.append(q[2])

    if flag % 2 == 1:
        Sd.reverse()
        print("".join(Sd))
    elif flag % 2 == 0:
        print("".join(Sd))

if __name__ == "__main__":
    main()

Solution (Python):
S = list(str(input()))
Q = int(input())
s1 = []
s2 = []
b = []
num_rev = 0
for i in range(Q):
  a = list(map(str, input().split()))
  if len(a) == 1:
    num_rev += 1
  else:
    if (num_rev + int(a[1]))%2==0:
      s2.append(a[2])
    else:
      s1.append(a[2])
if num_rev % 2 == 0:
  s1 = list(reversed(s1))
  s1.extend(S)
  s1.extend(s2)
  print(''.join(s1))
else:
  s2 = list(reversed(s2))
  s2.extend(list(reversed(S)))
  s2.extend(s1)
  print(''.join(s2))

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

start = ''
end = ''

rvs = False

for i in range(q):
  query = input()
  if query[0] == '1':
    if rvs == False:
      rvs = True
    else:
      rvs = False
  else:
    t, f, c = query.split()
    if f == '1':
      if rvs == False:
        start = c + start
      else:
        end = end + c
    else:
      if rvs == False:
        end = end + c
      else:
        start = c + start

if rvs == False:
  print(start + s + end)
else:
  ans = start + s + end
  print(ans[::-1])

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

Solution (Python):
from collections import deque

def T2F1(org_S,rev_S, C, jdg):
    if jdg%2 == 0:
        org_S.appendleft(C)
        rev_S.append(C)
        return org_S, rev_S
    else:
        org_S.append(C)
        rev_S.appendleft(C)
        return org_S, rev_S
def T2F2(org_S, rev_S, C,jdg):
    if jdg%2 == 0:
        org_S.append(C)
        rev_S.appendleft(C)
        return org_S, rev_S
    else:
        org_S.appendleft(C)
        rev_S.append(C)
        return org_S, rev_S
    
org_S = deque(list(input()))
rev_S = deque(list(reversed(org_S)))
Q = int(input())
jdg = 0

for i in range(Q):
    A = list(input().split())
    if A[0] == "1":
        jdg += 1
    elif A[1] == "1":
        T2F1(org_S, rev_S, A[2],jdg)
    else:
        T2F2(org_S, rev_S, A[2],jdg)
        
if jdg%2 ==0:
    org_S = "".join(org_S)
    print(org_S)
    
else:
    rev_S = "".join(rev_S)
    print(rev_S)

Solution (Python):
S = input()
Q = int(input())
Query = []
kaisu = 0
sento = ""
for i in range(Q):
    Query.append(input().split())

for i in Query:
    if int(i[0]) == 1:
        kaisu = kaisu + 1
    elif int(i[0]) == 2:
        if kaisu % 2 == 0:
            if int(i[1]) == 1:
                sento = sento + i[2]
            else:
                S = S + i[2]
        else:
            if int(i[1]) == 2:
                sento = sento + i[2]
            else:
                S = S + i[2]
S = ''.join(list(reversed(sento))) + S
if kaisu % 2 == 0:
    print(S)
else:
    print(''.join(list(reversed(S))))

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

mode = True  
pre = []
aft = []
for query in Query:
    if query[0] == '1':
        
        mode = not mode
    else:
        if (mode and (query[1] == '1')) or (not mode and (query[1] == '2')):
            pre.append(query[2])
        else:
            aft.append(query[2])
    
ans = ''.join(pre[::-1]) +  S + ''.join(aft)
    
if not mode:
    ans = ans[::-1]
 
print(ans)

Solution (Python):
from collections import deque

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

flag = 0

for i in range(Q):
  A = input().split()
  if A[0] == "1":
    if flag == 0:
      flag = 1
    else:
      flag = 0
  else:
    if A[1] == "1" and flag == 0 or A[1] == "2" and flag == 1:      
      a.appendleft(A[2])
    else:
      a.append(A[2])

if flag == 1:
  a.reverse()
print("".join(a))

Solution (Python):
s = input()
q = int(input())
qs = [input().split() for i in range(q)]
n = False
left= right = ""
for operation in qs:
    if operation[0] == '1':
        n = not n
    if operation[0] == '2':
        t , f, c = operation
        f = int(f)
        if n:
            f = 1 if f != 1 else 2
        if f == 1:
            left=c+left
        elif f == 2:
            right+=c
s = left+s+right
print(s[::-1] if n else s)

Solution (Python):
s = input()
q = int(input())
qs = [input().split() for i in range(q)]
n = False
left= right = ""
for operation in qs:
    if operation[0] == '1':
        n = not n
    if operation[0] == '2':
        t , f, c = operation
        f = int(f)
        if n:
            f = 1 if f != 1 else 2
        if f == 1:
            left=c+left
        elif f == 2:
            right+=c
s = left+s+right
print(s[::-1] if n else s)

Solution (Python):


import glob



REL_PATH = 'ABC\\158\\D'


TOP_PATH = 'C:\\AtCoder'

class Common:

    problem = []
    index = 0

    def __init__(self, rel_path):
        self.rel_path = rel_path

    def initialize(self, path):
        file = open(path)
        self.problem = file.readlines()
        self.index = 0
        return

    def input_data(self):
        try:
            IS_TEST
            self.index += 1
            return self.problem[self.index-1]

        except NameError:
            return input()

    def resolve(self):
        pass

    def exec_resolve(self):
        try:
            IS_TEST
            for path in glob.glob(TOP_PATH + '\\' + self.rel_path + '/*.txt'):
                print("Test: " + path)
                self.initialize(path)
                self.resolve()
                print("\n\n")
        except NameError:
            self.resolve()


class Solver(Common):

    def resolve(self):

        S = self.input_data().split()[0]
        Q = int(self.input_data())
        query = []

        zengo = True
        add_first = ""
        add_last = ""

        for i in range(Q):
            tmp = [j for j in self.input_data().split()]
            if tmp[0] == "1":
                query.append([1])
            else:
                query.append([2, int(tmp[1]), tmp[2]])

        for q in query:
            if q[0] == 1:
                zengo = not zengo
            else:
                if zengo:
                    if q[1] == 1:
                        add_first = q[2] + add_first
                    else:
                        add_last = add_last + q[2]
                else:
                    if q[1] == 2:
                        add_first = q[2] + add_first
                    else:
                        add_last = add_last + q[2]

        result = add_first + S + add_last
        if zengo:
            print(result)
        else:
            print(result[::-1])




solver = Solver(REL_PATH)
solver.exec_resolve()

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

vector = 1
before = ""
after = ""
for i in range(Q):
    Query = input()
    if Query[0] == "1":
        vector *= -1
    elif vector == 1 and Query[0:3] == "2 1":
        before = Query[4] + before
    elif vector == 1 and Query[0:3] == "2 2":
        after = after + Query[4]
    elif vector == -1 and Query[0:3] == "2 1":
        after = after + Query[4]
    elif vector == -1 and Query[0:3] == "2 2":
        before = Query[4] + before


if vector==1:
    print(before + S + after)
else:
    print(after[::-1]+S[::-1]+before[::-1])