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())
Q = [list(input().split()) for _ in range(q)]

ans = deque(s)
ok = True
for v in Q:
    if v[0] == "1":
        if ok: ok = False
        else: ok = True
    else:
        if v[1] == "1":
            if ok: ans.appendleft(v[2])
            else: ans.append(v[2])
        else:
            if ok: ans.append(v[2])
            else: ans.appendleft(v[2])
ans = list(ans)
if not ok: ans = ans[::-1]
print("".join(ans))

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

from collections import deque
s = deque(s)

cnt = 0

for i in range(q):
    query = list(map(str, input().split()))
    if len(query) == 1:
        cnt += 1
    else:
        t, f, c = query
        if f == '1':
            if cnt%2 == 0:
                s.appendleft(c)
            else:
                s.append(c)
        else:
            if cnt%2 == 0:
                s.append(c)
            else:
                s.appendleft(c)
s = list(s)
if cnt%2 == 1:
    s.reverse()
print(''.join(s))

Solution (Python):
S=input()

def split(word): 
    return [char for char in word]  
word = S
splits=split(S)
Q=int(input())
flipcounter=0
prestring=[]
poststring=[]
for i in range(0,Q):
  inputforround=input()
  if inputforround=='1':
    flipcounter=flipcounter+1
  else:
    T,F,C=map(str,inputforround.split())
    if (int(F)+flipcounter)%2==1:
      prestring.append(C)
    else:
      poststring.append(C)
prestring.reverse()
for j in splits:
  prestring.append(j)
for k in poststring:
  prestring.append(k)
if flipcounter%2==1:
  prestring.reverse()
ans=''.join(prestring)
print(ans)

Solution (Python):
from collections import deque
import numpy as np
 
S=deque(input())
Q=int(input())
count = 0
L=np.array([input().split() for _ in range(Q)])
 
for i in range(Q):
  l=L[i]
  if len(l)==1:
    count+=1
    continue
  elif len(l)==3:
    F=int(l[1])
    C=l[2]
    if (count%2==1) ^ (F==1):
      S.appendleft(C)
    else:
      S.append(C)
 
if count%2==1:
  S.reverse()
print(''.join(S))

Solution (Python):
from collections import deque
import numpy as np

S=deque(input())
Q=int(input())
count = 0
L=np.array([input().split() for _ in range(Q)])

for i in range(Q):
  l=L[i]
  if len(l)==1:
    count+=1
    continue
  elif len(l)==3:
    F=int(l[1])
    C=l[2]
    if count%2==0:
      if F==1:
        S.appendleft(C)
      else:
        S.append(C)
    else:
      if F==2:
        S.appendleft(C)
      else:
        S.append(C)

if count%2==1:
  S.reverse()
print(''.join(S))

Solution (Python):

from collections import deque
S = input()
Q = int(input())
qs = [input().split() for i in range(Q)]

ans = deque(S)
flag = 0
count = 0
for q in qs:
    if q[0] == '1':
        if flag == 0:
            flag = 1
        else:
            flag = 0
        count += 1
    else:
        if flag == 0:
            if q[1] == '1':
                ans.appendleft(q[2])
            else:
                ans.append(q[2])
        else:
            if q[1] == '1':
                ans.append(q[2])
            else:
                ans.appendleft(q[2])

if count % 2 == 1:
    ans.reverse()

print(*ans, sep='')

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

Solution (Python):

from collections import deque
S = input()
Q = int(input())
now = 1
tmp = [deque([]),deque([])]
for _ in range(Q):
    t = list(input().split())
    if t[0]=='1':
        now = 1+(now==1)
    else:
        if int(t[1])==now:
            tmp[0].append(t[2])
        else:
            tmp[1].append(t[2])
ans = ''
if now==1:
    i,j = 0,1
else:
    i,j = 1,0
while len(tmp[i])>0:
    ans += tmp[i].pop()
if now==2:
    S = S[::-1]
ans += S
while len(tmp[j])>0:
    ans += tmp[j].popleft()
print(ans)

Solution (Python):
s=input()
q=int(input())
mae=""
usr=""
flg=True
for i in range(q):    
    a=list(map(str,input().split()))
    if a[0]=="1":
        if flg:
            flg=False
        elif flg==False:
            flg=True
    if a[0]=="2":
        if a[1]=="1":
            if flg:
                mae=a[2]+mae
            else:
                usr=usr+a[2]
        if a[1]=="2":
            if flg:
                usr=usr+a[2]
            else:
                mae=a[2]+mae
