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 copy
import random
import bisect 
import fractions 
import math
import sys
import collections

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

d = collections.deque()
def LI(): return list(map(int, sys.stdin.readline().split()))

S = str(input())
Q = int(input())
q = [0 for i in range(Q)]
for i in range(Q):
    q[i] = list(input().split(" "))


status = True 
ans = collections.deque()
for s in list(S):
    ans.append(s)
for i in range(Q):
    query = q[i]
    if query[0] == "1":
        status = not(status)
    else:
        if query[1] == "1":
            if status:
                ans.appendleft(query[2])
            else:
                ans.append(query[2])
        else:
            if status:
                ans.append(query[2])
            else:
                ans.appendleft(query[2])
if status:
    print("".join(list(ans)))
else:
    print("".join(reversed(list(ans))))

Solution (Python):
from collections import deque

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

judge = True
S = deque()
for string in s:
    S.append(string)

for query in queries:
    if query[0] == '1':
        judge = not judge
    else:
        if judge:
            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])
ans = ''.join(S)
if not judge:
    print(ans[::-1])
else:
    print(ans)

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


def main():
    S = input().strip()
    Q = int(input())
    Query = [list(input().split()) for _ in range(Q)]
    reverse = 1
    head = ""
    tail = ""
    for T in Query:
        if T[0] == "1":
            if reverse == 1:
                reverse = 2
            else:
                reverse = 1
        elif T[0] == "2":
            if int(T[1]) == reverse:
                head = T[2] + head
            else:
                tail = tail + T[2]
    S = head + S + tail
    print(S) if reverse == 1 else print(S[::-1])


if __name__ == '__main__':
    main()

Solution (Python):
import sys
sys.setrecursionlimit(10**6)
readline = sys.stdin.readline
s = readline().rstrip()
q = int(readline())
queries = [[i for i in readline().split()] for _ in range(q)]

t = ''

for query in queries:
    if query[0] == '1':
        s,t = t,s
    else:
        if query[1] == '1':
            t += query[2]
        else:
            s += query[2]
            
print(t[::-1]+s)

Solution (Python):
from collections import deque
S = deque()
tmp = input()
for i in range(len(tmp)):
  S.append(tmp[i])
Q = int(input())
flag = True
for i in range(Q):
  string = input().split()
  if string[0] == '1':
    flag = not flag
  elif flag ^ (string[1] == '1'):
      S.append(string[2])
  else:
      S.appendleft(string[2])
if not flag:
  S.reverse()
for s in S:
  print(s, end='')

Solution (Python):
import sys
sys.setrecursionlimit(10**6)
readline = sys.stdin.readline
s = readline().rstrip()
q = int(readline())
queries = [[i for i in readline().split()] for _ in range(q)]

from collections import deque

d = deque([i for i in s])
rev = 1

for query in queries:
    if query[0] == '1':
        rev *= -1
    else:
        if rev == 1:
            if query[1] == '1':
                d.appendleft(query[2])
            else:
                d.append(query[2])
        else:
            if query[1] == '1':
                d.append(query[2])
            else:
                d.appendleft(query[2])

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

Solution (Python):
import math
import queue
from collections import defaultdict
 
def readInt():
	return int(input())
def readInts():
	return list(map(int, input().split()))
def readChar():
	return input()
def readChars():
	return input().split()
