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):
import sys
from collections import deque


def input():
    return sys.stdin.readline()[:-1]


def main():
    S = input()
    Q = int(input())
    que = deque(S)
    rev = 0
    for i in range(Q):
        q = input()
        if q[0] == '1':
            
            rev += 1
        else:
            _, f, c = q.split()
            if rev % 2 == 0:
                if f == '1':
                    que.appendleft(c)
                else:
                    que.append(c)
            else:
                if f == '1':
                    que.append(c)
                else:
                    que.appendleft(c)
    if rev % 2 == 1:
        que.reverse()
    print("".join(list(que)))


if __name__ == "__main__":
    main()

Solution (Python):
from collections import deque
S = input()
L = deque()
L.append(S)
Q = int(input())
is_lookforward = True
for i in range(Q):
    query = input()
    if query[0] == '1':
        is_lookforward = not is_lookforward
    else:
        F = int(query[2])
        C = query[4]
        if (F == 1 and is_lookforward) or (F == 2 and not is_lookforward):
            L.appendleft(C)

        else:
            L.append(C)
ans = ''.join(L)
print(ans if is_lookforward else ans[::-1])

Solution (Python):
import sys
import math
from collections import defaultdict

sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]

mod = 10**9 + 7

def I(): return int(input())
def LI(): return list(map(int, input().split()))
def LIR(row,col):
    if row <= 0:
        return [[] for _ in range(col)]
    elif col == 1:
        return [I() for _ in range(row)]
    else:
        read_all = [LI() for _ in range(row)]
        return map(list, zip(*read_all))



from collections import deque
from bisect import bisect_right
S = deque(list(str(input())))
Q = I()

one = []
isone = []
F = [0]*Q
C = [0]*Q
for i in range(Q):
    q = input().split()
    if q[0]=='1':
        one.append(i)
        isone.append(True)
    else:
        F[i] = (int(q[1]))
        C[i] = q[2]
        isone.append(False)


num = sum(isone)
if num%2==1:
    S = deque(list(reversed(S)))

bef = deque([])
aft = deque([])
for i,now in enumerate(isone):
    if now==1:
        if num%2==1:
            for b in reversed(bef):
                S.append(b)
            for a in aft:
                S.appendleft(a)
        else:
            for b in reversed(bef):
                S.appendleft(b)
            for a in aft:
                S.append(a)
        bef = deque([])
        aft = deque([])
        num -= 1
    else:
        if F[i]==1:
            bef.appendleft(C[i])
        else:
            aft.append(C[i])
for b in reversed(bef):
    S.appendleft(b)
for a in aft:
    S.append(a)
print(*S,sep='')

Solution (Python):
import sys
int1 = lambda x: int(x) - 1
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(500000)

S = readline().decode().rstrip()
q = int(readline())

head = ""
tail = ""
flip = 0
for _ in range(q):
    que = readline().decode().split()
    if len(que) == 1:
        flip += 1

    else:
        if que[1] == '1':
            if flip % 2 == 0:

                head = ''.join([que[2], head])
            else:
                tail = ''.join([tail, que[2]])

        else:
            if flip % 2 == 0:

                tail = ''.join([tail, que[2]])
            else:
                head = ''.join([que[2], head])

if flip % 2 == 0:
    print(''.join([head, S, tail]))
else:
    print(''.join([tail[::-1], S[::-1], head[::-1]]))

Solution (Python):
import collections
from collections import deque

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

s=deque(s)
flag=True
for i in range(n):
    if q[i][0]==str(1):
        if flag:
            flag=False
        else:
            flag=True
    else:
        if flag:
            if q[i][1]==str(1):
                s.appendleft(q[i][2])
            else:
                s.append(q[i][2])
        else:
            if q[i][1]==str(1):
                s.append(q[i][2])
            else:
                s.appendleft(q[i][2])

if not flag:
    s.reverse()
print(''.join(s))

Solution (Python):
from collections import deque

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

Query = [[0] * 3 for _ in range(Q)]
for i in range(Q):
    tmp = input().split()
    if tmp[0] == "1":
        Query[i][0] = 1
    elif tmp[0] == "2":
        Query[i][0] = 2
        Query[i][1] = int(tmp[1])
        Query[i][2] = tmp[2]

q = deque()
q.extend(S)
rev_flag = 0


