Problem:
 Score : 400 points    Problem Statement Takahashi has a string S consisting of lowercase English letters.  Starting with this string, he will produce a new one in the procedure given as follows.  The procedure consists of Q operations. In Operation i  (1 \leq i \leq Q) , an integer T_i is provided, which means the following:    If T_i = 1 : reverse the string S .    If T_i = 2 : An integer F_i and a lowercase English letter C_i are additionally provided.   If F_i = 1 : Add C_i to the beginning of the string S .  If F_i = 2 : Add C_i to the end of the string S .     Help Takahashi by finding the final string that results from the procedure.      Constraints  1 \leq |S| \leq 10^5  S consists of lowercase English letters.  1 \leq Q \leq 2 \times 10^5  T_i = 1 or 2 .  F_i = 1 or 2 , if provided.  C_i is a lowercase English letter, if provided.         Input Input is given from Standard Input in the following format:  S  Q  Query_1  :  Query_Q   In the 3 -rd through the (Q+2) -th lines, Query_i is one of the following:  1   which means T_i = 1 , and:  2  F_i  C_i   which means T_i = 2 .      Output Print the resulting string.        Sample Input 1 a
4
2 1 p
1
2 2 c
1      Sample Output 1 cpa  There will be Q = 4 operations. Initially, S is a .    Operation 1 : Add p at the beginning of S . S becomes pa .    Operation 2 : Reverse S . S becomes ap .    Operation 3 : Add c at the end of S . S becomes apc .    Operation 4 : Reverse S . S becomes cpa .    Thus, the resulting string is cpa .       Sample Input 2 a
6
2 2 a
2 1 b
1
2 2 c
1
1      Sample Output 2 aabc  There will be Q = 6 operations. Initially, S is a .    Operation 1 : S becomes aa .    Operation 2 : S becomes baa .    Operation 3 : S becomes aab .    Operation 4 : S becomes aabc .    Operation 5 : S becomes cbaa .    Operation 6 : S becomes aabc .    Thus, the resulting string is aabc .       Sample Input 3 y
1
2 1 x      Sample Output 3 xy

Solution (Python):

s = input()

q = int(input())
x = []
y = []

flag = 0

for i in range(q):
    li = list(input().split())
    if flag%2 == 0:
        if li[0] == "2":
            if li[1] =="1":
                x.append(li[2])
            else:
                y.append(li[2])
        else:
            flag += 1

    else:
        if li[0] == "2":
            if li[1] =="1":
                y.append(li[2])
            else:
                x.append(li[2])
        else:
            flag += 1

if flag%2 == 0:
    x = x[::-1]
    print("".join(x)+s+"".join(y))

else:
    s = s[::-1]
    y = y[::-1]
    print("".join(y)+s+"".join(x))

Solution (Python):
S,Q=input(),int(input())
l,r='',''
reverse=0
for i in range(Q):
    T=input().split()
    if T[0]=='1':reverse=0 if reverse else 1
    else:
        if T[1]=='1':
            if reverse:r=r+T[2]
            else:l=T[2]+l
        else:
            if reverse:l=T[2]+l
            else:r=r+T[2]
if reverse:
    s=list(l+S+r);s.reverse()
    print(''.join(s))
else:
    print(l+S+r)

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

flag = 0
for _ in range(q):
  query = list(map(str, input().split()))
  if query[0] == "1": flag = 1 - flag
  else:
    if query[1] == "1":
      if flag == 0: s.appendleft(query[2])
      else: s.append(query[2])
    else:
      if flag == 0: s.append(query[2])
      else: s.appendleft(query[2])

if flag == 1: s.reverse()
print(*s, sep="")

Solution (Python):
S = list(input())
N = int(input())
normalFlag = True
front = []
back = []
for _ in range(N):
    q = list(map(str,input().split()))
    if q[0] == '1':
        normalFlag = not normalFlag
    else:
        if q[1] == '1':
            if normalFlag:
                front.append(q[2])
            else:
                back.append(q[2])
        else:
            if normalFlag:
                back.append(q[2])
            else:
                front.append(q[2])

