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):
import math
import fractions
import bisect
import collections
import itertools
import heapq
import string
import sys
import copy
from collections import deque
sys.setrecursionlimit(10**7)
MOD = 10**9+7
def gcd(a,b):return fractions.gcd(a,b) 
def lcm(a,b):return (a*b) // fractions.gcd(a,b) 
def iin(): return int(input()) 
def ifn(): return float(input()) 
def isn(): return input().split() 
def imn(): return map(int, input().split()) 
def fmn(): return map(float, input().split()) 
def iln(): return list(map(int, input().split())) 
def iln_s(): return sorted(iln()) 
def iln_r(): return sorted(iln(), reverse=True) 
def fln(): return list(map(float, input().split())) 
def join(l, s=''): return s.join(l) 
def perm(l, n): return itertools.permutations(l, n) 
def perm_count(n, r): return math.factorial(n) // math.factorial(n-r) 
def comb(l, n): return itertools.combinations(l, n) 
def comb_count(n, r): return math.factorial(n) // (math.factorial(n-r) * math.factorial(r)) 
def two_distance(a, b, c, d): return ((c-a)**2 + (d-b)**2)**.5 
def m_add(a,b): return (a+b) % MOD

def sieves_of_e(n):
    is_prime = [True] * (n+1)
    is_prime[0] = False
    is_prime[1] = False
    for i in range(2, int(n**0.5)+1):
        if not is_prime[i]: continue
        for j in range(i * 2, n+1, i): is_prime[j] = False
    return is_prime

S = list(input())
d = deque(S)
Q = iin()
Query = [input().split() for _ in range(Q)]
isReversed = False

for i in range(Q):
    if Query[i][0] == '1':
        if isReversed:
            isReversed = False
        else:
            isReversed = True
    else:
        fl = 0

        if Query[i][1] == '1':
            fl = 0
            if isReversed:
                fl = 1
        else:
            fl = 1
            if isReversed:
                fl = 0
        if fl == 0:
            d.appendleft(Query[i][2])
        else:
            d.append(Query[i][2])
if isReversed:
    d.reverse()    
print(''.join(d))

Solution (Python):
from collections import deque

s = deque(input())
q = int(input())
flag = 0
for i in range(q):
    query = list(input().split())
    if query[0] == "1":
        flag = (flag+1)%2
    else:
        if flag == 0:
            if query[1] == "1":
                s.appendleft(query[2])
            else:
                s.append(query[2])
        else:
            if query[1] == "2":
                s.appendleft(query[2])
            else:
                s.append(query[2])

s = list(s)
if flag == 0:
    for i in s:
        print(i, end="")
if flag == 1:
    l = len(s)
    for i in range(l):
        print(s[l-i-1], end="")

print()

Solution (Python):
from _collections import deque
S = deque(input())
Q = int(input())
dir = 1

for i in range(Q):
    query = list(input().split())
    if len(query) == 1:
       dir *= -1
    else:
        T, f, c = query
        if int(f) == 1:
            if dir == 1:
                S.appendleft(c)
            else:
                S.append(c)
        else:
            if dir == 1:
                S.append(c)
            else:
                S.appendleft(c)
if dir == -1:
    S.reverse()
print(''.join(S))

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

ds = deque(S)

flg = True
for i in Q:
  if i[0] == "1":
    flg = not flg
  else:
    if flg == True:
      if i[1] == "1":
        ds.appendleft(i[2])
      else:
        ds.append(i[2])
    else:
      if i[1] == "2":
        ds.appendleft(i[2])
      else:
        ds.append(i[2])
if flg:
  print("".join(ds))
else:
  print("".join(ds)[::-1])

Solution (Python):
from collections import deque

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

for _ in range(q):
  query = input()
  if query == '1':
    reverse = 1 - reverse
  else:
    _,f,c = query.split()
    if 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):
import sys

input = sys.stdin.readline

S = input().strip()
Q = int(input())
Query = []
for _ in range(Q):
    Query.append(input().strip())

