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 collections import deque

def solve():
    S = input()
    Q = int(input())
    length = len(S)
    reverse = False
    first,last = deque([]),deque([])
    
    for _ in range(Q):
        query = input().split()
        if len(query) == 1:
            reverse = reverse^1
            first,last = last,first
        else:
            T,F,C = query
            T = int(T)
            F = int(F)
            if F == 1:
                if reverse:
                    first.append(C)
                else:
                    first.appendleft(C)
            else:
                if reverse:
                    last.appendleft(C)
                else:
                    last.append(C)
                
            length += 1
    
    if length == 1:
        print(S)
    else:
        first = list(first)
        last = list(last)
        first = first[::-1] if reverse else first
        mid = list(S)[::-1] if reverse else list(S)
        last = last[::-1] if reverse else last
        
        print(''.join(first+mid+last))
        
solve()

Solution (Python):
from collections import deque
S=input()
Q=int(input())
words=[]
for a in range(Q):
    A=input().split()
    words.append(A)
T=[a for a in S]
answers=deque(T)
number=0
for a in words:
    if a[0]=='1':
        number+=1
    else:
        if number%2==0:
            if a[1]=='1':
                answers.appendleft(a[2])
            else:
                answers.append(a[2])
        else:
            if a[1]=='1':
                answers.append(a[2])
            else:
                answers.appendleft(a[2])
if number%2==0:
    kotae=list(answers)
    moji=''
    for a in kotae:
        moji+=a
    print(moji)
else:
    kotae = list(answers)
    kotae.reverse()
    moji = ''
    for a in kotae:
        moji += a
    print(moji)

Solution (Python):
from collections import deque
ans=deque()
ans.append(str(input()))
q=int(input())
l=[list(map(str,input().split())) for i in range(q)]

c=0
for i in range(q):
	if l[i][0]=='1':
		c+=1
	else:
		if c%2==0:
			if l[i][1]=='1':
				ans.appendleft(l[i][2])
			else:
				ans.append(l[i][2])
		else:
			if l[i][1]=='2':
				ans.appendleft(l[i][2])
			else:
				ans.append(l[i][2])
A="".join(map(str,list(ans)))
if c%2==1:
	print(A[::-1])
else:
	print(A)

Solution (Python):
from collections import deque

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

rev = False

for _ in range(Q):
    T, F, C, *_ = input().split() + [None, None]

    if int(T) == 1:
        rev = not rev
    else:
        F = int(F)
        if rev:
            F = F % 2 + 1

    if F == 1:
        S.appendleft(C)
    else:
        S.append(C)

if rev:
    S.reverse()

print(*[x for x in S if x], sep='')

Solution (Python):
from collections import deque

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

rev = False

for _ in range(Q):
    T, F, C, *D = input().split() + [None, None]
    T = int(T)

    if T == 1:
        rev = not rev
    else:
        F = int(F)
        if rev:
            if F == 1:
                F = 2
            else:
                F = 1

    if F == 1:
        S.appendleft(C)
    else:
        S.append(C)

if rev:
    S.reverse()

print(*[x for x in list(S) if x], sep='')

Solution (Python):
S=str(input())
Q=int(input())
which=0
front=""
back=""
for i in range(Q):
    a=list(map(str,input().split()))
    if a==["1"]:
        which+=1
        which%=2
        continue
    else:
        if which==0:
            if a[1]=="1":
                front=a[2]+front
            if a[1]=="2":
                back+=a[2]
        if which==1:
            if a[1]=="1":
                back+=a[2]
            if a[1]=="2":
                front=a[2]+front
S=front+S+back
if which==1:
    print(S[::-1])
if which==0:
    print(S)

Solution (Python):
S=str(input())
Q=int(input())
which=0
front=""
back=""
for i in range(Q):
    a=list(map(str,input().split()))
    if a==["1"]:
        which+=1
        which%=2
        continue
    else:
        if which==0:
            if a[1]=="1":
                front=a[2]+front
            if a[1]=="2":
                back+=a[2]
        if which==1:
            if a[1]=="1":
                back+=a[2]
            if a[1]=="2":
                front=a[2]+front

S=front+S+back
if which==1:
    print(S[::-1])
if which==0:
    print(S)

