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 sys
import math
import collections
from collections import deque
def main():
    
    
    s = input()
    
    q = int(input())
    d = deque([])
    for i in range(len(s)):
        d.append(s[i])

    rev = 1
    for i in range(q):
        que = sys.stdin.readline().split()
        if que[0] == "1":
            rev *= -1

        else:
            if rev == 1:
                if que[1] == "1":
                    d.appendleft(que[2])
                else:
                    d.append(que[2])

            else:
                if que[1] == "1":
                    d.append(que[2])
                else:
                    d.appendleft(que[2])


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

main()

Solution (Python):
s = input()
q = int(input())
top = ""
end = ""
count = 0
for i in range(q):
    a = input()
    if len(a) == 1:
        count += 1
    else:
        a = list(a.split())
        if (count + int(a[1])) % 2 == 0:
            end = end + a[2]
        else:
            top = a[2] + top
ans = top + s + end
if count % 2 == 1:
    ans = ans[::-1]
print(ans)

Solution (Python):
Slist = list(input())
Q = int(input())

hanten = 0
mae = [] 
ushiro = []

for q in range(Q):
    query = input()
    if query == "1":
        if hanten == 0:
            hanten = 1
        else:
            hanten = 0
    else:
        T,F,C = query.split()
        if F == "1":
            if hanten == 0:
                mae.append(C)
            else:
                ushiro.append(C)
        elif F =="2":
            if hanten == 0:
                ushiro.append(C)
            else:
                mae.append(C)

if hanten == 0:
    rmae = mae[::-1]
    goukei = rmae + Slist + ushiro
    print(''.join(goukei))
else:
    goukei =ushiro[::-1] + Slist[::-1] + mae
    print(''.join(goukei))

Solution (Python):
s=input()
q=int(input())
t1counter=0
s1=''
for i in range(q):
    tfc=input().split()
    if(tfc[0]=='1'):
        t1counter+=1
    else:
        if(t1counter%2==0 and tfc[1]=='1'):
            s1=s1+tfc[2]
        elif(t1counter%2==1 and tfc[1]=='2'):
            s1=s1+tfc[2]
        elif(t1counter%2==0 and tfc[1]=='2'):
            s=s+tfc[2]
        elif(t1counter%2==1 and tfc[1]=='1'):
            s=s+tfc[2]
        else:
            print('error')
s=s1[::-1]+s
if(t1counter%2==0):
    print(s)
elif(t1counter%2==1):
    print(s[::-1])

Solution (Python):
from queue import deque
def main():
    que=deque()
    que.append(input())
    head = True
    for i in range(int(input())):
        q=input().split()
        if q[0]=="1":
            head = not head
        else:
            h = q[1]=="1"
            if (head and h) or (not head and not h):
                que.appendleft(q[2])
            else:
                que.append(q[2])      
    string = "".join(que)
    print(string if head else string[::-1])
main()

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

s = input().split()[0]
q = int(input())

left, right = [], []
state = 1
for _ in range(q):
    q_i = input().split()
    if len(q_i) == 1:
        state = (state)%2 + 1
    else:
        _, f, c = q_i
        f = int(f)
        if (f+state)%2 == 0:
            left += [c]
        else:
            right += [c]
if state == 1:
    print(''.join(left[::-1]) + s + ''.join(right))
else:
    print(''.join(right[::-1]) + s[::-1] + ''.join(left))

Solution (Python):
if __name__ == '__main__':
	S = input()
	Q = int(input())

	c = 1

	A = ''
	B = ''

	for i in range(Q):
		I = input()
		if len(I) == 1:
			c = c * (-1)
		else:
			T, F, C = I.split()
			f = int(F)
			if f == 1 and c == 1:
				A = C + A
			elif f == 1 and c == -1:
				B = B + C
			elif f == 2 and c == -1:
				A = C + A
			else:
				B = B + C

	S = A + S + B

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

	print(S)

Solution (Python):
import collections

s = collections.deque(input())
q = int(input())
rev = 0
for i in range(q):
  tmp = input().split()
  if len(tmp) == 1:
    rev += 1
  else:
    if tmp[1] == '1':
      if rev % 2 == 0:
        s.appendleft(tmp[2])
      else:
        s.append(tmp[2])
    else:
      if rev % 2 == 0:
        s.append(tmp[2])
      else:
        s.appendleft(tmp[2])
