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 math import floor
import sys
from copy import copy

from collections import deque
input = sys.stdin.readline
'''
n, m = map(int, input().split())
n = int(input())
A = list(map(int, input().split()))
S = input().strip()
for test in range(int(input())):
'''
inf = 100000000000000000  
mod = 998244353




S = input().strip()
D = deque(S)


n = int(input())
reverse = False
for operation in range(n):
    data = input().strip()
    if data[0] == "1":
        if reverse is False:
            reverse = True
        else:
            reverse = False
    else:
        f = data[2]
        ele = data[4]
        if (f == "1" and reverse is False) or (f == "2" and reverse == True):
            D.appendleft(ele)
        else:
            D.append(ele)


if reverse is True:
    D.reverse()

print("".join(D))

Solution (Python):
import sys
def input(): return sys.stdin.readline().rstrip()
from collections import deque
def main():
    s=input()
    q=int(input())
    s=deque(s)
    inv=False
    for i in range(q):
        que=input()
        if que[0]=='1':
            inv=not(inv)
        else:
            f=que[2]
            if (f=='1' and not(inv)) or (f=='2' and inv):
                s.appendleft(que[4])
            else:
                s.append(que[4])
    if inv:s.reverse()
    print(*s,sep='')



if __name__=='__main__':
    main()

Solution (Python):
import sys
def input(): return sys.stdin.readline().rstrip()
from collections import deque
def main():
    s=input()
    q=int(input())
    s=deque(s)
    inv=False
    for i in range(q):
        que=input()
        if que[0]=='1':
            inv=not(inv)
        else:
            f=que[2]
            if (f=='1' and not(inv)) or (f=='2' and inv):
                s.appendleft(que[4])
            else:
                s.append(que[4])
    if inv:s.reverse()
    print(*s,sep='')

if __name__=='__main__':
    main()

Solution (Python):
from collections import deque
S = deque(list(input()))
Q = int(input())
t = 0
for i in range(Q):
    query = input().split()
    if int(query[0]) == 1:
        t += 1
    else:
        query[1] = int(query[1])
        if t % 2 == 1:
            if query[1] == 1:
                query[1] = 2
            else:
                query[1] = 1
        if query[1] == 1:
            S.appendleft(query[2])
        else:
            S.append(query[2])
if t % 2 == 1:
    S.reverse()
print("".join(S))

Solution (Python):
from collections import deque
S = deque(list(input()))
Q = int(input())
t = 0
for i in range(Q):
    query = input().split()
    if int(query[0]) == 1:
        t += 1
    else:
        query[1] = int(query[1])
        if t % 2 == 1:
            if query[1] == 1:
                query[1] = 2
            else:
                query[1] = 1
        if query[1] == 1:
            S.appendleft(query[2])
        else:
            S.append(query[2])
if t % 2 == 1:
    S.reverse()
print("".join(S))

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

rev = 0
for i in range(q):
    inp = list(input().split())
    if inp[0] == '1':rev = rev^1
    else:
        if (inp[1] == '1' and rev == 0) or (inp[1] == '2' and rev == 1):
            s.appendleft(inp[2])
        else:
            s.append(inp[2])

s = list(s)
if rev == 1:s = s[::-1]
print(*s, sep = '')

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

S = deque(S)

rev = False
for i in range(Q):
  query = list(input())
  if query[0] == '1':
    rev = not rev
  else:
    c = query[4]
    if query[2] == '1' and not rev or (rev and query[2] == '2'):
      S.appendleft(c)
    else:
      S.append(c)
      
if rev:
  S.reverse()


print(''.join(S))

Solution (Python):
import sys

s = sys.stdin.readline().rstrip()
q = int(sys.stdin.readline().rstrip())
query = [sys.stdin.readline().rstrip().split() for i in range(q)]
top = 1
s = list(s)
s_front = []
s_back = []
for i in range(q):
    que = query[i]
    if que[0] == '1':
        top *= -1
    else:
        if que[1] == '1':
            if top > 0:
                s_front.append(que[2])
                
                
            else:
                s_back.append(que[2])
                
                
        else:
            if top > 0:
                s_back.append(que[2])
                
                
            else:
                s_front.append(que[2])
                
                
