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

Solution (Python):
from sys import stdin

S = list(stdin.readline().rstrip())
Q = int(stdin.readline().rstrip())

b = [""]
f = [""]

t = 0

for i in range(Q):
    T = stdin.readline().rstrip().split()
    if(len(T) == 1):
        t += 1
        continue
    else :
        F,C = int(T[1]),T[2]

    if t%2 == 0: 
        if F == 1:
            b.append(C)
        else :
            f.append(C)
    else: 
        if F == 1:
            f.append(C)
        else :
            b.append(C)



if(t%2 == 0):
    print(''.join(b[::-1] + S + f))
else:
    print(''.join(f[::-1] + S[::-1] + b))

Solution (Python):
from sys import stdin, setrecursionlimit


def main():
    input = stdin.readline
    s = list(input()[:-1])
    Q = int(input())
    reverse = False
    before = []
    after = []
    for _ in range(Q):
        q = list(map(str, input().split()))
        t = q[0]
        if t == '1':
            reverse = not (reverse)
        else:
            if reverse ^ (q[1] == '1'):
                before.append(q[2])
            else:
                after.append(q[2])
    if reverse:
        after.reverse()
        s.reverse()
        print(''.join(after + s + before))
    else:
        before.reverse()
        print(''.join(before + s + after))


if __name__ == "__main__":
    setrecursionlimit(10000)
    main()

Solution (Python):
from sys import stdin, setrecursionlimit


def main():
    input = stdin.readline
    s = list(input()[:-1])
    Q = int(input())
    reverse = False
    before = []
    after = []
    for _ in range(Q):
        q = list(map(str, input().split()))
        t = q[0]
        if t == '1':
            reverse = not (reverse)
        else:
            if reverse ^ (q[1] == '1'):
                before.append(q[2])
            else:
                after.append(q[2])
    if reverse:
        after.reverse()
        s.reverse()
        print(''.join(after + s + before))
    else:
        before.reverse()
        print(''.join(before + s + after))


if __name__ == "__main__":
    setrecursionlimit(10000)
    main()

Solution (Python):
def main():
    s=input()
    q=int(input())
    queryy=[]
    sent="a"
    back="a"
    for i in range(q):
        queryy.append(tuple(input().split()))
    check=-1
    for query in queryy:
    
        if query[0]=="1":
            check*=-1
        else:
            if query[1]=="1":
                if check==-1:
                    sent+=str(query[2])
                   
                else:
                    back+=str(query[2])
                   
                    
            else:
                if check==-1:
                    back+=str(query[2])
                    
                else:
                    sent+=str(query[2])
                    
   
    
     
    sent=sent[1:]
    sent=sent[::-1]   
    back=back[1:]

    if check==1:
        s=sent+s+back        
        print(s[::-1]) 
    else:
        s=sent+s+back
        print(s)

if __name__=="__main__":
    main()

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

front_s = ''
back_s = ''
rev_flg = False

for i in range(q):
    query = list(map(str, input().split())) 
    if query[0] == '1':
        rev_flg = not rev_flg
    else:
        if not rev_flg:
            if query[1] == '1':
                front_s += query[2]
            else:
                back_s += query[2]
        else:
            if query[1] == '1':
                back_s += query[2]
            else:
                front_s += query[2]
    
res = front_s[::-1] + s + back_s
if rev_flg: res = res[::-1]
print(res)

Solution (Python):
s=input()
l=len(s)
q=int(input())
cnt=1
l=str()
r=str()

for _ in range(q):
	a=list(input().split())
	if a[0]=="1":
		cnt*=(-1)
	else:
		if int(a[1])*cnt==1 or int(a[1])*cnt==-2:
			l+=a[2]
		else:
			r+=a[2]				

s=l[::-1]+s+r
if cnt==-1:
	s=s[::-1] 	

print(s)

Solution (Python):


s = input()
q = int(input())
ht = 1
head = ''
tail = ''
count = 0
for i in range(q):
    txt3 = input().split()
    if txt3[0] == '1':
        ht = 3 - ht
        count += 1
    else:
        if int(txt3[1]) == ht:
            head = txt3[2] + head
        else:
            tail = tail + txt3[2]
s = head + s + tail
if count % 2 == 1:
    s = s[::-1]
