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

Solution (Python):
s=input()
n=int(input())
flag=1
left=""
right=""
for i in range(n):
    l=list(input().split())
    if len(l)==1:
        flag*=(-1)
    elif l[1]=="1" and flag==1 or l[1]=="2" and flag==-1:
        left+=l[2]
    else:right+=l[2]
s=left[::-1]+s+right
if flag==-1:print(s[::-1])
else:print(s)

Solution (Python):
from collections import deque
S=deque(input())
ans=""
Q=int(input())
tmp=0
r=[]
l=[]

for i in range(Q):
    que=input().split()
    if que[0]=='1':
        tmp=1-tmp
    if que[0]=='2':
        if que[1]=='1':
            if tmp==0:
                
                
                
                S.appendleft(que[2])
            else:
                
                
                
                S.append(que[2])
        else:
            if tmp==0:
                
                
                
                S.append(que[2])
            else:
                
                
                
                S.appendleft(que[2])

"""
for i in range(Q):
    if que[i][0]=='1':
        tmp=1-tmp
    if que[i][0]=='2':
        if que[i][1]=='1':
            if tmp==0:
                S=que[i][2]+S
            else:
                S=S+que[i][2]
        else:
            if tmp==0:
                S=S+que[i][2]
            else:
                S=que[i][2]+S
"""

"""
for i in range(len(r)):
    S=S+r[i]
    ans=r[i]+ans
for i in range(len(l)):
    S=l[i]+S
    ans=ans+l[i]
"""
if tmp==1:
    print("".join(reversed(S)))
    exit()

print("".join(S))

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

s=list(input2)
Q=int(input())
direction=0
d=deque(s)
d.pop()

for i in range(Q):
    tmp=list(input())
    if tmp[0] == "1":
        if direction == 0:
            direction = 1
        else:
            direction = 0
    else:
        if tmp[2] == "1":
            if direction == 0:
                d.appendleft(tmp[4])
            else:
                d.append(tmp[4])
        else:
            if direction == 0:
                d.append(tmp[4])
            else:
                d.appendleft(tmp[4])
if direction==1:
    d.reverse()
ans=list(d)
answer="".join(ans)
print(answer)

Solution (Python):
from collections import deque

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

l_q = []
for i in range(q):
  l_q.append( list(input().split())  ) 


c = 0
for i in l_q:

  if i[0] == "1":
    c = (c+1)%2

  else:
    if (c+int(i[1]))%2 ==1:
      s.appendleft(i[2])
    else:
      s.append(i[2])

s=list(s)

if c == 1:
  s.reverse()

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

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

flip=0
fr=''
ba=''
for i in range(Q):
    Qq=input()
    
    if(Qq.count(' ')>0):
        ti,fi,ci=Qq.split()
        if( flip%2==0   ):
            if(fi=='1'):
                fr=str(ci)+fr
            else:
                ba=ba+str(ci)
        else:
            if(fi=='1'):
                ba=ba+str(ci)
            else:
                fr=str(ci)+fr
    else:
        flip+=1
if(flip%2==0):
    print(fr+S+ba)
else:
    print(ba[::-1]+S[::-1]+fr[::-1])

Solution (Python):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**6)


def main():
    s = str(input())
    q = int(input())
    s = s.strip()
    a = [''] * (10**5*2)
    a.extend(list(s))
    a.extend([''] * (10**5*2))
    start = 10**5*2
    end = start + len(s) - 1
    frip = False
    for i in range(q):
        query = input().split()
        if query[0] == '1':
            frip = not frip
        elif query[0] == '2':
            if (query[1] == '1' and frip == False) or (query[1] == '2' and frip == True):
                a[start-1] = query[2]
                start -= 1
            else:
                a[end+1] = query[2]
                end += 1
    if frip:
        a.reverse()
        tmp = end
        end = len(a)-start-1
        start = len(a)-tmp-1
    print(''.join(a[start:end+1]))


main()

Solution (Python):

import sys
from collections import deque
 
ans = str(input())
n = int(input())
r = 2
be=deque()
af=deque()
 