for i in range(Q):
    if Query[i][0] == 1:
        rev_flag = (1 - rev_flag)
    else:
        if Query[i][1] == 1 and rev_flag == 0:
            q.appendleft(Query[i][2])
        elif Query[i][1] == 1 and rev_flag == 1:
            q.append(Query[i][2])
        elif Query[i][1] == 2 and rev_flag == 0:
            q.append(Query[i][2])
        elif Query[i][1] == 2 and rev_flag == 1:
            q.appendleft(Query[i][2])

if rev_flag == 1:
    q.reverse()
print("".join(list(q)))

Solution (Python):


import sys
import math
import time

import collections
from collections import deque
import queue
import copy
import bisect


def dfs(s, prev, pre):
  val[s] += Ope[s] + prev
  for i in G[s]:
    if(i == pre):
      continue
    dfs(i, val[s], s)

def bfs(sx, sy, n):
  que = deque([[sy,sx]])
  while que:
    y,x = que.popleft()
    for i,j in D:
      if(x+i<0 or y+j<0 or x+i>W-1 or y+j>H-1):
        continue
      dist = G[y+j][x+i]
      if(G[y+j][x+i] != "X"):
        if(type(G[y+j][x+i]) is str):
          G[y+j][x+i] = G[y][x]+1
          que.append([y+j,x+i])
        elif(dist>G[y][x]+1):
          G[y+j][x+i] = G[y][x]+1
          que.append([y+j,x+i])
        







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

S2 = deque(S)



for i in range(Q):
  Que = str(input()).split()
  if(int(Que[0])==1):
    flag = 1 - flag
  else:
    if(flag == 1):
      if(int(Que[1])==1):
        S2.append(Que[2])
      else:
        S2.appendleft(Que[2])
    else:
      if(int(Que[1])==1):
        S2.appendleft(Que[2])
      else:
        S2.append(Que[2])

S = "".join(list(S2))

if(flag == 1):
  S = S[::-1]

print(S)

Solution (Python):
from collections import deque

S = input()
Q = int(input())
W = 0
ans = deque(list(S.rstrip()))

for _ in range(Q):

    qry = list(input().rstrip().split())
    if qry[0] == "1":
        W = 1-W
        continue
    T,F,C = qry
    C.rstrip()
    if (W == 0 and F=="2") or (W==1 and F=="1"):
        ans.append(C)
    else:
        ans.appendleft(C)
ans = "".join(map(str,ans))
if W == 1:
    ans = ans[::-1]
print(ans)

Solution (Python):
S = list(input())
Q = int(input())
reverse = False
flont = []
for _ in range(Q):
    tmp = input().split()
    if len(tmp) == 1:
        reverse = not reverse
    else:
        F = int(tmp[1])
        C = tmp[2]
        if F == 1:
            if reverse:
                S.append(C)
            else:
                flont.append(C)
        else:
            if reverse:
                flont.append(C)
            else:
                S.append(C)
flont.reverse()
flont.extend(S)
if reverse:
    flont.reverse()
print(''.join(flont))

Solution (Python):
from collections import deque


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

    d = deque(S)

    r = False
    for _ in range(Q):
        q = input()
        if q == '1':
            r = not r
        else:
            _, b, c = q.split()
            if r:
                if b == '1':
                    d.append(c)
                else:
                    d.appendleft(c)
            else:
                if b == '1':
                    d.appendleft(c)
                else:
                    d.append(c)

    if r:
        d.reverse()
    print("".join(d))


if __name__ == "__main__":
    main()

Solution (Python):
s=input()
s1=""
s2=""
q=int(input())
Q=[input().split() for i in range(q)]

flg=1
for i in range(q):
    if Q[i][0]=="1":flg*=-1
    if Q[i][0]=="2":
        if Q[i][1]=="1" and flg==1: s1=Q[i][2]+s1
        if Q[i][1]=="2" and flg==-1:s1=Q[i][2]+s1
        if Q[i][1]=="1" and flg==-1:s2=s2+Q[i][2]
        if Q[i][1]=="2" and flg==1:s2=s2+Q[i][2]
s=s1+s+s2
if flg==-1:s=s[::-1]
print(s)

Solution (Python):
from collections import deque

s = input()
q = int(input())
queries = [input() for i in range(q)]

ans = deque(s)
flag_reverse = False

