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()
count = int(input())
flg = False
top = ""
for i in range(count):
    tfc = input().split()
    if tfc[0] == "1":
        flg = not flg
    elif tfc[0] == "2":
        if (not flg and tfc[1] == "1") or (flg and tfc[1] == "2"):
            top = top + tfc[2]
        elif (flg and tfc[1] == "1") or (not flg and tfc[1] == "2"):
            s = s + tfc[2]

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

Solution (Python):
S = list(input())
Q = int(input())
q = [list(input().split()) for _ in range(Q)]
head = []
tail = []
flip = 0
for i in range(len(q)):

    if q[i][0] == "1":
        flip ^= 1
    else:
        if flip == 0 and q[i][1] == "1":
            head.append(q[i][2])
        elif flip == 0 and q[i][1] == "2":
            tail.append(q[i][2])
        elif flip == 1 and q[i][1] == "1":
            tail.append(q[i][2])
        else:
            head.append(q[i][2])

if flip == 0:
    ans = "".join(head[::-1] + S + tail)
    
    
    
    print( ans )
else:
    ans = "".join(tail[::-1] + S[::-1] + head)
    print( ans )

Solution (Python):
from collections import deque

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

d = deque(list(S))

rev = False

for q in range(Q):
    query = list(map(str, input().split()))
    if int(query[0]) == 1:
        if rev:
            rev = False
        else:
            rev = True
    else:
        if int(query[1]) == 1:
            if not rev:
                d.appendleft(query[2])
            else:
                d.append(query[2])
        else:
            if not rev:
                d.append(query[2])
            else:
                d.appendleft(query[2])

if rev:
    print(''.join(d)[::-1])
else:
    print(''.join(d))

Solution (Python):
a = input()
flag = 0
first = []
last = []
for i in range(int(input())):
    query = [j for j in input().split()]
    t = int(query[0])
    if t == 1:
        flag = 1 - flag
    else:
        x = int(query[1])
        if x == 1:
            if flag == 0:
                first.append(query[2])
            else:
                last.append(query[2])
        else:
            if flag == 0:
                last.append(query[2])
            else:
                first.append(query[2])

if flag == 0:
    print("".join(first)[::-1] + a + "".join(last))
else:
    print("".join(last)[::-1] + a[::-1] + ''.join(first))

Solution (Python):
S = input()
Q = int(input())
queries = []
for i in range(Q):
    query = [x for x in input().split()]
    query[0] = int(query[0])
    if query[0] == 2:
        query[1] = int(query[1])
    queries.append(query)

former = []
latter = []
is_normal = True
for query in queries:
    if query[0] == 1:
        is_normal = not is_normal
    
    else:
        if is_normal and query[1] == 1:
            former.append(query[2])
        elif not is_normal and query[1] == 1:
            latter.append(query[2])
        elif is_normal and query[1] == 2:
            latter.append(query[2])
        elif not is_normal and query[1] == 2:
            former.append(query[2])

if is_normal:
    ans = "".join(former[::-1]) + S + "".join(latter)
else:
    ans = "".join(latter[::-1]) + S[::-1] + "".join(former)

print(ans)

Solution (Python):
from collections import deque
S=deque(input())
Q=int(input())
ref=0
for i in range(Q):
    T=input().split()
    if T[0] == '2':
        if (T[1]=='1' and ref%2==0):
            S.appendleft(T[2])
        elif (T[1]=='2' and ref%2==0):
            S.append(T[2])
        elif (T[1]=='1' and ref%2==1):
            S.append(T[2])
        elif (T[1]=='2' and ref%2==1):
            S.appendleft(T[2])
    else:
        ref+=1
for i in range(len(S)):
    if ref%2==0:
        char=S.popleft()
        print(char,end='')
    else:
        char=S.pop()
        print(char,end='')

Solution (Python):

from collections import deque

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


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

    if query[0] == '1':
        
        flag = not flag
        continue

    if query[0] == '2':
        if (query[1] == '1' and flag is False) or (query[1] == '2' and flag is True):
            S.appendleft(query[2])
        else:
            S.append(query[2])