Solution (Python):
S=str(input())
Q=int(input())
which=0
front=""
back=""
for i in range(Q):
    a=list(map(str,input().split()))
    if a==["1"]:
        which+=1
        which%=2
        continue
    else:
        if which==0:
            if a[1]=="1":
                front=a[2]+front
            if a[1]=="2":
                back+=a[2]
        if which==1:
            if a[1]=="1":
                back+=a[2]
            if a[1]=="2":
                front=a[2]+front

S=front+S+back
if which==1:
    print(S[::-1])
if which==0:
    print(S)

Solution (Python):
import math
import string
import itertools
import fractions
import heapq
import collections
import re
import array
import bisect
import sys
import random
import time
from collections import deque
inf = 10**9


def main():
    o = 1
    s = deque(input())
    q = int(input())
    for i in range(q):
        qu = input().split()
        if qu[0] == '1':
            o = int(not o)
        else:
            addo = 1 if qu[1] == '1' else 0
            if o == addo:
                s.appendleft(qu[2])
            else:
                s.append(qu[2])
        
    
    if o:
        print(''.join(s))
    else:
        s.reverse()
        print(''.join(s))




if __name__ == '__main__':
    main()

Solution (Python):
from collections import deque

def msf(s,queries):
    queue = deque(s)
    addl = queue.appendleft
    addr = queue.append
    is_reversed = 0

    for query in queries:
        if query[0] == '1':
            is_reversed ^= 1
            addl,addr = addr,addl
        elif query[0] == '2':
            f = query[1]
            c = query[2]
            if f == '1':
                addl(c)
            elif f == '2':
                addr(c)
    return ''.join(queue) if not is_reversed else ''.join(reversed(queue))

if __name__ == '__main__':
    s = list(input())
    q = int(input())
    ql = [input().split() for i in range(q)]
    print(msf(s,ql))

Solution (Python):



from collections import deque








s = deque(input())
q = int(input())
rev = False
for i in range(q):
    que = input()
    if que[0] == '1':
        rev = not rev
    else:
        que = list(que.split())
        if (int(que[1])==1 and not rev) or (int(que[1])==2 and rev):
            s.appendleft(que[2])
        else:
            s.append(que[2])

if rev: ans = ''.join(reversed(s))
else: ans = ''.join(s)
print(ans)

Solution (Python):



from collections import deque








s = deque(input())
q = int(input())
rev = False
for i in range(q):
    que = input()
    if que[0] == '1':
        rev = not rev
    else:
        que = list(que.split())
        if (int(que[1])==1 and not rev) or (int(que[1])==2 and rev):
            s.appendleft(que[2])
        else:
            s.append(que[2])

if rev: ans = ''.join(reversed(s))
else: ans = ''.join(s)
print(ans)

Solution (Python):
S = input()
Q = int(input())
reverse = False
front = ""
end = ""
 
for i in range(Q):
    inp = input()
    if inp[0] == "1":
        reverse = not reverse
    else:
        _, F, C = inp.split()
        if (F == "1") ^ reverse:
            front = front+C
        else:
            end = end+C
if reverse:
    print(end[::-1]+S[::-1]+front)
else:
    print(front[::-1]+S+end)

Solution (Python):
from collections import deque
S = list(input())
S = deque(S)
Q = int(input())
c = 0
for _ in range(Q):
    q = list(input().split())
    if int(q[0]) == 1:
        if c == 0:
            c = 1
        else:
            c = 0
    else:
        if c == 0:
            if int(q[1]) == 1:
                S.appendleft(q[2])
            else:
                S.append(q[2])
        else:
            if int(q[1]) == 2:
                S.appendleft(q[2])
            else:
                S.append(q[2])
S = list(S)
if c == 0:
    print("".join(S))
else:
    S = S[::-1]
    print("".join(S))

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

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

    d = deque(S)
    mode = True

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

        if query[0] == "1":
            mode = not mode
        elif query[0] == "2":
            F, C = query[1], query[2]
            if F == "1":
                if mode:
                    d.appendleft(C)
                else:
                    d.append(C)
            elif F == "2":
                if mode:
                    d.append(C)
                else:
                    d.appendleft(C)

    s = list(d)
    if not mode:
        s = s[::-1]

    print(''.join(s))

if __name__ == "__main__":
    main()

