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(input())
q = int(input())

rev_flag = False
for i in range(q):
    tmp_in = list(input().split())

    if tmp_in[0] == '1':
        if rev_flag:
            rev_flag = False
        else:
            rev_flag = True
    else:
        if rev_flag:
            if tmp_in[1] == '1':
                s.append(tmp_in[2])
            else:
                s.appendleft(tmp_in[2])
        else:
            if tmp_in[1] == '1':
                s.appendleft(tmp_in[2])
            else:
                s.append(tmp_in[2])

if rev_flag:
    s.reverse()

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

Solution (Python):
from collections import deque


S = deque(list(input()))
Q = int(input())
f = True
for i in range(Q):
    query = input().split(' ')
    if len(query) == 1:
        f = not f
    else:
        if (query[1] == '1' and f) or (query[1] == '2' and not f):
            S.appendleft(query[2])
        else:
            S.append(query[2])
S = "".join(list(S))
if not f:
    S = S[::-1]
print(S)

Solution (Python):

S = input()
Q = int(input())
f = True
head = []
tail = []
for i in range(Q):
    query = input().split(' ')
    if len(query) == 1:
        f = not f
    else:
        if (query[1] == '1' and f) or (query[1] == '2' and not f):
            head.append(query[2])
        else:
            tail.append(query[2])

S = ''.join(head)[::-1]+S+''.join(tail)
if not f:
    S = S[::-1]
print(S)

Solution (Python):
S = input()
S2 = ""
Q = int(input())
reverse = 1
for i in range(Q):
    list = input()
    if list[0] == "1":
        reverse *= -1
    else:
        if reverse == 1:
            if list[2] == "2":
                S += list[4]
            else:
                S2 += list[4]
        else:
            if list[2] == "2":
                S2 += list[4]
            else:
                S += list[4]
if reverse == 1:
    print(S2[::-1] + S)
else:
    print(S[::-1] + S2)

Solution (Python):
s = input()
q = int(input())
cnt = 0
front = ''
rear = ''
for _ in range(q):
    t = input()
    if t[0] == '1':
        cnt += 1
    elif t[2] == '1':
        if cnt%2 == 1:
            rear = rear + t[4]
        else:
            front = front + t[4]
    else:
        if cnt%2 == 1:
            front = front + t[4]
        else:
            rear = rear + t[4]
if cnt%2 == 1:
    s = (front[::-1] + s + rear)[::-1]
else:
    s = front[::-1] + s + rear
print(s)

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

reverse=False
for i in range(Q):
  ins=input()
  if len(ins)==1:
    if reverse:
      reverse=False
    else:
      reverse=True
  else:
    q,f,c=map(lambda x: x, ins.split())
    
    if f=="1":
      if reverse:
        back.append(c)
        
      else:
        front.appendleft(c)
        
    else:
      if reverse:
        front.appendleft(c)
      else:
        back.append(c)
        
back=list(back)
front=list(front)
if reverse:
  back=back[::-1]
  front=front[::-1]
  S=list(S)[::-1]
  print("".join(back)+"".join(S) +"".join(front))
else:
  print("".join(front)+S+"".join(back))

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

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

rev = 1
fr = ""
rr = ""

for i in range(q):
    ss=input()
    if int(ss[0])==1:
        rev *= -1
    else:
        f,c=int(ss[2]),ss[4]
        if f == 2:
            f = -1
        
        if f*rev ==1:
            fr+=c
        else:
            rr+=c

if rev == 1:
    ans = fr[::-1]+s+rr
else:
    ans = rr[::-1]+s[::-1]+fr
print(ans)

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

head = ''
tail = ''
rev = False

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

    if Query_i[0] == '1':
        rev = not rev
    
    elif Query_i[0] == '2':
        F_i, C_i = Query_i[1], Query_i[2]

        if not rev and F_i == '1':
            head += C_i
        elif not rev and F_i == '2':
            tail += C_i
        elif rev and F_i == '1':
            tail += C_i
        elif rev and F_i == '2':
            head += C_i

res = ''
if not rev:
    for c in reversed(head):
        res += c
    res += S
    res += tail
else:
    for c in reversed(tail):
        res += c
    for c in reversed(S):
        res += c
    res += head

print(res)