if normalFlag:
    front.reverse()
    ans = front+S+back
else:
    back.reverse()
    S.reverse()
    ans = back+S+front
print(''.join(map(str,ans)))

Solution (Python):
from collections import deque
s = input()
q = int(input())
S = [c for c in s]
deque = deque(S)
reversed = 0
for i in range(q):
    line = input()
    if line[0] == '1':
        reversed = reversed ^ 1
    else:
        op, f, c = line.split()
        f = int(f)
        if reversed == 0:
            if f == 1:
                deque.appendleft(c)
            else:
                deque.append(c)
        else:
            if f == 1:
                deque.append(c)
            else:
                deque.appendleft(c)
if reversed:
    deque.reverse()
result = "".join(deque)
print(result)

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

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

c = 1
for _ in range(q):
    t = list(input().split())
    if t[0] == '1':
        c *= -1
    else:
        if t[1] == '1':
            if c == 1:
                d.appendleft(t[2])
            else:
                d.append(t[2])
        else:
            if c == 1:
                d.append(t[2])
            else:
                d.appendleft(t[2])

print(''.join(list(d)[::c]))

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

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

c = 1
for _ in range(q):
    t = list(input().split())
    if t[0] == '1':
        c *= -1
    else:
        if t[1] == '1':
            if c == 1:
                d.appendleft(t[2])
            else:
                d.append(t[2])
        else:
            if c == 1:
                d.append(t[2])
            else:
                d.appendleft(t[2])

print(''.join(list(d)[::c]))

Solution (Python):
def main():
    S = input()
    Q = int(input())
    order = [list(input().split()) for _ in range(Q)]
    left_flag = 0
    right_flag = 0
    cnt = 0
    for i in range(Q):
        if order[i][0] == '1':
            cnt += 1
        else:
            if cnt%2 == 0:
                if order[i][1] == '1':
                    if left_flag == 1:
                        left = order[i][2] + left
                    else:
                        left = order[i][2]
                        left_flag = 1
                else:
                    if right_flag == 1:
                        right = right + order[i][2]
                    else:
                        right = order[i][2]
                        right_flag = 1
            else:
                if order[i][1] == '2':
                    if left_flag == 1:
                        left = order[i][2] + left
                    else:
                        left = order[i][2]
                        left_flag = 1
                else:
                    if right_flag == 1:
                        right = right + order[i][2]
                    else:
                        right = order[i][2]
                        right_flag = 1
    if left_flag == 1 and right_flag == 1:
        S = left + S + right
    elif left_flag == 1 and right_flag == 0:
        S = left + S
    elif left_flag == 0 and right_flag == 1:
        S = S + right
    else:
        S = S

    if cnt%2 == 0:
        return(S)
    else:
        S2 = S[-1]
        for i in range(len(S)-2,-1,-1):
            S2 = S2 + S[i]
        return S2

print(main())

Solution (Python):
import sys
import math
import itertools
import collections
from collections import deque
from collections import defaultdict

sys.setrecursionlimit(1000000)
MOD = 10 ** 9 + 7
MOD2 = 998244353
INF = float('inf')
input = lambda: sys.stdin.readline().strip()

NI = lambda: int(input())
NMI = lambda: map(int, input().split())
NLI = lambda: list(NMI())
SI = lambda: input()

def main():
    S = SI()
    Q = NI()
    
    que = deque(list(S))
    
    reverse = False
    
    for q in range(Q):
        query = input()

        if query == "1":
            if reverse == False:
                reverse = True
            else:
                reverse = False
        else:
            x,y,z = map(str,query.split())
            if reverse == False:
                if y == "1":
                    que.appendleft(z)
                else:
                    que.append(z)
            else:
                if y == "1":
                    que.append(z)
                else:
                    que.appendleft(z)

    if reverse==True:
        print("".join(reversed(list(que))))
    else:
        print("".join(list(que)))



