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
import sys
input=sys.stdin.readline
s=deque([input().rstrip()])
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]
		if f=="1":
			if rev%2==0:
				s.appendleft(c)
			else:
				s.append(c)
		else:
			if rev%2==0:
				s.append(c)
			else:
				s.appendleft(c)
ans=""
for x in s:
	ans+=x
if rev%2==0:
	print(ans)
else:
	print(ans[::-1])

Solution (Python):
from collections import deque

myqueue = deque(input())


normal = True

num = int(input())

for i in range(num):
    query = input().split()

    if len(query) == 1:
        normal = not normal
    else:
        if query[1] == "1":
            if normal:
                myqueue.appendleft(query[2])
            else:
                myqueue.append(query[2])
        else:
            if normal:
                myqueue.append(query[2])
            else:
                myqueue.appendleft(query[2])


counter = len(myqueue)

if normal:
    for i in range(counter):
        print(myqueue.popleft(), end="")
else:
    for i in range(counter):
        print(myqueue.pop(), end="")

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


rev = False
forw = ""
back = ""
for i in range(Q):
  query = input().split()
  if query[0] == "1":
    rev = not rev
  else:
    if (not rev and query[1] == "1") or (rev and query[1] == "2"):
      forw = query[2] + forw
    else:
      back += query[2]

if rev:
  S = back[::-1] + S[::-1] + forw[::-1]
else:
  S = forw + S + back
print(S)

Solution (Python):
from collections import deque

S = [input()]
d = deque(S)

Q = int(input())

pos = 0

for q in range(Q):
  myinput = list(input().split())
  
  if len(myinput) == 1:
      pos += 1
  else:
      if myinput[1] == '1':
          if pos% 2 == 0:
               d.appendleft(myinput[2])   
          elif pos%2 !=0:
              
              d.append(myinput[2])   
      elif myinput[1] == '2':
          if pos% 2 == 0:
              
              d.append(myinput[2])   
          elif pos%2 !=0:
              
              d.appendleft(myinput[2])   

if pos%2 == 0:                 
    print(''.join(d))
else:
    print((''.join(d))[::-1])

Solution (Python):
s=input()
q = int(input())
toggle = "ok"
top = ""
last = ""

for i in range(q):
    si = input().split()
    if si[0] == "1":
        if toggle == "ok":
            toggle = "re"
        else:
            toggle = "ok"
    else:
        if si[1] == "1" and toggle == "ok":
            top += si[2] 
        elif si[1] == "2" and toggle == "re":
            top += si[2] 
        elif si[1] == "1" and toggle == "re":
            last += si[2]
        elif si[1] == "2" and toggle == "ok":
            last += si[2]
if toggle == "ok":
    retop = ''.join(list(reversed(top)))
    print(retop, end="")
    print(s,end="")
    print(last,end="")
else:
    relast = ''.join(list(reversed(last)))
    print(relast,end="")
    res = ''.join(list(reversed(s)))
    print(res,end="")
    print(top,end="")

Solution (Python):
import sys
input = lambda: sys.stdin.readline().rstrip()
input_nums = lambda: list(map(int, input().split()))

s = input()
n = int(input())
front = ''
end = ''
front_flag = True 
for i in range(n):
    q = input()
    if len(q) == 1:
        front_flag = not front_flag
    else:
        _, f, c = q.split()
        if f == '1':
            if front_flag:
                front += c
            else:
                end += c
        else:
            if not front_flag:
                front += c
            else:
                end += c

if front_flag:
    s = front[::-1] + s + end
else:
    s = end[::-1] + s[::-1] + front

print(s)

Solution (Python):
s = list(input())
q = int(input())
ss = list()
flip = False
for i in range(q):
    tfc = input()
    if tfc[0] == '1':
        flip = not flip
    else:
        if flip:
            if tfc[2] == '1':
                s.append(tfc[4])
            else:
                ss.append(tfc[4])
        else:
            if tfc[2] == '1':
                ss.append(tfc[4])
            else:
                s.append(tfc[4])