Solution (Python):
S = input()
Q = [input() for i in range(int(input()))]
rev = 0
l,r = -1,1
res ={0:S}
for q in Q:
  if q == "1":
    rev = (rev+1)%2
  else:
    t,f,c = q.split()
    if int(f) + rev == 2 :
      res[r] = c
      r += 1
    else:
      res[l] = c
      l -= 1

if rev == 0:
  for i in range(l+1,r):
    print(res[i],end="")
else:
  for i in range(r-1,l,-1):
    print("".join(list(reversed(res[i]))),end="")

Solution (Python):
S = input()
Q = int(input())
flag = 0
H = ""
for _ in range(Q):
    q = list(input().split())
    if q[0] == "1":
        flag += 1
    else:
        if flag%2 == 0:
            if q[1] == "1":
                H = H+q[2]
            else:
                S = S+q[2]
        else:
            if q[1] == "1":
                S = S+q[2]
            else:
                H = H+q[2]
H = H[::-1]
H = H+S
if flag%2 == 0:
    print(H)
else:
    H = H[::-1]
    print(H)

Solution (Python):




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

    rev_count = 0
    s_prev = ""
    s_suffix = ""
    for i in range(q):
        query = input().strip()
        if query[0] == "1":
            rev_count += 1
        else:
            _, f, c = query.split()
            if (f == "1" and rev_count % 2 == 0) or (f == "2" and rev_count % 2 == 1):
                s_prev += c
            else:
                s_suffix += c

    if rev_count % 2 == 0:
        print(s_prev[::-1] + s + s_suffix)
    else:
        print(s_suffix[::-1] + s[::-1] + s_prev)


if __name__ == '__main__':
    main()

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

Solution (Python):
import sys
readline = sys.stdin.readline
s = readline().strip()
n = int(readline())
rev = False
h = []
t = []
for i in range(n):
    q = readline()
    if q.startswith("1"):
        rev = not rev
    if q.startswith("2 1"):
        if not rev:
            h.append(q[-2])
        else:
            t.append(q[-2])
    if q.startswith("2 2"):
        if not rev:
            t.append(q[-2])
        else:
            h.append(q[-2])
s = "".join(h[::-1]) + s + "".join(t)
if rev:
    print(s[::-1])
else:
    print(s)

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

Solution (Python):
from collections import deque

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

S_list = list(S)

r = 1
a = []
b = []
d = deque(a)

for i in range(Q):
    q = input().split()
    if q[0] == '1':
        r *= -1
        continue
    elif len(q) == 3:
        if q[1] == '1':
            if r == -1:
                b.append(q[2])
            else:
                d.appendleft(q[2])
        else:
            if r == -1:
                d.appendleft(q[2])
            else:
                b.append(q[2])
                
answer = list(d) + S_list + b

if r == -1:
    answer = answer[::-1]
    
print(''.join(answer))

Solution (Python):
import itertools
import math
import sys
import heapq
from collections import Counter
from collections import deque
from fractions import gcd
from functools import reduce

sys.setrecursionlimit(4100000)
from functools import lru_cache

INF = 1 << 60


s = input()
q = int(input())
cnt = 0
left = []
right = []
for i in range(q):
    query = input().split()
    if len(query) == 1:
        cnt += 1
    else:
        f = query[1]
        c = query[2]
        if cnt % 2 == 0:
            if f == "1":
                
                left.append(c)
            else:
                
                right.append(c)
        else:
            if f == "1":
                
                right.append(c)
            else:
                
                left.append(c)

left = left[::-1]
right = "".join(right)
left = "".join(left)
ans = "".join([left, s, right])

if cnt % 2 != 0:
    ans = ans[::-1]

print(ans)

Solution (Python):
from collections import deque

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

rev = True

for i in range(q):
    q = list(input().split())
    if(int(q[0]) == 1):
        rev = rev ^ True
    else:
        n = int(q[1])
        if((n == 1 and rev) or (n == 2 and not rev)):
            s.appendleft(q[2])
        else:
            s.append(q[2])


txt = ''
if rev:
    txt = ''.join(s)
else:
    while not(not s):
        txt += s.pop()

print(txt)

Solution (Python):
start = []
end = []

def query1(b):
    return not b

def query2(F, C, string, b):
    if (F == '1' and b) or (F == '2' and not b):
        start.append(C)
        return C + string
    else:
        end.append(C)

S = input()
Q = int(input())
direction = True

