Problem:
 Score : 400 points    Problem Statement Takahashi has a string S consisting of lowercase English letters.  Starting with this string, he will produce a new one in the procedure given as follows.  The procedure consists of Q operations. In Operation i  (1 \leq i \leq Q) , an integer T_i is provided, which means the following:    If T_i = 1 : reverse the string S .    If T_i = 2 : An integer F_i and a lowercase English letter C_i are additionally provided.   If F_i = 1 : Add C_i to the beginning of the string S .  If F_i = 2 : Add C_i to the end of the string S .     Help Takahashi by finding the final string that results from the procedure.      Constraints  1 \leq |S| \leq 10^5  S consists of lowercase English letters.  1 \leq Q \leq 2 \times 10^5  T_i = 1 or 2 .  F_i = 1 or 2 , if provided.  C_i is a lowercase English letter, if provided.         Input Input is given from Standard Input in the following format:  S  Q  Query_1  :  Query_Q   In the 3 -rd through the (Q+2) -th lines, Query_i is one of the following:  1   which means T_i = 1 , and:  2  F_i  C_i   which means T_i = 2 .      Output Print the resulting string.        Sample Input 1 a
4
2 1 p
1
2 2 c
1      Sample Output 1 cpa  There will be Q = 4 operations. Initially, S is a .    Operation 1 : Add p at the beginning of S . S becomes pa .    Operation 2 : Reverse S . S becomes ap .    Operation 3 : Add c at the end of S . S becomes apc .    Operation 4 : Reverse S . S becomes cpa .    Thus, the resulting string is cpa .       Sample Input 2 a
6
2 2 a
2 1 b
1
2 2 c
1
1      Sample Output 2 aabc  There will be Q = 6 operations. Initially, S is a .    Operation 1 : S becomes aa .    Operation 2 : S becomes baa .    Operation 3 : S becomes aab .    Operation 4 : S becomes aabc .    Operation 5 : S becomes cbaa .    Operation 6 : S becomes aabc .    Thus, the resulting string is aabc .       Sample Input 3 y
1
2 1 x      Sample Output 3 xy

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

hanten = -1
atama = ""
shiri = ""

for row in range(quantity):
  each = L[row]
  if each[0] == "1":
    hanten = hanten * -1
  else:
    if each[1] == "1":
      if hanten == -1:
        atama = atama + each[2]
      else:
        shiri = shiri + each[2]
    else:
      if hanten == -1:
        shiri = shiri + each[2]
      else:
        atama = atama + each[2]

atama = atama[::-1]
ans = atama + s + shiri

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

Solution (Python):
from collections import deque
S = deque(list(input()))
Q = int(input())
f = 0
for _ in range(Q):
  a, *b = input().split()
  f += int(a)
  if a == "2":
    if (int(b[0]) + f) % 2 == 1 :
      S.appendleft(b[1])
    else:
      S.append(b[1])
D = list(S)
print ("".join(D) if f % 2 == 0 else "".join(D[::-1]))

Solution (Python):
import sys
input = sys.stdin.readline
s=input().strip()
q=int(input())
from collections import deque
que=deque(s)
mae=1
for _ in range(q):
    query=list(input().split())
    if query[0]=="1":
        mae*= -1
    else:
        if mae==1:
            if query[1]=="1":
                que.appendleft(query[2])
            else:
                que.append(query[2])
        else:
            if query[1]=="1":
                que.append(query[2])
            else:
                que.appendleft(query[2])
    
if mae==1:
    print("".join(que))
else:
    print("".join(list(que)[::-1]))

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

tmp=0
s=S
t=''

for li in A:
    if li[0]=='1':
        tmp=(tmp+1)%2
    else:
        if (tmp+int(li[1]))%2 == 1:
            t += li[2]
        else:
            s += li[2]

if tmp == 0:
    print(t[::-1]+s)
else:
    print(s[::-1]+t)