for i in range(n) :
    x = input().split(" ")
    if int(x[0]) == 1 :
        r = r + 1
    else :
        pp = int(x[1])
        if r % 2 == 1 :
            pp = pp + 1
        if pp % 2  == 1 :
            be.appendleft(str(x[2]))
        else :
            af.append(str(x[2]))

ans = ''.join(be) + ans + ''.join(af)
if r % 2 == 1 :
    ans = ans[::-1]

print(ans)

Solution (Python):

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



queries = []
for i in range(Q):
    query = input()
    queries.append(query)

from collections import deque
slist = deque(list(S))
isReversed = False
for query in queries:
    if query == "1":
        isReversed = not isReversed
    else:
        t, f, c = query.split()
        if f == '1':
            if isReversed:
                slist.append(c)
            else:
                slist.appendleft(c)
        else:
            if isReversed:
                slist.appendleft(c)
            else:
                slist.append(c)

if isReversed:
    slist.reverse()
print("".join(slist))

Solution (Python):

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



queries = []
for i in range(Q):
    query = input()
    queries.append(query)

from collections import deque
slist = deque(list(S))
isReversed = False
for query in queries:
    if query == "1":
        isReversed = not isReversed
    else:
        t, f, c = query.split()
        if f == '1':
            if isReversed:
                slist.append(c)
            else:
                slist.appendleft(c)
        else:
            if isReversed:
                slist.appendleft(c)
            else:
                slist.append(c)

if isReversed:
    slist.reverse()
print(''.join(slist))

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

query = [input() for _ in range(q)]
count = 0
right = ""
left = ""
for qu in query:
    if len(qu.split()) == 1:
        count += 1
        right, left = left, right
        continue
    num, f, c = qu.split()
    f = int(f)

    if f == 1:
        left += c
        
    else:
        right += c
if count % 2 == 1:
  s = s[::-1]

print(left[::-1] + s + right)

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


def main():
    s = input().strip()
    q = int(input())
    queries = [input().split() for _ in range(q)]
    pre = []
    post = []
    flg = True
    for query in queries:
        if query[0] == "1":
            flg = not flg
            continue
        if query[1] == "1":
            if flg:
                pre.append(query[2])
            else:
                post.append(query[2])
        else:
            if flg:
                post.append(query[2])
            else:
                pre.append(query[2])
    pre.reverse()
    pre.append(s)
    pre.extend(post)
    s = "".join(pre)
    if not flg:
        s = s[::-1]
    print(s)


if __name__ == "__main__":
    main()

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

from collections import deque

l = deque(list(S))


rev = 0
for qi in QUERY:
    if qi[0] == '1':
        rev += 1
    else:
        if rev % 2 == 0:
            if qi[1] == '1':
                l.appendleft(qi[2])
            else:
                l.append(qi[2])
        else:
            if qi[1] == '1':
                l.append(qi[2])
            else:
                l.appendleft(qi[2])


ans = "".join(l)

if rev % 2 != 0:
    ans = ans[::-1]

print(ans)

Solution (Python):
def main():
  from collections import deque
  s=input()
  q=int(input())
  turn = 2
  d=deque(s)

  for _ in [0]*q:
      query = input()
      if query[0] == "1":
          t = int(query)
          turn = 3 - turn
      else:
          t,f,c = map(str,query.split())
          if (int(f) + turn)% 2  == 0:
              d.append(c)
          else:
              d.appendleft(c)
  if turn == 2:
      print("".join(d))
  else:
      print("".join(list(d)[::-1]))
main()

Solution (Python):
from collections import deque
s = deque()
l = input()
for i in range(len(l)):
	s.append(l[i:i+1])
g = 0
q = int(input())
for i in range(q):
	d = input().split(" ")
	if d[0] == "1":
		if g == 0:
			g = 1
		else:
			g = 0
	if d[0] == "2":
		f = d[1]
		if g == 0:
			if f == "1":
				s.appendleft(d[2])
			else:
				s.append(d[2])
		else:
			if f == "1":
				s.append(d[2])
			else:
				s.appendleft(d[2])
if g == 1:
	s.reverse()
	ans = "".join(s)
	print(ans)