Solution (Python):
'''

from collections import deque
letter = deque(input())
Q= int(input())
reversed = False
for i in range(Q):
    query = input().split()
    if len(query)==1:
        reversed = not bool(reversed)
    else:
        if int(query[1])==1: 
            if reversed == False:
                letter.appendleft(query[2])
            else:
                letter.append(query[2])
        else:
            if reversed == False:
                letter.append(query[2])
            else:
                letter.appendleft(query[2])
if reversed==True:
    letter.reverse()

output = ''.join(letter)
print(output)
'''

from collections import deque
true_letter = input()
Q= int(input())
letter = deque(['*'])
reversed = False
for i in range(Q):
    query = input().split()
    if len(query)==1:
        reversed = not bool(reversed)
    else:
        if int(query[1])==1: 
            if reversed == False:
                letter.appendleft(query[2])
            else:
                letter.append(query[2])
        else:
            if reversed == False:
                letter.append(query[2])
            else:
                letter.appendleft(query[2])
if reversed==True:
    letter.reverse()
    true_letter = true_letter[::-1]

output = ''.join(letter)
output = output.replace('*',true_letter)
print(output)

Solution (Python):
from collections import deque
letter = deque(input())
Q= int(input())
reversed = False
for i in range(Q):
    query = input().split()
    if len(query)==1:
        reversed = not bool(reversed)
    else:
        if int(query[1])==1: 
            if reversed == False:
                letter.appendleft(query[2])
            else:
                letter.append(query[2])
        else:
            if reversed == False:
                letter.append(query[2])
            else:
                letter.appendleft(query[2])
if reversed==True:
    letter.reverse()

output = ''.join(letter)
print(output)

Solution (Python):
S = input()
Q = int(input())
line = True
first, last = '',''

for _ in range(Q):
    q = input().split()
    if q[0] == '1':
        line = True if line==False else False
    else:
        if line == True:
            if q[1] == '1':
                first = q[2] + first
            else:
                last = last + q[2]
        elif line==False:
            if q[1] == '1':
                last = last + q[2]
            else:
                first = q[2] + first
S = first+S+last
print(S if line else S[::-1])

Solution (Python):
from collections import deque

S = deque(input())
Q = int(input())
Query = [input().split() for _ in range(Q)]

reverse = False
for query in Query:
    if query[0] == '1':
        reverse = not reverse
    else:
        if query[1] == ('2' if reverse else '1'):
            S.appendleft(query[2])
        else:
            S.append(query[2])
print(''.join(reversed(S) if reverse else S))

Solution (Python):
s = input()
q = int(input())
flag = False
l = ''
r = ''
for _ in range(q):
  Q = input().split()
  if Q[0] == '1':
    flag = not flag
  else:
    if flag:
      if Q[1] == '1':
        r += Q[2]
      else:
        l += Q[2]
    else:
      if Q[1] == '1':
        l += Q[2]
      else:
        r += Q[2]
if flag:
  print(''.join(reversed(r)), ''.join(reversed(s)), l, sep='')
else:
  print(''.join(reversed(l)), s, r, sep='')

Solution (Python):
from collections import deque


s = deque(list(input()))
n = int(input())
flag = True
for _ in range(n):
    _input = input().split()
    if len(_input) == 1:
        if flag == True:
            flag = False
        else:
            flag = True
    else:
        if (_input[1] == '1' and flag == True) or (_input[1] == '2' and flag == False):
            s.appendleft(_input[2])
        else:
            s.append(_input[2])

if flag == True:
    print("".join(s))
else:
    print("".join([x for x in reversed(s)]))

Solution (Python):
from collections import deque

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

queries = [input().strip().split() for _ in range(q)]
is_reversed = False

for query in queries:
    t = query[0]
    if t == '1':
        is_reversed = not is_reversed
    else:
        f = query[1]
        c = query[2]
        if f == '1':
            if is_reversed:
                s.append(c)
            else:
                s.appendleft(c)
        else:
            if is_reversed:
                s.appendleft(c)
            else:
                s.append(c)

str_list = list(s)
if is_reversed:
    str_list = reversed(str_list)

ans = ''.join(str_list)
print(ans)

Solution (Python):
from collections import deque

s = input()
q = int(input())
is_reverse = False