if flag is True:
    
    S.reverse()
print(''.join(S))

Solution (Python):
from collections import deque

def rev(s):
    new_s = list(reversed(s))
    return new_s

S = deque(input())
N = int(input())
r_flag=False
for _ in range(N):
    Q = input()
    if Q.isdigit():
        r_flag=not r_flag
    else:
        q = Q.split()
        if r_flag:
            if q[1]=="1":
                S.append(q[2])
            else:
                S.appendleft(q[2])
        else:
            if q[1]=="1":
                S.appendleft(q[2])
            else:
                S.append(q[2])
if r_flag:
    print(''.join(reversed(S)))
else:print(''.join(S))

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

guuki=0
ANS=S
ANS_back=""
ANS_front=""
for i in range(Q):
    A=list(input().split())
    if A[0]=="1":
        guuki+=1
    elif int(A[1])*guuki%2==1 or (A[1]=="2" and guuki%2==0):
        ANS_back+=A[2]
    else:
        ANS_front+=A[2]
if guuki%2==1:
    ANS=ANS[::-1]
    ANS_back,ANS_front=ANS_front,ANS_back
print(ANS_front[::-1]+ANS+ANS_back)

Solution (Python):
import copy
import math
import string

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

is_reverse = False
front = ""
back = ""
for i in range(Q):
    A = list(map(str,input().split()))
    A[0] = int(A[0])
    if A[0] == 1:
        is_reverse = (not is_reverse)
    elif A[0] == 2:
        A[1] = int(A[1])
        if A[1] == 1:
            if is_reverse:
                back += A[2]
            else:
                front += A[2]
        else:
            if is_reverse:
                front += A[2]
            else:
                back += A[2]
front = front[::-1]
S = front + S + back
if is_reverse:
    S = S[::-1]
print(S)

Solution (Python):
S = input()
Q = int(input())
before_chr = []
after_chr = []
reverse_status = 1
for i in range(Q):
    query = list(input().split())
    if query[0] == "1":
        reverse_status *= -1
    elif reverse_status * (1.5-int(query[1])) == 0.5:
        before_chr += query[2]
    else:
        after_chr += query[2]
if reverse_status == 1:
    print("".join(before_chr[::-1])+S+"".join(after_chr))
else:
    print("".join(after_chr[::-1])+S[::-1]+"".join(before_chr))

Solution (Python):
import collections
import math
import operator as op
from functools import reduce
from collections import Counter
import numpy as np
import math
import bisect
import heapq


MOD = 1000000007


def gcd(a, b):
    while b:
        a, b = b, a % b
    return a


def lcm(a, b):
    return a * b // gcd(a, b)


def get_inputs(cast_func=None):
    if cast_func is None:
        return input().split()
    else:
        return list(map(cast_func, input().split()))


def get_input(cast_func=None):
    if cast_func is None:
        return input()
    else:
        return cast_func(input())


def is_prime(n):
    if n == 1:
        return False
    for i in range(2, int(n**0.5)+1):
        if n % i == 0:
            return False
    return True


def main():
    S = get_input()
    Q = get_input(int)

    
    heads = ""
    tails = ""
    direction = 1
    for _ in range(Q):
        query = get_input()
        if query[0] == "1":
            direction *= -1
        else:
            _, F, C = query.split(" ")
            if F == "1":
                if direction == 1:
                    heads += C
                else:
                    tails += C
            else:
                if direction == 1:
                    tails += C
                else:
                    heads += C
     
    if direction == 1:
        print(heads[::-1] + S + tails)
    else:
        print(tails[::-1] + S[::-1] + heads)


if __name__ == '__main__':
    main()

Solution (Python):
from collections import deque
s = deque(i for i in input())
n = int(input())
l = [[i for i in input().split()] for _ in range(n)]

front = True

for i in l:
    if i[0] == '1':
        front = not front
    else:
        if front ^ (int(i[1]) == 1):
            s.append(i[2])
        else:
            s.appendleft(i[2])
            
ans = ''.join(s)