else:
	ans = "".join(s)
	print(ans)

Solution (Python):
s = input()
q = int(input())
 
right = []
left = []
 
reverse = 0
 
r = 2
for _ in range(q):
    Q = list(input().split())
    if int(Q[0]) == 1:
        if r == 2:
            r = 1
        else:
            r = 2
        reverse += 1
    else:
        if r == 2:
            if int(Q[1]) == 2:
                right.append(Q[2])
            else:
                left.append(Q[2])
        else:
            if int(Q[1]) == 2:
                left.append(Q[2])
            else:
                right.append(Q[2])
                
                
if reverse%2 == 0:
    print(''.join(left[::-1]) + s + ''.join(right))
else:
    print(''.join(right[::-1]) + s[::-1] + ''.join(left))

Solution (Python):
s=str(input())
q=int(input())
a=[input()for i in range(q)]
b=0
c=""
d=""
if q<=10**5:
    for i in range(q):
        if a[i]=="1":
            if b==0:
                b=1
            else:
                b=0
        else:
            if b==0:
                if a[i][2]=="1":
                    s=a[i][4]+s
                else:
                    s=s+a[i][4]
            else:
                if a[i][2]=="1":
                    s=s+a[i][4]
                else:
                    s=a[i][4]+s
    if b==0:
        print(s)
    else:
        s= ''.join(list(reversed(s)))
        print(s)

else:
    for i in range(10**5):
        if a[i]=="1":
            if b==0:
                b=1
            else:
                b=0
        else:
            if b==0:
                if a[i][2]=="1":
                    s=a[i][4]+s
                else:
                    s=s+a[i][4]
            else:
                if a[i][2]=="1":
                    s=s+a[i][4]
                else:
                    s=a[i][4]+s
    for i in range(10**5,q):
        if a[i]=="1":
            if b==0:
                b=1
            else:
                b=0
        else:
            if b==0:
                if a[i][2]=="1":
                    c=a[i][4]+c
                else:
                    d=d+a[i][4]
            else:
                if a[i][2]=="1":
                    d=d+a[i][4]
                else:
                    c=a[i][4]+c
    if b==0:
        print(c+s+d)
    else:
        c=''.join(list(reversed(c)))
        d=''.join(list(reversed(d)))
        s= ''.join(list(reversed(s)))
        print(d+s+c)

Solution (Python):
from collections import deque

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

for _ in range(q):
  Q = [x for x in input().split()]
  if Q[0] == '1':
    flg += 1
  else:
    if (Q[1] == '1' and flg%2==0) or (Q[1] == '2' and flg%2==1):
      s.appendleft(Q[2])
    else:
      s.append(Q[2])
if flg%2==1:
  s.reverse()

print('' . join(s))

Solution (Python):
from collections import deque

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

inv_flag = False
for _ in range(Q):
    query = list(input().split())
    if int(query[0]) == 1:
        inv_flag = not inv_flag
    else:
        if (int(query[1]) - 1) ^ inv_flag:
            S.append(query[2])
        else:
            S.appendleft(query[2])
if inv_flag:
    print(''.join(reversed(S)))
else:
    print(''.join(S))

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

queries = []
for i in range(Q):
    query = input()
    queries.append(query)

from collections import deque
slist = deque(list(S))
even = True
for query in queries:
    if query == "1":
        even = not even
    else:
        t, f, c = query.split()
        if f == '1':
            if even:
                slist.appendleft(c)
            else:
                slist.append(c)
        else:
            if even:
                slist.append(c)
            else:
                slist.appendleft(c)
if not even:
    slist.reverse()

print(''.join(slist))

Solution (Python):
import sys
from collections import deque

s = deque([sys.stdin.readline().rstrip()])
q = int(input())
cnt = 0
for i in range(q):
    query = sys.stdin.readline().split()
    if query[0] == '1':
        cnt = (cnt + 1)%2
    else:
        if (int(query[1]) + cnt)%2 == 1:
            s.appendleft(query[2])
        else:
            s.append(query[2])
s = "".join(s)
if cnt:
    s = s[::-1]