print(s)

Solution (Python):
from collections import deque

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

t = deque(S)
reverse = False
for _ in range(Q):
    query = input()
    if query[0] == '1':
        reverse = not reverse
    elif query[0] == '2':
        _, F, C = query.split()
        if F == '1':
            if reverse:
                t.append(C)
            else:
                t.appendleft(C)
        elif F == '2':
            if reverse:
                t.appendleft(C)
            else:
                t.append(C)
result = ''.join(t)
if reverse:
    result = result[::-1]
print(result)

Solution (Python):
from collections import deque

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

t = deque(S)
reverse = False
for _ in range(Q):
    query = input()
    if query[0] == '1':
        reverse = not reverse
    elif query[0] == '2':
        _, F, C = query.split()
        if F == '1':
            if reverse:
                t.append(C)
            else:
                t.appendleft(C)
        elif F == '2':
            if reverse:
                t.appendleft(C)
            else:
                t.append(C)
result = ''.join(t)
if reverse:
    result = result[::-1]
print(result)

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

isR = -1
for i in range(q):
    line = list(input().split())
    t = int(line[0])
    if t == 1:
        isR *= -1
    else:
        f = int(line[1])
        if isR + f == 0 or isR + f == 3:
            s.appendleft(line[2])
        else:
            s.append(line[2])
    if i == q-1 and isR == 1:
        s.reverse()
print("".join(s))

Solution (Python):
from collections import deque
s = deque(list(input()))
q = int(input())
query = [[1, 0, ''] for i in range(q)]
for i in range(q):
    tmp = input().split()
    if tmp[0] == '2':
        query[i][0] = int(tmp[0])
        query[i][1] = int(tmp[1])
        query[i][2] = tmp[2]

dire = 1
for i in range(q):
    if query[i][0] == 1:
        dire *= -1
    else:
        if (2*query[i][1]-3) * dire == 1:
            s.append(query[i][2])
        else:
            s.appendleft(query[i][2])

ans = ''.join(s)
if dire == 1:
    print(ans)
else:
    print(ans[::-1])

Solution (Python):
from collections import deque
S=deque(input())
Q=int(input())
re=True
cnt=0
for i in range(Q):
    q=list(map(str,input().split()))
    if q[0]=='1':
        cnt+=1
        if re==True:
            re=False
        else:
            re=True
    else:
        if re==True:
            if q[1]=='1':
                S.appendleft(q[2])
            else:
                S.append(q[2])
        else:
            if q[1]=='2':
                S.appendleft(q[2])
            else:
                S.append(q[2])
if cnt%2==1:
    S.reverse()
print(*S,sep='')

Solution (Python):

s = list(input())
n = int(input())

flag = 0
s1 = []
s2 = s
for i in range(n):
    Q = input()
    if Q[0] == "2":
        if Q[2] == "1":
            if flag == 1:
                s2.append(Q[4])
            else:
                s1.append(Q[4])
        else:
            if flag == 0:
                s2.append(Q[4])
            else:
                s1.append(Q[4])
    else:
        flag ^= 1
s2 = s1[::-1] + s2
print("".join(s2) if flag == 0 else "".join(s2[::-1]))

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

query = [[x for x in input().split()] for i in range(q)]

T2query = []
begin = []
end = []
cnt=0
for i in range(q-1,-1,-1):
    if(query[i][0]=='1'):
        cnt+=1
        continue
    if(cnt%2==1):
        if(query[i][1]=='1'):
            end.append(query[i][2])
        else:
            begin.append(query[i][2])
    else:
        if(query[i][1]=='1'):
            begin.append(query[i][2])
        else:
            end.append(query[i][2])
wb = ''.join(begin)
we = ''.join(end)
we = we[::-1] 

if(cnt%2!=0):
    s = s[::-1]

s = wb + s + we

print(s)

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

direction = 1
head = []
tail = []
for i in range(Q):
    T = input()
    if T == '1':
        direction = direction * -1
    else:
        T = T.split()
        if T[1] == '1':
            if direction == 1:
                head.append(T[2])
            else:
                tail.append(T[2])
        else:
            if direction == 1:
                tail.append(T[2])
            else:
                head.append(T[2])

