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

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

right=True
rev=False
for i in range(q):
    t=input().split()
    if t[0]=="1":
        right=not right
    if t[0]=="2":
        f=int(t[1])
        c=t[2]
        if f==1:rev=True
        else:rev=False

        if right^rev:
            s.append(c)
        else:
            s.appendleft(c)

if not right:
    s.reverse()
ans="".join(list(s))
print(ans)

Solution (Python):
s = input().strip()
q = int(input())
l, r = "", ""
rev = 0
for i in range(q):
    a = input().split()
    t = int(a[0])
    if t == 1:
        rev += 1
    else:
        f = int(a[1])
        c = a[2]
        if f == 1:
            if rev % 2 == 0:
                l = c+l
            else:
                r = r+c
        else:
            if rev % 2 == 0:
                r = r+c
            else:
                l = c+l

if rev % 2 == 0:
    print(l+s+r)
else:
    print(r[::-1]+s[::-1]+l[::-1])

Solution (Python):
from collections import deque

S = deque(list(input()))
Q = int(input())
count = 0
for q in range(Q):
    Query = input().split()
    if Query == ['1']:
        count += 1
    else:
        if count % 2 == 0:
            if Query[1] == "1":
                S.appendleft(Query[2])
            else:
                S.append(Query[2])
        else:
            if Query[1] == "1":
                S.append(Query[2])
            else:
                S.appendleft(Query[2])
if count % 2 == 1:
    print(''.join(list(S)[::-1]))
else:
    print(''.join(S))

Solution (Python):
import sys
fastinput = sys.stdin.readline
from collections import deque
S = input()
Q = int(input())
ans = deque([S])
Reverse = False
for q in range(Q):
    query = fastinput().split()
    if query[0]=="1":
        Reverse = not Reverse
    elif (query[1]=="1") ^ Reverse:
        ans.appendleft(query[2])
    else:
        ans.append(query[2])
SS = "".join(list(ans))
if Reverse:
    SS = "".join(reversed(SS))
print(SS)

Solution (Python):
from collections import deque
S = deque(input())
Q = int(input())
inversion_flag = False
for _ in range(Q):
    query_list = list(input().split())
    if query_list[0] == "1":
        inversion_flag = not inversion_flag
        continue
    C = query_list[2]
    if query_list[1] == "2":
        if inversion_flag:
            S.appendleft(C)
        else:
            S.append(C)
    else:
        if inversion_flag:
            S.append(C)
        else:
            S.appendleft(C)

S = list(S)
if inversion_flag:
    print(*S[::-1], sep="")
else:
    print(*S, sep="")

Solution (Python):
from collections import deque

S = deque(input())
rev = False

Q = int(input())

for _ in range(Q):
    i_q = list(input().split())
    if i_q[0] == "1":
        rev = not rev
    else:
        appendtop = False
        if not rev and i_q[1] == "1":
            appendtop = True
        elif rev and i_q[1] == "2":
            appendtop = True

        if appendtop:
            S.appendleft(i_q[2])
        else:
            S.append(i_q[2])

if rev:
    while len(S) > 0:
        print(S.pop(), end="")
else:
    while len(S) > 0:
        print(S.popleft(), end="")

print()

Solution (Python):
def main():
    S = input()
    Q = int(input())
    reverse = False
    front = []
    back = []
    for _ in range(Q):
        TFC = list(input().split())
        if len(TFC) == 1:
            reverse = not(reverse)
        else:
            t,f,c = TFC
            if reverse:
                if f == "2":
                    front.append(c)
                else:
                    back.append(c)

            else:
                if f == "1":
                    front.append(c)
                else:
                    back.append(c)
    if reverse:
        print("".join(back[::-1]) + S[::-1] + "".join(front))
    else:
        print("".join(front[::-1]) + S + "".join(back))
    return

if __name__ == "__main__":
    main()

Solution (Python):
a=input();b='';l='';f=0
for _ in range(int(input())):
    k=list(input().split())
    if k[0]=='1':
        f=(f+1)%2
    else:
        if (k[1]=='1' and f==0) or (k[1]=='2' and f==1):
            b+=k[2]
        else:
            l+=k[2]
p=b[::-1]+a+l
print(p if f==0 else p[::-1])