for i in range(q):
    query = queries[i].split()
    if query[0] == "1":
        flag_reverse = not flag_reverse
    else:
        if query[1] == "1" and not flag_reverse or query[1] == "2" and flag_reverse:
            ans.appendleft(query[2])
        else:
            ans.append(query[2])

if flag_reverse:
    ans.reverse()

print("".join(ans))

Solution (Python):
s=list(input())
q=int(input())
t1,t2=[],[]
cnt=0
for _ in range(q):
  t=list(input().split())
  if len(t)==1:
    cnt+=1
    t1,t2=t2,t1
  else:
    if int(t[1])==1:
      t1+=[t[2]]
    else:
      t2+=[t[2]]
if cnt%2==1:
  s=s[::-1]
u=t1[::-1]+s+t2
print(''.join(u))

Solution (Python):


from queue import Queue
from queue import LifoQueue as Stack
from math import sqrt, floor, ceil, log2
from fractions import gcd
from itertools import permutations, combinations
from operator import itemgetter
from functools import cmp_to_key



__MOD__=(10**9)+7
yn = 'YNeos'
judge = False
cnt = 0
ans = None


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

def intinput():
    return int(input())

def mulinputs():
    return map(int,input().split())

def lineinputs(func=intinput):
    datas = []
    while True:
        try:
            datas.append(func())
        except EOFError:
            break
    return datas

class ModInt():
    def __init__(self, x):
        self.__x = (x % __MOD__)
    def __add__(self, other):
        if type(other) == int:
            other = self.__class__(other)
            return int(self.__class__((self.__x + other.__x)%__MOD__))
        elif type(other) == ModInt:
            return self.__class__((self.__x + other.__x)%__MOD__)
        else:
            raise Exception("Not Int or Not ModInt")
    def __sub__(self, other):
        if type(other) == int:
            other = self.__class__(other)
            return int(self.__class__((self.__x - other.__x)%__MOD__))
        elif type(other) == ModInt:
            return self.__class__((self.__x - other.__x)%__MOD__)
        else:
            raise Exception("Not Int or Not ModInt")
    def __mul__(self, other):
        if type(other) == int:
            other = self.__class__(other)
            return int(self.__class__((self.__x * other.__x)%__MOD__))
        elif type(other) == ModInt:
            return self.__class__((self.__x * other.__x)%__MOD__)
        else:
            raise Exception("Not Int or Not ModInt")
    def __truediv__(self, other):
        if type(other) == int:
            other = self.__class__(other)
            return int(self.__class__((self.__x * other.__modinv())%__MOD__))
        elif type(other) == ModInt:
            return self.__class__((self.__x * other.__modinv())%__MOD__)
        else:
            raise Exception("Not Int or Not ModInt")
    def __pow__(self, other):
        if type(other) == int:
            other = self.__class__(other)
            return int(self.__class__(pow(self.__x, other.__x, __MOD__)))
        elif type(other) == ModInt:
            return self.__class__(pow(self.__x, other.__x, __MOD__))
        else:
            raise Exception("Not Int or Not ModInt")
    def __modinv(self, m=__MOD__):
        a = self.__x
        if a == 0:
            raise ZeroDivisionError()
        if gcd(a, m) != 1:
            raise Exception("%sの逆数は求まりません。" % a)
        b, u, v = m, 1, 0
        while b != 0:
            t = a//b;
            a -= t*b
            a, b = b, a
            u -= t * v
            u, v = v, u
        u %= m
        if u < 0:
            u += m
        return u
    def __int__(self):
        return self.__x



if __name__ == '__main__':

















    S = input()
    Q = intinput()
    queries = []
    prefix = []
    suffix = []
    direction = 0
    for q in range(Q):
        query = list(input().split())
        queries.append(query)
        if int(query[0]) == 1:
            direction += 1
        else:
            dl = int(query[1])
            dm = direction % 2
            if dl == 1 and dm == 0:
                
                prefix.append(query[2])
            elif dl == 1 and dm == 1:
                
                suffix.append(query[2])
            elif dl == 2 and dm == 0:
                
                suffix.append(query[2])
            else:
                
                prefix.append(query[2])

    if direction % 2 == 1:
        S = S[::-1]
        prefix, suffix = suffix, prefix

    p = "".join(prefix[::-1])
    s = "".join(suffix)
    print(p+S+s)

Solution (Python):

from sys import stdin
import sys