if rev % 2:
  while True:
    try:
      print(s.pop(), end='')
    except IndexError:
      break
else:
  while True:
    try:
      print(s.popleft(), end='')
    except IndexError:
      break

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

m=1
A=[]
B=[]
for i in range(Q):
  if int(Query[i][0])==1:
    m*=-1
  else:
    
    if m*((-2*int(Query[i][1]))+3)==1:
      A.append(Query[i][2])
    else:
      B.append(Query[i][2])
if m==1:
  print(''.join(A)[::-1]+S+''.join(B))
else:
  print(''.join(B)[::-1]+S[::-1]+''.join(A))

Solution (Python):
S = list(input())
Q = int(input())
reverse=0
head=[]
tail=[]
for i in range(Q):
    q=input().split()
    if int(q[0])==1:
        reverse=(reverse+1)%2
    else :      
        if (reverse+(int(q[1])-1))%2:
            tail.append(q[2])
        else:
            head.append(q[2])

            
head.reverse()
if reverse:
    tmp=head
    head=tail
    tail=tmp
    S.reverse()
    head.reverse()
    tail.reverse()
    
    
print("".join(head+S+tail))

Solution (Python):
def m():
    s = input()
    q = int(input())
    inv = 0
    pre = []
    sub = []

    for i in range(q):
        raw = input().split()
        if len(raw) == 1:
            inv = inv ^ 1
        else:
            f = int(raw[1])-1
            c = raw[2]
            if inv==f:
                pre.append(c)
            else:
                sub.append(c)
    
    if inv==0:
        print(''.join(pre[::-1])+s+''.join(sub))
    else:
        print(''.join(sub[::-1])+s[::-1]+''.join(pre))

m()

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

num = 0
mae = ""
usi = ""
for i in range(Q):
    tmp = input().split()

    if tmp[0] == "1":
        num += 1

    elif tmp[0] == "2":
        num = num % 2
        dada = num + int(tmp[1])

        if dada == 2:
            usi += tmp[2]
            
            
        else:
            mae = tmp[2] + mae
            
            


if num % 2 == 1:
    S = S[::-1]
    S = usi[::-1] + S + mae[::-1]
else:
    S = mae + S + usi
print(S)

Solution (Python):
def solve():
    S = input()
    q = int(input())
    flg = 0
    li = ["", ""]
    for _ in range(q):
        Q = input().split()
        if Q[0] == "1":
            flg = 1 - flg
        else:
            li[(int(Q[1]) - 1 + flg) % 2] += Q[2]
    if flg:
        S = S[::-1]
    print(li[flg][::-1] + S + li[1 - flg])
solve()

Solution (Python):
s = input()
q = int(input())
que = [list(map(str, list(input().split()))) for i in range(q)]

hanten = 0
left = ""
right = ""
for i in range(q):
    if que[i][0] == "1":
        if hanten == 0:
            hanten = 1
        else:
            hanten = 0
    elif que[i][0] == "2":
        if hanten == 0:
            if que[i][1] == "2":
                sub = [right, que[i][2]]
                right = ''.join(sub)
            elif que[i][1] == "1":
                sub = [que[i][2], left]
                left = ''.join(sub)
        else:
            if que[i][1] == "2":
                sub = [que[i][2],left]
                left = ''.join(sub)
            elif que[i][1] == "1":
                sub = [right,que[i][2]]
                right = ''.join(sub)

sub = [left, s, right]
s = ''.join(sub)
if hanten == 1:
    s = s[::-1]
print(s)

Solution (Python):


S = input()
S_list = list(S)

tmp = [''] * (2 * 10**5)
tmp.extend(S_list)
tmp.extend([''] * (2 * 10**5))

Q = int(input())

rev = False
front_i = 2 * 10**5
end_i = front_i + len(S_list)
for i in range(Q):
    query = input()
    if len(query) == 1:
        
        rev = not rev
    else:
        
        [N, F, C] = [i for i in query.split()]
        F = int(F)
        front_flag = True
        if F == 2:
            front_flag = False
        if rev:
            front_flag = not front_flag
        
        if front_flag:
            front_i -= 1
            tmp[front_i] = C
        else:
            tmp[end_i] = C
            end_i += 1

if rev:
    print(''.join(reversed(tmp)))
else:
    print(''.join(tmp))