if __name__ == '__main__':
    main()

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

flg = True
for q in Q:
    if q[0] == "1":
        flg = not flg
    else:
        F = int(q[1])
        if not flg:
            F = 3 - F
        if F == 1:
            S.appendleft(q[2])
        if F == 2:
            S.append(q[2])

if not flg:
    S.reverse()
print("".join(S))

Solution (Python):
def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**7)
    from collections import Counter, deque
    from collections import defaultdict
    from itertools import combinations, permutations, accumulate, groupby, product
    from bisect import bisect_left,bisect_right
    from heapq import heapify, heappop, heappush
    from math import floor, ceil,pi,factorial
    from operator import itemgetter
    def I(): return int(input())
    def MI(): return map(int, input().split())
    def LI(): return list(map(int, input().split()))
    def LI2(): return [int(input()) for i in range(n)]
    def MXI(): return [[LI()]for i in range(n)]
    def SI(): return input().rstrip()
    def printns(x): print('\n'.join(x))
    def printni(x): print('\n'.join(list(map(str,x))))
    inf = 10**17
    mod = 10**9 + 7

    s=SI()
    turn=0
    q=I()
    ortop=[]
    orbot=[]
    for i in range(q):
        query=list(input().rstrip().split(" "))
        if query[0]=="1":
            turn=(turn+1)%2
        else:
            x=int(query[1])
            if (x+turn)%2==1:
                ortop.append(query[2])
            else:
                orbot.append(query[2])
    if turn%2==0:
        s1="".join(list(reversed(ortop)))
        s2="".join(orbot)
        print(s1+s+s2)
    else:
        s1="".join(list(reversed(orbot)))
        s2="".join(ortop)
        s="".join(list(reversed(s)))
        
        print(s1+s+s2)
        
        
            
            
            
    

        
    
            
        

        
if __name__=="__main__":
    main()

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

S=pin()[:-1]
Q=int(pin())
now=1
ans1=""
ans2=""
for i in range(Q):
  D=pin().split()
  T=0
  F=0
  C=""
  if len(D)==1:
    T=int(D[0])
  else:
    T=int(D[0])
    F=int(D[1])
    C=D[2]
  if T==1:
    now*=-1
  else:
    if F==1:
      if now==1:
        ans1=C+ans1
      else:
        ans2=ans2+C
    else:
      if now==1:
        ans2=ans2+C
      else:
        ans1=C+ans1
if now==1:
  print(ans1+S+ans2)
else:
  print(ans2[::-1]+S[::-1]+ans1[::-1])

Solution (Python):
s=input()
q=int(input())
que=[list(map(str,input().split())) for i in range(q)]
count=0
for i in que:
  if i[0]=="1":
    count+=1
ans=[[],[]]
ak=count
for i in que:
  if i[0]=="1":
    count-=1
  else:
    if i[1]=="1":
      ans[count%2].append(i[2])
    else:
      ans[(count+1)%2].append(i[2])
if ak%2==0:
  print("".join(reversed(ans[0]))+s+"".join(ans[1]))
else:
  print("".join(reversed(ans[0]))+"".join(reversed(list(s)))+"".join(ans[1]))

Solution (Python):
from collections import deque
S = str(input());s = []
for i in range(len(S)):
  s.append(S[i])
s = deque(s)
Q = int(input())
eo = 0
for query in range(Q):
  temp = list(map(str,input().split()))
  if temp[0] == "1":
    eo += 1 
  else:
    if temp[1] == "1":
      if eo%2 == 0:
        s.appendleft(temp[2])
      else:
        s.append(temp[2])
    else:
      if eo%2 == 0:
        s.append(temp[2])
      else:
        s.appendleft(temp[2])

s = list(s)
if eo%2 == 0:
  ans = "".join(s)
else:
  s = s[::-1]
  ans = "".join(s)
