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())
isForward = True
for _ in range(Q):
  v = input().split()
  if v[0] == '1':
    isForward = (isForward == False)
    continue
  if isForward:
    if v[1] == '1':
      S.appendleft(v[2])
    else:
      S.append(v[2])
  else:
    if v[1] == '1':
      S.append(v[2])
    else:
      S.appendleft(v[2])
if isForward == False:
  S = reversed(S)
print(''.join(S))

Solution (Python):
from collections import deque
S = str(input())
Q = int(input())
Qer = [list(map(str,input().split())) for i in range(Q)]
flag = True
anss = deque(S)
for i in range(Q):
    if Qer[i][0] == "1":
        flag = not flag
    else:
        if flag ==True:
            if Qer[i][1] =="1":
                anss.appendleft(Qer[i][2])
            else:
                anss.append(Qer[i][2])
        else:
            if Qer[i][1] =="2":
                anss.appendleft(Qer[i][2])
            else:
                anss.append(Qer[i][2])
anss=''.join(anss)
if flag == True:
    print(anss)
else:
    anss = anss[::-1]
    print(anss)

Solution (Python):
s = input()
q = int(input())
status = 1
top = []
bottom = []
for i in range(q):
    query = [i for i in input().split()]
    if query[0] == "1":
        status *= -1
    else:
        if (query[1] == "1" and status == 1) or (query[1] == "2" and status == -1):
            top.append(query[2])
        else:
            bottom.append(query[2])

tmp = "".join(top[::-1]) + s + "".join(bottom)
print(tmp if status == 1 else tmp[::-1])

Solution (Python):
from collections import deque

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

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

if rev:
    print("".join(list(s)[::-1]))
else:
    print("".join(list(s)))

Solution (Python):
from collections import deque

s = input()
s = list(s)
q = int(input())
query = list(list(input().split()) for _ in range(q))

d = deque(s)

rs = False

for i in range(q):
    qry = query[i]
    if qry[0] == '1':
        rs = not rs
    else:
        if qry[1] == '1':
            if not rs:
                d.appendleft(qry[2])
            else:
                d.append(qry[2])
        else:
            if not rs:
                d.append(qry[2])
            else:
                d.appendleft(qry[2])
                
d = list(d)
if rs:
    d.reverse()
                
d = ''.join(d)
print(d)

Solution (Python):
s = input()
q = int(input())
a = 0
c = ""
d = ""
for i in range(q):
    b = list(input().split())
    if b[0] == "1":
        if a == 0:
            a = 1
        else:
            a = 0
    else:
        if (a, b[1]) == (0, "1") or (a, b[1]) == (1, "2"):
            c = b[2]+c
        else:
            d += b[2]
s = c+s+d
if a == 0:
    print(s)
else:
    print(s[::-1])

Solution (Python):
s = input()
q = int(input())
a = 0
c = ""
d = ""
for i in range(q):
    b = list(input().split())
    if b[0] == "1":
        if a == 0:
            a = 1
        else:
            a = 0
    else:
        if (a, b[1]) == (0, "1") or (a, b[1]) == (1, "2"):
            c += b[2]
        else:
            d += b[2]
s = c[::-1]+s+d
if a == 0:
    print(s)
else:
    print(s[::-1])

Solution (Python):
from collections import deque
s = deque(input())
q = int(input())
a = 0
c = ""
for i in range(q):
    b = list(input().split())
    if b[0] == "1":
        if a == 0:
            a = 1
        else:
            a = 0
    else:
        if (a, b[1]) == (0, "1") or (a, b[1]) == (1, "2"):
            s.appendleft(b[2])
        else:
            s.append(b[2])
if a == 0:
    for i in range(len(s)):
        c += s.popleft()
else:
    for i in range(len(s)):
        c += s.pop()
print(c)

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

rs_cnt = 0

head = []
tail = []

for i in range(q):
    qry = query[i]
    if qry[0] == '1':
        rs_cnt += 1
    else:
        if qry[1] == '1':
            if rs_cnt % 2 == 0:
                head.append(qry[2])
            else:
                tail.append(qry[2])
        else:
            if rs_cnt % 2 == 0:
                tail.append(qry[2])
            else:
                head.append(qry[2])
            