for i in range(Q):
    Query = input().split()
    if len(Query) == 1:
        direction = query1(direction)
    else:
        query2(Query[1], Query[2], S, direction)

if direction:
    print(''.join(start[::-1]) + S + ''.join(end))
else:
    print(''.join(end[::-1]) + S[::-1] + ''.join(start))

Solution (Python):
from collections import deque
s = deque(input())
n = int(input())
q = []
for i in range(n):
  query = list(input().split())
  q.append(query)
  
a = 1
for i in range(n):
  if int(q[i][0]) == 1:
    a *= -1
  else:
    if int(q[i][1]) == 1:
      if a > 0:
        s.appendleft(q[i][2])
      else:
        s.append(q[i][2])
    else:
      if a > 0:
        s.append(q[i][2])
      else:
        s.appendleft(q[i][2])
s = list(s)
s = "".join(s)
if a > 0:
  print(s)
else:
  s = s[::-1]
  print(s)

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

Solution (Python):
s=input()
n=int(input())
t=""
for _ in range(n):
  q=input().split()
  if q[0]=="1":
    t,s=s,t
  else:
    if q[1]=="1":
      t+=q[2]
    else:
      s+=q[2]
t=t[::-1]
print(t+s)

Solution (Python):


S = input()
Q = int(input())
T = ""

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

T = T[::-1]
print(T + S)

Solution (Python):
t=list(input())
q=int(input())
s=[]

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

moji=''.join(s)
print(moji)

Solution (Python):
S = input()
Q = int(input())
Query = [input() for _ in range(Q)]
flag = True
forward = ""
backward = ""
for i in range(Q):
    if Query[i][0] == "1":
        if flag:
            flag = False
        else:
            flag = True
    else:
        if Query[i][2] == '1':
            if flag:
                forward = Query[i][4] + forward
            else:
                backward = backward + Query[i][4]
        else:
            if flag:
                backward = backward + Query[i][4]
            else:
                forward = Query[i][4] + forward

if flag:
    ans = forward + S + backward
else:
    forward = forward[::-1]
    backward = backward[::-1]
    S = S[::-1]
    ans = backward + S + forward
print(ans)

Solution (Python):
from collections import deque

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

flag = 1

for _ in range(q):
    tfc = input().split()
    if tfc[0] == "1":
        flag *= -1

    elif tfc[0] == "2":
        if (tfc[1] == "1" and flag == 1) or (tfc[1] == "2" and flag == -1):
            s.appendleft(tfc[2])
        elif (tfc[1] == "1" and flag == -1) or (tfc[1] == "2" and flag == 1):
            s.append(tfc[2])
else:

    if flag == -1:
        s = list(s)[::-1]

print("".join(s))

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

rev_flag = 0

bef = ''
aft = ''

for i in range(Q):
    if Query[i][0] == '1':
        rev_flag = (rev_flag + 1) % 2
    if Query[i][0] == '2':
        if Query[i][1] == '1':
            if rev_flag == 0:
                bef = Query[i][2] + bef
            elif rev_flag == 1:
                aft = aft + Query[i][2]
        elif Query[i][1] == '2':
            if rev_flag == 1:
                bef = Query[i][2] + bef
            elif rev_flag == 0:
                aft = aft + Query[i][2]

S = bef + S + aft
if rev_flag == 1:
    S = S[::-1]

print(S)

Solution (Python):
s = [_ for _ in input()]

rev = False

qn = int(input())

ins = []

for i in range(qn):
    q = input().split()
    if int(q[0])==1:
        rev = not rev
        continue

    if rev:
        if int(q[1])==1:
            s.append(q[2])
        else:
            ins.append(q[2])
    else:
        if int(q[1])==1:
            ins.append(q[2])
        else:
            s.append(q[2])

ins = ins[::-1]
ins.extend(s)

if rev:
    print("".join(ins[::-1]))
    exit()
print("".join(ins))

Solution (Python):
S = input()
Q = int(input())
Query = [input() for _ in range(Q)]
flag = True
s = "!"
for i in range(Q):
    if Query[i][0] == "1":
        if flag:
            flag = False
        else:
            flag = True
    else:
        if Query[i][2] == '1':
            if flag:
                s = Query[i][4] + s
            else:
                s = s + Query[i][4]
        else:
            if flag:
                s = s + Query[i][4]
            else:
                s = Query[i][4] + s