print(ans)

Solution (Python):
S = input().strip()
L = []
R = []
Q = int(input())
flag = 0
for _ in range(Q):
    q = list(input().split())
    if len(q)==1:
        flag = 1-flag
    else:
        F = q[1]
        C = q[2]
        if flag==0 and F=="1":
            L.append(C)
        elif flag==0 and F=="2":
            R.append(C)
        elif flag==1 and F=="1":
            R.append(C)
        elif flag==1 and F=="2":
            L.append(C)
L = "".join(L)
R = "".join(R)
if flag==1:
    S = S[::-1]
    R = R[::-1]
    S = R+S+L
else:
    L = L[::-1]
    S = L+S+R
print(S)

Solution (Python):
from collections import deque

s = list(input())
Q = int(input())
r = False

q = deque(s)
for _ in range(Q):
    query = list(map(str, input().split()))
    if query[0] == '1':
        r = not r
    else:
        f,c = query[1],query[2]
        if f == '1':
            if not r:
                q.appendleft(c)
            else:
                q.append(c)
        else:
            if not r:
                q.append(c)
            else:
                q.appendleft(c)

ans = list(q)
if r: ans = ans[::-1]
print(''.join(ans))

Solution (Python):
from collections import deque
import sys


def input():
    return sys.stdin.readline().strip()


q = deque(input())
Q = int(input())
flipped = False
for _ in range(Q):
    q_t, *q_b = input().split()
    if q_t == "1":
        flipped = not flipped
    else:
        F, C = q_b
        if (F == "2" and not flipped) or (F == "1" and flipped):
            q.append(C)
        else:
            q.appendleft(C)
q = list(q)
if flipped:
    print("".join(q[::-1]))
else:
    print("".join(q))

Solution (Python):
from collections import deque
import sys
read = sys.stdin.read
readlines = sys.stdin.readlines
input = sys.stdin.readline
S = list(input().rstrip())
S = deque(S)
input()
rev = 0
for line in readlines():
    q, *query, = line.split()
    if q == '1':
        rev ^= 1
    else:
        f, c = query
        if f == '1':
            if not rev:
                S.appendleft(c)
            else:
                S.append(c)
        else:
            if not rev:
                S.append(c)
            else:
                S.appendleft(c)

print(''.join(S) if not rev else ''.join(S)[::-1])

Solution (Python):
s=list(input())
q=int(input())
rv=False
right=[]
left=[]
for _ in range(q):
  query=input()
  if query[0]=='1':
    rv=not rv
  else:
    _,f,c=query.split()
    if f=='1' and rv==False:
      left.append(c)
    elif f=='1' and rv==True:
      right.append(c)
    elif f=='2' and rv==False:
      right.append(c)
    elif f=='2' and rv==True:
      left.append(c)
if not rv:
  left.reverse()
  print(''.join(left)+''.join(s)+''.join(right))
else:
  right.reverse()
  s.reverse()
  print(''.join(right)+''.join(s)+''.join(left))

Solution (Python):
from collections import deque
s=deque(list(input()))
n=int(input())
Q=[]
for i in range(n) :
    Q.append(list(input().split()))
 
count=0
for q in Q :
    if q[0]=="1" :
        count+=1
    elif q[0]=="2" and count%2==0 :
        if q[1]=="1" :
            s.appendleft(q[2])
        else :
            s.append(q[2])
    else :
        if q[1]=="1" :
            s.append(q[2])
        else :
            s.appendleft(q[2])
 
ans="".join(s)
if count%2==0 :
    print(ans)
else :
    print(ans[::-1])

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


for i in range(n) :
    x=input()
    if x[0]=="1" :
        count+=1
        count%=2

    elif x[2]=="1" :
        if count%2==0 :
            s.appendleft(x[4])
        else :
            s.append(x[4])
    else :
        if count%2==0 :
            s.append(x[4])
        else :
            s.appendleft(x[4])


