Problem:
 Score : 400 points    Problem Statement Takahashi has a string S consisting of lowercase English letters.  Starting with this string, he will produce a new one in the procedure given as follows.  The procedure consists of Q operations. In Operation i  (1 \leq i \leq Q) , an integer T_i is provided, which means the following:    If T_i = 1 : reverse the string S .    If T_i = 2 : An integer F_i and a lowercase English letter C_i are additionally provided.   If F_i = 1 : Add C_i to the beginning of the string S .  If F_i = 2 : Add C_i to the end of the string S .     Help Takahashi by finding the final string that results from the procedure.      Constraints  1 \leq |S| \leq 10^5  S consists of lowercase English letters.  1 \leq Q \leq 2 \times 10^5  T_i = 1 or 2 .  F_i = 1 or 2 , if provided.  C_i is a lowercase English letter, if provided.         Input Input is given from Standard Input in the following format:  S  Q  Query_1  :  Query_Q   In the 3 -rd through the (Q+2) -th lines, Query_i is one of the following:  1   which means T_i = 1 , and:  2  F_i  C_i   which means T_i = 2 .      Output Print the resulting string.        Sample Input 1 a
4
2 1 p
1
2 2 c
1      Sample Output 1 cpa  There will be Q = 4 operations. Initially, S is a .    Operation 1 : Add p at the beginning of S . S becomes pa .    Operation 2 : Reverse S . S becomes ap .    Operation 3 : Add c at the end of S . S becomes apc .    Operation 4 : Reverse S . S becomes cpa .    Thus, the resulting string is cpa .       Sample Input 2 a
6
2 2 a
2 1 b
1
2 2 c
1
1      Sample Output 2 aabc  There will be Q = 6 operations. Initially, S is a .    Operation 1 : S becomes aa .    Operation 2 : S becomes baa .    Operation 3 : S becomes aab .    Operation 4 : S becomes aabc .    Operation 5 : S becomes cbaa .    Operation 6 : S becomes aabc .    Thus, the resulting string is aabc .       Sample Input 3 y
1
2 1 x      Sample Output 3 xy

Solution (Python):
s=list(input())
q=int(input())
c=0
left=[]
right=[]
for i in range(q):
    s1=input()
    if s1[0]=='1':
        c+=1
    else:
        if s1[2] == '1':
            if c % 2 == 0:
                left.append(s1[4])
            else:
                right.append(s1[4])
        else:
            if c % 2 == 0:
                right.append(s1[4])
            else:
                left.append(s1[4])
left=left[::-1]
left.extend(s)
left.extend(right)
if c%2==0:
    print(''.join(left))
else:
    left=left[::-1]
    print(''.join(left))

Solution (Python):
s=list(input())
q=int(input())
c=0
left=[]
right=[]
for i in range(q):
    s1=input()
    if s1[0]=='1':
        c+=1
    else:
        if s1[2] == '1':
            if c % 2 == 0:
                left.append(s1[4])
            else:
                right.append(s1[4])
        else:
            if c % 2 == 0:
                right.append(s1[4])
            else:
                left.append(s1[4])
left=left[::-1]
left.extend(s)
left.extend(right)
if c%2==0:
    print(''.join(left))
else:
    left=left[::-1]
    print(''.join(left))

Solution (Python):
from _collections import deque

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

for i in range(q):
    query = list(input().split())
    if query[0] == '1':
        reverse_flg = not reverse_flg
    else:
        if query[1] == '1':
            if reverse_flg:
                s.append(query[2])
            else:
                s.appendleft(query[2])
        else:
            if reverse_flg:
                s.appendleft(query[2])
            else:
                s.append(query[2])
if reverse_flg:
    s.reverse()
print("".join(s))

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

front = ''
back = ''
rv = False
for i in range(q):
    line = input()
    if line[0] == '1':
        rv = not rv
    else:
        ti, fi, ci = line.split()
        if (fi == '1' and rv) or (fi == '2' and not rv):
            back += ci
        else:
            front += ci

if rv:
    s = back[::-1] + s[::-1] + front
else:
    s = front[::-1] + s + back
print(s)

Solution (Python):
import sys
s = sys.stdin.readline()
s = s[:-1]
q = int(input())