Solution (Python):
S = list(input())
Q = int(input())
f = list()
b = list()
r = False
for i in range(Q):
    q = input()
    if q[0] == '1':
        r = not r
    else:
        T, F, C = q.split()
        if (F == '1') ^ r:
            f.append(C)
        else:
            b.append(C)
if r:
    S = list(reversed(S + b)) + f
else:
    S = list(reversed(f)) + S + b

print(''.join(S))

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

isreverse = False
head=[]
tail=[]

for i in range(Q):
    query = input()
    if str(query)[0]=="1":
        isreverse = isreverse^True
    else:
        F = int(query.split()[1])
        C = str(query.split()[2])
        if (F==1 and isreverse==False) or (F==2 and isreverse==True):
            head.append(C)
        else:
            tail.append(C)


if isreverse==False:
    print("".join(reversed(head))+S+"".join(tail))
else:
    print("".join(reversed(tail))+S[::-1]+"".join(head))

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












left = ""
right = ""
target = 0

for query in queries:
    if query == "1":
        target = 1 - target
    else:
        _, F, C = list(query.split())
        if F == "1":
            if target == 0:
                left += C
            else:
                right += C
        else:
            if target == 0:
                right += C
            else:
                left += C
if target == 0:
    print("{}{}{}".format(left[::-1], S, right))
else:
    print("{}{}{}".format(right[::-1], S[::-1], left))

Solution (Python):
s = list(input())
n = int(input())
right_lst = ["0"]*(2*10**5)
left_lst = ["0"]*(2*10**5)
S = left_lst + s + right_lst
left = 2*10**5
right = 2*10**5+len(s)-1
isReverse=False

for i in range(n):
  A = input().split(" ")
  if A[0]=="1":
    isReverse = not isReverse
    
    
    continue
  else:
    if (A[1]=="1" and isReverse) or (A[1]=="2" and not isReverse):
      right +=1
      S[right] = A[2]
      
    else:
      left-=1
      S[left] = A[2]
      
    
    

ans = S[left:right+1] 

if isReverse:
  ans = list(reversed(ans))

a = "".join(ans)
print(a)

Solution (Python):
from collections import deque
s = deque(list(input()))
q = int(input())
hanten = 0
for i in range(q):
  nyuuryoku = input().split()
  n = nyuuryoku[0]
  if len(nyuuryoku) == 1:
    hanten += 1
    continue
  else:
    f = int(nyuuryoku[1])
    c = nyuuryoku[2]
    if hanten%2 == 0:
      if f ==1:
        s.appendleft(c)
      else:
        s.append(c)
    else:
      if f == 1:
        s.append(c)
      else:
        s.appendleft(c)
if hanten%2 == 1:
  s.reverse()
ans = "".join(s)
print(ans)

Solution (Python):
S = list([i for i in input().split()][0])
Q = [int(i) for i in input().split()][0]

r = 0
firstS = []
lastS = []
for q in range(Q):
  row = input().split()
  if len(row) == 1:
    r = (r+1) % 2
  else:
    f = int(row[1])
    c = row[2]
    if r == 0:
      if f == 1:
        firstS.append(c)
      else:
        lastS.append(c)
    else:
      if f == 2:
        firstS.append(c)
      else:
        lastS.append(c)
        
if r % 2 == 0:
  S = firstS[::-1] + S + lastS
else:
  S = lastS[::-1] + S[::-1] + firstS 
print("".join(S))

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

R=0
S1=""
S2=""
for i in range(Q):
    temp=list(input().split())

    if int(temp[0])==1:
        S=S1+S+S2
        S1=""
        S2=""
        R+=1
    elif int(temp[0])==2:
        F=int(temp[1])
        c=temp[2]
        if F==1:
            if R%2==0:
                S1=c+S1
            else:
                S2=S2+c
        elif F==2:
            if R%2==0:
                S2=S2+c
            else:
                S1=c+S1
S=S1+S+S2

if R%2==1:
    ans=S[-1::-1]
    print(ans)
else:
    print(S)

Solution (Python):
s=input()
q=int(input())
flg=1
mae,ushiro='',''
for i in range(q):
  inp=input()
  if inp=='1':
    flg*=-1
  else:
    f,c=inp[2],inp[4]
    if f=='1'and flg==1:
      mae+=c
    elif f=='2' and flg==-1:
      mae+=c
    else:
      ushiro+=c