ss.reverse()
s = ss + s
if flip:
    s.reverse()
print("".join(s))

Solution (Python):
s = input()
num = int(input())
t = "X"
reverse = False

for _ in range(num):
    row = input().split()
    if len(row) == 1:
        if reverse == False:
            reverse = True
        else:
            reverse = False
    else:
        x = row[1]
        y = row[2]
        if x == "1" and reverse:
            t += y
        elif x == "1" and not reverse:
            t = y + t
        elif x == "2" and reverse:
            t = y + t
        else:
            t += y

if reverse:
    t = t.replace("X", s)
    print(t[::-1])
else:
    t = t.replace("X", s)
    print(t)

Solution (Python):
a = input()
l = list(a)
list_ins = [0] * 200000
ins_cnt = 0
list_app = [0] * 200000
app_cnt = 0

q = int(input())

default_order = True
for i in range(q):
  x = input().split()
  if x[0] == "1":
    default_order = default_order ^ True
  elif x[0] == "2":
    insert_type = default_order ^ False
    if x[1] == "2":
      insert_type = not(insert_type)
    if insert_type:
      list_ins[ins_cnt] = x[2]
      ins_cnt += 1
    else:
      list_app[app_cnt] = x[2]
      app_cnt += 1
ret = ""
if ins_cnt > 0:
  list_ins = list_ins[0:ins_cnt]
else:
  list_ins = []
if app_cnt > 0:
  list_app = list_app[0:app_cnt]
else:
  list_app = []
if default_order:
  list_ins.reverse()
  ret = ''.join(list_ins) + ''.join(l) + ''.join(list_app)
else:
  list_app.reverse()
  l.reverse()
  ret = ''.join(list_app) + ''.join(l) + ''.join(list_ins)

print(ret)

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

Solution (Python):
s = input()
n = int(input())
a = "*"
spin = 1

for i in range(n):
    q = input()
    if q[0] == "1":
        spin *= -1
        a = a[::-1]
    else:
        if q[2] == "1":
            a = q[4] + a
        else:
            a = a + q[4]
            
    if len(a) == 100:
        s = a[:a.index("*")] + s[::spin] + a[a.index("*")+1:]
        a = "*"
        spin = 1
            
print(a[:a.index("*")] + s[::spin] + a[a.index("*")+1:])

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

c = 0
R = ""
L = ""
for _ in range(Q):
    T = input().split()
    if len(T) == 1:
        c +=1
    else:
        if T[1] == "1":
            if c % 2 == 0:
                L = T[2] + L
            else:
                R = R + T[2]
        else:
            if c % 2 == 0:
                R = R + T[2]
            else:
                L = T[2] + L
if c % 2 == 0:
    print(L + S + R)
else:
    print("".join(list(reversed(L+S+R))))

Solution (Python):

from collections import deque
s = deque(str(input()))
q_count = int(input())
rev = False
for i in range(q_count):
    q = list(input().split())
    if q[0] == '1':
        rev = not(rev)
    else:
        if q[1] == '1':
            if rev:
                
                s.append(q[2])
            else:
                
                s.appendleft(q[2])
        else:
            if rev:
                
                s.appendleft(q[2])
            else:
                
                s.append(q[2])
if rev:
    s.reverse()
print(*s, sep='')

Solution (Python):





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

    

    rq = [0] * (Q+1)
    for i in range(Q):
        if len(q[i]) == 1:
            rq[i+1] = rq[i] + int(q[i][0])
        else:
            rq[i+1] = rq[i]
        rq[i+1] %= 2

    b = []
    a = []

    for i in range(Q):
        if len(q[i]) != 1:
            if (q[i][1] == '1' and rq[i+1] == 0) or (q[i][1] == '2' and rq[i+1] == 1):
                b.append(q[i][2])
            else:
                a.append(q[i][2])

    b = b[::-1]

    if rq[Q]%2 == 1:
        b, a = reversed(a), reversed(b)
        s = s[::-1]


    ans = ''.join([''.join(b), s, ''.join(a)])

    print(ans)