s_front.reverse()
s_front.extend(s)
s_front.extend(s_back)
s = s_front
s = ''.join(s)
if top < 0:
    s = s[::-1]
print(s)

Solution (Python):
s=input()
q=int(input())
cnt1=0
s_mae=[]
s_ushiro=[]
for i in range(q):
    query=list(input().split())
    if int(query[0])==1:
        cnt1+=1
    else:
        if (int(query[1])+cnt1)%2==0:
            s_ushiro.append(query[2])
        else:
            s_mae.append(query[2])
s1=""
for i in range(len(s_mae)):
    s1=s1+s_mae[-1-i]
s2="".join(s_ushiro)
ans=s1+s+s2
if cnt1%2==0:
    print(ans)
else:
    ans2=""
    for i in range(len(ans)):
        ans2=ans2+ans[-1-i]
    print(ans2)

Solution (Python):
from collections import deque

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

d = deque(list(S))
reverse = False

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

    if query[0] == '1':
        reverse = not reverse

    elif query[0] == '2':
        if query[1] == '1' and not reverse:
            d.appendleft(query[2])
        elif query[1] == '2' and not reverse:
            d.append(query[2])
        elif query[1] == '1' and reverse:
            d.append(query[2])
        elif query[1] == '2' and reverse:
            d.appendleft(query[2])

if reverse:
    d.reverse()

print(''.join(d))

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

inverse = 1
left = ''
right = ''
for i in range(q):
    query = list(map(str, input().split()))
    if query[0] == '1':
        inverse *= -1
    if query[0] == '2':
        if query[1] == '1' and inverse == 1:
            left += query[2]
        elif query[1] == '2' and inverse == 1:
            right += query[2]
        elif query[1] == '1' and inverse == -1:
            right += query[2]
        elif query[1] == '2' and inverse == -1:
            left += query[2]
s = left[::-1]+s+right
if inverse == 1:
    print(s)
else:
    print(s[::-1])

Solution (Python):
import sys
from collections import deque

readline = sys.stdin.readline
readlines = sys.stdin.readlines

s = readline().rstrip()
q = readline()
queries = readlines()
s = deque(s)
tot_1 = 0
for i in queries:
    if i[0] == '1':
        tot_1 += 1
        continue
    c = i[4]
    if (tot_1 + int(i[2])) % 2 == 0:
        s.append(c)
    else:
        s.appendleft(c)
ans = "".join(s) if tot_1 % 2 == 0 else "".join(reversed(s))
print(ans)

Solution (Python):
from collections import deque

s = deque(input())
q = int(input())
counter = 1
lista = [list(input().split()) for i in range(q)]

def rever(i):
    if i == '1':
        return 1
    else:
        return -1
for i in lista:
    if i[0] == '1':
        counter = -1 * counter
    else:
        if counter * rever(i[1]) > 0:
            s.appendleft(i[2])
        else:
            s.append(i[2])
s = "".join(list(s))
if counter > 0:
    print(s)
else:
    print(s[::-1])

Solution (Python):
from collections import deque

d = deque(list(input()))
q = int(input())
flag = 1
for i in range(q):
    query = input().split()
    if query[0] == '1':
        flag = - flag
    elif query[0] == '2':
        if flag == 1:
            if query[1] == '1':
                d.appendleft(query[2])
            elif query[1] == '2':
                d.append(query[2])
        else:
            if query[1] == '1':
                d.append(query[2])
            elif query[1] == '2':
                d.appendleft(query[2])
if flag == -1:
    d.reverse()
print(''.join(list(d)))

Solution (Python):
from collections import deque
S=deque(list(input()))
sign=1

for i in range(int(input())):
  ls=input().split()
  if ls[0]=='1':
    sign*=-1
  else:
    if sign*int(ls[1])==1 or sign*int(ls[1])==-2:
      S.appendleft(ls[2])
    else:
      S.append(ls[2])
if sign==-1:
  S.reverse()
print(*S,sep='')

Solution (Python):
from collections import deque

s_list = list(input())
d = deque(s_list)
q = int(input())
reverse = False

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


if reverse:
	d.reverse()
for s in d:
  print(s, end ="")

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