front = ''
back = ''
rv = False
for i in range(q):
    line = input()
    if line[0] == '1':
        rv = not rv
    else:
        ti, fi, ci = line.split()
        if (fi == '1' and rv) or (fi == '2' and not rv):
            back += ci
        else:
            front += ci

if rv:
    s = back[::-1] + s[::-1] + front
else:
    s = front[::-1] + s + back
print(s)

Solution (Python):
s = input()
q = int(input())
r = False
top = ""
end = ""

for i in range(q):
    t = input().split()
    if t[0] == "1":
        r = not r
    else:
        if t[1] == "1":
            if r == False:
                top += t[2]
            else:
                end += t[2]
        else:
            if r == True:
                top += t[2]
            else:
                end += t[2]
if r == False:
    print(top[::-1] + s + end)
else:
    print(end[::-1] + s[::-1] + top)

Solution (Python):
import numpy as np
import scipy.sparse as sps
import scipy.misc as spm
import collections as col
import functools as func
import itertools as ite
import fractions as frac
import math as ma
import cmath as cma
import copy as cp
import sys
def sinput(): return sys.stdin.readline().strip()
def iinput(): return int(sinput())
def imap(): return map(int, sinput().split())
def fmap(): return map(float, sinput().split())
def iarr(n=0):
    if n: return [0 for _ in range(n)]
    else: return list(imap())
def farr(): return list(fmap())
def sarr(n=0):
    if n: return ["" for _ in range(n)]
    else: return sinput().split()
def barr(n): return [False for _ in range(n)]
sys.setrecursionlimit(10**7)
MOD = 10**9 + 7; EPS = sys.float_info.epsilon
PI = np.pi; EXP = np.e; INF = np.inf

s = col.deque(sinput().split())
q = iinput()
cnt = 0
for i in range(q):
    try: t,f,c = sarr()
    except: cnt += 1; cnt %= 2; continue
    if cnt: 
        if f == "1": s.append(c)
        if f == "2": s.appendleft(c)
    else: 
        if f == "1": s.appendleft(c)
        if f == "2": s.append(c)
    

s = "".join(s)
s = s[::-1] if cnt else s
print(s)

Solution (Python):
s = input()
q = int(input())
r = False
top = []
end = []


for i in range(q):
    t = list(input().split())
    if t[0] == "1":
        r = not r
    else:
        if t[1] == "1":
            if r == False:
                top.append(t[2])
            else:
                end.append(t[2])
        else:
            if r == True:
                top.append(t[2])
            else:
                end.append(t[2])

if r == False:
    print("".join(top)[::-1] + s + "".join(end))
else:
    print("".join(end)[::-1] + s[::-1] + "".join(top))

Solution (Python):
from collections import deque
S=list(input())
Q=int(input())
ls=[list(input().split()) for _ in range(Q)]
D=deque()
for i in range(len(S)):
  D.append(S[i])  
a=1
for i in range(Q):
  if ls[i][0]=="1":
    a*=(-1)
  else:
    if (ls[i][1]=="1" and a==1) or (ls[i][1]=="2" and a==-1):
      D.appendleft(ls[i][2])
    else:
      D.append(ls[i][2])
if a==1:
  while D:
    print(D.popleft(), end="")
else:    
  while D:
    print(D.pop(), end="")

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

d = 1
S1 = []
S2 = []
for i in range(n):
    Q = list(input().split())
    if Q[0] == '1':
        d *= -1
    else:
        f = Q[1]
        c = Q[2]
        if f == '1':
            if d == 1:
                S1.append(c)
            else:
                S2.append(c)
        else:
            if d == 1:
                S2.append(c)
            else:
                S1.append(c)
if d == 1:
    ans = ''.join(S1[::-1]) + S + ''.join(S2)
else:
    ans = ''.join(S2[::-1]) + S[::-1] + ''.join(S1)

print(ans)

Solution (Python):


s=input()
q=int(input())
trans=0
head=''

for i in range(q):
  query = input().split()
  if query[0]=='1':
    trans += 1
  elif (int(query[1]) & 1) ^ (trans & 1):
    head += query[2]
  else:
    s += query[2]
res = ''

if trans % 2 == 0:
  res = ''.join([head[len(head)-i-1] for i in range(len(head))]) + s
else:
  res = ''.join([s[len(s)-i-1] for i in range(len(s))]) + head
print(res)

Solution (Python):
from collections import deque

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