Solution (Python):
s = input()
q = int(input())
is_reverse = 0
left = ''
for i in range(q):
    query = input()
    if query[0] == '1':
        is_reverse = (is_reverse+1) % 2
    else:
        _, f, c = query.split()
        if (is_reverse+int(f))%2 == 0:
            s += c
        else:
            left += c
s = left[::-1] + s
if is_reverse:
    print(s[::-1])
else:
    print(s)

Solution (Python):
import collections


class Formatter:
    def __init__(self, s):
        self._d = collections.deque(s)
        self._reversed = False

    def reverse(self):
        self._reversed = not self._reversed

    def append(self, s):
        if self._reversed:
            self._d.appendleft(s)
        else:
            self._d.append(s)

    def appendleft(self, s):
        if self._reversed:
            self._d.append(s)
        else:
            self._d.appendleft(s)

    def __iter__(self):
        if self._reversed:
            return reversed(self._d)
        else:
            return iter(self._d)


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

d = Formatter(S)

for _ in range(Q):
    Qi = input()
    if Qi == "1":
        d.reverse()
    else:
        T, F, C = Qi.split()
        if F == "1":
            d.appendleft(C)
        else:
            d.append(C)

print("".join(d))

Solution (Python):
import collections


class Formatter:
    def __init__(self, s):
        self._d = collections.deque(s)
        self._d_reverse = collections.deque(reversed(s))

    def reverse(self):
        self._d, self._d_reverse = self._d_reverse, self._d

    def append(self, s):
        self._d.append(s)
        self._d_reverse.appendleft(s)

    def appendleft(self, s):
        self._d.appendleft(s)
        self._d_reverse.append(s)

    def __iter__(self):
        return iter(self._d)


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

d = Formatter(S)

for _ in range(Q):
    Qi = input()
    if Qi == "1":
        d.reverse()
    else:
        T, F, C = Qi.split()
        if F == "1":
            d.appendleft(C)
        else:
            d.append(C)

print("".join(d))

Solution (Python):
S = input()
Q = int(input())
hanten = 1
front = []
back = []
for _ in range(Q):
    q = list(map(str, input().split()))
    if q[0]=='1':
        hanten *= -1
    else:
        if hanten==1:
            if q[1]=='1':
                front.append(q[2])
            else:
                back.append(q[2])
        else:
            if q[1]=='1':
                back.append(q[2])
            else:
                front.append(q[2])


if hanten==1:
    print(''.join(list(reversed(front)) + list(S) + back))
else:
    print(''.join(list(reversed(list(S)+back))+front))

Solution (Python):
from collections import deque

S = input()
Q = int(input())
query = [input() for _ in range(Q)]

S = deque(S)

inv = 0
for q in query:
    if q[0] == "1":
        inv += 1
    else:
        _, f, c = q.split()
        if f == "1":
            if inv % 2 == 0:
                S.appendleft(c)
            else:
                S.append(c)
        else:
            if inv % 2 == 0:
                S.append(c)
            else:
                S.appendleft(c)
S = "".join(S)
print(S if inv % 2 == 0 else S[::-1])

Solution (Python):
from collections import deque
S = deque([input()])
Q=int(input())
query=[[i for i in input().split()] for _ in range(Q)]

rf=0
for q in query:
    if q[0] == "1":
        rf ^= 1
    else:
        if q[1] == '1':
            if rf:
                S.append(q[2])
            else:
                S.appendleft(q[2])
        else:
            if rf:
                S.appendleft(q[2])
            else: 
                S.append(q[2])
print(''.join(S)[::-1] if rf else ''.join(S))

Solution (Python):
def op2(f, c, r):
  if r == 0:
    if f == "1":
      left.append(c)
    elif f == "2":
      right.append(c)

  if r == 1:
    if f == "1":
      right.append(c)
    elif f == "2":
      left.append(c)

S = input()
Q = int(input())
v = [list(input().split()) for i in range(Q)]
rev = 0
left = []
right = []

for val in v:
  if val[0] == "1":
    if rev == 0:
      rev = 1
    elif rev == 1:
      rev = 0
  elif val[0] == "2":
    op2(val[1], val[2], rev)

