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

Solution (Python):
S = input()
Q = int(input())
re = False
front = []
back = []

for i in range(Q):
    t = input()
    
    if len(t) == 1:
        re = not re
        
    else:
        q, f, c = t.split()
        if (re == True and int(f) == 2) or (re == False and int(f) == 1):
            front.append(c)
        elif (re == True and int(f) == 1) or (re == False and int(f) == 2):
            back.append(c)

if re == False:
    front.reverse()
    F = "".join(front)
    B = "".join(back)
    print(F + S + B)
else:
    back.reverse()
    S = S[::-1]
    F = "".join(front)
    B = "".join(back)
    print(B + S + F)

Solution (Python):
from collections import deque

s = input()
t = s[::-1]
ss = deque(s)
tt = deque(t)
isS = True
n = int(input())
query = []



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



for i in range(n):
    
    if query[i][0] == '1':
        
        isS = not isS
    
    else:
        
        if query[i][1] == '1':
            if isS:
                ss.appendleft(query[i][2])
                tt.append(query[i][2])
            else:
                ss.append(query[i][2])
                tt.appendleft(query[i][2])
        
        else:
            if isS:
                ss.append(query[i][2])
                tt.appendleft(query[i][2])
            else:
                ss.appendleft(query[i][2])
                tt.append(query[i][2])

if isS:
    temp = list(ss)
    ans = ''.join(temp)
    print(ans)
else:
    temp = list(tt)
    ans = ''.join(temp)
    print(ans)

Solution (Python):
s = input()
q = int(input())
qs = [input().split() for i in range(q)]
n = False
left= right = ""
for operation in qs:
    if operation[0] == '1':
        n = not n
    if operation[0] == '2':
        t , f, c = operation
        f = int(f)
        if n:
            f = 1 if f != 1 else 2
        if f == 1:
            left=c+left
        elif f == 2:
            right+=c
s = left+s+right
print(s[::-1] if n else s)

Solution (Python):
from collections import deque

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

reverse_status = False

for i in range(q):
  query = list(map(format, input().split()))
  if int(query[0]) == 1:
    if reverse_status:
      reverse_status = False
    else:
      reverse_status = True
  else:
    if int(query[1]) == 1:
      if reverse_status:
        s.append(query[2])
      else:
        s.appendleft(query[2])
    else:
      if reverse_status:
        s.appendleft(query[2])
      else:
        s.append(query[2])

if reverse_status:
  s.reverse()
print(''.join(s))

Solution (Python):
s = input()
q = int(input())
qs = [input().split() for i in range(q)]
n = False
left= right = ""
for operation in qs:
    if operation[0] == '1':
        n = not n
    if operation[0] == '2':
        t , f, c = operation
        f = int(f)
        if n:
            f = 1 if f != 1 else 2
        if f == 1:
            left=c+left
        elif f == 2:
            right+=c
s = left+s+right
print(s[::-1] if n else s)

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

query_list = [input().split() for _ in range(Q)]

T_cnt_list = collections.deque([0])
left = ''
right = ''

for i, query in enumerate(query_list):
    if query[0] == '1':
        T_cnt_list.append(1+T_cnt_list[-1])

    else:
        T_cnt_list.append(T_cnt_list[-1])

        if query[1] == '1' and T_cnt_list[i+1]%2 == 0:
            left = query[2] + left

        elif query[1] == '1' and T_cnt_list[i+1]%2 == 1:
            right = right + query[2]

        elif query[1] == '2' and T_cnt_list[i+1]%2 == 0:
            right = right + query[2]

        elif query[1] == '2' and T_cnt_list[i+1]%2 == 1:
            left = query[2] + left

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

Solution (Python):
I = input
s = I()
q = int(I())
a = [], []
i = 0
for _ in range(q):
  x = I()
  if x[0] == '1':
    i ^= 1
  else:
    f = int(x[2]) - 1
    a[i ^ f].append(x[4])