print(s)

Solution (Python):
from collections import deque

S = input()
Q = int(input())
ans = deque(S)
revcnt = 0
for i in range(Q):
    data = list(input().split())
    if data[0] == '1':
        revcnt += 1
        continue

    if data[1] == '1':
        if revcnt % 2 == 0:
            ans.appendleft(data[2])
        else:
            ans.append(data[2])
    else:
        if revcnt % 2 == 0:
            ans.append(data[2])
        else:
            ans.appendleft(data[2])

if revcnt % 2 != 0:
    print(''.join(reversed(list(ans))))
else:
    print(''.join(list(ans)))

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

count = 0
left = ""
right = ""
for query in P:
    if query[0]=="1":
        count+=1
    else:
        if query[1]=="1":
            if count%2==0:
                left+=query[2]
            else:
                right+=query[2]
        else:
            if count%2==0:
                right+=query[2]
            else:
                left+=query[2]
                
S = left[::-1]+S+right
if count%2==1:
    S=S[::-1]
    
print(S)

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

def main():
    s = input().strip()
    Q = int(input())

    head = ""
    tail = ""
    reverse = False

    for i in range(Q):
        t = input().strip().split()
        if t[0] == "1":
            
            reverse = not(reverse)
        else:
            
            if t[1] == "1":
                
                if reverse:
                    tail += t[2]
                else:
                    head += t[2]
            else:
                
                if reverse:
                    head += t[2]
                else:
                    tail += t[2]
    
    if reverse:
        print(tail[::-1] + s[::-1] + head)
    else:
        print(head[::-1] + s + tail)



if __name__ == "__main__":
    main()

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

for i in range(q):
    query=input()
    if query[0]=='1':
        direction=direction^1
    elif query[0]=='2':
        i,f,c=query.split()
        if f=='1' and direction==0:
            s.appendleft(c)
        elif f=='1' and direction==1:
            s.append(c)
        elif f=='2' and direction==0:
            s.append(c)
        elif f=='2' and direction==1:
            s.appendleft(c)


if direction==0:
    print(''.join(s))
else:
    s=''.join(s)
    s=s[::-1]
    print(s)

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

order = 'f'
front, back = [], []
for i in range(Q):
    q = input().split()
    if q[0] == '1':
        order = 'r' if order == 'f' else 'f'
        continue

    _, f, c = q
    if order == 'f':
        if f == '1':
            front.append(c)
        else:
            back.append(c)
    else:
        if f == '1':
            back.append(c)
        else:
            front.append(c)


front.reverse()
joined_s = ''.join(front) + S + ''.join(back)
print(joined_s if order == 'f' else ''.join(list(reversed(joined_s))))

Solution (Python):
def main():
    s = input()
    q = int(input())
    reverse_count = 0
    left = []
    right = []
    for _ in range(q):
        query = list(input().split())
        if query[0] == "1":
            reverse_count += 1
        else:
            if reverse_count % 2:
                if query[1] == "1":
                    right.append(query[2])
                else:
                    left.append(query[2])
            else:
                if query[1] == "1":
                    left.append(query[2])
                else:
                    right.append(query[2])
    print("".join(right[::-1]) + s[::-1] + "".join(left) if reverse_count % 2
          else "".join(left[::-1]) + s + "".join(right))


if __name__ == '__main__':
    main()

Solution (Python):
from collections import deque
S=input()
S_list=deque()
for k in range(len(S)):
    S_list.append(S[k])
Q=int(input())
kouho=[input().split() for i in range(Q)]
"""for i in range(Q):
    kari=input().split()
    kouho.append(kari)"""
hanten=[]
count=0
flaag=["sonomama","hantai"]

for j in range(Q):
    flag=flaag[count%2]
    if kouho[j][0]=="1":
        count+=1
    else:
        if flag=="sonomama":
            if kouho[j][1]=="1":
                S_list.appendleft(kouho[j][2])
            else:
                S_list.append(kouho[j][2])
        else:
            if kouho[j][1]!="1":
                S_list.appendleft(kouho[j][2])
            else:
                S_list.append(kouho[j][2])