if flg:
    ans=mae+s+usr
else:
    ans=usr[::-1]+s[::-1]+mae[::-1]
print(ans)

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

c = 0
w = []

for i in range(Q) :
    a = input()
    if a[0] == "1" :
        c ^= 1
    else :
        if a[2] == "1" :
            if c == 0 :
                w.append(a[4])
            else :
                S.append(a[4])
        else :
            if c == 0 :
                S.append(a[4])
            else :
                w.append(a[4])
S = w[::-1] + S
print("".join(S) if c == 0 else "".join(S[::-1]))

Solution (Python):
s=input()
q=int(input())
m=0
from collections import deque
s=deque(s)
for i in range(q):
  l=list(map(str,input().split()))
  if l[0]=="1":
    m+=1
    m%=2
  else:
    if m==0:
      if l[1]=="1":
        s.appendleft(l[2])
      else:
        s.append(l[2])
    else:
      if l[1]=="1":
        s.append(l[2])
      else:
        s.appendleft(l[2])
s=list(s)
if m==1:
  s=s[::-1]
print("".join(s))

Solution (Python):
s = input()
q = int(input())
h,t = [],[]
p = 0

for i in range(q):
    c = input().split()
    if c[0] =='1':
        h,t = t,h
        p = 1-p
    else:
        if c[1] =='1':
            h.append(c[2])
        else:
            t.append(c[2])
if p ==1:
    s = s[::-1]
print("".join(h[::-1])+s+"".join(t))

Solution (Python):
import sys
input = sys.stdin.readline
s = input().rstrip()
q = int(input())
query = []
rev = 0
for i in range(q):
    query.append(list(map(str, input().split())))
sa = []
sb = []
for i in range(q):
    if len(query[i]) == 1:
        rev = (rev + 1) % 2
    else:
        if rev == 0:
            if query[i][1] == '1':
                sa.append(query[i][2])
            else:
                sb.append(query[i][2])
        else:
            if query[i][1] == '2':
                sa.append(query[i][2])
            else:
                sb.append(query[i][2])
ssa = ''.join(sa)
ssa = ssa[::-1]
ssb = ''.join(sb)
ss = ssa + s + ssb
if rev == 1:
    ss = ss[::-1]
print(ss)

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

for i in range(Q):
    Query.append(list(input().split()))


turning = 1

ans = deque(S)
for i in Query:
    if i[0] == "1":
        turning *= -1
    else:
        if i[1] == "1":
            if turning == 1:
                ans.appendleft(i[2])
            else:
                ans.append(i[2])
        else:
            if turning == 1:
                ans.append(i[2])
            else:
                ans.appendleft(i[2])


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

Solution (Python):
from collections import deque
d=deque()
d.append(input())
q=int(input())
r=1
for i in range(q):
  q2=input().split()
  if int(q2[0])==1:
    r*=-1
  else:
    if (r==1 and int(q2[1])==1) or (r==-1 and int(q2[1])==2):
        d.appendleft(q2[2])
    else:
        d.append(q2[2])

print("".join(d) if r==1 else "".join(d)[::-1])

Solution (Python):
s = list(input())
q = int(input())
r = 0
first = []
last = []

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

    if query[0] == "1":
        r = (r+1) % 2
        continue

    if r == 0:
        if query[1] == "1":
            first.append(query[2])
        else:
            last.append(query[2])
    else:
        if query[1] == "1":
            last.append(query[2])
        else:
            first.append(query[2])

first.reverse()
ans = first + s + last

if r == 1:
    ans.reverse()

print("".join(ans))

Solution (Python):
from collections import deque
def main():
    s = deque(input())
    q = int(input())
    qs = [tuple(input().split()) for _ in range(q)]
    flag = 1
    for qse in qs:
        if qse[0] == '1':
            flag ^= 1
        else:
            f = int(qse[1]) - 1
            f ^= flag
            if f:
                s.appendleft(qse[2])
            else:
                s.append(qse[2])
    if flag:
        print("".join(s))
    else:
        ss = list(s)
        print("".join(ss[::-1]))

if __name__ == '__main__':
    main()

