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())

from collections import deque

S = deque(S)

tfc = [[] for _ in range(Q)]

swich = 0
for i in range(Q):
    tfc[i] = input().split()
    
    if int(tfc[i][0])==1:
        swich += 1
    else:
        if swich%2==0:
            if int(tfc[i][1])==1:
                S.appendleft(tfc[i][2])
            else:
                S.append(tfc[i][2])
        else:
            if int(tfc[i][1])==1:
                S.append(tfc[i][2])
            else:
                S.appendleft(tfc[i][2])

if swich%2==1:
    S = reversed(S)
    print("".join(S))
else:
    print("".join(S))

Solution (Python):
import sys
from collections import deque

s = input()
q = int(input())
ans = deque()
ans.append(s)
reverseFlg = False

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

    if query[0]=='1':
        reverseFlg = not reverseFlg
    else:
        if reverseFlg:
            if query[1]=='1':
                ans.append(query[2])
            else:
                ans.appendleft(query[2])
        else:
            if query[1]=='1':
                ans.appendleft(query[2])
            else:
                ans.append(query[2])
    

out = ''.join(ans)

if reverseFlg:
    print(out[::-1])
else:
    print(out)

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

addPlace = 1
frontlist=[]
endlist=[]
for i in range(len(query)):
    if query[i][0] == "1":
        addPlace = -1 * addPlace
    else:
        if (query[i][1] == "1" and addPlace == 1) or (query[i][1] == "2" and addPlace == -1):
            frontlist.append(query[i][2])
        else:
            endlist.append(query[i][2])
s = frontlist[::-1] + s + endlist
if addPlace == 1:
    print("".join(s))
else:
    print("".join(s[::-1]))

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

from collections import deque

S=deque(list(S))

flg=0

for i in range(Q):

    Query = list(input().split())
    
    if len(Query)!=1:
        F_i ,C_i = int(Query[1]),Query[2]
        
        if F_i==1 and flg==0:
            S.appendleft(C_i)
        
        elif F_i==2 and flg==0:
            S.append(C_i)   
        
        elif F_i==1 and flg==1:
            S.append(C_i)
        else:
            S.appendleft(C_i)
    else:
        if flg ==0:
            flg=1
        else:
            flg=0 

if flg ==1:
    S=reversed(S)

print(''.join(S))

Solution (Python):
from collections import deque
s = list(input())
d = deque(s)
q = int(input())
flag = True
for i in range(q):
  l = list(map(str,input().split()))
  if l[0] == '1':
    if flag:
      flag = False
    else:
      flag = True
  else:
    if l[1] == '1':
      if flag:
        d.appendleft(l[2])
      else:
        d.append(l[2])
    else:
      if flag:
        d.append(l[2])
      else:
        d.appendleft(l[2])
if flag:
  print(''.join(d))
else:
  d.reverse()
  print(''.join(d))

Solution (Python):
from collections import deque
from bisect import bisect_left, bisect_right

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

count = 0
l = []
reverse = []
for i in range(q):
	tmp = list(map(str,input().split()))
	if len(tmp) == 1:
		count += 1
		if len(l) != 0:
			reverse.append(len(l))
	elif tmp[1] == '1':
		l.append([1, tmp[2]])
	elif tmp[1] == '2':
		l.append([2, tmp[2]])



for i in range(len(l)):
	
	l[i][0] += len(reverse) - bisect_right(reverse,i)
	l[i][0] %= 2

if count % 2 == 1:
	s = s[::-1]
s = deque(s)

for i in range(len(l)):
	if l[i][0] == 0:
		s.append(l[i][1])
	else:
		s.appendleft(l[i][1])
print("".join(s))

Solution (Python):
from collections import deque
s = list(input())
d = deque(s)
q = int(input())
flag = True
for i in range(q):
  l = list(map(str,input().split()))
  if l[0] == '1':
    if flag:
      flag = False
    else:
      flag = True
  else:
    if l[1] == '1':
      if flag:
        d.appendleft(l[2])
      else:
        d.append(l[2])
    else:
      if flag:
        d.append(l[2])
      else:
        d.appendleft(l[2])