reverse = False
for _ in range(Q):
    query = input().split()
    if query[0] == "1":
        reverse = not reverse
    else:
        tail = bool(int(query[1]) - 1) ^ reverse
        c = query[2]
        if tail:
            S.append(c)
        else:
            S.appendleft(c)

print("".join(reversed(S)) if reverse else "".join(S))

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

reverse = False
for _ in range(Q):
    query = input().split()
    if query[0] == "1":
        reverse = not reverse
    else:
        tail = bool(int(query[1]) - 1) ^ reverse
        c = query[2]
        if tail:
            S.append(c)
        else:
            S.appendleft(c)

print("".join(reversed(S)) if reverse else "".join(S))

Solution (Python):
from collections import deque
S = input()
Q = int(input())
ans = deque(S)
rev_flag = False
for i in range(Q):
    query = input()
    if query == "1":
        rev_flag = not rev_flag
    elif not rev_flag:
        if query[2] == "1":
            ans.appendleft(query[4])
        else:
            ans.append(query[4])
    else:
        if query[2] == "2":
            ans.appendleft(query[4])
        else:
            ans.append(query[4])

if rev_flag:
    ans.reverse()

print("".join(ans))

Solution (Python):
from collections import deque

S = deque(list(input()))

f = False

for i in range(int(input())):
    q = input().split()
    
    if q[0] == '1': f = not f
    else:
        if not f:
            if q[1] == '1': S.appendleft(q[2])
            else: S.append(q[2])
        else:
            if q[1] == '1': S.append(q[2])
            else: S.appendleft(q[2])
        
if f: S.reverse()
print(''.join(map(str, list(S))))

Solution (Python):
from collections import deque

s = input()
q=int(input())
deq = deque()
deq.append(s)
fl = False
for i in range(q):
    a=[i for i in input().split()]
    if a[0]=="1":
        fl = not fl
    else:
        if a[1]=="1":
            if fl:
                deq.append(a[2])
            else:
                deq.appendleft(a[2])
        else:
            if fl:
                deq.appendleft(a[2])
            else:
                deq.append(a[2])
if fl:
    s=''.join(deq)
    print(s[::-1])
else:
    print(''.join(deq))

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

state = 0
for i in range(q):
    query = list(input().split())
    if query[0] == "1":
        state += 1
    else:
        if (int(query[1]) + state) % 2 == 1:
            s.appendleft(query[2])
        else:
            s.append(query[2])

if state % 2 == 1:
    s = list(s)[::-1]

print("".join(s))

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

jun = True
head = ""

for _ in range(Q):
    T = input().split()
    if T[0] == "1":
        jun = not jun
    
    else:

        if (T[1] == "1" and jun) or (T[1] == "2" and not jun):
            head += T[2]
            continue

        else:
            S += T[2]

if jun:
    print(head[::-1] + S)
else:
    print(S[::-1] + head)

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

is_reversed = False
head = ""

for i in range(Q):
    tmp_Q = input().split()

    if len(tmp_Q) == 1:
        is_reversed = not is_reversed
        
    else:
        if tmp_Q[1] == "1":
            if is_reversed:
                S = S + tmp_Q[2]
            else:
                head = head + tmp_Q[2]
        else:
            if is_reversed:
                head = head + tmp_Q[2]
            else:
                S = S + tmp_Q[2]

if is_reversed:
    print(S[::-1] + head)
else:
    print(head[::-1] + S)

Solution (Python):
from sys import stdin
from collections import deque

if __name__ == "__main__":
    _in = [_.rstrip() for _ in stdin.readlines()]
    S =     _in[0]   
    Q =     _in[1]   
    Q_arr = []
    for __in in _in[2:]:
        _ = __in.split(' ')
        if len(_) == 1:
            Q_arr.append([int(_[0])])
        else:
            Q_arr.append([int(_[0]),int(_[1]),_[2]])
    
    S_arr = deque([s for s in S])
    reverse = False
    for _Q in Q_arr:
        if _Q[0] == 1:
            reverse = not reverse
        elif (_Q[1] == 1 and (not reverse)) or (_Q[1] == 2 and reverse):
            S_arr.appendleft(_Q[2])
        else:
            S_arr.append(_Q[2])
    if reverse:
        S_arr.reverse()
    S_arr = list(S_arr)
    S = ''.join(S_arr)
    
    print(S)

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