ANS = deque(S)

rev = 0

for _ in range(Q):
    query = input().split(' ')
    if len(query) == 1:
        rev = rev ^ 1
    else:
        f, c = int(query[1]), query[2]
        if (f - 1) ^ rev:
            ANS.append(c)
        else:
            ANS.appendleft(c)

if rev:
    ANS.reverse()

print(''.join(ANS))

Solution (Python):
s = input()
q = int(input())
querys = [list(input().split()) for i in range(q)]

rev = 0
moji = []
for query in querys:
    if query[0] == '1':
        rev += 1
    else:
        query[1] = int(query[1])
        p = True if query[1] == 1 else False
        moji.append([query[2], rev, p])

if rev%2 != 0:
    s = s[::-1]
head = ""
end = ""
for m in moji:
    if (rev - m[1]) % 2 != 0:
        m[2] = not(m[2])
    
    if m[2]:
        head = m[0] + head
    else:
        end = end + m[0]

s = head + s + end
print(s)

Solution (Python):
s = input()
q = int(input())
s_s = []
s_e = []
sw = 0
for i in range(q):
    Q = list(map(str,input().split()))
    if int(Q[0]) == 1:
        sw += 1
    else:
        if (int(Q[1]) + sw)%2 == 0:
            s_s.append(Q[2])
        else:
            s_e.append(Q[2])
if sw%2 != 0:
    ans = s_s[::-1] + list(s[::-1]) + s_e
    print("".join(ans))
else:
    ans = s_e[::-1] + list(s) + s_s
    print("".join(ans))

Solution (Python):
import sys
import itertools

import time
import math

sys.setrecursionlimit(10 ** 7)

from collections import defaultdict

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

s = readline().decode().strip()
q = int(readline())
is_reversed = False
qs = read().decode().split("\n")
left = ""
right = ""
for i in range(q):
    query = qs[i].split()
    t = int(query[0])
    if t == 1:
        is_reversed = not is_reversed
    elif t == 2:
        f = int(query[1])
        c = query[2]
        if f == 1:
            if is_reversed:
                right += c
            else:
                left += c
        elif f == 2:
            if is_reversed:
                left += c
            else:
                right += c
s = left[::-1] + s + right
if is_reversed:
    print(s[::-1])
else:
    print(s)

Solution (Python):
import sys
import itertools

import time
import math

sys.setrecursionlimit(10 ** 7)

from collections import defaultdict

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

s = readline().decode().strip()
q = int(readline())
is_reversed = False
qs = read().decode().split("\n")
left = []
right = [] 
for i in range(q):
    query = qs[i].split()
    t = int(query[0])
    if t == 1:
        is_reversed = not is_reversed
    elif t == 2:
        f = int(query[1])
        c = query[2]
        if f == 1:
            if is_reversed:
                right.append(c)
            else:
                left.append(c)
        elif f == 2:
            if is_reversed:
                left.append(c)
            else:
                right.append(c)

s = "".join(reversed(left)) + s + "".join(right)
if is_reversed:
    print(s[::-1])
else:
    print(s)

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

q=int(input())
x=0
appends=[[] for i in range(2)]
for i in range(q):
    s=list(input().split())
    if int(s[0])==1:
        x+=1
    else:
        appends[(x+int(s[1])+1)%2].append(s[2])
        
if  x%2==1:
    S=S[::-1]
S=appends[x%2][::-1]+S+appends[(x+1)%2]

for k in S:
    print(k,end='')

Solution (Python):
from collections import deque

S=input()
N=int(input())
inilen=len(S)
d=deque()
for i in range(inilen):
    d.append(S[i])
revflag=False
for i in range(N):
    L=list(map(str,input().split()))
    if L[0]=="1":
        if revflag==False:
            revflag=True
        else:
            revflag=False
    elif L[1]=="1":
        if revflag==False:
            d.appendleft(L[2])
        else:
            d.append(L[2])
        
    elif L[1]=="2":
        if revflag==False:
            d.append(L[2])
        else:
            d.appendleft(L[2])

if revflag==True:
    d.reverse()

Z=list(d)
Z=[str(a) for a in Z]
Z="".join(Z)
print(Z)

Solution (Python):
ans = input()
q = int(input())
top = 0
first = str()
last = str()