if front:
    print(ans)
else:
    ans = ans[::-1]
    print(ans)

Solution (Python):
import sys
import math

s = input()
Q = int(input())
query = []
[query.append(input().split()) for _ in range(Q)]

inversion = -1

if len(s) >= 3:
    head = s[0]
    tail = s[-1]
    body = s[1:-1]
    for i in range(Q):
        if query[i][0] == "1":
            inversion *= -1
            continue

        c = str(query[i][2])
        if query[i][1] == "1" and inversion == -1:
            head = c + head
        elif query[i][1] == "1" and inversion == 1:
            tail = tail + c
        elif query[i][1] == "2" and inversion == -1:
            tail = tail + c
        else:
            head = c + head
        
    if inversion == -1:
        print(head + body + tail)
    else:
        print(tail[::-1] + body[::-1] + head[::-1])

else:
        for i in range(Q):
            if query[i][0] == "1":
                inversion *= -1
                continue

            c = str(query[i][2])
            if query[i][1] == "1" and inversion == -1:
                s = c + s
            elif query[i][1] == "1" and inversion == 1:
                s = s + c
            elif query[i][1] == "2" and inversion == -1:
                s = s + c
            else:
                s = c + s
        
        if inversion == -1 :
            print(s)
        else:
            print(s[::-1])

Solution (Python):
S = input()
Q = int(input())
R = 1  
P = []
B = []
for _ in range(Q):
    Query = [i for i in input().split()]
    if Query[0] == '1':
        R *= -1
    else:
        if R == 1:
            if Query[1] == '1':
                P.append(Query[2])
            else:
                B.append(Query[2])
        else:
            if Query[1] == '1':
                B.append(Query[2][::-1])
            else:
                P.append(Query[2][::-1])

ans = ''.join(P[::-1])+S+''.join(B)
if R == -1:
    ans = ans[::-1]

print(ans)

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

flag = 0
for i in range(Q):
    query = input().split()
    if query[0] == '1':
        flag = flag ^ 1
    elif query[0] == '2':
        if flag == 0:
            if query[1] == '1':
                S.appendleft(query[2])
            else:
                S.append(query[2])
        else:
            if query[1] == '1':
                S.append(query[2])
            else:
                S.appendleft(query[2])
if flag == 0:
    print(''.join(S))
else:
    
    S.reverse()
    print(''.join(S))

Solution (Python):
import collections

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

Query = [input() for i in range(Q)]

deq = collections.deque(S)

flg = False
for q in Query:
    query = q.split(' ')
    if query[0]=='1':
        flg = not flg

    elif query[0] == '2':
        if query[1] == '1':
            if flg == True:
                deq.append(query[2])
            else:
                deq.appendleft(query[2])
        else:
            if flg==True:
               deq.appendleft(query[2])
            else:
               deq.append(query[2]) 
if flg == True:
    deq.reverse()        
print(''.join(deq))

Solution (Python):
s = input()
q = int(input())
fstr = []
bstr = []
bstr.extend(s)


sta = 0
for i in range(q):
  buff = input()
  if(buff[0] == "2"):
    tmp = buff[4]
    if(buff[2] == "2"):
      
      if(sta == 0):
        
        bstr.append(tmp)  
      else:
        fstr.append(tmp)
    else:
      if(sta == 1):
        bstr.append(tmp)  
      else:
        fstr.append(tmp)
  else:
    if(sta == 0):
      sta = 1
    else:
      sta = 0

fstr.reverse()
fstr.extend(bstr)
if(sta == 1):
  fstr.reverse()

print(''.join(fstr))

Solution (Python):
S=input()
Q=int(input())
Query=[input().split() for i in range(Q)]
bf=''
af=''
Flag=True

for i in range(Q):
    if Query[i][0] == '1':
        Flag = not Flag
    else:
        if Query[i][1] == '1':
            if Flag:
                bf = bf + Query[i][2]
            else:
                af = af + Query[i][2]
        else:
            if Flag:
                af = af + Query[i][2]
            else:
                bf = bf + Query[i][2]