nleft = left[::-1]
ans = "".join(nleft + [S] + right)

if rev == 0:
  print(ans)
else:
  print(ans[::-1])

Solution (Python):
from collections import deque

s = list(input())
q = int(input())
query = [list(input().split()) for i in range(q)]
now = 0
a = deque(s)
for i in query:
  if i[0] == "1":
    now ^= 1
  elif i[0] == "2":
    f,c = int(i[1]),i[2]
    if (f+now) % 2:
      a.appendleft(c)
    else:
      a.append(c)
if now:
  a.reverse()
ans = "".join(a)
print(ans)

Solution (Python):
S = input()
quantity = int(input())
L = [input().split() for i in range(quantity)]
hanten = -1
atama = ""
shiri = ""
for row in range(quantity):
    each = L[row]
    if each[0] == "1":
        hanten = hanten * -1
    else:
        if each[1] == "1":
            if hanten == -1:
                atama = atama + each[2]
            else:
                shiri = shiri + each[2]
        else:
            if hanten == -1:
                shiri = shiri + each[2]
            else:
                atama = atama + each[2]

atama = atama[::-1]
ans = atama + S + shiri
if hanten == 1:
    ans = ans[::-1]

print(ans)

Solution (Python):
s = input()
q = int(input())
t  ={}
for i in range(q):
    t[i]=input().split()
rv_count = 0
x = "X"
for d in t.values():
	if d[0] == "1":
		rv_count += 1
	else:
		if rv_count % 2 ==0:
			if d[1] == "1":
				x2 = d[2]+x
			else:
				x2 = x+d[2]
			x = x2
		else:
			red = d[2][::-1]
			if d[1] == "1":
				x2 = x + red
			else:
				x2 = red+x
			x = x2

if rv_count%2 == 0:
    xf = x.replace("X", s)
else:
	xf = x.replace("X", s)[::-1]
    
print(xf)

Solution (Python):
S = input('')
Q = int(input(''))
head, tail, rev_flag = '', '', 1
for i in range(Q):
    char = input()
    if char == '1':
        rev_flag *= -1
    else:
        t, f, c = char.split()
        if (f == '1' and rev_flag == 1) or (f == '2' and rev_flag == -1):
            head = c + head
        elif (f == '2' and rev_flag == 1) or (f == '1' and rev_flag == -1):
            tail = tail + c
if rev_flag == 1:
    print(head, S, tail, sep='')
else:
    print(tail[::-1], S[::-1], head[::-1], sep='')

Solution (Python):
from collections import deque

S = deque(input())
Q = int(input())
SWITCH = False

for i in range(Q):
    v = list(input().split())
    if v[0] == '1':
        SWITCH = not SWITCH

    else:
        if v[1] == '1':
            if SWITCH==False:
                S.appendleft(v[2])
            else:
                S.append(v[2])
        else:
            if SWITCH==False:
                S.append(v[2])
            else:
                S.appendleft(v[2])

S = ''.join(S)

if SWITCH:
    print(S[::-1])
else:
    print(S)

Solution (Python):
S=input()
Q=int(input())
lst=[]
for _ in range(Q):
    lst.append(input().split())

import collections

str=list(S)
d=collections.deque(str)

right=True
for operation in lst:
    if operation[0]=='1':
       right=not right

    else:
        if right==True and operation[1]=='1' or right==False and operation[1]=='2':
            d.appendleft(operation[2])
        else:
            d.append(operation[2])
str=list(d)
if right==True:
    print(''.join(str))
else:
    print(''.join(str[::-1]))

Solution (Python):
from collections import deque

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

S = 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:
                S.append(C)
            else:
                S.appendleft(C)
        elif F == '2':
            if reverse:
                S.appendleft(C)
            else:
                S.append(C)
            
result = ''.join(S)
if reverse:
    result = result[::-1]
