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
S = input()
Q = int(input())
querys = [input() for _ in range(Q)]
is_reverse = False
d = deque([S])

for q in querys:
    query = q.split()
    if len(query) == 1:
        is_reverse = not is_reverse
        continue
    _, f, c = query
    if is_reverse:
        
        if f == "1":
            d.append(c)
        elif f == "2":
            d.appendleft(c)
    else:
        if f == "1":
            d.appendleft(c)
        elif f == "2":
            d.append(c)
if is_reverse:
    print("".join(d)[::-1])
else:
    print("".join(d))

Solution (Python):
from collections import deque

d = deque()
S = input()
d.append(S)
Q = int(input())
Flag = True
 
for i in range(Q):
    Query = input()
    if Query[0] == "1":
        Flag = not Flag
    else:
        QueryList = Query.split()
        if Flag:
            if QueryList[1] =="1":
                d.appendleft(QueryList[2])
            else:
                d.append(QueryList[2])
        else:
            if QueryList[1] =="2":
                d.appendleft(QueryList[2])
            else:
                d.append(QueryList[2])

d_string = "".join(list(d))

if not Flag:
    d_string = d_string[::-1]    
print(d_string)

Solution (Python):
s = input()
q = int(input())
q1 = [list(map(str,input().split())) for i in range(q)]

ap = ["", ""]
revCount = 0

for j in q1:
  if j[0] == "1":
    revCount = revCount ^ 1

  else:
    ind = revCount ^ int(j[1]) - 1
    ap[ind] = ap[ind] + j[2]

if revCount == 0:
  ap[0] = ap[0][::-1]
  print(ap[0] + s + ap[1])

else:
  aps = (s+ap[1])[::-1]
  print(aps + ap[0])

Solution (Python):
from sys import stdin
S = input()
x = S
y =str()   
N = int(input())
Q =  [list(map(str, stdin.readline().split())) for _ in range(N)]
flag = True
for i in range(N):
    if Q[i][0]=="1":
        flag = not(flag)                
    else: 
        if (Q[i][1] == "2") == (flag == True):
            x += Q[i][2]
        else :
            y += Q[i][2]
M = "".join([y[::-1],x])
if flag == True:
    print(M)
else:
    print(M[::-1])

Solution (Python):

from collections import deque

S = deque(input())
Q = int(input())
queries = [input().rstrip('\n') for _ in range(Q)]
reverse = False

for query in queries:
    q = query.split(' ')
    if q[0] == '1':
        reverse = not reverse
    else:
        if q[1] == '1':
            S.appendleft(q[2]) if not reverse else S.append(q[2])
        else:
            S.append(q[2]) if not reverse else S.appendleft(q[2])

if reverse:
    S.reverse()
print("".join(S))

Solution (Python):
import sys
a = sys.stdin.read().split('\n')[:-1]
q = int(a[1])
f = 1
l = [[], []]
for i in range(2, q + 2):
    if a[i][0] == '1':
        f = 1 - f
    else:
        l[0 if a[i][2] == '21'[f] else 1].append(a[i][4])
if f != 1:
    l[1].reverse()
    print(''.join(l[1]) + ''.join(reversed(list(a[0]))) + ''.join(l[0]))
else:
    l[0].reverse()
    print(''.join(l[0]) + a[0] + ''.join(l[1]))

Solution (Python):


from collections import deque
S=list(input())
Q=int(input())
r=0

d=deque(S)

for _ in range(Q):
    temp=list(input().split())
    if len(temp)==1:
        r^=1
    else:
        if r==0:
            if temp[1]=="1":
                d.appendleft(temp[2])
            else:
                d.append(temp[2])
        else:
            if temp[1]=="1":
                d.append(temp[2])
            else:
                d.appendleft(temp[2])
ans=list(d)
print("".join(ans) if r==0 else "".join(ans[::-1]))

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.reverse()
    print("".join(S))


if __name__ == '__main__':
    main()

Solution (Python):