Solution (Python):
S = input()
Q = int(input())
cnt = 0
l = []
r = []
for i in range(Q):
    q = list(input().split())
    if int(q[0]) == 1:
        cnt += 1
    else:
        if (cnt+int(q[1]))%2 == 0:
            r.append(q[2])
        else:
            l.append(q[2])
if cnt%2 == 0:
    l.reverse()
    print(''.join(l)+S+''.join(r))
else:
    r.reverse()
    print(''.join(r)+S[::-1]+''.join(l))

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

import math

import logging

logging.basicConfig(level=logging.DEBUG)

logger = logging.getLogger(__name__)

from collections import deque


def resolve():
    S = [x for x in sys.stdin.readline().split()][0]  
    Q = [int(x) for x in sys.stdin.readline().split()][0]  
    
    

    
    
    
    

    logger.debug('{}'.format([]))

    reverse = False

    s_list = deque(S)

    for _ in range(Q):
        q = input().strip()
        if q == '1':
            reverse = not reverse
        else:
            t, f, c = q.split(' ')
            if (f == '1' and not reverse) or (f == '2' and reverse):
                s_list.appendleft(c)
            else:
                s_list.append(c)

    if reverse:
        print(''.join(list(s_list)[::-1]))
    else:
        print(''.join(list(s_list)))


if __name__ == "__main__":
    resolve()





import sys
from io import StringIO
import unittest


class TestClass(unittest.TestCase):
    def assertIO(self, input, output):
        stdout, stdin = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(input)
        resolve()
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, stdin
        self.assertEqual(out, output)

    def test_入力例_1(self):
        input = """a
4
2 1 p
1
2 2 c
1"""
        output = """cpa"""
        self.assertIO(input, output)

    def test_入力例_2(self):
        input = """a
6
2 2 a
2 1 b
1
2 2 c
1
1"""
        output = """aabc"""
        self.assertIO(input, output)

    def test_入力例_3(self):
        input = """y
1
2 1 x"""
        output = """xy"""
        self.assertIO(input, output)

Solution (Python):
import sys
from collections import deque

readline = sys.stdin.readline

S = list(readline().rstrip())
Q = int(readline())
a = deque([])
b = deque([])
s = 1

for _ in range(Q):
    query = readline().rstrip()
    if query == '1':
        s = -s
    else:
        _, F, C = map(str, query.split())
        if s == 1:
            if F == '1':
                a.appendleft(C)
            else:
                b.append(C)
        else:
            if F == '1':
                b.append(C)
            else:
                a.appendleft(C)

answer = list(a) + S + list(b)
answer = ''.join(answer)
if s == -1:
    answer = answer[::-1]
print(answer)

Solution (Python):
from collections import deque
s = input()
q = int(input())
flag = True
atama = deque()
osiri = deque()
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:
                atama.appendleft(c)
            else:
                osiri.append(c)
        else:
            if flag:
                osiri.append(c)
            else:
                atama.appendleft(c)

if flag:
    while(atama):
        tmp = atama.popleft()
        print(tmp, end='')
    print(s, end='')
    while(osiri):
        tmp = osiri.popleft()
        print(tmp, end='')
else:
    while(osiri):
        tmp = osiri.pop()
        print(tmp, end='')
    print(s[::-1], end='')
    while(atama):
        tmp = atama.pop()
        print(tmp, end='')

Solution (Python):

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

reverse = False 

list_first =  []
list_last = []

for i in range(Q):
  query = input().split()
  if query[0] == '1':
    reverse = not reverse
  elif (query[1]=='1') == reverse: 
    list_last.append(query[2])
  else: 
    list_first.append(query[2])

if not reverse:
  for c in list_first[::-1] + list(S) + list_last:
    print(c, end='')
else:
  for c in list_last[::-1] + list(S)[::-1] + list_first:
    print(c, end='')

print('')

Solution (Python):
from collections import deque
s=deque(list(input()))
p=0
for i in range(int(input())):
    a=list(map(str,input().split()))
    if len(a)==1:
        p^=1
    else:
        if p%2:
            if a[1]=="2":
                s.appendleft(a[2])
            else:
                s.append(a[2])
        else:
            if a[1]=="1":
                s.appendleft(a[2])
            else:
                s.append(a[2])
ans=""
if p%2==1:
    while s:
        ans+=s.pop()
else:
    while s:
        ans+=s.popleft()
print(ans)

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