if flag:
  ll = list(d)
  print(''.join(ll))
else:
  d.reverse()
  ll = list(d)
  print(''.join(ll))

Solution (Python):
from collections import deque
s = list(input())
d = deque(s)
q = int(input())
flag = True
for i in range(q):
  l = list(map(str,input().split()))
  if l[0] == '1':
    if flag:
      flag = False
    else:
      flag = True
  else:
    if l[1] == '1':
      if flag:
        d.appendleft(l[2])
      else:
        d.append(l[2])
    else:
      if flag:
        d.append(l[2])
      else:
        d.appendleft(l[2])
if flag:
  ll = list(d)
  print(''.join(ll))
else:
  d.reverse()
  ll = list(d)
  print(''.join(ll))

Solution (Python):

import sys
from collections import deque

def solve():
    S = input()
    ans = deque()
    ans.append(S)
    Q = input()
    reverseFlg = False
    for i in range(int(Q)):
        TFC = input()
        l = TFC.split()
        if l[0] == "1":
            reverseFlg = not reverseFlg
        else:
            if reverseFlg:
                if l[1] == "1":
                    ans.append(l[2])
                else:
                    ans.appendleft(l[2])
            else:
                if l[1] == "1":
                    ans.appendleft(l[2])
                else:
                    ans.append(l[2])
    
    res = ''.join(ans)
    if(reverseFlg):
        print(res[::-1])
    else:
        print(res)


            

    return 

def main():
    solve()

if __name__ == '__main__':
    main()

Solution (Python):
from collections import deque

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

head = 1
for _ in range(Q):
    cmd = input()
    if cmd == "1":
        head *= -1
    elif "2 1" == cmd[:3]:
        if head == 1:
            S.appendleft(cmd[4])
        else:
            S.append(cmd[4])
    else:
        if head == 1:
            S.append(cmd[4])
        else:
            S.appendleft(cmd[4])

ans = ""
for _ in range(len(S)):
    if head == 1:
        ans += S.popleft()
    else:
        ans += S.pop()

print(ans)

Solution (Python):
x = input()
s = []
n = int(input())
for i in range(n):
    array = list(map(str,input().split()))
    s.append(array)
    
a = ""
b = ""
c = 0
for i in range(n):
    if s[i][0] == "1":
        c += 1
        c = c%2
    else:
        if c == 0:
            if s[i][1] == "1":
                a = s[i][2] + a
            else:
                b = b + s[i][2]
        else:
            if s[i][1] == "1":
                b = b + s[i][2]
            else:
                a = s[i][2] + a
if c == 0:
    print(a + x + b)
else:
    print(''.join(list(reversed(b))) + ''.join(list(reversed(x))) + ''.join(list(reversed(a))))

Solution (Python):
def main():
    S = input()
    Q = int(input())
    queries = [input() for _ in range(Q)]

    reverse = False
    front = ""

    for e in queries:
        if e[0] == '1':
            reverse = not reverse
        else:
            T, F, C = e.split()
            if F == '2': F = True
            else: F = False

            if reverse ^ F:
                
                S += C
            else:
                
                
                front += C
        

    if reverse:
        S = S[::-1]
        S = S + front
    else:
        S = front[::-1] + S

    print(S)
    

if __name__ == '__main__':
    main()

Solution (Python):
from collections import deque

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

tmp = deque(S)
houkou = 1 

for i in range(Q):
    inp = input().split()
    if inp[0] == '1':
        if houkou == 1:
            houkou = 2
        else:
            houkou = 1
    else:
        if houkou == 1 and inp[1] == '1':
            tmp.appendleft(inp[2])
        elif houkou == 1 and inp[1] == '2':
            tmp.append(inp[2])
        elif houkou == 2 and inp[1] == '1':
            tmp.append(inp[2])
        else:
            tmp.appendleft(inp[2])
    