''':'
docker run --rm -it -v $(cd $(dirname $0) && pwd):/vo:ro pypy:3-2.4.0 \
   /bin/bash -c "pypy3 /vo/$(basename $0) < /vo/g.inp"
exit $?
'''
import sys
from collections import deque


def _s(): return sys.stdin.readline().strip()


def _sa(): return sys.stdin.readline().strip().split()


def _i(): return int(sys.stdin.readline().strip())


def main():
    s = deque([c for c in _s()])
    q = _i()
    flip = 0
    for _ in range(q):
        tmp = _sa()
        op = int(tmp[0])
        if op == 1:
            flip ^= 1
        else:
            loc = int(tmp[1]) - 1
            c = tmp[2]
            if flip ^ loc:
                s.append(c)
            else:
                s.appendleft(c)

    n = len(s)
    for _ in range(n):
        if flip == 0:
            print(s.popleft(), end="")
        else:
            print(s.pop(), end="")


if __name__ == "__main__":
    main()

Solution (Python):
from collections import deque

def solve():
    S = list(input())
    M = int(input())
    Q = [list(input().split()) for _ in range(M)]
    
    bef = deque([])
    s = deque(S)
    aft = deque([])
    c = 0
    
    for q in Q:
        if q[0] == '1':
            c += 1
        else:
            ad = q[2]
            if q[1] == '1':
                
                if c % 2 == 0:
                    bef.appendleft(ad)
                else:
                    aft.append(ad)
            else:
                
                if c % 2 == 0:
                    aft.append(ad)
                else:
                    bef.appendleft(ad)

    ans = ''.join(bef) + ''.join(s) + ''.join(aft)
    if c % 2 == 1:
        ans = ans[::-1]

    return ans

print(solve())

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

reverse = False
count = 0
mae = []
ushiro = []
for i in range(q):
    qu = input()
    if qu[0] == "1":
        count += 1
        if reverse:
            reverse = False
        else:
            reverse = True
    else:
        _, f, c = qu.split()
        if reverse:
            if f == "1":
                ushiro.append(c)
            else:
                mae.append(c)
        else:
            if f == "1":
                mae.append(c)
            else:
                ushiro.append(c)

s = "".join(mae[::-1]) + s + "".join(ushiro)

if count % 2:
    print(s[::-1])
else:
    print(s)

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

r = 0

B = []
A = []

for i in range(Q):
  if int(Qu[i][0]) == 1:
    r = 1 - r
  else:
    F = int(Qu[i][1])
    C = Qu[i][2]
    F -= 1
    if F == r:
      B.append(C)
    else:
      A.append(C)

if r == 0:
  B.reverse()
  print("".join(B + S + A))
else:
  A.reverse()
  S.reverse()
  print("".join(A + S + B))

Solution (Python):
S = input()
Q = int(input())
Q_list = list()
for i in range(Q):
    Q_list.append(input().split())

from collections import deque
def operate(i):
    global count, str_list
    if Q_list[i][0] == '1':
        count += 1
    else:
        if count % 2 == 0:
            if Q_list[i][1] == '1':
                str_list.appendleft(Q_list[i][2])
            else:
                str_list.append(Q_list[i][2])
        else:
            if Q_list[i][1] == '1':
                str_list.append(Q_list[i][2])
            else:
                str_list.appendleft(Q_list[i][2])

check = 0
for i in range(Q):
    if Q_list[i][0] == '1':
        check += 1

S_list = [i for i in S]
if check % 2 == 1:
    S_list = list(reversed(S_list))

S_n = ''.join(S_list)

str_list = deque()
str_list.append(S_n)
count = check%2
for i in range(Q):
    operate(i)
print(''.join(str_list))

Solution (Python):
from collections import deque

d = deque()
s = input()
d.append(s)
rev = 0
q = int(input())

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

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

print(ans)

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

rev = False

for _ in range(q):
    t = [x for x in input().split()]
    if t[0] == "1":
        rev = not rev
    else:
        if t[1] == "1":
            s.append(t[2]) if rev else s.appendleft(t[2])
        else:
            s.appendleft(t[2]) if rev else s.append(t[2])

if rev:
    s.reverse()