rev = S[::-1]
left = ""
right = ""
count = 0
flag = 'Normal'
for value in Query:
    if value == '1':
        count += 1
    if count % 2 == 1:
        flag = 'Reverse'
    else:
        flag = 'Normal'
    if value != "1":
        if flag == 'Normal':
            if value[2] == '1':
                left += value[4]
            elif value[2] == '2':
                right += value[4]
        elif flag == 'Reverse':
            if value[2] == '1':
                right += value[4]
            elif value[2] == '2':
                left += value[4]

if flag == 'Normal':
    ans = left[::-1] + S + right
elif flag == 'Reverse':
    ans = right[::-1] + rev + left
print(ans)

Solution (Python):

import sys
from collections import deque

SL = list(input())
Qn = int(input())


S = deque()
for s in SL:
    S.append(s)
    
even = 1
for i in range(Qn):
    t = list(map(str,input().split()))
    if len(t)==1:
        even = even^1
    else:
        if t[1]=='1':
            if even:
                S.appendleft(t[2])
            else:
                S.append(t[2])
        else:
            if even:
                S.append(t[2])
            else:
                S.appendleft(t[2])

if even == 0:
    S = reversed(S)

print(''.join(S))

Solution (Python):


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

A = []
B = []
revFlag = False;

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

    if q[0] == '1':
        revFlag = not revFlag
        A,B = B,A
    else:
        if q[1] == '1':
            A.append(q[2])
        else:
            B.append(q[2])


if revFlag:
    A,B = B,A
    


S2 = ''.join(reversed(A)) + S + ''.join(B)

if revFlag:
    S2 = ''.join(reversed(S2))

print(S2)

Solution (Python):
from collections import deque
s = deque(input())
q_num = int(input())
d = 0
for _ in range(q_num):
    q = input().split()
    if q[0] == '1':
        d = 1 - d
    else:
        if d == 0 and q[1] == '1' or d == 1 and q[1] == '2':
            s.appendleft(q[2])
        else:
            s.append(q[2])

s = ''.join(s)
if d == 1:
    s = s[::-1]
print(s)

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

count=0
add_s=[]
add_e=[]
for i in range(q):
    qi= input()
    if qi=='1':
        add_s,add_e=add_e,add_s
        count+=1
    else:
        _t,f,c=qi.split()
        if f =='1':
            add_s.append(c)
        else:
            add_e.append(c)

if count%2==0:
    print(''.join(reversed(add_s))+s+''.join(add_e))
else:
    print(''.join(reversed(add_s))+''.join(reversed(list(s)))+''.join(add_e))

Solution (Python):
from collections import deque

S = deque(list(input()))
Q = int(input())
r = False
left = deque()
right = deque()
for i in range(Q):
    q = input().split()
    if q[0] == "1":
        r = not r
    else:
        f, c = q[1:]
        if f == "1":
            if r:
                right.append(c)
            else:
                left.appendleft(c)
        else:
            if r:
                left.appendleft(c)
            else:
                right.append(c)

S, left, right = map(list, (S, left, right))
left.extend(S)
left.extend(right)
print("".join(left[::-1] if r else left))

Solution (Python):
S = input()
Q = int(input())
reverse = False
pref, suf = "", ""

for i in range(Q):
    query = list(input().split())
    if query[0] == "1":
        reverse = not reverse
    else:
        if reverse == False:
            if query[1] == "1":
                pref = query[2] + pref
            else:
                suf = suf + query[2]
        else:
            if query[1] == "1":
                suf = suf + query[2]
            else:
                pref = query[2] + pref

if reverse == False:
    ans = pref + S + suf
else:
    ans = suf[::-1] + S[::-1] + pref[::-1]

print(ans)

Solution (Python):
S=list(input())

D={}
MIN=0
MAX=len(S)-1
for i in range(len(S)):
    D[i]=S[i]