trans = False
s2 = ''
for i in range(q):
    query = input().split()
    if(query[0] == '1'):
        trans = not trans
    else:
        if(trans):
            if(query[1] == '1'):
                s = s + query[2]
            else:
                s2 = s2 + query[2]
        else:
            if(query[1] == '1'):
                s2 = s2 + query[2]
            else:
                s = s + query[2]

s2 = s2[::-1]
s = s2 + s
if(trans):
    s = s[::-1]
print(s)

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

S = deque(input().strip())
Q = int(input())
flag = 0

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

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

Solution (Python):
from collections import deque
import sys
input = lambda: sys.stdin.readline().rstrip() 
def main():
    moji=input()
    d=deque(moji)
    cycle=int(input())
    flont_or_back=0
    flont=""
    back=""
    for i in range(cycle):
        line=input()
        if line[0]=="1":
            flont_or_back+=1
        else:
            fi=line[2]
            ci=line[4]
            if flont_or_back%2==0:
                if fi=="1":
                    d.appendleft(ci)
                else:
                    d.append(ci)
            else:
                if fi=="1":
                    d.append(ci)
                else:
                    d.appendleft(ci)
    if flont_or_back%2==0:
        print("".join(d))
    else:
        d.reverse()
        print("".join(d))

if __name__ == '__main__':
    main()

Solution (Python):
import sys
input = lambda: sys.stdin.readline().rstrip() 
s=input()
count=0
s1=str()
s2=str()
for _ in range(int(input())):
  l=list(map(str,input().split()))
  
  if l[0]=='1':
    count+=1
  elif count%2==0:
    if l[1]=='1':
      s1=l[2]+s1
    else:
      s2=s2+l[2]
  else:
    if l[1]=='2':
      s1=l[2]+s1
    else:
      s2=s2+l[2]
s=s1+s+s2
if count%2!=0:
  s=s[::-1]
print(s)

Solution (Python):

s=input()
count=0
s1=str()
s2=str()
for _ in range(int(input())):
  l=list(map(str,input().split()))
  
  if l[0]=='1':
    count+=1
  elif count%2==0:
    if l[1]=='1':
      s1=l[2]+s1
    else:
      s2=s2+l[2]
  else:
    if l[1]=='2':
      s1=l[2]+s1
    else:
      s2=s2+l[2]
s=s1+s+s2
if count%2!=0:
  s=s[::-1]
print(s)

Solution (Python):
import sys
input = lambda: sys.stdin.readline().rstrip() 
s=input()
count=0
s1=str()
s2=str()
for _ in range(int(input())):
  l=list(map(str,input().split()))
  
  if l[0]=='1':
    count+=1
  elif count%2==0:
    if l[1]=='1':
      s1=l[2]+s1
    else:
      s2=s2+l[2]
  else:
    if l[1]=='2':
      s1=l[2]+s1
    else:
      s2=s2+l[2]
s=s1+s+s2
if count%2!=0:
  s=s[::-1]
print(s)

Solution (Python):
s=input()
count=0
s1=str()
s2=str()
for _ in range(int(input())):
  l=list(map(str,input().split()))
  
  if l[0]=='1':
    count+=1
  elif count%2==0:
    if l[1]=='1':
      s1=l[2]+s1
    else:
      s2=s2+l[2]
  else:
    if l[1]=='2':
      s1=l[2]+s1
    else:
      s2=s2+l[2]
s=s1+s+s2
if count%2!=0:
  s=s[::-1]
print(s)

Solution (Python):
from collections import deque, Counter as cnt
from collections import defaultdict as dd
from operator import itemgetter as ig
from bisect import bisect_right as bsr
from math import factorial, ceil, floor
import sys
sys.setrecursionlimit(1000000)


args = None
INF = float("inf")
MOD = int(1e9 + 7)


def input(*ps):
    if type(ps[0]) is list:
        return [input(*ps[0][:-1]) for _ in range(ps[0][-1])]
    elif len(ps) == 1:
        return ps[0](next(args))
    else:
        return [p(next(args)) for p in ps]