r = ''.join(reversed(a[0])) + s + ''.join(a[1])
print(r[::-1] if i else r)

Solution (Python):
s = input()
q = int(input())
rev = 0
mae = []
usiro = []

for i in range(q):
    a = input()
    
    if a[0] == '1':
        rev += 1
    else:
        if (int(a[2]) + rev) % 2 == 1:
            mae.append(a[4])
        else:
            usiro.append(a[4])
            
mae = mae[::-1]
mae2 = "".join(mae)
usiro2 = "".join(usiro)
if rev%2 == 0:
    print(mae2 + s + usiro2)
else:
    ans = mae2 + s + usiro2
    print(ans[::-1])

Solution (Python):
s = input()
q = int(input())
qs = [input().split() for i in range(q)]
n = False
left= right = ""
for operation in qs:
    if operation[0] == '1':
        n = not n
    if operation[0] == '2':
        t , f, c = operation
        f = int(f)
        if n:
            f = 1 if f != 1 else 2
        if f == 1:
            left=c+left
        elif f == 2:
            right+=c
s = left+s+right
print(s[::-1] if n else s)

Solution (Python):
S=input()
Q=int(input())
import sys
from collections import deque
d=deque()
for _ in S:
  d.append(_)
flag=False

for _ in range(Q):
  s = list(sys.stdin.readline())
  if s[0]=="1":
    if flag:
      flag=False
    else:
      flag=True
  else:
    if s[2]=="1":
      if flag:
        d.append(s[4])
      else:
        d.appendleft(s[4])
    else:
      if flag:
        d.appendleft(s[4])
      else:
        d.append(s[4])
      
if flag:
  d.reverse()
  
print("".join(d))

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

reverse = 0
sento = ''
matsubi = ''

for i in range(q):
    inp = input()
    if len(inp) <= 1:
        reverse += 1
    else:
        dmy, f, c = inp.split()
        f = int(f)
        if reverse & 1:
            if f == 1:
                matsubi = matsubi + c
            else:
                sento = c + sento
        else:
            if f == 1:
                sento = c + sento
            else:
                matsubi = matsubi + c

s = sento + s + matsubi

if reverse & 1:
    print(s[::-1])
else:
    print(s)

Solution (Python):
S = input()
Q = int(input())
turn = 0
head = ""
tail = ""
for i in range(Q):
  s = input().split()
  if s[0] == "1": 
    turn += 1
  elif s[0] == "2": 
    if (s[1] == "1" and turn % 2 == 0) or (s[1] == "2" and turn % 2 == 1):
      head = s[2] + head
    elif (s[1] == "2" and turn % 2 == 0) or (s[1] == "1" and turn % 2 == 1):
      tail = tail + s[2]
if turn % 2 == 0:
  print(head + S + tail)
else:
  print((head + S + tail)[::-1])

Solution (Python):
from collections import deque
s = deque(input())
qn = int(input())
q = []
rev = False

for i in range(qn):
    qi = input().split()
    if qi[0] == "1":
        rev = not rev
    else:
        if rev:
            if qi[1] == "1":
                qi[1] = "2"
            else:
                qi[1] = "1"
        q.append((2, int(qi[1]), qi[2]))
for i in range(len(q)):
    if q[i][0] == 2:
        if q[i][1] == 1:
            s.appendleft(q[i][2])
        else:
            s.append(q[i][2])
if rev:
    s = list(s)[::-1]
print("".join(s))

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

s=S
s2=""
d.extend(S)

for i in range(Q):
    
    if (len(query[i])==1):
        if len(d)>1:
            flag=(flag+1)%2 
    else:
        if flag==0:
            if query[i][1]=="1":
                d.appendleft(query[i][2])
            else:
                d.append(query[i][2])
        else:
            if query[i][1]=="2":
                d.appendleft(query[i][2])
            else:
                d.append(query[i][2])
    
