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 heapq import heappush,heappop,heapify
from collections import deque,defaultdict,Counter
import itertools
from functools import *
from itertools import permutations,combinations,groupby
import sys
import bisect
import string
import math
import time
import random
def Golf():
    *a,=map(int,open(0))
def S_():
    return input()
def IS():
    return input().split()
def LS():
    return [i for i in input().split()]
def I():
    return int(input())
def MI():
    return map(int,input().split())
def LI():
    return [int(i) for i in input().split()]
def LI_():
    return [int(i)-1 for i in input().split()]
def NI(n):
    return [int(input()) for i in range(n)]
def NI_(n):
    return [int(input())-1 for i in range(n)]
def StoI():
    return [ord(i)-97 for i in input()]
def ItoS(nn):
    return chr(nn+97)
def LtoS(ls):
    return ''.join([chr(i+97) for i in ls])
def GI(V,E,Directed=False,index=0):
    org_inp=[]
    g=[[] for i in range(n)]
    for i in range(E):
        inp=LI()
        org_inp.append(inp)
        if index==0:
            inp[0]-=1
            inp[1]-=1
        if len(inp)==2:
            a,b=inp
            g[a].append(b)
            if not Directed:
                g[b].append(a)
        elif len(inp)==3:
            a,b,c=inp
            aa=(inp[0],inp[2])
            bb=(inp[1],inp[2])
            g[a].append(bb)
            if not Directed:
                g[b].append(aa)
    return g,org_inp