Solution (Python):
s=str(input())
s=list(s)
q=int(input())
sb=[0]*(q+1)
sa=[0]*(q+1)
sbi=0
sai=0
ta=0
for i in range(q):
  temp=str(input())
  temp=list(temp)
  if temp[0]=="1":
    ta=ta+1
  else:
    if (temp[2]=="1" and ta%2==0) or (temp[2]=="2" and ta%2!=0):
      sb[sbi]=temp[4]
      sbi=sbi+1
    else:
      sa[sai]=temp[4]
      sai=sai+1
lsb=sb.index(0)
lsa=sa.index(0)
sb.reverse()
ans=sb[len(sb)-lsb:len(sb)]+s+sa[0:lsa]
if ta%2!=0:
  ans.reverse()
print("".join(ans))

Solution (Python):
import sys 
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
S = readline().decode().rstrip()
Q = int(readline())
cnt = 0
start = ''
end = ''
for i in range(Q):
    A = list(map(str,readline().decode().split()))
    if A[0] == '1':
        cnt +=1
    else:
        if (cnt + int(A[1])) % 2:
            start += A[2]
        else:
            end += A[2]
ans = start + S + end
if cnt % 2 == 1:
    print(end[::-1] + S[::-1] + start)
else:
    print(start[::-1] + S + end)

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

for i in range(Q):
    a = input().split()
    if a[0]=="1":
        flag = not flag

    else:
        f = a[1]
        c = a[2]
        if f=="1":
            if flag:
                que.appendleft(c)
            else:
                que.append(c)
        else:
            if flag:
                que.append(c)
            else:
                que.appendleft(c)

if not flag:
    que.reverse()

print("".join(que))

Solution (Python):
from collections import deque

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

d = deque()
d.append(S)

for _ in range(Q):
    A = list(input().split())
    if A[0] == "1":
        if t == 0:
            t = 1
        else:
            t = 0

    elif (A[1] == "1" and t == 0) or (A[1] == "2" and t == 1):
        d.appendleft(A[2])
    else:
        d.append(A[2])

l = list(d)
ans = "".join(l)
if t == 1:
    ans = ans[::-1]
print(ans)

Solution (Python):
S = list(input())
Q = int(input())
a = 0
A = []
B = []
for i in range(Q):
  q = list(input().split())
  q[0] = int(q[0])
  if q[0] == 1:
    if a == 0:
      a = 1
    else:
      a = 0
  else:
    q[1] = int(q[1])
    if (q[1] == 1 and a == 0) or (q[1] == 2 and a == 1):
      B.append(q[2])
    else:
      A.append(q[2])
B = B[::-1]
ans = B + S + A
if a == 1:
  ans = ans[::-1]
ans = ''.join(ans)
print(ans)

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

q = int(input())

before = []

after = []

cnt = 0

for i in range(q):
    t = list(input().split())
    
    if t[0] == "1":
        cnt += 1
    else:
        f = t[1]
        c = t[2]
        if f == "1":
            if cnt%2 == 0:
                before.append(c)                      
            else:
                after.append(c)
        else:
            if cnt%2 == 0:
                after.append(c)
            else:
                before.append(c)
                
if cnt%2 == 1:
    s.reverse()
    after.reverse()
    
    for i in after:
        print(i,end = "")
    for i in s:
        print(i,end = "")
    for i in before:
        print(i,end = "")
else:
    before.reverse()
    before.extend(s)
    before.extend(after)
            
    for i in before:
        print(i,end = "")

Solution (Python):
from collections import deque
S= deque(input())
Q = int(input())
ba = 1
for i in range(Q):
    Query =list(map(str,input().split()))
    Query0 = Query[0]
    if Query[0] == '1':
        ba *= -1
    else:
        if Query[1] == '1' and ba ==1:
            S.appendleft(Query[2])
        elif Query[1] == '1' and ba ==-1:
            S.append(Query[2])
        elif Query[1] == '2' and ba ==1:
            S.append(Query[2])
        elif Query[1] == '2' and ba ==-1:
            S.appendleft(Query[2])
if ba == -1:
    S.reverse()
mojieru = ''.join(S)
print(mojieru)

Solution (Python):
from collections import deque

S = deque(list(input()))
Q = int(input())
q = [list(input().split()) for _ in range(Q)]
r = 0