if count%2==1 :
    s.reverse()

print("".join(s))

Solution (Python):
from collections import deque

z=list(input())
Q=deque()
for i in z:
  Q.append(i)
q=int(input())

cnt=0

for i in range(q):
  x=input()
  if x[0]=='1':
    cnt+=1
    cnt%=2
    
  elif x[0]=='2':
    if x[2]=='1':
        if cnt%2==0:
            Q.appendleft(x[4])
        else:
            Q.append(x[4])

    else:
        if cnt%2==0:
            Q.append(x[4])
        else:
            Q.appendleft(x[4])
      
if cnt%2==1:
  Q.reverse()


print(''.join(Q))

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

switch = 1
top = []
bottom = []

for i in range(Q):
  if query[i][0] == "1":
    switch *= -1

  else:
    f = query[i][1]
    c = query[i][2]
    if switch == 1:
      if f == "1":
        top.append(c)
      else:
        bottom.append(c)
    else:
      if f == "1":
        bottom.append(c)
      else:
        top.append(c)

if switch == 1:
  ans = top[::-1] + list(S) + bottom
else:
  ans = bottom[::-1] + list(S)[::-1] + top
  
print("".join(ans))

Solution (Python):
from collections import deque
s=deque(list(input()))
q=int(input())
count=0
for i in range(q):
    query=input()
    if query=="1":
        count+=1
    elif query[2]=="1":
        if count%2==0:
            s.appendleft(query[-1])
        else:
            s.append(query[-1])
    else:
        if count%2==0:
            s.append(query[-1])
        else:
            s.appendleft(query[-1])
if count%2==1:
    s.reverse()
print(*s,sep="")

Solution (Python):
from collections import deque


Q=deque(list(input()))

q=int(input())

cnt=0

for i in range(q):
  x=input()
  if x[0]=='1':
    cnt+=1
    cnt%=2
    
  elif x[0]=='2':
    if x[2]=='1':
        if cnt%2==0:
            Q.appendleft(x[4])
        else:
            Q.append(x[4])

    else:
        if cnt%2==0:
            Q.append(x[4])
        else:
            Q.appendleft(x[4])
      
if cnt%2==1:
  Q.reverse()


print(''.join(Q))

Solution (Python):
import sys
def I(): return int(sys.stdin.readline().rstrip())
def LS(): return list(sys.stdin.readline().rstrip().split())  
def LS2(): return list(sys.stdin.readline().rstrip())  


S = LS2()
r = 0
left,right = [],[]

Q = I()
for i in range(Q):
    A = LS()
    if A[0] == '1':
        r = 1-r
    else:
        f,c = A[1],A[2]
        if (r == 0 and f == '1') or (r == 1 and f == '2'):
            left.append(c)
        else:
            right.append(c)

if r == 0:
    left.reverse()
    print(''.join(left+S+right))
else:
    S.reverse()
    right.reverse()
    print(''.join(right+S+left))

Solution (Python):
import sys
from collections import deque


def LS():
    return list(input().split())


S = deque(input())
Q = int(input())
rev = 0
for i in range(Q):
    A = LS()
    if A[0] == "1":
        rev += 1
        rev %= 2
    else:
        if A[1] == "1":
            if rev == 0:
                S.appendleft(A[2])
            else:
                S.append(A[2])
        else:
            if rev == 0:
                S.append(A[2])
            else:
                S.appendleft(A[2])
if rev == 0:
    print(''.join(S))
else:
    S.reverse()
    print(''.join(S))

Solution (Python):
from collections import deque
S = input()
ds = deque(S)
Q = int(input())
query = []
for i in range(Q):
  tq = list(input().split())
  if (len(tq) == 1):
    query.append(1)
  if (len(tq) == 3):
    query.append([int(tq[1]),tq[2]])
eo = 1
for q in query:
  if q == 1:
    eo = eo*(-1)
  else:
    eoq = eo*q[0]
    if eoq == 1 or eoq == -2:
      ds.extendleft(q[1])
    else:
      ds.extend(q[1])