def nlist(n, v):
    if not n:
        return [] if type(v) is list else v
    return [nlist(n[1:], v) for _ in range(n[0])]


def yesno(v):
    print(["Yes", "No"][not v])



def main():
    S, Q = input(str, int)
    front, end, state = "", "", 0
    for _ in range(Q):
        t = int(next(args))
        if t == 1:
            state = (state + 1) % 2
            continue
        F, C = int(next(args)), str(next(args))
        if state == 0:
            if F == 1:
                front = C + front
            else:
                end = end + C
        else:
            if F == 1:
                end = end + C
            else:
                front = C + front
    ans = front + S + end
    if state == 1:
        ans = end[::-1] + S[::-1] + front[::-1]
    print(ans)


if __name__ == '__main__':
    args = iter(sys.stdin.read().split())
    main()

Solution (Python):
s=input()
n=int(input())
q=[]
for _ in range(n):
  q.append(input().split())
sw=True 
sl=''
sr=''
for i in range(n):
  if q[i][0]=='1':
    sw = not sw
  else:
    if q[i][1]=='1' and sw==True:
      sl += q[i][2]
    elif q[i][1]=='2' and sw==False:
      sl += q[i][2]
    else:
      sr +=q[i][2]

ans=''
if sw:
  for i in range(len(sl)):
    ans+=sl[len(sl)-1-i]
  print(ans+s+sr)
else:
  for i in range(len(sr)):
    ans+=sr[len(sr)-1-i]
  for i in range(len(s)):
    ans+=s[len(s)-1-i]
  print(ans+sl)

Solution (Python):
import sys
input = lambda: sys.stdin.readline().rstrip() 
def main():
    moji=input()
    cycle=int(input())
    flont_or_back=0
    flont=""
    back=""
    for i in range(cycle):
        line=input()
        if line[0]=="1":
            flont_or_back+=1
        else:
            fi=line[2]
            ci=line[4]
            if flont_or_back%2==0:
                if fi=="1":
                    flont+=ci
                else:
                    back+=ci
            else:
                if fi=="1":
                    back+=ci
                else:
                    flont+=ci
    flont=flont[::-1]
    moji=flont+moji+back
    if flont_or_back%2==0:
        print(moji)
    else:
        print(moji[::-1])
    

if __name__ == '__main__':
    main()

Solution (Python):
moji=input()
cycle=int(input())
flont_or_back=0
flont=""
back=""
for i in range(cycle):
    line=input()
    if line[0]=="1":
        flont_or_back+=1
    else:
        fi=line[2]
        ci=line[4]
        if flont_or_back%2==0:
            if fi=="1":
                flont=ci+flont
            else:
                back+=ci
        else:
            if fi=="1":
                back+=ci
            else:
                flont=ci+flont
moji=flont+moji+back
if flont_or_back%2==0:
    print(moji)
else:
    print(moji[::-1])

Solution (Python):
import sys
def input():
    return sys.stdin.readline().rstrip()

moji=input()
cycle=int(input())
flont_or_back=0
flont=""
back=""
for i in range(cycle):
    line=input()
    if line[0]=="1":
        flont_or_back+=1
    else:
        fi=line[2]
        ci=line[4]
        if flont_or_back%2==0:
            if fi=="1":
                flont=ci+flont
            else:
                back+=ci
        else:
            if fi=="1":
                back+=ci
            else:
                flont=ci+flont
moji=flont+moji+back
if flont_or_back%2==0:
    print(moji)
else:
    print(moji[::-1])

Solution (Python):
import sys
def input():
    return sys.stdin.readline().rstrip()
def main():
    moji=input()
    cycle=int(input())
    flont_or_back=0
    flont=""
    back=""
    for i in range(cycle):
        line=input()
        if line[0]=="1":
            flont_or_back+=1
        else:
            fi=line[2]
            ci=line[4]
            if flont_or_back%2==0:
                if fi=="1":
                    flont=ci+flont
                else:
                    back+=ci
            else:
                if fi=="1":
                    back+=ci
                else:
                    flont=ci+flont
    moji=flont+moji+back
    if flont_or_back%2==0:
        print(moji)
    else:
        print(moji[::-1])
    