if __name__ == '__main__':
    main()

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 collections

s = raw_input()
qn = int(raw_input())
qs = [raw_input().split() for _ in xrange(qn)]

direction = 1
deq = collections.deque(s)

for q in qs:
    if q[0] == '1':
        direction *= -1
    else:
        _, fi, ci = q
        fi = -1 if fi == '1' else +1
        if direction * fi == -1:
            deq.appendleft(ci)
        else:
            deq.append(ci)
    


ans = None
ln = len(deq)
if direction == 1:
    ans = ''.join(deq.popleft() for _ in xrange(ln))
else:
    ans = ''.join(deq.pop() for _ in xrange(ln))
print ans

Solution (Python):
S = input()
Q = int(input())
num_rev = 0
S_front = [""] * Q
S_back = [""] * Q
 
for i in range(Q):
  inp = input()
  if len(inp) == 1:
    num_rev += 1
  else:
    T, F, C = inp.split()
    if ((num_rev % 2 == 0) and int(F) == 1) or ((num_rev % 2 == 1) and int(F) == 2):
      S_front[i] = C
    else:
      S_back[i] = C

S = "".join(S_front)[::-1] + S + "".join(S_back)
if num_rev % 2 == 0:
  print(S)
else:
  print(S[::-1])

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

b_str = ""
f_str = ""

for q in range(Q):
  r = input().split()
  if len(r) == 1:
    reverse_flg = not reverse_flg
  else:
    
    if int(r[1]) == 1:
      if reverse_flg:
        f_str = f_str + r[2]
      else:
        b_str = r[2] + b_str
    else:
      if reverse_flg:
        b_str = r[2] + b_str
      else:
        f_str = f_str + r[2]

S = b_str + S + f_str

if reverse_flg:
  S = S[::-1]

print(S)

Solution (Python):
from collections import deque
S = input()
Q = int(input())
S = deque(S)
rev = False
for i in range(Q):
    T, *FC = input().split()
    if T == "1":
        rev = not rev
    else:
        if FC[0] == "1":
            if rev:
                S.append(FC[1])
            else:
                S.appendleft(FC[1])
        else:
            if rev:
                S.appendleft(FC[1])
            else:
                S.append(FC[1])

if rev:
    S.reverse()
print("".join(S))

Solution (Python):



def main():
    from collections import deque

    s = input()
    q = int(input())
    d = deque()
    d.append(s)
    reverse_count = 0

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

        if qi[0] == '1':
            reverse_count += 1
        else:
            if reverse_count % 2 == 0:
                if qi[1] == '1':
                    d.appendleft(qi[2])
                else:
                    d.append(qi[2])

            else:
                if qi[1] == '1':
                    d.append(qi[2])
                else:
                    d.appendleft(qi[2])

    c = ''.join(map(str, d))

    if reverse_count % 2 == 1:
        c = c[::-1]

    print(c)


if __name__ == '__main__':
    main()

Solution (Python):
S = list(input())
Q = int(input())
t_count = 0
before = []
after = []

for i in range(Q):
  list_ = list(input().split())
  T = int(list_[0])
  if T == 1:
    t_count += 1
  else:
    F, C = int(list_[1]), list_[2]
    if F == 1:
      if t_count%2 == 0:
        before.append(C)
      else:
        after.append(C)
    else:
      if t_count%2 == 0:
        after.append(C)
      else:
        before.append(C)
before.reverse()
S.extend(after)
before.extend(S)
if t_count%2 == 1:
  before.reverse() 
ans = ''.join(before)
print(ans)

Solution (Python):


import sys
iim = lambda: map(int, input().rstrip().split())
from collections import deque