jds = "".join(ds)
if eo == -1:
  jds = jds[::-1]
print(jds)

Solution (Python):
S = input()
T = ""
B = ""
f = 1
for _ in range(int(input())):
    L = list(input().split())
    if L[0] == "1":
        f *= -1
    else:
        if f == 1:
            if L[1] == "1":
                T += L[2]
            else:
                B += L[2]
        else:
            if L[1] == "1":
                B += L[2]
            else:
                T += L[2]

if f == 1:
    print(T[::-1]+S+B)
else:
    print(B[::-1]+S[::-1]+T)

Solution (Python):
from collections import deque
from heapq import heapify,heappop,heappush,heappushpop
from copy import copy,deepcopy
from itertools import product,permutations,combinations,combinations_with_replacement
from collections import defaultdict,Counter
from bisect import bisect_left,bisect_right
from math import gcd,ceil,floor,factorial

from functools import reduce
from pprint import pprint
from statistics import mean,median,mode

INF = float("inf")

def mycol(data,col):
    return [ row[col] for row in data ]

def mysort(data,col,reverse_flag):
    data.sort(key=lambda x:x[col],reverse=reverse_flag)
    return data

def mymax(data):
    M = -1*float("inf")
    for i in range(len(data)):
        m = max(data[i])
        M = max(M,m)
    return M

def mymin(data):
    m = float("inf")
    for i in range(len(data)):
        M = min(data[i])
        m = min(m,M)
    return m

def mycount(ls,x):
    
    l = bisect_left(ls,x)
    r = bisect_right(ls,x)
    return (r-l)

def myoutput(ls,space=True):
    if space:
        if len(ls)==0:
            print(" ")
        elif type(ls[0])==str:
            print(" ".join(ls))
        elif type(ls[0])==int:
            print(" ".join(map(str,ls)))
        else:
            print("Output Error")
    else:
        if len(ls)==0:
            print("")
        elif type(ls[0])==str:
            print("".join(ls))
        elif type(ls[0])==int:
            print("".join(map(str,ls)))
        else:
            print("Output Error")

def I():
    return int(input())

def MI():
    return map(int,input().split())

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

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

def LI(n):
    return [ list(map(int,input().split())) for _ in range(n) ]

def S():
    return input()

def MS():
    return input().split()

def RS():
    return list(input())

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

def LS(n):
    return [ list(input()) for _ in range(n) ]







s = S()
q = I()





flag = True
r = []
l = []
for i in range(q):
    d = input().split()
    if len(d)==1:
        flag = not flag
    elif d[1]=='1':
        c = d[2]
        if flag:
            l.append(c)
        else:
            r.append(c)
    elif d[1]=='2':
        c = d[2]
        if flag:
            r.append(c)
        else:
            l.append(c)


l = "".join(l[::-1])
r = "".join(r)
ans = l+s+r
if flag:
    print(ans)
else:
    print(ans[::-1])

Solution (Python):



from fractions import gcd
from itertools import combinations,permutations,accumulate, product 

from collections import deque,defaultdict,Counter
import decimal
import re
import math
import bisect














import sys
sys.setrecursionlimit(10000000)
mod = 10**9 + 7


from sys import stdin
readline = stdin.readline
def readInts():
  return list(map(int,readline().split()))
def readTuples():
    return tuple(map(int,readline().split()))
def I():
  return int(readline())
s = input()
q = I()
straight = True
ans = deque(s)
for i in range(q):
    A = input().split()
    if A[0] == '1':
        straight = not straight
    else:
        if straight:
            if A[1] == '1':
                ans.appendleft(A[2])
            else:
                ans.append(A[2])
        else:
            if A[1] == '1':
                ans.append(A[2])
            else:
                ans.appendleft(A[2])
if straight:
    print(*ans,sep='')