head = head[::(-1)]
head.append(S)
head.extend(tail)
S = "".join(head)[::direction]

print(S)

Solution (Python):
s=input()
q=int(input())
que=[]
count=0
left=[]
right=[]
for i in range(q):
  qu=list(map(str,input().split()))
  if qu[0]=="1":
    count+=1
  que.append(qu)
han=0
for i in range(q):
  if que[i][0]=="1":
    han+=1
  else:
    if que[i][1]=="1":  
      if (count-han)%2==0:
        left.append(que[i][2])
      else:
        right.append(que[i][2])
    elif que[i][1]=="2":  
      if (count-han)%2==0:
        right.append(que[i][2])
      else:
        left.append(que[i][2])
ls="".join(left[::-1])
rs="".join(right)
if count%2==0:
  print(ls+s+rs)
else:
  print(ls+s[::-1]+rs)

Solution (Python):
from collections import deque
queue = deque(input())
q = int(input())
li = [list(input().split()) for _ in range(q)]

flag = 1
for i in range(q):
    if int(li[i][0]) == 1:
        flag *= -1
        continue

    if int(li[i][1]) == 1:
        if flag == 1:
            queue.appendleft(li[i][2])
        else:
            queue.append(li[i][2])
    else:
        if flag == 1:
            queue.append(li[i][2])
        else:
            queue.appendleft(li[i][2])

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

Solution (Python):



'''
A=[]
B=[]
for i in range():
    a, b = map(int, input().split())
    A.append(a)
    B.append(b)'''


def main():
    import sys
    input = sys.stdin.readline
    s = input().rstrip("\n")
    q = int(input())
    
    qq = [input().split() for i in range(q)]
    cnt=0
    r=""
    l=""
    
    for que in qq:
        if que[0] == "1":
            
            r,l=l,r
            cnt+=1
            
        else:
            if que[1] == "1":
                l+=que[2]
            else:
                r+=que[2]
            
    
    if cnt%2==0:
        print(l[::-1]+s+r)
    else:
        print(l[::-1]+s[::-1]+r)



if __name__ == '__main__':
    main()

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

cnt = [0]*(q+5)
arr=[]
l=0
r=0

for i in range(q):
    t=input().split()
    if t[0]=='1':
        r+=1
        cnt[0]+=1 
        cnt[l]-=1 
    else:
        arr.append([t[2],int(t[1])])
        l+=1 
if r%2==1:
    s=s[::-1]
sm=0
for i in range(len(arr)):
    sm+=cnt[i]
    if sm%2==1:
        if arr[i][1]==1:arr[i][1]=2
        elif arr[i][1]==2:arr[i][1]=1 
ans1=""
ans2=""
for i in range(len(arr)):
    if arr[i][1]==1:
        ans1+=arr[i][0]

    else:ans2+=arr[i][0]
print("%s%s%s"%(ans1[::-1],s,ans2))

Solution (Python):
from collections import deque
S=deque(list(input()))
Q=int(input())
ju=True
for _ in range(Q):
  l=list(input().split())
  if l[0]=="1":
    if ju:
      ju=False
    else:
      ju=True
  else:
    if ju:
      if l[1]=="1":
        S.appendleft(l[2])
      else:
        S.append(l[2])
    else:
      if l[1]=="1":
        S.append(l[2])
      else:
        S.appendleft(l[2])
if ju:
  while S:
    t=S.popleft()
    print(t,end="")
else:
  while S:
    t=S.pop()
    print(t,end="")

Solution (Python):
from collections import deque
s = input()
q = int(input())
right_side = deque()
left_side = deque()
is_reversed = False
for i in range(q):
    l = list(input().split())
    if l[0] == '1':
        is_reversed = not is_reversed
    else:
        if l[1] == '2':
            if is_reversed == False:
                right_side.append(l[2])
            else:
                left_side.appendleft(l[2])
        else:
            if is_reversed == False:
                left_side.appendleft(l[2])
            else:
                right_side.append(l[2])
ans = ''.join([''.join(left_side),s ,''.join(right_side)])
if is_reversed :
    ans = ans[::-1]
print(ans)

Solution (Python):
import sys
from collections import deque

input = sys.stdin.readline