if __name__ == '__main__':
    main()

Solution (Python):
import sys
input = lambda: sys.stdin.readline().rstrip() 
def main():
    moji=input()
    cycle=int(input())
    flont_or_back=0
    flont=""
    back=""
    for i in range(cycle):
        line=input()
        if line[0]=="1":
            flont_or_back+=1
        else:
            fi=line[2]
            ci=line[4]
            if flont_or_back%2==0:
                if fi=="1":
                    flont=ci+flont
                else:
                    back+=ci
            else:
                if fi=="1":
                    back+=ci
                else:
                    flont=ci+flont
    moji=flont+moji+back
    if flont_or_back%2==0:
        print(moji)
    else:
        print(moji[::-1])
    

if __name__ == '__main__':
    main()

Solution (Python):
def main():
    moji=input()
    cycle=int(input())
    flont_or_back=0
    flont=""
    back=""
    for i in range(cycle):
        line=input()
        if line[0]=="1":
            flont_or_back+=1
        else:
            fi=line[2]
            ci=line[4]
            if flont_or_back%2==0:
                if fi=="1":
                    flont=ci+flont
                else:
                    back+=ci
            else:
                if fi=="1":
                    back+=ci
                else:
                    flont=ci+flont
    moji=flont+moji+back
    if flont_or_back%2==0:
        print(moji)
    else:
        print(moji[::-1])
    

if __name__ == '__main__':
    main()

Solution (Python):
def main():
    moji=input()
    cycle=int(input())
    flont_or_back=0
    flont=""
    back=""
    for i in range(cycle):
        line=input()
        if line[0]=="1":
            flont_or_back+=1
        else:
            fi=line[2]
            ci=line[4]
            if flont_or_back%2==0:
                if fi=="1":
                    flont=ci+flont
                else:
                    back+=ci
            else:
                if fi=="1":
                    back+=ci
                else:
                    flont=ci+flont
    moji=flont+moji+back
    if flont_or_back%2==0:
        print(moji)
    else:
        print(moji[::-1])
    

if __name__ == '__main__':
    main()

Solution (Python):
from collections import deque

S = list(input())
q = deque(S)
cnt_rev = 0
Q = int(input())
for _ in range(Q):
    T, *R = input().split()
    if T == "1":
        cnt_rev += 1
    else:
        F, c = R
        if cnt_rev % 2 == 0:
            if F == "1":
                q.appendleft(c)
            else:
                q.append(c)
        else:
            if F == "1":
                q.append(c)
            else:
                q.appendleft(c)

ans = list(q)
if cnt_rev % 2 == 1:
    ans.reverse()
print(*ans, sep="")

Solution (Python):
s = input()
q = int(input())
d = True
head = []
tail = []
for i in range(q):
    t, *u= input().split()
    if t == "1":
        d = not d
    else:
        f, c = u[0], u[1]
        if d:
            if f == "1":
                head.append(c)
            else:
                tail.append(c)
        else:
            if f == "1":
                tail.append(c)
            else:
                head.append(c)
s = "".join(head)[::-1] + s + "".join(tail)
print(s if d else s[::-1])

Solution (Python):
def main():
    end = list(input())
    n = int(input())
    start = []


    for i in range(n):
        query = input().split()
        q = int(query[0])
        if q == 1:
            start,end = end,start
        else:
            f = int(query[1])
            if f == 1:
                start.append(query[-1])
            else:
                end.append(query[-1])
    print(''.join(start[::-1] + end))
    return

if __name__ == "__main__":
    main()

Solution (Python):


import sys


from collections import deque                
















def main():
    mod = 1000000007                  
    inf = float('inf')
    sys.setrecursionlimit(10**6)    
    def input(): return sys.stdin.readline().rstrip()
    def ii():  return int(input())
    def mi():  return map(int, input().split())
    def mi_0(): return map(lambda x: int(x)-1, input().split())
    def lmi(): return list(map(int, input().split()))
    def lmi_0(): return list(map(lambda x: int(x)-1, input().split()))
    def li():  return list(input())
    
    
    rev = False
    s = input()
    dq = deque(s)
    n = ii()
    for _ in range(n):
        query = list(input().split())
        if len(query) == 1:
            rev = not rev
        else:
            _, num, char = query
            num_flag = True if num == '1' else False
            if (num_flag and not rev) or (not num_flag and rev):
                dq.appendleft(char)
            else:
                dq.append(char)
    L = list(reversed(dq)) if rev else list(dq)
    print(''.join(L))