if rs_cnt % 2 == 0:
    head.reverse()
    head = ''.join(head)
    s = ''.join(s)
    tail = ''.join(tail)
    print(head + s + tail)
else:
    s.reverse()
    tail.reverse()
    head = ''.join(head)
    s = ''.join(s)
    tail = ''.join(tail)
    print(tail + s + head)

Solution (Python):
from collections import deque
S = list(input()) 
d = deque()
for s in S:
    d.append(s)
q = int(input())
o = 0

for _ in range(q):
    qu = list(input().split())
    if len(qu) == 1:
        o = -1 if o == 0 else 0
    if len(qu) == 3:
        if qu[1] == '1':
            d.appendleft(qu[2]) if o == 0 else d.append(qu[2])
        if qu[1] == '2':
            d.append(qu[2]) if o == 0 else d.appendleft(qu[2])

r = ''
if o == 0:
    for _ in range(len(d)):
        r += d.popleft()
else:
    for _ in range(len(d)):
        r += d.pop()

print(r)

Solution (Python):
import collections

d = collections.deque()
for i in input():
    d.append(i)
q = int(input())
is_head = True

for _ in range(q):
    tmp = input().split(" ")
    if (len(tmp) == 1):
        is_head = not is_head
    else:
        t, f, c = tmp
        if (f == '1'):
            if (is_head):
                d.appendleft(c)
            else:
                d.append(c)
        else:
            if (is_head):
                d.append(c)
            else:
                d.appendleft(c)

ans = ""
if is_head:
    while len(d) > 0:
        ans += d.popleft()
else:
    while len(d) > 0:
        ans += d.pop()
print(ans)

Solution (Python):

from collections import deque

s = input()
q = int(input())
deq = deque(s)
cur = 1
for _ in range(q):
    query = input().split()
    if query[0] == "1":
        cur ^= 1
    else:
        f, c = query[1:]
        f = int(f) - 1
        if cur ^ f:
            deq.appendleft(c)
        else:
            deq.append(c)
ans = ""
while len(deq) > 0:
    ans += deq.popleft() if cur else deq.pop()
print(ans)

Solution (Python):
from collections import deque
S = deque(list(input()))
Q = int(input())
rev_cnt = 0
for i in range(Q):
    query = [_ for _ in input().split()]
    if len(query) == 1:
        rev_cnt += 1
    else:
        if query[1] == '1':
            if rev_cnt%2 == 0:
                S.appendleft(query[2])
            else:
                S.append(query[2])
        else:
            if rev_cnt%2 == 0:
                S.append(query[2])
            else:
                S.appendleft(query[2])
if rev_cnt % 2 == 0:
    print(''.join(S))
else:
    S.reverse()
    print(''.join(S))

Solution (Python):

import sys
from collections import deque

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

S = deque(sr())
Q = ir()
rev = 0
for _ in range(Q):
    q = sr().split()
    if q[0] == '1':
        rev ^= 1
    else:
        _, f, c = q
        if (int(f)^rev)&1:
            S.appendleft(c)
        else:
            S.append(c)

if rev&1:
    S.reverse()

print(''.join(S))

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

top = ""
isReverse = False
for i in range(q):
  qq = input().split()
  if qq[0] == "1":
    isReverse = not isReverse
  else:
    if qq[1] == "1":
      if isReverse: s+= qq[2]
      else: top += qq[2]
    else:
      if isReverse: top += qq[2]
      else: s+= qq[2]
        
if isReverse: s = s[::-1] + top
else: s = top[::-1] + s
print(s)

Solution (Python):

import sys

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

S = sr()
Q = ir()
rev = 0
top = ''
tail = ''

for _ in range(Q):
    x = sr().split()
    if x[0] == '1':
        rev ^= 1
    else:
        _, f, c = x
        if (int(f)^rev) & 1:
            top += c
        else:
            tail += c

answer = top[::-1] + S + tail
if rev&1:
    answer = answer[::-1]
print(answer)

Solution (Python):
from collections import deque
s = 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 copy
import itertools

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