def main():
    S = input().rstrip()
    Q = int(input())

    prefix = deque()
    surfix = deque()
    prefix_append = prefix.appendleft
    surfix_append = surfix.append
    is_reversed = False
    for _ in range(Q):
        q = input().split()
        if q[0] == "1":
            is_reversed = not is_reversed
        else:
            if q[1] == "1":
                if is_reversed:
                    surfix_append(q[2])
                else:
                    prefix_append(q[2])
            else:
                if is_reversed:
                    prefix_append(q[2])
                else:
                    surfix_append(q[2])

    ans = "".join(["".join(prefix), S, "".join(surfix)])
    if is_reversed:
        ans = ans[::-1]

    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
import sys
from collections import deque

input = sys.stdin.readline


def main():
    S = input().rstrip()
    Q = int(input())
    query = [None] * Q
    for i in range(Q):
        query[i] = input().split()

    prefix = deque()
    surfix = deque()
    prefix_append = prefix.appendleft
    surfix_append = surfix.append
    is_reversed = False
    for q in query:
        if q[0] == "1":
            is_reversed = not is_reversed
        else:
            F, C = q[1], q[2]
            if F == "1":
                if is_reversed:
                    surfix_append(C)
                else:
                    prefix_append(C)
            else:
                if is_reversed:
                    prefix_append(C)
                else:
                    surfix_append(C)

    ans = "".join(["".join(prefix), S, "".join(surfix)])
    if is_reversed:
        ans = ans[::-1]

    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
import sys

input = sys.stdin.readline


def main():
    S = input().rstrip()
    Q = int(input())
    query = [None] * Q
    for i in range(Q):
        query[i] = input().split()

    prefix = []
    surfix = []
    prefix_append = prefix.append
    surfix_append = surfix.append
    is_reversed = False
    for q in query:
        if q[0] == "1":
            is_reversed = not is_reversed
        else:
            F, C = q[1], q[2]
            if F == "1":
                if is_reversed:
                    surfix_append(C)
                else:
                    prefix_append(C)
            else:
                if is_reversed:
                    prefix_append(C)
                else:
                    surfix_append(C)

    ans = "".join(["".join(prefix)[::-1], S, "".join(surfix)])
    if is_reversed:
        ans = ans[::-1]

    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
import sys

input = sys.stdin.readline


def main():
    S = input().rstrip()
    Q = int(input())
    query = [None] * Q
    for i in range(Q):
        query[i] = input().split()

    prefix = []
    surfix = []
    is_reversed = False
    for q in query:
        if q[0] == "1":
            is_reversed = not is_reversed
        else:
            F, C = q[1], q[2]
            if F == "1":
                if is_reversed:
                    surfix.append(C)
                else:
                    prefix.append(C)
            else:
                if is_reversed:
                    prefix.append(C)
                else:
                    surfix.append(C)

    ans = "".join(["".join(prefix)[::-1], S, "".join(surfix)])
    if is_reversed:
        ans = ans[::-1]

    print(ans)


if __name__ == "__main__":
    main()

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

    reverse = False
    pre = ""
    post = ""
    for _ in range(q):
        line_ls = input().split()
        
        if len(line_ls) == 1:
            reverse = not reverse
            continue

        fi = line_ls[1]
        ci = line_ls[2]
        if fi == "1":
            if not reverse:
                pre += ci
            else:
                post += ci
        else:
            if not reverse:
                post += ci
            else:
                pre += ci

    if reverse:
        s = s[::-1]
        post = post[::-1]
        print(post + s + pre)
    else:
        pre = pre[::-1]
        print(pre + s + post)


main()

Solution (Python):
from collections import deque
queue = deque(input())
q = int(input())
li = [list(input().split()) for _ in range(q)]

flag = 1
for i in range(q):
    if int(li[i][0]) == 1:
        flag *= -1
        continue

    if int(li[i][1]) == 1:
        if flag == 1:
            queue.appendleft(li[i][2])
        else:
            queue.append(li[i][2])
    else:
        if flag == 1:
            queue.append(li[i][2])
        else:
            queue.appendleft(li[i][2])

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