d = deque(list(s))
for _ in range(q):
    qi = input()
    if len(qi) == 1:
        
        is_reverse = not is_reverse
    else:
        
        _, f, c = qi.split()
        f1 = f == '1'
        if f1 == is_reverse:
            d.append(c)
        else:
            d.appendleft(c)

d = list(d)
if is_reverse:
    d = d[::-1]
ans = ''.join(d)
print(ans)

Solution (Python):
from collections import deque

S = input()
Q = int(input())
rev = False
ans = deque(S)
for i in range(Q):
    tfc = input()
    if tfc == '1':
        rev = not rev
    else:
        t,f,c = tfc.split()
        if f == '1':
            if rev: ans.append(c)
            else: ans.appendleft(c)
        elif f == '2':
            if rev: ans.appendleft(c)
            else: ans.append(c)

if rev: ans.reverse()

print(''.join(ans))

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
















def get_result(data):
    S, Q = data[0][0], int(data[1][0])
    
    ans = deque(S)
    cnt = 0
    for i in range(Q):
        if data[i+2][0] == '1':
            cnt += 1
        else:
            F, C = data[i+2][1], data[i+2][2]
            if F == '1':
                if cnt % 2 == 0:
                    
                    ans.appendleft(C)
                else:
                    ans.append(C)
            else:
                if cnt % 2 == 0:
                    ans.append(C)
                else:
                    ans.appendleft(C)

    ans = ''.join(ans)
    if cnt % 2 == 1:
        ans = ans[::-1]

    return ans


if __name__ == '__main__':
    raw_data = [val.rstrip('\n') for val in stdin.readlines()]
    data = [list(map(str, val.split(' '))) for val in raw_data]
    result = get_result(data)
    print(result)

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
















def get_result(data):
    S, Q = data[0][0], int(data[1][0])
    
    ans = deque(S)
    cnt = 0
    for i in range(Q):
        if data[i+2][0] == '1':
            cnt += 1
        else:
            F, C = data[i+2][1], data[i+2][2]
            if F == '1':
                if cnt % 2 == 0:
                    ans.appendleft(C)
                else:
                    ans.append(C)
            else:
                if cnt % 2 == 0:
                    ans.append(C)
                else:
                    ans.appendleft(C)

    if cnt % 2 == 1:
        ans = ''.join(reversed(ans))
    else:
        ans = ''.join(ans)

    return ans


if __name__ == '__main__':
    raw_data = [val.rstrip('\n') for val in stdin.readlines()]
    data = [list(map(str, val.split(' '))) for val in raw_data]
    result = get_result(data)
    print(result)

Solution (Python):
from collections import deque

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

for i in range(q):
    cmd = input().split()
    if cmd[0] == '1':
        rev_f = (False if rev_f else True)
    else:
        f = cmd[1]
        c = cmd[2]
        if (rev_f and f == '1') or (not rev_f and f != '1'):
            s.appendleft(c)
        else:
            s.append(c)

print(''.join(s if rev_f else reversed(s)))

Solution (Python):
S,f=__import__('collections').deque(input()),0
for _ in range(int(input())):
	T=input()
	if'2'>T:f=1-f
	else:(S.appendleft if(T[2]<'2')^f else S.append)(T[4])
if f:S.reverse()
print(*S,sep='')

Solution (Python):
from collections import deque

def main():
    S = list(input())
    Q = int(input())
    l = [list(input().split(' ')) for _ in range(Q)]

    d = deque(S)

    FLAG = False

    for li in l:
        if int(li[0]) == 1: 
            if FLAG == True:
                FLAG = False
            else:
                FLAG = True
        
        elif int(li[0]) == 2: 
            if int(li[1]) == 1:
                if FLAG == True:
                    d.append(li[2])
                else:
                    d.appendleft(li[2])
            elif int(li[1]) == 2:
                if FLAG == True:
                    d.appendleft(li[2])
                else:
                    d.append(li[2])

    S = ''.join(d)
    if FLAG:
        S = S[::-1]
    print(S)


main()

Solution (Python):
S,f=__import__('collections').deque(input()),0
for _ in range(int(input())):
	T=input()
	if T<'2':f=1-f
	else:(S.appendleft if (T[2]<'2')^f else S.append)(T[4])