def getInputs():
	inputs=[]
	for line in stdin:
		line=line.split()
		inputs.append(line)
	return inputs

def rev(s):
	return s[-1::-1]



















class E:
	def __init__(self, c, pre, nex):
		self.c=c
		self.pre=pre
		self.nex=nex
		

	
def main(inputs):
	s=inputs[0][0]
	q=int(inputs[1][0])
	
	prev=None
	for ci,c in enumerate(s):
		e=E(c, prev, None)
		if prev is not None: prev.nex=e
		prev=e
		if ci==0:
			head=e
	tail=e
	
	size=len(s)
	order=0 
	for qi in range(q):
		ti=int(inputs[qi+2][0])
		if ti==1:
			order=1-order
		else:
			f=int(inputs[qi+2][1])
			c=inputs[qi+2][2]
			e=E(c, None, None)
			size+=1
			if f==1:
				if order==0:
					e.nex=head
					head.pre=e
					head=e
				else:
					e.pre=tail
					tail.nex=e
					tail=e
			else:
				if order==1:
					e.nex=head
					head.pre=e
					head=e
				else:
					e.pre=tail
					tail.nex=e
					tail=e
	
	cList=[None]*size
	if order==0:
		e=head
		for i in range(size):
			cList[i]=e.c
			e=e.nex
	else:
		e=tail
		for i in range(size):
			cList[i]=e.c
			e=e.pre
	
	print("".join(cList))
	

if __name__=="__main__":
	inputs=getInputs()

	main(inputs)

Solution (Python):

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

rev_flag = 1
head_str = ""
tail_str = ""
for _ in range(Q):
    query = list(map(str, input().split()))
    if query[0] == "1":
        rev_flag *= -1
    else:
        h_or_t_flag = 1 if query[1] == "1" else -1
        h_or_t_flag *= rev_flag
        
        if h_or_t_flag == 1:
            head_str = query[2] + head_str
        else:
            tail_str += query[2]

ans = head_str + S + tail_str
print(ans if rev_flag == 1 else ans[::-1])

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

    isReversed = False
    for i in range(Q):
        query = list(input().split())
        T = int(query[0])
        if T == 1:
            if isReversed:
                isReversed = False
            else:
                isReversed = True
        if T == 2:
            F = int(query[1])
            C = query[2]
            
            if (isReversed and F == 2) or (not isReversed and F == 1):
                S.appendleft(C)
            
            else:
                S.append(C)
        
    ans = ""
    if isReversed:
        ans = list(S)[::-1]
    else:
        ans = list(S)
        
    print("".join(ans))

if __name__ == '__main__':
    solve()

Solution (Python):
from collections import deque
S = input()
S = [S[i] for i in range(len(S))]
S = deque(S)                         
Q = int(input())
rev = 1
for i in range(Q):
  comm = input()
  if comm == "1":
    rev *= -1
  else:
    T, F, C = comm.split()
    F = int(F) * 2 - 3
    if rev * F == 1:
      S.append(C)
    else:
      S.appendleft(C)