F=1
Q=int(input())
for i in range(Q):
    q=list(input().split())
    if F==1:
        if q[0]=="1":
            F*=-1
        else:
            if q[1]=="1":
                D[MIN-1]=q[2]
                MIN-=1
            else:
                D[MAX+1]=q[2]
                MAX+=1
    else:
        if q[0]=="1":
            F*=-1
        else:
            if q[1]=="2":
                D[MIN-1]=q[2]
                MIN-=1
            else:
                D[MAX+1]=q[2]
                MAX+=1
ans=""

for i in range(MIN,MAX+1):
    ans+=D[i]
if F==1:
    print(ans)
else:
    print(ans[::-1])

Solution (Python):

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

front = []
back = []
cnt = 0
for i in range(q):
    t = input()
    if t[0] == '1':
        cnt ^= 1
    else:
        f = 1 if int(t[2]) == 1 else 0
        c = t[4]
        if cnt ^ f:
            front.append(c)
        else:
            back.append(c)

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

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

s=input().rstrip('\n')
n=int(input())

s_f=[]
s_b=[]
muki=True

for i in range(n):
    line=input().rstrip('\n')
    
    if line == "1":
        muki=not muki
        continue

    q,f,c=map(str, line.split())
    q,f=int(q),int(f)
    if (f == 1 and muki) or (f == 2 and not muki):
        s_f.append(c)
    else:
        s_b.append(c)

ans = "".join(s_f[::-1]) + s + "".join(s_b)

if muki:
    print(ans)
else:
    print(ans[::-1])

Solution (Python):
from collections import deque
S = list(input())
Q = int(input())
ans = deque(S)
a = 1
for i in range(Q):
    q = input()
    if q == '1':
        a *= -1
    else:
        d, f, c = q.split()
        if f == '1':
            if a == 1:
                ans.appendleft(c)
            else:
                ans.append(c)
        else:
            if a == 1:
                ans.append(c)
            else:
                ans.appendleft(c)

ans = ''.join(list(ans))
if a == -1:
    ans = ans[::-1] 

print(ans)

Solution (Python):


s = input()
queNum = int(input())


reverse_cnt = 0
begin_lst = []
end_lst = []
for _ in range(queNum):
    query = input()

    if query == '1':
        reverse_cnt += 1
        begin_lst, end_lst = end_lst, begin_lst
    else:
        num, fi, ci = query.split()
        if fi == '1':
            begin_lst.append(ci)
        else:
            end_lst.append(ci)
    
if reverse_cnt % 2 == 0:
    pass
else:
    s = s[::-1]

print(''.join(reversed(begin_lst)) + s + ''.join(end_lst))

Solution (Python):
from collections import deque


def solve():
    S = deque(list(input()))
    Q = int(input())
    state = 0
    for _ in range(Q):
        query = list(input().split())
        if int(query[0]) == 1:
            state = 1 if state == 0 else 0
        else:
            if (state + int(query[1])) % 2 == 1:
                S.appendleft(query[2])
            else:
                S.append(query[2])
    if state == 0:
        print(''.join(S))
    else:
        S.reverse()
        print(''.join(S))


if __name__ == '__main__':
    solve()

Solution (Python):
import collections
s = collections.deque(list(input()))
q = int(input())
cnt = 0
for i in range(q):
    fc = input().split()
    if fc[0] == '1':
        cnt += 1
    else:
        if cnt == 1:
            fc[1] = '1' if fc[1] == '2' else '2'
        if fc[1] == '1':
            s.appendleft(fc[2])
        else:
            s.append(fc[2])
    cnt %= 2
s = list(s)
if cnt == 1:
    s.reverse()
print(*s, sep='')

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

    S = input().rstrip()
    Q = int(input())

    dq = deque(S)

    reverse = 0

    for _ in range(Q):
        Query = iter(input().rstrip().split())
        T = next(Query)
        if T == '1':
            reverse = 1 - reverse
            
            
            
            
            
            

        else:
            F, C = Query
            if F == '1':
                
                if reverse:
                    dq.append(C)
                else:
                    dq.appendleft(C)

            else:
                
                if reverse:
                    dq.appendleft(C)
                else:
                    dq.append(C)

    if reverse:
        dq = reversed(list(dq))

    res = ''.join(dq)
    print(res)