ans = s.replace("!", S)
if flag:
    print(ans)
else:
    print(ans[::-1])

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

cnt = 0
S2 = []

for i in range(q):
    if procedure[i][0] == '1':
        cnt += 1
    if procedure[i][0] == '2':
        if (procedure[i][1] == '1' and cnt%2 == 0) or (procedure[i][1] == '2' and cnt%2 == 1):
            S2.append(procedure[i][2])
        else:
            S.append(procedure[i][2])

S2.reverse()
S = S2 + S
if cnt%2 == 1:
    S.reverse()

print(''.join(S))

Solution (Python):
from collections import deque

s = deque(input().split())
n = int(input())
query_li = (input().split() for i in range(n))

flag = 1

for query in query_li:
    if int(query[0]) == 1:
        flag = flag * -1
    else:
        if int(query[1]) == 1:
            if flag == 1:
                s.appendleft(query[2])
            else:
                s.append(query[2])
        else:
            if flag == 1:
                s.append(query[2])
            else:
                s.appendleft(query[2])

s = ''.join(s)
if flag == -1:
    s = s[::-1]

print(s)

Solution (Python):
S = input()
Q = int(input())
is_norm = True  
s_head = ""     
s_tail = ""     

for i in range(0, Q):
    query = input().split()
    if int(query[0]) == 1:
        is_norm = not is_norm
    else:
        if is_norm:
            if int(query[1]) == 1:
                s_head = query[2] + s_head
            else:
                s_tail = s_tail + query[2]
        else:
            if int(query[1]) == 1:
                s_tail = s_tail + query[2]
            else:
                s_head = query[2] + s_head

if is_norm:
    print(s_head + S + s_tail)
else:
    print(s_tail[::-1] + S[::-1] + s_head[::-1])

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

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

    d = deque(S)
    mode = True

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

        if query[0] == "1":
            mode = not mode
        elif query[0] == "2":
            F, C = query[1], query[2]
            if F == "1":
                if mode:
                    d.appendleft(C)
                else:
                    d.append(C)
            elif F == "2":
                if mode:
                    d.append(C)
                else:
                    d.appendleft(C)

    s = list(d)
    if not mode:
        s = s[::-1]

    print(''.join(s))

if __name__ == "__main__":
    main()

Solution (Python):
from collections import deque

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

    d = deque(S)
    mode = True

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

        if query[0] == "1":
            mode = not mode
        elif query[0] == "2":
            F, C = query[1], query[2]
            if F == "1":
                if mode:
                    d.appendleft(C)
                else:
                    d.append(C)
            elif F == "2":
                if mode:
                    d.append(C)
                else:
                    d.appendleft(C)

    s = list(d)
    if not mode:
        s = s[::-1]

    print(''.join(s))

if __name__ == "__main__":
    main()

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

Solution (Python):

from collections import deque

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

is_reverse = False
text = deque(S)
for _ in range(Q):
    query = input()
    if query[0] == '1':
        is_reverse = not is_reverse
    else:
        if query[2] == '1':
            if is_reverse:
                text.append(query[4])
            else:
                text.appendleft(query[4])
        else:
            if is_reverse:
                text.appendleft(query[4])
            else:
                text.append(query[4])

if is_reverse:
    print(''.join(text)[::-1])
else:
    print(''.join(text))

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):
from collections import deque
S = deque(list(input().strip()))
Q = int(input().strip())
flag = 1
for i in range(Q):
  line = input().strip()
  if len(line) == 1:
    flag = 1 - flag
  else:
    _, F, C = line.strip().split()
    if flag:
      if int(F) == 1:
        S.appendleft(C)
      else:
        S.append(C)
    else:
      if int(F) == 1:
        S.append(C)
      else:
        S.appendleft(C)

if flag:
  print(''.join(S))
else:
  print(''.join(list(reversed(S))))

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

Inv = False 
front_str = [] 
back_str = [] 

for query_list in query:
    if query_list[0] == '1':
        Inv = not Inv
    else:
        if query_list[1] == '1': 
            if Inv:
                front_str.append(query_list[2])
            else:
                back_str.append(query_list[2])
        else:
            if Inv:
                back_str.append(query_list[2])
            else:
                front_str.append(query_list[2])

answer = "".join(back_str[::-1] + [s] + front_str)

if Inv:
    answer = answer[::-1]

print(answer)