print(result)

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

    query_list = [input().split() for _ in range(Q)]

    T_cnt_list = collections.deque([0])
    left = ''
    right = ''

    for i, query in enumerate(query_list):
        if query[0] == '1':
            T_cnt_list.append(1+T_cnt_list[-1])

        else:
            T_cnt_list.append(T_cnt_list[-1])

            if query[1] == '1' and T_cnt_list[i+1]%2 == 0:
                left = query[2] + left

            elif query[1] == '1' and T_cnt_list[i+1]%2 == 1:
                right = right + query[2]

            elif query[1] == '2' and T_cnt_list[i+1]%2 == 0:
                right = right + query[2]

            elif query[1] == '2' and T_cnt_list[i+1]%2 == 1:
                left = query[2] + left

    if T_cnt_list[-1]%2 == 0:
        print(left + S + right)
    else:
        print(right[::-1] + S[::-1] + left[::-1])

if __name__ == "__main__":
    resolve()

Solution (Python):
string = input()
kai = int(input())
data = [input().split(' ') for i in range(kai)]
key = 0  
former = []
latter = []

for i in range(kai):
    if(data[i][0] == '1'):
        key = (key + 1) % 2
    elif((int(data[i][1]) + key) % 2 == 1):
        former.append(data[i][2])
    else:
        latter.append(data[i][2])

string = ''.join(former[::-1]) + string + ''.join(latter)
if(key == 0):
    print(string)
else:
    print(string[::-1])

Solution (Python):

s = input()
q = int(input())
turn = 0

before = "" 
after = ""
for _ in range(q):
    tmp = input()
    if tmp=="1":
        turn += 1
    else:
        _,f,c = tmp.split()
        f = int(f)
        if (turn + f) % 2 == 1:
            before += c
        else:
            after += c
if turn%2==0:
    out = before[::-1] + s + after
else:
    out = after[::-1] + s[::-1] + before
print(out)

Solution (Python):
from collections import deque

s = deque(input())
n = int(input())
l = [input().split(" ") for _ in range(n)]

x = 0
for i in range(n):
    if l[i][0]=='1':
        if x == 0:
            x = 1
        else:
            x = 0
    elif l[i][0]=='2':
        if x == 0:
            if l[i][1]=='1':
                s.appendleft(l[i][2])
            else:
                s.append(l[i][2])
        else:
            if l[i][1]=='1':
                s.append(l[i][2])
            else:
                s.appendleft(l[i][2])

if x == 0:
    print(''.join(s))
else:
    print(''.join(reversed(s)))

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

front = ""
back = ""

now_swap = 0
q=int(input())
for i in range(q):
    a = input().strip()
    a = list(a)
    
    if a[0] == "1":
        if now_swap == 0:
           now_swap = 1
        else: 
            now_swap = 0
    else:
        if now_swap == 1:
            if a[2] == "1":
               
                back+=a[4]

            else: 
               
                front = a[4] + front

        else: 
            if a[2] == "1":
                front = a[4] + front

            else: 
                
                back+=a[4]
ans= front + s + back

if now_swap == 1:
    
    print(ans[::-1])
else:
    print(ans)

Solution (Python):
from collections import deque

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

flag = 1
for i in range(Q):
    qry = input().split()

    if qry[0] == '1':
        if flag:
            flag = 0
        else:
            flag = 1
    
    elif qry[0] == '2':
        if flag:
            if qry[1] == '1':
                S.appendleft(qry[2])
            else:
                S.append(qry[2])
        else:
            if qry[1] == '1':
                S.append(qry[2])
            else:
                S.appendleft(qry[2])

S = ''.join(S)

if flag:
    print(S)
else:
    print(S[::-1])

Solution (Python):
class Node:
    def __init__(self, char: str):
        self.before = None
        self.char = char
        self.next = None

class Linked_List:
    def __init__(self):
        self.head = None
        self.tail = None
    
    def add_head(self, node):
        if self.head is None:
            self.head = node
            self.tail = node
        else:
            node.next = self.head
            self.head.before = node
            self.head = node

    def add_tail(self, node):
        if self.tail is None:
            self.head = node
            self.tail = node
        else:
            node.before = self.tail
            self.tail.next = node
            self.tail = node

    def show(self, is_reversed: bool):
        ret = []
        node = self.tail if is_reversed else self.head
        while not node is None:
            c = ''.join(list(node.char)[::-1]) if is_reversed else node.char
            ret.append(c)
            node = node.before if is_reversed else node.next
        print(''.join(ret))