ct = 0
ls = []
rs = []
for i in range(q):
    qtmp = input().split()
    
    t = int(qtmp[0])
    if(t == 1):
        ct += 1
        ct = ct % 2
    else:
        f = int(qtmp[1])
        if(ct+1 == f):
            ls.append( qtmp[2])
        else:
            rs.append( qtmp[2])
if(ct == 1):
    tmp = rs
    rs = ls
    ls = tmp
    s = s[::-1]
for i in range(len(ls)):
    print(ls[len(ls)-i-1], end='')
print(s, end='')
for i in range(len(rs)):
    print(rs[i], end='')
print("")

Solution (Python):
import collections
q = collections.deque(list(raw_input()))
operations = [raw_input().split(' ') for _ in range(int(raw_input()))]
rev = False

for op in operations:
	if len(op) == 1:
		rev = not(rev)
	elif len(op) == 3:
		_, where, char = op
		where = bool(int(where) - 1) ^ rev
		if where:			
			q.append(char)
		else:
			q.appendleft(char)

print ''.join(list(q)[:: (-1 if rev else 1)])

Solution (Python):
import sys
import re
import math
import collections
import decimal
import bisect
import itertools







INF = sys.maxsize


ni = lambda: int(sys.stdin.readline())
ns = lambda: map(int, sys.stdin.readline().split())
na = lambda: list(map(int, sys.stdin.readline().split()))





def main():
    s = input()
    n = ni()

    front = ""
    rear = ""

    cnt = 0
    for i in range(n):
        d = list(sys.stdin.readline().split())
        if int(d[0]) == 1:
            cnt = (cnt + 1) % 2
        else:
            if not cnt:
                if int(d[1]) == 1:
                    front = d[2] + front
                else:
                    rear = rear + d[2]
            else:
                if int(d[1]) == 1:
                    rear = rear + d[2]
                else:
                    front = d[2] + front

    if cnt:
        s = s[::-1]
        front = front[::-1]
        rear = rear[::-1]
        ans = rear + s + front
    else:
        ans = front + s + rear

    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
S = input()
Q = int(input())
flag = 0   
head = ""
tail = ""
for i in range(Q):
    quary = list(map(str, input().split()))
    if quary[0] == "1":
        if flag == 0:
            flag = 1
        else:
            flag = 0
    else:
        if flag == 1:
            if quary[1] == "1":
                tail = tail+quary[2]
            else:
                head = quary[2]+head
        else:
            if quary[1] == "1":
                head = quary[2]+head
            else:
                tail = tail+quary[2]
S = head+S+tail
if flag == 1:
    S = S[::-1]
print(S)

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

dire = 0
front = ""
back = ""

for i in range(Q):
    quary = [_ for _ in input().split()]
    if len(quary) == 1:
        T = quary[0]
        dire += 1
    else:
        T, F, C = quary
        if F == "1":
            if dire % 2 == 0:
                front += C
            else:
                back += C
        else:
            if dire % 2 == 1:
                front += C
            else:
                back += C

S = front[::-1] + S + back

if dire % 2 == 1:
    print(S[::-1])
else:
    print(S)

Solution (Python):
import sys
stdin = sys.stdin
sys.setrecursionlimit(10 ** 7)

def LI(): return list(map(int, stdin.readline().split()))
def LS(): return list(stdin.readline())

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

head = True
right = ''
left = ''
for i in range(q):
    query = input().split(' ')
    
    if query[0]=='1':
        head = not head
    else:
        if query[1]=='1':
            if head:
                left += query[2]
            else:
                right += query[2]
        else:
            if head:
                right += query[2]
            else:
                left += query[2]
    
s = left[::-1]+s+right
if head:
    print(s)
else:
    print(s[::-1])

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

s = input().strip()
q = int(input())
query = [list(input().strip().split()) for _ in range(q)]

left = ''
right = ''
t = False
for ls in query:
    if len(ls) == 1:
        t = not t
    else:
        two, f, c = ls
        if f == '1':
            if t:
                right += c
            else:
                left += c
        else:
            if t:
                left += c
            else:
                right += c

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

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