if __name__ == '__main__':
    main()

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

reverse = -1
n = int(input())
for i in range(n):
    query = input().split()
    if len(query) == 1:
        reverse *= (-1)
    else:
        f = int(query[1])
        c = query[2]
        if reverse == -1:
            if f == 1:
                d.appendleft(c)
            else:
                d.append(c)
        else:
            if f == 1:
                d.append(c)
            else:
                d.appendleft(c)

if reverse == 1:
    d.reverse()

print("".join(d))

Solution (Python):
s = list(input())
q = int(input())
direction = 1
mae = []
usiro = []
for i in range(q):
    qi = input()
    if qi == "1":
        direction *= -1
    else:
        tfq = list(qi.split())
        t = tfq[0]
        f = tfq[1]
        q = tfq[2]

        if f == "1":
            if direction == 1:
                mae.append(q)
            else:
                s.append(q)
        else:
            if direction == 1:
                s.append(q)
            else:
                mae.append(q)

if direction == -1:
    for i in s[::-1]:
        print(i, end = "")
    for i in mae:
        print(i, end = "")
else:
    for i in mae[::-1]:
        print(i, end = "")
    for i in s:
        print(i, end = "")

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

reverse=False
lst=["" for _ in range(500000+10)]
sp=200000+5
fp=200000+5

for i in range(len(S)):
    lst[sp+i]=S[i]
    fp+=1

for q in query:
    if q[0]=="1":
        reverse = not reverse
    else:
        if q[1]=="1":
            if reverse:
                
                lst[fp]=q[2]
                fp+=1
            else:
                
                sp-=1
                lst[sp]=q[2]
        else:
            if reverse:
                
                sp-=1
                lst[sp]=q[2]
            else:
                
                lst[fp]=q[2]
                fp+=1

if reverse:
    
    print("".join(lst[::-1]))
else:
    
    print("".join(lst))

Solution (Python):
from collections import deque
S=input()
q=int(input())
Q=[]
state=True
ans=deque(S)
for _ in range(q):
    t = input().split()
    if t[0] == "1":
        Q.append(int(t[0]))
    elif t[0] == "2":
        Q.append((int(t[0]),int(t[1]),t[2]))
for que in Q:
    if que == 1:
        state = not state
    else:
        if state:
            if que[1] == 1:
                ans.appendleft(que[2])
            else:
                ans.append(que[2])
        else:
            if que[1] == 2:
                ans.appendleft(que[2])
            else:
                ans.append(que[2])
anss=""
if not state:
    while len(ans) >0:
        anss += ans.pop()
else:
    while len(ans) >0:
        anss += ans.popleft()
print(anss)

Solution (Python):


import sys
sys.setrecursionlimit(300000)


def solve(s, Q, queries):
    l = ['']
    r = ['']
    rev = 0
    for q in queries:
        if q[0] == 1:
            rev += 1
            continue
        f = q[1]
        c = q[2]
        if rev % 2 == 0 and f == 2 or rev % 2 == 1 and f == 1:
            r.append(c)
        else:
            l.append(c)
    if rev % 2 == 0:
        l.reverse()
        ret = ''.join(l) + s + ''.join(r)
    else:
        r.reverse()
        ret = ''.join(r) + s[::-1] + ''.join(l)
    print(ret)
    return

def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    s = str(next(tokens))
    Q = int(next(tokens))
    q = []
    for i in range(Q):
        t = int(next(tokens))
        if t == 1:
            q.append([1, None, None])
        else:
            f = int(next(tokens))
            c = str(next(tokens))
            q.append([2, f, c])
    solve(s, Q, q)

if __name__ == '__main__':
    main()

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


Q = int(input())