output = ''.join(list(tmp))
if houkou == 2:
    output = output[::-1]

print(output)

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

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

Solution (Python):

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

is_reverse = False
before = ""
after = ""

for _ in range(q):
  lst = input().split()
  if lst[0] == "1":
    is_reverse = not is_reverse
  else:
    _, f, c = lst
    if f == "1":
      if not is_reverse:
        before = c + before
      else:
        after = after + c
    else:
      if not is_reverse:
        after = after + c
      else:
        before = c + before

if is_reverse:
  s = s[::-1]
  before, after = after[::-1], before[::-1]

print(before + s + after)

Solution (Python):
from collections import deque

S = deque(input())
lenq = int(input())
Q = [input() for _ in range(lenq)]






head = 1 
for q in Q:
    if q == '1':
        head *= -1
    else:
        _, f, c = q.split()
        if f == '1':
            if head == 1:
                S.appendleft(c)
            else:
                S.append(c)
        else:  
            if head == 1:
                S.append(c)
            else:
                S.appendleft(c)
S = ''.join(list(S))
if head == -1:
    S = S[::-1]
print(S)

Solution (Python):
from collections import deque

S = deque(input())
lenq = int(input())
Q = [input() for _ in range(lenq)]






head = 1 
for q in Q:
    if q == '1':
        head *= -1
    else:
        _, f, c = q.split()
        if f == '1':
            if head == 1:
                S.appendleft(c)
            else:
                S.append(c)
        else:  
            if head == 1:
                S.append(c)
            else:
                S.appendleft(c)
S = ''.join(list(S))
if head == -1:
    S = S[::-1]
print(S)

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

ok = True 
for i in range(Q):
    l = q[i]
    if l[0] == "1":
        if ok: ok = False
        else: ok = True
    else:
        if ok:
            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])
if not ok:
    S = list(S)[::-1]
print("".join(list(S)))

Solution (Python):
from collections import deque
 
S = deque(list(input()))
Q = int(input())
rvs = False
 
for q in range(Q):
    query = list(input().split())
    if len(query) > 1:
        if query[1] == "1":
            if rvs:
                S.append(query[2])
            else:
                S.appendleft(query[2])
        elif query[1] == "2":
            if rvs:
                S.appendleft(query[2])
            else:
                S.append(query[2])
    else:
        rvs = not (rvs)
 
if rvs:
    S.reverse()
 
print("".join(S))

Solution (Python):


def main():
    from itertools import chain
    S = input()
    Q = int(input())
    Queries = [input().split() for _ in range(Q)]
    H_T = H, T = [], []
    flag = 0
    for query in Queries:
        if len(query) == 1:
            flag ^= 1
            continue
        _, f, c = query
        H_T[(int(f) + flag - 1) % 2].append(c)
    t = ''.join(chain(reversed(H), S, T))
    if flag:
        t = reversed(t)
    print(''.join(t))


if __name__ == '__main__':
    main()

Solution (Python):
s = input()
q = int(input())
mode = False
mae = ""
usiro = ""

for i in range(q):
  q_list = list(input().split())
  if q_list[0] == "1":
    mode = not mode
  else:
    if mode:
      if q_list[1] == "1":
        usiro = usiro + q_list[2]
      else:
        mae = q_list[2] + mae
    else:
      if q_list[1] == "1":
        mae = q_list[2] + mae  
      else:
        usiro = usiro + q_list[2]
if mode:
  print(usiro[::-1]+s[::-1]+mae[::-1])
else:
  print(mae+s+usiro)

Solution (Python):
S = input()
Q = int(input())
flag = 1
left = ""
right = ""

for i in range(Q):
    q = input().split()
    if q==["1"]:
        flag *= -1
    else:
        if flag==1 and q[1]=="1":
            left = q[2]+left
        elif flag==1 and q[1]=="2":
            right += q[2]
        elif flag==-1 and q[1]=="1":
            right += q[2]
        else:
            left = q[2]+left


