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

def main():
    S = input()
    Q = int(input())
    q = deque(S)
    cnt = 0
    for _ in range(Q):
        query = list(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 == 0:
        print(*q, sep ="")
    else:
        q.reverse()
        print(*q, sep ="")







if __name__ == "__main__":
    main()

Solution (Python):
from collections import deque

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

Sq = deque(S)


flg = True
for i in range(Q):
    que = input()
    if que == "1":
        if flg:
            flg = False
        else:
            flg = True
    else:
        j, F, C = que.split()
        if int(F) == 1:
            if flg:
                Sq.appendleft(C)
            else:
                Sq.append(C)
        else:
            if flg:
                Sq.append(C)
            else:
                Sq.appendleft(C)

if flg:
    for i in Sq:
        print(i, end="")
else:
    Sq.reverse()
    for i in Sq:
        print(i, end="")

Solution (Python):
from collections import deque

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

Sq = deque(S)


flg = True
for i in range(Q):
    que = input()
    if que == "1":
        if flg:
            flg = False
        else:
            flg = True
    else:
        j, F, C = que.split()
        if int(F) == 1:
            if flg:
                Sq.appendleft(C)
            else:
                Sq.append(C)
        else:
            if flg:
                Sq.append(C)
            else:
                Sq.appendleft(C)

if flg:
    for i in Sq:
        print(i, end="")
else:
    Sq.reverse()
    for i in Sq:
        print(i, end="")

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

head = ''
back = ''
flg = 1
for _ in range(q):
    i = list(map(str, input().split()))
    if i[0] == '1':
        
        flg *= -1
    else:
        if i[1] == '1':
            if flg == 1:
                head = head + i[2]
            else:
                back = back + i[2]
        else:
            if flg == 1:
                back = back + i[2]
            else:
                head = head+ i[2]

if flg == 1:
    print(head[::-1]+s+back)
else:
    print(back[::-1]+s[::-1]+head)

Solution (Python):
from collections import deque

deq=deque()
k = input()
for i in range(len(k)):
	deq.append(k[i])
q=int(input())
x=True
for i in range(q):
	a=input()
	if a[0]=="1":
		x = not x
	elif a[2]=="1":
		if x==True:
			deq.appendleft(a[4])
		else:
			deq.append(a[4])
	else:
		if x==True:
			deq.append(a[4])
		else:
			deq.appendleft(a[4])
if x==True:
	c="".join(deq)
else:
	deq.reverse()
	c="".join(deq)
print(c)

Solution (Python):
import sys
input = sys.stdin.readline
rev=0
ST=""
EN=""
S=input()
Q=int(input())
for i in [0]*Q:
 A=list(input().split())
 if int(A[0])==1 :
  rev+=1
 else:
  if (int(A[1])+rev)%2==1:
   ST+=A[2]
  else:
   EN+=A[2]
if rev%2==0:
 print(ST[::-1].strip()+S.strip()+EN.strip())
else:
 print(EN[::-1].strip()+S[::-1].strip()+ST.strip())

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

rev = False
head = []
tail = []

for i in range(q):
    query = input()
    if query == "1":
        rev = not rev
    else:
        _, f, c = query.split()
        if (not rev and f == "1") or (rev and f == "2"):
            head.append(c)
        else:
            tail.append(c)

if not rev:
    res = head[::-1] + s + tail
else:
    res = tail[::-1] + s[::-1] + head

print(*res, sep="")

Solution (Python):
from collections import deque
s = deque(input())
q = int(input())
query = [list(map(str, input().split())) for _ in range(q)]

order = True
for i in query:
    if i[0] == "1":
        order = not order
    elif i[1] == "1":
        if order:
            s.appendleft(i[2])
        else:
            s.append(i[2])
    else:
        if order:
            s.append(i[2])
        else:
            s.appendleft(i[2])

if not order:
    s.reverse()
print("".join(s))

Solution (Python):
S=list(input())
Q=int(input())
Rev=True
front=[]
back=[]
for i in range(Q):
    a=input()
    if a=='1':
        Rev=not(Rev)
    else:
        b,c,x=a.split()
        if c == '1':
            if Rev:
                front.append(x)
            else:
                back.append(x)
        else:
            if Rev:
                back.append(x)
            else:
                front.append(x)
if Rev:
    front.reverse()
    front.extend(S)
    front.extend(back)
    print("".join(front))
else:
    back.reverse()
    S.reverse()
    back.extend(S)
    back.extend(front)
    print("".join(back))

Solution (Python):
S=list(input())
Q=int(input())
Rev=True
front=[]
back=[]
for i in range(Q):
    a=input()
    if a=='1':
        Rev=not(Rev)
    else:
        b,c,x=a.split()
        if c == '1':
            if Rev:
                front.append(x)
            else:
                back.append(x)
        else:
            if Rev:
                back.append(x)
            else:
                front.append(x)
if Rev:
    front.reverse()
    front.extend(S)
    front.extend(back)
    print("".join(front))
else:
    back.reverse()
    S.reverse()
    back.extend(S)
    back.extend(front)
    print("".join(back))

Solution (Python):
s = input()
q = int(input())
query_list = [list(input().split()) for _ in range(q)]
reverse_flag = False
concat = ''
initial = 0
for query in query_list:
    if query[0] == '1':
        reverse_flag = not reverse_flag
    elif query[0] == '2':
        if (query[1] == '1' and not reverse_flag) or (query[1] == '2' and reverse_flag):
            concat = query[2] + concat
            initial += 1
        elif (query[1] == '2' and not reverse_flag) or (query[1] == '1' and reverse_flag):
            concat = concat + query[2]
ans = ''
if reverse_flag:
    s = s[::-1]
    concat = concat[::-1]
    ans = concat[:-initial]+s+concat[-initial:]
else:
    ans = concat[:initial]+s+concat[initial:]
print(ans)

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

left = ''
right = S
for q in range(Q):
    Query = list(map(str, input().split()))
    if Query[0] == '1':
        
        tmp = left
        left = right
        right = tmp
    else:   
        if Query[1] == '1':
            
            left += Query[2]
        else:   
            
            right += Query[2]

ans = left[::-1] + right
print(ans)

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

dir = 1
for q in range(Q):
    Query = list(map(str, input().split()))
    if Query[0] == '1':
        
        dir *= -1
    else:   
        if Query[1] == '1':
            
            if dir > 0:
                
                S.appendleft(Query[2])
            else:   
                
                S.append(Query[2])
        else:   
            
            if dir > 0:
                
                S.append(Query[2])
            else:   
                
                S.appendleft(Query[2])

if dir < 0:
    S.reverse()
else:   
    pass

print(''.join(S))

Solution (Python):
S=input()
Q=int(input())
Rev=True
front=''
back=''
for i in range(Q):
    a=input()
    if a=='1':
        Rev=not(Rev)
    else:
        b,c,x=a.split()
        if c == '1':
            if Rev:
                front += x
            else:
                back += x
        else:
            if Rev:
                back += x
            else:
                front += x
if Rev:
    print(front[::-1] + S + back)
else:
    print(back[::-1]+S[::-1]+front)

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

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

f=False
q= deque(s)

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

    if i[0] == '1': f=False if f else True
    else:
        _,a,b =i.split()
        
        if f :
            if a == '1':q.append(b)
            else:q.appendleft(b)
        else:
            if a == '1':q.appendleft(b)
            else:q.append(b)

if f: q.reverse()

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

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

f=False
q= deque(s)

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

    if i[0] == '1': f=False if f else True
    else:
        _,a,b =i.split()
        
        if f :
            if a == '1':q.append(b)
            else:q.appendleft(b)
        else:
            if a == '1':q.appendleft(b)
            else:q.append(b)

if f: q.reverse()

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

Solution (Python):
S = input()
Q = int(input())
que = [0]*Q
count = 0
flag = True
flist = []
llist = []

for i in range(Q):
    que[i] = list(map(str, input().split()))
for i in que:
    if len(i) == 1:
        count += 1
        flag = not flag
    else:
        if i[1] == '1' and flag:
            flist.append(i[2])
        elif i[1] == '2' and not flag:
            flist.append(i[2])
        elif i[1] == '1' and not flag:
            llist.append(i[2])
        else:
            llist.append(i[2])

F = ''.join(flist)
L = ''.join(llist)

if count % 2 == 1:
    S = S[::-1]
    L = L[::-1]
    print(L + S + F)
else:
    F = F[::-1]
    print(F + S + L)

Solution (Python):
s = input()
n = int(input())
head = ''
tail = s
for _ in range(n):
    q = input()
    if q[0] == '1':
        head, tail = tail, head
    else:
        _, f, c = q.split()
        if f == '1':
            head += c
        else:
            tail += c
print(head[::-1] + tail)

Solution (Python):
def string():
    s = input()
    q = int(input())
    l = ''
    r = ''
    re = False
    for _ in range(q):
        t = input().split()
        if t[0] == '1':
            re = not re
        else:
            if re:
                if t[1] == '1':
                    r = r + t[2]
                else:
                    l = t[2] + l
            else:
                if t[1] == '1':
                    l = t[2] + l
                else:
                    r = r + t[2]
    s = l + s + r
    if re:
        s = s[::-1]
    print(s)
string()

Solution (Python):
S = input()
Q = int(input())
flip = False
head = ''
tail = ''
for _ in range(Q):
    q = input()
    if q == '1':
        flip = not flip
        head, tail = tail, head
    else:
        _, f, c = q.split()
        if f == '1':
            head += c
        else:
            tail += c
if flip:
    S = S[::-1]
print(head[::-1] + S + tail)

Solution (Python):
def main():
    s  = input()
    q  = int(input())
    inv = 1
    sfb = [0,"",""]
    for i in range(q):
        ls = list(input().split())
        if ls[0] == "1":
            inv *=-1
        else:
            f,c = ls[1:]
            if f == "1":
                sfb[inv] +=c
            else:
                sfb[-inv] +=c

    ans = sfb[1][::-1] + s + sfb[-1]
    print(ans[::inv])
if __name__ == "__main__":
    main()

Solution (Python):
s  = input()
q  = int(input())
inv = 1
sfb = [0,"",""]
for i in range(q):
    ls = list(input().split())
    if ls[0] == "1":
        inv *=-1
    else:
        f,c = ls[1:]
        if f == "1":
            sfb[inv] +=c
        else:
            sfb[-inv] +=c

ans = sfb[1][::-1] + s + sfb[-1]
print(ans[::inv])

Solution (Python):
def main():
    S = input()
    Q = int(input())
    L = ""
    R = ""
    is_turned = False

    for _ in range(Q):
        Query = input().split()
        if Query[0] == "1":
            is_turned = not is_turned
        else:
            if is_turned ^ (Query[1] == "1"):
                
                L = Query[2] + L
            else:
                
                R = R + Query[2]

    if (is_turned):
        print("".join(L + S + R)[::-1])
    else:
        print("".join(L + S + R))


if __name__ == "__main__":
    main()

Solution (Python):








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

Change = " "
counter = 0

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

    if proc[0] == "1":
        counter += 1
    else:
        
        if (counter%2 == 0 and proc[1] == "1") or (counter%2 == 1 and proc[1] == "2"):
            Change = proc[2] + Change
        else:
            Change = Change + proc[2]

if counter%2 == 0:
    print(Change.replace(" ",S))
else:
    print(Change.replace(" ",S)[::-1])

Solution (Python):
S = input()
Q = int(input())
L = ''
R = ''
rev = False
for _ in range(Q):
    Query = input().split()
    if Query[0] == '1':
        rev = not rev
    else:
        if rev:
            if Query[1] == '1':
                R = R + Query[2]
            else:
                L = Query[2] + L
        else:
            if Query[1] == '1':
                L = Query[2] + L
            else:
                R = R + Query[2]
S = L + S + R
if rev:
    S = S[::-1]
print(S)

Solution (Python):
import sys
sys.setrecursionlimit(10**6)
readline = sys.stdin.readline
read = sys.stdin.read




s = input()
u = ""
t = ""
q = int(input())
num = 0

for _ in range(q):
    a = readline().split()
    if len(a)==1:
        u,t = t,u
        num ^= 1
    else:
        f = int(a[1])
        c = a[2]
        if f==1:
            t += c
        else:
            u += c

if num:
    print(t[::-1]+s[::-1]+u)
else:
    print(t[::-1]+s+u)

Solution (Python):
from collections import deque

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

reverse = False
for i in range(q):
    query = input()
    if int(query[0]) == 1:
        reverse = not reverse
    else:
        _, f, c = query.split()
        if int(f) == 1:
            if reverse:
                s.append(c)
            else:
                s.appendleft(c)
        else:
            if reverse:
                s.appendleft(c)
            else:
                s.append(c)
if reverse:
    s.reverse()

print(''.join(s))

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

reverse  = False
forward  = ""
backward = ""

for _ in range(n):
  query = input().split()
  if query == ["1"]:
    reverse = not reverse
  else:
    if query[1] == "1":
      if reverse:
        backward += query[2][::-1]
      else:
        forward = query[2] + forward
    else:
      if reverse:
        forward = query[2][::-1] + forward
      else:
        backward += query[2]

if reverse:
  print(backward[::-1] + s[::-1] + forward[::-1])
else:
  print(forward + s + backward)

Solution (Python):
def main():
  import sys
  input = sys.stdin.readline
  from collections import deque
  s = list(input())
  s = s[:-1]
  s = deque(s)
  q = int(input())
  c = [[i for i in input().split()] for j in range(q)]
  cnt = 1
  for i in c:
    if i[0] == '1':
      cnt *= -1
    else:
      if (i[1] == '1' and cnt == 1) or (i[1] == '2' and cnt == -1):
        s.appendleft(i[2])
      else:
        s.append(i[2])
  if cnt == 1:
    print(''.join(s))
  else:
    print(''.join(s)[::-1])
if __name__ == '__main__':
    main()

Solution (Python):
def main():
    s = input()
    n = int(input())
    pre = ''
    suf = ''
    r = 1
    for _ in range(n):
        q = input().split()
        if len(q) == 1:
            r *= -1
            continue
        if r * int(q[1]) in (1, -2):
            pre = q[2] + pre
        else:
            suf += q[2]
    ans = pre + s + suf
    if r == -1:
        ans = ans[::-1]
    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
from collections import deque

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

reverse_flag = 1

for _ in range(q):
    query = input().split()
    if query[0] == "1":
        reverse_flag *= -1
    else:
        if (query[1] == "1" and reverse_flag == 1) or (query[1] == "2" and reverse_flag == -1):
            s.appendleft(query[2])
        else:
            s.append(query[2])
if reverse_flag == 1:
    print("".join(s))
else:
    s.reverse()
    print("".join(s))

Solution (Python):
def RevFlag(flag):
    if flag:
        return False
    else:
        return True

def main():
    from collections import deque
    S = input()
    d = deque()
    for s in S:
        d.append(s)
    N = int(input())
    rev = False

    for i in range(N):
        Q = [i for i in input().split()]
        if len(Q) == 1:
            rev = RevFlag(rev)
        else:
            F = int(Q[1])
            C = Q[2]
            if F == 1:
                if rev:
                    d.append(C)  
                else:
                    d.appendleft(C)  
            else:
                if rev:
                    d.appendleft(C)  
                else:
                    d.append(C)  
    if rev:
        d = ''.join(list(reversed(d)))
    else:
        d = ''.join(list(d))
    print(d)


if __name__ == '__main__':
    main()

Solution (Python):

from collections import deque
s = deque(input())
q = int(input())
num = 0
for _ in range(q):
    t = input()
    if t == '1':
        if num == 0:
            num = 1
        else:
            num = 0
    else:
        t, f, c = t.split()
        if num == 0:
            if f == '1':
                s.appendleft(c)
            else:
                s.append(c)
        else:
            if f == '2':
                s.appendleft(c)
            else:
                s.append(c)
s = ''.join(s)
if num == 1:
    s = s[::-1]
print(s)

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

cnt = 0
left = ''
right = ''
for i in range(q):
    q_very = input()

    
    if q_very[0] == '1':
        
        cnt += 1
    else:
        instruction = q_very.split()
        
        if cnt % 2 == 0:
            if instruction[1] == '1':
                left = instruction[2] + left
            else:
                right = right + instruction[2]
        else: 
            if instruction[1] == '1':
                right = right + instruction[2]
            else:
                left = instruction[2] + left

s = left + s + right

if cnt % 2 != 0:
    s = s[::-1]
print(s)

Solution (Python):
from bisect import bisect_left as bl
from bisect import bisect_right as br
from heapq import heappush,heappop
from math import floor,ceil
from collections import *
from functools import reduce,cmp_to_key
import sys
input = sys.stdin.readline

M = mod = 998244353
def factors(n):return sorted(set(reduce(list.__add__, ([i, n//i] for i in range(1, int(n**0.5) + 1) if n % i == 0))))
def inv_mod(n):return pow(n, mod - 2, mod)

def li():return [int(i) for i in input().rstrip('\n').split()]
def st():return input().rstrip('\n')
def val():return int(input().rstrip('\n'))
def li2():return [i for i in input().rstrip('\n').split(' ')]
def li3():return [int(i) for i in input().rstrip('\n')]


s = st()
q = val()
l = []

cou = 0

d = deque()
for i in s:d.append(i)


for j in range(q):
    l = li2()
    if len(l) == 1:cou = not cou
    else:
        if cou:
            a = 3 - int(l[1])
        else:a = int(l[1])
        if a == 2:
            d.append(l[-1])
        else:d.appendleft(l[-1])

s = ''.join(e for e in d)
if cou:s = s[::-1]
print(s)

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

    from collections import deque
    strings = deque([S])
    switch = 0
    for _ in range(Q):
        line = input()
        if line.startswith('1'):
            switch ^= 1
            continue
        _, n, char = line.split()
        if n == '1':
            if switch == 0:
                strings.appendleft(char)
            else:
                strings.append(char)
        else:
            if switch == 0:
                strings.append(char)
            else:
                strings.appendleft(char)

    if switch:
        print(''.join(strings)[::-1])
    else:
        print(''.join(strings))
resolve()

Solution (Python):
S = input()
numQ = int(input())
lead = 1
front = ""
rear = ""
for i in range(numQ):
  Q = input()
  if Q[0] == "1":
    lead *= -1
  else:
    if Q[2] == "1":
      if lead == 1:
        front = Q[4] + front
      else:
        rear = rear + Q[4]
    else:
      if lead == 1:
        rear = rear + Q[4]
      else:
        front = Q[4] + front
S = front + S + rear
if lead == -1:
  S = S[::-1]

print(S)

Solution (Python):
S=input()
QN=int(input())
front=''
rear=''
rev=False

for i in range(QN):
    op=input()
    if op[0]=='1':
        rev=not(rev)
    else:
        if op[2]=='1':
            if rev:
                rear+=op[4]
            else:
                front+=op[4]
        else:
            if rev:
                front+=op[4]
            else:
                rear+=op[4]

if rev:
    print(rear[::-1]+S[::-1]+front)
else:
    print(front[::-1]+S+rear)

Solution (Python):
from collections import deque

S = input()
Q = int(input())
right, left = deque(), deque()
right_inv, left_inv, S_inv = False, False, False

for q in range(Q):
  T = input().split(" ")
  if len(T) == 1:
    
    S_inv = not S_inv
    right_inv = not right_inv
    left_inv = not left_inv
  else:
    
    F, C = int(T[1]), T[2]
    if (F == 1) ^ S_inv:
      left.appendleft(C)
    else:
      right.append(C)

if S_inv:
  print(''.join(list(right)[::-1]), S[::-1], ''.join(list(left)[::-1]), sep='')
else:
  print(''.join(list(left)), S, ''.join(list(right)), sep='')

Solution (Python):
from collections import deque

S = input()
Q = int(input())
right, left = deque(), deque()
right_inv, left_inv, S_inv = False, False, False

for q in range(Q):
  T = input().split(" ")
  if len(T) == 1:
    
    S_inv = not S_inv
    right_inv = not right_inv
    left_inv = not left_inv
  else:
    
    F, C = int(T[1]), T[2]
    if F == 1:
      if S_inv:
        
        right.append(C)
      else:
        
        left.appendleft(C)
    else:
      if S_inv:
        
        left.appendleft(C)
      else:
        
        right.append(C)

if S_inv:
  print(''.join(list(right)[::-1]), S[::-1], ''.join(list(left)[::-1]), sep='')
else:
  print(''.join(list(left)), S, ''.join(list(right)), sep='')

Solution (Python):
from collections import deque

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

ans = deque(s)
rev = False

for _ in range(q):
    t = list(input().split())
    if t[0] == '1':
        rev = not rev
    elif t[0] == '2':
        if rev == False:
            if t[1] == '1':
                ans.appendleft(t[2])
            else:
                ans.append(t[2])
        else: 
            if t[1] == '1':
                ans.append(t[2])
            else:
                ans.appendleft(t[2])

result = ''.join(ans)
if rev == True:
    result = result[::-1]

print(result)

Solution (Python):
from collections import deque
s = deque(input().split())
q = int(input())
rev = False
for _ in range(q):
    qu = input()
    if qu == "1":
        rev = not rev
    else:
        _, f, c = qu.split()
        if (f != "1") == rev:
            s.appendleft(c)
        else:
            s.append(c)

s = "".join(s)
if rev:
    s = reversed(s)
print("".join(s))

Solution (Python):
s = input()
q = int(input())
r, st1, st2 = 0, "", ""
for _ in range(q):
  query = input()
  if query[0] == "1":
    r += 1
  else:
    t, f, c = query.split()
    if (r + int(f)) % 2:
      st1 += c
    else:
      st2 += c

string = "".join(reversed(list(st1))) + s + st2
if r % 2 == 0:
  print(string)
else:
  print("".join(reversed(list(string))))

Solution (Python):
from collections import deque

s=input()
n=int(input())
q=deque()
q.append(s)
f=1
for i in range(n):
    t=list(input().split())
    if int(t[0])==1:
        f*=-1
    else:
        if f==1:
            if t[1]=='1' :
                q.appendleft(t[2])
            else:
                q.append(t[2])
        else:
            if t[1]=='2':
                q.appendleft(t[2])
            else:    
                q.append(t[2])
ans=''.join(q)
if f==-1:
    ans=ans[::-1]


print(ans)

Solution (Python):
def solve(string):
    s, q, *qs = string.split("\n")
    pre = []
    post = []
    preapp = lambda x:pre.append(x)
    postapp = lambda x:post.append(x)
    r = False
    for _q in qs[::-1]:
        if _q == "1":
            r = not r
            continue
        _, f, c = _q.split()
        if r and f == "2" or not r and f == "1":
            preapp(c)
        else:
            postapp(c)
    pre, post = "".join(pre), "".join(post[::-1])
    return pre + s[::-1] + post if r else pre + s + post

if __name__ == '__main__':
    import sys
    print(solve(sys.stdin.read().strip()))

Solution (Python):
a = input()
n = int(input())
rev = False
front = []
back = []
q = [input() for i in range(n)]
for i in q:
    if i == "1":
        rev = not rev
    else:
        i,f,c = i.split()
        if rev == False:
            if f == "1":
                front.append(c)
            else:
                back.append(c)
        else:
            if f == "1":
                back.append(c)
            else:
                front.append(c)
if rev == False:
    print("".join(list(reversed(front))+[a]+back))
else:
    print("".join(list(reversed(back))+[a[::-1]]+front))

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

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

hanten = -1
atama = ""
oshiri = ""
for i in range(n):
    List = list(input().split())
    if List[0] == "1":
        hanten = hanten * -1
    else:
        if List[1] == "1":
            if hanten == -1:
                atama = List[2] + atama
            else:
                oshiri = oshiri + List[2]
        else:
            if hanten == -1:
                oshiri = oshiri + List[2]
            else:
                atama = List[2] + atama

ans = atama + a + oshiri
if hanten == 1:
    ans = ans[::-1]

print(ans)

Solution (Python):
S = input()
Q = int(input())
query = [list(input().split()) for _ in range(Q)]
revflag = 0
before = ""
after = ""

for i in query:
    if i[0] == '1':
        revflag = 1 - revflag
    else:
        if (revflag == 0 and i[1] == '1') or (revflag == 1 and i[1] == '2'):
            before += i[2]
        else:
            after += i[2]

if revflag == 0:
    print(before[::-1]+S+after)
else:
    print(after[::-1]+S[::-1]+before)

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

is_reverse = False
front_queue = []
back_queue = []
for i in range(q):
    t = input()
    if t == "1":
        is_reverse = not is_reverse
    else:
        t2, f, c = t.split()
        if (f == "1" and not is_reverse) or (f == "2" and is_reverse):
            front_queue.append(c)
        else:
            back_queue.append(c)

result = ""
if is_reverse:
    result = "".join(reversed(back_queue)) + s[::-1] + "".join(front_queue)
else:
    result = "".join(reversed(front_queue)) + s + "".join(back_queue)
print(result)

Solution (Python):
def solve(string):
    s, q, *qs = string.split("\n")
    pre = []
    post = []
    r = False
    for _q in qs[::-1]:
        if _q == "1":
            r = not r
            continue
        _, f, c = _q.split()
        if r and f == "2" or not r and f == "1":
            pre.append(c)
        else:
            post.append(c)
    pre, post = "".join(pre), "".join(post[::-1])
    return pre + s[::-1] + post if r else pre + s + post


if __name__ == '__main__':
    import sys
    print(solve(sys.stdin.read().strip()))

Solution (Python):
from collections import deque
def main():
    s = deque(input())
    q = int(input())
    appendleft=s.appendleft
    append=s.append
    flag = False
    for i in range(q):
        temp = input().split()
        if temp[0] == "1":
            flag = not flag
        else:
            if temp[1] == "1":
                if flag:
                    append(temp[2])
                else:
                    appendleft(temp[2])
            else:
                if flag:
                    appendleft(temp[2])
                else:
                    append(temp[2])
    if flag:
        s.reverse()
    print("".join(list(s)))
if __name__=="__main__":
    main()

Solution (Python):
S = input()
Q = int(input())
Q_list = list()
for i in range(Q):
    Q_list.append(input().split())
 
from collections import deque
def operate(i):
    global count, str_list
    if Q_list[i][0] == '1':
        count += 1
    else:
        if count % 2 == 0:
            if Q_list[i][1] == '1':
                str_list.appendleft(Q_list[i][2])
            else:
                str_list.append(Q_list[i][2])
        else:
            if Q_list[i][1] == '1':
                str_list.append(Q_list[i][2])
            else:
                str_list.appendleft(Q_list[i][2])
 
check = 0
for i in range(Q):
    if Q_list[i][0] == '1':
        check += 1
 
S_list = [i for i in S]
if check % 2 == 1:
    S_list = list(reversed(S_list))
 
S_n = ''.join(S_list)
 
str_list = deque()
str_list.append(S_n)
count = check%2
for i in range(Q):
    operate(i)
print(''.join(str_list))

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)