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

hanten = 0
front = ""
end = ""

for i in range(Q):
  query = list(map(str, input().split()))
  if query[0] == "1":
    hanten += 1
    hanten = hanten % 2
  else:
    if query[1] == "1":
      if hanten == 0:
        front = front + query[2]
      else:
        end = end + query[2]
    else:
      if hanten == 0:
        end = end + query[2]
      else:
        front = front + query[2]

front = front[::-1]
ans = front + S + end
if hanten == 1:
  ans = ans[::-1]
print(ans)

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


reverse = 0

for query in sys.stdin:
    if query[0] == '1':  
        reverse = 1 - reverse

    else:
        _, f, c = query.split()
        is_append_left = (reverse + int(f)) % 2
        if is_append_left:
            S.appendleft(c)  
        else:
            S.append(c)

if reverse:  
    S.reverse()

print(''.join(S))

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

hanten = 0
front = ""
end = ""

for i in range(Q):
  query = list(map(str, input().split()))
  if query[0] == "1":
    hanten += 1
    hanten = hanten % 2
  else:
    if query[1] == "1":
      if hanten == 0:
        front = query[2] + front
      else:
        end = end + query[2]
    else:
      if hanten == 0:
        end = end + query[2]
      else:
        front = query[2] + front

ans = front + S + end
if hanten == 1:
  ans = ans[::-1]
print(ans)

Solution (Python):
def rev_f(string , i):
    if i[2] == '2':
        string = string + i[4]

    if i[2] == '1':
        string = i[4] + string
    return string



def rev_s(string , i):
    if i[2] == '1':
        string = string + i[4]

    if i[2] == '2':
        string = i[4] + string
    return string



front = ''
back = ''
string = input()
num = int(input())
q_list = []
reverse_num = 0
reverse = True
reverese_s = False
for a in range(num):
    i = input()
    if i == '1':
        reverse_num  +=1 
        if reverese_s:
            reverese_s = False
        else:
            reverese_s = True
    else:
        if reverese_s == False:
            if i[2] == '2':
                back += i[4]
            else:
                front = i[4] + front
            
        else :
            if i[2] == '2':
                front = i[4] + front
            else:
                back += i[4]
        
if reverse_num %2 == 0:
    reverse = False
    
string = front + string + back
if reverse == True:
    string = string[::-1]
    
print(string)

Solution (Python):
from collections import deque

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

rev_stetus = False
for _ in range(Q):
    ipt = input().split()
    if ipt[0] == "1":
        rev_stetus = not rev_stetus
    else:
        f = int(ipt[1])
        c = ipt[2]
        if rev_stetus:
            if f == 1:
                S.append(c)
            else:
                S.appendleft(c)
        else:
            if f == 1:
                S.appendleft(c)
            else:
                S.append(c)

if rev_stetus:
    S.reverse()
    print("".join(S))
else:
    print("".join(S))

Solution (Python):
from collections import deque

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

for _ in range(q):
    qs = input().split()
    if qs[0] == '1':
        cnt += 1
    else:
        if qs[1] == '1':
            if cnt%2 == 0:
                s.appendleft(qs[2])
            else:
                s.append(qs[2])
        else:
            if cnt%2 == 0:
                s.append(qs[2])
            else:
                s.appendleft(qs[2])
ans = ''.join(s)
print(ans if cnt%2 == 0 else ans[::-1])

Solution (Python):
from collections import deque
S=input()
d=deque(S)
rev=False
Q=int(input())
for i in range(Q):
    Que=input()
    if(Que=='1'):
        rev=True if not rev else False
    else:
        T,F,C=map(str,Que.split())
        if(F=='1'):
            if(not rev):
                d.appendleft(C)
            else:
                d.append(C)
        elif(F=='2'):
            if(not rev):
                d.append(C)
            else:
                d.appendleft(C)
if(not rev):
    for i in range(len(d)):
        print(d[i],end='')