for k in range(count%2):
    S_list.reverse()
print(*S_list,sep="")

Solution (Python):
from collections import deque


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

r=False
for i in range(0, q):
    t=input()
    if t[0] == "1":
        r = not r
        
    else:
        if t[2] == "1" and r is False or t[2] == "2" and r is True:
            
            l.appendleft(t[4])
        else:
            
            l.append(t[4])

if r is True:
    
    l = list(l)[::-1]


print("".join(l))

Solution (Python):
import numpy as np
import copy
from collections import deque

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

Que = [0]* Q
for i in range(Q):
    Que[i] = list(map(str, input().split()))

check = 1
L = deque()
R = deque()
for i in range(Q):
    T = Que[i][0]
    if int(T) == 1:
        check *= -1
    else:
        F = Que[i][1]
        if (int(F) == 1 and check == 1) or (int(F) == 2 and check == -1):

            L.appendleft(Que[i][2])
        else:

            R.append(Que[i][2])
answer = ''.join(L) + S + ''.join(R)
if check == -1:
    answer = ''.join(list(reversed(answer)))
print(answer)

Solution (Python):
from collections import deque
S = deque(input().rstrip())
Q=int(input())
cnt=True
for i in range(Q):
    query_list=input().split()
    if query_list[0] == "1":
        cnt=(cnt==False)
    else:
        if cnt:
            if query_list[1] == "1":
                S.appendleft(query_list[2])
            else:
                S.append(query_list[2])
        else:
            if query_list[1] == "1":
                S.append(query_list[2])
            else:
                S.appendleft(query_list[2])
if cnt:
    print("".join(S))
else:
    print("".join(reversed(S)))

Solution (Python):
from collections import deque
S = input()
Q = int(input())
T = [input() for i in range(Q)]

d =deque(S)

count = True

for i in range(Q):
  t = T[i]
  if(int(t[0]) == 1):
    count = not count
  else:
    if(int(t[2]) == 1):
      if(count):
        d.appendleft(t[4])
      else:
        d.append(t[4])
    else:
      if(count):
        d.append(t[4])
      else:
        d.appendleft(t[4])

ans = list(d)

if(count):
  print("".join(ans))
else:
  print("".join(ans[::-1]))

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

T1_m = 0
T1_s = 0
rev = 0

myDict = {"fr" : "", "fl" : ""}

for indx in range(N):
    que = input().split()

    if len(que) == 1:
        T1_m += 1
        T1_s += 1
    else:
        F = int(que[1])
        C = str(que[2])

        if T1_s % 2 == 1:
            rev += 1
        
        T1_s = 0
        
        if rev % 2 == 0:
            if F == 1:
                myDict["fl"] = myDict["fl"] + C
            else:
                myDict["fr"] = myDict["fr"] + C
        else:
            if F == 2:
                myDict["fl"] = myDict["fl"] + C
            else:
                myDict["fr"] = myDict["fr"] + C
        
if T1_s % 2 == 1:
    rev += 1

if T1_m % 2 == 1:
    S = S[::-1]

if rev % 2 == 0:
    print(myDict["fl"][::-1] + S + myDict["fr"])
else:
    print(myDict["fr"][::-1] + S + myDict["fl"])

Solution (Python):
import copy
from collections import deque
s = deque(input())
sc = copy.deepcopy(s) 
sc.reverse()
n = int(input())
l = [list(input().split()) for i in range(n)]

for i in range(n):
    if l[i][0] == '1':
        save = s
        s = sc
        sc = save
    elif l[i][1] == '1':
        s.appendleft(l[i][2])
        sc.append(l[i][2])
        
        
    else:
        s.append(l[i][2])
        sc.appendleft(l[i][2])
        
        

print(''.join(s))

Solution (Python):
import sys
from collections import deque
def main():
  S = input()
  Q = int(input())
  r = False
  ans = deque(S)
  for l in sys.stdin:
    TFC = l.split()
    if TFC[0]=="1": r = not r
    elif TFC[1]=="1":
      if r: ans.append(TFC[2])
      else: ans.appendleft(TFC[2])
    elif TFC[1]=="2":
      if r: ans.appendleft(TFC[2])
      else: ans.append(TFC[2])
  print("".join(reversed(ans) if r else ans))