if f:S.reverse()
print(*S,sep='')

Solution (Python):
S=str(input())
S_list=list(S)

Q=int(input())

q=[]
right=[]
left=[]

rev=1

for i in range(Q):
    q.append(input().split())
    if q[i][0]=="1":
        rev=rev*(-1)
    elif q[i][0]=="2":
        if q[i][1]=="1":
            if rev==1:
                right.append(q[i][2])
            else:
                left.append(q[i][2])
        elif q[i][1]=="2":
            if rev==1:
                left.append(q[i][2])
            else:
                right.append(q[i][2])
right.reverse()
if rev==1:
    S_list=right+S_list+left
else:
    S_list=right+S_list+left
    S_list.reverse()

S=''.join(S_list)
print(S)

Solution (Python):
S = input()
l1 = []
l2 = []
rev = False
for _ in range(int(input())):
    l = [s for s in input().split()]
    if l[0] == "1":
        rev = not rev
    elif l[1] == "1":
        if rev:
            l2.append(l[2])
        else:
            l1.append(l[2])
    else:
        if rev:
            l1.append(l[2])
        else:
            l2.append(l[2])

if rev:
    ret = "".join(l2)[::-1] + S[::-1] + "".join(l1)
else:
    ret = "".join(l1)[::-1] + S + "".join(l2)

print(ret)

Solution (Python):
from collections import deque
S=deque(input())
q=int(input())
rev=False
for i in range(q):
  c=list(input().split())
  if c[0]=='1':
    rev=not rev
  elif c[1]=='1' and rev==False:
    S.appendleft(c[2])
  elif c[1]=='2' and rev==True:
    S.appendleft(c[2])
  else:
    S.append(c[2])
if rev:
  S.reverse()
ans=''.join(S)
print(ans)

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

pres = []
posts = []
mode = 0
for _ in range(q):
    query = input().split()

    if len(query) == 1:
        mode = 1 - mode
    else:
        if mode == 0:
            if query[1] == "1":
                pres.append(query[2])
            else:
                posts.append(query[2])
        else:
            if query[1] == "1":
                posts.append(query[2])
            else:
                pres.append(query[2])
s = "".join(pres[::-1]) + s + "".join(posts)
if mode == 1:
    s = s[::-1]
print(s)

Solution (Python):
S = input()
T = ''
Q = int(input())
flag = True
for i in range(Q):
    que = input()
    if len(que) == 1:
        if flag:
            flag = False
        else:
            flag = True
    else:
        if que[2] == '1':
            if flag:
                T = T+que[4]
            else:
                S = S+que[4]
        else:
            if flag:
                S = S+que[4]
            else:
                T = T+que[4]
if flag:
    U = ''
    for i in range(len(T)):
        U += T[len(T)-i-1]
    print(U+S)
else:
    U = ''
    for i in range(len(S)):
        U += S[len(S)-i-1]
    print(U+T)

Solution (Python):
import sys, math
from collections import deque

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

count_reverse = 0
for i in range(Q):
  q = [j for j in input().split()]
  if len(q) == 1:
    count_reverse += 1
  else:
    if q[1] == '1':
      if count_reverse % 2 == 0:
        S.appendleft(q[-1])
      else:
        S.append(q[-1])
    else:
      if count_reverse % 2 == 0:
        S.append(q[-1])
      else:
        S.appendleft(q[-1])
        
ret = ''
if count_reverse % 2:
  for s in list(S)[::-1]:
    ret = ret + s
else:
  for s in S:
    ret = ret + s
    
print(ret)

Solution (Python):
from collections import deque

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

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

if rev:
	S.reverse()

print("".join(S))

Solution (Python):
from collections import deque
s = deque(input())
q = int(input())
cnt = 0
for i in range(q):
  query = input()
  if query == '1':
    cnt += 1
  else:
    t, f, c = query.split()
    if (f == '1' and cnt % 2 == 0) or (f == '2' and cnt % 2 == 1):
      s.appendleft(c)
    else:
      s.append(c)
if cnt % 2 == 0:
  print(''.join(s))
else:
  s.reverse()
  print(''.join(s))

Solution (Python):
s=input()
n=int(input())
front=""
back=s
reverse=False