else:
    for i in reversed(range(len(d))):
        print(d[i],end='')

Solution (Python):
from collections import deque
S = deque(list(input()))
Q = int(input())
re = 0
for i in range(Q):
    X = input().split()
    if X[0] == "1":
        re += 1
    else:
        if (int(X[1]) + re)%2 == 1:
            S.appendleft(X[2])
        else:
            S.append(X[2])
while S:
    if re % 2:
        print(S.pop(),end = "")
    else:
        print(S.popleft(),end = "")
print()

Solution (Python):
def main():
	s = str(input())
	q = int(input())
	shiji = 1
	rcount = 0
	from collections import deque
	s = deque(s)
	for i in range(q):
		qu = list(map(str,input().split()))
		if qu[0] != '1':
			if shiji == 1:
				if qu[1] == '1':
					s.appendleft(qu[2])
				else:
					s.append(qu[2])
			else:
				if qu[1] == '2':
					s.appendleft(qu[2])
				else:
					s.append(qu[2])
		else:
			shiji *= -1
			rcount += 1
	if rcount % 2 == 0:
		print(''.join(map(str,s)))
	else:
		s.reverse()
		print(''.join(map(str,s)))

if __name__ == '__main__':
	main()

Solution (Python):

from collections import deque
S = deque(input())
Q = int(input())
cnt = 0
for i in range(Q):
    q = input().split()
    if q[0] == "1":
        cnt += 1
    else:
        if (int(q[1]) + cnt) % 2 == 1:
            S.appendleft(q[2])
        else:
            S.append(q[2])
ans = "".join(S)
print(ans if cnt % 2 == 0 else ans[::-1])

Solution (Python):
flag = True
sen = ""
ushi = ""
S = input()
Q = int(input())
for i in range(Q):
  T = input().split()
  if T[0] == "1" :
    flag = not flag
  else:
    if flag:
      if T[1] == "1":
        sen = T[2] + sen
      else:
        ushi = ushi + T[2]
    else:
      if T[1] == "1":
        ushi = ushi + T[2]
      else:
        sen = T[2] + sen
ans = sen + S + ushi
if flag == False:
  print(ans[::-1])
else:
  print(ans)

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

for i in s:
    d.append(i)

q = int(input())

reverse = 0
for i in range(q):
    query = input().split()
    if query[0] == "1":
        t = int(query[0])
    else:
        t = int(query[0])
        f = int(query[1])
        c = query[2]

    if t == 1:
        reverse = 1 - reverse
    else:
        if reverse == 1:
            f = f - 1

        if f == 1:
            d.appendleft(c)
        else:
            d.append(c)
if reverse == 1:
    d.reverse()
print("".join(d))

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().split())
f=False
for i in range(int(input())):
  _=input().split()
  if _[0]=='1':
    f=not f
  else:
    if f:
      [s.append(_[2]) if _[1]=='1'
       else s.appendleft(_[2])]
    else:
      [s.appendleft(_[2]) if _[1]=='1'
       else s.append(_[2])]
print("".join(s)[::-1] if f else "".join(s))

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

Solution (Python):