S = list(S)
ans = ""
for i in range(len(S)):
  ans += S[i * rev + (rev-1) // 2]
print(ans)

Solution (Python):
from collections import deque

s = list(input())
s = deque(s)
q = int(input())
flag = ''
count = 0
for i in range(q):
    que = list(map(str,input().split()))
    if que[0] == "1":
        count += 1
        if flag == 'one':
          flag = ''
        else:
          flag = 'one'
    else:
        if que[1] == '1':
            if flag == 'one':
                s.append(que[2])
            else:
                s.appendleft(que[2])
        elif que[1] == '2':
            if flag == 'one':
                s.appendleft(que[2])
            else:
                s.append(que[2])
if count % 2 == 0:
  for i in s:
    print(i,end="")
else:
  for i in reversed(s):
    print(i,end="")

Solution (Python):
from collections import deque
S=list(input())
Q=int(input())
A=deque()
O=deque()
flag=0
for i in range(Q):
    q=input()
    if q=='1':
        if flag==0:
            flag=1
        else:
            flag=0
    else:
        t,f,c=q.split()
        if flag==0:
            if f=='1':
                A.appendleft(c)
            else:
                O.append(c)
        else:
            if f=='1':
                O.append(c)
            else:
                A.appendleft(c)
if flag==0:
    R=list(A)+S+list(O)
else:
    R=list(O)[::-1]+S[::-1]+list(A)[::-1]  
print(''.join(R))

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

prev = ""
aft = ""
rev = 0
for i in range(q):
	g = input()
	if len(g) == 1:
		rev += 1
	else:
		f = int(g[2])
		c = g[4]
		if f == 2:
			if rev%2 == 1:
				prev = c + prev
			else:
				aft = aft + c
		else:
			if rev%2 == 0:
				prev = c + prev
			else:
				aft = aft + c
if rev%2 == 1:
	print(aft[::-1]+s[::-1]+prev[::-1])
else:
	print(prev+s+aft)

Solution (Python):
from collections import deque

def switch(flag):
    if flag==1:
        return 2
    else:
        return 1

s = list(input())
n = int(input())


s = deque(s)

flag = 0
hanten = 1
counter = 0
for i in range(n):
    
    tmp = list(input().split())
    t = int(tmp[0])
    if len(tmp) == 3:
        f = int(tmp[1])
        c = tmp[2]
    

    if t == 1:
        hanten = switch(hanten)
        counter += 1
    if t == 2:
        if (f == 1 and hanten == 1) or (f==2 and hanten==2):
            s.appendleft(c)
        if (f == 2 and hanten == 1) or (f==1 and hanten ==2):
            s.append(c)
    
if counter%2 !=0:
    s.reverse()

print("".join(s))

Solution (Python):
from collections import deque

def main():
    S = deque(input())
    Q = int(input())
    revFlag = False
    for i in range(Q):
        tmp = list(input().split())
        if tmp[0] == "1":
            if revFlag:
                revFlag = False
            else:
                revFlag = True
        else:
            if tmp[1] == "1":
                if revFlag:
                    S.append(tmp[2])
                else:
                    S.appendleft(tmp[2])
            else:
                if revFlag:
                    S.appendleft(tmp[2])
                else:
                    S.append(tmp[2])
    if revFlag:
        S = list(S)
        S.reverse()
        print("".join(S))
    else:
        S = list(S)
        print("".join(S))


if __name__ == '__main__':
    main()

Solution (Python):
s = input()
q = int(input())
t = 0
form = ""
late = ""
for i in range(q):
  l = list(input().split())
  if l[0] == "1":
    t += 1
  else:
    if t % 2 == 0:
      if l[1] == "1":
        form = l[2] + form
      else:
        late += l[2]
    else:
      if l[1] == "1":
        late += l[2]
      else:
        form = l[2] + form
if t % 2 == 0:
  ans = form + s + late
else:
  ans = late[::-1] + s[::-1] + form[::-1]
print(ans)

Solution (Python):
from collections import deque
import sys

input = lambda: sys.stdin.readline().rstrip()

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

rev_cnt = 0
for i in range(q):
    query = input().split()
    if query[0] == '2':
        if (query[1] == '1' and rev_cnt % 2 == 0) or ((query[1] == '2' and rev_cnt % 2 == 1)):
            s.appendleft(query[2])
        else:
            s.append(query[2])
    else:
        rev_cnt += 1

if rev_cnt % 2 == 1:
    print(''.join([s[len(s) - i - 1] for i in range(len(s))]))
else:
    print(''.join([c for c in s]))

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

sg = 1
fr = []
ba = []
for x in range(q):
  query = [s for s in input().split()]
  if query[0] == '1':
    sg *= -1
  else:
    if int(query[1])*2 + sg == 3:
      fr.append(query[2])
    else:
      ba.append(query[2])

if sg == 1:
  fr.reverse()
  frn = ''.join(fr)
  ban = ''.join(ba)
  print(frn + s + ban)
else:
  ba.reverse()
  frn = ''.join(fr)
  ban = ''.join(ba)
  print(ban + ''.join(list(reversed(s))) + frn)

Solution (Python):
from collections import deque

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

deq = deque(S)

direction = 1

for _ in range(Q):
  q = input()
  if q == '1':
    direction *= -1
  else:
    _, f, c = q.split()
    if direction > 0:
      if f == '1':
        deq.appendleft(c)
      else:
        deq.append(c)
    else:
      if f == '1':
        deq.append(c)
      else:
        deq.appendleft(c)

if direction < 0:
  deq.reverse()
        
print(''.join(deq))

Solution (Python):
S = input()
Q = int(input())
mae_usiro = [[],[]]
rev = 0
for _ in range(Q):
    temp = input().split()
    T = temp[0]
    if T =="1":
        rev += 1
    else:
        F = temp[1]
        C = temp[2]
        if F == "1": 
            mae_usiro[rev % 2].append(C)
        else: 
            mae_usiro[(rev + 1) % 2].append(C)


mae =  mae_usiro[0]
usiro= mae_usiro[1]
if rev % 2 == 1: 
    S = S[::-1] 
    usiro.reverse()
    ans = "".join(usiro) + S + "".join(mae)
else:
    mae.reverse()
    ans = "".join(mae) + S + "".join(usiro)

print(ans)

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


def main():
    input = lambda: stdin.readline()[:-1]
    S = input()
    Q = int(input())

    s = deque(S)
    left, right = s.appendleft, s.append
    rev = 0
    for _ in [0] * Q:
        T = input().split()
        if T[0] == '1':
            left, right = right, left
            rev += 1
        else:
            f, c = T[1], T[2]
            if f == '1':
                left(c)
            else:
                right(c)

    if rev % 2:
        s.reverse()
    print(''.join(s))


main()

Solution (Python):

import sys
import math
import numpy as np
import itertools


s = list(input())

q = int(input())

t=0
a=[]
b=[]

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

    if query[0]=='1':
        t = (t+1)%2
    else:
        if (t+int(query[1]))%2==0:
            b.append(query[2])
        else:
            a.append(query[2])
p=[]

if t == 0:
    a.reverse()
    p.extend(a)
    p.extend(s)
    p.extend(b)
else:
    b.reverse()
    s.reverse()
    p.extend(b)
    p.extend(s)
    p.extend(a)

for i in p:
    print(i,end="")
print("")

Solution (Python):
s = input()
n = int(input())
rev = 0
rev_check = False
first = list()
last = list()
for i in range(n):
    q = list(input().split())
    if q[0] == "1":
        rev += 1
        rev_check = not rev_check
    else:
        if rev_check ==  True:
            if q[1] == "1":
                last.append(q[2])
            else:
                first.append(q[2])
        else:
            if q[1] == "1":
                first.append(q[2])
            else:
                last.append(q[2])
if rev_check == False:
    print("".join(reversed(first)) + s + "".join(last))
else:
    print("".join(reversed(last)) + s[::-1] + "".join(first))

Solution (Python):
S = input()
Q = int(input())
r = False
F = []
B = []
for i in range(Q):
  q = list(input().split())
  if q[0] == '1':
    if r:
      r = False
    else:
      r = True
  else:
    if (q[1] == '1' and not r) or (q[1] == '2' and r):
      F.append(q[2])
    else:
      B.append(q[2])

if r:
  print(''.join(B[::-1]+list(reversed(S))+F))
else:
  print(''.join(F[::-1]+list(S)+B))

Solution (Python):
from collections import deque

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

qs = [list(input()) for _ in range(q)]

d = deque([])

for c in s:
    d.append(c)

flag_r = False

for qq in qs:
    if qq[0] == '1':
        flag_r = not flag_r
    else:
        if flag_r:
            if qq[2] == '1':
                d.append(qq[4])
            else:
                d.appendleft(qq[4])
        else:
            if qq[2] == '1':
                d.appendleft(qq[4])
            else:
                d.append(qq[4])

if flag_r:
    d.reverse()

print(''.join(d))

Solution (Python):


def main():
    S = input()
    Q = int(input())
    flag = False
    head = []
    tail = []
    for n in range(Q):
        query = input()
        if query[0] == '1':
            flag = not flag
        elif query[0] == '2':
            command = query.split()
            F = int(command[1])
            C = command[2]
            if (not flag and F == 1) or (flag and F == 2):
                head.append(C)
            else:
                tail.append(C)
    result = ''.join(reversed(head)) + S + ''.join(tail)
    if flag:
        result = result[::-1]
    print(result)


if __name__ == '__main__':
    main()

Solution (Python):
from collections import deque
s=deque(list(input()))
q=int(input())
REVERSE=False
for _ in range(q):
  Input=input().split()
  if len(Input)==1:
    REVERSE = not REVERSE
  else:
    f=int(Input[1])
    c=Input[2]
    if not REVERSE:
      if f==1:
        s.appendleft(c)
      else:
        s.append(c)
    else:
      if f==1:
        s.append(c)
      else:
        s.appendleft(c)
if not REVERSE:
  print("".join(s))
else:
  print("".join(reversed(s)))

Solution (Python):
from collections import deque

s = deque(list(str(input())))
q = int(input())
que = [list(map(str, input().split())) for _ in range(q)]

r_cnt=0
for query in que:
    if len(query) == 1:
        r_cnt+=1
    else:
        if int(query[1]) == 1:
            if r_cnt % 2 == 1:
                s.append(query[2])
            else:
                s.appendleft(query[2])
        else:
            if r_cnt % 2 == 1:
                s.appendleft(query[2])
            else:
                s.append(query[2])

for _ in range(r_cnt % 2):
    s.reverse()

print(*s,sep='')

Solution (Python):
from collections import deque
flag = 1
s = deque(input())
q = int(input())
for i in range(q):
         z = list(map(str,input().split()))
         if z[0] == "1":
                  flag = flag*(-1)
         else:
                  if (flag == 1 and z[1] == "1")  or (flag == (-1) and z[1] == "2"):
                           s.appendleft(z[2])
                  else:
                           s.append(z[2])
if flag == (-1):
         s.reverse()
ans = "".join(s)
print(ans)

Solution (Python):
from collections import deque

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

for i in range(q):
    query = input()
    if query == "1":
        reversed_flag = not reversed_flag
    else:
        t, f, c = query.split()
        if f == "1":
            if reversed_flag:
                s.append(c)
            else:
                s.appendleft(c)
        else:
            if reversed_flag:
                s.appendleft(c)
            else:
                s.append(c)

if reversed_flag:
    s.reverse()

print("".join(s))

Solution (Python):
from collections import deque

S = deque(input())
Q = int(input())
re_flag = False 
start = deque()
finish = deque()
for i in range(Q):
    l = input()
    if l == "1":
        re_flag = not bool(re_flag)
    else:
        t, f, c = l.split()
        if f == "1":
            if re_flag:
                finish.append(c)
            else:
                start.append(c)
        else:
            if re_flag:
                start.append(c)
            else:
                finish.append(c)

S.extend(finish)
S.extendleft(start)

ans = deque()
if re_flag:
    ans.extendleft(S)
else:
    ans = S

print("".join(list(ans)))

Solution (Python):
from collections import deque

def main():
    S = deque(input())
    Q = int(input())
    rev_flag = False
    for _ in range(Q):
        query = input().split()
        if query[0] == "1":
            rev_flag = not(rev_flag)
            continue
        if query[1] == "1":
            if rev_flag:
                S.append(query[2])
            else:
                S.appendleft(query[2])
        else:
            if rev_flag:
                S.appendleft(query[2])
            else:
                S.append(query[2])
    
    if rev_flag:
        S.reverse()

    print("".join(list(S)))
        
if __name__ == "__main__":
    
    main()

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

S = list(sys.stdin.readline().strip())
Q = int(sys.stdin.readline().strip())

q = deque(S)

n_rev = 0
for _ in range(Q):
    tmp = sys.stdin.readline().strip().split()
    if tmp[0] == "1":
        n_rev += 1
    else:
        f, c = tmp[1], tmp[2]
        if f == "1":
            if n_rev % 2 == 0:
                
                q.appendleft(c)
            else:
                
                q.append(c)
        else:
            if n_rev % 2 == 0:
                
                q.append(c)
            else:
                
                q.appendleft(c)

S = "".join(list(q))
if n_rev % 2 == 0:
    print(S)
else:
    print(S[::-1])

Solution (Python):
S = input()
Q = int(input())
QUERY = []
for i in range(Q):
    
    q = input().split()
    if q[0] == '1':
        QUERY.append([int(q[0])])
    else:
        QUERY.append([int(q[0]), int(q[1]), q[2]])

st = []
en = []
rev = False
rev_c = 0
for q in QUERY:
    if q[0] == 1:
        if rev:
            rev = False
        else:
            rev = True
        rev_c += 1
    else:
        if q[1] == 1:
            if not rev:
                st.append(q[2])
            else:
                en.append(q[2])
        else:
            if not rev:
                en.append(q[2])
            else:
                st.append(q[2])




st_s = "".join(st)
st_s = st_s[::-1]
en_s = "".join(en)

ans = st_s + S + en_s
if rev_c % 2 == 0:
    pass
else:
    ans = ans[::-1]

print(ans)