s=mae[::-1]+s+ushiro
if flg==-1:
  s=s[::-1]
print(s)

Solution (Python):
s = str(input())
n = int(input())

right = ""
left = ""

flag = 0
for i in range(n):
    q = list(map(str,input().split()))
    if len(q) == 1:
        flag = 1 - flag 
    elif q[1] == "1":
        if flag == 0:
            left = q[2] + left
        else:
            right = right + q[2]
    else:
        if flag == 0:
            right = right + q[2]
        else:
            left = q[2] + left


if flag == 0:
    print(left + s + right)
elif flag == 1:
    print(right[::-1] + s[::-1] + left[::-1])

Solution (Python):
S = input()
Q = int(input())
rev = 1
head = []
tail = []
for _ in range(Q):
    lst = input().split() 
    if (lst[0] == '1'):
        rev *= -1
    else:
        if (lst[1] == '1'):
            pos = rev
        else:
            pos = -1 * rev
        if (pos == 1):
            head.append(lst[2])
        else:
            tail.append(lst[2])
ans = ''.join(head[::-1]) + S + ''.join(tail)
print(ans[::rev])

Solution (Python):
S = input()
Q = int(input())
Atama = []
Osiri = []
rev = [0]*Q
for i in range(Q):
    a = list(input().split())
    if len(a) == 1:
        rev[i] = 1
    else:
        if a[1] == "1":
            Atama.append([i,a[2],1])
        else:
            Osiri.append([i,a[2],-1])


Rev = []
tmp = 0
r = sum(rev)
for i in range(len(rev)):
    r -= rev[i]
    Rev.append(r)

for i in range(len(Atama)):
    Atama[i][2] *= (-1)**Rev[Atama[i][0]]
for i in range(len(Osiri)):
    Osiri[i][2] *= (-1)**Rev[Osiri[i][0]]

Ans = Atama+Osiri
Ans.sort()

fw = ""
aw = ""
N = len(Ans)
for i in range(N):
    if Ans[i][2] == 1:
        fw += Ans[i][1]
    else:
        aw += Ans[i][1]

if sum(rev)%2 == 1:
    S = S[::-1]
fw = fw[::-1]

print(fw+S+aw)

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

answer = s
s1 = ""
s2 = ""
state = 0


for _ in range(q):
    Q = input()
    if Q == "1":
        if state == 0:
            state = 1
        else:
            state = 0
        continue

    _,F,C = Q.split()
    if F == "1":
        if state == 0:
            s2 = s2 + C
        else:
            s1 = s1 + C
    else:
        if state == 0:
            s1 = s1 + C
        else:
            s2 = s2 + C





if state == 0:
    answer = s2[::-1] + s + s1
else:
    answer = s1[::-1] + s[::-1] + s2



print(answer)

Solution (Python):
S = input()
Q = int(input())
query = []
for i in range(Q):
    query.append(list(map(str, input().split())))


def start(S, x):
    S = x + S
    return S


def end(S, x):
    S = S + x
    return S


front = ''
back = ''
flg = True
for q in query:
    if len(q) == 1:
        flg = not flg
    else:
        if flg == True:
            if q[1] == '1':
                front = start(front, q[2])
            else:
                back = end(back, q[2])
        else:
            if q[1] == '1':
                back = end(back, q[2])
            else:
                front = start(front, q[2])
else:
    if flg == True:
        print(front + S + back)
    else:
        print(back[::-1] + S[::-1] + front[::-1])

Solution (Python):
import sys, bisect, math, itertools, string, queue, copy


from collections import Counter,defaultdict,deque
from itertools import permutations, combinations
from heapq import heappop, heappush

sys.setrecursionlimit(10**8)
mod = 10**9+7
def inp(): return int(input())
def inpm(): return map(int,input().split())
def inpl(): return list(map(int, input().split()))
def inpls(): return list(input().split())
def inplm(n): return list(int(input()) for _ in range(n))
def inplL(n): return [list(input()) for _ in range(n)]
def inplT(n): return [tuple(input()) for _ in range(n)]
def inpll(n): return [list(map(int, input().split())) for _ in range(n)]
def inplls(n): return sorted([list(map(int, input().split())) for _ in range(n)])