if flag==1:
    print(left+S+right)
else:
    print(right[::-1]+S[::-1]+left[::-1])

Solution (Python):
from collections import deque

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

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

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

Solution (Python):
from collections import deque


def main():
    side = 0
    S = input()
    len_s = len(S)
   
    d = deque()
    for s in S:
        d.append(s)
   
    n = int(input())
    for i in range(n):
        q = input().split()
        if len(q) == 1:
            side += 1
        elif len(q) == 3:
            len_s += 1
            if side % 2 == 0:
                if q[1] == "1":
                    d.appendleft(q[2])
                else:
                    d.append(q[2])
            else:
                if q[1] == "1":
                    d.append(q[2])
                else:
                    d.appendleft(q[2])
    S = ""
    if side % 2 == 0:
        for i in range(len_s):
            S += d.popleft()
    else:
        for i in range(len_s):
            S += d.pop()
 
    print(S)


if __name__ == "__main__":
    main()

Solution (Python):
s = input()
q = int(input())
quest = [0]*q
state = 0
former, latter = '', ''
for i in range(q):
    que = input().split()

    if (que[0] == '1'):
        
        state = 1 - state
        continue
    else:
        if (state == 0 and que[1] == '1'):
            former = que[2] + former
        elif(state == 0 and que[1] == '2'):
            latter = latter + que[2]
        elif (state == 1 and que[1] == '1'):
            latter = latter + que[2]
        else:
            former = que[2] + former

if (state == 1):
    print(latter[::-1] + s[::-1] + former[::-1])
else:
    print(former + s + latter)

Solution (Python):
S = input()
Q = int(input())
rev = False
ends = [[], []]
for _ in range(Q):
    parts = input().split()
    if parts[0] == "1":
        rev = not rev
    else:
        _, end, s = parts
        ends[(int(end) - 1) ^ rev].append(s)
res = list(reversed(ends[0])) + list(S) + ends[1]
if rev:
    res = reversed(res)
print("".join(res))

Solution (Python):
s = input()
q = int(input())
tbl = [input().split() for _ in range(q)]
cnt = 0
r = []
l = []
for i in range(q-1,-1,-1):
    if tbl[i][0] == '1':
        cnt += 1
    else:
        if (cnt + int(tbl[i][1]))%2 == 1:
            l.append(tbl[i][2])
        else:
            r.append(tbl[i][2])
l = ''.join(l)
if cnt%2 == 1:
    s = s[::-1]
r = ''.join(r[::-1])
print(l+s+r)

Solution (Python):
from collections import deque

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

reverse = False
cnt = 0
for i in range(Q):
    inp = input().split()
    if inp[0] == "1":
        cnt += 1
        reverse = (cnt % 2 != 0)
        
    else:
        f, c = inp[1:]
        if int(f) == 1:
            if reverse:
                S.append(c)
            else:
                S.appendleft(c)
        else:
            if reverse:
                S.appendleft(c)
            else:
                S.append(c)
S = "".join(S)
print(S[::-1] if reverse else S)

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

direction = True
front = ""
back = ""

for i in range(q):
    q_i = input().split()
    if q_i[0] == "1":
        if direction:
            direction = False
        else:
            direction = True

    else:
        if direction:
            if q_i[1] == "1":
                front += q_i[2]
            else:
                back += q_i[2]
        else:
            if q_i[1] == "1":
                back += q_i[2]
            else:
                front += q_i[2]

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

Solution (Python):
from collections import deque
S=input()
st="X"
Q=int(input())
q=[]
for _ in range(Q):
  q.append(input())
t=0
for i in range(Q):
  if q[i]=="1":
    t+=1
  else:
    T,F,C=q[i].split()
    if F=="1" and t%2==0:
      st=C+st
    elif F=="2" and t%2==0:
      st=st+C
    elif F=="1" and t%2==1:
      st=st+C
    else:
      st=C+st
if t%2==0:
  print(st.replace("X",S))