print("".join(s))

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

Solution (Python):
from collections import deque






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


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

ans = list(s)

if cnt % 2 == 1:
    ans = ans[::-1]

print(''.join(ans))

Solution (Python):
def resolve():
    from collections import deque
    d=deque()
    s=list(input())
    d.extend(s)
    
    q=int(input())
    Q=[list(map(str,input().split())) for i in range(q)]

    reverse= False
    for i in range(q):
        if int(Q[i][0])==1:
            reverse=not reverse
        elif int(Q[i][0])==2:
            if int(Q[i][1])==1:
                if reverse:
                    d.append(Q[i][2])
                else:
                    d.appendleft(Q[i][2])
                
            else:
                if reverse:
                    d.appendleft(Q[i][2])
                else:
                    d.append(Q[i][2])
                

    if reverse:
        print(''.join(list(reversed(d))))
    else:
        print(''.join(list(d)))






resolve()

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

li = [input() for i in range(q)]
k = 0
ans = [s]
ans = deque(ans)

for i in li:
    if i == "1":
        k += 1
    else:
        if (int(i[2]) + k) % 2 == 0:
            ans.append(i[4])
        else:
            ans.appendleft(i[4])

ans = list(ans)
s = "".join(ans)
if k % 2 == 0:
    print(s)
else:
    print(s[::-1])

Solution (Python):

import sys
def p(*_a):
  _s=" ".join(map(str,_a))
  
  sys.stderr.write(_s+"\n")

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

r=0

a=""
z=""

for i in range(q):
	t = input()
	
	if t=='1':
		if r==0:
			r=1
		else:
			r=0

		continue
	
	_,f,c = t.split()

	if f=='1':
		if r==0: a = a + c
		if r==1: z = z + c
	if f=='2':
		if r==0: z = z + c
		if r==1: a = a + c




ans=""
if (r==0):
	ans = a[::-1] + s + z
else:
	ans = z[::-1] + s[::-1] + a

print(ans)

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

s = deque(input().rstrip())

sw = True

nq = int(input().rstrip())
for _ in range(nq):
    query = input().rstrip()
    if query[0] == '1':
        sw = (sw == False)
    else:
        c = query[4]
        if (query[2] == '1') == sw: 
            s.appendleft(c)
        else:
            s.append(c)
if sw:
    print("".join(s))
else:
    print("".join(reversed(s)))

Solution (Python):
from collections import deque

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

conr = 0

for i in range(Q):
  q = list(input().split())
  if q[0] == "1":
    conr += 1
  else:
    s = q[-1]
    f = int(q[1])
    if conr % 2 == 0:
      if f == 1:
        D.appendleft(s)
      else:
        D.append(s)
    else:
      if f == 1:
        D.append(s)
      else:
        D.appendleft(s)

D = list(D)
if conr % 2 == 1:
  D = D[::-1]
ans = "".join(D)
print(ans)

Solution (Python):
S = input()
Q = int(input())
reverse = 1
head = ""
tail = ""
for i in range(Q):
    a = input().split()
    if a[0] == "1":
        reverse *= -1
    elif a[1] == "1":
        if reverse == 1:
            head += a[2]
        else:
            tail += a[2]
    else:
        if reverse == 1:
            tail += a[2]
        else:
            head += a[2]














S = head[::-1] + S + tail
if reverse == -1:
    S = S[::-1]
print(S)

Solution (Python):
from collections import deque

S = list(input())
Q = int(input())
Query = []
cnt_re = 0
for i in range(Q):
    tmp = input().split()
    Query.append(tmp)
    if (len(tmp) == 1):
        cnt_re += 1
prev = deque()
after = deque()
if (cnt_re % 2 != 0):
    S = list(reversed(S))
for q in Query:
    if (len(q) == 3):
        if q[1] == '1':
            if (cnt_re % 2 == 0):
                prev.appendleft(q[2])
            else:
                after.append(q[2])
        else:
            if (cnt_re % 2 == 0):
                after.append(q[2])
            else:
                prev.appendleft(q[2])
    else:
        cnt_re -= 1