for i in range(q):
    li =input().split()
    if li[0] =="1":
        top = (top+1)%2
    else:
        if li[1] == "1":
            if top ==0:
                first += li[2]
            else:
                last += li[2]
        else:
            if top ==1:
                first += li[2]
            else:
                last += li[2]

if top == 0:
    ans = first[::-1] + ans + last
else:
    ans = last[::-1] + ans[::-1] + first

print(ans)

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

top =0
first = str()
last = str()

for i in range(times):
    Q =input().split()
    if Q[0] =="1":
        top = (top+1)%2
    else:
        if Q[1] == "1":
            
            if top ==0:
                first +=Q[2]
            else:
                last += Q[2]
        else:
            if top ==1:
                first +=Q[2]
            else:
                last += Q[2]
if top == 0:
    S = first[::-1] + S + last
else:
    S = last[::-1] + S[::-1] + first


print(S)

Solution (Python):
S = str(input())
Q = int(input())
List = [list(map(str, input().split())) for i in range(Q)]






han=0
b=''
for a in List:
    if a[0]=='1':
        han+=1
    if a[0]=='2':
        if (a[1]=='1' and han%2==0) or (a[1]=='2' and han%2==1):
            b+=a[2]
        if (a[1]=='2' and han%2==0) or (a[1]=='1' and han%2==1):
            S=S+a[2]


if han%2==0:
    print(b[::-1]+S)
else:
    print(S[::-1]+b)

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

for i in range(q):
  a = list(input().split())
  if a[0] == '1':
    count += 1
    continue
    
  if count%2 == 0:
    if a[0] == '2':
      if a[1] == '1':
        s.appendleft(a[2])
      elif a[1] == '2':
        s.append(a[2])
        
  if count%2 == 1:
    if a[0] == '2':
      if a[1] == '1':
        s.append(a[2])
      elif a[1] == '2':
        s.appendleft(a[2])
      
ans = ''.join(s)
print(ans if count%2 == 0 else ''.join(list(reversed(ans))))

Solution (Python):

s=input()
q=int(input())
p=0
a=""
b=""
for i in range(q):
    l=list(map(str,input().split()))
    if l[0]=="1":
        p=(p+1)%2
    else:
        if p==0:
            if l[1]=="1":
                a+=l[2]
            else:
                b+=l[2]
        else:
            if l[1]=="1":
                b+=l[2]
            else:
                a+=l[2]
if p==1:
    print(b[::-1]+s[::-1]+a)
else:
    print(a[::-1]+s+b)

Solution (Python):




def solve(S, Q, Qs):
    rev_flg = False
    bef = ''
    aft = ''
    for q in Qs:
        if q[0] == 1:
            rev_flg = not rev_flg
            bef, aft = aft, bef
        else:
            bf = q[1]
            if bf == 1:
                bef += q[2]
            elif bf == 2:
                aft += q[2]
    bef = bef[::-1]
    if rev_flg:
        S = S[::-1]
    print(bef + S + aft)


    
    
    
    
    
    
    
    
    
    
    
    


if __name__ == '__main__':
    
    
    
    
    
    
    
    
    
    

    
    S = input()
    Q = int(input())
    Qs = []
    i = 0
    for _ in range(Q):
        s = input()
        i += 1
        if s[0] == '1':
            q = [1]
        else:
            q = s.split()
            q[0] = int(q[0])
            q[1] = int(q[1])

        Qs.append(q)
    solve(S, Q, Qs)

Solution (Python):
from collections import deque
s = input()
q = int(input())
f = ""
b = ""
forward = True
for _ in range(q):
    que = list(input().split())
    if int(que[0]) == 1:
        if forward:
            forward = False
        else:
            forward = True
    else:
        if (int(que[1]) == 1 and forward) or (int(que[1]) == 2 and not forward):
            f+=que[2]
        else:
            b+=que[2]

if forward:
    print(f[::-1]+s+b)
else:

    print(b[::-1]+s[::-1]+f)

Solution (Python):
from collections import deque

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

for i in range(q):
    try:
        a,f,c = map(str, input().split())
    except ValueError:
        re = not re
        continue

    if f == "1":
        if not re:
            s.appendleft(c)
        else:
            s.append(c)
    else:
        if not re:
            s.append(c)
        else:
            s.appendleft(c)

if re:
    s = list(reversed(s))

print("".join(s))

Solution (Python):
import bisect