Solution (Python):
from collections import deque

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

point = 0
for i in range(n):
  q = input().split()
  if q.pop(0)=='1':
    point = (point+1)%2
  else:
    _q = int(q.pop(0))-1
    if (point+_q)%2 == 0:
      s.appendleft(q.pop(0))
    else:
      s.append(q.pop(0))

if point == 1:
  s.reverse()

print(''.join(s))

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

S=front+S+back
if reverse==0:
    print(S)
else:
    S=S[::-1]
    print(S)

Solution (Python):
s = input()
Q = int(input())
r = -1
head = ''
tail = ''
for i in range(Q):
    t = input()
    if t[0] == '1':
        r *= -1
    else:
        if t[2] == '1':
            if r == 1:
                tail += t[4]
            else:
                head = t[4] + head
        else:
            if r == 1:
                head = t[4] + head
            else:
                tail += t[4]
if r == 1:
    print(tail[::-1] + s[::-1] + head[::-1])
else:
    print(head + s + tail)

Solution (Python):
from collections import deque
import sys

S = input()
Q = int(input())
t = deque(S)
rev = False

for _ in range(Q):
  Query = input().split()
  if Query[0] == '1':
    rev = not rev
    
  else:
    F = Query[1]
    C = Query[2]
    if F == '1':
      if rev:
        t.append(C)
      else:
        t.appendleft(C)
    else:
      if rev:
        t.appendleft(C)
      else:
        t.append(C)

result = ''.join(t) 
if rev:
  print(result[::-1])
else:
  print(result)

Solution (Python):
from collections import deque

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

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

Solution (Python):
s=input()
n=len("s")
q=int(input())
l=[]
start=""
end=""
m=0
for i in range(q):
    l.append(input())
    if l[i]=="1":
        m += 1

    else:
        due,f,c=l[i].split()
        if (f=="1" and m % 2==0) or (f !="1" and m % 2==1) :
            start += c
        else:
            end += c
if m % 2 ==0:

    start=start[::-1]
    s = start + s + end

else:
    s = s + end
    s = s[::-1]
    s = s + start

print(s)

Solution (Python):
S = input()
lQ = list(range(int(input())))
def main(S):
  T = True
  L = ''
  R = ''
  for i in lQ:
    q = list(input().split())
    if q[0]=='1':
      T = not(T)
    else:
      if (T and q[1]=='1') or (not(T) and q[1]=='2'):
        L += q[2]
      elif (T and q[1]=='2') or (not(T) and q[1]=='1'):
        R += q[2]
  SS = L[::-1] + S + R
  if T:
    print(SS)
  else:
    print(SS[::-1])
main(S)

Solution (Python):
s = input()
q = int(input())
mode = True
l = ''
r = ''
for _ in range(q):
    tfc = input().split()
    if int(tfc[0]) == 1:
        mode = not mode
    else:
        if mode:
            if int(tfc[1]) == 1:
                l += tfc[2]
            else:
                r += tfc[2]
        else:
            if int(tfc[1]) == 1:
                r += tfc[2]
            else:
                l += tfc[2]
print(l[::-1]+s+r if mode else r[::-1]+s[::-1]+l)

Solution (Python):
S = input()
Q = int(input())
head, tail = "", ""
reverse = False
for _ in range(Q):
    cmd = input().split()
    if cmd[0] == "1":
        reverse = not reverse
    else:
        top = (cmd[1] == "1") ^ reverse
        if top:
            head += cmd[2]
        else:
            tail += cmd[2]
print(tail[::-1] + S[::-1] + head) if reverse else print(head[::-1] + S + tail)

Solution (Python):
from collections import deque
s = input()
n = int(input())
query = []
for i in range(n):
    query.append(input().split())
cond = 1
word = deque(s)
for i in range(n):
    if query[i] == ['1']:
        cond *= -1
    else:
        if cond == 1:
            if query[i][1] == '1':
                word.appendleft(query[i][2])
            else:
                word.append(query[i][2])
        else:
            if query[i][1] == '2':
                word.appendleft(query[i][2])
            else:
                word.append(query[i][2])

result = ''.join(word)
if cond == -1:
    result = result[::-1]
print(result)

Solution (Python):
string = input()
N=int(input())
Que = []
for i in range(N):
	Que.append(input())