print(''.join(list(prev)) + ''.join(list(S)) + ''.join(list(after)))

Solution (Python):
from collections import deque
s = input()
q = int(input())
flag = True
atama = []  
osiri = []
for i in range(q):
    inp = input()
    if inp == '1':
        flag = not flag

    else:
        _, f, c = map(str, inp.split())
        if f == '1':
            if flag:
                atama.append(c)
            else:
                osiri.append(c)
        else:
            if flag:
                osiri.append(c)
            else:
                atama.append(c)

if flag:
    for t in atama[::-1]:
        print(t, end='')
    print(s, end='')
    for t in osiri:
        print(t, end='')
else:
    for t in osiri[::-1]:
        print(t, end='')
    print(s[::-1],  end='')
    for t in atama:
        print(t, end='')

Solution (Python):


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

def addflont(s,c):
    text = "".join([c,s])
    return text
def addback(s,c):
    text = "".join([s,c])
    return text

reverse = 0
r = "";u = ""
for i in range(q):
    t = input()
    if t[0]=="1":
        reverse = reverse^1
    if t[0]=="2":
        if t[2]=="1":
            if reverse==0:
                r = addflont(r,t[4])
            else:
                u = addback(u,t[4])
        elif t[2]=="2":
            if reverse==0:
                u = addback(u,t[4])
            else:
                r = addflont(r,t[4])

s = r + s + u
if reverse==1:
    print(s[::-1])
else:
    print(s)

Solution (Python):



from fractions import gcd 
from functools import reduce
import sys
sys.setrecursionlimit(200000000)
from inspect import currentframe




def printargs2err(*args):
    names = {id(v):k for k,v in currentframe().f_back.f_locals.items()}
    print(', '.join(names.get(id(arg),'???')+' : '+repr(arg) for arg in args),file=sys.stderr)
def debug(*args):
    print(*args,file=sys.stderr)

def printglobals():
    for (symbol, value) in globals().items():
        print('symbol="%s"、value=%s' % (symbol, value),file=sys.stderr)
def printlocals():
    for (symbol, value) in locals().items():
        print('symbol="%s"、value=%s' % (symbol, value),file=sys.stderr)

def pin(type=int):
    return map(type,input().split())
     

from collections import deque
import sys
def resolve():
    S=deque(input())
    Q,=pin()
    ans=""
    turn=0
    for i in range(Q):
        q=sys.stdin.readline().rstrip().split()
        if q[0]=="1":turn=(turn+1)%2
        else:
            f=int(q[1])%2
            if f!=turn:
                S.appendleft(q[2])
            else:
                S.append(q[2])
    if turn==1:S.reverse()
    for s in S:
        ans+=s
    print(ans)
    


"""


"""
"""
お前カッコ閉じるの忘れてるだろ
"""
import sys
from io import StringIO
import unittest

class TestClass(unittest.TestCase):
    def assertIO(self, input, output):
        stdout, stdin = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(input)
        resolve()
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, stdin
        self.assertEqual(out, output)
    def test_入力例_1(self):
        input = """a
4
2 1 p
1
2 2 c
1"""
        output = """cpa"""
        self.assertIO(input, output)
    def test_入力例_2(self):
        input = """a
6
2 2 a
2 1 b
1
2 2 c
1
1"""
        output = """aabc"""
        self.assertIO(input, output)
    def test_入力例_3(self):
        input = """y
1
2 1 x"""
        output = """xy"""
        self.assertIO(input, output)

if __name__=="__main__":resolve()

Solution (Python):
from collections import deque
d = deque(input())
q = int(input())
reverse = False
for _ in range(q):
    query = list(input().split())
    if query[0] == '1':
        if reverse:
            reverse = False
        else:
            reverse = True
        continue
    else:
        if query[1] == '1':
            if reverse:
                d.append(query[2])
            else:
                d.appendleft(query[2])
        else:
            if reverse:
                d.appendleft(query[2])
            else:
                d.append(query[2])

if reverse:
    d = list(reversed(d))

print(''.join(map(str, d)))