for _ in range(q):
    t = input().split()
    if t[0]=="1":
        f = not f
    elif t[0]=="2":
        t[2]=list(t[2])
        if f:
            t[2]=t[2][::-1]
            if t[1]=="1":
                s.extend(t[2])
            elif t[1]=="2":
                s.extendleft(t[2])
        else:
            if t[1]=="1":
                s.extendleft(t[2])
            elif t[1]=="2":
                s.extend(t[2])
if f:
    print("".join(s)[::-1])
else:
    print("".join(s))

Solution (Python):
S=input()
S_=list(S)
Q=int(input())
F1=[]
F2=[]
Q_=[]
for i in range(Q):
  Q_.append(list(input().split()))
count=0  
for i in range(len(Q_)):
  if Q_[i]==['1']:
    count+=1
  elif Q_[i][0:2]==['2','1']:
    if count%2==0:
      F1.append(Q_[i][2])
    else:
      F2.append(Q_[i][2])
  else:
    if count%2==0:
      F2.append(Q_[i][2])
    else:
      F1.append(Q_[i][2])
SS=F1[::-1]+S_+F2                
if count%2==0:
  print(''.join(SS))
else:
  print(''.join(SS[::-1]))

Solution (Python):
import sys
from collections import deque
s = input()
q = int(input())
dq = deque(s)
rev = False
for i in range(q):
    t = input().split()
    if t[0] == "1":
        rev = not rev
    else:
        if t[1] == "1":
            if rev:
                dq.append(t[2])
            else:
                dq.appendleft(t[2])
        else:
            if rev:
                dq.appendleft(t[2])
            else:
                dq.append(t[2])
s = "".join(list(dq))
if rev:
    print(s[::-1])
else:
    print(s)

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

up = 0

head = []
tail = []

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

    if query[0] == "1":
        if up:
            up = 0
        else:
            up = 1

    elif query[0] == "2":
        if query[1] == "1":
            if up:
                tail.append(query[2])
            else:
                head.append(query[2])
        elif query[1] == "2":
            if up:
                head.append(query[2])
            else:
                tail.append(query[2])

head=''.join(map(str,head))
tail=''.join(map(str,tail))

ans = head[::-1] + s + tail

if up:
    ans = ans[::-1]
print(ans)

Solution (Python):

"""
Created on Thu May  7 13:56:40 2020

@author: shinba
"""
import sys
from collections import deque

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

dq = deque(s)
rev = False

for i in range(q):
    t = sys.stdin.readline().rstrip().split()
    if t[0] == "1":
        rev = not rev
    else:
        if t[1] == "1":
            if rev:
                dq.append(t[2])
            else:
                dq.appendleft(t[2])
        else:
            if rev:
                dq.appendleft(t[2])
            else:
                dq.append(t[2])

s = "".join(list(dq))

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

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

dq = deque(s)
rev = False

for _ in range(q):
    t = list(input().split())
    if t[0] == "1":
        rev = not rev
    elif t[0] == "2":
        f = t[1]
        c = t[2]
        if f == "1":
            if rev:
                dq.append(c)
            else:
                dq.appendleft(c)
        else:
            if rev:
                dq.appendleft(c)
            else:
                dq.append(c)
                
if rev:
    print("".join(list(dq)[::-1]))
else:
    print("".join(list(dq)))

Solution (Python):
from collections import deque
S = deque(input())
Q = int(input())
cnt = 0
for i in range(Q):
  x = list(input().split())
  if(int(x[0]) == 1):
    cnt += 1
  else:
    if((cnt+int(x[1])) % 2 == 1):
      S.appendleft(x[2])
    else:
      S.append(x[2])

S = list(S)
if(cnt % 2 == 1):
  S = S[::-1]
print("".join(S))

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

S = input()

dq = deque(S.rstrip())
Q = int(input());

reverse = False

for _ in range(Q):
    q = input().split()
    if q[0] == '1':
        reverse ^= 1
    else:
        F, C = q[1:]
        if reverse:
            if F == '1':
                dq.append(C)
            else:
                dq.appendleft(C)
        else:
            if F == '1':
                dq.appendleft(C)
            else:
                dq.append(C)