if flag==0:
    print("".join(list(d)))
else:
    d.reverse()
    print("".join(list(d)))

Solution (Python):
s = input("")
Q = int(input())
c = 0
before = []
after = []
for i in range(Q):
    t = input()
    if len(t) == 1:
        c += 1
    elif t[2] == "1":
        if c%2 == 0:
            before.append(t[4])
        else:
            after.append(t[4])
    else:
        if c%2 == 0:
            after.append(t[4])
        else:
            before.append(t[4])

if c%2 == 0:
    print("".join(before[::-1]),end="")
    print(s,end="")
    print("".join(after),end="")
else:
    print("".join(after[::-1]),end="")
    print(s[::-1],end="")
    print("".join(before),end="")

Solution (Python):
s = input()
q = int(input())
v = 0
fs = ''
rs = ''

for i in range(q):
    t = input().split()
    if t[0] == '1':
        v = v + 1
    elif t[0] == '2':
        if (t[1] == '1') and (v % 2 == 0):
            fs = t[2]+fs
        elif (t[1] == '1') and (v % 2 == 1):
            rs = rs + t[2]
        elif (t[1] == '2') and (v % 2 == 0):
            rs = rs + t[2]
        elif (t[1] == '2') and (v % 2 == 1):
            fs = t[2]+fs

if v % 2 == 0:
    s = fs + s + rs
else:
    s = fs + s + rs
    s = s[::-1]        
            
print (s)

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

top=[]
bot=[]
cnt=0
for i in range(Q):
    if q[i][0]=='1':
        cnt+=1
    if q[i][0]=='2':
        if cnt%2==0:
            if q[i][1]=='1':
                top+=q[i][2]
            if q[i][1]=='2':
                bot+=q[i][2]
        else:
            if q[i][1]=='1':
                bot+=q[i][2]
            if q[i][1]=='2':
                top+=q[i][2]

if cnt%2==1:
    ans=[S[::-1]]
    bot=bot[::-1]
    print(''.join(bot+ans+top))
else:
    ans=[S]
    top=top[::-1]
    print(''.join(top+ans+bot))

Solution (Python):
S=str(input())
Q=int(input())
count=0
M=''
U=''
for i in range(Q):
	a=list(map(str,input().split()))
	if a[0]=="1":
		count+=1
	elif a[0]=="2":
		if count%2==0:
			if a[1]=="1":
				M=a[2]+M
			elif a[1]=="2":
				U=U+a[2]
		elif count%2==1:
			if a[1]=="1":
				U=U+a[2][::-1]
			elif a[1]=="2":
				M=a[2][::-1]+M
S=M+S+U
if count%2==1:
	print(S[::-1])
else:
	print(S)

Solution (Python):
S = input()
S_h = ''
S_t = ''
Q = int(input())

def add_car(S_h,S_t,s,is_left,is_rev):
    if is_rev:
        is_left = not(is_left)
    if is_left:
        S_h = ''.join([S_h,s])
    else:
        S_t = ''.join([S_t,s])
    return S_h,S_t

is_rev = False
for q in range(Q):
    x = input().split(' ')
    if len(x) == 1:
        is_rev = not is_rev
    else:
        S_h,S_t = add_car(S_h,S_t,x[2],x[1]=='1',is_rev)

if is_rev:
    S_t = ''.join(list(reversed(S_t)))
    S = ''.join(list(reversed(S)))
    print(''.join([S_t,S,S_h]))
else:
    S_h = ''.join(list(reversed(S_h)))
    print(''.join([S_h,S,S_t]))

Solution (Python):
from collections import deque
def main():
  s=deque(list(input()))
  q=int(input())
  ok=1
  for i in range(q):
    query=list(input().split())
    if query[0]=='1':
      ok=(ok+1)%2
    else:
      f,c=query[1],query[2]
      if ok:
        if f=='1':
          s.appendleft(c)
        else:
          s.append(c)
      else:
        if f=='1':
          s.append(c)
        else:
          s.appendleft(c)
  s=list(s)
  if ok:
    print(''.join(s))
  else:
    print(''.join(s[::-1]))