def resolve():
    S = deque(input().rstrip())
    Q = int(input())

    rev = False
    for ti, *arg in (line.split() for line in sys.stdin):

        if ti == "1":
            rev = not rev
        else:
            fi, ci = arg
            fi = fi == "1"
            if fi ^ rev:
                S.appendleft(ci)
            else:
                S.append(ci)
    if rev:
        S.reverse()
    print(*S, sep="")


if __name__ == "__main__":
    resolve()

Solution (Python):



def main():
    from collections import deque

    s = input()
    q = int(input())
    d = deque()
    d.append(s)
    reverse_count = 0

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

        if qi[0] == '1':
            reverse_count += 1
        else:
            if reverse_count % 2 == 0:
                if qi[1] == '1':
                    d.appendleft(qi[2])
                else:
                    d.append(qi[2])

            else:
                if qi[1] == '1':
                    d.append(qi[2])
                else:
                    d.appendleft(qi[2])

    c = ''.join(map(str, d))

    if reverse_count % 2 == 1:
        c = c[::-1]

    print(c)


if __name__ == '__main__':
    main()

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


M = 10 ** 6
start_position = int(M / 2)
left_position = start_position
right_position = start_position + len(S)
result = ["-"] * M
for i in range(len(S)):
    
    result[i + left_position] = S[i]




reverse_flag = False

for i in range(Q):
    
    
    q = query_list[i]
    
    t = int(q[0])
    if t == 1:
        reverse_flag = not reverse_flag
    else:
        t, f, c = q.split()
        f = int(f)
        c = c
        if f == 1:
            if reverse_flag:
                result[right_position] = c
                right_position += 1
            else:
                result[left_position - 1] = c
                left_position -= 1
        else:
            if reverse_flag:
                result[left_position - 1] = c
                left_position -= 1
            else:
                result[right_position] = c
                right_position += 1
    
    
    





result = "".join(result[left_position:right_position])
if reverse_flag:
    result = result[::-1]
print(result)

Solution (Python):
import sys

s = sys.stdin.readline().strip()

q = int(sys.stdin.readline().strip())

class Node:
    def __init__(self, c):
        self.prev = None
        self.next = None
        self.c = c
s = list(s)

head = Node(s[0])
tail = None
prev = head
for i in range(1, len(s)):
    crt = Node(s[i])
    prev.next = crt
    crt.prev = prev
    prev = crt
tail = prev

order = 0
for _ in range(q):
    l = sys.stdin.readline().strip()
    if l[0] == "1":
        order = (order + 1) %2
    else:
        t, f, c = l.split()
        n = Node(c)
        if (f == "1" and order == 0) or ( f== "2" and order==1):
            
            n.next = head
            head.prev = n
            head = n
        else:
            
            tail.next = n
            n.prev = tail
            tail = n
if order == 0:
    crt = head
    while crt != None:
        print(crt.c, end="")
        crt = crt.next
else:
    crt = tail
    while crt != None:
        print(crt.c, end="")
        crt = crt.prev
print("")

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


def main(s, q, query):
    addflag = 'front'

    s_front = ''
    s_back = ''

    for i in range(q):
        if query[i][0] == '1':
            if addflag == 'front':
                addflag = 'back'
            elif addflag == 'back':
                addflag = 'front'
        elif query[i][0] == '2':
            if addflag == 'front' and query[i][1] == '1':
                s_front = query[i][2] + s_front
                
            elif addflag == 'front' and query[i][1] == '2':
                s_back = s_back + query[i][2]
                
            elif addflag == 'back' and query[i][1] == '1':
                s_back = s_back + query[i][2]
                
            elif addflag == 'back' and query[i][1] == '2':
                s_front = query[i][2] + s_front
                

    if addflag == 'back':
        s = ''.join(reversed(list(s)))
        s_tmp = ''.join(reversed(list(s_front)))
        s_front = ''.join(reversed(list(s_back)))
        s_back = s_tmp

    return s_front + s + s_back