init = ""
end = ""
rev = 2
for Q in Que:	
	if int(Q[0]) == 1:
		if rev != 1:
			rev=1
		else:
			rev=2
	elif int(Q[0])==2:
		T,F,C = Q.split(' ')
		if rev == 1:
			if int(F) == 1:
				F =2
			else:
				F = 1
		if int(F) == 1:
			init = init+C
		elif int(F) == 2:
			end = end + C
final = init[-1::-1]+string+end
if rev == 1:
	print(final[-1::-1])
else:
	print(init[-1::-1]+string+end)

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

F, B = [], []
reverse = False
for i in range(Q):
	Query = input().split()
	if Query[0] == '1':
		reverse = not reverse
	else:
		if not reverse:
			if Query[1] == '1':
				F.append(Query[2])
			else:
				B.append(Query[2])
		else:
			if Query[1] == '1':
				B.append(Query[2])
			else:
				F.append(Query[2])

if not reverse:
	ans = "".join(reversed(F))
	ans += "".join(S)
	ans += "".join(B)
	print(ans)
else:
	ans = "".join(reversed(B))
	ans += "".join(reversed(S))
	ans += "".join(F)
	print(ans)

Solution (Python):
from collections import deque
S = deque(input())
N = int(input())
is_inversed = False
for i in range(N):
    Q = input()
    if Q[0] == '1':
        is_inversed = not (is_inversed)
    else:
        if (Q[2] == '1' and is_inversed) or (Q[2] == '2' and not (is_inversed)):
            S.append(Q[4])
        else:
            S.appendleft(Q[4])
if is_inversed:
    print(''.join(list(S)[::-1]))
else:
    print(''.join(S))

Solution (Python):
S = input()

flag = 1
S1 = ""
S2 = ""
N = int(input())
for i in range(N):
  line = input().split()
  if line[0] == "1":
    if flag == 1:
      flag = 2
    else:
      flag = 1
  else:
    if flag == 1:
      if line[1] == "1":
        S1 = line[2] + S1
      else:
        S2 = S2 + line[2]
    else:
      if line[1] == "1":
        S2 = S2 + line[2]
      else:
        S1 = line[2] + S1

S = S1 + S + S2
if flag == 2: S = S[::-1]
print(S)

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

top_list = []
end_list = []

flag = 1

for q in q_list:
    if q[0] == '1':
        flag *= -1
    else:
        if flag == 1:
            if q[1] == '1':
                top_list.append(q[2])
            else:
                end_list.append(q[2])
        else:
            if q[1] == '2':
                top_list.append(q[2])
            else:
                end_list.append(q[2])
if flag == 1:            
    print(''.join(top_list[::-1]) + s + ''.join(end_list))
else:
    print(''.join(end_list[::-1]) + s[::-1] + ''.join(top_list))

Solution (Python):
S = input()
Q = int(input())
turnover = False
suffix = ""
prefix = ""

for i in range(Q):
  TFC = input().split()
  T = int(TFC[0])
  if T == 1:
    
    turnover = not turnover
  elif T == 2:
    F = int(TFC[1])
    C = TFC[2]
    if turnover:
      if F == 1:
        F = 2
      else:
        F = 1
    if F == 1:
      prefix = prefix + C
      
    elif F == 2:
      suffix = suffix + C
      
S = prefix[-1::-1]+S+suffix
if turnover:
  S = S[-1::-1]

print(S)

Solution (Python):
from collections import deque

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

flag = False

for i in range(1, q+1):
    a = input().split()
    if len(a) == 1:
        flag = not flag
    else:
        _, f, c = a
        if (f == "1" and flag == False) or (f == "2" and flag == True):
            ans.extendleft(c)
        elif (f == "1" and flag == True) or (f == "2" and flag == False):
            ans.extend(c)
        else:
            print("fac")
            exit(1)

if flag:
    print("".join(list(ans)[::-1]))
else:
    print("".join(ans))

Solution (Python):
from collections import deque
d = deque()
d.extend(list(input()))
q = int(input())
z = 0
F = ""
R = ""
for i in range(q):
    a = tuple(input().split())
    if a[0] == "1":
        z += 1
        z %= 2
    else:
        if (int(a[1]) + z) % 2 == 0:
            d.append(a[2])
        else:
            d.appendleft(a[2])

ans = "".join(d)
if z % 2 == 0:
    print(ans)
else:
    print(ans[::-1])