ans = bf[::-1] + S + af

if not Flag:
    ans = ans[::-1]

print(ans)

Solution (Python):
from collections import deque

s = input()
q = int(input())
S = deque(s)
head = True

for i in range(q):
    li = input().split()
    if li[0] == '1':
        head = not head
    else:
        if head:
            if li[1] == '1':
                S.appendleft(li.pop())
            else:
                S.append(li.pop())
        else:
            if li[1] == '2':
                S.appendleft(li.pop())
            else:
                S.append(li.pop())
s = ''.join(S)
if not head:
    s = s[::-1]
print(s)

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

S = input().rstrip()
Q = int(input())
FTC = [tuple(input().split()) for _ in range(Q)]

que = deque(['?'])
cnt = 0
for query in FTC:
    if query[0] == '1':
        cnt ^= 1
        continue

    sign = 0 if query[1] == '1' else 1

    if sign ^ cnt == 0:
        que.appendleft(query[2])
    else:
        que.append(query[2])

que = list(que)
ans = ''.join(que[:: (-1)**cnt])
ans = ans.replace('?', S[:: (-1)**cnt])

print(ans)

Solution (Python):
from collections import deque

s = input()
q = int(input())
S = deque(s)
head = True

for i in range(q):
    li = input().split()
    if li[0] == '1':
        head = not head
    else:
        if head:
            if li[1] == '1':
                S.appendleft(li.pop())
            else:
                S.append(li.pop())
        else:
            if li[1] == '2':
                S.appendleft(li.pop())
            else:
                S.append(li.pop())
s = ''.join(S)
if not head:
    s = s[::-1]
print(s)

Solution (Python):
import sys
from collections import deque




s = deque([s for s in input()])
q = int(input())
rev = False

for query in sys.stdin:
    if query.strip() == '1': 
        if rev:
            rev = False
        else:
            rev = True
    else:
        t,f,c = query.split()
        if f == '1':
            if rev:
                s.append(c)
            else:
                s.appendleft(c)
        elif f == '2':
            if rev:
                s.appendleft(c)
            else:
                s.append(c)
if rev == True:
    s.reverse()
print(''.join(s))

Solution (Python):
s=input()
n=int(input())
isreverse=False
top=''

for i in range(n):
    ss=input().split()
    if len(ss)==1:
        isreverse=not isreverse
    else:
        if ss[1]=="1":
            if isreverse: s+=ss[2]
            else: top+=ss[2]
        else:
            if isreverse: top+=ss[2]
            else: s+=ss[2]
if isreverse:
    s=s[::-1]+top
else:
    s=top[::-1]+s


print(s)

Solution (Python):
from collections import deque

s_input = [char for char in input()]
q = int(input())
s = deque()
s_rev = deque()
s.extend(s_input)
s_rev.extendleft(s_input)
flag = True 
count_append = 0 
for i in range(q):
  query = input().split()
  if query[0] == "1":
    flag = not flag
  else:
    if flag:
      if query[1] == "1":
        s.appendleft(query[2])
        s_rev.append(query[2])
      else:
        s.append(query[2])
        s_rev.appendleft(query[2])
    else:
      if query[1] == "1":
        s.append(query[2])
        s_rev.appendleft(query[2])
      else:
        s.appendleft(query[2])
        s_rev.append(query[2])
    count_append += 1
s_output = ""
if flag:
  for i in range(len(s_input) + count_append):
    s_output += s.popleft()
else:
  for i in range(len(s_input) + count_append):
    s_output += s.pop()
print(s_output)

Solution (Python):

s=raw_input()
q=int(raw_input())
res=""
for i in range(q):
 x=raw_input().split()
 if x[0]=='1':
  res,s=s,res
 else:
  if x[1]=='1':
   res+=x[2]
  else:
   s+=x[2]
res=res[::-1]
print res+s