S = input()
Q = int(input())
ll = Linked_List()
ll.add_head(Node(S))
is_reversed = False

for _ in range(Q):
    query = input()
    if query[0] == '1':
        is_reversed = False if is_reversed else True
    else:
        t, f, c = query.split()
        if is_reversed:
            c = ''.join(list(c)[::-1])
        if f == '1':
            if is_reversed:
                ll.add_tail(Node(c))
            else:
                ll.add_head(Node(c))
        else:
            if is_reversed:
                ll.add_head(Node(c))
            else:
                ll.add_tail(Node(c))

ll.show(is_reversed)

Solution (Python):
from collections import deque

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

t=True

for i in range(Q):
  query=input().split()
  if query[0]=='1':
    t = not t
  else:
    if (t and query[1]=='1') or (not t and query[1]=='2'):
      S.appendleft(query[2])
    else:
      S.append(query[2])

S=''.join(S)

if not t:
  S=S[::-1]

print(S)

Solution (Python):
from collections import deque

s = deque(input())
Q = int(input())
    
t = True
    
for i in range(Q):
    query = input().split()
    if query[0] == '1':
        t = not t
    else:
        if(t and query[1] == '1') or (not t and query[1] == '2'):
            s.appendleft(query[2])
        else:
            s.append(query[2])
            
s = ''.join(s)
    
if not t:
    s = s[::-1]
        
print(s)

Solution (Python):
from collections import deque

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

t=deque(S)
flag_r=0
for _ in range(Q):
    Query=input().split()
    if int(Query[0])==1:
        flag_r=(flag_r+1)%2
    elif int(Query[1])==1+flag_r:
        t.appendleft(Query[2])
    else:
        t.append(Query[2])
result=''.join(t)
if flag_r:
    result=result[::-1]
print(result)

Solution (Python):
from collections import deque

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

t=deque(S)
flag_r=0
for _ in range(Q):
    Query=input().split()
    if int(Query[0])==1:
        flag_r=(flag_r+1)%2
    elif int(Query[1])==1+flag_r:
        t.appendleft(Query[2])
    else:
        t.append(Query[2])
result=''.join(t)
if flag_r:
    result=result[::-1]
print(result)

Solution (Python):
from collections import deque

S = input()
Q = int(input())
d = deque(list(S))
tf = 1

for i in range(Q):
    Queryi = list(map(str, input().split()))
    if Queryi[0] == '1':
        tf *= -1
    else:   
        if tf == 1:
            if Queryi[1] == '1':
                d.appendleft(Queryi[2])
            else: 
                d.append(Queryi[2])
        else: 
            if Queryi[1] == '1':
                d.append(Queryi[2])
            else: 
                d.appendleft(Queryi[2])

if tf == -1:
    d.reverse()

print(''.join(d))

Solution (Python):
from collections import deque

L = deque([])
S = str(input())
q = int(input())
Q = [[] for _ in range(q)] 

for i in range(len(S)):
    L.append(S[i])

for i in range(q):
    A = input().split()
    Q[i] = A

num = 0

for i in range(q):
    if Q[i][0] == '1':
        num ^= 1
    if Q[i][0] == '2':
        if num == 0:
            if Q[i][1] == '1':
                L.appendleft(Q[i][2])
            else:
                L.append(Q[i][2])
        else:
            if Q[i][1] == '2':
                L.appendleft(Q[i][2])
            else:
                L.append(Q[i][2])

if num:
    L.reverse()

print(''.join(L))

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

X=deque(list(S))
f=1
for _ in range(Q):
    A=list(input().split())
    if A[0]=="1":
        f*=-1
    else:
        if A[1]=="1":
            if f==1:
                X.appendleft(A[2])
            else:
                X.append(A[2])
        else:
            if f==-1:
                X.appendleft(A[2])
            else:
                X.append(A[2])