Solution (Python):
from collections import deque
def myAnswer(S:deque,Q:int,querys:list) -> str:
   setF = {1,2}
   reverse = False
   for query in querys:
      command = int(query[0])
      if(command == 1):
         reverse = not reverse
      else:
         F = int(query[1])
         C = query[2]
         if(reverse):
            F = set(setF - {F}).pop()
         S.appendleft(C) if(F == 1) else S.append(C)

   if(reverse):S.reverse()
   return "".join(S)

def modelAnswer():
   return
def main():
   S = deque(input())
   Q = int(input())
   querys = [list(map(str,input().split())) for _ in range(Q)]
   print(myAnswer(S,Q,querys))



if __name__ == '__main__':
   main()

Solution (Python):

from collections import deque

class abc_158:
    
    def __init__(self):
        pass
    
    def A(self):
        S=input()
        if "BA" in S or "AB" in S:print("Yes")
        else:print("No")
    
    def B(self):
        NAB=list(map(int,input().split()))
        
        if NAB[0]<=NAB[1]:ans=NAB[0]
        elif NAB[0]<=NAB[1]+NAB[2]:ans=NAB[1]
        else:
            tail = NAB[0]%(NAB[1]+NAB[2]) if NAB[0]%(NAB[1]+NAB[2])<NAB[1] else NAB[1]
            ans=(int(NAB[0]/(NAB[1]+NAB[2]))*NAB[1])+tail
            
        print(int(ans))
            
    def C(self):
        AB=list(map(int,input().split()))
        ans=-1
        
        
        for i in range(1,1001):
            if AB[0]==int(i*0.08) and AB[1]==int(i*0.1):
                ans=i
                break
            else:continue
        print(ans)
            
    def D(self):
        S=input()
        Q=int(input())
        Q_list=[list(input().split()) for i in range(Q)]
        d=deque()
        top=True
        end=False
        count_revers=0
        
        d.append(S)
        for i in Q_list:
            if int(i[0])==1:
                count_revers+=1
                if top==True:
                    top=False
                    end=True
                else:
                    top=True
                    end=False
            else:
                if int(i[1])==1 :
                    if top==True:d.appendleft(i[2])
                    else:d.append(i[2])
                else :
                    if top==True:d.append(i[2])
                    else:d.appendleft(i[2])
                
        ans = "".join(list(d)) if count_revers%2==0 else  "".join(list(d))[::-1]
        print(ans)
        
    def E(self):
        pass
    
    def F(self):
        pass
    
    
if __name__=='__main__':
    obj=abc_158()
    obj.D()

Solution (Python):
string = input()
n = int(input())

tail = ''
head = ''
reverted = False

for _ in range(n):
    q = input().split()
    if q[0] == '1':
        reverted = not reverted
    else:
        if not reverted:
            if q[1] == '1':
                tail = q[2] + tail
            
            elif q[1] == '2':
                head  = head + q[2]
        
        elif reverted:
            if q[1] == '1':
                head  = head + q[2]
            elif q[1] == '2':
                tail = q[2] + tail

ans = tail+string+head
if not reverted:
    print(ans)
else:
    print(ans[::-1])

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

from collections import deque

flip = False
deq = deque(s)

for _ in range(q):
    query = input().split(' ')
    if len(query)==1:
        flip = not flip
    else:
        f,c = int(query[1])-1,query[2]
        if flip ^ f:
            deq.append(c)
        else:
            deq.appendleft(c)

if flip:
    deq.reverse()

res =""
for c in deq:
    res+=c

print(res)

Solution (Python):
from collections import deque

S = deque(input())
Q = int(input())
Flag = 1

for _ in range(Q):
  q = input()
  
  if q[0] == "1":
    Flag *= -1
  
  if q[0] == "2":
    if Flag > 0:
      if q[2] == "1":
        S.appendleft(q[4])
      else:
        S.append(q[4])
    else:
      if q[2] == "1":
        S.append(q[4])
      else:
        S.appendleft(q[4])
  
if Flag < 0:
  S.reverse()

print("".join(S))