from collections import defaultdict,deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys, random, itertools, math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt
def LI(): return list(map(int, input().split()))
def LF(): return list(map(float, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def II(): return int(input())
def IF(): return float(input())
def LS(): return input().split()
def S(): return list(input().rstrip())
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = 1e10


def solve():
    s = S()
    t = 0
    l = []
    r = []
    for _ in range(II()):
        q = LS()
        if q[0] == "1":
            t ^= 1
        else:
            _,f, c = q
            if f == "1":
                if t:
                    r.append(c)
                else:
                    l.append(c)
            else:
                if t:
                    l.append(c)
                else:
                    r.append(c)
    if t:
        print("".join(r[::-1] + s[::-1] + l))
    else:
        print("".join(l[::-1] + s + r))
    return



if __name__ == '__main__':
    solve()

Solution (Python):
from collections import deque
s = input()
q = int(input())
flag = True
d = deque(s)
for i in range(q):
    inp = input()
    if inp == '1':
        flag = not flag

    else:
        _, f, c = map(str, inp.split())
        if f == '1':
            if flag:
                d.appendleft(c)
            else:
                d.append(c)
        else:
            if flag:
                d.append(c)
            else:
                d.appendleft(c)

if flag:
    while(d):
        t = d.popleft()
        print(t, end='')
else:
    while(d):
        t = d.pop()
        print(t, end='')

Solution (Python):
s=input()
q=int(input())
from collections import deque
qq=deque(s)
cnt=0
for i in range(q):
    j=input()
    if j=="1":
        cnt+=1
        continue
    _,k,l=j.split()
    if (k=="2")^cnt%2:
        qq.append(l)
    else:
        qq.appendleft(l)

if cnt%2==1:
    print("".join(reversed(list(qq))))
else:
    print("".join(qq))

Solution (Python):
s = input()
q = int(input())
query = []
lead = ''
end = ''
inversion_flg = False
 
for j in range(q):
    i = input().split()
    if i[0] == "1":
        if inversion_flg:
            inversion_flg = False 
        else:
            inversion_flg = True
    else:
        if inversion_flg:
            if i[1] == "1":
                i[1] = "2"
            else:
                i[1] = "1"
        if i[1] == "1":
            lead+=i[2]
        else:
            end+=i[2]
 
lead = lead[::-1]
s = lead+s+end
if inversion_flg:
    s = s[::-1]
print(s)

Solution (Python):
S = input()
Q = int(input())
count = 0
from collections import deque
q1 = deque()
q2 = deque()
for i in range(Q):
    li = input().split()
    if len(li) == 1:
        count +=1
        q11 = q1
        q22 = q2
        q1 = q22
        q2 = q11
    if len(li) == 3:
        if li[1] == '1':
            if count % 2 == 1:
                q1.append(li[2])
            if count % 2 == 0:
                q1.appendleft(li[2])
        if li[1] == '2':
            if count % 2 == 1:
                q2.appendleft(li[2])
            if count % 2 == 0:
                q2.append(li[2])
s1 = ''.join(list(q1))
s2 = ''.join(list(q2))
if count % 2 == 1:
    K = reversed(list(S))
    S = ''.join(K)
    s1 = ''.join(reversed(list(s1)))
    s2 = ''.join(reversed(list(s2)))
print(s1+S+s2)

Solution (Python):
from collections import deque
string = input()
s = deque(string)
iteration = int(input())
reverse = 1

for c in range(0,iteration):
    sousa = input().split()
    
    if sousa[0] == "1":
        reverse = reverse*(-1)
    
    elif sousa[1] == "1":
        if reverse == 1:
            s.appendleft(sousa[2])
        else:
            s.append(sousa[2])
    else:
        if reverse == 1:
            s.append(sousa[2])
        else:
            s.appendleft(sousa[2])

result = "".join(s)
if reverse == -1:
    result = result[::-1]


print(result)

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


def main():
    a = input()
    d = deque(a)
    n = int(input())
    flagr = True
    for i in range(n):
        j = list(readline().split())
        if len(j) == 1:
            flagr = not flagr
        elif int(j[1] == "1") ^ flagr:
            d.append(j[2])
        else:
            d.appendleft(j[2])
    if not flagr:
        d.reverse()
    print("".join(d))


if __name__ == '__main__':
    main()

Solution (Python):
s = input()
q = int(input())
zengo = True
mae = []
usiro = []
for i in range(q):
  qi = input()
  if qi[0] == "1":
    if zengo:
      zengo = False
    else:
      zengo = True
  else:
    t, f, c = qi.split() 
    if zengo:
      if f == "1":
        mae.append(c)
      else:
        usiro.append(c)
    else:
      if f == "1":
        usiro.append(c)
      else:
        mae.append(c)
mae = mae[::-1]
s = ''.join(mae) + s + ''.join(usiro)
if zengo:
  print(s)
else:
  print(s[::-1])

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

added = ''
inv = 0
for i in range(Q):
  query = input().split()
  if query == ['1']:
    inv = (inv + 1)%2
  else:
    f, c = query[1], query[2]
    if (f=='1' and inv) or (f=='2' and not inv):
      S += c
    else:
      added += c

if inv:
  print(S[::-1] + added)
else:
  print(added[::-1] + S)

Solution (Python):
s = input()
q = int(input())
query = [list(input().split()) for _ in range(q)]
right = []
left = []
rev_count = 0
for i in range(q):
  if query[i][0] == "1":
    rev_count += 1
    right,left = left,right
  else:
    if query[i][1] == "1":
      left.append(query[i][2])
    else:
      right.append(query[i][2])
if rev_count % 2 == 1:
  s = s[::-1]
print("".join(reversed(left))+s+"".join(right))

Solution (Python):
from collections import deque
s = deque(list(input()))
q = int(input())
t1 = 1
for i in range(q):
    t = input()
    if t == '1':
        t1 *= -1
    else:
        if t1 == 1:
            if t[2] == '1':
                s.appendleft(t[4])
            else:
                s.append(t[4])
        else:
            if t[2] == '2':
                s.appendleft(t[4])
            else:
                 s.append(t[4])

ans = ''
if t1 == -1:
    ans = ''.join(reversed(s))
else:
    ans = ''.join(s)

print(ans)

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


flip = 0
head = ""
for _ in range(Q):
    q = input()
    if q == '1':
        flip += 1
    else:
        _, f, c = q.split(' ')
        if f == '1' and (flip % 2) == 0:
            
            head = head + c
        elif f == '1' and (flip % 2) != 0:
            S = S + c
        elif f != '1' and (flip % 2) == 0:
            S = S + c
        else:
            
            head = head + c

if (flip % 2) == 0:
    head = head[::-1]
    S = head + S
else:
    S = S[::-1]
    S = S + head

print(S)

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

head = ""
tail = ""
main = ""

main = S

status = 0

while Q:
  Q -= 1
  l = list(map(str,input().split()))
  if l[0] == "1":
    status = 1-status
  else:
    letter = l[2]
    if l[1] == "1":
      if status == 0:
        head += letter
      else:
        tail += letter
    else:
      if status == 0:
        tail += letter
      else:
        head += letter

if status == 0:
  print(head[::-1]+main+tail)
else:
  print(tail[::-1]+main[::-1]+head)

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

head = ""
tail = ""
main = S

status = 0

while Q:
  Q -= 1
  l = list(map(str,input().split()))
  if l[0] == "1":
    status = (status+1)%2
  else:
    letter = l[2]
    if l[1] == "1":
      if status == 0:
        head += letter
      else:
        tail += letter
    else:
      if status == 0:
        tail += letter
      else:
        head += letter

if status == 0:
  print(head[::-1]+main+tail)
else:
  print(tail[::-1]+main[::-1]+head)

Solution (Python):

def main():
    s = input()
    addfront = ''
    addback = ''
    q = int(input())
    direction = 0
    for i in range(q):
        t = input()
        if t[0] == '1':
            direction += 1
            continue
        if t[0] == '2':
            if direction % 2 == 0:
                if t[2] == '1':
                    addfront = t[4] + addfront 
                if t[2] == '2':
                    addback = addback + t[4]
            else:
                if t[2] == '1':
                    addback = addback + t[4]
                if t[2] == '2':
                    addfront = t[4] + addfront
    ans = addfront + s + addback
    if direction%2 == 0:
        print(ans)
    else:
        ans = ''.join(reversed(ans))
        print(ans)
main()

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


class Node:
    def __init__(self, value=None, prev=None, next=None):
        self.value = value
        self.prev = prev
        self.next = next

    def __str__(self):
        return str(self.value)


class LinkedList:
    def __init__(self, initial_string=None):
        self.first = Node()
        self.last = Node()
        self.first.next = self.last
        self.last.prev = self.first
        self.should_reverse = False
        for s in initial_string:
            self.insert_last(s)

    def _insert_first(self, value=None):
        prev_next = self.first.next
        self.first.next = Node(value, self.first, prev_next)
        prev_next.prev = self.first.next

    def _insert_last(self, value=None):
        prev_prev = self.last.prev
        self.last.prev = Node(value, prev_prev, self.last)
        prev_prev.next = self.last.prev

    def insert_first(self, value=None):
        if self.should_reverse:
            self._insert_last(value)
        else:
            self._insert_first(value)

    def insert_last(self, value=None):
        if self.should_reverse:
            self._insert_first(value)
        else:
            self._insert_last(value)

    def set_reverse(self):
        self.should_reverse = not self.should_reverse

    def _print(self):
        node = self.first.next
        while node is not None:
            if node.value is not None:
                print(node, end="")
            node = node.next
        print("")

    def _print_reverse(self):
        node = self.last.prev
        while node is not None:
            if node.value is not None:
                print(node, end="")
            node = node.prev
        print("")

    def print_list(self):
        if self.should_reverse:
            self._print_reverse()
        else:
            self._print()


def formation():
    global S, Q

    linked_list = LinkedList(S)

    for _ in range(Q):
        t_str = input()

        if t_str[0] == '1':
            linked_list.set_reverse()
        else:
            split = t_str.split(' ')
            f = split[1]
            c = split[2]
            if f == '1':
                linked_list.insert_first(c)
            else:
                linked_list.insert_last(c)

    linked_list.print_list()


formation()

Solution (Python):
from collections import deque
s=deque(input())
n=int(input())
r=0
for i in range(n):
    strlist=input()
    if strlist[0]=='1':
        r+=1
    elif strlist[0]=='2' and strlist[2] == '1':
        if r%2==0:
            s.appendleft(strlist[4])
        else:
            s.append(strlist[4])
    elif strlist[0]=='2' and strlist[2] =='2':
        if r%2==0:
            s.append(strlist[4])
        else:
            s.appendleft(strlist[4])
if r%2==0:
    for s_ in s:
        print(s_, end='')
else:
    s.reverse()
    for s_ in s:
        print(s_, end='')

Solution (Python):
import collections

S = collections.deque(list(input()))
Q = int(input())
rev = 0
for i in range(0, Q):
    query = list(input().split())
    if query[0] == "1":
        rev = rev+1
    else:
        if query[1] == "1":
            if rev%2 == 0:
                S.appendleft(query[2])
            else:
                S.append(query[2])
        else:
            if rev%2 == 0:
                S.append(query[2])
            else:
                S.appendleft(query[2])
if rev%2 == 0:
    for i in S:
        print (i, end = "")
else:
    for i in reversed(S):
        print (i, end = "")

Solution (Python):
from collections import deque

s = deque(input(""))
Q = int(input())
c = 0

for i in range(Q):
    t = input("")
    if len(t) == 1:
        c += 1
    elif t[2] == "1":
        if c % 2 == 0:
            s.appendleft(t[4])
        else:
            s.append((t[4]))
    else:
        if c % 2 == 0:
            s.append(t[4])
        else:
            s.appendleft(t[4])

if c % 2 == 0:
    for i in s:
        print(i,end="")
else:
    for i in range(len(s)-1,-1,-1):
        print(s[i],end="")

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

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

Solution (Python):
from collections import deque
S = input()
q = deque()
for s in S:
    q.append(s)

Q = int(input())
flag = True
cnt = 0
for _ in range(Q):
    query = list(map(str, input().split()))
    if len(query) == 1:
        
        cnt += 1
        if flag:
            flag = False
        else:
            flag = True
    else:
        f, c = query[1], query[2]
        if f == "1":  
            if flag:  
                q.appendleft(c)
            else:  
                q.append(c)
        elif f == "2":  
            if flag:  
                q.append(c)
            else:
                q.appendleft(c)
ans = "".join(list(q))
if cnt % 2 == 1:
    ans = ans[::-1]
print(ans)

Solution (Python):
from collections import deque

def string_formation(s, q_list):
    ans = deque()
    for x in s:
        ans.append(x)
    count = 0
    for query in q_list:
        if query[0] == '1':
            count += 1
        else:
            if count % 2 == 0:
                if query[1] == '1':
                    ans.appendleft(query[2])
                else:
                    ans.append(query[2])
            else:
                if query[1] == '1':
                    ans.append(query[2])
                else:
                    ans.appendleft(query[2])
    if count % 2 == 1:
        ans.reverse()
    print(''.join(ans))

s = input()
q = int(input())
q_list = []
for _ in range(q):
    tmp = input().split()
    q_list.append(tmp)

string_formation(s, q_list)

Solution (Python):

s = input()
q = int(input())
f = b = ''
odd = 0
for i in range(q):
    t,*fc = input().split()
    if t=='1': odd ^= 1 
    else: 
        if odd ^ (int(fc[0])-1): b += fc[1]
        else: f += fc[1]
s = b[::-1] + s[::-1] + f if odd else f[::-1] + s + b
print(s)

Solution (Python):
from collections import deque

N = deque(input())
Q = int(input())
Qs = []
for _ in range(Q):
    Qs.append(list(map(str, input().split())) ) 
k=0
for q in Qs:
    if q[0] == '1':
        k+=1
    else:
        if k%2 ==0:
            if q[1]=='1':
                N.appendleft(q[2])
            else:
                N.append(q[2])
        else:
            if q[1]=='2':
                N.appendleft(q[2])
            else:
                N.append(q[2])
if k%2==1:
    ans=''
    for x in reversed(N):
        ans += x
else:
    ans=''
    for x in N:
        ans += x

print(ans)

Solution (Python):
from collections import deque

s = deque(input())
q = int(input())
 
r = 0
for i in range(q):
  tfc = input().split()
  if tfc[0] == '1':
    r += 1
  else:
    if tfc[1] == '1':
      if r % 2 == 0:
        s.appendleft(tfc[2])
        
      else:
        s.append(tfc[2])
    if tfc[1] == '2':
     if r % 2 == 1:
        s.appendleft(tfc[2])
     else:
        s.append(tfc[2])          
if r % 2 == 0:
  for i in s:
      print(i, end='')
else:
  for i in (reversed(s)):
    print(i, end='')

Solution (Python):

from collections import deque
s = deque(input())
q = int(input())
odd = 0
for i in range(q):
    t,*fc = input().split()
    if t=='1': odd ^= 1 
    else: 
        if odd ^ (int(fc[0])-1): s.append(fc[1])
        else: s.appendleft(fc[1])
if odd: deque.reverse(s)
print(''.join(s))

Solution (Python):
s = list(input())
q = int(input()) 
inv = 1
query = [input() for _ in range(q)]
bef = []
aft = []


for t in query:
  if t == '1':
    inv *= -1 
  else:
    _, f, c = map(str, t.split())
    if (f =='1' and inv == 1) or (f =='2' and inv == -1):
      
      bef.append(c)
    else:
      
      aft.append(c)

bef.reverse()
bef.extend(s)
bef.extend(aft)

print(''.join(bef[::inv]))

Solution (Python):
from collections import deque
S=input()
N=int(input())
Q=[input() for i in range(N)]
T=deque(S)
cnt=0
for i in range(N):
    if Q[i][0]=="1":
        cnt+=1
    if cnt%2==0:
        if Q[i][0]=="2":
            if Q[i][2]=="1":
                T.appendleft(Q[i][4])
            if Q[i][2]=="2":
                T.append(Q[i][4])
    if cnt%2==1:
        if Q[i][0]=="2":
            if Q[i][2]=="1":
                T.append(Q[i][4])
            if Q[i][2]=="2":
                T.appendleft(Q[i][4])
if cnt%2==1:
    T.reverse()
print(''.join(T))

Solution (Python):
from collections import deque

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

R=0
S=deque(S)

for i in range(Q):
    temp=list(input().split())
    if int(temp[0])==1:
        R+=1
    elif int(temp[0])==2:
        F=int(temp[1])
        c=temp[2]
        if F==1:
            if R%2==0:
                S.appendleft(c)
            else:
                S.append(c)
        elif F==2:
            if R%2==0:
                S.append(c)
            else:
                S.appendleft(c)

if R%2==1:
    S=list(S)
    ans=S[-1::-1]
    ans=map(str,ans)
else:
    ans=map(str,S)

print("".join(ans))

Solution (Python):
s = input()
q = int(input())
revFlag = False
head, tail = [], []

for i in range(q):
  query = list(input().split())
  if int(query[0]) == 1:
    revFlag = not revFlag
  else:
    if int(query[1]) == 1:
      if not revFlag:
        head.append(query[2])
      else:
        tail.append(query[2])
    else:
      if not revFlag:
        tail.append(query[2])
      else:
        head.append(query[2])

if not revFlag:
  h = "".join(reversed(head))
  t = "".join(tail)
  print("".join([h, s, t]))
else:
  h = "".join(head)
  t = "".join(reversed(tail))
  print("".join([t, s[::-1], h]))

Solution (Python):

from collections import deque
s = deque(input())
q = int(input())
odd = 0
for i in range(q):
    t,*fc = input().split()
    if t=='1': odd ^= 1 
    else: 
        if odd ^ (int(fc[0])-1): s.append(fc[1])
        else: s.appendleft(fc[1])
if odd: deque.reverse(s)
print(*s, sep='')

Solution (Python):
from collections import deque

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

d = deque([S[i] for i in range(len(S))])


flg = 0

for i in range(Q):
    query = input()
    if query[0] == '1':
        flg = flg%2 + 1
    else:
        t, f, c = query.split()
        if f == '1':
            if flg % 2 == 0:
                d.appendleft(c)
            else:
                d.append(c)
        else:
            if flg % 2 == 0:
                d.append(c)
            else:
                d.appendleft(c)
if flg % 2 == 0:
    print(''.join(list(d)))
else:
    tmp = ''.join(list(d))
    print(tmp[::-1])

Solution (Python):
S = input()
Q = int(input())
flag = True  
cnt = 0  
before = []
after = []
for _ in range(Q):
    query = list(map(str, input().split()))
    if len(query) == 1:
        
        cnt += 1
        if flag:
            flag = False
        else:
            flag = True
    else:
        f, c = query[1], query[2]
        if f == "1":  
            if flag:  
                before.append(c)
            else:  
                after.append(c)
        elif f == "2":  
            if flag:  
                after.append(c)
            else:
                before.append(c)
before.reverse()
ans = "".join(before) + S + "".join(after)
if cnt % 2 == 1:
    ans = ans[::-1]
print(ans)

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

s = input().strip()
q = int(input())
front = []
back = []
flip = False

for i in range(q):
    query = input()
    if query[0] == '1':
        flip = (not flip)
    else:
        _, f, c = query.split()
        if flip:
            if f == '1':
                back.append(c)
            else:
                front.append(c)
        else:
            if f == '1':
                front.append(c)
            else:
                back.append(c)

if flip:
    print(''.join(back[::-1]) + s[::-1] + ''.join(front))
else:
    print(''.join(front[::-1]) + s + ''.join(back))