else:
  print(st.replace("X",S)[::-1])

Solution (Python):
from collections import deque
s = deque(input())
n = int(input())
re = 0
st = ''
en = ''
for i in range(n):
    a = list(input().split())
    if a[0]=='1':
        re = 1 if re==0 else 0
    else:
        if a[1]=='1':
            if re==0:
                s.appendleft(a[2])
            else:
                s.append(a[2])
        else:
            if re==0:
                s.append(a[2])
            else:
                s.appendleft(a[2])
if re==1:
    s.reverse()
res = list(s)
print(''.join(res))

Solution (Python):
def main():
    s = input()
    q = int(input())
    l,r = '',''
    rev = 0
    for i in range(q):
        Q = input().split()
        if Q[0]=="1":
            rev = (rev+1)%2
        else:
            if Q[1]=='1':
                if rev==0:
                    l = Q[2]+l
                else:
                    r = r+Q[2]
            else:
                if rev==0:
                    r = r+Q[2]
                else:
                    l = Q[2]+l
    s = l+s+r
    if rev==0:
        print(s)
    else:
        print(s[::-1])

if __name__ == "__main__":
    main()

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

add_s = ['', ''] 
rev_flag = 0

for i in range(q):
    query = input()
    if query == '1':
        rev_flag = (rev_flag+1)%2
    else:
        _, f, c = query.split()
        if f == '1':
            add_s[rev_flag] += c
        else:
            add_s[(rev_flag+1)%2] += c

if rev_flag == 1:
    s = s[::-1]

print(add_s[rev_flag][::-1]+s+add_s[(rev_flag+1)%2])

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

Solution (Python):
from collections import deque

S = deque(list(input()))
Q = int(input())
rvs = False

for q in range(Q):
    query = list(input().split())
    if len(query) > 1:
        if query[1] == "1":
            if rvs:
                S.append(query[2])
            else:
                S.appendleft(query[2])
        elif query[1] == "2":
            if rvs:
                S.appendleft(query[2])
            else:
                S.append(query[2])
    else:
        rvs = not (rvs)

if rvs:
    S.reverse()

print("".join(S))

Solution (Python):
from collections import deque

S = deque(list(input()))
Q = int(input())
rvs = False

for q in range(Q):
    query = list(input().split())
    if len(query) > 1:
        if query[1] == "1":
            if rvs:
                S.append(query[2])
            else:
                S.appendleft(query[2])
        elif query[1] == "2":
            if rvs:
                S.appendleft(query[2])
            else:
                S.append(query[2])
    else:
        rvs = not (rvs)

if rvs:
    S.reverse()

print("".join(S))

Solution (Python):
def main():
    from collections import deque
    import sys
    def input():
        return sys.stdin.readline()[:-1]
    S=input()
    Q=int(input())
    A=[input().split() for _ in range(Q)]
    d=deque(S)
    
    d_left=d.appendleft
    d_right=d.append
    j=["1","2"]
    k=0
    j_re=j.reverse
    for x in A:
        if x[0]=="1":
            j_re()
            k+=1
        else:
            if x[1]==j[0]:
                d_left(x[2])
            else:
                d_right(x[2])
    if k%2==1:
        d.reverse()
    print("".join(d))    

if __name__ == '__main__':
    main()

Solution (Python):




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

rev =0
front =""
back =""

for i in range(Q):
   

    if FQ_dic[i][0] =="1": 
                rev +=1
    else:                   
        if FQ_dic[i][1]=="1":     
            if rev %2 ==0:
                front += FQ_dic[i][2]
            else:
                back  += FQ_dic[i][2]  
        else: 
            if rev %2 ==0:
                back  += FQ_dic[i][2]  
            else:        
                front += FQ_dic[i][2]
front =front[::-1]
S =front + S + back

if rev %2 ==0:
    print(*S,sep="")
else:
    print(*S[::-1],sep="")

Solution (Python):
from collections import deque

def main():
    S = deque(input())
    
    Q = int(input())
    count = 1

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

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

