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,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools
from collections import deque

sys.setrecursionlimit(10**7)
inf = 10**20
mod = 10**9 + 7

DR = [1, -1, 0, 0]
DC = [0, 0, 1, -1]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
     
def main():
    q = deque()
    s = S()
    for c in s:
        q.append(c)
    Q = I()
    backward = False
    for _ in range(Q):
        query = LS()
        if int(query[0]) == 1:
            backward = not backward
            continue
        _, f, c = query
        f = int(f)
        if backward and f == 1:
            
            q.append(c)
        if backward and f == 2:
            
            q.appendleft(c)
        if not backward and f == 1:
            
            q.appendleft(c)
        if not backward and f == 2:
            
            q.append(c)
    if not backward:
        ans = ''.join(q)
    else:
        q.reverse()
        ans = ''.join(q)
    print(ans)
main()

Solution (Python):
s = list(input())
q = int(input())
cnt = 0
pre = []
end = []
for i in range(q):
    t, *fc = input().split()
    if t == '1':
        cnt^=1
    else:
        f, c = fc
        f = int(f)%2
        if f^cnt:
            pre.append(c)
        else:
            end.append(c)
ans = list(reversed(pre))+s+end
print
if cnt:
    ans = reversed(ans)
print(*ans, sep = '')

Solution (Python):
from collections import deque

rev = 0
s = input()
queue = deque()
for c in s:
    queue.append(c)

q = int(input())
for i in range(q):
    cs = input().split()
    if cs[0] == '1':
        rev = 1 - rev
    else:
        if ((cs[1] == '1' and rev == 0) or (cs[1] == '2' and rev == 1)):
            queue.appendleft(cs[2])
        else:
            queue.append(cs[2])
r = ''.join(queue)
if rev == 1:
    print(r[::-1])
else:
    print(r)

Solution (Python):
from collections import deque

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

asc = True
for _ in range(q):
    qs = input()
    if qs[0]=="1":
        asc = not asc
        continue

    _,f,c = qs.split()
    if f=="1":
        if asc:
            s.appendleft(c)
        else:
            s.append(c)
    else:
        if asc:
            s.append(c)
        else:
            s.appendleft(c)

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

Solution (Python):
import sys, math
sys.setrecursionlimit(10**5 + 10)

def input():
    return sys.stdin.readline()[:-1]

def main():
    S = input()
    Q = int(input())
    f = 1
    m = ""
    u = ""
    for k in range(Q):
        q = list(input().split())
        if q[0] == "1":
            f *= -1
        else:
            if f == 1:
                if q[1] == "1":
                    m = q[2] + m
                else:
                    u = u + q[2]
            else:
                if q[1] == "1":
                    u = u + q[2]
                else:
                    m = q[2] + m
    if f == 1:
        print(m+S+u)
    else:
        print(u[::-1]+S[::-1]+m[::-1])

if __name__ == '__main__':
    main()

Solution (Python):
import sys,collections as cl,bisect as bs
sys.setrecursionlimit(100000)
input = sys.stdin.readline
mod = 10**9+7
Max = sys.maxsize
def l(): 
    return list(map(int,input().split()))
def m(): 
    return map(int,input().split())
def onem(): 
    return int(input())
def s(x): 
    a = []
    aa = x[0]
    su = 1
    for i in range(len(x)-1):
        if aa != x[i+1]:
            a.append([aa,su])
            aa = x[i+1]
            su = 1
        else:
            su += 1
    a.append([aa,su])
    return a
def jo(x): 
    return "".join(map(str,x))
def max2(x): 
    return max(map(max,x))
def In(x,a): 
    k = bs.bisect_left(a,x)
    if k != len(a) and a[k] ==  x:
        return True
    else:
        return False
"""
def nibu(x,n,r):
    ll = 0
    rr = r
    while True:
        mid = (ll+rr)//2

    if rr == mid:
        return ll
    if (ここに評価入れる):
        rr = mid
    else:
        ll = mid+1
"""

s = input().split()
q = onem()

de = cl.deque(s)

co = 0

for i in range(q):
    po = input().split()
    if len(po) == 1:
        co = co ^ 1
    else:
        f = int(po[1])
        if co == 0:
            if f == 1:
                de.appendleft(po[2])
            else:
                de.append(po[2])

        else:
            if f == 1:
                de.append(po[2])
            else:
                de.appendleft(po[2])

if co == 0:
    print(jo(de))
else:
    print(jo(de)[::-1])