ans = ""
if reverse:
    while dq:
        ans += dq.pop()
else:
    while dq:
        ans += dq.popleft()
print(ans)

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

S = input()

dq = deque(S.rstrip())
Q = int(input());

reverse = False

for _ in range(Q):
    q = input().split()
    if q[0] == '1':
        reverse ^= 1
    else:
        F, C = q[1:]
        if (F == '1') ^ reverse:
            dq.appendleft(C)
        else:
            dq.append(C)

ans = ""
if reverse:
    while dq:
        ans += dq.pop()
else:
    while dq:
        ans += dq.popleft()
print(ans)

Solution (Python):
S=input()
Q=int(input())
sousa=[list(map(str,input().split())) for i in range(Q)]
counter=0
up=''
down=''
for i in sousa:
    if i[0]=='1':

        counter+=1
    if i[0]=='2':
        if counter%2==0:
            if i[1]=='1':
                up=i[2]+up
            if i[1]=='2':
                down=down+i[2]
        else:
            if i[1]=='1':
                down=down+i[2]
            if i[1]=='2':
                up=i[2]+up   
                
S=up+S+down
if counter%2==1:
    S=S[::-1]

print(S)

Solution (Python):
s=input()
q=int(input())
head=[]
tail=[]
rev=False
for _ in range(q):
  tmp=list(input().split())
  if tmp[0]=='1':
    if rev==False:
      rev=True
    else:
      rev=False
  if tmp[0]=='2':
    if rev==False:
      if tmp[1]=='1':
        head.append(tmp[2])
      if tmp[1]=='2':
        tail.append(tmp[2])
    if rev==True:
      if tmp[1]=='1':
        tail.append(tmp[2])
      if tmp[1]=='2':
        head.append(tmp[2])
head=''.join(map(str,head))
tail=''.join(map(str,tail))
ans=head[::-1]+s+tail
if rev==True:
  ans=ans[::-1]
print(ans)

Solution (Python):
from collections import deque

s = input()
q = int(input())
Q = [list(map(str, input().split())) for i in range(q)]

ans = deque(s)

reverse_num = 0
for i in range(q):
  if int(Q[i][0]) == 1:
    reverse_num += 1
  else:
    if int(Q[i][1]) == 1:
      if reverse_num % 2 == 0:
        ans.appendleft(Q[i][2])
      else:
        ans.append(Q[i][2])
    else:
      if reverse_num % 2 != 0:
        ans.appendleft(Q[i][2])
      else:
        ans.append(Q[i][2])

res = ''
if reverse_num % 2 == 0: 
  for i in range(len(ans)):
    res += ans.popleft()
  print(res)
else:
  for i in range(len(ans)):
    res += ans.pop()
  print(res)

Solution (Python):
from collections import deque

S = input()
Q = int(input())
s = deque(S)
reverse = True

for i in range(Q):
    tmp = list(input().split())
    if tmp[0] == '1':
        reverse = not reverse
    if tmp[0] == '2':
        F = tmp[1]
        C = tmp[2]
        if F == '1':
            if reverse:
                s.appendleft(C)
            else:
                s.append(C)
        else:
            if reverse:
                s.append(C)
            else:
                s.appendleft(C)
if reverse:
    print(''.join(s))
else:
    print(''.join(list(s)[::-1]))

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

s = input()
q = int(input())
ans = deque(s)
cnt = 0
for _ in range(q):
    inp = readline().rstrip()
    if inp[0] == "2":
        if inp[2] == "1":
            if cnt % 2 == 0:
                ans.appendleft(inp[4])
            else:
                ans.append(inp[4])
        else:
            if cnt % 2 == 1:
                ans.appendleft(inp[4])
            else:
                ans.append(inp[4])
    else:
        cnt += 1

if cnt % 2 == 1:
    ans = list(ans)[::-1]
    
print("".join(ans))