Solution (Python):
from collections import deque
s = input()
q = int(input())
reverse_now = 0
first = deque([])
latter = ""
for i in range(q):
    k = list(input().split())
    if k[0] == "1":
        reverse_now = 1 - reverse_now
    else:
        f,c = k[1], k[2]
        if f == "1" and reverse_now == 0:
            first.appendleft(c)
        if f == "1" and reverse_now == 1:
            latter += c
        if f == "2" and reverse_now == 0:
            latter +=  c
        if f == "2" and reverse_now == 1:
            first.appendleft(c)
first = "".join(first)
if reverse_now == 1:
    ans = list(first + s + latter)
    ans.reverse()
    ans = "".join(ans)
else:
    ans = first + s + latter
print(ans)

Solution (Python):
from collections import deque
S = input()
aaa = list(S)
aaa.reverse()
sss = "".join(aaa)

Q = int(input())
d = deque()
d.append(S)
rev = 1
cnt = 0

for i in range(Q):
  List = list(input().split())
  a = int(List[0])
  if a == 1:
    rev = rev * -1
  else:
    b = int(List[1])
    if (b == 1 and rev == 1) or (b == 2 and rev == -1):
      d.appendleft(List[2])
      cnt = cnt + 1
    else:
      d.append(List[2])

if rev == -1:
  d[cnt] = sss
  d.reverse()
string = "".join(list(d))
print(string)

Solution (Python):
from collections import deque
s = deque(list(input()))
n = int(input())
flag = True
for _ in range(n):
    q = list(map(str, input().split()))
    if len(q) == 1:
        if flag:
            flag = False
        else:
            flag = True
    if len(q) == 3:
        if (q[1] == "1" and flag == False) or (q[1] == "2" and flag):
            s.append(q[2])
        else:
            s.appendleft(q[2])
if flag == False:
    s.reverse()
print("".join(s))

Solution (Python):
import sys
from collections import deque

def main():
    input = sys.stdin.readline

    s = input()[:-1]
    q = int(input())
    tmp = [input().split() for _ in range(q)]

    flag = 0
    ans = deque([s])
    for t in tmp:
        if len(t) == 1:
            flag = ~flag 
        else:
            f = int(t[1])
            c = t[2]
            if (f == 1 and flag == 0) or (f ==2 and flag == -1):
                ans.appendleft(c)
            else:
                ans.append(c)
    if flag == 0:
        print(''.join(ans))
    else:
        print(''.join(ans)[::-1])

if __name__ == '__main__':
    main()

Solution (Python):

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

count = True
ans = S

ireru_mae = ''
ireru_usiro = ''
for i in range(Q):
    T = input().split()
    if len(T)==1:
        count = not(count)
        continue

    if count:
        if T[1] == '1': ireru_mae = T[2] + ireru_mae
        else: ireru_usiro += T[2]
    else:
        if T[1] == '1': ireru_usiro += T[2]
        else: ireru_mae = T[2] + ireru_mae

ans = ireru_mae + ans + ireru_usiro
if count:
    print(ans)
else:
    print(ans[::-1])

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

Solution (Python):
from collections import deque

s = str(input())
q = int(input())
flip = -1
strf = deque()
strb = deque()

for i in range(q):
    query = list(map(str, input().split()))
    if query[0] == '1':
        flip = flip * -1
    elif query[0] == '2':
        if query[1] == '1':
            if flip == -1:
                strf.appendleft(query[2])
            else:
                strb.append(query[2])
        elif query[1] == '2':
            if flip == -1:
                strb.append(query[2])
            else:
                strf.appendleft(query[2])



ans = ''.join(strf) + s + ''.join(strb)
if flip == 1:
    ans = ans[::-1]
print(ans)

Solution (Python):
from collections import deque
S = input()
Q = int(input())
query = [list(input().split()) for _ in range(Q)]
reversecount = 0
d = deque(S)
for x in query:
    if int(x[0]) == 1:
        reversecount += 1
    else:
        if (int(x[1]) == 1 and (reversecount % 2 == 0)) or (int(x[1]) == 2 and (reversecount % 2 == 1)):
            d.appendleft(x[2])
        else:
            d.append(x[2])