def main():
    s = input()
    qc = int(input())
    front = ""
    back = ""
    direct = 1
    for _ in range(qc):
        t = input()
        if t[0] == "1":
            direct = 2 if direct == 1 else 1
        else:
            _,f,c = t.split()
            if f == "1":
                if direct == 1:
                    front += c
                else:
                    back += c
            else:
                if direct == 1:
                    back += c
                else:
                    front += c
    if direct % 2 == 0:
        print((s+back)[::-1] + front)
    else:
        print(front[::-1]+s+back)
    
if __name__ == "__main__":
    main()

Solution (Python):
from collections import deque
S=input()
N=int(input())
q=deque([S[i] for i in range(len(S))])

c=0
for i in range(N):
  A=list(map(str,input().split()))
  if A[0]=='1':
     c+=1
     c%=2
  else:
     if (c+int(A[1]))%2==1:
       q.appendleft(A[2])
     if (c+int(A[1]))%2==0:
       q.append(A[2])
s=''
if c==0:
  for i in range(len(q)):
    r=q.popleft()
    s+=r
if c==1:
  for i in range(len(q)):
    d=q.pop()
    s+=d
print(s)

Solution (Python):
from collections import deque
def main():
    S = input()
    Q = int(input())
    ans = deque(S)
    switch = "F"
    count = 0

    for q in range(Q):
        T, *arg = input().split()
        T = int(T)

        if T == 1:
            count += 1
            if switch == "F":
                switch = "R"
            elif switch == "R":
                switch = "F"
        else:
            if switch == "F":
                if int(arg[0]) == 1:
                    ans.appendleft(arg[1])
                else:
                    ans.append(arg[1])
            else:
                if int(arg[0]) == 1:
                    ans.append(arg[1])
                else:
                    ans.appendleft(arg[1])

    if count % 2 == 0:
        print("".join(ans))
    else:
        ans.reverse()
        print("".join(ans))
        
if __name__ == "__main__":
    main()

Solution (Python):
from collections import deque
def main():
    S = input()
    Q = int(input())
    ans = deque(S)
    switch = 1
    count = 0

    for q in range(Q):
        T, *arg = input().split()
        T = int(T)

        if T == 1:
            count += 1
            if switch == 1:
                switch = 2
            elif switch == 2:
                switch = 1
        else:
            if switch == 1:
                if int(arg[0]) == 1:
                    ans.appendleft(arg[1])
                else:
                    ans.append(arg[1])
            else:
                if int(arg[0]) == 1:
                    ans.append(arg[1])
                else:
       
                    ans.appendleft(arg[1])

    if count % 2 == 0:
        print("".join(ans))
    else:
        ans.reverse()
        print("".join(ans))
if __name__ == "__main__":
    main()

Solution (Python):
from collections import deque
S = deque(list(input()))
Q = int(input())
Flag = True
for _ in range(Q):
    q = input().split()
    if len(q) == 1:
        Flag = False if Flag else True
    else:
        if Flag:
            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:
    print(''.join(S))
else:
    print(''.join(reversed(S)))

Solution (Python):
from collections import deque
s = input()
s = deque(s)
q = int(input())
reverse = False
for i in range(q):
    query = input()
    if len(query) == 1:
        reverse = not reverse
    else:
        if not reverse:
            if query[2] == '1':
                s.appendleft(query[4])
            else:
                s.append(query[4])
        else:
            if query[2] == '2':
                s.appendleft(query[4])
            else:
                s.append(query[4])
s ="".join(list(s))
if reverse:
    s = s[::-1]
print(s)

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

Solution (Python):
from collections import deque
Q=deque()
a=input()
Q.append(a)
q=int(input())
reverse=False
for i in range(q):
    d=input()
    if d[0]=="1": reverse= not reverse
    elif d[0]=="2":
        _, f, c=d.split()
        if f == "1":
            if reverse:
                Q.append(c)
            else:
                Q.appendleft(c)
        elif f =="2":
            if reverse:
                Q.appendleft(c)
            else:
                Q.append(c)
ans="".join(Q)
if reverse:
    ans=ans[::-1]
print(ans)