for i in range(n):
  t=input().split()
  if t[0]=="1":
    reverse=not reverse
  else:
    if (t[1]=="1")^reverse:
      front=front+t[2]
    else:
      back=back+t[2]
if reverse:
  print(back[::-1]+front)
else:
  print(front[::-1]+back)

Solution (Python):
S = input()
Q = int(input())
rev = False
head = []
tail = []
for i in range(Q):
    q = input()
    if q == '1':
        rev = not rev
    else:
        F, C = q.split()[1:]
        if (F == '1' and not rev) or (F == '2' and rev):
            head.append(C)
        else:
            tail.append(C)

head = ''.join(head)
tail = ''.join(tail)
S = head[::-1] + S + tail
if rev:
    S = S[::-1]
print(S)

Solution (Python):
from collections import deque

S = deque(input())
Q = int(input())
flip = False 
for i in range(Q):
  Query_i = list(map(str, input().split()))
  if Query_i[0]=='1':
    flip = not flip
  elif Query_i[1] == '1' and not flip:
    S.appendleft(Query_i[2])
  elif Query_i[1] == '1' and flip:
    S.append(Query_i[2])
  elif Query_i[1] == '2' and not flip:
    S.append(Query_i[2])
  elif Query_i[1] == '2' and flip:
    S.appendleft(Query_i[2])
if flip:
  S.reverse()
print(''.join(S))

Solution (Python):
S = input()
Q = int(input())
reverse = False
front = ""
end = S

for _ in range(Q):
    I = input().split()
    if I[0] == "1":
        reverse = not reverse
    else:
        if (I[1] == "1") ^ reverse:
            front =front + I[2]
        else:
            end = end + I[2]


if reverse:
    print(end[::-1]+front)
    
else:
    print(front[::-1]+end)

Solution (Python):
S = input()
Q = int(input())
reverse = False
front = ""
end = ""
 
for i in range(Q):
    inp = input()
    if inp[0] == "1":
        reverse = not reverse
    else:
        _, F, C = inp.split()
        if (F == "1") ^ reverse:
            front = front+C
        else:
            end = end+C
 
if reverse:
    print(end[::-1]+S[::-1]+front)
else:
    print(front[::-1]+S+end)

Solution (Python):
S = input()
Q = int(input())
reverse = False
front = ""
end = ""
 
for i in range(Q):
    inp = input().split()
    if inp[0] == "1":
        reverse = not reverse
    else:
        if (inp[1] == "1") ^ reverse:
            front = front+inp[2]
        else:
            end = end+inp[2]
 
if reverse:
    print(end[::-1]+S[::-1]+front)
else:
    print(front[::-1]+S+end)

Solution (Python):
S = input()
Q = int(input())
 
A = ""
B = ""
count = 0

Que = []
ope = []

def rev(s):
    re = ""
    for i in range(len(s)):
        re = re + s[len(s) - 1 - i]
    return re
 
for i in range(Q):
    k = input()
    if k == "1":
        count += 1
        Que.append("1")
        ope.append("0")
    else:
        Que.append("2")
        ope.append([k[2],k[4]])
 
cou = 0
 
if count % 2 == 0:
    for i in range(Q):
        if Que[i] == "1":
            cou += 1
        else:
            if (int(ope[i][0]) + cou) % 2 == 1:
                A = ope[i][1] + A
            else:
                B = B + ope[i][1]
else:
    for i in range(Q):
        if Que[i] == "1":
            cou += 1
        else:
            if (int(ope[i][0]) + cou) % 2 == 1:
                A = A + ope[i][1]
            else:
                B = ope[i][1] + B
 
if count % 2 == 0:
    S = A + S + B
else:
    S = rev(S)
    S = B + S + A
 
print(S)

Solution (Python):
import sys









def main():
    S = input()
    Q = int(input())
    print(solve(S, Q))


def solve(S, Q):
    input = sys.stdin.readline

    prefix = ''
    suffix = ''

    switch1 = 1
    switch2 = 1

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

        q0 = int(query[0])

        if q0 == 1:
            switch1 *= -1
            continue

        F = int(query[1])
        C = query[2]

        if F == 2:
            F = -1

        switch2 = switch1 * F

        if switch2 == 1:
            prefix = C + prefix

        else:
            suffix = suffix + C

    ans = prefix + S + suffix

    if switch1 == -1:
        ans = ans[::-1]

    return ans