if __name__ == "__main__":
    main()

Solution (Python):
from collections import deque
S = input()
s = list(S)
d = deque()
for i in range(len(s)):
    d.append(s[i])
Q = int(input())
p = [[0 for i in range(3)]for j in range(Q)]
x = 1
for i in range(Q):
    p[i] = list(map(str,input().split()))
def add(s,x,f,c):
    if x ==1:
        if f == 1:
            s.appendleft(str(c))
        else:
            s.append(str(c))
    else:
        if f == 1:
            s.append(str(c))
        else:
            s.appendleft(str(c))
for i in range(Q):
    if int(p[i][0]) == 1:
        x = -x
    else:
        add(d,x,int(p[i][1]),p[i][2])
if x == 1:
    print(''.join(d))
else:
    d.reverse()
    print(''.join(d))

Solution (Python):
S = input()
Q = int(input())
q = [input() for i in range(Q)]
reverse = False
p =[]
f =[]
appP = p.append
appF = f.append

for i in range(0,Q,1):
    if q[i][0]=="1":
        reverse=not reverse

    elif q[i][0]=="2":
        num,F,C = q[i].split(sep=" ")
        if F=="1":
            if reverse == False:
                appP(C)
            else:
                appF(C)
        elif F=="2":
            if reverse == False:
                appF(C)
            else:
                appP(C)
if reverse:
    tmpP="".join(p)
    tmpF="".join(f[::-1])
    print(tmpF+S[::-1]+tmpP)
else:
    tmpF="".join(f)
    tmpP="".join(p[::-1])
    print(tmpP+S+tmpF)

Solution (Python):
import numpy as np
s = input()
q = int(input())
rbef = []
aft = []
sgn = 1
for i in range(q):
    qi = np.array(input().split(),dtype=str)
    if qi[0] == "1":
        sgn *= -1
    else:
        if (qi[1] == "1" or sgn == -1) and (qi[1] == "2" or sgn == 1):
            rbef.append(qi[2])
        else:
            aft.append(qi[2])

bef = "".join(reversed(rbef))
taft = "".join(aft)
ans = bef + s + taft

if sgn == 1:
    print(ans)
else:
    rans = ''.join(list(reversed(ans)))
    print(rans)

Solution (Python):
S = input()
Q = int(input())
rev = False
ad = ''
for _ in range(Q):
    A = input().split()
    if len(A) == 1:
        rev ^= True
    else :
        if (A[1] == '1' and rev == False) or (A[1] == '2' and rev == True):
            ad += A[2]
        else :
            S += A[2]

ans = (ad[::-1] + S)[::1 - 2 * rev]
print(ans)

Solution (Python):
import collections as co
S=input()
Q=int(input())
count=0
back=str()
front=str()
judge=True
for i in range(Q):
    a=list(map(str,input().split()))   
    if  int(a[0])==1:
        if judge==True:
            judge=False
        else:
            judge=True
    elif int(a[0])==2 and int(a[1])==1:
        if judge==True:
            front=a[2]+front
        if judge==False:
            back=back+a[2]
    else:
        if judge==True:
            back=back+a[2]
        if judge==False:
            front=a[2]+front
    a.clear
if judge==True:
    S=front+S+back
if judge==False:
    S=front+S+back
    S=S[::-1]
print(S)

Solution (Python):
s=input()
n=int(input())
str=[""]*(n*2+1)
str[n]=s
start=n-1
end=n+1
dir=1


for _ in range(n):
    h=input()
    if h[0]=="1":
        if end-start!=2:
            dir*=-1
    else:
        if h[2]=="1":
            if dir==1:
                str[start]=h[4]
                start-=1
            else:
                str[end]=h[4]
                end+=1
        else:
            if dir==1:
                str[end]=h[4]
                end+=1
            else:
                str[start]=h[4]
                start-=1
if dir==-1:
    str.reverse()
    str[n]="".join(list(reversed(str[n])))