Solution (Python):
from collections import deque
s=input()
Q=int(input())
hanten = 0
mae = deque()
ushiro = deque()
for i in range(Q):
	q=input()
	if q=="1":
		hanten ^= 1
	else:
		if q[2] == "1":
			if hanten == 0:
				mae.appendleft(q[4])
			else:
				ushiro.append(q[4])
		else:
			if hanten == 0:
				ushiro.append(q[4])
			else:
				mae.appendleft(q[4])
if hanten == 0:
	print("".join(mae) + s + "".join(ushiro))
else:
	print(("".join(mae) + s + "".join(ushiro))[::-1])

Solution (Python):
from collections import deque
import sys


def input(): return sys.stdin.readline().strip()
def I(): return int(input())
def LI(): return list(map(int, input().split()))
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def S(): return input()
def LS(): return input().split()


s = S()
q = I()
queries = SR(q)
que = deque(list(s))
top = True  
for query in queries:
    if query[0] == '1':
        top = not(top)
    else:
        _, f, c = query.split()
        if f == '1':
            if top:
                que.appendleft(c)
            else:
                que.append(c)
        else:
            if top:
                que.append(c)
            else:
                que.appendleft(c)
if top:
    print(''.join(list(que)))
else:
    print(''.join(list(que))[::-1])

Solution (Python):
from collections import deque
que=deque(input())
rev=False
for _ in range(int(input())):
    t=input()
    if t=='1':
        rev=not rev
    else:
        tt,f,c=t.split()
        if (f=='1' and rev==False)  or (f=='2' and rev):
            que.appendleft(c)
        else:
            que.append(c)
if rev:
    que.reverse()

print(''.join(que))

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

lst = []
for i in range(Q):
    lst.append(list(input().split()))


flag = 'left' 
for i in range(Q):
    if flag == 'left':
        if lst[i][0] == '1':
            flag = 'right'
        else:
            if lst[i][1] == '1':
                S.appendleft(lst[i][2])
            else:
                S.append(lst[i][2])
    else: 
        if lst[i][0] == '1':
            flag = 'left'
        else:
            if lst[i][1] == '1':
                S.append(lst[i][2])
            else:
                S.appendleft(lst[i][2])
if flag == 'left':
    print(''.join(S))
else:
    S.reverse()
    print(''.join(S))

Solution (Python):
s=input()
q=int(input())
check=0
query=["","",""]
tfel=""
right=""
for i in range(q):
    query=list(map(str,input().split()))
    if (query[0]=="1"):
        check+=1
    else:
        if (query[1]=="1"):
            if (check%2==0):
                tfel+=query[2]
            else:
                right+=query[2]
        else:
            if (check%2==0):
                right+=query[2]
            else:
                tfel+=query[2]
            
s=tfel[::-1]+s+right
print(s if check%2==0 else s[::-1])

Solution (Python):
S = input()

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

result = "".join(S)
if reverse % 2 != 0:
    result = result[::-1]

print(result)

Solution (Python):
s = input()
q = int(input())
t = ""
for i in range(q):
    que = input().split()
    if que[0] == "1":
        s, t = t, s
    else:
        if que[1] == "1":
            t += que[2]
        else:
            s += que[2]
print(t[::-1] + s)

Solution (Python):
from collections import deque
S = deque(list(input()))
Q = int(input())
Flag = True
for _ in range(Q):
    q = input().split()
    if len(q) == 1:
        Flag = False if Flag else True
    else:
        if Flag:
            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:
    print(''.join(S))
else:
    print(''.join(reversed(S)))

Solution (Python):
S=input()
Q=int(input())
added=(list(),list())
head=0
for i in range(Q):
    l=input().split()
    if int(l[0])==1:
        head=(head+1) % 2
    else:
        adding=(int(l[1])+head-1)%2
        added[adding].append(l[2])



print(''.join(reversed(added[head])),end="")
if head==1:
    S=S[::-1]
print(S,end="")
print(''.join(added[(head+1)%2]))

Solution (Python):
s = input()
q = int(input())
a = []




pre = ""
nex = ""
c = 1
TFC = [input().split() for _ in range(q)]
for i in TFC:


    if i ==["1"]:
        c *= -1
        
    elif i[0]=="2" and i[1]=="1":
        
        if c == 1:
            pre=pre+i[2]
        else:
            nex = nex + i[2]
    elif i[0]=="2" and i[1]=="2":
        
        if c ==1:
            nex = nex +i[2]
        else:
            pre = pre+i[2]