Solution (Python):
import sys
import math
from decimal import Decimal, ROUND_HALF_UP, ROUND_HALF_EVEN
from collections import deque
from bisect import bisect_left
from itertools import product
def I(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LI2(N): return [list(map(int, sys.stdin.readline().split())) for i in range(N)]
def LSI(): return list(map(int, list(sys.stdin.readline().rstrip())))
def LSI2(N): return [list(map(int, list(sys.stdin.readline().rstrip()))) for i in range(N)]
def S(): return sys.stdin.readline().rstrip()
def LS(): return sys.stdin.readline().split()
def LS2(N): return [sys.stdin.readline().split() for i in range(N)]
def FILL(i,h): return [i for j in range(h)]
def FILL2(i,h,w): return [FILL(i,w) for j in range(h)]
def FILL3(i,h,w,d): return [FILL2(i,w,d) for j in range(h)]
def FILL4(i,h,w,d,d2): return [FILL3(i,w,d,d2) for j in range(h)]
def sisha(num,digit): return Decimal(str(num)).quantize(Decimal(digit),rounding=ROUND_HALF_UP)

MOD = 998244353
INF = float("inf")
sys.setrecursionlimit(10**6+10)
input = sys.stdin.readline
S = S()
S = [S[i] for i in range(len(S))]
QS= deque(S)
S = QS
Q = I()
rev = False
for i in range(Q):
    q = LS()
    if q[0] == '1':
        rev = not rev
        continue
    if (q[1] == '1') ^ rev:
        S.appendleft(q[2])
    else:
        S.append(q[2])
if rev:
    ans = ''.join(reversed(S))
else:
    ans = ''.join(S)

print(ans)

Solution (Python):
from collections import deque

def check(reverse_cnt, t):

    if reverse_cnt%2 == 0 and t == 1:
        return True
    elif reverse_cnt%2 == 0 and t == 2:
        return False
    elif reverse_cnt%2 == 1 and t == 1:
        return False
    elif reverse_cnt%2 == 1 and t == 2:
        return True

def solve(s, q, qs):
    reverse_cnt = 0

    for item in qs:

        if len(item) == 1:
            
            reverse_cnt += 1
        else:
            

            if check(reverse_cnt, int(item[1])):
                
                s.appendleft(item[2])

            else:
                
                s.append(item[2])


    if reverse_cnt%2 == 1:
        ans = "".join(s)
        ans = ans[::-1]
    else:
        ans = "".join(s)

    return ans

if __name__ == '__main__':

    s = deque() 

    str = input()
    q = int(input())
    qs = [list(input().split()) for i in range(q)]

    s.append(str)

    ans = solve(s, q, qs)
    print(ans)

Solution (Python):

import sys
from collections import deque
S=sys.stdin.readline().strip()
Q=int(sys.stdin.readline().strip())

cnt=0   
deq1=deque()
deq2=deque()

for _ in range(Q):
    t=sys.stdin.readline().split()
    if t[0]=="1": 
        cnt+=1      
    elif t[0]=="2": 
        qtype,front_or_back,c=t[0],int(t[1]),t[2]
        if cnt%2==0:    
            if front_or_back==1:    
                deq1.appendleft(c)
            elif front_or_back==2:  
                deq2.append(c)
        else:   
            if front_or_back==1:    
                deq2.append(c)      
            elif front_or_back==2:  
                deq1.appendleft(c)  

s_deq1=""
for x in deq1:
    s_deq1+=x

s_deq2=""
for x in deq2:
    s_deq2+=x

if cnt%2==0:    
    print s_deq1+S+s_deq2
else:    
    print s_deq2[::-1]+S[::-1]+s_deq1[::-1]

Solution (Python):
s = input()
q = int(input())
r = 0
qn = []*3
left =''
right = ''

for i in range(q):
    qn = input().split()
    if qn[0] == '1':
        r += 1
    elif qn[0] == '2':
        if qn[1] == '1' and r%2 == 0:
            left = qn[2] + left
        elif qn[1] == '1' and r%2 == 1:
            right += qn[2]
        elif qn[1] == '2' and r % 2 == 0:
            right += qn[2]
        elif qn[1] == '2' and r % 2 == 1:
            left = qn[2] + left

ans = left + s + right

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

print(ans)

Solution (Python):
import collections
from functools import reduce

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

deque = collections.deque(S)

is_reverse = False
for _ in range(Q):
    query = input().split()
    if query[0] == '1':
        is_reverse = not is_reverse
    else:
        if (query[1] == '1' and not is_reverse) or (query[1] =='2' and is_reverse):
            deque.appendleft(query[2])
        else:
            deque.append(query[2])
if is_reverse:
    deque.reverse()

res = list(deque)

print("".join(res))

Solution (Python):
from __future__ import print_function

from functools import reduce
from operator import mul
from collections import Counter
from collections import deque
from itertools import accumulate
from queue import Queue
from queue import PriorityQueue as pq
from heapq import heapreplace
from heapq import heapify
from heapq import heappushpop
from heapq import heappop
from heapq import heappush
import heapq
import time
import random
import bisect
import itertools
import collections
from fractions import Fraction
import fractions
import string
import math
import operator
import functools
import copy
import array
import re
import sys
sys.setrecursionlimit(500000)


input = sys.stdin.readline


def eprint(*args, **kwargs):
    print(*args, file=sys.stderr, **kwargs)
    return


















def combinations_count(n, r):
    r = min(r, n - r)
    numer = reduce(mul, range(n, n - r, -1), 1)
    denom = reduce(mul, range(1, r + 1), 1)
    return numer // denom


def main():
    
    s0 = input().strip()
    q = int(input().strip())

    
    stc1 = []
    stc2 = []
    flag = 0

    
    
    
    
    

    
    for i in range(q):
        t, *fc = map(lambda x: int(x) if x.isdecimal() else x, input().strip().split())
        
        

        if t == 2:    
            
            if (fc[0]-1)^flag==1:  
                stc2.append(fc[1])
            else:
                stc1.append(fc[1])
        else:       
            flag = flag ^ 1

    
    if flag == 0:
        ans ="".join(stc1[::-1] + list(s0) + stc2)
    else:
        ans ="".join( stc2[::-1] + list(s0)[::-1] + stc1)
    print(ans)
    
    return


if __name__ == '__main__':
    main()

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

head = ""
tail = ""

reverse = False

for i in range(Q):
    t = input()

    if t[0] == "1":
        reverse = not reverse
    else:
        if t[2] == "1":  
            if(reverse):
                tail = tail + t[4]
            else:
                head = t[4] + head
        else:  
            if(reverse):
                head = t[4] + head
            else:
                tail = tail + t[4]

ans = head + s + tail

if(reverse):
    ans = ans[::-1]

print(ans)

Solution (Python):
S=(input())
Q=int(input())
cnt_re=0
s=''
for i in range(Q):
    q=list(input().split())
    if q[0]=='1':
        cnt_re+=1
    else:
        if (int(q[1])+cnt_re)%2==1:
            s=s+q[2]
        else:
            S=S+q[2]
if cnt_re%2==1:
    print(S[::-1]+s)
else:
    print(s[::-1]+S)

Solution (Python):
s = input()
q = int(input())
count = 0
top = ""
end = ""
ans = s
for i in range(q):
    tmp = input()
    if tmp[0] == "1":
        count += 1
    else:
        _, f, c = map(str, tmp.split())
        if (count % 2 == 0 and f == "1") or (count % 2 == 1 and f == "2"):
            top = c + top
        else:
            end = end + c

ans = top + ans + end
ans = ans[::-1] if count % 2 == 1 else ans
print(ans)

Solution (Python):
s = input()
q = int(input())
count = 0
top = ""
end = ""
ans = s
for i in range(q):
    tmp = input()
    if tmp[0] == "1":
        count += 1
    else:
        _, f, c = map(str, tmp.split())
        if count % 2 == 0:
            if f == "1":
                top = c + top
            else:
                end = end + c
        else:
            if f == "1":
                end = end + c
            else:
                top = c + top

ans = top + ans + end
ans = ans[::-1] if count % 2 == 1 else ans
print(ans)

Solution (Python):


from collections import deque

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

que = deque()
for s in S:
    que.append(s)

is_reverse = False

for _ in range(Q):
    query = [q for q in input().split()]
    t = int(query[0])
    if t == 1:
        is_reverse = not is_reverse
    elif t == 2:
        f, c = int(query[1]), str(query[2])
        if f == 1:
            if not is_reverse:
                que.appendleft(c)
            else:
                que.append(c)
        elif f == 2:
            if not is_reverse:
                que.append(c)
            else:
                que.appendleft(c)

if not is_reverse:
    ans = ''.join(list(que))
else:
    ans = ''.join(reversed(list(que)))

print(ans)