if f==-1:
    X.reverse()

print("".join(X))

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

X=deque(S)
f=1
for _ in range(Q):
    A=list(input().split())
    if A[0]=="1":
        f*=-1
    if A[0]=="2":
        if A[1]=="1":
            if f==1:
                X.appendleft(A[2])
            if f==-1:
                X.append(A[2])
        if A[1]=="2":
            if f==-1:
                X.appendleft(A[2])
            if f==1:
                X.append(A[2])

if f==-1:
    print("".join(reversed(X)))
else:
    print("".join(X))

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

X=deque(S)
f=1
for _ in range(Q):
    A=list(input().split())
    if A[0]=="1":
        f*=-1
    if A[0]=="2":
        if A[1]=="1":
            if f==1:
                X.appendleft(A[2])
            if f==-1:
                X.append(A[2])
        if A[1]=="2":
            if f==-1:
                X.appendleft(A[2])
            if f==1:
                X.append(A[2])

if f==-1:
    print("".join(list(reversed(X))))
else:
    print("".join(list(X)))

Solution (Python):
s=input()
q=int(input())
cnt=0
a1,a2=[],[]
for _ in range(q):
  t=list(input().split())
  if t[0]=='1':
    cnt+=1
    a1,a2=a2,a1
  else:
    if t[1]=='1':
      a1.append(t[2])
    else:
      a2.append(t[2])
if cnt%2==1:
  s=s[::-1]
print(''.join(a1[::-1])+s+''.join(a2))

Solution (Python):
from collections import deque
S = deque(str(input()))
Q = int(input())
count = 0
for i in range(Q):
    tmp = list(input().split())
    if tmp[0]=="1":
        count += 1
    else:
        if tmp[1]=="1" and count%2==0 or tmp[1]=="2" and count%2==1:
            S.appendleft(tmp[2])
        elif tmp[1]=="2" and count%2==0 or tmp[1]=="1" and count%2==1:
            S.append(tmp[2])
if count%2==0:
    ans = "".join(S)
    print(ans)
else: 
    S.reverse()
    ans = "".join(S)
    print(ans)

Solution (Python):
from collections import deque
S=deque(input())
Q=int(input())
r=1
for i in range(Q):
    T=input().split()
    if T[0]=="1":
        r*=-1
        continue
    t=T[2]
    if T[1]=="1":
        if r==1:
            S.appendleft(t)
        else:
            S.append(t)
        continue
    if r==1:
        S.append(t)
    else:
        S.appendleft(t)
if r==1:
    print("".join(list(S)))
else:
    S.reverse()
    print("".join(list(S)))

Solution (Python):
import copy

S=input()
Q=int(input())
Query=[]
temp=[]

revflag=0
sbef=[]
saft=[]

for i in range(Q):
    temp=input().split()
    if len(temp) ==1:
        revflag += 1
    else:
        if (revflag + int(temp[1])-1 )%2 == 0:
            sbef.append(temp[2])
        else:
            saft.append(temp[2])

ans=""

for i in range(len(sbef)):
    ans+=sbef[len(sbef)-i-1]

ans += S
for i in range(len(saft)):
    ans+=saft[i]

tmp=""
tmp2 = copy.copy(ans)


if revflag%2 ==1:
    for i in range(len(ans)):
        tmp+=tmp2[len(ans)-i-1]
    ans=tmp


print(ans)

Solution (Python):
"""
    Author: _YL_
"""
s = input()
n = int(input())

flip = 0
left = 0
right = 1
strs = ["", ""]

i = 0
while i < n:
    lst = input().split()

    if lst[0] == "1":
        flip += 1
        left = 0 if left == 1 else 1
        right = 0 if right == 1 else 1
    elif lst[1] == "1":
        strs[left] += lst[2]
    else:
        strs[right] += lst[2]
    i += 1

if flip % 2 == 1:
    s = s[::-1]
    
s = strs[left][::-1] + s + strs[right]