print(main(S, Q, QUERY))

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):
s1=list(input())
q=int(input())
s0=[]
f=1
for i in range(q):
  a=list(input().split())
  
  if int(a[0])==1:
    f=(f+1)%2
  else:
    if (f+int(a[1]))%2==0:
      s0.append(a[2])
    else:
      s1.append(a[2])
if f==1:
  print("".join(s0[::-1])+"".join(s1))
else:
  print("".join(s1[::-1])+"".join(s0))

Solution (Python):
import sys
from collections import deque
input = sys.stdin.readline
S=deque(input()[:-1])
Q=int(input())
r = False
for _ in range(Q):
  qs = input()[:-1].split(' ')
  if len(qs) == 1:
    r = not r
  else:
    if (qs[1] == '1' and (not r)) or (qs[1] == '2' and r):
      S.appendleft(qs[2])
    else:
      S.append(qs[2])
if r:
  print(''.join(reversed(S)))
else:
  print(''.join(S))

Solution (Python):
s = input()
totalre = 0
q = int(input())
gm = []
gu = []
for _ in range(q):
    query = list(input().split())
    if int(query[0])==1:
        totalre += 1
    else:
        if (totalre + int(query[1]))%2 != 0:
            gm.append(query[2])
        else:
            gu.append(query[2])

if totalre%2==0:
    ans = ''.join(gm)[::-1] + s + ''.join(gu)
else:
    ans = ''.join(gu)[::-1] + s[::-1] + ''.join(gm)
print(ans)

Solution (Python):

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

forward_flag = 1

front = ""
back = ""

for i in range(Q):
    n = list(map(str, input().split()))

    
    if len(n)==1:
        if forward_flag==1:
            forward_flag = 0
        else:
            forward_flag = 1
    else:
        a, b, c = n
        if (forward_flag==1 and b=="1") or (forward_flag==0 and b=="2"):
            front += c
        else:
            back += c

if forward_flag==1:
    print(front[::-1], end="")
    print(S, end="")
    print(back)
else:
    print(back[::-1], end="")
    print(S[::-1], end="")
    print(front)

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()

flag = 0
cnt = 0
n = int(input())

que_ar = [input() for _ in range(n)]

f_x = ["" for _ in range(n)]
b_x = ["" for _ in range(n)]


f = n-1
b = 0

for i in range(n):
    que = que_ar[i]
    if(len(que) == 1):
        cnt += 1
    else:
        que = que.split()
        if(cnt%2 == 0):
            if(que[1]=="1"):
                f_x[f] = que[2]
                f -= 1
            else:
                b_x[b] = que[2]
                b += 1
        else:
            if(que[1]=="1"):
                b_x[b] = que[2]
                b += 1
            else:
                f_x[f] = que[2]
                f -= 1

s = "".join(f_x)+s+"".join(b_x)             
                
if(cnt%2 == 0):
    print(s)
else:
    print(s[::-1])

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


def main():
    a = deque(input())
    a.pop()

    a_append = a.append
    a_appendleft = a.appendleft

    Q = int(input())
    count = 0
    for _ in range(Q):
        q = list(input().split())
        if q[0] == "1":
            count += 1
        else:
            if count % 2 == 0:
                if q[1] == "1":
                    a_appendleft(q[2])
                else:
                    a_append(q[2])
            else:
                if q[1] == "1":
                    a_append(q[2])
                else:
                    a_appendleft(q[2])

    b = list(a)
    if count % 2 != 0:
        b.reverse()
    print("".join(b))


if __name__ == '__main__':
    main()

Solution (Python):
from collections import deque
def main():
  s = deque(list(input()))
  q = int(input())
  sr = s.reverse
  sa = s.append
  sal = s.appendleft

  cnt = 0
  for _ in range(q):
    t = input()
    if t[0] == "1":
      cnt += 1
    else:
      _,f,c = t.split(" ")
      if f == "1":
        if cnt%2==1:
          sa(c)
        else:
          sal(c)
      else:
        if cnt%2==1:
          sal(c)
        else:
          sa(c)
  if cnt%2 == 1:
    sr()
  print("".join(list(s)))