reverse = 0

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

    if q[0] == "2":
        if not reverse:
            if q[1] == "1":
                S_deque.appendleft(q[2])
            elif q[1] == "2":
                S_deque.append(q[2])
        if reverse:
            if q[1] == "1":
                S_deque.append(q[2])
            elif q[1] == "2":
                S_deque.appendleft(q[2])
    else:
        if not reverse:
            reverse = 1
        else:
            reverse = 0

if reverse:
    print("".join(reversed(S_deque)))
else:
    print("".join(S_deque))

Solution (Python):
from collections import deque
S = input()
Q = int(input())
d = deque()
d.append(S)
reverse = False
for _ in range(Q):
    tmp = input().split()
    q = int(tmp[0])
    if q == 1:
        reverse = not reverse
    elif q == 2:
        f = int(tmp[1])
        if f == 1:
            if reverse:
                d.append(tmp[2])
            else:
                d.appendleft(tmp[2])
        elif f == 2:
            if reverse:
                d.appendleft(tmp[2])
            else:
                d.append(tmp[2])
S = "".join(d)
if reverse:
    print(S[::-1])
else:
    print(S)

Solution (Python):
import sys
import math
mod=10**9+7
inf=float("inf")
from math import sqrt
from collections import deque
input=lambda: sys.stdin.readline().strip()
sys.setrecursionlimit(11451419)
from functools import lru_cache




s=deque(list(input()))
q=int(input())
rev=0
for _ in range(q):
    a=input()
    if a=="1": 
        rev=1-rev
        continue
    n,m,p=map(str,a.split())
    if m=="1":
        if rev==1:
            s.append(p)
        else:
            s.appendleft(p)
    else:
        if rev==1:
            s.appendleft(p)
        else:
            s.append(p)
if rev==1:
    print("".join(list(s)[::-1]))
else:
    print(("".join(list(s))))

Solution (Python):





if __name__ == "__main__":

    S = input()
    Q = int(input())
    rev = False
    head = []
    tail = []
    for _ in range(Q):
        task = input()
        if task == "1":
            rev = not rev
        else:
            _, f, c = task.split()
            if f == "1":
                if rev:
                    tail.append(c)
                else:
                    head.append(c)
            else:
                if rev:
                    head.append(c)
                else:
                    tail.append(c)

        

    ans = ''.join(reversed(head)) + S + ''.join(tail)
    if rev:
        ans = ''.join(reversed(ans))

    print(ans)

Solution (Python):

from collections import deque


def resolve():
    S=deque(list(input()))
    Q=int(input())
    querys=[input() for i in range(Q)]
    rev=False
    for qq in querys:
        if qq=="1":
            rev = not rev
            continue
        _,f,c=qq.split()
        if rev==False and f=="1":
            S.appendleft(c)
        elif rev == True and f=="1":
            S.append(c)
        elif rev==False and f=="2":
            S.append(c)
        else:
            S.appendleft(c)
    ans=list(S)
    if rev ==False:
        print("".join(ans))
    else:
        print("".join(reversed(ans)))



    


if __name__ == "__main__":
    resolve()

Solution (Python):
from collections import deque
d = deque()
s = input()
d.append(s)
rev = 0

Q = int(input())

for q in range(Q):
    ls = input().split()
    
    if ls[0] == '1':
        rev = (rev + 1) % 2
    else:
        if (int(ls[1]) + rev) % 2 == 0:
            d.append(ls[2])
        else:
            d.appendleft(ls[2])

ans = ''.join(list(d))
if rev == 1:
    ans = ans[::-1]
print (ans)

Solution (Python):
s = input()
q = int(input())
add_head = ""
add_tail = ""
rev = False
for _ in range(q):
    query = input().split()
    if query[0] == "1":
        rev = not rev
    else:
        if query[1] == "1":
            if rev:
                add_tail = add_tail + query[2]
            else:
                add_head = add_head + query[2]
        else:
            if rev:
                add_head = add_head + query[2]
            else:
                add_tail = add_tail + query[2]
                
if not rev:
    print(add_head[::-1] + s + add_tail)
else:
    print(add_tail[::-1] + s[::-1] + add_head)