print(s)

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

t = ''
for i in range(n):
    q = input().split()
    if len(q) == 1:
        t, s = s, t
    elif len(q) == 3:
        if q[1] == '1':
            t += q[2]
        elif q[1] == '2':
            s += q[2]

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

Solution (Python):
s = list(str(input()))
q = int(input())
w = [list(map(str,input().split())) for i in range(q)]

reverse = False
front_list = []
end_list = []

for data in w:
    if int(data[0]) == 1:
        reverse = not reverse
    else:
        if (int(data[1]) == 1 and not reverse) or (int(data[1]) == 2 and reverse):
            front_list.append(data[2])
        else:
            end_list.append(data[2])
if not reverse:
    front_list.reverse()
    print("".join(front_list + s + end_list))
else:
    end_list.reverse()
    s.reverse()
    print("".join(end_list + s + front_list))

Solution (Python):
from collections import deque

S = deque(input())
Q = int(input())
is_reversed = False

for i in range(Q):
    T, *fc = input().split()
    if T == "1":
        is_reversed = not is_reversed
        continue
    
    F, C = fc
    if F == "1":
        
        if is_reversed:
            S.append(C)
        else:
            S.appendleft(C)
    elif F == "2":
        
        if is_reversed:
            S.appendleft(C)
        else:
            S.append(C)

ans = "".join(S) if not is_reversed else "".join(S)[::-1]
print(ans)

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

Solution (Python):
s = input()
n = int(input())
t = ''
for i in range(n):
    q = input().split()
    if len(q) == 1:
        t, s = s, t
    elif len(q) == 3:
        if q[1] == '1':
            t += q[2]
        elif q[1] == '2':
            s += q[2]

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

Solution (Python):
str = input()
Q = int(input())
left = []
right = []
flag = 1
for _ in range(Q):
    line = input().split()
    op1 = int(line[0])
    if op1 == 1:
        flag = -flag
    else:
        op2 = int(line[1])
        char = line[2]
        if (op2 == 1 and flag == 1) or (op2 == 2 and flag == -1) :
            left.append(char)
        else:
            right.append(char)

if flag == -1:
    right.reverse()
    str = str[::-1]
    str = "".join(right) + str
    str += "".join(left)
else:
    left.reverse()
    str = "".join(left) + str
    str += "".join(right)

print(str)

Solution (Python):
from collections import deque
a = input()
a = deque(a)
n = int(input())
tmp = 0
for i in range(n):
    q = input().split()
    if len(q) == 1:
        tmp += 1
    elif len(q) == 3:
        if q[1] == '1':
            if tmp % 2 == 0:
                a.appendleft(q[2])
            else:
                a.append(q[2])
        elif q[1] == '2':
            if tmp % 2 == 0:
                a.append(q[2])
            else:
                a.appendleft(q[2])

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

Solution (Python):
s = input()
q = int(input())
n = len(s)
flag = 0
mae = 0
ushiro = 0
b1 = ["a" for i in range(q)]
b2 = ["a" for i in range(q)]
for i in range(q):
    a = list(input().split())
    if a[0] == "1":
        flag += 1
    elif (flag+int(a[1])) % 2 == 1:
        b1[mae] = a[2]
        mae += 1
    else:
        b2[ushiro] = a[2]
        ushiro += 1

if flag % 2 == 0:
    for i in range(mae):
        print(b1[mae-1-i], end="")
    print(s, end="")
    for i in range(ushiro):
        print(b2[i], end="")
else:
    for i in range(ushiro):
        print(b2[ushiro-1-i], end="")
    print(s[::-1], end="")
    for i in range(mae):
        print(b1[i], end="")

Solution (Python):
from collections import deque

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

for i in range(len(ss)) :
  s.append(ss[i])

r = 0
for i in range(q) :
  t = list(map(str,input().split()))
  if t[0] == '1' :
    r += 1
  if t[0] == '2' :
    f = t[1] ; c = t[2]
    if r%2 == 0 :
      if f == '1' :
        s.appendleft(c)
      else :
        s.append(c)
    else :
      if f == '2' :
        s.appendleft(c)
      else :
        s.append(c)
        