Solution (Python):
S = input()
revFlg = False
Q = int(input())
first_append = ""
last_append = ""
for i in range(Q):
    query = input().split()
    if query[0] == "1":
        revFlg = not revFlg
    else:
        if query[1] == "1":
            if revFlg:
                last_append = last_append+query[2]
            else:
                first_append = query[2]+first_append

        else:
            if revFlg:
                first_append = query[2]+first_append
            else:
                last_append = last_append+query[2]

ans = first_append+S+last_append
if revFlg:
    print(ans[::-1])
else:
    print(ans)

Solution (Python):
S = input()

Q = int(input())

from collections import deque
ans = deque()

for s in S:
    ans.append(s)

rev = False
for _ in range(Q):
    q = input().split()
    if q[0] == "1":
        rev = not rev
    else:
        is_right = (q[1] =="2") ^ rev
        if is_right:
            for s in q[2]:
                ans.append(s)
        else:
            for s in q[2]:
                ans.appendleft(s)
if rev:
    ans.reverse()
print("".join(ans))

Solution (Python):
import math
from functools import reduce
from collections import deque
import sys
sys.setrecursionlimit(10**7)


def get_nums_l():
    return [ int(s) for s in input().split(" ")]


def get_nums_n(n):
    return [ int(input()) for _ in range(n)]


def get_all_int():
    return map(int, open(0).read().split())

def log(*args):
    print("DEBUG:", *args, file=sys.stderr)

s = input()
q = int(input())
qqq = list(map(lambda s: s.strip(), sys.stdin.readlines()))

que = deque()

que.append(s)

rev = False

for a in qqq:
    b = a.split()

    if len(b) == 1:
        rev = not rev
    else:
        front = b[1] == "1"
        t = b[2] if not rev else "".join(reversed(b[2]))

        if front == (not rev):
            que.appendleft(t)
        else:
            que.append(t)

if rev:
    print("".join(reversed("".join(que))))
else:
    print("".join(que))

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

s = str(input().rstrip())
ans = deque(list(s))

f = True
q = int(input())
for i in range(q):
    q = list(map(str, input().split()))
    if int(q[0]) == 1:
        if f:
            f = False
        else:
            f = True
    else:
        f_i = int(q[1])
        if f_i == 1:
            if f:
                ans.appendleft(q[2])
            else:
                ans.append(q[2])
        else:
            if f:
                ans.append(q[2])
            else:
                ans.appendleft(q[2])
ans = list(ans)
if f:
    print("".join(ans))
else:
    print("".join(ans[::-1]))

Solution (Python):
from collections import deque

def main():
    S = input()
    Q = int(input())
    cnt = 0
    dS = deque(S)
    for _ in range(Q):
        query = input()
        if query[0] == '1':
            cnt += 1
        else:
            A, F, C = query.split()
            if cnt % 2 == 0:
                if F == '1':
                    dS.appendleft(C)
                else:
                    dS.append(C)
            else:
                if F == '1':
                    dS.append(C)
                else:
                    dS.appendleft(C)

    if cnt % 2 == 0:
        print(*dS, sep ="")
    else:
        dS.reverse()
        print(*dS, sep ="")





if __name__ == "__main__":
    main()

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

a = []
b = []
d = 1

for q in query:
    if q[0] == '1':
        d *= -1
    else:
        if q[1] == '1':
            if d == 1:
                a.append(q[2])
            else:
                b.append(q[2])
        else:
            if d == 1:
                b.append(q[2])
            else:
                a.append(q[2])

if d == 1:
    ans = list(reversed(a)) + s + b
else:
    ans = list(reversed(b)) + list(reversed(s)) + a

print(''.join(ans))

Solution (Python):

import bisect
import heapq
import math
import random
import sys
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
from operator import add, mul, sub

sys.setrecursionlimit(100000)
input = sys.stdin.readline
INF = 2**62-1

def read_int():
    return int(input())


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


def read_float():
    return float(input())


def read_float_n():
    return list(map(float, input().split()))


def read_str():
    return input().strip()


def read_str_n():
    return list(map(str, input().split()))


def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


@mt
def slv(S, Q):
    ans = 0
    S = deque(S)
    r = False
    for q in Q:
        if q[0] =='1':
            r = not r
        else:
            f = q[1]
            c = q[2]
            if f == '1':
                if r:
                    S.append(c)
                else:
                    S.appendleft(c)
            else:
                if r:
                    S.appendleft(c)
                else:
                    S.append(c)
    S = list(S)
    if r:
        S.reverse()
    return ''.join(S)