Solution (Python):
from collections import deque



def main():
    S = input()
    Q = int(input())

    order = 1

    ans = deque()
    for i in range(len(S)):
        ans.append(S[i])

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

        if tfc[0] == "1":
            order *= -1
        else:
            if order == 1:
                if tfc[1] == "1":
                    
                    ans.appendleft(tfc[2])
                else:
                    
                    ans.append(tfc[2])

            else:
                if tfc[1] == "2":
                    
                    ans.appendleft(tfc[2])
                else:
                    
                    ans.append(tfc[2])


    ans = "".join(ans)

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

if __name__ == "__main__":
    main()

Solution (Python):
import sys
sys.setrecursionlimit(700000)

def s_in():
    return input()

def n_in():
    return int(input())

def l_in():
    return list(map(int, input().split()))

def print_l(l):
    print(' '.join(map(str, l)))

class Interval():
    def __init__(self, li):
        self.li = li
        self.n = len(li)
        self.sum_li = [li[0]]
        for i in range(1, self.n):
            self.sum_li.append(self.sum_li[i-1] + li[i])

    def sum(self, a, b=None):
        if b is None:
            return self.sum(0, a)

        res = self.sum_li[min(self.n-1, b-1)]
        if a > 0:
            res -= self.sum_li[a-1]
        return res

s = s_in()
q = n_in()

d = True

pre = []
suf = []

for _ in range(q):
    query = s_in()
    if len(query) == 1:
        d = not d
    else:
        _, f, c = query.split()
        if d:
            if f == '1':
                pre.append(c)
            else:
                suf.append(c)
        else:
            if f == '1':
                suf.append(c)
            else:
                pre.append(c)

s = "".join(pre[::-1]) + s + "".join(suf)

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

Solution (Python):
s=list(input())
q=int(input())
cnt=1
sen=[]
matu=[]
for i in range(q):
    a=list(input().split())
    if a[0]=='1':
        cnt*=-1
    else:
        if a[1]=='1':
            if cnt==1:
                sen.append(a[2])
            else:
                matu.append(a[2])
        else:
            if cnt==1:
                matu.append(a[2])
            else:
                sen.append(a[2])

if cnt==-1:
    s=s[::-1]
ss=''.join(s)
if cnt==1:
    sen=sen[::-1]
sensen=''.join(sen)
if cnt==-1:
    matu=matu[::-1]
matumatu=''.join(matu)
if cnt==1:
    print(sensen+ss+matumatu)
else:
    print(matumatu+ss+sensen)

Solution (Python):
s = input()
q = int(input())
reverse = False
front = []
back = []
for i in range(q):
    inp = list(input().split())
    if len(inp) == 1:
        if reverse:
            reverse = False
        else:
            reverse = True
        continue
    t, f, c = inp[0], int(inp[1]), inp[2]
    if not reverse:
        if f == 1:
            front.append(c)
        else:
            back.append(c)
    else:
        if f == 2:
            front.append(c)
        else:
            back.append(c)
s = "".join(front[::-1]) + s + "".join(back)
if reverse:
    print(s[::-1])
else:
    print(s)

Solution (Python):
s = input()
q = int(input())
left = ""
right = ""


def s_add(r, f):
    if r % 2 == 0:
        if f == 1:
            return True
        else:
            return False
    else:
        if f == 1:
            return False
        else:
            return True


cnt = 0
for _ in range(q):
    query = input()
    if query == "1":
        cnt += 1
    else:
        z, f, c = query.split()
        if s_add(cnt, int(f)):
            left += c
        else:
            right += c

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

Solution (Python):



def main():
    import sys
    sys.setrecursionlimit(10**6)
    

    def dump(*args):
        sys.stderr.write(str(args))

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

    mode = 0
    from collections import deque
    que = deque(list(s))
    for i in query:
        if len(i) == 1:
            mode = 1-mode
            continue
        f = int(i[1])
        if mode == 0:
            if f == 1:
                que.appendleft(i[2])
            else:
                que.append(i[2])
        else:
            if f == 1:
                que.append(i[2])
            else:
                que.appendleft(i[2])
    mode = 1 if mode == 0 else - 1
    
    qq = list(que)
    
    print(*(qq[::mode]), sep='')