else:
    print(*reversed(ans),sep='')

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

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

rev = 0
for _ in range(q):
    a = tuple(read().split())
    if a[0] == '1':
        rev ^= 1
    else:
        dir = rev ^ int(a[1]) - 1
        if dir == 0:
            s.appendleft(a[2])
        else:
            s.append(a[2])

if rev:
    print(''.join(s)[::-1])
else:
    print(''.join(s))

Solution (Python):
S = input().strip()
Q = int(input())
ind = 1
x = []
y = []
for i in range(Q):
    q = list(input().split())
    if len(q)==1:
        ind = ind*(-1)
    else:
        if q[1]=="1":
            if ind>0:
                x.append(q[2])
            else:
                y.append(q[2])
        else:
            if ind>0:
                y.append(q[2])
            else:
                x.append(q[2])
if ind>0:
    x = x[::-1]
    S = "".join(x)+S+"".join(y)
else:
    S = S[::-1]
    y = y[::-1]
    S = "".join(y)+S+"".join(x)
print(S)

Solution (Python):
import sys

sys.setrecursionlimit(10**7)
def I(): return int(sys.stdin.readline().rstrip())
def MI(): return map(int,sys.stdin.readline().rstrip().split())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))  
def LI2(): return list(map(int,sys.stdin.readline().rstrip()))  
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())  
def LS2(): return list(sys.stdin.readline().rstrip())  


S = LS2()
r = 0
left,right = [],[]

Q = I()
for i in range(Q):
    A = LS()
    if A[0] == '1':
        r = 1-r
    else:
        f,c = A[1],A[2]
        if (r == 0 and f == '1') or (r == 1 and f == '2'):
            left.append(c)
        else:
            right.append(c)

if r == 0:
    left.reverse()
    print(''.join(left+S+right))
else:
    S.reverse()
    right.reverse()
    print(''.join(right+S+left))

Solution (Python):
import sys
from collections import deque


def input():
    return sys.stdin.readline().strip()


S = input()
Q = int(input())
q = deque(S)
flipped = False
for _ in range(Q):
    q_t, *q_d = input().split()
    if q_t == "1":
        flipped = not flipped
    else:
        F, C = q_d
        if (not flipped and F == "2") or (flipped and F == "1"):
            q.append(C)
        else:
            q.appendleft(C)

ans = list(q)
if flipped:
    print("".join(ans[::-1]))
else:
    print("".join(ans))

Solution (Python):
s = list(str(input()))
q = int(input())
flag = 1
from collections import deque
s = deque(s)
for i in range(q):
    query = list(map(str, input().split()))
    if len(query) == 1:
        flag = 1-flag
    else:
        t, f, c = query
        if f == '1':
            if flag:
                s.appendleft(c)
            else:
                s.append(c)
        else:
            if flag:
                s.append(c)
            else:
                s.appendleft(c)

s = list(s)
if not flag:
    s.reverse()
print(''.join(s))

Solution (Python):
from collections import deque
S = deque(input())
Q = int(input())
q = [input().split() for _ in range(Q)]
flag = True
for x in q:
    if x[0] == "1":
        if flag: flag = False
        else: flag = True
    else:
        if x[1] == "1":
            if flag: S.appendleft(x[2])
            else: S.append(x[2])
        else:
            if flag: S.append(x[2])
            else: S.appendleft(x[2])
s = list(S)
if not flag: s = s[::-1]
print("".join(s))