if __name__=='__main__':
  main()

Solution (Python):
from collections import deque

s = input()
q = int(input())
Q = [input() for _ in range(q)]

s = deque(list(s))
insert = s.appendleft
append = s.append
flag = True

for query in Q:
    if query[0] == '1':
        flag = not flag
    else:
        if (query[2] == '1' and flag) or (query[2] == '2' and not flag):
            insert(query[4])
        else:
            append(query[4])

if not flag:
    s.reverse()

print(*s, sep='')

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

sl = ['']*(q*2+2)
sl[q] = s
m = q-1
u = q+1
rev = False
for _ in range(q):
    query = input().split()

    if query[0] == '1' and not(rev):
        rev = True
    elif query[0] == '1' and rev:
        rev = False

    elif query[0] == '2' and query[1] == '1' and not(rev):
        sl[m] = query[2]
        m -= 1

    elif query[0] == '2' and query[1] == '1' and rev:
        sl[u] = query[2]
        u += 1

    elif query[0] == '2' and query[1] == '2' and not(rev):
        sl[u] = query[2]
        u += 1

    elif query[0] == '2' and query[1] == '2' and rev:
        sl[m] = query[2]
        m -= 1

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

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

flag=0
forward=""
back=""
for i in range(Q):
    
    b=input()

    if(len(b)==1):
        
        flag+=1
    else:
        a=b.split(" ")
        if(flag==0):
            if(a[1]=="1"):
                
                forward=a[2]+forward
            else:
                
                back=back+a[2]
        else:
            if(a[1]=="1"):
                
                back=back+a[2]
            else:
                
                forward=a[2]+forward
                
    flag=flag%2

S=forward+S+back
if(flag==1):
    S=S[::-1]
print(S)

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

front = ""
end = ""
f = 0
for i in range(Q):
    query = input().split()

    if query[0] == "1":
        if f == 0:
            f = 1
        else:
            f = 0
    else:
        if f == 0:
            if query[1] == "1":
                front = query[2] + front
            else:
                end = end + query[2]
        else:
            if query[1] == "2":
                front = query[2] + front
            else:
                end = end + query[2]

S = front + S + end
if f == 1:
    S = S[::-1]
print(S)

Solution (Python):
S = input()
Q = int(input())
I = 0
L = ''
R = ''
for i in range(Q):
    Query = input().split()
    if Query[0] == "1":
        I = I + 1
    else:
        if (int(Query[1]) + I) % 2 == 0:
            R = R + Query[2]
        else:
            L = L + Query[2]
L = L[::-1]
S = L + S + R

if I % 2:
    S = S[::-1]
print(S)

Solution (Python):
import sys
import collections

sys.setrecursionlimit(10 ** 8)

input = sys.stdin.readline


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

    rev = 0
    sdeq = collections.deque(list(S))

    for i in range(Q):
        A = [x for x in input().split()]
        if len(A) == 1:
            rev = (rev + 1) % 2
        else:
            f = int(A[1])
            c = A[2]
            if f == 1:
                if rev == 0:
                    sdeq.appendleft(c)
                else:
                    sdeq.append(c)
            else:
                if rev == 0:
                    sdeq.append(c)
                else:
                    sdeq.appendleft(c)

    if rev == 0:
        print("".join(list(sdeq)))
    else:
        print("".join(list(sdeq)[::-1]))
    print()



if __name__ == '__main__':
    main()

Solution (Python):


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



    
    

t=0
b=""
a=""
for j in range(Q):
    i=list(input().split())
    if i[0]=="1":
        t+=1
    elif i[0]=="2":
        if t%2==0:
            if i[1]=="1":
                b=i[2]+b
            elif i[1]=="2":
                a=a+i[2]
        elif t%2==1:
            if i[1]=="1":
                a=a+i[2]
            elif i[1]=="2":
                b=i[2]+b