hanten = 0
mae = ''
ushiro = ''
for i in range(q):
    x = input()
    if x[0] == '1':
        
        hanten += 1
    else:
        xx = x.split(' ')
        
        if xx[1] == '1':
            if hanten % 2 == 0:
                mae = xx[2] + mae
            else:
                ushiro = ushiro + xx[2]
        else:
            if hanten % 2 == 0:
                ushiro = ushiro + xx[2]
            else:
                mae = xx[2] + mae           

if hanten % 2 == 0:
    s = mae + s + ushiro
else:
    s = ushiro[::-1] + s[::-1] + mae[::-1]

print(s)

Solution (Python):
s = input()
front = []
back = []
isflip = False




a = int(input())
for i in range(a):
    query = input()
    if(int(query[0]) == 1):
        isflip = not isflip
    else:
        t, f, c = query.split()
        if(int(f) == 2):
            if(isflip):
                front.append(c)
            else:
                back.append(c)
        else:
            if(not isflip):
                front.append(c)
            else:
                back.append(c)

if(isflip):
    print("".join(str(e) for e in reversed(back)) + "".join(str(e) for e in reversed(s)) + "".join(str(e) for e in front))
else:
    print("".join(str(e) for e in reversed(front)) + s + "".join(str(e) for e in back))

Solution (Python):
from collections import deque
def inputs(): return [int(x) for x in input().split()]
s = deque(input())
q = int(input())
flag = 1
for i in range(q):
    q1 = input()
    if len(q1)==1:
      flag *=-1 
    else:
        l,f,c = q1.split()
        if flag ==1:
            if int(f)==1:
                s.appendleft(c)
            else:
                s.append(c)
        elif flag==-1:
            if int(f)==1:
                s.append(c)
            else:
                s.appendleft(c)
if flag==1:
    print("".join(list(s)))
else:
    s = list(s)
    s = s[::-1]
    print("".join(list(s)))

Solution (Python):
s=input()
n=int(input())
head=[]
end=[]
rev = False
for i in range(n):
    q = list(map(str,input().split()))
    if q[0]=='1':
        head,end = end, head
        rev = not rev
    elif q[0]=='2':
        if q[1]=='1':
            head.append(q[2])
        else:
            end.append(q[2])

head.reverse()
if rev:
    ans = list(s)
    ans.reverse()
else:
    ans = list(s)
print("".join(head+ans+end))

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

forward = ""
reverse = ""

rev = 0
for _ in range(q):
    query = list(input().split())
    t = int(query[0])
    if t == 1:
        rev = rev^1
    else:
        f = int(query[1])
        c = query[2]
        if f == 1:
            if rev == 0:
                forward += c
            else:
                reverse += c
        else:
            if rev == 0:
                reverse += c
            else:
                forward += c

ans = ""
if rev == 0:
    for i in reversed(range(len(forward))):
        ans += forward[i]
    ans += s
    ans += reverse
else:
    for i in reversed(range(len(reverse))):
        ans += reverse[i]
    for i in reversed(range(len(s))):
        ans += s[i]
    ans += forward

print(ans)

Solution (Python):
from collections import deque
s = list(input())
q = int(input())
d = deque(s)
rot = 0
for i in range(q):
    que = list(map(str,input().split()))
    if len(que) == 1:
        rot += 1
    else:
        num = int(que[1])
        cha = que[2]
        if num == 1 and rot % 2 == 0:
            d.appendleft(cha)
        elif num == 1 and rot % 2 == 1:
            d.append(cha)
        elif num == 2 and rot % 2 == 0:
            d.append(cha)
        elif num == 2 and rot % 2 == 1:
            d.appendleft(cha)
d = list(d)
if rot % 2 == 1:
    d.reverse()
    print("".join(d))
else:
    print("".join(d))

Solution (Python):
from collections import deque
S = list(input())
s = deque(S)
Q = int(input())
t = 0
for i in range(Q):
    qi = input()
    if qi == "1":
        t += 1
    else:
        qi = list(qi)
        if (int(qi[2])+t) % 2 ==0:
            s.append(qi[4])
        else:
            s.appendleft(qi[4])

s = list(s)
if t % 2 == 1:
    ans = [[] for j in range(len(s))]
    for i in range(len(s)):
        ans[i] = s[len(s)-i-1]