def main():
    s = str(input())
    q = inp()
    before = []
    after = []
    flag = True
    for _ in range(q):
        query = inpls()
        if query[0] == '1':
            if flag:
                flag = False
            else:
                flag = True
        else:
            f = query[1]
            S = query[2]
            if (flag and f=='2') or ((not flag) and f=='1'):
                after.append(S)
            else:
                before.append(S)
    ans = []
    for i in range(len(before)-1,-1,-1):
        ans.append(before[i])
    s = list(s)
    for i in range(len(s)):
        ans.append(s[i])
    for i in range(len(after)):
        ans.append(after[i])
    if not flag:
        ans.reverse()
    print(''.join(ans))
    
if __name__ == "__main__":
    main()

Solution (Python):
from collections import deque
s=input()
q=int(input())
Q=[input().split() for _ in range(q)]
word_l,word_r=deque([]),deque([])

flag=1
for i in Q:
    if i[0]=="1" and flag==1:
        flag=0
    elif i[0]=="1" and flag==0:
        flag=1

    elif i[0]=="2":
        num,f,c=i
        if f=="1" and flag==1:
            word_l.appendleft(c)
        elif f=="1" and flag==0:
            word_r.append(c)
        elif f=="2" and flag==1:
            word_r.append(c)
        elif f=="2" and flag==0:
            word_l.appendleft(c)

word_r="".join(word_r)
word_l="".join(word_l)
if flag==1:
    print(word_l+s+word_r)
else:
    print(word_r[::-1]+s[::-1]+word_l[::-1])

Solution (Python):
from collections import deque

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

is_reversed = False

S = deque(S)

for _ in range(Q):
    q = input().split(" ")
    if q[0] == "1":
        is_reversed = not is_reversed
    else:
        if q[1] == "1":
            if is_reversed:
                S.append(q[2])
            else:
                S.appendleft(q[2])
        else:
            if is_reversed:
                S.appendleft(q[2])
            else:
                S.append(q[2])

S = list(S)
if is_reversed:
    S = ''.join(list(reversed(S)))
else:
    S = ''.join((S))

print(S)

Solution (Python):
from collections import deque

S = deque(input().strip())
q = int(input())

rev = False
for _ in range(q):
    query = input().split()
    if query[0] == '1':
        rev = not rev
    else:
        if (query[1] == '1') ^ rev:
            S.appendleft(query[2])
        else:
            S.append(query[2])
S = S if not rev else reversed(S)
print(*S, sep='')

Solution (Python):

s = input()
q = int(input())
b=0
f,l="",""
for _ in range(q):
    qi = input().split(' ')
    
    if(len(qi)==1):
        b=b^1
    else:
        fi = int(qi[1])
        if(fi==1):
            if(b==0):
                f=f+qi[2]
            else:
                l=l+qi[2]
        elif(fi==2):
            if(b==0):
                l=l+qi[2]
            else:
                f=f+qi[2]
s = f[::-1]+s+l
if(b==1):
    s=s[::-1]

print(s)

Solution (Python):
s = input()
q = int(input())
ansList = [""] * (2 * 10 ** 5 * 2 + 1)
last = 2 * 10 ** 5
first = 2 * 10 ** 5 - 1
for x in range(len(s)):
    ansList[last] = s[x]
    last += 1
isReverce = False
for x in range(q):
    query = list(map(str, input().split()))
    if query[0] == "1": isReverce = not isReverce
    else:
        if (query[1] == "1" and isReverce == False) or (query[1] == "2" and isReverce == True):
            ansList[first] = query[2]
            first -= 1
        else:
            ansList[last] = query[2]
            last += 1
ans = "".join(ansList)
if isReverce == False: print(ans)
else: print(ans[::-1])

Solution (Python):
import math

S = input().strip()
Q = int(input().strip())
order = 1
tmp = 'T'

for i in range(Q):
  q = input().strip().split()
  
  if q[0] == '1':
    
    
    order *= -1
  else:
    F = q[1]
    if F == '1':
      if order == 1:
        
        
        tmp = q[2] + tmp
      else:
        
        
        tmp = tmp + q[2]
    else:
      if order == 1:
        
        
        tmp = tmp + q[2]
      else:
        
        
        tmp = q[2] + tmp

S = tmp.replace('T', S)        
print(S[::order])

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