def factorization(n):
	res = []
	if n%2==0:
		res.append(2)
	for i in range(3,math.floor(n//2)+1,2):
		if n%i==0:
			c = 0
			for j in res:
				if i%j==0:
					c=1
			if c==0:
				res.append(i)
	return res
def fact2(n):
	p = factorization(n)
	res = []
	for i in p:
		c=0
		z=n
		while 1:
			if z%i==0:
				c+=1
				z/=i
			else:
				break
		res.append([i,c])
	return res
def fact(n):
	ans = 1
	m=n
	for _i in range(n-1):
		ans*=m
		m-=1
	return ans
def comb(n,r):
	if n<r:
		return 0
	l = min(r,n-r)
	m=n
	u=1
	for _i in range(l):
		u*=m
		m-=1
	return u//fact(l)
def printQueue(q):
	r=q
	ans=[0]*r.qsize()
	for i in range(r.qsize()-1,-1,-1):
		ans[i] = r.get()
	print(ans)

s = readChar()
q = readInt()
qa = []
booh = 0
for i in range(q):
	x = input()
	if x=="1":
		qa.append([1,0,0])
	else:
		x = x.split()
		qa.append([int(x[0]),int(x[1]),x[2]])

def t1(s):
	return s[::-1]

def t2(f,c,s):
	if f==1:
		return c+s
	else:
		return s+c

fi=""
la=""
q = queue.deque()
for a in qa:
	if a[0]==1:
		booh = (booh+1)%2
	else:
		if (a[1]+booh)%2==1:
			fi=a[2]+fi
			q.appendleft(a[2])
		else:
			la+=a[2]

s = "".join(list(q))+s+la
if booh==1:
	s = t1(s)

print(s)

Solution (Python):
import math
import queue
from collections import defaultdict
 
def readInt():
	return int(input())
def readInts():
	return list(map(int, input().split()))
def readChar():
	return input()
def readChars():
	return input().split()
def factorization(n):
	res = []
	if n%2==0:
		res.append(2)
	for i in range(3,math.floor(n//2)+1,2):
		if n%i==0:
			c = 0
			for j in res:
				if i%j==0:
					c=1
			if c==0:
				res.append(i)
	return res
def fact2(n):
	p = factorization(n)
	res = []
	for i in p:
		c=0
		z=n
		while 1:
			if z%i==0:
				c+=1
				z/=i
			else:
				break
		res.append([i,c])
	return res
def fact(n):
	ans = 1
	m=n
	for _i in range(n-1):
		ans*=m
		m-=1
	return ans
def comb(n,r):
	if n<r:
		return 0
	l = min(r,n-r)
	m=n
	u=1
	for _i in range(l):
		u*=m
		m-=1
	return u//fact(l)
def printQueue(q):
	r=q
	ans=[0]*r.qsize()
	for i in range(r.qsize()-1,-1,-1):
		ans[i] = r.get()
	print(ans)

s = readChar()
q = readInt()
qa = []
booh = 0
for i in range(q):
	x = input()
	if x=="1":
		qa.append([1,0,0])
	else:
		x = x.split()
		qa.append([int(x[0]),int(x[1]),x[2]])

def t1(s):
	return s[::-1]

def t2(f,c,s):
	if f==1:
		return c+s
	else:
		return s+c

fi=""
la=""
for a in qa:
	if a[0]==1:
		booh = (booh+1)%2
	else:
		if (a[1]+booh)%2==1:
			fi=a[2]+fi
		else:
			la+=a[2]

s = fi+s+la
if booh==1:
	s = t1(s)

print(s)

Solution (Python):


from collections import deque

S = deque([s for s in input()])
Q = int(input())
rev = False

for _ in range(Q):
    T = [t for t in input().split()]
    t = int(T[0])
    if t == 1:
        rev = not rev
    if t == 2:
        f, c = int(T[1]), T[2]
        if f == 1:
            if rev:
                S.append(c)
            else:
                S.appendleft(c)
        if f == 2:
            if rev:
                S.appendleft(c)
            else:
                S.append(c)

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

Solution (Python):
s = input()
q = int(input())
m = 0
t = ''
l = ''
for i in range(q):
    Q = input().split()
    if(Q[0] == '1'):
        m = m ^ 1
        
    elif(Q[0] == '2'):
        if(Q[1] == '1'):
            if(m == 0):t += Q[2]
            else:l += Q[2]
        elif(Q[1] == '2'):
            if(m == 0):l += Q[2]
            else:t += Q[2]

s = t[::-1] + s + l

if(m == 1):s = s[::-1]
print(s)

Solution (Python):
S = input()
Q = int(input())
Qlist = [list(input().split()) for _ in range(Q)]
toplist=[]
backlist=[]
Flag=1

i=0
while i<Q:
    if Qlist[i][0]=="1":
        Flag*=-1
    else:
        
        if Qlist[i][1]=="1" and Flag==-1:
            backlist.append(Qlist[i][2])
        elif Qlist[i][1]=="1" and Flag==1:
            toplist.append(Qlist[i][2])
        elif Qlist[i][1]=="2" and Flag==-1:
            toplist.append(Qlist[i][2])
        elif Qlist[i][1]=="2" and Flag==1:
            backlist.append(Qlist[i][2])
    i+=1
if Flag==1:
    
    
    
    print("".join(toplist[::-1])+S+"".join(backlist))
else:
    
    
    
    print("".join(backlist[::-1])+S[::-1]+"".join(toplist))

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

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

Solution (Python):
s=input()
q=int(input())
cnt=0
l=''
r=''
for i in range(q):
  k=input()
  if k[0]=='1':
    cnt+=1
  else:
    if k[2]=='1':
      if cnt%2==0:
        l=k[4]+l
      else:
        r+=k[4]
    elif k[2]=='2':
      if cnt%2!=0:
        l=k[4]+l
      else:
        r+=k[4]
if cnt%2==0:
  s=l+s+r
else:
  s=l+s+r
  s=s[::-1]
print(s)

Solution (Python):

import sys, re
from collections import deque, defaultdict, Counter, OrderedDict
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from heapq import heappush, heappop, heapify, nlargest, nsmallest
def STR(): return list(input())
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def list2d(a, b, c): return [[c] * b for i in range(a)]
def sortListWithIndex(listOfTuples, idx):   return (sorted(listOfTuples, key=lambda x: x[idx]))
def sortDictWithVal(passedDic):
    temp = sorted(passedDic.items(), key=lambda kv: (kv[1], kv[0]))
    toret = {}
    for tup in temp:
        toret[tup[0]] = tup[1]
    return toret
def sortDictWithKey(passedDic):
    return dict(OrderedDict(sorted(passedDic.items())))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

s = input()
q = INT()
rev = 0
begg = []
end = []
for _ in range(q):
     temp = input()
     if(len(temp.split())==1):
         rev+=1

     else:
         t,f,c = temp.split()
         if(f=="1"):
             if(rev%2==0):
                 begg.append(c)
                 
             else:
                 end.append(c)
                 
         else:
             if(rev%2==0):
                 end.append(c)
                 
             else:
                 begg.append(c)
                 
abc = "".join(begg[::-1]) + s + "".join(end)
if(rev%2==0):
    print(abc)
else:
    print(abc[::-1])

Solution (Python):
from collections import deque

s = deque(list(input()))
q = int(input())
flg = 1
for i in range(q):
    array = input().split(' ')
    if array[0] == '1':
        flg *= -1
    elif array[0] == '2':
        if array[1] == '1' and flg == 1 or array[1] == '2' and flg == -1:
            s.appendleft(array[2])
        else:
            s.append(array[2])

s = list(s)
if flg == -1:
    s = s[::-1]

s = ''.join(s)
print(s)

Solution (Python):
from collections import deque

s=input()
s=deque(s)
q=int(input())
ans=''
flg=0
for _ in range(q):
	query=list(map(str,input().split()))
	if query[0]=='1':
		flg=1-flg
	else:
		if flg:
			if query[1]=='1':
				s.append(query[2])
			else:
				s.appendleft(query[2])
		else:
			if query[1]=='1':
				s.appendleft(query[2])
			else:
				s.append(query[2])
if flg:
	t=[]
	for i in range(len(s)):
		t.append(s.pop())
	print(''.join(t))
else:
    print(''.join(s))

Solution (Python):
from sys import stdin
s = str(stdin.readline().rstrip())


q= int(stdin.readline().rstrip())
hanten = 0
t=""
u=""

for i in range(q):
    l = list(map(str, stdin.readline().rstrip().split()))
    if int(l[0]) == 1:
        t,u = u,t
        hanten ^= 1
    else:
        if int(l[1])==1:
            t+=l[2]
        else:
            u+=l[2]


if hanten==0:
    print(t[::-1]+s+u)
else:
    print(t[::-1]+s[::-1]+u)

Solution (Python):
from collections import deque

s = input()
q = int(input())
b = deque(s)
swc = 0
for i in range(q):
	a = list(map(str, input().split()))
	if a[0] == "1":
		swc += 1
	elif a[1] == "1":
		if swc%2 == 0:
			b.appendleft(a[2])
		else:
			b.append(a[2])
	else:
		if swc%2 == 0:
			b.append(a[2])
		else:
			b.appendleft(a[2])
ans = ""
if swc%2 == 0:
	ans = "".join(list(b))
else:
	ans = "".join(reversed(list(b)))
print (ans)

Solution (Python):
from collections import deque


def main():
    S = input()
    que = deque(S)
    Q = int(input())
    reverse = False
    for _ in range(Q):
        query = input()
        if len(query) == 1:
            reverse = not reverse
        else:
            _, F, C = query.split()
            if (not reverse and F == '1') or (reverse and F == '2'):
                que.appendleft(C)
            else:
                que.append(C)
    if reverse:
        que.reverse()
    print(''.join([c for c in que]))


if __name__ == '__main__':
    main()

Solution (Python):
import itertools
import sys
import math
import numpy as np
from collections import deque
from itertools import combinations
from functools import reduce
from functools import lru_cache
from collections import deque
sys.setrecursionlimit(10**9)

def main():
    S = input()
    Q = int(input())
    d = deque(list(S))
    rev = False

    def insert(x,f):
        g = f == "2" 
        if rev:
            g = not g

        if g:
            d.append(x)
        else:
            d.appendleft(x)


    for q in range(Q):
        inp = input()
        if inp == "1":
            rev = not rev
        else:
            _,F,C = inp.split()
            insert(C,F)


    if rev:
        d.reverse()

    print("".join(d))



if __name__ == "__main__":
  main()

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

rev = 0
head = []
tail = []

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

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

if rev == 0:
    print(h[::-1] + s + t)
else:
    s = s[::-1]
    print(t[::-1] + s + h)

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

count = 0
top = []
bottom = []

for i in range(q):
  query = list(input().split())
  if query[0] == "1":
    count = (count + 1) % 2  
  if query[0] == "2":    
    if query[1] == "1":      
      if count == 0:        
        top.append(query[2])    
      else:
        bottom.append(query[2])
    if query[1] == "2":      
      if count == 0:        
        bottom.append(query[2])        
      else:       
        top.append(query[2])
  
t = "".join(top)
b = "".join(bottom)

lst = [t[::-1], s, b]
ans = "".join(lst)

if count == 1:
  ans = ans[::-1]
  
print(ans)

Solution (Python):
S = input()
Q = int(input())
query = [0]*Q

for i in range(Q):
    inp = input()
    if inp == "1":
        query[i] = "1"
    else:
        query[i] = list(inp.split())

left_head_if_even = 0
right = ""
left = ""

for i in range(Q):
    if query[i] == "1":
        left_head_if_even += 1
    else:
        if query[i][1] == "1":
            if left_head_if_even%2 == 0:
                left = query[i][2] + left
            else:
                right = right + query[i][2]

        else:
            if left_head_if_even%2 == 1:
                left = query[i][2] + left
            else:
                right = right + query[i][2]

if left_head_if_even%2==0:
    S = left + S + right
else:
    S = left + S + right
    S = S[::-1]

print(S)

Solution (Python):


S = input()
Q = int(input())
query = []
flag = 1
before = ""
after  = ""

for i in range(Q):
    query = input().split()
    if query[0] == "1":
        flag *= -1
    else:
        if (query[1] == "1" and flag == 1) or  (query[1] == "2" and flag ==-1):
            before += query[2]
        else:
            after += query[2]
        
if flag == 1:
    print(''.join(list(reversed(before))) + S + after)
else:
    print(''.join(list(reversed(after))) + ''.join(list(reversed(S))) + before)

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

u = []
for i in range(q):
    u.append(list(map(str,input().split())))

from collections import deque
l = deque([])
r = deque([])

x = 1
for i in range(q):
    if int(u[i][0]) == 1:
        x = x*(-1)
    else:
        if int(u[i][1])*x == 1 or int(u[i][1])*x == -2:
            l.append(u[i][2])
        else:
            r.append(u[i][2])
if x == 1:
    print(''.join(l)[::-1]+s[::x]+''.join(r))
else:
    print(''.join(r)[::-1]+s[::x]+''.join(l))

Solution (Python):
from collections import deque

ans = deque(input())
Q = int(input())
state = True
for _ in range(Q):
    Query = input().split()
    if int(Query[0]) == 1:
        state = not state
    else:
        if int(Query[1]) == 1:
            if state:
                ans.appendleft(Query[2])
            else:
                ans.append(Query[2])
        if int(Query[1]) == 2:
            if state:
                ans.append(Query[2])
            else:
                ans.appendleft(Query[2])

if state:
    print("".join(map(str, ans)))
else:
    print("".join(map(str, reversed(ans))))

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

Solution (Python):
import collections

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

s2 = collections.deque(s)

inn = []
jun = 0

for i in range(q):
    inn = list(input().split())
    if inn[0] == "1":
        if jun == 0:
            jun = 1
        else:
            jun = 0
    elif inn[0] == "2":
        if inn[1] == "1" and jun == 0:
            s2.appendleft(inn[2])
        elif inn[1] == "2" and jun == 0:
            s2.append(inn[2])
        if inn[1] == "1" and jun == 1:
            s2.append(inn[2])
        elif inn[1] == "2" and jun == 1:
            s2.appendleft(inn[2])
    
if jun == 0:
    news = ''.join(s2)
else:
    s2.reverse()
    news = ''.join(s2)

print(news)

Solution (Python):
"""
import random
import functools
import copy
import bisect
import array
import re
import collections
import heapq
import fractions
import itertools
import string
import math
from operator import itemgetter as ig
from bisect import bisect_left, bisect_right, insort_left, insort_right
from itertools import permutations, combinations, product, accumulate, groupby
from heapq import heappush, heappop
from collections import deque, defaultdict, Counter
import sys
sys.setrecursionlimit(10 ** 7)


inf = 10 ** 20
INF = float("INF")
mod = 10 ** 9 + 7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = dd + [(-1, 1), (1, 1), (1, -1), (-1, -1)]
ddn9 = ddn + [(0, 0)]
'''for dx, dy in dd:
        nx = j + dx; ny = i + dy
            if 0 <= nx < w and 0 <= ny < h:'''
"""
from collections import defaultdict
import sys
sys.setrecursionlimit(10 ** 7)


def wi(): return list(map(int, sys.stdin.readline().split()))



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


def ws(): return sys.stdin.readline().split()


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


def ss(): return input()


def hi(n): return [si() for _ in range(n)]


def hs(n): return [ss() for _ in range(n)]  


def s_list(): return list(input())


def mi(n): return [wi() for _ in range(n)]  


def mip(n): return [wip() for _ in range(n)]


def ms(n): return [ws() for _ in range(n)]


def num_grid(n): return [[int(i) for i in sys.stdin.readline().split()[
    0]] for _ in range(n)]  


def grid(n): return [s_list() for _ in range(n)]


def gcd(a, b):
    while b:
        a, b = b, a % b
    return a




def round_up(a, b):
    """ a/b の切り上げ """
    c = (a + b - 1) / b
    return c


def lcm(a, b):
    """a,bの最小公倍数"""
    return a * b // gcd(a, b)


def main():
    s = input()
    q = int(input())
    Q = []
    cnt = 0
    head = []
    tail = []
    for _ in range(q):
        Q.append(list(map(str, input().split())))
    
    for i in range(q):
        if Q[i][0] == '1':
            cnt += 1
            head, tail = tail, head

        else:
            if Q[i][1] == '1':
                head.append(Q[i][2])
            else:
                tail.append(Q[i][2])
    print("".join(head[::-1]) + (s[::-1] if cnt %
                                 2 == 1 else s) + "".join(tail))


if __name__ == '__main__':
    main()

Solution (Python):
from collections import deque
S = deque(input())
Q = int(input())
cnt = 1
num = 0
for i in range(Q):
    TFC = input()
    if TFC == "1":
        cnt *= -1
        num += 1
    else:
        if TFC[:3]== "2 1":
            if cnt ==1:
                S.appendleft(TFC[4])
            else:
                S.append(TFC[4])
        else:
            if cnt ==1:
                S.append(TFC[4])
            else:
                S.appendleft(TFC[4])
if num % 2 ==1:
    print(("".join(reversed([i for i in S]))))
else:
    print(("".join([i for i in S])))

Solution (Python):
S = input()
Q = int(input())
Switch = False
Query = []

import collections
dq = collections.deque(S)

for i in range(Q):
    s = input()
    if s[0] == '1':
        if Switch:
            Switch = False
        else:
            Switch = True
    if s[0] == '2':
        if s[2] == '1' and Switch==False:
            dq.appendleft(s[4])

        elif s[2]=='1' and Switch==True:
            dq.append(s[4])

        elif s[2] == '2' and Switch==False:
            dq.append(s[4])

        else:

            dq.appendleft(s[4])

if Switch == False:
    print(''.join(dq))
else:
    print(''.join(dq)[::-1])

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

Queries = []
for i in range(Q):
    Queries.append(list(input().split()))

rev = False
head = ""
tail = ""



for query in Queries:
    if query[0] == '1':
        tmp = head
        head = tail
        tail = tmp
        rev = 1 - rev  

    if query[0] == '2':
        if query[1] == '1':
            head += query[2]
        if query[1] == '2':
            tail += query[2]

if rev:
    S = S[::-1]
S = head[::-1] + S + tail
print(S)

Solution (Python):
from collections import deque
S = input()
Q = int(input())
F=0
d=deque(S)
for i in range(Q):
  q=list(map(str,input().split()))
  if q[0]=="1":
    F+=1
    F%=2
  else:
    if q[1]=="1":
      if F==0:
        d.appendleft(q[2])
      else:
        d.append(q[2])
    else:
      if F==0:
        d.append(q[2])
      else:
        d.appendleft(q[2])  
d=list(d)
if F%2==0:
  print("".join(d))
else:
  print("".join(d[::-1]))

Solution (Python):
import sys
sys.setrecursionlimit(10**6) 
import math

from copy import copy, deepcopy
from operator import itemgetter

import bisect

from collections import deque 



from collections import Counter

from itertools import accumulate

from heapq import heapify,heappop,heappush


def input(): return sys.stdin.readline()[:-1]
def printl(li): print(*li, sep="\n")
def argsort(s): return sorted(range(len(s)), key=lambda k: s[k])



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




















qs=[]
s=deque(list(S))
for i in range(Q):
    qs.append(list(input().split()))

rev=0
for q in qs:
    if int(q[0])==1: rev=(rev+1)%2
    else:
        if (int(q[1])+rev)%2==1:
            s.appendleft(q[2])
        else:
            s.append(q[2])
    

if rev==0:
    for i in range(len(s)): print(s.popleft(), end="")
else:
    for i in range(len(s)): print(s.pop(), end="")
print()

Solution (Python):
s = input()
q = int(input())
ans_list1 = []
ans_list2 = []
flag = 1
for i in range(q):
    i = input().split()
    if i[0]=="1":
        flag *= -1
    else:
        if i[1]=='1':
            if flag==1:
                ans_list1.append(i[2])
            else:
                ans_list2.append(i[2])
        else:
            if flag==1:
                ans_list2.append(i[2])
            else:
                ans_list1.append(i[2])

if flag==1:
    print(''.join(ans_list1[::-1]) + s +''.join(ans_list2))
else:
    print(''.join(ans_list2[::-1]) + s[::-1] +''.join(ans_list1))

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

import collections
dq = collections.deque(s)

rev = 1
for i in Q:
    if i == ['1']:
        rev *= -1
    else:
        if (i[1] == '1' and rev == 1) or (i[1] == '2'and rev == -1):
            dq.appendleft(i[2])
        else:
            dq.append(i[2])

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

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

states=1
front=[]
back=[]
for n in range(Q):
    query=input()
    if query=="1":
        states*=-1
    else:
        N,F,C=query.strip().split()
        if int(F)==1:
            if states==1:
                front.append(C)
            else:
                back.append(C)
        else:
            if states==1:
                back.append(C)
            else:
                front.append(C)

if states==1:
    print("".join(front)[::-1]+S+"".join(back))
else:
    print("".join(back)[::-1]+S[::-1]+"".join(front))

Solution (Python):
word = input().rstrip()
n = int(input())
hanten = 0
lhantenlist = []
rlist = []
for i in range(n):
    a = input().rstrip().split(" ")
    if a[0] == "1":
        hanten = (hanten + 1) % 2
        
        
    else:
        if (a[1] == "2" and hanten == 0) or (a[1] == "1" and hanten == 1):
            rlist.append(a[2])
            
        else:
            lhantenlist.append(a[2])
            

lhanten="".join(lhantenlist)
r="".join(rlist)

if hanten == 0:
    anslist=[lhanten[::-1],word,r]
    ans="".join(anslist)
else:
    anslist = [r[::-1], word[::-1], lhanten]
    ans="".join(anslist)
print(ans)

Solution (Python):
def main():
    from collections import deque
    s = list(input())
    dq = deque()
    for i in s:
        dq.append(i)

    q = int(input())
    flag = True
    for _ in range(q):
        line = input().split(" ")
        if line[0] == "1":
            flag = not flag
        else:
            if line[1] == "1":
                if flag:
                    dq.appendleft(line[2])
                else:
                    dq.append(line[2])
            elif line[1] == "2":
                if flag:
                    dq.append(line[2])
                else:
                    dq.appendleft(line[2])
    if flag == False:
        dq.reverse()
    for i in list(dq):
        print(i,end="")
main()

Solution (Python):
from collections import deque
S=list(input())
Q=int(input())
query=[]
reverseflg=False
S=deque(S)

for i in range(Q):
  query.append(list(input().split()))

for q in range(Q):
  if query[q][0]=="1":
    reverseflg= not reverseflg
  
  if query[q][0]=="2":
    if query[q][1]=="1":
      if reverseflg== False:
        S.appendleft(query[q][2])
      else:
        S.append(query[q][2])
    
    else:
      if reverseflg== False:
        S.append(query[q][2])
      else:
        S.appendleft(query[q][2])

if reverseflg==True:
  S.reverse()
  
print(''.join(S))

Solution (Python):
from collections import deque
S=list(input())
Q=int(input())
query=[]
reverseflg=False
S=deque(S)

for i in range(Q):
  query.append(list(input().split()))

for q in range(Q):
  if query[q][0]=="1":
    reverseflg= not reverseflg
  
  if query[q][0]=="2":
    if query[q][1]=="1":
      if reverseflg== False:
        S.appendleft(query[q][2])
      else:
        S.append(query[q][2])
    
    else:
      if reverseflg== False:
        S.append(query[q][2])
      else:
        S.appendleft(query[q][2])

if reverseflg==True:
  S.reverse()
  
print(''.join(S))

Solution (Python):
from collections import deque
S = input()
deque = deque([s for s in S])
Q = int(input())
rev_flag = 0
for _ in range(Q):
    q = list(map(str, input().split()))
    if len(q) == 1:
        if rev_flag:
            rev_flag = 0
        else:
            rev_flag = 1
    else:
        if q[1] == "1":
            if rev_flag:
                deque.append(q[2])
            else:
                deque.appendleft(q[2])
        else:
            if rev_flag:
                deque.appendleft(q[2])
            else:
                deque.append(q[2])
if rev_flag:
    print("".join(deque)[::-1])
else:
    print("".join(deque))

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

for i in range(len(s)):
    q.append(s[i])

flag = True
for i in range(n):
    query = input()
    if query[0]=="1":
        flag = not flag
    else:
        if query [2]=="1":
            if flag:
                q.appendleft(query[4])
            else:
                q.append(query[4])
        else:
            if flag:
                q.append(query[4])
            else:
                q.appendleft(query[4])
if flag:
    print(''.join(q))
else:
    q.reverse()
    print("".join(q))

Solution (Python):
from collections import deque
S = input()
que = deque()
que.append(S)
Q = int(input())
Front_flag = 1

for i in range(Q):
    Query = tuple(input().split())
    if len(Query) == 1:
        Front_flag = 1 - Front_flag
    else:
        a, b, c = Query
        if (Front_flag + int(b)) % 2 == 1:
            que.append(c)
        else:
            que.appendleft(c)

ans = ''.join(que)
if Front_flag:
    print(ans)
else:
    print(ans[::-1])