Solution (Python):
s=input()
q=int(input())
dir=True
ans=s
right=""
left=""
query=[]
for i in range(q):
  temp=input()
  if temp[0] == "1":
    dir = dir ^ True
  if temp[0] == "2":
    t,f,c = temp.split()

    if (f == "1" and dir) or (f=="2" and not dir):
      left=c+left
    else:
      right=right+c

def reverse(s):
  t=""
  for i in range(len(s)-1,-1,-1):
    t+=s[i]
  return t

if dir:
  print(left+ans+right)
else:
  t=reverse(right)+reverse(ans)+reverse(left)
  print(t)

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

    rev = 0
    append_front_rev = ''
    append_rear = ''

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

        if (T[0] == "1"):
            rev ^= 1
            continue

        if (T[0] == "2" and T[1] == "1"):
            if rev == 0:
                append_front_rev += T[2]
            else:
                append_rear += T[2]
            continue

        if (T[0] == "2" and T[1] == "2"):
            if rev == 0:
                append_rear += T[2]
            else:
                append_front_rev += T[2]
            continue

    ans = append_front_rev[::-1] + S + append_rear
    if rev:
        ans = ans[::-1]

    print("".join(ans))


resolve()

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

for i in range(q):
    inp=list(input().split())
    if inp[0] == "1":
        quelist.append([False,0,0])
    else:
        quelist.append([True,int(inp[1]),inp[2]])

gflag=True
maelist=[]
atolist=[]
for que in quelist:
    if que[0]:
        if gflag:
            if que[1] ==1:
                maelist.append(que[2])
            else:
                atolist.append(que[2])
        else:
            if que[1] ==1:
                atolist.append(que[2])
            else:
                maelist.append(que[2])
    else:
        if gflag:
            gflag=False
        else:
            gflag=True

if gflag:
    maelist.reverse()
    maelist.append(s)
    maelist.extend(atolist)
    output="".join(maelist)
else:
    atolist.reverse()
    s=s[::-1]
    atolist.append(s)
    atolist.extend(maelist)
    output="".join(atolist)

print(output)

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

n = S
mode = 0
operations = []
deq = deque(n)
for i in range(Q):
    operations.append(input().split())
for op in operations:
    if op[0] == "1":
        mode += 1
        mode %= 2
    elif int(op[1]) + mode == 2:
        deq.append(op[2])
    else:
        deq.appendleft(op[2])
if mode == 0:
    print("".join(deq))
else:
    print("".join(reversed(deq)))

Solution (Python):
from collections import deque
D=deque(input().rstrip())
Q=int(input())
count=0
for _ in range(Q):
	Y=list(input().split())
	if Y[0]=="1":
		count=(count+1)%2
	else:
		if Y[1]=="1":
			if count==0:
				D.appendleft(Y[2])
			else:
				D.append(Y[2])
		else:
			if count==0:
				D.append(Y[2])
			else:
				D.appendleft(Y[2])
D=list(D)
if count==0:
	print("".join(D))
else:
	D.reverse()
	print("".join(D))

Solution (Python):
from collections import deque
s = deque(input())
q = int(input())
l = list(list(input().split()) for _ in range(q))
t = 0
for i in l:
  if len(i)==1:
    t += 1
  else:
    a,f,c = i
    if (f=="1" and t%2==0) or (f=="2" and t%2==1) :
      s.appendleft(c)
    else:
      s.append(c)
if t%2==1:
  s.reverse()
print(*s,sep="")

Solution (Python):
from collections import deque
s=deque(list(input()))
q=int(input())
rev=0
for i in range(q):
  t=list(input().split(" "))
  if t[0]=="1":
    rev=rev+1
  else:
    if t[1]=="1":
      if rev%2==0:
        s.appendleft(t[2])
      else:
        s.append(t[2])
    else:
      if rev%2==0:
        s.append(t[2])
      else:
        s.appendleft(t[2])
if rev%2==0:
  print("".join(s))
else:
  s.reverse()
  print("".join(s))

Solution (Python):

from collections import deque

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

for i in range(q):
    L = list(input().split())
    if L[0] == '1' :
        head = (head + 1)%2
    if L[0] == '2' :
        if head == 0:
            if L[1] == '1' :
                s.appendleft(L[2])
            if L[1] == '2' :
                s.append(L[2])
        if head == 1:
            if L[1] == '1' :
                s.append(L[2])
            if L[1] == '2' :
                s.appendleft(L[2])