if reversecount % 2 == 1:
    d.reverse()
print(*d, sep='')

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

fwd = ""
rev = ""
now = 1

for i in range(Q) :
  Qn = list(map(str, input().split()))
  
  if Qn == ["1"] :
    now *=-1
  else :
    if now == 1 :
      if Qn[1] == "1" :
        fwd = Qn[2] + fwd
      if Qn[1] == "2" :
        rev = rev + Qn[2]
    else : 
      if Qn[1] == "1" :
        rev = rev + Qn[2]
      if Qn[1] == "2" :
        fwd = Qn[2] + fwd

ans = fwd + S + rev
if now == -1 :
  ans = ans[::-1]
print(ans)

Solution (Python):
import sys
from collections import deque

s = list(sys.stdin.readline().rstrip())
q = int(sys.stdin.readline())
head = 0
s = deque(s)

for _ in range(q):
    query = sys.stdin.readline().split()
    if len(query) == 1:
        head = 1 if head == 0 else 0
    elif (query[1] == '1' and head == 1) or (query[1] == '2' and head == 0):
        s.append(query[2])
    else:
        s.appendleft(query[2])

if head == 1:
    s.reverse()

print(''.join(s))

Solution (Python):
s = input()
q = int(input())
c = 0
t = ""
u = ""
for i in range(q):
    qy = list(input().split())
    if qy[0] == "1":
        c += 1
    else:
        if (qy[1] == "1" and c % 2 == 0) or (qy[1] == "2" and c % 2 == 1):
            t = qy[2] + t
        else:
            u = u + qy[2]
ans = t + s + u
if c % 2 == 0:
    print(t + s + u)
else:
    print(ans[::-1])

Solution (Python):
S = input()
Q = int(input())
s = 0
s1 = []
s2 = []
for i in range(Q):
    q = list(input().split())
    s += int(q[0])
    if q[0]=='2':
        s_i = s + int(q[1]) 
        if s_i%2 == 0:
            s1.append(q[2])
        else:
            s2.append(q[2])
s1 = ''.join(s1)
s2 = ''.join(s2)
s2 = s2[::-1]
S = s2 + S + s1
if s%2 == 0:
    print(S)
else:
    print(S[::-1])

Solution (Python):
from queue import deque
s = deque([i for i in input()])
Q = int(input())
 
f = 0
 
for _ in range(Q):
    q = input()
    if int(q[0]) == 1:
        f = abs(f-1)
        continue
    
    _, F, c = q.split()
    if f+int(F) % 2 == 1:
        s.appendleft(c)
    else:
        s.append(c)
        
if f:
    print(''.join(reversed(s)))
else:
    print(''.join(s))

Solution (Python):
from collections import deque

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

for _ in range(q):
  Q = [x for x in input().split()]
  if Q[0] == '1':
    flg += 1
  else:
    if (Q[1] == '1' and flg%2==0) or (Q[1] == '2' and flg%2==1):
      s.appendleft(Q[2])
    else:
      s.append(Q[2])
if flg%2==1:
  s.reverse()

print('' . join(s))

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

hanten = 0
sen = ""
mat = ""
for i in range(q):
    x = input()
    if x == "1":
        if hanten == 0:
            hanten = 1
        else:
            hanten = 0
    else:
        if x[2] == "1":
            if hanten == 0:
                sen += x[4]
            else:
                mat += x[4]
        else:
            if hanten == 0:
                mat += x[4]
            else:
                sen += x[4]

if hanten == 0:
    print(sen[::-1] + s + mat)
else:
    print(mat[::-1] + s[::-1] + sen)

Solution (Python):

get_str = list(input())


N = int(input())


r_flag = 0

left_str = []
right_str = []

for i in range(N):
    
    inp_cmd = list(input().split())
    
    if(inp_cmd[0] == '1'):
        r_flag = (r_flag+1) % 2
    else:
        
        
        
        if((inp_cmd[1] == '1' and r_flag == 0) or (inp_cmd[1] == '2' and r_flag == 1)):
            left_str.append(inp_cmd[2])
        
        else:
            right_str.append(inp_cmd[2])