if __name__ == "__main__":
  main()

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

top = ""
tail = ""

rev = 1


for i in range(q):
    query = input()
    if query == "1":
        rev *= -1
    elif query[2] == "1":
        if rev == 1:
            top += query[4]
        else:
            tail += query[4]
    else:
        if rev != 1:
            top += query[4]
        else:
            tail += query[4]

if rev == 1:
    print(top[::-1] + s + tail)
else:
    print(tail[::-1] + s[::-1] + top)

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

for i in range(q):
    query=list(input().split())
    i=int(query[0])
    if i==1:
        direction=direction^1
    elif i==2:
        f,c=int(query[1]),query[2]
        if f==1 and direction==0:
            s.appendleft(c)
        elif f==1 and direction==1:
            s.append(c)
        elif f==2 and direction==0:
            s.append(c)
        elif f==2 and direction==1:
            s.appendleft(c)

if direction==0:
    print(''.join(s))
else:
    s=''.join(s)
    s=s[::-1]
    print(s)

Solution (Python):
from collections import deque

S = input()
pre, post = deque(), deque()
Q = int(input())
reversed = False
for _ in range(Q):
  query = input().split()
  if query[0] == '1':
    reversed = not reversed
    pre, post = post, pre
  else:  
    F, C = query[1:]
    if F == '1':
      if reversed:
        pre.append(C)
      else:
        pre.appendleft(C)
    else:  
      if reversed:
        post.appendleft(C)
      else:
      	post.append(C)
if reversed:
  print(''.join(pre)[::-1] + S[::-1] + ''.join(post)[::-1])
else:
  print(''.join(pre) + S + ''.join(post))

Solution (Python):


from collections import deque

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


is_reverse = False

d = deque()
d.append(S)


for i in range(Q):
    query = list(input().split())
    
    if int(query[0]) == 1:
        
        
        is_reverse = not is_reverse
    else:
        if int(query[1]) == 1:
            
            if is_reverse:
                d.append(query[2])
            else:
                d.appendleft(query[2])
        else:
            
            if is_reverse:
                d.appendleft(query[2])
            else:
                d.append(query[2])

if is_reverse:
    print("".join(d)[::-1])
else:
    print("".join(d))

Solution (Python):
from collections import deque

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

reversing = False

for i in range(Q):
    query = list(input().split())
    if query[0] == '1':
        reversing = not reversing
    else:
        if query[1] == '1':
            if not reversing:
                S.appendleft(query[2])
            else:
                S.append(query[2])
        else:
            if reversing:
                S.appendleft(query[2])
            else:
                S.append(query[2])

print(''.join(S)) if not reversing else print(''.join(reversed(S)))

Solution (Python):
from collections import deque

d = deque(input())
q = int(input())

reverse = False

for _ in range(q):
    query = list(input().split())
    if query[0] == '1':
        if reverse:
            reverse = False
        else:
            reverse = True
        continue
    else:
        if query[1] == '1':
            if reverse:
                d.append(query[2])
            else:
                d.appendleft(query[2])
        else:
            if reverse:
                d.appendleft(query[2])
            else:
                d.append(query[2])

if reverse:
    d = list(reversed(d))


print("".join(map(str, d)))

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

def main():
    S=deque(input().strip())
    q=int(input())
    rev=0
    for _ in range(q):
        query=input().strip()
        if query=='1':
            rev^=1
        else:
            _,f,c=query.split()
            if (int(f)+rev)%2:
                S.appendleft(c)
            else:
                S.append(c)
    if rev:
        S.reverse()
    print(''.join(S))
    
if __name__=='__main__':
    main()

Solution (Python):
from collections import deque
S = deque(input())
Q = int(input())
 
p = 0
 
for i in range(Q):
    query = input().split()
    if query[0]=='1':
        p ^= 1
    else:
        f = int(query[1])-1
        c = query[2]
        if p:
            f ^= 1
        if f:
            S.append(c)
        else:
            S.appendleft(c)
 