def GGI(h,w,search=None,replacement_of_found='.',mp_def={'

    mp=[1]*(w+2)
    found={}
    for i in range(h):
        s=input()
        for char in search:
            if char in s:
                found[char]=((i+1)*(w+2)+s.index(char)+1)
                mp_def[char]=mp_def[replacement_of_found]
        mp+=[1]+[mp_def[j] for j in s]+[1]
    mp+=[1]*(w+2)
    return h+2,w+2,mp,found
def bit_combination(k,n=2):
    rt=[]
    for tb in range(n**k):
        s=[tb//(n**bt)%n for bt in range(k)]
        rt+=[s]
    return rt
def show(*inp,end='\n'):
    if show_flg:
        print(*inp,end=end)
YN=['YES','NO']
Yn=['Yes','No']
 
mo=10**9+7
inf=float('inf')
l_alp=string.ascii_lowercase
u_alp=string.ascii_uppercase


input=lambda: sys.stdin.readline().rstrip()
 
def ran_input():
    import random
    n=random.randint(4,16)
    rmin,rmax=1,10
    a=[random.randint(rmin,rmax) for _ in range(n)]
    return n,a

def ran_input():
    import random
    n=random.randint(4,16)
    rmin,rmax=1,10
    a=[random.randint(rmin,rmax) for _ in range(n)]
    return n,a

show_flg=False
show_flg=True

ans=0

p=deque()

for i in StoI():
    p.append(i)
q=I()
m=0
for i in range(q):
    si=input()
    if len(si)==1:
        m=1-m
    else:
        t,f,c=si.split()
        c=ord(c)-97
        f=int(f)
        if f==1:
            M=1-m
        else:
            M=m

        if M==0:
            p.append(c)
        else:
            p.appendleft(c)
show(LtoS(p)[::1-2*m])

Solution (Python):
from collections import deque

d = deque(list(input()))
flag = 1
for _ in range(int(input())):
    a = input()
    if a[0] == '1':
        flag *= -1
    elif a[2] == '1' and flag == 1:
        d.appendleft(a[4])
    elif a[2] == '1' and flag == -1:
        d.append(a[4])
    elif a[2] == '2' and flag == 1:
        d.append(a[4])
    else:
        d.appendleft(a[4])
if flag == -1:
    d.reverse()
print(*d, sep='')

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

rev = 0
front = ""
back = ""
all_rev = 0

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

    
    if len(t) == 1:
        rev += 1
        all_rev += 1

    
    else:
        if rev%2 == 1:
            if t[1] == '1':
                back = back + t[2]
            else:
                front = t[2] + front
        else:
            if t[1] == '1':
                front = t[2] + front
            else:
                back = back + t[2]


if all_rev%2 == 1:
    s = s[::-1]
    front,back = back[::-1],front[::-1]

print(front + s + back)

Solution (Python):



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

mae = []
usiro = []
count = 0
for i in range(Q):
    query = input().rstrip().split()
    if len(query) == 3:
        if (count + int(query[1])) % 2 == 0:
            mae.append(query[2])
        else:
            usiro.append(query[2])
    else:
        count += 1
        
if count % 2 == 1:
    output = ''.join(mae[::-1]) + S[::-1] + ''.join(usiro)
else:
    output = ''.join(usiro[::-1]) + S + ''.join(mae)
print(output)

Solution (Python):
S = input()
Q = int(input())
T1 = 0
S_left = ''
S_right = ''
for _ in range(Q):
  a = list(map(str,input().split()))
  if a[0] == '1':
    T1 += 1
  elif a[0] == '2':
    if ((a[1]=='1')and(T1%2==0)) or ((a[1]=='2')and(T1%2!=0)):
      S_left = a[2]+S_left
    else:
      S_right = S_right+a[2]
S = S_left+S+S_right
if T1 % 2 == 0:
  print(S)
else:
  print(S[::-1])

Solution (Python):
from collections import deque

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

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

Solution (Python):
from collections import deque

S = input()
S = deque(S)
Q = int(input())
Query1 = 0
flag = False
for i in range(Q):
    Queryi = input().split()
    if Queryi[0] == '1':
        Query1 += 1
        flag = not flag
    else:
        if (Queryi[1] == '1') ^ flag:
            S.appendleft(Queryi[2])
        else:
            S.append(Queryi[2])

if Query1 % 2 != 0:
    S.reverse()

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

Solution (Python):
from collections import deque

S = input()
S = deque(S)
Q = int(input())
Query1 = 0
flag = False
for i in range(Q):
    Queryi = input().split()
    if Queryi[0] == '1':
        Query1 += 1
        flag = not flag
    else:
        if (Queryi[1] == '1') ^ flag:
            S.appendleft(Queryi[2])
        else:
            S.append(Queryi[2])

if Query1 % 2 != 0:
    S.reverse()

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

Solution (Python):
from collections import deque

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

flag = 0
for i in range(Q):
	q = list(map(str, input().split()))
	if q[0] == '1':
		flag = 1 - flag
	else:
		if (q[1] == '1' and flag == 1) or (q[1] == '2' and flag == 0):
			S.append(q[2])
		else:
			S.appendleft(q[2])
if flag == 1:
	S.reverse()
ans = ''.join(S)
print(ans)

Solution (Python):
import sys
read=sys.stdin.buffer.read
readline=sys.stdin.buffer.readline
readlines=sys.stdin.buffer.readlines
from collections import deque

S=deque(readline().rstrip().decode())
Q=int(readline())
query=readlines()

flag=False
for q in query:
    if q[0]==ord('1'):
        flag= not flag
    else:
        _,f,c=q.decode().split()
        f=int(f)
        if flag^(f==1):
            S.appendleft(c)
        else:
            S.append(c)
ans=''.join(S)
if flag:
    ans=ans[::-1]
print(ans)

Solution (Python):
from collections import deque
a = input()
c = input()
d = []
alist = list(a)
adeque = deque(a)
count = 0
for i in range(int(c)):
  e = input().split()
  d.append(e)
for i in range(int(c)):
  if d[i][0]=="1":
    count = count + 1
  else:
    if d[i][1] == "1" and count % 2 == 0:
      adeque.appendleft(d[i][2])
    elif d[i][1] == "2" and count % 2 == 1:
      adeque.appendleft(d[i][2])
    else:
      adeque.append(d[i][2])
alist = list(adeque)
if count % 2 == 1:
  l = len(alist)
  for x in range(int(l//2)):
    alist[x],alist[l-x-1] = alist[l-x-1],alist[x]
answer = "".join(alist)
print(answer)

Solution (Python):
from collections import deque

s = input()
n = int(input())
d = deque()

for ele in s:
  d.append(ele)


rev = 1

for i in range(n):
  cs = input().split()
  
  if( cs[0] == '1' ) :
    rev *= -1
  else:
    cf = cs[1]
    cfs = cs[2]
    if rev == 1 :
      if cf == '1' :
        
        d.appendleft(cfs)
      else:
        
        d.append( cfs )
    else: 
      if cf == '1' :
        
        d.append( cfs )
      else:
        
        d.appendleft(cfs)

      
if rev == 1 :
 print( ''.join(list(d)) )
else:
 res = ''.join(list(reversed( d )))
 print (res)

Solution (Python):
right = input()
flag = 1
left = ""
for i in range(int(input())):
    t = input()
    if t == "1":
        flag ^= 1
        continue
    t, f, c = t.split(" ")
    if not ((f=="1")^flag):
        left += c
    else:
        right += c
ans = left[::-1] + right
if not flag:
    ans = ans[::-1]
print(ans)

Solution (Python):
right = input()
q = int(input())
left = ""
flag = 1
for i in range(q) :
    t = input()
    if t == "1" :
        flag ^= 1
        continue
    t, f, c = t.split(" ")
    f = int(f)
    left += c*flag*(2-f)
    right += c*flag*(f-1)
    left += c*(1-flag)*(f-1)
    right += c*(1-flag)*(2-f)
if flag == 1 :
    print(left[::-1]+right)
else :
    print(right[::-1]+left)

Solution (Python):
right = input()
Q = int(input())
flag = 0b1
left = ""
for i in range(Q):
    q = input()
    if q == "1":
        flag ^= 1
        continue
    m,f,c = q.split(" ")
    f = int(f)
    left += c*(1-flag)*(f-1)
    right += c*flag*(f-1)
    left += c*flag*(2-f)
    right += c*(1-flag)*(2-f)
if flag == 1:
    print(left[::-1]+right)
else:
    print(right[::-1]+left)

Solution (Python):
s = input()
b = ""
f = ""
change_flag = 1
for i in range(int(input())):
    a = input().split(" ")
    if(a[0] == "1"):
        change_flag *= -1
    else:
        if((a[1] == "1" and change_flag == 1) or (a[1] == "2" and change_flag == -1)):
            b = a[2] + b
        else:
            f = f + a[2]

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

Solution (Python):
right = input()
Q = int(input())
flag = 1
left = ""
for i in range(Q):
  q = input()
  if q == "1":
    flag = 1-flag
    continue
  m,f,c = q.split(" ")
  f = int(f)
  left += c*(1-flag)*(f-1)
  right += c*flag*(f-1)
  left += c*flag*(2-f)
  right += c*(1-flag)*(2-f)
if flag == 1:
  print(left[::-1]+right)
else:
  print(right[::-1]+left)

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

Solution (Python):
import collections
s = collections.deque(input())
rev = 0
for i in range(int(input())):
    q = list(input().split())
    if len(q) == 1:
        rev += 1
    else:
        if q[1] == "1":
            if rev % 2 == 0:
                s.appendleft(q[2])
            else:
                s.append(q[2])
        else:
            if rev % 2 == 1:
                s.appendleft(q[2])
            else:
                s.append(q[2])
if rev % 2 == 1:
    s.reverse()
print("".join(s))

Solution (Python):
import collections

x = collections.deque(input())
n = int(input())
rev = False

for i in range(n):
    y = input().split()
    if len(y) == 1:
      rev = not rev
    elif rev:
      if int(y[1]) == 2:
          x.appendleft(y[2])
      else:
          x.append(y[2])
    else:
      if int(y[1]) == 2:
          x.append(y[2])
      else:
          x.appendleft(y[2])

if rev:
  x.reverse()

print("".join(x))

Solution (Python):
from collections import deque

S = deque(input())
Q = int(input())
rev = False
for _ in range(Q):
    s = input().split()
    if len(s) == 1:
        rev = not rev
        continue
    if not rev:
        if s[1] == '1':
            S.appendleft(s[2])
        else:
            S.append(s[2])
    else:
        if s[1] == '1':
            S.append(s[2])
        else:
            S.appendleft(s[2])
if rev:
    print(''.join(reversed(S)))
else:
    print(''.join(S))

Solution (Python):
from collections import deque
d = deque()
S = input()
d.append(S)
N = int(input())
direct = 1
for i in range(N):
  X = input().split()
  if len(X) == 1:
    direct *= -1
  else:
    if (X[1] == "1" and direct == 1) or (X[1] == "2" and direct == -1):
      d.appendleft(X[2])
    else:
      d.append(X[2])
      
S = "".join(list(d))
print(S) if direct == 1 else print(S[::-1])

Solution (Python):
from collections import deque
d = deque()
S = input()
d.append(S)
N = int(input())
direct = 0
for i in range(N):
  X = input().split()
  if len(X) == 1:
    direct = 1 - direct
  else:
    if (X[1] == "1" and direct == 0) or (X[1] == "2" and direct == 1):
      d.appendleft(X[2])
    else:
      d.append(X[2])
      
S = "".join(list(d))
print(S) if direct == 0 else print(S[::-1])

Solution (Python):
S = input()
f = ""
N = int(input())
direct = 0
for i in range(N):
  X = input().split()
  if len(X) == 1:
    direct = 1 - direct
  else:
    if (X[1] == "1" and direct == 0) or (X[1] == "2" and direct == 1):
      f += X[2]
    else:
      S += X[2]
print(f[::-1]+S) if direct == 0 else print(S[::-1]+f)

Solution (Python):
from collections import deque
s = input()
d = deque(s)
q = int(input())
flag = 0
for i in range(q):
    tfc = input().split()
    if tfc[0] == '1':
        if flag == 0:
            flag = 1
        else:
            flag = 0
    else:
        if flag == 0:
            if int(tfc[1]) == 1:
                d.appendleft(tfc[2])

            else:
                d.append(tfc[2])

        else:
            if int(tfc[1]) == 1:
                d.append(tfc[2])

            else:
                d.appendleft(tfc[2])

if flag == 0:
    print(''.join(d))
else:
    print(''.join(reversed(d)))

Solution (Python):
sf=""
sb=input()
q=int(input())

rev=1

for _ in range(0,q):
  n=input().split()
  if n[0]=="1":
    rev*=-1
  else:
    a,b=n[1],n[2]
    if (a=="1" and rev==1) or (a=="2" and rev==-1):
      sf+=b
    else:
      sb+=b

s=sf[::-1]+sb

if rev==1:
  print(s)
else:
  print(s[::-1])

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

rev=1

sf=""
sb=s

for _ in range(0,q):
  n=input().split()
  if n[0]=="1":
    rev*=-1
  else:
    if (n[1]=="1" and rev==1) or (n[1]=="2" and rev==-1):
      sf+=n[2]
    else:
      sb+=n[2]

s=sf[::-1]+sb

if rev==1:
  print(s)
else:
  print(s[::-1])

Solution (Python):
from collections import deque
S=input()
Q=int(input())
query=[list(map(str,input().split())) for _ in range(Q)]

d=deque(S)
reverse=False
for q in query:
  if len(q)==1:
    reverse=not reverse
  else:
    _,f,c=q
    if f=='1':
      d.append(c) if reverse else d.appendleft(c)
    else:
      d.appendleft(c) if reverse else d.append(c)
print(*reversed(d),sep='') if reverse else print(*d,sep='')

Solution (Python):
B = input()
order = True
F = ""
for q in range(int(input())):
    T = input().rstrip().split()
    if T[0] == "1":
        order ^= True
    else:
        f,c = T[1],T[2]
        if not((f == "1") ^ order):
            F += c
        else:
            B += c
    
F = F[::-1]+B
if not order:
    F = F[::-1]
print(F)

Solution (Python):

S = input()

Q = int(input())

def problem_gksn(S,Q):
  check_reverse = 0 
  atama = ""
  oshiri = ""
  for _ in range(Q):
    
    query = input().split()
    
    if len(query) == 1: 
      
      check_reverse += 1
      
    else: 
      
      if int(query[1]) == 2 - check_reverse % 2:
        oshiri += query[2]
      else:
        atama += query[2]
      
      
  S = atama[::-1] + S + oshiri
  if check_reverse % 2==1:
    S = S[::-1]
  
  return S

print(problem_gksn(S,Q))

Solution (Python):
S = input()

Q = int(input())

def problem_gksn(S,Q):
  check_reverse = 0 
  atama = ""
  oshiri = ""
  for _ in range(Q):
    
    query = input().split()
    
    if len(query) == 1: 
      
      check_reverse += 1
      
    else: 
      
      if int(query[1]) == 2 - check_reverse % 2:
        oshiri += query[2]
      else:
        atama += query[2]
      
      
  S = atama[::-1] + S + oshiri
  if check_reverse % 2==1:
    S = S[::-1]
  
  return S

print(problem_gksn(S,Q))

Solution (Python):
from collections import deque

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

lr = 'left'

def reverse_list(lr):
    if lr == 'right':
        return 'left'
    else:
        return 'right'

def moji_tuika(S, f, c, lr):
    if f == 1 and lr == 'left':
        S.appendleft(c)
    elif f == 1 and lr == 'right':
        S.append(c)
    elif f == 2 and lr == 'left':
        S.append(c)
    else:
        S.appendleft(c)
    return S

q = []
for i in range(Q):
    temp = input().split()
    if len(temp) == 1:
        q.append(1)
    else:
        q.append(temp)

S = deque(list(S))
for i in q:

    if type(i) == int:
        lr = reverse_list(lr)
    elif int(i[0]) == 2:
        S = moji_tuika(S,int(i[1]),i[2],lr)
if lr == 'right':
    S.reverse()

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

Solution (Python):
from collections import deque
S = str(input())
Q = int(input())
d = deque()
reverse = 1
for i in S:
    d.append(i)
for j in range(Q):
  Query = input().split()
  if len(Query) == 1:
    reverse *= -1
  else:
    F = int(Query[1])
    C = str(Query[2])
    if reverse == 1:
      if F == 1:
        d.appendleft(C)
      elif F == 2:
        d.append(C)
    elif reverse == -1:
      if F == 1:
        d.append(C)
      elif F == 2:
        d.appendleft(C)
if reverse == -1:
    d.reverse()
ans = ''.join(d)
print(ans)

Solution (Python):
S=input()
Q=int(input())
L=list()
B=True
X=''
Y=''
for i in range(Q):
  L=list(input().split())
  if L[0]=='1':
    B=not(B)
  else:
    if ((L[1]=='1' and B) or (not(L[1]=='1' or B))):
      Y=Y+L[2]
    else:
      X=X+L[2]
S=S[::-1]
S=S+Y
S=S[::-1]
S=S+X
if not(B):
  S=S[::-1]
print(S)

Solution (Python):
from collections import deque
d = deque()
for x in input():
    d.append(x)
Q = int(input())
f = 0
for _ in range(Q):
    l = list(input().split())
    if l[0] == '1':
        f ^= 1
    else:
        if f == 0: 
            if l[1] == '2': d.append(l[2])
            else: d.appendleft(l[2])
        else:
            if l[1] == '2': d.appendleft(l[2])
            else: d.append(l[2])
if f:
    d.reverse()
print("".join(d))

Solution (Python):
from collections import deque
d = deque()
length = 0
for x in input():
    d.append(x)
    length += 1
Q = int(input())
f = 0
for _ in range(Q):
    l = list(input().split())
    if l[0] == '1':
        f ^= 1
    else:
        length += 1
        if f == 0: 
            if l[1] == '2': d.append(l[2])
            else: d.appendleft(l[2])
        else:
            if l[1] == '2': d.appendleft(l[2])
            else: d.append(l[2])
ans = ''
for x in range(length):
    ans += d.pop()
if f == 0:
    print(ans[::-1])    
else:
    print(ans)

Solution (Python):
from collections import deque 
s = input()
q = int(input())
flag = 1
d = deque(s)
for i in range(q):
    in_line = input().split()
    if in_line[0] == "1":
        flag *= -1
    else:
        if in_line[1] == "1":
            if flag == 1:
                d.appendleft(in_line[2])
            else:
                d.append(in_line[2])
        else:
            if flag == 1:
                d.append(in_line[2])
            else:
                d.appendleft(in_line[2])
if flag == -1:
    d.reverse()
print("".join(d))

Solution (Python):
from collections import deque
s = str(input())
q = int(input())
s = deque(s)
query = []
w = True
for i in range(q):
    query = list(map(str,input().split()))
    if(query[0] == "1"):
        w = not(w)
    else:
        mem = query[2]
        if(query[1] == "1"):
            if(w):
                s.appendleft(mem)
            else:
                s.append(mem)
        else:
            if(w):
                s.append(mem)
            else:
                s.appendleft(mem)
if(w):
    print("".join(s))
else:
    s.reverse()
    print("".join(s))

Solution (Python):
S = input()
Q = int(input())
flg = 0
senntou = ''
ushiro = ''
for i in range(Q):
    A = list(input().split())
    if int(A[0]) == 1 and flg == 0:
        flg = 1
        continue
    elif int(A[0]) == 1 and flg == 1:
        flg = 0
        continue
    if (int(A[1]) == 1 and flg == 0) or (int(A[1]) == 2 and flg == 1):
        senntou = A[2] + senntou
    else:
        ushiro = ushiro + A[2]
if flg == 0:
    print(senntou + S + ushiro)
if flg == 1:
    print(ushiro[::-1] + S[::-1] + senntou[::-1])

Solution (Python):
from collections import deque

s = input()
d = deque()
header = 1
for i in s:
    d.append(i)
q = int(input())

for i in range(q):
    data = input().split()
    if len(data) == 1:
        header *= -1
    else:
        f = int(data[1])
        c = data[2]
        if header == 1:
            if f == 1:
                d.appendleft(c)
            else:
                d.append(c)
        else:
            if f == 1:
                d.append(c)
            else:
                d.appendleft(c)

if header == -1:
    d.reverse()

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

print(s)

Solution (Python):
s=input();q=int(input());r,a=0,[[],[]]
for _ in range(q):
  t=input()
  if t=='1':r=-(-~r%2)
  else:a[~-r+int(t[2])].append(t[4])
print(''.join(a[~-r+1][::-1])+s[::r or 1]+''.join(a[~-r+2]))

Solution (Python):
word = input()
count = int(input())
reversed_flg = False
head = ''
tail = ''

for i in range(count):
    line = input()
    if len(line) == 1:
        reversed_flg = not reversed_flg
    else:
        str_t, str_f, char = line.split()
        f = int(str_f)

        if (f == 1 and not reversed_flg) or (f == 2 and reversed_flg):
            
            head = char + head
        else:
            
            tail += char

word = head + word + tail

if reversed_flg:
        word = word[::-1]

print(word)

Solution (Python):
import sys








def main():
	s = input()
	n = int(input())
	f = 0
	l = ""
	r = ""
	for i in range(n):
		q=input().split()
		if q[0] == "1":
			f ^= 1
		else:
			if (f == 0 and q[1] == "1") or (f == 1 and q[1] == "2"):
				l = l + q[2]
			else:
				r = r + q[2]
	if f == 0:
		ans = l[::-1] + s + r
	else:
		ans = r[::-1] + s[::-1] + l
	print(ans)


if __name__ == "__main__":
	main()

Solution (Python):
from collections import deque

s=input()
q=int(input())
count=0 
ss=deque(s)
flag=True 
for i in range(q):
    info=input()
    if info[0]=="1":
        count+=1
        if flag:
            flag=False
        else:
            flag=True
    else:
        _,f,c=info.split()
        if flag:
            if f=="1":
                ss.appendleft(c)
            else:
                ss.append(c)
        else: 
            if f=="1":
                ss.append(c)
            else:
                ss.appendleft(c)
ans="".join(ss)
print(ans if count%2==0 else ans[::-1])

Solution (Python):
s=input()
n=int(input())
f=0
l=""
r=""
for i in range(n):
	q=input().split()
	if q[0]=="1":
		f^=1
	else:
		if (f==0 and q[1]=="1") or (f==1 and q[1]=="2"):
			l=l+q[2]
		else:
			r=r+q[2]

if f==0:
	ans=l[::-1]+s+r
else:
	ans=r[::-1]+s[::-1]+l

print(ans)

Solution (Python):
def main():
  w = input()
  s = collections.deque(w)
  n = int(input())
  
  order = True
  for _ in range(n):
    q = list(input_list_str())
    if len(q) == 1:
      order = False if order else True
      continue
    t, f, c = q
    if int(f) == 1:
      if order:
        s.appendleft(c)
      else:
        s.append(c)
    else:
      if order:
        s.append(c)
      else:
        s.appendleft(c)
  if order is False:
    s.reverse()
  print(''.join(s))

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

def input_list_str():
  return map(str, input().split())

def lcm_base(x, y):
    return (x * y) // fractions.gcd(x, y)

def lcm_list(numbers):
    return reduce(lcm_base, numbers, 1)

def gcd(*numbers):
    return reduce(fractions.gcd, numbers)

def gcd_list(numbers):
    return reduce(fractions.gcd, numbers)

import math
import fractions
import collections
from functools import reduce
main()

Solution (Python):

from collections import deque
Str = str(input())
Q = int(input())
S = deque(list(Str))


A = 0
B = []
C = []

for i in range(Q):
    a = list(map(str, input().split()))
    if a[0] == '1':
        if A%2==0:
            if len(B)>1:
                S.extendleft(B)
                B = []
            elif len(B) == 1:
                S.extendleft(B)
                B = []
            if len(C) > 0:
                S.extend(C)
                C = []
        else:
            if len(C)>1:
                S.extendleft(C)
                C = []
            elif len(C) == 1:
                S.extendleft(C)
                C = []
            if len(B) > 0:
                S.extend(B)
                B = []
        A += 1
    elif a[1] == '1':
        B.append(a[2])
    else:
        C.append(a[2])

if a[0] != '1':
    if A%2==0:
        if len(B)>1:
            S.extendleft(B)
            B = []
        elif len(B) == 1:
            S.extendleft(B)
            B = []
        if len(C) > 0:
            S.extend(C)
            C = []
    else:
        if len(C)>1:
            S.extendleft(C)
            C = []
        elif len(C) == 1:
            S.extendleft(C)
            C = []
        if len(B) > 0:
            S.extend(B)
            B = []

if A%2 != 0:
    S.reverse()
print(''.join(S))

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

s = input().rstrip()
q = int(input())

isRev = False

head = ''

for i in range(q):
    query = list(input().split())
    if len(query) == 1:
        isRev = not isRev
    else:
        if isRev and query[1] == '1':
            s = s + query[2]
        elif query[1] == '1':
            head = head + query[2]
        elif isRev and query[1] == '2':
            head = head + query[2]
        elif query[1] == '2':
            s = s + query[2]
            
if isRev:
    s = s[::-1] + head
else:
    s = head[::-1] + s
        
print(s)

Solution (Python):
from collections import deque

S = list(input())
S = deque(S)
Q = int(input())
reverse = 1
query = [input().split() for i in range(Q)]
for i in query:
    if len(i) == 1:
        reverse *= -1
    elif (i[1]=='1' and reverse == 1) or (i[1]=='2' and reverse==-1):
        S.appendleft(i[2])
    else:
        S.append(i[2])
if reverse == -1:
   S.reverse() 
print(''.join(list(S)))

Solution (Python):
from collections import deque
ss=deque()
s=list(input())
l=len(s)
for i in range(l):
    ss.append(s[i])
q=int(input())
info=[]
rev=[] 
num=0
for i in range(q):
    info.append(list(input().split()))
moji=0 
for i in range(q):
    if info[i][0]=="1":
        num+=1
    else:
        if info[i][1]=="2":
            rev.append([info[i][2],2,num]) 
        else:
            rev.append([info[i][2],1,num])
        moji+=1
if num%2==1:
    ss.reverse()


for i in range(moji):
    now=rev[i][1]
    if (num-rev[i][2])%2==1: 
        
        if rev[i][1]==2:
            rev[i][1]=1
        else:
            rev[i][1]=2
    if rev[i][1]==2:
        ss.append(rev[i][0])
    else:
        ss.appendleft(rev[i][0])
print("".join(ss))

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

q = int(input())
s = deque(s)
r = False
for _ in range(q):
    cmd = input()
    if cmd[0] == '1':
        r = not r
    else:
        cmd, f, c = cmd.split()
        if (f == '2' and not r) or (f=='1' and r):
            s.append(c)
        else:
            s.appendleft(c)
if r:
    s.reverse()
print(''.join(s))

Solution (Python):
from collections import deque

s = input()
d = deque()
header = 1
for i in s:
    d.append(i)
q = int(input())
for i in range(q):
        data = input().split()
        if len(data) == 1 :
            header *= -1
        else:
            f = int(data[1])
            c = data[2]
            if header == 1:
                if f == 1:
                    d.appendleft(c)
                else:
                    d.append(c)
            else:
                if f == 1:
                    d.append(c)
                else:
                    d.appendleft(c)
                
if header == -1:
    d.reverse()

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



print(s)

Solution (Python):

import sys
import numpy as np
from collections import deque



s = list(input())

n = int(input())

d = deque(s)

reverse = False
for ii in range(n):
    q = input()

    if q[0] == "1":
        reverse += 1
        reverse %= 2

    else:
        t, f, c = q.split()
        if f == "1":
            if reverse:
                d.append(c)
            else:
                d.appendleft(c)
        else:
            if reverse:
                d.appendleft(c)
            else:
                d.append(c)
                
if reverse:
    d.reverse()
print("".join(d))

Solution (Python):
from collections import deque


def input_int_list():
    return [int(thing) for thing in input().split(" ")]


S=input()
Q=int(input())
Q_list=[input().split(" ") for i in range(Q)]
hantenness=1
left=[]
right=[]
ans=deque()
ans.extend(list(S))

for Query in Q_list:
    if Query[0]==str(1):
        hantenness*=-1
    else:
        C=Query[2]
        F=int(Query[1])
        if hantenness==1:
            if F==1:
                ans.appendleft(C)
            else:
                ans.append(C)
        else:
            if F==1:
                ans.append(C)
            else:
                ans.appendleft(C)

print("".join(ans) if hantenness==1 else "".join(ans)[::-1])

Solution (Python):
from collections import deque as D 
d = D()
s=input();q=int(input())
ans=0
d.extend(list(s))
for i in range(q):
  k=list(input().split())
  if len(k)==1:
    ans=(ans+1)%2
  else:
    x,y,z=k
    y=(int(y)-1+ans)%2
    d.append(z) if y else d.appendleft(z)
print("".join(list(d)[::(-1)**ans]))