Solution (Python):
s = input()
n = int(input())
stat = 1
head = []
tail = []
for i in range(n):
    q = input().split()
    if len(q) == 1:
        stat = stat *(-1)
    else:
        if q[1] == "1":
            if stat == 1:
                head.append(q[2])
            else:
                tail.append(q[2])
        else:
            if stat == 1:
                tail.append(q[2])
            else:
                head.append(q[2])

if stat == 1:
    print("".join(head[::-1])+s+"".join(tail))
else:
    print("".join(tail[::-1])+s[::-1]+"".join(head))

Solution (Python):
from collections import deque
S = deque(input())
N = int(input())
is_inversed = False
for i in range(N):
    Q = input()
    if Q[0] == '1':
        is_inversed = not (is_inversed)
    else:
        if (Q[2] == '1' and is_inversed) or (Q[2] == '2' and not (is_inversed)):
            S.append(Q[4])
        else:
            S.appendleft(Q[4])
if is_inversed:
    print(''.join(list(S)[::-1]))
else:
    print(''.join(S))

Solution (Python):
from collections import deque

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

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

    print(result)

if __name__ == '__main__':
    main()

Solution (Python):


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



head = 0
pre = ''
post = ''

for _ in range(0, Q):
    q = input()
    if len(q.split()) == 1:
        head = 1 if head == 0 else 0
    else:
        _, f, c = q.split()
        if f == '1':
            
            if head == 0:
                pre = c + pre
            else:
                post = post + c
        else:
            
            if head == 1:
                pre = c + pre
            else:
                post = post + c

S = pre + S + post
print(S[::-1] if head == 1 else S)

Solution (Python):
from collections import deque

s = input()
q = int(input())
revflag = 1
d = deque(s)
for i in range(q):
	tfc = list(map(str, input().split()))
	if tfc[0] == '1':
		revflag *= -1
	elif tfc[1] == '1':
		if revflag > 0:
			d.appendleft(tfc[2])
		else:
			d.append(tfc[2])
	else:
		if revflag > 0:
			d.append(tfc[2])
		else:
			d.appendleft(tfc[2])
if revflag > 0:
	print(''.join(d))
else:
	print(''.join(reversed(d)))

Solution (Python):
from collections import deque
S = input()
d = deque(S)
Q = int(input())
rev = False
for _ in range(Q):
    Q, *sub = input().split()
    if Q == "1":
        rev = not rev
    else:
        F, C = sub
        if F == "1":
            if rev:
                d.append(C)
            else:
                d.appendleft(C)
        else:
            if rev:
                d.appendleft(C)
            else:
                d.append(C)
if rev:
    d.reverse()
print("".join(list(d)))

Solution (Python):
from collections import deque

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

for i in range(Q):
    A = list(input().split())
    if int(A[0]) == 1:
        L = -L
    if int(A[0]) == 2:
        A[1] = 2*int(A[1]) - 3
        if L*A[1] == -1:
            S.appendleft(A[2])
        else:
            S.append(A[2])

if L == 1:
    print(''.join(S))
else:
    print(''.join(list(reversed(S))))

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

S = deque(stdin.readline().rstrip())
Q = int(stdin.readline().rstrip())
query = [stdin.readline().rstrip().split() for _ in range(Q)]
front = 0

for q in query:
    if q[0] == '1':
        if front == 0:
            front = -1
        elif front == -1:
            front = 0
    elif q[0] == '2':
        if q[1] == '1':
            if front == 0:
                S.appendleft(q[2])
            elif front == -1:
                S.append(q[2])
        elif q[1] == '2':
            if front == 0:
                S.append(q[2])
            elif front == -1:
                S.appendleft(q[2])
if front == 0:
    print(''.join(S))
elif front == -1:
    S.reverse()
    print(''.join(S))

Solution (Python):


from collections import deque as dq

mode = True

def prob_d(s, q):
    ans = dq(s)
    for i in range(q):
        ans = ope(ans)
    if not mode:
        ans = ''.join(list(ans)[::-1])
    else:
        ans = ''.join(ans)
    print(ans)