def main():
    S = read_str()
    N = read_int()
    Q = [read_str_n() for _ in range(N)]
    print(slv(S, Q))


if __name__ == '__main__':
    main()

Solution (Python):

import sys
from collections import deque
sys.setrecursionlimit(10**9)
INF=10**18
MOD=10**9+7
def input(): return sys.stdin.readline().rstrip()

def main():
    S=input()
    Q=int(input())
    f=False
    ans=deque(list(S))
    for _ in range(Q):
        Query=list(input().split())
        if Query[0]=='1':
            f=not f
        else:
            F,C=Query[1],Query[2]
            if F=='1':
                if not f:
                    ans.appendleft(C)
                else:
                    ans.append(C)
            else:
                if not f:
                    ans.append(C)
                else:
                    ans.appendleft(C)
    if f:
        ans=list(reversed(ans))
    print(''.join(ans))
        
    

if __name__ == '__main__':
    main()

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

pre = ''
mat = ''
state = 1

for _ in range(q):
    query = input()
    if query[0] == '1':
        state = 3 - state
    else:
        _, f, c = query.split()
        if int(f) == state:
            pre += c
        else:
            mat += c

if state == 1:
    print(pre[::-1] + s + mat)
else:
    print(mat[::-1] + s[::-1] + pre)

Solution (Python):
from collections import deque
def main():
    S = input()
    d = deque(S)
    Q = int(input())
    direction = True
    for _ in range(Q):
        x = input()
        if x[0] == '1':
            direction = not direction
        else:
            a,f,c = x.split(' ')
            if (int(f) == 1 and direction) or (int(f) == 2 and not direction):
                d.appendleft(c)
            else:
                d.append(c)
    if direction:
        print(''.join(d))
    else:
        print(''.join(reversed(d)))
                
main()

Solution (Python):
from collections import deque

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

s = deque(s)

rev = 0
for _ in range(q):
    que = input()
    if que[0] == "1":
        rev ^= 1

    else:
        t, f, c = que.split()
        if rev:
            if f == "1":
                s.append(c)
            else:
                s.appendleft(c)

        else:
            if f == "1":
                s.appendleft(c)
            else:
                s.append(c)

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

Solution (Python):
from collections import deque


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

deq = deque(s)

flag = 1
for i in range(q):
    if query[i][0] == "1":
        flag ^= 1
    else:
        _, f, c = query[i]
        f = int(f) % 2
        if flag ^ f:
            deq.append(c)
        else:
            deq.appendleft(c)

ans = "".join(deq)
if flag:
    print(ans)
else:
    print(ans[::-1])

Solution (Python):
from collections import deque

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

D1 = deque()
D2 = deque()

rev = False

for i in range(Q):
    q = input().split()
    if int(q[0]) == 1:
        if rev == True:
            rev = False
        else:
            rev = True
    else:
        f = int(q[1])
        c = q[2]
        if f == 1:
            if not rev:
                D1.appendleft(c)
            else:
                D2.append(c)
        else:
            if not rev:
                D2.append(c)
            else:
                D1.appendleft(c)


ans = ''.join(D1)+S+''.join(D2)

if not rev:
    print(ans)

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

Solution (Python):
from collections import deque

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


rev = False
out = deque(S)
for i in range(Q):
    args = input().split()
    if args[0] == '1':
        rev = not rev
    else:
        t, f, c = args
        assert t == '2'
        if f == '1':
            if rev:
                out.append(c)
            else:
                out.appendleft(c)
        else:
            assert f == '2'
            if rev:
                out.appendleft(c)
            else:
                out.append(c)

if rev:
    out = reversed(out)
print(''.join(out))

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

from collections import deque


S = list(input().rstrip())
Q = int(input())
Query = [list(map(str, input().split())) for _ in range(Q)]

q = deque()
gyaku = False
for s in S:
    q.append(s)

for A in Query:
    if A[0] == "1":
        gyaku =  not gyaku
    else:
        if (gyaku and A[1] == "1") or (not gyaku and A[1] == "2"):
            q.append(A[2])
        else:
            q.appendleft(A[2])


S = []
for p in q:
    S.append(p)

if gyaku:
    S = S[::-1]

print("".join(S))

Solution (Python):
import sys
from collections import deque
s = list(input())
q = int(input())
h = deque()
h.extend(s)