if t%2==0:
    print(b+S+a)
    
else:
    print(a[::-1]+S[::-1]+b[::-1])

Solution (Python):
from sys import stdin

def main():
    S = list(stdin.readline().rstrip())
    Q = int(input())
    rev = 0
    head = []
    tail = []

    for _ in range(Q):
        q = stdin.readline()
        if q[0] == "1":
            rev = (rev+1)%2
        else:
            if q[2] == "1":
                if rev == 0:
                    head.append(q[4])
                else:
                    tail.append(q[4])
            else:
                if rev ==0:
                    tail.append(q[4])
                else:
                    head.append(q[4])

    if rev == 0:
        ans = "".join(head[::-1] + S + tail)
    else:
        ans = "".join(tail[::-1] + S[::-1] + head)
    
    print(ans)
    """
    S = stdin.readline().rstrip() 
    Q = int(input())
    rev = 0
    for _ in range(Q):
        q = stdin.readline()

        if q[0] == "1":
            rev = (rev+1)%2
        else:
            if q[2] == "1":
                if rev == 0:
                    S = "{}{}".format(q[4], S)
                else:
                    S = "{}{}".format(S, q[4])
            else:
                if rev ==0:
                    S = "{}{}".format(S, q[4])
                else:
                    S = "{}{}".format(q[4], S)
    if rev == 1:
        print(S[::-1])
    else:
        print(S)
    """
main()

Solution (Python):
from collections import deque
def main():
    S = input()
    S = deque([c for c in S])
    Q = int(input())
    rvCt = 0

    for _ in range(Q):
        query = input().split()
        if query[0] == '1':
            rvCt += 1
        else:
            doReverse = False
            if rvCt % 2 == 1:
                doReverse = True

            if query[1] == '1':
                if doReverse:
                    S.append(query[2])
                else:
                    S.appendleft(query[2])
            else:
                if doReverse:
                    S.appendleft(query[2])
                else:
                    S.append(query[2])

    if rvCt % 2 == 1:
        S.reverse()
    
    print(''.join(S))

main()

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

Ques = []
nReverse = 0
nAddFront= 0
nAddBack = 0
for i in range(nQ):
  line = input().split()
  if int(line[0]) == 1:
    nReverse += 1
    Ques.append([int(line[0])])
  else:
    if int(line[1]) == 1:
      if nReverse % 2 == 0:
        nAddFront += 1
      else:
        nAddBack  += 1
    else:
      if nReverse % 2 == 0:
        nAddBack += 1
      else:
        nAddFront  += 1      
    Ques.append([int(line[0]),int(line[1]),line[2]])


SS = [-1] * (nAddFront + len(S) + nAddBack)
for i in range(len(S)):
   SS[nAddFront + i] = S[i]

left = nAddFront -1
right= nAddFront + len(S)

nReverseLoc = 0
for iQ in Ques:
   if iQ[0] == 1:
      nReverseLoc += 1
   if iQ[0] == 2:
    if int(iQ[1]) == 1:
      if nReverseLoc % 2 == 0:
        
        SS[left] = iQ[2]
        left -= 1
      else:
        SS[right] = iQ[2]
        right += 1
    else:
      if nReverseLoc % 2 == 0:
        SS[right] = iQ[2]
        right += 1
      else:
        SS[left] = iQ[2]
        left  -= 1 

S = "".join(SS)
if nReverse % 2 == 1:
  S = S[::-1]
  
print(S)

Solution (Python):
s = str(input())
q = int(input())
g = 0
A = ""
B = ""
for i in range(q):
  query = input()
  if (query == "1"):
    g = 1-g
  else:
    queries = query.split()
    f = int(queries[1])-1
    c = queries[2]
    if (g == f):
      B = "".join([B,c])
    else:
      A = "".join([A,c])