def ope(s):
    global mode
    op = input().split()
    if len(op) == 1 and op[0] == '1':
        mode = not mode 
    elif len(op) == 3 and op[0] == '2':
        if op[1] == "1":
            if mode:
                s.appendleft(op[2])
            else:
                s.append(op[2])
        elif op[1] == "2":
            if mode:
                s.append(op[2])
            else:
                s.appendleft(op[2])
        else:
            pass
    else:
        pass
    return s

if __name__ == '__main__':
    s = input()
    q = int(input())
    prob_d(s, q)

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

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

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

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

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

print(b)

Solution (Python):
from collections import deque as dq
s = input()
nq = int(input())

s = dq(s)

jun = True
for i in range(nq):
  query = input().split()
  if query[0] == '1':
    jun = not jun
    continue
  if jun:
    if query[1] == '1':
      s.appendleft(query[2])
    else:
      s.append(query[2])
  else:
    if query[1] == '1':
      s.append(query[2])
    else:
      s.appendleft(query[2])
if jun:
  print(''.join(s))
else:
  print(''.join(list(s)[::-1]))

Solution (Python):
str = input()
times = int(input())
revers = 0
head = ""
tail = ""
for i in range(times):
    com = input().split()
    if com[0] == "1":
        if revers == 0:
            revers = 1
        else :
            revers = 0
        
    else :
        if com[1] == "1":
            if revers == 0:
                head = com[2]+head
            else :
                tail = tail + com[2]
        else : 
            if revers == 1:
                head = com[2]+head
            else :
                tail = tail + com[2]          
str = head + str + tail
if revers == 1:
    str = str[::-1]
                
print(str)

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

for i in range(0, Q):
    l = input().split()
    if len(l) == 1:
        perm += 1
    else:
        if (int(l[1])+perm) % 2 == 0:
            d.append(l[2])
        else:
            d.appendleft(l[2])
 
S0 = list(d)
if perm % 2 != 0:
    S0.reverse()
ans = ""
for i in range(0, len(S0)):
    ans = ans + S0[i]
print(ans)

Solution (Python):
from collections import deque
S = input()
Q = int(input())
Query = [input().split() for _ in range(Q)]
s = list(S)
d = deque(s)
d_is_reversed = False
for j in range(Q):
    if Query[j][0] == '1':
        d_is_reversed = not d_is_reversed
    elif Query[j][1] == '1':
        if d_is_reversed == False:
            d.appendleft(Query[j][2])
        else:
            d.append(Query[j][2])
    else:
        if d_is_reversed == False:
            d.append(Query[j][2])
        else:
            d.appendleft(Query[j][2])
if d_is_reversed == True:
    d.reverse()
printed = ''.join(d)
print(printed)

Solution (Python):
from collections import deque
words=deque(input())

order_amount=int(input())
flg=True
for _ in range(order_amount):
    orders=input()
    if orders[0]=='1':
        flg=not flg
    else:
        _,f,c=orders.split()
        if (f=='1' and flg) or (f=='2' and not flg):
            words.appendleft(c)
        elif (f=='2' and flg) or (f=='1' and not flg):
            words.append(c)
if not flg:
    words.reverse()
word=''.join(words)
print(word)

Solution (Python):
from collections import deque

S = deque(input())
Q = int(input())
flg = True
for _ in range(Q):
    query = input()
    if query[0] == "1":
        flg = not flg
    else:
        _,F,C = query.split()
        if (F == "1" and flg) or (F == "2" and not flg):
            S.appendleft(C)
        else:
            S.append(C)
if not flg:
    S.reverse()

print(''.join(S))

Solution (Python):
from collections import deque

s = deque(input())
n = int(input())
q_list = [input().split() for i in range(n)]
count = 1
for i in q_list:
    if i[0] == '1':
        count *= -1
    else:
        if i[1] == '1':
            if count == 1:
                s.appendleft(i[2])
            else:
                s.append(i[2])
        else:
            if count == 1:
                s.append(i[2])
            else:
                s.appendleft(i[2])
if count != 1:
    s.reverse()
print(*s, sep='')

Solution (Python):
from collections import deque

revFlag = False

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