rev = False
for _ in range(q):
    que = sys.stdin.readline().split()
    if que[0] == '1':
        if rev:
            rev = False
        else:
            rev = True
    elif que[1] == '1':
        if rev:
            h.append(que[2])
        else:
            h.appendleft(que[2])
    else:
        if rev:
            h.appendleft(que[2])
        else:
            h.append(que[2])
if rev:
    print(''.join(list(h)[::-1]))
else:
    print(''.join(list(h)))

Solution (Python):
from collections import deque

S = list(input())
Q = int(input())
d = deque(S)
reverse_flg = False

for i in range(Q):
    try:
        x, f, c = input().split()
        f = int(f)
        if f == 1 and not reverse_flg:
            d.appendleft(c)
        elif f == 1 and reverse_flg:
            d.append(c)
        elif f == 2 and not reverse_flg:
            d.append(c)
        else:
            d.appendleft(c)
    except:
        reverse_flg = not reverse_flg

if reverse_flg:
    d.reverse()

print("".join(d))

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

rev = False
head = []
tail = []
for _ in range(Q):
    q = input()
    if q[0] == '1':
        rev = not rev

    else:
        _, f, c = q.split()
        if (f == '1') ^ rev:
            head.append(c)
        else:
            tail.append(c)
ans = ''.join(reversed(head)) + S + ''.join(tail)
if rev:
    ans = ans[::-1]
print(ans)

Solution (Python):
from collections import deque

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

isNormal = 1

D = deque(list(S))

for i in range(Q):
    t = tuple(input().split())
    if t[0] == '1':
        isNormal ^= 1
    else:
        a = int(t[1]) + isNormal
        
        if a % 2:
            D.append(t[2])
        else:
            D.appendleft(t[2])

SS = ""
if isNormal % 2:
    SS = ''.join(list(D))
else:
    SS = ''.join(reversed(D))

print(SS)

Solution (Python):
from collections import deque

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

rev = False

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

if rev:
    s.reverse()

print(''.join(s))

Solution (Python):
from collections import deque
rev = False
S = deque(list(input().strip()))

Q = int(input())

for _ in range(Q):
    q = input().split()
    if len(q) == 3 and q[1] == '1':
        if not rev:
            S.appendleft(q[2])
        else:
            S.append(q[2])
    elif len(q) == 3 and q[1] == '2':
        if rev:
            S.appendleft(q[2])
        else:
            S.append(q[2])
    else:
        rev = not rev
if not rev:
    print(''.join(list(S)))
else:
    print(''.join(list(S))[::-1])

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

head = []
tail = []
flip = 0

for i in range(q):
    query = input().split()
    if query[0] == "1":
        flip ^= 1
    else:
        f = int(query[1]) - 1
        c = query[2]
        if (f ^ flip) == 0:
            head.append(c)
        else:
            tail.append(c)

if flip:
    head, tail = tail, head
    s = s[::-1]

head = "".join(head)
tail = "".join(tail)

print(head[::-1] + s + tail)

Solution (Python):
from collections import deque


reverse = 0
S = deque(list(input()))
q = int(input())

for _ in range(q):
    query = input()
    if query[0] == '1':
        reverse = 1 - reverse
        continue

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

if reverse:
    S = list(S)[::-1]

print(''.join(S))

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

Solution (Python):
def examA():
    S = SI()
    if S=="AAA" or S=="BBB":
        print("No")
    else:
        print("Yes")
    return

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

def examC():
    A, B = LI()
    ans = -1
    for i in range(1,20000):
        if i*8//100==A:
            if i*10//100==B:
                ans = i
                break
    print(ans)
    return

def examD():
    S = SI()
    Q = I()
    rev = 0
    L = ""; R = ""
    for _ in range(Q):
        q = LSI()
        if q[0]=="1":
            rev += 1
        else:
            c = int(q[1])
            if (c+rev)%2==1:
                L += q[2]
            else:
                R += q[2]
    ans = ""
    if rev%2==0:
        ans = L[::-1] + S + R
    else:
        ans = R[::-1] + S[::-1] + L
    print(ans)
    return

def examE():
    ans = 0
    print(ans)
    return

def examF():
    ans = 0
    print(ans)
    return