X=[]
Y=[]
f=1
for _ in range(Q):
    A=list(input().split())
    if A[0]=="1":
        f*=-1
    if A[0]=="2":
        if A[1]=="1":
            if f==1:
                X.append(A[2])
            if f==-1:
                Y.append(A[2])
        if A[1]=="2":
            if f==-1:
                X.append(A[2])
            if f==1:
                Y.append(A[2])

x="".join(X)
x="".join(list(reversed(x)))
y="".join(Y)

a=[x,S,y]
b="".join(a)

if f==-1:
    b="".join(list(reversed(b)))

print(b)

Solution (Python):
from collections import deque

S = [l for l in input()]
Q = int(input())
d = deque(S)
direction = 'f'

for i in range(Q):
    query = input()
    if query[0] == '1':
        if direction == 'f':
            direction = 'r'
        else :
            direction = 'f'
    else :
        t, f, c = query.split()
        if f == '1':
            if direction == 'f':
                d.appendleft(c)
            else :
                d.append(c)
        else :
            if direction == 'f':
                d.append(c)
            else :
                d.appendleft(c)

if direction == 'r':
    d.reverse()

print(''.join(d))

Solution (Python):
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
 
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
 
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

s = input()
s = deque(s)
q = INT()

r_count = 0
L = [S_LIST() for i in range(q)]
for i in range(q):
    if L[i][0] == "1":
        r_count ^= 1
        
    else:
        query, sent = int(L[i][1]), str(L[i][2])
        if r_count == 0:
            if query == 1:
                s.appendleft(sent)
            else:
                s.append(sent)
        else:
            if query == 2:
                s.appendleft(sent)
            else:
                s.append(sent)

if r_count != 0:
    s.reverse()
print("".join(list(s)))

Solution (Python):
import sys







import collections


mod = int(1e9+7)
INF = 1<<29

def main():
    s = collections.deque(list(input()))
    q = int(input())
    count = 0
    for i in range(q):
        tfc = list(input().split())
        if tfc[0]=='1':
            count = abs(count - 1)
        else:
            t,f,c = tfc
            if count:
                if f=='1':
                    s.append(c)
                else:
                    s.appendleft(c)
            else:
                if f=='1':
                    s.appendleft(c)
                else:
                    s.append(c)
    if count:
        t = []
        for i in range(len(s)):
            t.append(s.pop())
        print(''.join(t))
    else:
        print(''.join(s))
    return

if __name__=='__main__':
    main()

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

S_front, S_back = [], []
reverse = False

for i in range(Q):
    query = input().split()
    if query[0] == "1":
        reverse = not reverse
    else:
        T = False if (query[1] == "1") else True
        if reverse ^ T is False:
            S_front += query[2]
        else:
            S_back += query[2]

if reverse is True:
    S = S[::-1]
    ans = "".join(S_back)[::-1] + S + "".join(S_front)
else:
    ans = "".join(S_front)[::-1] + S + "".join(S_back)
print(ans)

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

cnt = 0
left = []
right = []
for j in range(q):
    a = [str(i) for i in input().split()]
    if a[0] == '1':
        cnt += 1
    else:
        if (a[1] == '1' and cnt % 2 == 0) or (a[1] == '2' and cnt % 2 == 1):
            right.append(a[2])
        else:
            left.append(a[2])

if cnt % 2 == 0:
    print(''.join(reversed(right)) + s + ''.join(left))
else:
    print(''.join(reversed(left)) + ''.join(list(reversed(s))) + ''.join(right))

Solution (Python):
S = input()
Q = int(input())
top = ''
end = ''
reverse_flag = 1
for _ in range(Q):
    query = input()
    if query == '1':
        reverse_flag *= -1
    else:
        _, F, C = query.split()
        if (int(F)-1.5) * reverse_flag > 0:
            end += C
        else:
            top += C
ans = top[::-1] + S + end
if reverse_flag == 1:
    print(ans)
else:
    print(ans[::-1])

Solution (Python):
S=input()
Q=int(input())
c=0
left=""
right=""
for i in range(Q):
    T=list(map(str,input().split()))
    if len(T)==1:
        c+=1
    else:
        if (T[1]=="1" and c%2==0) or (T[1]=="2" and c%2==1):
            left+=T[2]
        else:
            right+=T[2]

if c%2==0:
    ans=left[::-1]+S+right
else:
    ans=right[::-1]+S[::-1]+left