if p:
    print(*reversed(S), sep='')
else:
    print(*S, sep='')

Solution (Python):

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

count = True
ans = S

ireru_mae = ''
ireru_usiro = ''
for i in range(Q):
    T = input().split()
    if len(T)==1:
        count = not(count)
        continue

    if count:
        if T[1] == '1': ireru_mae = T[2] + ireru_mae
        else: ireru_usiro += T[2]
    else:
        if T[1] == '1': ireru_usiro += T[2]
        else: ireru_mae = T[2] + ireru_mae

ans = ireru_mae + ans + ireru_usiro
if count:
    print(ans)
else:
    print(ans[::-1])

Solution (Python):
from queue import deque

s = deque([i for i in input()])
Q = int(input())
state = 0
for _ in range(Q):
    q = input()
    if q[0] == '1':
        if state == 0:
            state = 1
        else:
            state = 0
    else:
        if int(q[2]) == 1:
            if state == 0:
                s.appendleft(q[-1])
            else:
                s.append(q[-1])
        elif int(q[2]) == 2:
            if state == 0:
                s.append(q[-1])
            else:
                s.appendleft(q[-1])

if state == 1:
    s.reverse()
print(''.join(s))

Solution (Python):
from collections import deque

def main():
    s = list(input())
    Q = int(input())
    Qry = []
    for i in range(Q):
        q = list(input().split())
        Qry.append(q)
    d = deque(s)

    flg = 0
    l = 0
    r = len(s) -1
    for i, q in enumerate(Qry):
        if int(q[0]) == 1:
            tmp = l
            l = r
            r = tmp
            if flg == 0:
                flg =1
            else:
                flg = 0
        if int(q[0]) == 2:
            if int(q[1]) == 1:
                if flg == 0:
                    d.appendleft(q[2])
                else:
                    d.append(q[2])
            if int(q[1]) == 2:
                if flg ==1:
                    d.appendleft(q[2])
                else:
                    d.append(q[2])
    ans = []
    if flg == 0:
        for _ in range(len(d)):
            ans.append(d.popleft())
    if flg == 1:
        for _ in range(len(d)):
            ans.append(d.pop())
    print(''.join(ans))

if __name__=='__main__':
    main()

Solution (Python):
from collections import deque

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

reverse = False
count = 0
s = deque(s)
for i in range(q):
    qu = input()
    if qu[0] == "1":
        count += 1
        if reverse:
            reverse = False
        else:
            reverse = True
    else:
        _, f, c = qu.split()
        if reverse:
            if f == "1":
                s.append(c)
            else:
                s.appendleft(c)
        else:
            if f == "1":
                s.appendleft(c)
            else:
                s.append(c)

s = "".join(s)

if count % 2:
    print(s[::-1])
else:
    print(s)

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

ans=deque(S)
reverse=False
for i,q in enumerate(query):
  if len(q)==1:
    reverse=not reverse
  else:
    _,f,c=q
    if (f=='2') ^ reverse:
      ans.append(c)
    else:
      ans.appendleft(c)
if reverse:
  print(*reversed(ans),sep='')
else:
  print(*ans,sep='')

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

left = ""
right = ""
cnt = 0

for i in range(Q):
    q = input()
    if q == "1":
        cnt += 1
    elif (cnt%2 == 0 and q[2] == "1") or (cnt%2 == 1 and q[2] == "2"):
        left += q[4]
    else:
        right += q[4]

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

print(ans)

Solution (Python):
S = input()
q = int(input())
head = ""
tail = ""
rev_cnt = 1
for ind in range(q):
    cur_list = list(input().split())
    
    if int(cur_list[0]) == 1:
        rev_cnt *= -1
    else:
        f = int(cur_list[1])
        c = cur_list[2]
        if f == 1:
            if rev_cnt == 1:
                head += c
            else:
                tail += c
        elif f == 2:
            if rev_cnt == 1:
                tail += c
            else:
                head += c
            
S = head[::-1] + S + tail   
if rev_cnt == -1:
    S = S[::-1]
    
print("".join(S))