left_str = left_str[::-1]


list_str = []
list_str = left_str + get_str + right_str


if(r_flag == 1):
    list_str = list_str[::-1]

out_str = ''.join(list_str)
print(out_str)

Solution (Python):

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

flip = 0
ad1 = []
ad2 = []
for i in range(Q):
  st = input()
  if(st[0] == '1'):
    flip += 1
  else:
    if((st[2] == '1' and flip % 2 == 0) or (st[2] == '2' and flip % 2 == 1)):
      
      ad1.append(st[4])
    else:
      
      ad2.append(st[4])

ft = ''.join(ad1)
ft = ft[::-1]
ed = ''.join(ad2)

S = ft + S + ed

if(flip % 2 != 0):
  S = S[::-1]
print(S)

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

flag = False
for _ in range(Q):
    q = list(input().split())
    if q[0] == '1':
        flag = True if flag is False else False
    else:
        if flag is False:
            if q[1] == '1':
                S.appendleft(q[2])
            else:
                S.append(q[2])
        else:
            if q[1] == '1':
                S.append(q[2])
            else:
                S.appendleft(q[2])

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

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

Solution (Python):
from collections import deque

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

conr = 0

for i in range(Q):
  q = list(input().split())
  if q[0] == "1":
    conr += 1
  else:
    s = q[-1]
    f = int(q[1])
    if conr % 2 == 0:
      if f == 1:
        D.appendleft(s)
      else:
        D.append(s)
    else:
      if f == 1:
        D.append(s)
      else:
        D.appendleft(s)

D = list(D)
if conr % 2 == 1:
  D = D[::-1]
ans = "".join(D)
print(ans)

Solution (Python):


from collections import deque

S = input().strip()
Q = int(input().strip())
Q_list = [input().strip() for _ in range(Q)]




order=1

dq = deque()

for word in S:
    dq.append(word)

for line in Q_list:
    if line == "1":
        order *= -1
    else:
        t,f,c = line.split()
        
        if order == 1 and f == "1":
            dq.appendleft(c)
        elif order == 1 and f == "2":
            dq.append(c)
        elif order == -1 and f == "1":
            dq.append(c)
        elif order == -1 and f == "2":
            dq.appendleft(c)

if order == 1:
    print( *list(dq), sep="" )
else:
    print( *list(reversed(dq)), sep="" )

Solution (Python):
from collections import deque
S=deque(input())
Q=int(input())
r='1'
for _ in range(Q):
    target = input()
    if target == '1':
        if r=='1':
            r='2'
        else:
            r='1'
    else:
        a,b,c=target.split()
        if b=='1' and r=='1':
            S.appendleft(c)
        elif b=='2' and r=='1':
            S.append(c)
        elif b=='1' and r=='2':
            S.append(c)
        else:
            S.appendleft(c)
if r=='2':
    S.reverse()
print(''.join(list(S)))

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

fwd = ""
rev = ""
now = 1

for i in range(Q) :
  Qn = list(map(str, input().split()))
  
  if Qn == ["1"] :
    now *=-1
  else :
    if now == 1 :
      if Qn[1] == "1" :
        fwd = Qn[2] + fwd
      if Qn[1] == "2" :
        rev = rev + Qn[2]
    else : 
      if Qn[1] == "1" :
        rev = rev + Qn[2]
      if Qn[1] == "2" :
        fwd = Qn[2] + fwd

ans = fwd + S + rev
if now == -1 :
  ans = ans[::-1]
print(ans)

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

t = 0
l = ''
r = ''
for i in range(q):
    query = input()
    if query == '1':
        if t == 1:
            t = 0
        else:
            t = 1
    else:
        if query[2] == '1':
            if t == 0:
                l = query[4:]+l
            else:
                r = r+query[4]
        else:
            if t == 0:
                r = r+query[4]
            else:
                l = query[4] + l
s = l+s+r
if t == 1:
    print(s[::-1])
else:
    print(s)