if __name__ == '__main__':
    main()

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

Solution (Python):
from collections import deque

def main():
    s=deque(input())
    q=int(input())
    
    right=True
    for _ in range(q):
        l=list(map(str,input().split()))
        if l[0]=='1':
            if right:
                right=False
            else:
                right=True
        else:
            if l[1]=='1':
                if right:
                    s.appendleft(l[2])
                else:
                    s.append(l[2])
            else:
                if right:
                    s.append(l[2])
                else:
                    s.appendleft(l[2])
    
    if right:
        print(''.join(s))
    else:
        print(''.join(list(s)[::-1])) 

if __name__=='__main__':
    main()

Solution (Python):
import sys

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

S = sr()
Q = ir()
dir = 0 
head_tail = ['', '']
for _ in range(Q):
    x = sr().split()
    if x[0] == '1':
        dir = (1 - dir)
    else:
        t, f, c = x
        h_d = dir
        if f == '2':
            h_d = (1 - h_d)
        head_tail[h_d] += c

if dir == 0:
    answer = head_tail[0][::-1] + S + head_tail[1]
else:
    answer = head_tail[1][::-1] + S[::-1] + head_tail[0]

print(answer)

Solution (Python):
from collections import deque

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

inv = False
for _ in range(q):
    com, *query = input().split()
    if com == "1":
        if not inv:
            inv = True
        else:
            inv = False
    else:
        f, c = query
        if not inv:
            if f == "1":
                s.appendleft(c)
            else:
                s.append(c)
        else:
            if f == "1":
                s.append(c)
            else:
                s.appendleft(c)


s = "".join(s)
print(s if not inv else s[::-1])

Solution (Python):
from collections import deque

s = input()

q = int(input())

d = deque(list(s))

inversed = -1

for i in range(q):
    query = list(input().split())
    if int(query[0]) == 1:
        inversed *= -1
    else:
        if int(query[1]) == 1:
            if inversed < 0:
                d.appendleft(query[2])
            else:
                d.append(query[2])
        else:
            if inversed < 0:
                d.append(query[2])
            else:
                d.appendleft(query[2])

if inversed < 0:
    print(''.join(list(d)))
else:
    print(''.join(list(d)[::-1]))

Solution (Python):
from collections import deque
s = list(input())
ans = deque(s)
q = int(input())
check = 1
for i in range(q):
    a = list(input().split())
    if a[0] == "1":
        check *= -1
    else:
        if a[1] == "1":
            if check == 1:
                ans.appendleft(a[2])
            else:
                ans.append(a[2])
        else:
            if check == 1:
                ans.append(a[2])
            else:
                ans.appendleft(a[2])
    
ans = list(ans)
if check==1:
    print(*ans,sep="")
else:
    ans = ans[::-1]
    print(*ans,sep="")

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

S = input()[:-1]
Q = int(input())
que = []
acc = [0]

for i in range(Q):
    l = list(input().split())
    ty = int(l[0])
    
    if ty==1:
        acc.append(acc[-1]+1)
    else:
        acc.append(acc[-1]+0)
        
    if ty==1:
        continue
        
    f = int(l[1])
    que.append((i, f, l[2]))
    
ans = deque([])

if (acc[-1]-acc[0])%2==0:
    for i in range(len(S)):
        ans.append(S[i])
else:
    for i in range(len(S)-1, -1, -1):
        ans.append(S[i])

for i, f, c in que:
    cnt = acc[-1]-acc[i]
    
    if cnt%2==0:
        if f==1:
            ans.appendleft(c)
        else:
            ans.append(c)
    else:
        if f==1:
            ans.append(c)
        else:
            ans.appendleft(c)

ans = list(ans)
print(''.join(ans))

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