s = "".join(["".join(list(B)[::-1]),s,A])
if (g == 1):
  s = "".join(list(s)[::-1])
print(s)

Solution (Python):



reverse = False

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

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

prefix = []
suffix = []

for query in queries:
    qs = query.split()
    if qs[0] == '1':
        reverse = not reverse
    elif qs[0] == '2':
        if qs[1] == '1':
            if reverse:
                suffix.append(qs[2])
            else:
                prefix.append(qs[2])
        elif qs[1] == '2':
            if reverse:
                prefix.append(qs[2])
            else:
                suffix.append(qs[2])

if reverse:
    print (''.join(suffix[::-1]) + s[::-1] + ''.join(prefix))
else:
    print (''.join(prefix[::-1]) + s + ''.join(suffix))

Solution (Python):
from collections import deque
s= input()
q= int(input())
inv = False
head = deque()
tail = deque()
for i in range(q):
    p = input()
    if p[0]=='1':
        if inv:inv=False
        else:inv=True
    elif p[0]=='2':
        _,f,c= p.split()
        if f=='1':
            if inv:tail.append(c)
            else:head.appendleft(c)
        elif f=='2':
            if inv:head.appendleft(c)
            else:tail.append(c)
news = ''.join(head) + s + ''.join(tail)
if inv:print(news[::-1])
else:print(news)

Solution (Python):
s=input()
q=int(input())
trans=0
aft=""
bef=""
for i in range(q):
  que=input()
  if que[0]=="1":
    trans=1-trans
  else:
    if (que[2]=="1" and trans==0) or (que[2]=="2" and trans==1):
      bef=bef+que[4]
    else:
      aft=aft+que[4]
if trans==0:
  print("".join(bef[::-1])+s+"".join(aft))
else:
  print("".join(aft[::-1])+s[::-1]+"".join(bef))

Solution (Python):
S = input()
N = int(input())
Q = [[] for _ in range(N)]
for i in range(N):
    line = input().split()
    Q[i] = line
reverse = 0
r_b = ""
r_f = ""
for i in range(N):
    if Q[i][0] == '1' :
        reverse = (reverse + 1) % 2
    else :
        if reverse == 0 :
            if Q[i][1] == '1' :
                r_f = Q[i][2] + r_f
            else :
                r_b = r_b +Q[i][2]
        else:
            if Q[i][1] == '1':
                r_b = r_b +Q[i][2]
            else:
                r_f = Q[i][2] + r_f
if reverse == 0 :
    print(r_f + S + r_b)
else:
    r_f = r_f[::-1]
    r_b = r_b[::-1]
    S = S[::-1]
    print(r_b + S + r_f)

Solution (Python):
s = input()

q = int(input())
a = [input().split() for l in range(q)]
flag =0
c=0
start=""
finish=""
for i in range(0,q):
    if a[i][0]=='1':
        flag +=1
        c=flag%2
    else:
        if a[i][1]=='1':
            if c==0:
                start = a[i][2] + start
            else:
                finish = finish + a[i][2]
        else:
            if c==0:
                finish = finish +a[i][2]
            else:
                start = a[i][2] + start
ans = start + s + finish
if c==0:
    print(ans)
else:
    ans=ans[::-1]
    print(ans)

Solution (Python):
import sys
from collections import deque

readline = sys.stdin.readline
s = deque(readline().strip())
n = int(readline())
q = sys.stdin.readlines()
reverse = False
for qi in q:
    if qi[0] == '1':
        reverse = not reverse
    else:
        c = qi[4]
        if qi[2] == '2':
            if reverse:
                s.appendleft(c)
            else:
                s.append(c)
        else:
            if reverse:
                s.append(c)
            else:
                s.appendleft(c)
if reverse:
    s = reversed(list(s))
print(''.join(s))

Solution (Python):
import sys
from collections import deque