if head == 0:
    print(''.join(s))
else: 
    print(''.join(reversed(s)))

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

flag = 1
s1 = ''

for i in range(Q):
	q = input()
	if len(q) == 1:
		s1,S = S,s1
	else:
		T,F,C = q.split(' ')
		if int(F) == 1:
			s1 = s1 + C
		else:
			S = S + C

print(s1[::-1] + S)

Solution (Python):
S = input()
Q = int(input())
t = ''
for i in range(Q):
  instructions = list(map(str, input().split()))
  if instructions[0] == '1':
    S, t = t, S
  else:
    if instructions[1] == '1':
      t += instructions[2]
    else:
      S += instructions[2]
print(t[::-1] + S)

Solution (Python):
from collections import deque
s=deque(list(input()))
q=int(input())
rev=0
for i in range(q):
    t=list(input().split())
    if t[0]=="1":
        rev=rev+1
    else:
        if t[1]=="1":
            if rev%2==0:
                s.appendleft(t[2])
            else:
                s.append(t[2])
        else:
            if rev%2==0:
                s.append(t[2])
            else:
                s.appendleft(t[2])
if rev%2==0:
    print("".join(s))
else:
    s.reverse()
    print("".join(s))

Solution (Python):
import sys
sys.setrecursionlimit(10 ** 6)

int1 = lambda x: int(x) - 1
def II(): return int(input())

def MI(): return map(int, input().split())
def MI1(): return map(int1, input().split())

def LI(): return list(map(int, input().split()))
def LI1(): return list(map(int1, input().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]

INF = float('inf')

from collections import deque
def solve():
    S = deque(list(input()))

    Q = II()
    rev = False
    for i in range(Q):
        q = list(input().split())
        
        if q[0] == '1':
            rev = ~rev
        else:
            op = q[1]
            if (op == '1' and not rev) or op == '2' and rev:
                S.appendleft(q[2])
            else:
                S.append(q[2])

    S = list(S)
    if rev:
        S = S[::-1]

    print(''.join(S))

if __name__ == '__main__':
    solve()

Solution (Python):
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop
from itertools import permutations, accumulate
from math import sqrt, ceil, floor, factorial
from bisect import bisect_left, bisect_right
from copy import deepcopy
from operator import itemgetter
from fractions import gcd
import sys
def I(): return int(input())  
def Is(): return map(int, input().split())  
def LI(): return list(map(int, input().split()))  
def TI(): return tuple(map(int, input().split()))  
def IR(n): return [I() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def TIR(n): return [TI() for _ in range(n)]
def S(): return input()
def Ss(): return input().split()  
def LS(): return list(input())  
def SR(n): return [S() for _ in range(n)]
def SsR(n): return [Ss() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
sys.setrecursionlimit(1000000)
MOD = 1000000007
INF = float('inf')

s = S()
q = I()
flag = 0
ans = deque(s)

for i in range(q):
    query = input()
    if query[0] == '1':
        flag = flag ^ 1  
    else:
        if (query[2] == '1' and not flag) or (query[2] == '2' and flag):
            ans.appendleft(query[4])
        else:
            ans.append(query[4])

if not flag:
    print("".join(ans))
else:
    print("".join(reversed(ans)))

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

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

print(t[::-1] + s)

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

s_mae = ''
s_ato = s

for _ in range(n):
    ipt = input()

    if ipt[0] == '1':
        s_mae, s_ato = s_ato, s_mae
    else:
        _, id_, s_ = ipt.split()
        is_head = True if id_ == '1' else False
        if is_head:
            s_mae += s_
        else:
            s_ato += s_

print(s_mae[::-1] + s_ato)

Solution (Python):

s, _, *q = open(0)
g = ""
s = s.strip()
for q in q:
  q = q.strip().split()
  if q[0] == "1":
    g, s = s, g
  else:
    if q[1] == "1":
      g += q[2]
    else:
      s += q[2]
print(g[::-1] + s)