for i in range(Q):
    if q[i][0] == "1":
        r += 1
    else:
        if (q[i][1] == "1" and r%2==0)or (q[i][1] == "2" and r%2==1 ):
            S.appendleft(q[i][2])
        else:
            S.append(q[i][2])

S = "".join(S)
if r%2==1:
    S = S[::-1]
print(S)

Solution (Python):
from collections import deque
S=deque(input())
Q=int(input())
reverse=0
for q in range(Q):
    Query=list(input().split())
    T=Query[0]
    if T=="1":
        reverse+=1
    if T=="2":
        F,C=Query[1],Query[2]
        if F=="1" and reverse%2==0 or F=="2" and reverse%2==1:
            S.appendleft(C)
        else:
            S.append(C)
S="".join(S)
print(S if reverse%2==0 else S[::-1])

Solution (Python):
from collections import deque

s = input()
q = int(input())
isInversed = False
front = deque()
back = deque()
for _ in range(q):
    inp =  list(input().split())
    if inp[0] == '1':
        isInversed = not isInversed
    else:
        if inp[1] == '1':
            if not isInversed:
                front.appendleft(inp[2])
            else:
                back.append(inp[2])
        else:
            if not isInversed:
                back.append(inp[2])
            else:
                front.appendleft(inp[2])

front_str = ''.join(front)
back_str = ''.join(back)
ans = front_str + s + back_str

if not isInversed:
    print(ans)
else:
    print(ans[::-1])

Solution (Python):
from collections import deque

s = input()
q = int(input())
isInversed = False
front = deque()
back = deque()
for _ in range(q):
    inp =  list(input().split())
    if inp[0] == '1':
        isInversed = not isInversed
    else:
        if inp[1] == '1':
            if not isInversed:
                front.appendleft(inp[2])
            else:
                back.append(inp[2])
        else:
            if not isInversed:
                back.append(inp[2])
            else:
                front.appendleft(inp[2])

front_str = ''.join(front)
back_str = ''.join(back)
ans = front_str + s + back_str

if not isInversed:
    print(ans)
else:
    print(ans[::-1])

Solution (Python):
s = input()
q = int(input())
before = []
after = []
isReversed = 0


for _ in range(q):
    tmp = input()
    if len(tmp) == 1:
        if isReversed:
            isReversed = 0
        else:
            isReversed = 1
    else:
        if tmp[2] == str(2):
            if isReversed:
                before.append(tmp[-1])
            else:
                after.append(tmp[-1])
        else:
            if isReversed:
                after.append(tmp[-1])
            else:
                before.append(tmp[-1])


if isReversed:
    ans = "".join(after[::-1]) + s[::-1] + "".join(before)
    print(ans)

else:
    ans = "".join(before[::-1]) + s + "".join(after)
    print(ans)

Solution (Python):
import sys
input = sys.stdin.readline
from collections import deque
 
S = input().rstrip()
S = deque(S)
Q = int(input())
Que = (input().rstrip() for _ in range(Q))
 
flag = 0 
for elem in Que:
  if elem[0] == '1':
    flag ^= 1
  else:
    s = int(elem[2])
    if s == 1 and flag == 0:
      S.appendleft(elem[4])
    elif s == 2 and flag == 0:
      S.append(elem[4])
    elif s == 1 and flag == 1:
      S.append(elem[4])
    else:
      S.appendleft(elem[4])
 
if flag == 1:
  S.reverse()
print(''.join(S))

Solution (Python):
from collections import deque
S=input()
N=int(input())
q = deque()
A = [input().split()]
for n in range(N-1):
  A.append(input().split())
for s in S:
  q.append(s)
top = True
for n in range(N):
  if A[n][0]=="1":
    top = not top
  elif A[n][1]=="1" and top or A[n][1]=="2" and not top:
    q.appendleft(A[n][2])
  else:
    q.append(A[n][2])
T = ""
while len(q)>0:
  if top:
    T+=q.popleft()
  else:
    T+=q.pop()
print(T)

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

t = 0
for _ in range(q):
  query = list(input().split())
  query[0] = int(query[0])
  if query[0] == 1: t = 1 - (t)
  else:
    if t == 0:
      if int(query[1]) == 1: s.appendleft(query[2])
      else: s.append(query[2])
    else:
      if int(query[1]) == 1: s.append(query[2])
      else: s.appendleft(query[2])