if c==-1:
    print(nex[::-1]+s[::-1]+pre)
else:
    print(pre[::-1]+s+nex)

Solution (Python):
s = input()
q = int(input())
a = []




pre = ""
nex = ""
c = 1
TFC = [input().split() for _ in range(q)]
for i in TFC:


    if i ==["1"]:
        c *= -1
        
    elif i[0]=="2" and i[1]=="1":
        
        if c == 1:
            pre=i[2]+pre
        else:
            nex = nex + i[2]
    elif i[0]=="2" and i[1]=="2":
        
        if c ==1:
            nex = nex +i[2]
        else:
            pre = i[2]+pre
s = pre+s+nex
if c ==-1:
    s = s[::-1]
print(s)

Solution (Python):
s = input()
q = int(input())
a = []




pre = ""
nex = ""
c = 1
for j in range(q):
    i = list(map(str,input().split()))
    if i ==["1"]:
        c *= -1
        
    elif i[0]=="2" and i[1]=="1":
        
        if c == 1:
            pre=i[2]+pre
        else:
            nex = nex + i[2]
    elif i[0]=="2" and i[1]=="2":
        
        if c ==1:
            nex = nex +i[2]
        else:
            pre = i[2]+pre
s = pre+s+nex
if c ==-1:
    s = s[::-1]
print(s)

Solution (Python):
S = input()
Q = int(input())
x = 0
p = ''
q = ''
for i in range(Q):
    A = list(input().split())
    if A[0] == '1':
        x += 1
    elif x%2 ==0:
        if A[1] == '1':
            p = A[2] + p
        else:
            q = q + A[2]
    else:
        if A[1] == '1':
            q = q + A[2] 
        else:
            p = A[2] + p
S = p + S + q
if x%2 == 1:
    S = ''.join(reversed(list(S)))
print(S)

Solution (Python):
S = input()
Q = int(input())
TFC = [input().split() for _ in range(Q)]
is_reversed = False
head = ''
tail = ''
for tfc in TFC:
    if tfc[0] == '1':
        is_reversed = not is_reversed
    else:
        if tfc[1] == '1':
            if is_reversed:
                tail += tfc[2]
            else:
                head += tfc[2]
        else:
            if is_reversed:
                head += tfc[2]
            else:
                tail += tfc[2]
if is_reversed:
    print(tail[::-1]+S[::-1]+head)
else:
    print(head[::-1]+S+tail)

Solution (Python):
from collections import deque

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

for i in range(Q):
    if Query[i][0] == '1':
        order *= -1
    else:
        F = Query[i][1]
        C = Query[i][2]

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

S = ''.join(list(S))

print(S[::order])

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

Solution (Python):
from collections import deque



s = deque(list(input()))
q = int(input())
rev = -1

for i in range(q):
    a = list(input().split())
    if a[0] == '1': 
        rev *= -1 
    
    else: 
        if a[1] == '1': 
            if rev == -1:
                s.appendleft(a[2])
            else:
                s.append(a[2])
                
        else: 
            if rev == -1:
                s.append(a[2])
            else:
                s.appendleft(a[2])
                
if rev == -1:
    print(''.join(s))
else:
    s = list(s)
    print(''.join(s[::-1]))

Solution (Python):
from collections import deque

S = deque(input())
num_of_query = int(input())

is_reverse = False

for _ in range(num_of_query):
    query = list(input().split())
    
    if query[0] == '1':
        is_reverse = not is_reverse
    else:
        if query[1] == '1':
            if is_reverse:
                S.append(query[2])
            else:
                S.appendleft(query[2])

        else:
            if is_reverse:
                S.appendleft(query[2])
            else:
                S.append(query[2])

S = list(S)

if is_reverse:
    S = S[::-1]

print(''.join(S))

Solution (Python):
from collections import deque

S = deque(input())
num_of_query = int(input())
querys = []
is_reverse = False

for _ in range(num_of_query):
    
    query = list(input().split())
    
    if query[0] == '1':
        is_reverse = not is_reverse
    else:
        if query[1] == '1':
            if is_reverse:
                S.append(query[2])
            else:
                S.appendleft(query[2])

        else:
            if is_reverse:
                S.appendleft(query[2])
            else:
                S.append(query[2])

S = list(S)
if is_reverse:
    S = S[::-1]














print(''.join(S))