for i in range(q):
    a,b,c, *_ = input().split() + [None, None]
    
    if(a == '1'):
        revFlag = not revFlag
        
    else:
        b = int(b)
        if(revFlag):
            b = (int(b)%2)+1
        
        if(b == 1):
            s.appendleft(c)
        else:
            s.append(c)
            
    

if(revFlag):
    s.reverse()
    
print(*s, sep='')

Solution (Python):
from collections import deque

string = input()
x = deque()
for s in string:
    x.append(s)

head_left = True
B = int(input())
for i in range(B):
    cmd = input().split()
    if len(cmd) == 1 and cmd[0] == "1":
        head_left = not head_left
    else:
        _, n, c = cmd
        if (n == "1" and head_left) or (n == "2" and not head_left):
            x.appendleft(c)
        else:
            x.append(c)

if head_left:
    print("".join(list(x)))
else:
    print("".join(reversed(list(x))))

Solution (Python):
from collections import deque
s = deque(input())
n = int(input())
initial = True
for i in range(n):
    order = list(input().split())
    if len(order) == 3:
        if initial:
            if int(order[1]) == 1:
                s.appendleft(order[2])
            else:
                s.append(order[2])
        else:
            if int(order[1]) == 1:
                s.append(order[2])
            else:
                s.appendleft(order[2])
    else:
        initial = not initial
    
if initial:
    print(''.join(s))
else:
    print(''.join(s)[::-1])

Solution (Python):
def resolve():
    from collections import deque
    s = deque(input())
    q = int(input())
    qlist = [input().split() for i in range(q)]
    flip = False
    for j in range(q):
        if qlist[j][0] == "1":
            flip = not flip
        elif qlist[j][1] == "1":
            if flip:
                s.append(qlist[j][2])
            else:
                s.appendleft(qlist[j][2])
        elif qlist[j][1] == "2":
            if flip:
                s.appendleft(qlist[j][2])
            else:
                s.append(qlist[j][2])
    if not flip:
        print("".join(s))
    else:
        s.reverse()
        print("".join(s))
resolve()

Solution (Python):
s = input()
q = int(input())
cnt = 0
l = ''
r = ''
for i in range(q):
    t = input()
    if t == '1':
        cnt = 1 - cnt
    else:
        t2, f, c = t.split()
        if cnt == 0:
            if f == '1':
                l = c + l
            else:
                r += c
        else:
            if f == '1':
                r += c
            else:
                l = c + l
                
if cnt == 0:
    print(l+s+r)
else:
    print(r[::-1]+s[::-1]+l[::-1])

Solution (Python):
from copy import deepcopy
from sys import exit,setrecursionlimit
import math
from collections import defaultdict,Counter,deque
from fractions import Fraction as frac
import fractions
from functools import reduce
from operator import mul
import bisect
import sys
import logging
import heapq
import itertools

logging.basicConfig(level=logging.ERROR)



setrecursionlimit(1000000)


def main():
    s =  input()
    querry = int(input())
    f = []
    l = []
    r = 0
    for _ in range(querry):
        q = input().split(' ')
        if(q[0] == '1'):
            r += 1
        else:
            if((int(q[1]) + r) % 2 == 0):
                l.append(q[2])
            else:
                f.append(q[2])
    for i in f[::-1] if r%2 == 0 else l[::-1]:
        print(i,end = '')
    print(s if r%2==0  else s[::-1],end = '')
    for i  in l if(r%2==0) else f:
        print(i,end='')
    print()

main()

Solution (Python):
from collections import deque

s = input()
q = int(input())
d = deque(s)
reverseFlag = False


def getQuery():
    yield input().split()


for _ in range(q):
    for query in getQuery():
        if query[0] == "1":
            reverseFlag = not reverseFlag
        else:
            if query[1] == "1":
                if reverseFlag:
                    d.append(query[2])
                else:
                    d.appendleft(query[2])
            else:
                if reverseFlag:
                    d.appendleft(query[2])
                else:
                    d.append(query[2])

ans = "".join(d)
if reverseFlag:
    ans = ans[::-1]

print(ans)