if t == 0: print(*s, sep = '')
else: print(*reversed(s), sep = '')

Solution (Python):
from collections import deque
s = input()
qq = int(input())
q = []
for _ in range(qq):
  q.append(input())
h = 1
for i in q:
  if i[0] == '1':
    h*=-1
if h == -1:
  ss = ''
  for i in reversed(s):
    ss += i
  s = ss
d = deque()
d.append(s)
h = 1
for i in q:
  if i[0] == '1':
    h*=-1
  else:
    if (h == 1 and i[2] == '1') or (h == -1 and i[2] == '2'):
      d.appendleft(i[4])
    else:
      d.append(i[4])
p = ''
if h == -1:
  for i in reversed(d):
    p += i
else:
  for i in d:
    p += i
print(p)

Solution (Python):
def main():
    from collections import deque
    d = deque([s for s in input()])
    Q = int(input())
    reverse = False

    for i in range(Q):
        q = input().split()
        if q[0] == "1":
            reverse = not reverse
        else:
            if (q[1] == "1" and not reverse) or (q[1] == "2" and reverse):
                d.appendleft(q[2])
            else:
                d.append(q[2])
    
    if reverse:
        d.reverse()
    
    print("".join(d))

if __name__ == '__main__':
    main()

Solution (Python):
s = input()
q = int(input())
list1 = []
list2 = []
flag = 1
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:
                list1.append(in_line[2])
            else:
                list2.append(in_line[2])
        else:
            if flag == 1:
                list2.append(in_line[2])
            else:
                list1.append(in_line[2])
if flag == 1:
    print("".join(list1[::-1]) + s + "".join(list2))
else:
    print("".join(list2[::-1]) + s[::-1] + "".join(list1))

Solution (Python):
S = list(input())
mae = []
ato = []
rev = False
Q = int(input())
for i in range(Q):
    A = list(input().rstrip().split(" "))
    if int(A[0]) == 1:
        if rev:
            rev = False
        else:
            rev = True
    else:
        if (rev and int(A[1]) == 2 or not rev and int(A[1]) == 1):
            mae.append(A[2])
        else:
            ato.append(A[2])
if rev:
    ato.reverse()
    S.reverse()
    print("".join(ato) + "".join(S) + "".join(mae))
else:
    mae.reverse()
    print("".join(mae) + "".join(S) + "".join(ato))

Solution (Python):
from collections import deque
import sys
s = deque(list(input()))
Q = int(input())
cnt = 0

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

if cnt == 1:
  s.reverse()
print("".join(s))

Solution (Python):
from collections import deque
S=deque(list(input()))
Q=int(input())
count=0
for i in range(Q):
  A=list(input().split())
  if A[0]=="1":
    count=(count+1)%2
  else:
    if A[1]=="1":
      if count==0:
        S.appendleft(A[2])
      else:
        S.append(A[2])
    else:
      if count==0:
        S.append(A[2])
      else:
        S.appendleft(A[2])
if count==1:
  S.reverse()
ans="".join(S)
print(ans)

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

for i in range(n):
    Input = input().split()
    if int(Input[0]) == 1:
        Reverse = abs(Reverse-1)
        continue
    else:
        if int(Input[1]) == 1:
            if Reverse == 0:
                q.appendleft(Input[2])
            else:
                q.append(Input[2])
        else:
            if Reverse == 0:
                q.append(Input[2])
            else:
                q.appendleft(Input[2])
if Reverse==1:
    q.reverse()
print(''.join(q))

Solution (Python):



from collections import deque
import sys
input = sys.stdin.readline
S = input().strip()
Q = int(input())
FQ_dic={}
for i in range(Q):
    FQ_dic[i] =input().split()

rev =0 
dq =deque(S)
for i in range(Q):
    if FQ_dic[i][0] =="1":
        rev +=1
    else:
        a,b,c =FQ_dic[i]
        if rev %2 ==0 and b =="1"  :
            dq.appendleft(c)
        elif rev %2 ==0 and b =="2":
            dq.append(c)
        elif rev %2 ==1 and b =="1":
            dq.append(c)
        elif rev %2 ==1 and b =="2":
            dq.appendleft(c)
if rev %2 ==1:
    dq.reverse()
ans ="".join(dq)
print(ans)

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

rev = 0