print(ans)

Solution (Python):
a = input()

q = int(input())

queries = [list(input().split()) for i in range(q)]

revers = False

head = []

tail = []

for query in queries:
    if len(query) == 1:
        revers = not revers
    else:
        command, f, chara = query
        if f == '1':
            if not revers:
                head.append(chara)
            else:
                tail.append(chara)
        else:
            if not revers:
                tail.append(chara)
            else:
                head.append(chara)







if revers:
    tail.reverse()
    print(''.join(tail) + a[::-1] + ''.join(head))
else:
    head.reverse()
    print(''.join(head) + a + ''.join(tail))

Solution (Python):
if __name__ == '__main__':
    s = input()
    q = int(input())

    pref = []
    suff = []
    rev = False

    for i in range(q):
        curr = [c for c in input().split()]
        if curr[0] == '1':
            rev = not rev
        else:
            if curr[1] == '1':
                if not rev:
                    pref.append(curr[2])
                else:
                    suff.append(curr[2])
            else:
                if not rev:
                    suff.append(curr[2])
                else:
                    pref.append(curr[2])

    res = "{}{}{}".format("".join(pref[::-1]), s, "".join(suff))
    if rev:
        print(res[::-1])
    else:
        print(res)

Solution (Python):



from collections import deque
S = deque(list(input()))
Q = int(input())
queries = []
rv_flag = False
for _ in range(Q):
    q = list(input().split())
    if len(q) == 1:
        rv_flag = not rv_flag
        continue
    _, f, c = q
    if f == '1' and rv_flag:
        S.append(c)
    elif f == '1' and not rv_flag:
        S.appendleft(c)
    elif f == '2' and rv_flag:
        S.appendleft(c)
    elif f == '2' and not rv_flag:
        S.append(c)

ans = ''.join(S)
if rv_flag:
    print(ans[::-1])
else:
    print(ans)

Solution (Python):
from collections import deque

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

is_forward = True
for query in queries:
    if "1" == query[0]:
        is_forward = not is_forward
    else:
        if is_forward:
            if "1" == query[1]:
                s.appendleft(query[2])
            else:
                s.append(query[2])
        else:
            if "1" == query[1]:
                s.append(query[2])
            else:
                s.appendleft(query[2])
if not is_forward:
    s.reverse()
print("".join(s))

Solution (Python):
def main():
	S=input()                                               
	Q=int(input())                                          
	Q_LIST=[input().split() for i in range(Q)]                
	 
	dir = 0 

	S_HEAD = ''
	S_BACK = ''

	for query in Q_LIST:
		if query[0] == '1':
			if dir == 0:
				dir = 1
			else:
				dir = 0
	 
		if dir == 0:
			HEAD = '1'
			BACK = '2'
		else:
			HEAD = '2'
			BACK = '1'
	 
		if query[0] == '2':
			if query[1] == HEAD:
				S_HEAD = query[2] + S_HEAD
			if query[1] == BACK:
				S_BACK =  S_BACK + query[2]

	S = S_HEAD + S + S_BACK

	if dir == 0:
		print(S)
	else:
		print(S[::-1])

if __name__ == '__main__':
    main()

Solution (Python):
import sys
S = input()
Q = int(input())
pin = 0
left = ''
right = ''
for i in range(Q):
  t = list(sys.stdin.readline().split())
  if int(t[0]) == 1:
    pin += 1
  else:
    if int(t[1]) % 2 == pin % 2 :
      right = right + t[2]
    else:
      left = t[2] + left
S = left + S + right
if pin % 2 == 1:
  S = S[::-1]
print(S)

Solution (Python):
def main():
    
    
    
    
    
    
    
    
    
    
    
    
    
    from collections import deque
    deq = deque(input())
    q = int(input())
    r = [input().split() for i in range(q)]
    cnt = 0
    for i in range(q):
        tmp = r[i]
        if tmp[0] == "1":
            cnt += 1
        else:
            if tmp[1] == "1" and cnt % 2 == 0 or tmp[1] != "1" and cnt % 2 != 0:
                deq.appendleft(tmp[2])
            else:
                deq.append(tmp[2])
    if cnt % 2 == 0:
        print(*(deq), sep="")
    else:
        print(*(deque(reversed(deq))), sep="")

if __name__ == '__main__':
    main()