if r%2 == 1 :
  s.reverse()
  
print(*s,sep='')

Solution (Python):
from collections import deque

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

for i in range(len(ss)) :
  s.append(ss[i])

r = 0
for i in range(q) :
  t = list(map(str,input().split()))
  if t[0] == '1' :
    r += 1
  if t[0] == '2' :
    f = t[1] ; c = t[2]
    if r%2 == 0 :
      if f == '1' :
        s.appendleft(c)
      else :
        s.append(c)
    else :
      if f == '2' :
        s.appendleft(c)
      else :
        s.append(c)
        
if r%2 == 1 :
  s.reverse()
  
print(*s,sep='')

Solution (Python):
from collections import deque

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

query = []
rev=1

for i in range(q):
    query.append(input().split())

for i in range(q):
    if query[i][0]=="1":
        rev*=-1
    else:
        if rev==1:
            if query[i][1]=="1":
                s.appendleft(query[i][2])
            else:
                s.append(query[i][2])
        else:
            if query[i][1]=="2":
                s.appendleft(query[i][2])
            else:
                s.append(query[i][2])

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

Solution (Python):
from collections import deque
s = deque(list(input()))
q = int(input())
sento = 0
for i in range(q):
    line = list(input().split(" "))
    if line[0] == "1":
        sento += 1
    elif line[1] == "1":
        if sento % 2 == 0:
            s.appendleft(line[2])
        else:
            s.append(line[2])
    else:
        if sento % 2 == 0:
            s.append(line[2])
        else:
            s.appendleft(line[2])
if sento % 2 ==1:
    s.reverse()
for i in range(len(s)):
    print(s[i],end ="")
print()

Solution (Python):
S = list(input())
Q = int(input())
Q_l = []
for i in range(Q):
    Q_l.append(input().split())
rev_flg = 0
head_list =[]
tail_list = []
for i in range(Q):
    if int(Q_l[i][0]) == 1:
        rev_flg += 1
    else:
        if rev_flg % 2 == 0 and int(Q_l[i][1]) == 1:
            head_list.append(Q_l[i][2])
        elif rev_flg % 2 == 1 and int(Q_l[i][1]) == 1:
            tail_list.append(Q_l[i][2])
        elif rev_flg % 2 == 0 and int(Q_l[i][1]) == 2:
            tail_list.append(Q_l[i][2])
        else:
            head_list.append(Q_l[i][2])
head_list.reverse()
head_list.extend(S)
head_list.extend(tail_list)
if rev_flg %2 ==1:
    head_list.reverse()
print("".join(head_list))

Solution (Python):

s = input()
q = int(input())
ht = 1
head = ''
tail = ''
count = 0
for i in range(q):
    txt3 = input().split()
    if txt3[0] == '1':
        ht = 3 - ht
        count += 1
    else:
        if int(txt3[1]) == ht:
            head = txt3[2] + head
        else:
            tail = tail + txt3[2]
s = head + s + tail
if count % 2 == 1:
    s = s[::-1]
print(s)

Solution (Python):
from collections import deque

s=deque(input())
n=int(input())
r=False
for i in range(n):
    q=input()
    if q[0]=="1":
        r= not r
    else:
        t,f,c=map(str,q.split())
        if f=="1":
            if r==False:
                s.appendleft(c)
            else:
                s.append(c)
        else:
            if r==True:
                s.appendleft(c)
            else:
                s.append(c)
s=list(s)
if r==True:
    s=s[::-1]
print(*s,sep="")

Solution (Python):
from collections import deque

S = deque(input())

Q = int(input())

c = False
for i in range(Q):
  *T, = map(str,input().split())
  if len(T) == 1:
    c = not c  
  elif (int(T[1]) == 1 and not c) or (int(T[1]) == 2 and c):
    S.appendleft(T[2])
  else:
    S.append(T[2])

print("".join(S) if not c else "".join(list(reversed(S))))