for i in range(N):
  Q = input().split()
  if Q[0] == "1":
    rev += 1
    rev %= 2
  else:
    if (rev + int(Q[1])) % 2 == 1:
      S.appendleft(Q[2])
    else:
      S.append(Q[2])
S = "".join(S)
if rev == 1:
  S = S[::-1]
print(S)

Solution (Python):
from collections import deque
H=deque(input())
Q=int(input())
reverse=False

A,B="",""
for _ in range(Q):
    T=input()
    if T[0]=="1":
        reverse=not reverse
    else:
        _,F,C=T.split()

        if ((not reverse) and F=="1") or (reverse and F=="2"):
            H.appendleft(C)
        else:
            H.append(C)

if reverse:
    H.reverse()

for s in H:
    print(s,end="")
print()

Solution (Python):
S = input()
N = int(input())
Q = tuple([_ for _ in input().split()] for _ in range(N))
C = 0
F = ""
R = ""
for q in Q:
    if q[0] == "1":
        C = (C+1)%2
    else:
        if (int(q[1])+C)%2 != 0:
            F = q[2] + F
        else:
            R = R + q[2]
S = F + S + R
if C != 0:
    S = S[::-1]
print(S)

Solution (Python):
S = input()
p = 0
head = ''
end = ''
Q = int(input())
for _ in range(Q):
  Qr = list(input().split())
  if int(Qr[0])==1:
    p = not p
  else:
    if int(Qr[1])==1:
      if p:
        end += Qr[2]
      else:
        head = Qr[2]+head
    else:
      if p:
        head = Qr[2]+head
      else:
        end += Qr[2]
S = head + S + end
if p:
  print(S[::-1])
else:
  print(S)

Solution (Python):
from collections import deque

s=list(input())
q=int(input())
d = deque(s)
reverse=False

for i in range(q):
  qs=list(input().split())
  if qs[0]=='1':
    reverse=not reverse
  else:
    if qs[1]=='1':
      d.appendleft(qs[2]) if not reverse else d.append(qs[2])
    else:
      d.append(qs[2]) if not reverse else d.appendleft(qs[2])

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

Solution (Python):
from collections import deque

S = input()
B = deque()
res = 0
for s in range(len(S)):
    B.append(S[s])
Q = int(input())
for q in range(Q):
    A = list(map(str, input().split()))
    if int(A[0]) == 1:
        if res == 0:
            res = 1
        else:
            res = 0
    else:
        if int(A[1]) == 1:
            if res == 0:
                B.appendleft(A[2])
            else:
                B.append(A[2])
        else:
            if res == 0:
                B.append(A[2])
            else:
                B.appendleft(A[2])
if res == 0:
    print(''.join(B))
else:
    B.reverse()
    print(''.join(B))

Solution (Python):
from collections import deque
s=deque(list(input()))
Q=int(input())
count = 0
for i in range(Q):
    q=input()
    if q[0]=='1':
        count += 1
    elif (q[2]=='1' and count%2==0) or (q[2]=='2' and count%2==1):
        s.appendleft(q[4])
    elif (q[2]=='2' and count%2==0) or (q[2]=='1' and count%2==1):
        s.append(q[4])

if count%2==0:
    print(''.join(s))
else:
    print(''.join(reversed(s)))

Solution (Python):
from collections import deque
S=deque(input())
Q=int(input())
rev=False
for i in range(Q):
    A=input().split()
    if A[0]=="1":
        if rev==False:rev=True
        else:rev=False
    else:
        if rev==False:
            if A[1]=="1":
                S.appendleft(A[2])
            else:
                S.append(A[2])
        else:
            if A[1]=="1":
                S.append(A[2])
            else:
                S.appendleft(A[2])
if rev:
    S.reverse()
print(*S,sep="")

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

S = input().rstrip()
S = deque(S)
Q = int(input())
Que = (input().rstrip() for _ in range(Q))

flag = 0 
for elem in Que:
  if elem[0] == '1':
    flag ^= 1
  else:
    s = int(elem[2])
    if s == 1 and flag == 0:
      S.appendleft(elem[4])
    elif s == 2 and flag == 0:
      S.append(elem[4])
    elif s == 1 and flag == 1:
      S.append(elem[4])
    else:
      S.appendleft(elem[4])

if flag == 1:
  S.reverse()
print(''.join(S))

Solution (Python):

from collections import deque