if __name__ == '__main__':
    main()

Solution (Python):
from collections import deque

S = input()
Q = int(input())
ans = deque(S)
cnt = 0
for i in range(Q):
    qlst = list(map(str, input().split()))
    if qlst[0] == '1':
        cnt += 1
        continue
    else:
        if qlst[1] == '1':
            if cnt % 2 == 0:
                ans.appendleft(qlst[2])
            else:
                ans.append(qlst[2])
        else:
            if cnt % 2 == 0:
                ans.append(qlst[2])
            else:
                ans.appendleft(qlst[2])

if cnt % 2 == 1:
    print(''.join(reversed(ans)))
else:
    print(''.join(ans))

Solution (Python):

s = input()
q = int(input())
t = []
 
def F(f,fr,ba,c):
  if f == '1':
     return c + fr, ba
  if f == '2':
    return fr,ba + c

def R(r):
  if r == '1':return '2'
  if r == '2':return '1'

ans = s
fr = ''
ba = ''
r = '1'
for n in range(q):
  t = input().split()
  if t[0] == '1':
    r = R(r)
  else:
    if r =='2':
      t[1] = R(t[1])
    fr, ba = F(t[1],fr,ba,t[2])
 
    
if r == '1': 
   print(fr + ans + ba)
else:
  print(ba[::-1] + ans[::-1]+ fr[::-1])

Solution (Python):

S = input()

ql = []
for i in range(int(input())):
    ql.append(input())

rev = False
add_head = ""
add_tail = ""

for query in ql:
    if query == "1":
        rev = not rev
    else:
        _, tp, s2 = query.split(" ")

        if tp == "1":
            if not rev:
                add_head = s2 + add_head
            if rev:
                add_tail = add_tail + s2[::-1]
        else:
            if not rev:
                add_tail = add_tail + s2
            if rev:
                add_head = s2[::-1] + add_head

S = add_head + S + add_tail
if rev:
    S = S[::-1]

print(S)

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

d = deque()

S = stdin.readline().rstrip().split()
Q = [int(x) for x in stdin.readline().rstrip().split()]

d.append(S[0])

flag = 0b0

for i in range(Q[0]):
    s = input().split()

    if len(s) == 1:
        
        if len(d) == 1 and len(d[0]) == 1:
            pass
        else:
            flag = ~flag

        
        
        
        
            
        
        
        

    else:
        if int(s[1]) == 1:
            if flag == 0:
                d.appendleft(s[2])
            else:
                s[2] = s[2][::-1]
                
                d.append(s[2])
                
            
            

        else:
            if flag == 0:
                d.append(s[2])
            else:
                s[2] = s[2][::-1]
                
                d.appendleft(s[2])
            
            

ans = "".join(d)

if flag == -1:
    ans = ans[::-1]
    
    


print(ans)

Solution (Python):
from collections import deque
s = list(input())
q = int(input())
rv = False
fr = deque()
re = []
for i in range(q):
    t = input().split(' ')
    if t[0] == '1':
        rv = True if not rv else False
    else:
        if t[1] == '1' and not rv or t[1] == '2' and rv:
            fr.appendleft(t[2])
        else:
            if t[1] == '1' and rv or t[1] == '2' and not rv:
                re.append(t[2])
if rv:
    re.reverse()
    s.reverse()
    fr.reverse()
    print(''.join(re + s) + ''.join(fr))
else:
    print(''.join(fr) + ''.join(s + re))

Solution (Python):
from collections import deque

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

d = deque(['S'])



rev = False

for i in range(q):
    if query[i][0] == '1':
        rev = not rev
        continue
    
    if not rev:
        if query[i][0] == '2':
            if query[i][1] == '1':
                d.appendleft(query[i][2])
            else:
                d.append(query[i][2])
    else:
        if query[i][0] == '2':
            if query[i][1] == '1':
                d.append(query[i][2])
            else:
                d.appendleft(query[i][2])


if rev:
    
    s = s[::-1]



while len(d):
    if rev:
        tmp = d.pop()
    else:
        tmp = d.popleft()
    if tmp == 'S':
        
        print(s,end='')
    else:
        print(tmp,end='')