if __name__=="__main__":
  main()

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):
from collections import deque
S = deque(input())
Q = int(input())
count = 0
for i in range(Q):
    l = list(input().split())
    if len(l) == 1:
        count +=1
    else:
        l[1] = int(l[1])
        if count%2 == 1:
            if l[1] == 1:
                l[1] = 2
            else:
                l[1] = 1
        if l[1] == 1:
            S.appendleft(l[2])
        else:
            S.append(l[2])

if count%2 == 0:
    print(''.join(list(S)))
else:
    S.reverse()
    print(''.join(list(S)))

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

def sol():
    s=input().rstrip()
    q=int(input())
    x=0
    a,b="",""
    for i in range(q):
        l=tuple([i for i in input().split()])
        if l[0]=="1":
            x=(x+1)%2
        elif l[1]=="1":
            if x==0: a=l[2]+a
            else: b=b+l[2]
        elif l[1]=="2":
            if x==0: b=b+l[2]
            else: a=l[2]+a
    s=a+s+b
    print(s if x==0 else s[::-1])

if __name__=="__main__":
    sol()

Solution (Python):
from collections import defaultdict
from collections import deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random


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


def I(): return int(sys.stdin.readline())


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


def S(): return list(sys.stdin.readline())[:-1]


def IR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = I()
    return l

def LIR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = LI()
    return l
def SR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = S()
    return l
def LSR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = LS()
    return l





s = sys.stdin.readline().split()[0]
lines_count = I()

chars = deque(s)

def prepend(chars, ch):
    chars.appendleft(ch)

def append(chars, ch):
    chars.append(ch)

actions = {
    1: prepend,
    2: append
}

for i in range(lines_count):
    line = LS()

    if len(line) == 1:
        actions[1], actions[2] = actions[2], actions[1]
    else:
        if line[1][0] == '1':
            actions[1](chars, line[2][0])
        else:
            actions[2](chars, line[2][0])

chars = list(chars)
if actions[1] == append:
    chars.reverse()

print(''.join(chars))

Solution (Python):


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

reverse = False

mae = []
ushiro = []

for i in range(Q):
    query = input()
    if query == "1":
        reverse = not reverse

    else:
        a,b,c = list(query.split())
        if (b == "1" and not reverse) or (b == "2" and reverse):
            mae.append(c)
        else:
            ushiro.append(c)

if reverse:
    origin = [S[::-1]]
    ans = ushiro[::-1]
    ans.extend(origin)
    ans.extend(mae)
    print(''.join(ans))
else:
    ans = mae[::-1]
    origin = [S]
    ans.extend(origin)
    ans.extend(ushiro)
    print(''.join(ans))

Solution (Python):
from collections import deque

S = input()
result_string = deque(S)
Q = int(input())
reverse_flag = 1
for idx in range(Q):
  new_in = input().split()
  if len(new_in) == 1:
    reverse_flag = -1*reverse_flag
  else:
    
    side_to_append = reverse_flag*(2*int(new_in[1])-3)
    if side_to_append == 1:
      result_string.append(new_in[2])
    else:
      result_string.appendleft(new_in[2])
if reverse_flag == -1: result_string.reverse()
print("".join(result_string))

Solution (Python):



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

rev = False
S1 = ['0'] * Q
S1i = 0
S2 = ['0'] * Q
S2i = 0

for q in range(Q):
    i = input()
    if i[0] == "1":
        rev = not rev
    elif not rev and i[2] == "1" or rev and i[2] == "2":
        
        S1[S1i] = i[4]
        S1i += 1
    else:
        
        S2[S2i] = i[4]
        S2i += 1

S1 = S1[0:S1i]
S2 = S2[0:S2i]
S1 = S1[::-1]

S = ''.join(''.join(S1) + S + ''.join(S2))