else:
    ans = s

print("".join(ans))

Solution (Python):
s = input().rstrip()
q = int(input().rstrip())
f = True

a = []
z = []
for _ in range(q):
    c = input().rstrip()
    if c == "1":
        f = not f
    else:
        _, cc, ccc = c.split()
        if (cc == "1") == f:
            a.append(ccc)
        else:
            z.append(ccc)

aa = "".join(a[::-1])
zz = "".join(z)
s = aa + s + zz

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

Solution (Python):
from collections import deque

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

for _ in range(q):
    input_ = input()
    if input_ == '1':
        rev *= -1
        continue
    _, f, c = input_.split()
    if rev == 1:
        if f == '1':
            s.appendleft(c)
        else:
            s.append(c)
    else:
        if f == '1':
            s.append(c)
        else:
            s.appendleft(c)
ans = list(s)
if rev == -1:
    ans = ans[::-1]

print(''.join(ans))

Solution (Python):




def solve(inp):
    S = inp.readline().strip()
    Q = int(inp.readline().strip())
    Queries = [inp.readline().strip() for i in range(Q)]

    l = []
    r = []
    dir = False
    for Query in Queries:
        if Query == "1":
            dir = not dir
        else:
            (T, F, C) = Query.split(' ')
            if dir == (F == "1"):
                r.append(C)
            else:
                l.append(C)

    if dir:
        return "".join(reversed(r)) + "".join(reversed(S)) + "".join(l)
    else:
        return "".join(reversed(l)) + S + "".join(r)


def main():
    import sys
    result = solve(sys.stdin)
    if result is not None and result != '':
        print(result)


if __name__ == '__main__':
    main()

Solution (Python):

from collections import deque
s = deque(list(input()))
q = int(input())

reverse_flag = 1

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


if reverse_flag == -1:
    s.reverse()

print("".join(s))

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


def LI(): return [int(x) for x in sys.stdin.readline().split()]


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


def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res


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



def A():
    S = input()
    if S == 'AAA' or S == 'BBB':
        print('No')
    else:
        print('Yes')
    return



def B():
    N, A, B = LI()
    ans = N // (A + B) * A + min(N % (A + B), A)
    print(ans)
    return



def C():
    A, B = LI()
    for i in range(1, 1001):
        if i * 8 // 100 == A and i * 10 // 100 == B:
            print(i)
            return
    print(-1)
    return



def D():
    s = S()
    d = deque(s)
    q = I()
    f = 1
    for i in range(q):
        query = input().split()
        if len(query) == 1:
            f *= -1
        else:
            if query[1] == '1':
                if f == 1:
                    d.appendleft(query[2])
                else:
                    d.append(query[2])
            else:
                if f == 1:
                    d.append(query[2])
                else:
                    d.appendleft(query[2])
    ans = ''.join(d)
    if f == -1:
        ans = ans[::-1]
    print(ans)
    return



def E():
    return



def F():
    return



if __name__ == "__main__":
    D()

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

Solution (Python):

S=input()
Q=int(input())
qls=[list(map(str,input().split())) for _ in range(Q)]
N=len(S)
res=['']*(2*Q+N)
res[Q:Q+N]=S
ins = [Q+N, Q-1]
flg=0 
for q in qls:
    if q[0]=='1':
        flg = (flg+1)%2
    else:
        x = (flg+int(q[1]))%2
        idx = ins[x]
        res[idx] = q[2]
        if x==0:
            ins[x]+=1
        else:
            ins[x]-=1
if flg==0:
    print(''.join(res))
else:
    print(''.join(res[::-1]))

Solution (Python):
from collections import deque

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

cnt=0
for _ in range(q):
    query=list(input().split())
    if query[0]=='1':
        cnt=(cnt+1)%2
    else:
        if query[1]=='1':
            if cnt==1:
                d.append(query[2])
            else:
                d.appendleft(query[2])
        else:
            if cnt==1:
                d.appendleft(query[2])
            else:
                d.append(query[2])
                
ans=list(d)
                
if cnt==1:
    print(*ans[::-1],sep='')
else:
    print(*ans,sep='')

Solution (Python):
import sys
from collections import *
import heapq
import math
import bisect
from itertools import permutations,accumulate,combinations,product
from fractions import gcd
def input():
    return sys.stdin.readline()[:-1]