Solution (Python):
import bisect, collections, copy, heapq, itertools, math, string
import sys
def I(): return int(sys.stdin.readline().rstrip())
def MI(): return map(int, sys.stdin.readline().rstrip().split())
def LI(): return list(map(int, sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())

from collections import deque
def main():
    S_ = S()
    S_que = deque(S_)
    Q = I()
    cnt = 0
    for i in range(Q):
        q = LS()
        if int(q[0]) == 1:
            cnt += 1
        elif int(q[0]) == 2:
            if int(q[1]) == 1:
                if cnt % 2 == 0:
                    S_que.appendleft(q[2])
                else:
                    S_que.append(q[2])
            else:
                if cnt % 2 == 1:
                    S_que.appendleft(q[2])
                else:
                    S_que.append(q[2])
    if cnt % 2 == 1:
        S_que.reverse()
    print(''.join(map(str, S_que)))

if __name__ == "__main__":
    main()

Solution (Python):

import sys

input = sys.stdin.readline


def ST():
    return input().rstrip()


def I():
    return int(input())


def MI():
    return map(int, input().split())


def LI():
    return list(MI())


S = ST()
Q = I()

switch = True
prefix = ""
suffix = ""
for _ in range(Q):
    query = list(input().split())
    if query[0] == "1":
        if switch:
            switch = False
        else:
            switch = True
    else:
        if switch:
            if query[1] == "1":
                prefix = query[2] + prefix
            else:
                suffix = suffix + query[2]
        else:
            if query[1] == "1":
                suffix = suffix + query[2]
            else:
                prefix = query[2] + prefix

ans = prefix + S + suffix
if switch:
    print(ans)
else:
    print(ans[::-1])

Solution (Python):

import sys

input = sys.stdin.readline


def ST():
    return input().rstrip()


def I():
    return int(input())


def MI():
    return map(int, input().split())


def LI():
    return list(MI())


S = ST()
Q = I()

switch = True
prefix = ""
suffix = ""
for _ in range(Q):
    query = list(input().split())
    if query[0] == "1":
        if switch:
            switch = False
        else:
            switch = True
    else:
        if switch:
            if query[1] == "1":
                prefix = query[2] + prefix
            else:
                suffix = suffix + query[2]
        else:
            if query[1] == "1":
                suffix = suffix + query[2]
            else:
                prefix = query[2] + prefix

ans = prefix + S + suffix
if switch:
    print(ans)
else:
    print(ans[::-1])

Solution (Python):
import bisect,collections,copy,itertools,math,string
import sys
def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())
def main():
    s = S()
    q = I()
    lst = collections.deque(list(s))
    state = False

    for _ in range(q):
        query = LS()
        if len(query) == 1:
            state = not state
        else:
            if state:
                if query[1] == "1":
                    lst.append(query[2])
                else:
                    lst.appendleft(query[2])
            else:
                if query[1] == "1":
                    lst.appendleft(query[2])
                else:
                    lst.append(query[2])
    ans = ""
    if state:
        for _ in range(len(lst)):
            ans += lst.pop()
    else:
        ans = "".join(lst)

    print(ans)
main()

Solution (Python):
s=input()
q=int(input())
quary=[]
for i in range(q):
    temp=list(input().split())
    quary.append(temp)
switch=False
first=''
last=''
for i in range(q):
    if quary[i][0]=='1':
        if switch:
            switch=False
        else:
            switch=True

    else:
        if quary[i][1]=='1':
            if switch:
                last+=quary[i][2]
            else:
                first+=quary[i][2]
        else:
            if switch:
                first+=quary[i][2]
            else:
                last+=quary[i][2]
s=first[::-1]+s+last
if switch:
    print(s[::-1])
else:
    print(s)

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


Reverse_Flag = 0  

for _ in range(Q):
    string = (input())
    Query = string.replace(' ', '')
    Query = list(Query)

    if Query[0] == "1":
        if Reverse_Flag == 0:
            Reverse_Flag = 1
        else:
            Reverse_Flag = 0
    
    elif  Query[1] == "1":
        if Reverse_Flag == 0:
            S.appendleft(Query[2])
        else:
            S.append(Query[2])

    else:
        if Reverse_Flag == 0:
            S.append(Query[2])
        else:
            S.appendleft(Query[2])

if Reverse_Flag == 1:
    S = list(S)
    S = S[::-1]
    print("".join(S))
else:
    S = list(S)
    print("".join(S))