if __name__ == "__main__":
    main()

Solution (Python):

S = input().split()
Q = int(input())

def pop(left, right, rev, f, c):
    if rev == 1:
        if f == '1':
            left.append(c)
        else:
            right.append(c)
    else:
        if f == '1':
            right.append(c)
        else:
            left.append(c)
    return left, right


left, right = [], []
rev = 1
for _ in range(Q):
    que = input().split()
    if que[0] == '1':
        rev *= -1
        continue
    if que[0] == '2':
        left, right = pop(left, right, rev, *que[1:])
print(''.join(left[::-1]+S+right)[::rev])

Solution (Python):
from collections import deque

S = input()
q = int(input())

is_s0_head = True
tail = len(S)-1

queue = deque(S)

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

if is_s0_head:
    print("".join(list(queue)))
else:
    print("".join(list(queue)[::-1]))

Solution (Python):
from collections import deque
s=deque(list(input()))
q=int(input())
l = [list(input().split()) for i in range(q)]
c=0
for i in range(q):
  if l[i][0]=="1":
    c+=1
  elif c%2==0 and l[i][1]=="1":
    s.appendleft(l[i][2])
  elif c%2==0:
    s.append(l[i][2])
  elif c%2==1 and l[i][1]=="1":
    s.append(l[i][2])
  else:
    s.appendleft(l[i][2])
s=list(s)
if c%2==1:
  s=s[::-1]
print(*s, sep="")

Solution (Python):
def Main(S, Q, q):
    pre = []
    suf = []
    flipped = False
    for i in range(Q):
        if q[i][0] == '1':
            if not flipped:
                flipped = True
            else:
                flipped = False
        elif q[i][0] == '2':
            if q[i][1] == '1':
                if not flipped:
                    pre.append(q[i][2])
                else:
                    suf.append(q[i][2])
            elif q[i][1] == '2':
                if not flipped:
                    suf.append(q[i][2])
                else:
                    pre.append(q[i][2])
    pre.reverse()
    ans = pre + list(S) + suf
    if flipped:
        ans.reverse()
    return "".join(ans)              
    
def main():
    S = str(input())
    Q = int(input())
    q = []
    for i in range(Q):
        q.append(list(map(str, input().split())))
    print(Main(S, Q, q))

if __name__ == '__main__':
    main()

Solution (Python):
from sys import stdin
from collections import deque
S=list(stdin.readline().strip())
d=deque(S)

N=list(map(int,stdin.readline().strip().split()))[0]
rev=False
for i in range(N):
    Q = list(stdin.readline().strip().split())
    if Q[0]=='1' :
        if rev==False:rev=True
        else:rev=False
    if Q[0]=='2':
        if Q[1]=='1':
            if rev==False:d.appendleft(Q[2])
            else:d.append(Q[2])
        if Q[1]=='2':
            if rev == False:
                d.append(Q[2])
            else:
                d.appendleft(Q[2])


if rev==False:[print(d.popleft(),end="") for x in range(len(d))]
else:[print(d.pop(),end="") for x in range(len(d))]

Solution (Python):
from sys import stdin
from collections import deque
d=deque(list(stdin.readline().strip()))
N=list(map(int,stdin.readline().strip().split()))[0]
rev=False
for i in range(N):
    Q = list(stdin.readline().strip().split())
    if Q[0]=='1' :
        if rev==False:rev=True
        else:rev=False
    if Q[0]=='2':
        if Q[1]=='1':
            if rev==False:d.appendleft(Q[2])
            else:d.append(Q[2])
        if Q[1]=='2':
            if rev == False:
                d.append(Q[2])
            else:
                d.appendleft(Q[2])


if rev==False:[print(d.popleft(),end="") for x in range(len(d))]
else:[print(d.pop(),end="") for x in range(len(d))]