print("".join(str))

Solution (Python):
import collections
s = input()
q = int(input())
back = 1
t = collections.deque()
for ss in s:
    t.append(ss)
for i in range(q):
    query = input().split()
    if query[0] == '1':
        back *= -1
    else:
        if query[1] == '1':
            if back == 1:
                t.appendleft(query[2])
            else:
                t.append(query[2])
        else:
            if back == 1:
                t.append(query[2])
            else:
                t.appendleft(query[2])
ans = ''
if back == -1:
    for i in range(len(t)):
        ans += t.pop()
else:
    for i in range(len(t)):
        ans += t.popleft()
print(ans)

Solution (Python):
s = input()
T = ""
n = int(input())
for i in range(n):
    
    t = list(map(str,input().split()))
    if t[0] == "1":
        s,T = T,s
    else:
        if t[1] == "1":
            T += t[2]
        else:
            s += t[2]

print(T[::-1]+s)

Solution (Python):
s = list(input())
q = int(input())
judge = 1
front = []
back = []
for i in range(q):
  a = list(map(str,input().split()))
  if int(a[0]) == 1:
    judge *= -1
  else:   
    if (int(a[1]) == 1 and judge == 1) or (int(a[1]) == 2 and judge == -1):
      front.append(a[2])
    else:
      back.append(a[2])
ans = front[::-1] + s + back
if judge == -1:
  ans.reverse()
print(*ans, sep='')

Solution (Python):
from collections import deque
s = deque(input())
Q = int(input())
a = True
for i in range(Q):
    t = list(input().split())
    if t[0] == "1":
        a = not(a)
    if t[0] == "2":
        if ((t[1] == "1") and not a ) or ((t[1]=="2") and a ):
            s.append(t[2])
        else:
            s.appendleft(t[2])
if not a :
    s.reverse()
print(*s,sep = "")

Solution (Python):
from collections import deque

S = deque(input())
Q = int(input())
reverse_mode = False 
reversed_count = 0
 
for i in range(Q):
    query = [str(e) for e in input().split()]
    if len(query)==1:
        reversed_count += 1
        
        if reverse_mode == False:
            reverse_mode = True
        elif reverse_mode == True:
            reverse_mode = False
    
    else:
        if reverse_mode == False:
            if query[1]=="1":
            
                S.appendleft(query[2])
            else:
            
                S.append(query[2])
        elif reverse_mode == True:
            if query[1]=="1":
            
                S.append(query[2])
            else:
            
                S.appendleft(query[2])


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

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

Solution (Python):
from collections import deque

S = deque(input())
Q = int(input())
reverse_mode = False 
reversed_count = 0
 
for i in range(Q):
    query = [str(e) for e in input().split()]
    if len(query)==1:
        reversed_count += 1
        
        if reverse_mode == False:
            reverse_mode = True
        elif reverse_mode == True:
            reverse_mode = False
    
    else:
        if reverse_mode == False:
            if query[1]=="1":
            
                S.appendleft(query[2])
            else:
            
                S.append(query[2])
        elif reverse_mode == True:
            if query[1]=="1":
            
                S.append(query[2])
            else:
            
                S.appendleft(query[2])


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

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

Solution (Python):
from math import floor
import sys
from copy import copy

from collections import deque
input = sys.stdin.readline
'''
n, m = map(int, input().split())
n = int(input())
A = list(map(int, input().split()))
S = input().strip()
for test in range(int(input())):
'''
inf = 100000000000000000  
mod = 998244353




S = input().strip()
D = deque(S)


n = int(input())
reverse = False
for operation in range(n):
    data = input().strip()
    if data[0] == "1":
        if reverse is False:
            reverse = True
        else:
            reverse = False
    else:
        f = data[2]
        ele = data[4]
        if (f == "1" and reverse is False) or (f == "2" and reverse == True):
            D.appendleft(ele)
        else:
            D.append(ele)


if reverse is True:
    D.reverse()
for d in D:
    print(d,end="")