import sys,copy,bisect,itertools,heapq,math,random
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LSI(): return list(map(str,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def SI(): return sys.stdin.readline().strip()
global mod,mod2,inf,alphabet,_ep
mod = 10**9 + 7
mod2 = 998244353
inf = 10**18
_ep = 10**(-12)
alphabet = [chr(ord('a') + i) for i in range(26)]

sys.setrecursionlimit(10**6)

if __name__ == '__main__':
    examD()

"""

"""

Solution (Python):
S = input()
L = []
R = []
rev = 0
for _ in range(int(input())):
    A = input().split()
    if A[0] == "1":
        rev ^= 1
    else:
        if (int(A[1]) ^ rev) & 1:
            L.append(A[2])
        else:
            R.append(A[2])

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

Solution (Python):
s = input()
u, v = [], []
q = int(input())
cur = 0
for _ in range(q):
    t, *c = input().split()
    t = int(t)
    if t == 1:
        cur ^= 1
    else:
        f = (int(c[0]) - 1) ^ cur
        if f:
            v.append(c[1])
        else:
            u.append(c[1])
if cur:
    print(''.join(reversed(v)) + ''.join(reversed(s)) + ''.join(u))
else:
    print(''.join(reversed(u)) + s + ''.join(v))

Solution (Python):
from collections import deque
s=input()
de=deque(list(s))
rev=0
append,appendleft=de.append,de.appendleft
for _ in range(int(input())):
	t=input()
	if t[0]=="1":
		rev^=1
	else:
		_,f,c=t.split()
		if f=="1":
			if rev:
				append(c)
			else:
				appendleft(c)
		else:
			if rev:
				appendleft(c)
			else:
				append(c)
li=list(de)
if rev:
	li=li[::-1]
print("".join(li))

Solution (Python):
import sys
from collections import deque as dq
input = sys.stdin.readline
S = dq(list(input())[: -1])
Q = int(input())
rev = 0
ap = [dq([]), dq([])]
for _ in range(Q):
  t = input().split()
  if int(t[0]) == 1: rev = (rev + 1) % 2
  else:
    f, c = int(t[1]) - 1, t[2]
    if (f + rev) % 2: S.append(c)
    else: S.appendleft(c)
if rev: S.reverse()
print("".join(S))

Solution (Python):
from collections import deque

S = deque(input())
Q = int(input())
d = 0
for a in range(Q):
    
    line = input()
    if line[0] == "1":
        d = 1 - d
    else:
        line = line.split()
        if line[1] == "1":
            if d == 1:
                S.append(line[2])
            else:
                S.appendleft(line[2])
        else:
            if d == 1:
                S.appendleft(line[2])
            else:
                S.append(line[2])
if d == 0:
    print("".join(S))
else:
    print("".join(S)[::-1])

Solution (Python):
raw = input()
l = len(raw)
from collections import deque
s = deque()
for i in range(l):
  s.append(raw[i])

n = int(input())
cnt = 0
for _ in range(n):
  tmp = input().split()
  if tmp[0] == '1':
    cnt = 1 - cnt
  else:
    t = int(tmp[1]) - 1
    t ^= cnt
    if t == 1:
      s.append(tmp[2])
    else:
      s.appendleft(tmp[2])


ans = ''
if cnt == 1:
  s.reverse()
for x in s:
  ans += x
print(ans)

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

    S = input().rstrip('\n')
    ans = deque()
    ans.append(S)
    q = int(input())
    rev = 1
    for _ in range(q):
        query = input().rstrip('\n')
        if query == '1':
            rev *= -1
        else:
            _, f, c = query.split()
            if rev == 1:
                if f == '1':
                    ans.appendleft(c)
                else:
                    ans.append(c)
            else:
                if f == '1':
                    ans.append(c)
                else:
                    ans.appendleft(c)
    ans = ''.join(ans)
    if rev == -1:
        ans = ans[::-1]
    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
from collections import deque

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

S = deque(S)

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

Solution (Python):
s = input()
from collections import deque
d = deque()
for i in s:
    d.append(i)
mode = 0
Q = int(input())
for _ in range(Q):
    q = input()
    if q[0]=='1':
        mode+=1
        mode%=2
    else:
        t,f,c = q.split()
        f = int(f)
        if (f+mode)%2==1:
            d.appendleft(c)
        else:
            d.append(c)
if mode==1:
    while(len(d)):
        print(d.pop(),end='')
else:
    while(len(d)):
        print(d.popleft(),end='')

Solution (Python):
def main():
    from collections import deque
    s = deque(list(input()))
    q = int(input())
    query = [input().split() for _ in [0]*q]

    cnt = 0
    for i in range(q):
        if query[i][0] == "1":
            cnt += 1
        else:
            if (cnt+int(query[i][1])) % 2 == 1:
                s.appendleft(query[i][2])
            else:
                s.append(query[i][2])
    if cnt % 2 == 0:
        print("".join(s))
    else:
        print("".join(list(reversed(s))))


main()

Solution (Python):
S = input()
Q = int(input())
arr1 = []
arr2 = []
count = 1
for _ in range(Q):
    q = input()
    if q == "1":
        count *= -1
    else:
        _, f, c = map(str, q.split())
        if f == "1":
            arr1.append(c) if count == 1 else arr2.append(c)
        else:
            arr2.append(c) if count == 1 else arr1.append(c)

if count == 1:
    ans = "".join(arr1[::-1]) + S + "".join(arr2)
else:
    ans = "".join(arr2[::-1]) + S[::-1] + "".join(arr1)
print(ans)

Solution (Python):
import sys
from collections import deque
s = input()
q = int(input())
is_reverse = False
res = deque(list(s))
for i in range(q):
    query = [i for i in sys.stdin.readline().split()]
    if query[0] == "1":
        is_reverse = False if is_reverse else True
    else:
        if query[1] == "1":
            if is_reverse:
                res.append(query[2])
            else:
                res.appendleft(query[2])
        else:
            if is_reverse:
                res.appendleft(query[2])
            else:
                res.append(query[2])
if is_reverse:
    res.reverse()
    print("".join(res))
else:
    print("".join(res))

Solution (Python):
s = list(input())
n = int(input())
rev = False
pre = []
suf = []
for _ in range(n):
    q = input()
    if q == '1':
        rev = not rev
    else:
        t,f,c = q.split()
        if f == '1':
            if rev:
                suf += [c]
            else:
                pre += [c]
        else:
            if rev:
                pre += [c]
            else:
                suf += [c]

if rev:
    print(''.join(reversed(suf)) + ''.join(reversed(s)) + ''.join(pre))
else:
    print(''.join(reversed(pre)) + ''.join(s) + ''.join(suf))

Solution (Python):
s = input()
q = int(input())
h, t = [], []
p = 0
for i in range(q):
	c = input().split()
	if c[0] == '1':
		h, t = t, h
		p = 1 - p
	else:
		if c[1] == '1':
			h.append(c[2])
		else:
			t.append(c[2])
if p == 1:
	s = s[::-1]
print("".join(h[::-1]) + s + "".join(t))

Solution (Python):
from collections import deque
sl = list(input())
q = int(input())
dl = deque(sl)
r = 0
for _ in range(q):
    query = list(map(str, input().split()))
    if len(query) == 1:
        r += 1
    else:
        if r % 2 == 0:
            if query[1] == "1":
                dl.appendleft(query[2])
            else:
                dl.append(query[2])
        else:
            if query[1] == "1":
                dl.append(query[2])
            else:
                dl.appendleft(query[2])
if r % 2 == 1:
    dl.reverse()
print("".join(dl))

Solution (Python):
S = input()
X = [None]*200001 + list(S) + [None]*200001
ifront = 200000
iback = 200001 + len(S)
rev = False

Q = int(input())
for _ in range(Q):
    query = input().split()
    if len(query)==1: rev = not rev
    else:
        F = int(query[1])
        C = query[2]

        if (not rev and F==1) or (rev and F==2):
            X[ifront] = C
            ifront -= 1
        else:
            X[iback] = C
            iback += 1

ans = ''.join(X[ifront+1:iback])
print(ans if not rev else ans[::-1])

Solution (Python):
from collections import deque

S = list(input())
Q = int(input())
s = deque(S)
h = 1


for i in range(Q):
    q = list(map(str, input().split()))
    if q[0] == "1":
        h *= -1
        continue
    if q[1] == "1":
        if h == 1:
            s.appendleft(q[2])
        else:
            s.append(q[2])
    else:
        if h == 1:
            s.append(q[2])
        else:
            s.appendleft(q[2])

print("".join(map(str,list(s)[::h])))

Solution (Python):
from collections import *
from itertools import *
from bisect import *
from heapq import *
import copy
import math
from fractions import gcd

mod=10**9+7

S=input()
Q=int(input())
Query=[list(input().split()) for i in range(Q)]


lst=deque(S)

flag=0
for query in Query:
    if len(query)==1:
        flag+=1
        flag%=2
    else:
        _,F,C=query
        if (int(F)+flag)%2:
            lst.appendleft(C)
        else:
            lst.append(C)

if flag:
    lst.reverse()
print("".join(lst))