S = input()
ls = deque()
for i in range(len(S)):
    ls.append(S[i])


Q = int(input())

lq = [list(map(str, input().split())) for _ in range(Q)]




mode = 'jun'

for i in range(Q):
    query = lq[i]
    ti = query[0]
    if(ti == '1'):
        if(mode == 'jun'):
            mode = 'gyaku'
        else:
            mode = 'jun'
    else:
        fi = query[1]
        ci = query[2]
        if(fi == '1'):
            if(mode == 'jun'):
                ls.appendleft(ci)
            else:
                ls.append(ci)
        else:
            if(mode == 'jun'):
                ls.append(ci)
            else:
                ls.appendleft(ci)

a = []

if(mode == 'jun'):
    for i in range(len(ls)):
        a.append(ls[i])
else:
    for i in range(len(ls)):
        j = (-1) * (i+1)
        a.append(ls[j])


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

Solution (Python):
from collections import deque

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

rev = False

for i in range(q):
    inp = input()
    if inp == '1':
        rev = True if rev == False else False
    else:
        n = inp.split()[1]
        t = inp.split()[2]
        if n == '1':
            d.append(t) if rev == True else d.appendleft(t)
        else:
            d.appendleft(t) if rev == True else d.append(t)

if rev == True:
    d.reverse()

print(''.join(d))

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

head = 1
ss = ''

for i in range(q):
    if(a[i][0] == '1'):
        head *= -1
    else:
        if(a[i][1] == '2' and head == 1):
            s = s + a[i][2]
        elif(a[i][1] == '1' and head == -1):
            s = s + a[i][2]    
        else:
            ss = ss + a[i][2]
if(head == 1):
    print(ss[::-1] + s)
else:
    print(s[::-1] + ss)

Solution (Python):
from collections import deque
S = deque(input())
Q = int(input())
rev = False
for i in range(Q):
    query = input()
    if query[0] == '1':
        rev = not rev
    else:
        qtype = query[2]
        qstr = query[4]
        if qtype == "1":
            if not rev:
                S.appendleft(qstr)
            else:
                S.append(qstr)
        else:
            if not rev:
                S.append(qstr)
            else:
                S.appendleft(qstr)
if rev:
    print(*list(S)[::-1],sep="")
else:
    print(*S,sep="")

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

rev = False
before = []
after = []
for i in range(q):
  inp = list(input().split())
  if inp[0] == '1':
    rev = not rev
  else:
    f = inp[1]
    c = inp[2]
    if rev:
      if f == '1':
        f = '2'
      else:
        f = '1'
    if f == '1':
      before.append(c)
    else:
      after.append(c)
ans = ''
if rev:
  after.reverse()
  ans = ''.join(after) + s[::-1] + ''.join(before)
else:
  before.reverse()
  ans = ''.join(before) + s + ''.join(after)
print(ans)

Solution (Python):

s_list=list(input())
q=int(input())
r=0
sl_list=[]
for i in range(q):
    ope=input().split()
    if ope[0]=='1':
        r+=1
    else:
        if (int(ope[1])+r)%2 == 1:
            sl_list.append(ope[2])
        else:
            s_list.append(ope[2])
sl_list.reverse()
s_list=sl_list + s_list
if r%2 !=0:
    s_list.reverse()
print(''.join(s_list))

Solution (Python):
s = str(input())
q = int(input())
ls = [list(map(str,input().split())) for _ in range(q)]
a = -1
f = []
b = []
for i in range(q):
    if ls[i] == ["1"]:
        if a == -1:
            a = -2
        else:
            a = -1
    else:
        if a+int(ls[i][1]) == 0:
            f.append(ls[i][2])
        else:
            b.append(ls[i][2])
if a == -1:
    f = f[::-1]
    print("".join(f)+s+"".join(b))
else:
    b = b[::-1]
    print("".join(b)+s[::-1]+"".join(f))

Solution (Python):
s = input()
q = int(input())
left = ""
right = ""
flag_swap = False
for i in range(q):
  query = list(input().split())
  if query[0] == "1":
    flag_swap = not flag_swap
  else:
    if (query[1] == "1" and not flag_swap) or (query[1] == "2" and flag_swap):
      left += query[2]
    else:
      right += query[2]

if flag_swap:
  print(right[::-1]+s[::-1]+left)
else:
  print(left[::-1]+s+right)