if rev:
    S = S[::-1]

print(S)

Solution (Python):
s = input()
q = int(input())
rev_flag = False
x_lis = []
y_lis = []
for _ in range(q):
    i = input()
    if i == '1':
        rev_flag = not rev_flag
    else:
        a,b,c = i.split()
        if rev_flag:
            if b == '1':
                
                y_lis.append(c)
            else:
                
                x_lis.append(c)
        else:
            if b == '1':
                
                x_lis.append(c)
            else:
                
                y_lis.append(c)
x = ''.join(reversed(x_lis))
y = ''.join(y_lis)
if rev_flag:
    print(''.join(list(reversed(x+s+y))))
else:
    print(x+s+y)

Solution (Python):

from collections import deque

s = input()
q = int(input())
x, y = deque(), deque()
r = 0
for _ in range(q):
    query = input().split()
    if query[0] == "1":
        r += 1
        x, y = y, x
    elif query[0] == "2":
        f, c = query[1], query[2]
        if f == "1":
            if r % 2 == 0:
                x.appendleft(c)
            else:
                x.append(c)
        elif f == "2":
            if r % 2 == 1:
                y.appendleft(c)
            else:
                y.append(c)

if r % 2 == 1:
    s = s[::-1]
    x = reversed(x)
    y = reversed(y)
print("".join(x) + s + "".join(y))

Solution (Python):
from collections import deque

S = deque(input())
n= int(input())
condition=True

for i in range(n):
  Q = [i for i in input().split()]
  if Q[0] =='1':
    if condition ==True:
      condition=False
    else:
      condition=True
  else:
    if Q[1]=='1':
      if condition ==True:
        S.appendleft(Q[2])
      else:
        S.append(Q[2])
    else:
      if condition==True:
        S.append(Q[2])
      else:
        S.appendleft(Q[2])
    

if condition ==False:
  S.reverse()

for i in S:
  print(i, end='')

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):
from collections import deque
S = deque(input())
N = int(input())
Q = []
gyaku = 0
for i in range(N):
    q = input()
    if len(q)>1:
        q = q.split()
    else:
        q = [q]
    Q.append(q)

for i in range(N):
    if len(Q[i])==1:
        gyaku +=1
        
    if len(Q[i])>1:
        if Q[i][1]=='1'and gyaku%2==0:
            S.appendleft(Q[i][2])
        elif Q[i][1]=='1'and gyaku%2==1:
            S.append(Q[i][2])
            
        elif Q[i][1]=='2'and gyaku%2==0:
            S.append(Q[i][2])
        else:
            S.appendleft(Q[i][2])
            
if gyaku%2==0:
    
    print(''.join(S))
else:
    S = list(S)
    print(''.join(S[::-1]))

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

flag = False
add_after = ""
add_before = ""
for q_num in q_list:
    if int(q_num[0]) == 1:
        if flag == False:
            flag = True
        else:
            flag = False
    
    else:
        if int(q_num[1]) == 1 and flag == False or int(q_num[1]) == 2 and flag == True:
            add_before = q_num[2] + add_before
        else:
            add_after = add_after + q_num[2]

result = add_before + s + add_after
if flag:
    print(result[::-1])
else:
    print(result)

Solution (Python):
S = list(map(str, input()))
q = int(input())
Q =[list(map(str, input().split())) for k in range(q)]
rev = False
f1 = []
f2 = []
def pls(n):
    global rev
    if n[0] == "1":
        rev = not rev
    else:
        if n[1] == "1":
            if rev:
                f2.append(n[2])
            else:
                f1.append(n[2])
        else:
            if rev:
                f1.append(n[2])
            else:
                f2.append(n[2])

for k in range(q):
    pls(Q[k])

if rev:
    S.reverse()
    f2.reverse()
    ans = f2 + S + f1
else:
    f1.reverse()
    ans = f1 + S + f2

print("".join(map(str, ans)))