INPUT = lambda: sys.stdin.readline().rstrip()
INT = lambda: int(INPUT())


def main():
    S = deque(INPUT())
    Q = INT()

    flag_order = True
    D_MAP = lambda: map(str, query.split())
    for i in range(Q):
        query = INPUT()
        if len(query.split()) == 1:
            T = 1
        else:
            T = 2
            _, F, C = D_MAP()
            F = int(F)

        if T == 1:
            if flag_order: flag_order = False
            else: flag_order = True
        if T == 2:
            if flag_order:
                if F == 1: S.appendleft(C)
                else: S.append(C)
            else:
                if F == 1: S.append(C)
                else: S.appendleft(C)

    print(''.join(S) if flag_order else ''.join(reversed(S)))


if __name__ == '__main__':
    main()

Solution (Python):
from collections import deque
S = input()
Q = int(input())
 
alpha_map = {chr(i):i for i in range(97,123)}
alpha_map_inv = {i:chr(i) for i in range(97,123)}
a = deque()
for s in S:
  a.append(s)
reversed = False
 
for _ in range(Q):
  query = list(map(str, input().split()))
  if query[0] == '1':
    if reversed:
      reversed = False
    else:
      reversed = True
  else:
    if (query[1] == '1')&(not reversed):
      a.appendleft(query[2])
    elif (query[1] == '2')&(reversed):
      a.appendleft(query[2])
    else:
      a.append(query[2])
if reversed:
  a.reverse()
print(''.join(list(a)))

Solution (Python):
S = input() 
Q = int(input())
rlist, llist = [], []
flag, cnt = 0, 0

def trans(f):
    if f == 1:
        f = 0
    elif f == 0:
        f = 1
    return f

def pros(f, r, add):
    if f == 0:
        if r == "1":
            llist.append(lists[2])
        elif r == "2":
            rlist.append(lists[2])
    elif f == 1:
        if r == "2":
            llist.append(lists[2])
        elif r == "1":
            rlist.append(lists[2])

for i in range(Q):
    lists = input().split()
    if lists[0] == "1":
        flag = trans(flag)
        cnt += 1
    else:
        pros(flag, lists[1], lists[2])
        
llist.reverse()
        
S = "".join(llist) + S + "".join(rlist)

if cnt % 2 == 1:
    S = S[::-1]
print(S)

Solution (Python):

import sys


def solve(S, Q, Qvery):
    pool = []
    fw = True
    for qvery in reversed(Qvery):
        if qvery[0] == 1:
            if fw:
                fw = False
            else:
                fw = True
        else:
            if qvery[1] == 1 and fw or qvery[1] == 2 and not fw:  
                print(qvery[2], end='')
            else:
                pool.append(qvery[2])

    if fw:
        print(S, end='')
    else:
        rev_S = S[::-1]
        print(rev_S, end='')
    for p in reversed(pool):
        print(p, end='')
    print()

    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    

    
    
    
    
    



def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    S = str(next(tokens))  
    Q = int(next(tokens))  
    Qvery = []
    for i in range(Q):
        T = int(next(tokens))
        if T == 1:
            Qvery.append([1])
        elif T == 2:
            Qvery.append([2, int(next(tokens)), str(next(tokens))])
    solve(S, Q, Qvery)

if __name__ == '__main__':
    main()

Solution (Python):
from collections import deque
S = input()
Q = int(input())
reverse_flag = False
sento = deque([])
matubi = deque([])

def add_s(f, c):
  if f == '1':
    
    sento.appendleft(c)
  else:
    
    matubi.append(c)

for _ in range(Q):
  q = input()
  if q == '1':
    reverse_flag = not reverse_flag
    continue
  _, f, c = q.split()
  
  if reverse_flag:
    add_s('1' if f == '2' else '2', c)
  else:
    add_s(f, c)
  

_s = ''.join(sento)
_m = ''.join(matubi)
ans = ''.join([_s, S, _m])
if reverse_flag:
  ans = ans[::-1]