def ruiseki(lst):
    return [0]+list(accumulate(lst))
mod=pow(10,9)+7

s=input()
q=int(input())
query=[input().split() for i in range(q)]
now=0
d=deque(s)

for i in range(q):
    if query[i][0]=="1":
        now+=1
    else:
        if now%2==0:
            if query[i][1]=="1":
                d.appendleft(query[i][2])
            else:
                d.append(query[i][2])
        else:
            if query[i][1]=="1":
                d.append(query[i][2])
            else:
                d.appendleft(query[i][2])
if now%2==1:
    d.reverse()
print("".join(d))

Solution (Python):

S=input()
Q=int(input())
t=1 
fro=''
bac=''
for _ in range(Q):
    q=input()
    if q[0]=='1':
        t=(t+1)%2
    else:
        __,f,c=q.split()
        if f=='1':
            if t==1:
                fro=c+fro
            else:
                bac=bac+c
        else:
            if t==1:
                bac=bac+c
            else:
                fro=c+fro
    
ans=fro+S+bac
print(ans if t else ans[::-1])

Solution (Python):

import sys

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

    def appendBottom(self,v):
        self.next = v
        v.prev = self
        return v

    def printRecursively(self):
        current = self
        s = ""
        while True:
            if current == None:
                break
            s += current.value
            current = current.next
        print(s)

    def printRecursivelyInvert(self):
        current = self
        s = ""
        while True:
            if current == None:
                break
            s += current.value
            current = current.prev
        print(s)


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    S=next(tokens)
    Q=int(next(tokens))
    queries = []
    invert = False
    for i in range(Q):
        t = int(next(tokens))
        if t == 1:
            queries.append((t,))
        else:
            queries.append((t,int(next(tokens)),next(tokens)))
    tail = Node(S[0])
    head = tail
    for i in range(1,len(S)):
        tail = tail.appendBottom(Node(S[i]))
    for query in queries:
        if query[0] == 1:
            invert = not invert
        else:
            if query[1] == 1:
                if invert:
                    tail = tail.appendBottom(Node(query[2]))
                else:
                    head = head.insertHead(Node(query[2]))
            else:
                if not invert:
                    tail = tail.appendBottom(Node(query[2]))
                else:
                    head = head.insertHead(Node(query[2]))
    if invert:
        tail.printRecursivelyInvert()
    else:
        head.printRecursively()
    pass

if __name__ == '__main__':
    main()

Solution (Python):

import collections


class Solution:
    def solve(self, s, queries):
        mQueue = collections.deque([ch for ch in s])

        revCount = 0

        for query in queries:
            if query[0] == 1:
                revCount += 1
            elif query[0] == 2:
                if revCount % 2 == 0:
                    if query[1] == '1':
                        mQueue.appendleft(query[2])
                    else:
                        mQueue.append(query[2])
                else:
                    if query[1] == '1':
                        mQueue.append(query[2])
                    else:
                        mQueue.appendleft(query[2])

        if revCount % 2 == 0:
            return "".join(mQueue)
        else:
            return "".join(reversed(mQueue))


sol = Solution()

s = input().strip()
q = int(input().strip())

queries = []

for i in range(q):
    query = list(input().strip().split())
    query[0] = int(query[0])

    queries.append(query)

print(sol.solve(s, queries))

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

flag = False
for _ in range(Q):
    IN = list(map(str,input().split()))

    if IN[0] == "1":
        flag = not flag
        continue

    _,F,C = IN
    if flag:
        if F == "1":
            S.append(C)
        else:
            S.appendleft(C)
    else:
        if F == "1":
            S.appendleft(C)
        else:
            S.append(C)

if flag:
    print(*reversed(S),sep="")
else:
    print(*S,sep="")

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

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

reverse = False

before = ""
after = ""

for _ in range(Q):
    s = input().strip()
    if s[0] == "1":
        reverse = not reverse
    else:
        _, f, c = s.split()
        if f == "1":
            if reverse:
                after = after + c
            else:
                before = c + before
        else:
            if reverse:
                before = c + before
            else:
                after = after + c

if reverse:
    print(after[::-1] + S[::-1] + before[::-1])
else:
    print(before + S + after)

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

import sys
input = sys.stdin.readline

qs = [list(input().split()) for _ in range(q)]

left = ''
right = ''