print(ans)

Solution (Python):
from collections import deque

s = list(input())
s2 = deque(list(reversed(s)))
s = deque(s)
Q = int(input())
q = []
f = []
c = []
count = 0

for _ in range(Q):
    tmp = list(map(str,input().split()))
    if len(tmp) == 1:
        count += 1
        q.append(int(tmp[0]))
        f.append(0)
        c.append('')
    else:
        q.append(int(tmp[0]))
        f.append(int(tmp[1]))
        c.append(tmp[2])


for i in range(Q):
    if q[i] == 1:
        tmp = s
        s = s2
        s2 = tmp
    else:
        if f[i] == 1:
            s.appendleft(c[i])
            s2.append(c[i])
        else:
            s.append(c[i])
            s2.appendleft(c[i])

for i in range(len(s)):
    print(s.popleft(),end="")
print("")

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

flg = "moto"

atama = list()
shippo = list()

for _ in range(Q):
    tmp = list(map(str, input().split()))
    if flg == "moto":
        if int(tmp[0]) == 1:
            flg = "hanten"
        else:
            if int(tmp[1]) == 1:
                atama.append(tmp[2])
            else:
                shippo.append(tmp[2])
    
    else:
        if int(tmp[0]) == 1:
            flg = "moto"
        else:
            if int(tmp[1]) == 1:
                shippo.append(tmp[2])
            else:
                atama.append(tmp[2])

if flg == "moto":
    atama = atama[::-1]
    print("".join(atama) + S + "".join(shippo))

else:
    shippo = shippo[::-1]
    print("".join(shippo) + S[::-1] + "".join(atama))

Solution (Python):
s = input()
q = int(input())
qs = [input().split() for i in range(q)]
n = False
left= right = ""
for operation in qs:
    if operation[0] == '1':
        n = not n
    if operation[0] == '2':
        t , f, c = operation
        f = int(f)
        if n:
            f = 1 if f != 1 else 2
        if f == 1:
            left=c+left
        elif f == 2:
            right+=c
s = left+s+right
print(s[::-1] if n else s)

Solution (Python):
from collections import deque


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

for _ in range(Q):
    q = input().split()
    if q[0] == "1":
        flag *= -1
    elif q[1] == "1":
        if flag == 1:
            S.appendleft(q[2])
        else:
            S.append(q[2])
    else:
        if flag == 1:
            S.append(q[2])
        else:
            S.appendleft(q[2])
s = list(S)
if flag == 1:
    print("".join(s))
else:
    for i in reversed(s):
        print(i, end="")

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

top = []
end = []


order = []
state = True

for n in range(N):
    order = input().split()
    if order[0] == '2':
        if (order[1] == '1') == state:
            top.append(order[2])
        else:
            end.append(order[2])
    elif state:
        state = False
    else:
        state = True


if state:
    S = ''.join(S)
    top = ''.join(top[::-1])
    end = ''.join(end)
    out = top + S + end
    
else:
    S = ''.join(S[::-1])
    top = ''.join(top)
    end = ''.join(end[::-1])
    out = end + S + top

print(out)

Solution (Python):
def input_int():
    return map(int, input().split())

def one_int():
    return int(input())

def one_str():
    return input()

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

S= one_str()

Q = one_int()

Qs = [ input() for i in range(Q)]
def ss(S, Qs):
    is_not_ac=False
    head = ""
    tail = ""
    for temp in Qs:

        if len(temp)>=2:
            T, F, C = temp.split()
            if F=="1":
                if is_not_ac:
                    tail += C
                else:
                    head += C
            else:
                if is_not_ac:
                    head += C
                else:
                    tail += C
        else:        
            is_not_ac = not is_not_ac
    if is_not_ac:
        return tail[::-1] + S[::-1] + head
    else:
        return head[::-1] + S + tail


print(ss(S,Qs))