front = False
for i in qs:
    if(i[0] == '1'):
        front = not front
        continue

    f = int(i[1])
    c = i[2]
    if(front + f == 2):
        right = right + c
    else:
        left = left + c

if(not front):
    ans = left[::-1] + s + right
else:
    ans = right[::-1] + s[::-1] + left

print(ans)

Solution (Python):
S = input()
additions = [[],[]]
direction = 0
Q = int(input())
for _ in range(Q):
    query = input().split()
    T = int(query[0])
    if T == 1:
        direction ^= 1
    else:
        F = int(query[1])
        C = query[2]
        additions[(F+direction+1) % 2].append(C)
if direction == 0:
    print(''.join(additions[0][::-1]), end='')
    print(S, end='')
    print(''.join(additions[1]))
else:
    print(''.join(additions[1][::-1]), end='')
    print(S[::-1], end='')
    print(''.join(additions[0]))

Solution (Python):
S = input()
Q = int(input())
cnt = 0
mae = []
usiro = []
for _ in range(Q):
    q = input().split()
    if len(q) == 1:
        cnt += 1
    else:
        if cnt % 2 == 0:
            if int(q[1]) == 1:
                mae.append(q[2])
            else:
                usiro.append(q[2])
        else:
            if int(q[1]) == 1:
                usiro.append(q[2])
            else:
                mae.append(q[2])

mae = mae[::-1]
S = "".join(mae) + S + "".join(usiro)

if cnt % 2 == 1:
    S = S[::-1]
print(S)

Solution (Python):
from collections import deque

S = deque(input())
Q = int(input())
rev = False
for _ in range(Q):
    q = input().split()
    if q[0] == '1':
        rev = not rev
    else:
        if q[1] == '1':
            if rev:
                S.append(q[2])
            else:
                S.appendleft(q[2])
        else:
            if rev:
                S.appendleft(q[2])
            else:
                S.append(q[2])
if rev:
    S.reverse()

print(''.join(S))

Solution (Python):


import sys
import math
import collections
import itertools
from inspect import currentframe
INF = 10 ** 10
MOD = 10 ** 9 + 7
def input() : return sys.stdin.readline().strip()
def gcd(x, y) : return y if x % y == 0 else gcd(y, x % y)
def lcm(x, y) : return (x * y) // gcd(x, y)
def I() : return int(input())
def MI() : return map(int, input().split())
def LI() : return [int(x) for x in input().split()]
def RI(N) : return [int(input()) for _ in range(N)]
def LRI(N) : return [[int(x) for x in input().split()] for _ in range(N)]
def chkprint(*args) : names = {id(v):k for k,v in currentframe().f_back.f_locals.items()}; print(', '.join(names.get(id(arg),'???')+' = '+repr(arg) for arg in args))

S = input()
Q = I()

d = collections.deque()
d.append(S)

rev = 0
for i in range(Q):
    line = input().split()

    if int(line[0]) == 1:
        rev += 1
    else:
        F = int(line[1])
        C = line[2]

        if F == 1:
            if rev % 2 == 1:
                d.append(C)
            else:
                d.appendleft(C)
        else:
            if rev % 2 == 1:
                d.appendleft(C)
            else:
                d.append(C)

ans = "".join(d)

if rev % 2 == 1:
    ans = ans[::-1]

print(ans)

Solution (Python):

s = input()
pre = []
post = []
rev_it = 0
q = int(input())
for i in range(q):
    query = input()
    if query[0] == "1": rev_it += 1
    elif query[2] == "1":
        if rev_it % 2 == 0:
            pre.append(query[4])
        else:
            post.append(query[4])
    else:
        if rev_it % 2 == 0:
            post.append(query[4])
        else:
            pre.append(query[4])
ans = "".join(pre)[::-1]+s+"".join(post)
if rev_it % 2 == 0:
    print(ans)
else:
    print(ans[::-1])

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())
mae=''
ato=''
ten=0

for i in range(q):
  ss=input()
  if ss[0]=='1':
    ten+=1
    ten%=2
  else:
    t,f,c=ss.split()
    if f=='1':
      if ten==0:
        mae+=c
      else:
        ato+=c        
    else:
      if ten==0:
        ato+=c
      else:
        mae+=c
mae=mae[::-1]
s=mae+s+ato
if ten==1:
  s=s[::-1]
print(s)