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

import sys
from collections import deque


def solve(S, Q, query):

    state = 1 
    head = deque([])
    tail = deque([])

    for qq in query:
        if qq[0] == "1":
            state *= -1
        else:
            if qq[1] == "1":
                is_head = (state==1)
            elif qq[1] == "2":
                is_head = (state==-1)
            
            if is_head:
                head.append(qq[2])
            else:
                tail.append(qq[2])
    
    head = "".join([head.pop() for _ in range(len(head))])
    tail = "".join([tail.popleft() for _ in range(len(tail))])

    print((head + S + tail)[::state])

    
    return



def main():
    def iterate_tokens():
        for line in sys.stdin:
            
            yield line
    tokens = iterate_tokens()
    S = str(next(tokens)).replace('\n', '')  
    Q = str(next(tokens)).replace('\n', '')  
    query = []
    for x in tokens:
        sp = x.split()
        
        if sp[0] == "1": query.append(sp[0])
        else :
            query.append((sp[0], sp[1], sp[2]))
    

    solve(S, Q, query)

if __name__ == '__main__':
    main()

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

Solution (Python):
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10**9 + 7

 
S = input()
Q = INT()
q = deque(S)
flg = 1
for _ in range(Q):
	Que = input().split()
	if Que[0] == "1":
		flg *= -1
	else:
		_, F, C = Que
		if any(int(F)*flg == x for x in [2, -1]):
			q.append(C)
		else:
			q.appendleft(C)
if flg == 1:
	print(*q, sep = "")
else:
	print(*list(q)[::-1], sep = "")

Solution (Python):
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10**9 + 7

 
S = input()
Q = INT()
q = deque(S)
flg = 1
for _ in range(Q):
	Que = input().split()
	if Que[0] == "1":
		flg *= -1
	else:
		_, F, C = Que
		if any(int(F)*flg == x for x in [2, -1]):
			q.append(C)
		else:
			q.appendleft(C)


ans = "".join(q)
if flg == -1:
	ans = ans[::-1]
print(ans)

Solution (Python):
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10**9 + 7

 
S = input()
Q = INT()
q = deque(S)
flg = 1
for _ in range(Q):
	Que = input().split()
	if Que[0] == "1":
		flg *= -1
	else:
		_, F, C = Que
		if any(int(F)*flg == x for x in [2, -1]):
			q.append(C)
		else:
			q.appendleft(C)
if flg == 1:
	print(*q, sep = "")
else:
	print(*list(q)[::-1], sep = "")

Solution (Python):
from collections import deque

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

q = deque()
rev = 0

for s in S:
  q.append(s)

for _ in range(Q):
  TFC = input()
  if len(TFC) == 1: 
    rev ^= 1
  else:
    T, F, C = (i for i in TFC.split())  
    if (F == '2' and not rev) or (F == '1' and rev):
      q.append(C)
    else:
      q.appendleft(C)
      
ans = ''.join(q)

if rev:
  print(ans[::-1])
else:
  print(ans)

Solution (Python):
import sys
def input(): return sys.stdin.readline().strip()

def resolve():
    from collections import deque
    d=deque(input())
    q=int(input())
    rev=False
    for i in range(q):
        l=input().split()
        if len(l)==1:
            if rev:
                rev=False
            else:
                rev=True
        else:
            if rev:
                if l[1]=='1':
                    d.append(l[2])
                else:
                    d.appendleft(l[2])
            else:
                if l[1]=='1':
                    d.appendleft(l[2])
                else:
                    d.append(l[2])
    if rev:
        d=reversed(d)
    print(''.join(d))
resolve()

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

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

    dq = deque(s)
    head_is_0 = 1
    for _ in range(q):
        query = iter(input().rstrip().split())
        if next(query) == '1':
            head_is_0 ^= 1
        elif (next(query) == '2') ^ head_is_0:
            dq.appendleft(next(query))
        else:
            dq.append(next(query))

    if not head_is_0:
        dq.reverse()

    ret = ''.join(dq)
    print(ret)


if __name__ == '__main__':
    main()

Solution (Python):
from collections import deque

S = deque(input())
Q = int(input())
Queries = [input().split() for _ in range(Q)]

need_reverse = False

for q in Queries:
    if q[0] == "1":
        need_reverse = not need_reverse
    else:
        if q[1] == "1":
            if not need_reverse:
                S.appendleft(q[2])
            else:
                S.append(q[2])
        else:
            if not need_reverse:
                S.append(q[2])
            else:
                S.appendleft(q[2])

S = "".join(S)
print(S if not need_reverse else S[::-1])

Solution (Python):
from collections import deque

s = deque(input())
q = int(input())
flag = True
for _ in range(q):
    query = input().split()
    if query[0] == "1":
        flag = not flag
    else:
        if (query[1] == "1" and flag) or (query[1] == "2" and not flag):
            s.appendleft(query[2])
        else:
            s.append(query[2])
if not flag:
    s.reverse()
print(''.join(s))

Solution (Python):
from collections import deque

q = deque(input())
Q = int(input())
reverse = -1

for i in range(Q):
    X = input().split()
    if X[0] == "1":
        reverse *= -1
    
    elif X[1] == "1":
        if reverse == -1:
            q.appendleft(X[2])
        else:
            q.append(X[2])
    elif X[1] == "2":
        if reverse == -1:
            q.append(X[2])
        else:
            q.appendleft(X[2])


if reverse == -1:
    print(''.join(q))
else:
    print(''.join(list(q)[::-1]))

Solution (Python):
from collections import deque

s = deque(input())
q = int(input())
qu = [[0] * 3] * q
count = 0

for i in range(q):
    qu[i] = input().split()

for i in range(q):
    if qu[i][0] == '1':
        count += 1
        continue

    else:
        if (count + int(qu[i][1])) % 2:
            s.appendleft(qu[i][2])
        else:
            s.append(qu[i][2])

if count % 2:
    s.reverse()

print(''.join(s))

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

Solution (Python):
from collections import deque

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

Solution (Python):
from collections import deque
q = deque(list(input()))
n = int(input())
han = 0
zengo = 0
for i in range(n):
    a = input().split()
    
    if len(a)==1:
        zengo ^=1
        han += 1
    else:
        a[1] = int(a[1])-1
        if a[1] == 0:
            if zengo == 0:
                q.appendleft(a[2])
            else:
                q.append(a[2])
        else:
            if zengo == 0:
                q.append(a[2])
            else:
                q.appendleft(a[2])
q = list(q)
if han >2 and han%2 :
    q.reverse()
print("".join(q))

Solution (Python):
num = 0
s = input()
k = int(input())
pre = ""
out = ""
for _ in range(k):
  inp = input()
  if inp[0] == "1":
    num += 1
    num %= 2
  else:
    if (inp[2] == "1" and num == 0) or (inp[2] == "2" and num == 1):
      pre = pre + inp[4]
    else:
      out = out + inp[4]
if num:
      print(out[::-1] + s[::-1] + pre)
else:
  print(pre[::-1] + s + out)

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

s = deque(s)
flip = False

for i in range(q):
    Q = list(input().split())
    if int(Q[0]) == 1:
        flip = not flip
    else:
        if int(Q[1]) == 1:
            if not flip:
                s.appendleft(Q[2])
            else:
                s.append(Q[2])
        else:
            if not flip:
                s.append(Q[2])
            else:
                s.appendleft(Q[2])

ans = ''

for _ in range(len(s)):
    if not flip:
        ans += s.popleft()
    else:
        ans += s.pop()

print(ans)

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

s = deque(s)
cnt = 0

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

ans = ''

for _ in range(len(s)):
    if cnt % 2 == 0:
        ans += s.popleft()
    else:
        ans += s.pop()

print(ans)

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

flg = False
for i in range(q):
    p = list(map(str, input().split()))
    if p[0] == "1":
        flg = True if flg == False else False
        continue
    else:
        if (p[1] == "1" and not flg) or (p[1] == "2" and flg):
            former.appendleft(p[2])
        else:
            latter.append(p[2])

ans = list(former) + list(s) + list(latter)
ans = "".join(ans)
print(ans if not flg else ans[::-1])

Solution (Python):
from collections import deque
S = deque(list(input().strip()))
Q = int(input())
cnt = 0
for i in range(Q):
    q = list(input().split())
    if len(q)==1:
        cnt += 1
    else:
        F = int(q[1])
        C = q[2]
        if cnt%2==0:
            if F==1:
                S.appendleft(C)
            else:
                S.append(C)
        else:
            if F==1:
                S.append(C)
            else:
                S.appendleft(C)
if cnt%2==0:
    print("".join(S))
else:
    S = list(S)[::-1]
    print("".join(S))

Solution (Python):
flip = 0
front = ""
back = ""

S = input()
Q = int(input())
for _ in range(Q):
    q = input().split()
    if len(q) == 1:
        flip ^= 1
        continue
    _, f, c = q
    if not flip:
        if f == "1":
            front = c + front
        else:
            back += c
    else:
        if f == "1":
            back += c
        else:
            front = c + front

ans = front + S + back
if flip:
    ans = ans[::-1]

print(ans)

Solution (Python):
import sys
from collections import deque

sys.setrecursionlimit(10 ** 7)
f_inf = float('inf')
mod = 10 ** 9 + 7


def resolve():
    s = input()
    q = int(input())
    query = [list(input().split()) for _ in range(q)]

    S = deque([s])
    flg = 1
    for i in range(q):
        if query[i][0] == "1":
            flg ^= 1
        else:
            _, f, c = query[i]
            if f == "1":
                S.appendleft(c) if flg else S.append(c)
            else:
                S.append(c) if flg else S.appendleft(c)

    print("".join(S) if flg else "".join(S)[::-1])



if __name__ == '__main__':
    resolve()

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

def split(word): 
    return [char for char in word]  
def convert(s): 
  
    
    new = "" 
  
    
    for x in s: 
        new += x  
  
    
    return new 
      
      

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=convert(prestring)
print(ans)

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

Solution (Python):
from collections import deque
S = input()
SS = deque()
for s in S:
    SS.append(s)
Q = int(input())
Que = [list(input().split()) for _ in range(Q)]
r = 0

for q in Que:
    if q[0] == '1':
        r += 1
    else:
        if (q[1] == '1' and r % 2 == 1) or (q[1] == '2' and r % 2 == 0):
            SS.append(q[2])
        else:
            SS.appendleft(q[2])
if r % 2 == 0:
   while len(SS) > 0:
        s = SS.popleft()
        print(s, end='')    
else:
    while len(SS) > 0:
        s = SS.pop()
        print(s, end='')
print('')

Solution (Python):
from collections import deque

s = list(input())
s = deque(s)
n = int(input())
dire = False

for i in range(n):
    q = input().split()
    if q[0] == '1':
        dire = not dire
    else:
        t = int(q[1])-1

        if dire ^ t:
            s.append(q[2])
        else:
            s.appendleft(q[2])
if dire:
    s.reverse()

print(''.join(s))

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

Solution (Python):
import collections
S = collections.deque(list(input()))
Q = int(input())
spin = False
for i in range(Q):
    temp = list(input().split())
    if temp[0] == "1":
        spin = not spin
    else:
        if spin:
            if temp[1] == "1":
                S.append(temp[2])
            else:
                S.appendleft(temp[2])
        else:
            if temp[1] == "1":
                S.appendleft(temp[2])
            else:
                S.append(temp[2])

res = ""
if spin:
    S.reverse()
    print("".join(S))
else:
    print("".join(S))

Solution (Python):
s=input()
q=int(input())
keep=1
mae=""
ato=""
for i in range(q):
  lis=list(map(str,input().split()))
  if lis[0]=="1":
    keep*=-1
  elif lis[1]=="1":
    if keep==1:
      mae=lis[2]+mae
    else:
      ato+=lis[2]
  else:
    if keep==1:
      ato+=lis[2]
    else:
      mae=lis[2]+mae

if keep==1:
  print(mae+s+ato)
else:
  print(ato[::-1]+s[::-1]+mae[::-1])

Solution (Python):



from collections import deque
s = deque(input())
q = int(input())

flg = False

for _ in range(q):
    t = tuple(map(str,input().split()))
    if t[0] == '1':
        if flg:
            flg = False
        else:
            flg = True
    else:
        if t[1] == "1" and flg == False:
            s.appendleft(t[2])
        elif t[1] == "1" and flg == True:
            s.append(t[2])
        elif t[1] == "2" and flg == False:
            s.append(t[2])
        else:
            s.appendleft(t[2])
 
    
if flg:
    s.reverse()
print("".join(s))

Solution (Python):
from  collections import deque

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



deque_S = deque(S)
re_flag = 1
for i in range(Q):
    que = str(input())

    if len(que) == 1:
        
        re_flag *= -1

    else:
        if que[2] == '1':
            if re_flag == 1:
                
                deque_S.appendleft(que[4:])
            else:
                
                deque_S.append(que[-1:3:-1])
        if que[2] == '2':
            if re_flag == 1:
                
                deque_S.append(que[4:])
            else:
                
                deque_S.appendleft(que[-1:3:-1])

ans = ''
if re_flag == 1:
    ans = ''.join(deque_S)
else:
    ans = ''.join(deque_S)[::-1]


print(ans)

Solution (Python):
import bisect,collections,copy,heapq,itertools,math,numpy,string
import sys
sys.setrecursionlimit(10**7)

def _S(): return sys.stdin.readline().rstrip()
def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def LS(): return list(sys.stdin.readline().rstrip().split())

def main():
    S = _S()
    Q = I()
    Querys = [LS() for _ in range(Q)]
    rev_flg = -1
    dq = collections.deque(S)
    for q in Querys:
        if q[0]=='1':
            rev_flg *= -1
        else:
            ti,fi,ci = q
            if int(fi)*rev_flg in (1,-2):
                dq.append(ci)
            else:
                dq.appendleft(ci)

    print("".join(dq)[::-1] if rev_flg==1 else "".join(dq))

main()

Solution (Python):
s = input()
q = int(input())
head = []
tail = []
rev=False
for _ in range(q):
  tmp = list(input().split())
  if tmp[0] == '1':
    if rev == False:
      rev = True
    else:
      rev = False
  if tmp[0] == '2':
    if tmp[1] == '1':
      if rev == False:
        head.append(tmp[2])
      if rev == True:
        tail.append(tmp[2])
    if tmp[1] == '2':
      if rev == False:
        tail.append(tmp[2])
      if rev == True:
        head.append(tmp[2])
head = ''.join(map(str,head))
tail = ''.join(map(str,tail))

ans = head[::-1]+s+tail
if rev == True:
  ans = ans[::-1]
print(ans)

Solution (Python):
s = input()
q = int(input())
t = 0
f = []
b = []
for _ in range(q):
    a = input().split()
    if len(a) == 1:
        t += 1
    else:
        m, n = int(a[1]), a[2]
        if t % 2 == 0:
            if m == 1:
                f.append(n)
            else:
                b.append(n)
        else:
            if m == 1:
                b.append(n)
            else:
                f.append(n)
if t % 2 == 0:
    x = ''
    for i in f:
        x = i + x
    x = x + s
    for i in b:
        x = x + i
    print(x)
else:
    x = ''
    for i in b:
        x = i + x
    x = x + s[::-1]
    for i in f:
        x = x + i
    print(x)

Solution (Python):
s = [ str( input() ) ]
q = int( input() )
query = []
rev_flag_k = 0
for _ in range( q ):
    query_i = list( input().split() )
    query.append( query_i )
    if query_i[ 0 ] == "1":
        rev_flag_k += 1

top = []
rev_flag = 0
for query_i in query:
    t = query_i[ 0 ]
    if t == "1":
        rev_flag += 1
    else:
        f = query_i[ 1 ]
        c = query_i[ 2 ]
        if f == "1" and rev_flag % 2 == 1:
            s.append( str( c ) )
        elif f == "2" and rev_flag % 2 == 0:
            s.append( str( c ) )
        else:
            top.append( str( c ) )
s = "".join( top )[::-1] + "".join( s )
if rev_flag_k % 2 == 1:
    s = s[::-1]

print( s )

Solution (Python):
from collections import deque
s = input()
q = int(input())
que = deque([s])
flg=False
for _ in range(q):
    a = list(input().split())
    if a[0]=='1':
        flg=not(flg)

    else:
        if flg ^ (a[1]!='1'):
            que.append(a[2])
        else:
            que.appendleft(a[2])

ans = ''.join(que)

if flg:
    ans = ans[::-1]
print(ans)

Solution (Python):
from collections import deque
s = input()
q = int(input())
que = deque([s])
flg=False
for _ in range(q):
    a = list(input().split())
    if a[0]=='1':
        flg=not(flg)

    else:
        if flg and a[1]=='1' or not flg and a[1]=='2':
            que.append(a[2])
        else:
            que.appendleft(a[2])

ans = ''.join(que)

if flg:
    ans = ans[::-1]
print(ans)

Solution (Python):
from collections import deque
s = input()
q = int(input())
que = deque([s])
flg=False
for _ in range(q):
    a = list(input().split())
    if a[0]=='1':
        flg=not(flg)

    else:
        if flg and a[1]=='1' or not flg and a[1]=='2':
            que.append(a[2])
        else:
            que.appendleft(a[2])

ans = ''.join(que)

if flg:
    ans = ans[::-1]
print(ans)

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

S = deque(input().rstrip())
Q = int(input())
lst = deque()
 
for i in range(Q):
    tmp = list(input().split())
    lst.append(tmp)

reverse = 0
c = 1
for i in range(Q):
 
    if int(lst[i][0]) == 1:
        reverse += 1
        c += 1
    
    if int(lst[i][0]) == 2:
        if c%2 == 1:
            if int(lst[i][1]) == 1:
                S.appendleft(lst[i][2])
            else:
                S.append(lst[i][2])
        else:
            if int(lst[i][1]) == 2:
                S.appendleft(lst[i][2])
            else:
                S.append(lst[i][2])
 
if reverse%2 == 1:
    S.reverse()
 
ans = "".join(S)
print(ans)

Solution (Python):

import sys
import collections

sys.setrecursionlimit(10 ** 8)
ni = lambda: int(sys.stdin.readline())
nm = lambda: map(int, sys.stdin.readline().split())
nl = lambda: list(nm())
ns = lambda: sys.stdin.readline().rstrip()

S = ns()
Q = ni()


def solve():
    ds = collections.deque(S)
    rev = False
    for i in range(Q):
        qt, *q = ns().split()
        if qt == "1":
            rev = not rev
            continue
        assert qt == "2"
        if (q[0] == "1") ^ rev:
            ds.appendleft(q[1])
        else:
            assert (q[0] == "2") ^ rev
            ds.append(q[1])
    if rev:
        ds = reversed(ds)
    return "".join(ds)


print(solve())

Solution (Python):
from collections import deque

S = input()
Q = int(input())
s = deque(list(S))
re = False
 
for i in range(Q):
    q = list(map(str,input().split()))
    if q[0] == "1":
        re = not re
    else:
        b = int(q[1])-1
        if (re and b) or not(re or b):
            s.appendleft(q[2])
        else:
            s.append(q[2])
 
ans = ""
if re:
    while s:
    	ans += s.pop()
else:
    while s:
        ans += s.popleft()
print(ans)

Solution (Python):
S = input()
Q = int(input())
from collections import deque
s = deque([i for i in S])
re = False

for i in range(Q):
    q = list(map(str,input().split()))
    if q[0] == "1":
        re = not re
    else:
        b = int(q[1])-1
        if (re and b) or not(re or b):
            s.appendleft(q[2])
        else:
            s.append(q[2])

ans = ""
if re:
    while s:
    	ans += s.pop()
else:
    while s:
        ans += s.popleft()
print(ans)

Solution (Python):
from collections import deque
d = deque([])
ans = input()
d.append(ans)
q = int(input())
rev = True 
for i in range(q):
    Query = list(map(str,input().split()))
    if Query[0] == '2':
        if Query[1] == '1':
            if rev:
                d.append(Query[2])
            else:
                d.appendleft(Query[2])
        elif Query[1] == '2':
            if rev:
                d.appendleft(Query[2])
            else:
                d.append(Query[2])
    else:
        rev = rev^True
output = ""
while d:
    output += d.pop()
if rev:
    print(output)
else:
    print(output[::-1])

Solution (Python):
from collections import deque

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

mae = deque()
usi = deque()
k = 0
m = 0
for i in range(q):
    a = input().split()
    if(a[0] == '1'):
        k += 1
    if(a[0] == '2'):
        if(k %2 == 0):
            if(a[1] == '1'):
                mae.appendleft(a[2])
            else:
                usi.append(a[2])
        else:
            if(a[1] == '1'):
                usi.append(a[2])
            else:
                mae.appendleft(a[2])
mae = list(mae)
usi = list(usi)

if(k % 2 == 1):
    mae = mae[::-1]
    usi=usi[::-1]
    s = s[::-1]
    s = ''.join(usi+s+mae)
else:
    s = ''.join(mae+s+usi)
print(s)

Solution (Python):
from collections import deque
S = deque(list(input()))
q = int(input())
r = False
for _ in range(q):
    t = input().split()
    if len(t) == 1:
        r = not r
    else:
        t[1] = int(t[1]) - 1
        if (not t[1] and not r) or (t[1] and r):
            S.appendleft(t[2])
        else:
            S.append(t[2])
if r:
    S = list(S)
    print(''.join(S[::-1]))
else:
    print(''.join(S))

Solution (Python):
from collections import deque

s = input()
m = int(input())
q_list = [list(input().split()) 
          for nesya in range(m)]
d = deque(s)
n = 0
for q in q_list:
  if q[0] == '1':
    n += 1
  else:
    if (n%2 == 0 and q[1] == '1') or (n%2 == 1 and q[1] == '2'):
      d.appendleft(q[2])
    else:
      d.append(q[2])
ans = ''.join(d)
if n%2 == 0:
  print(ans)
else:
  print(ans[::-1])

Solution (Python):
import math, string, itertools, fractions, heapq, collections, re, array, bisect, copy, functools, random
import sys
from collections import deque, defaultdict, Counter; from heapq import heappush, heappop
from itertools import permutations, combinations, product, accumulate, groupby
from bisect import bisect_left, bisect_right, insort_left, insort_right
from operator import itemgetter as ig
sys.setrecursionlimit(10 ** 7)
inf = 10 ** 20; INF = float("INF"); ans = 0; tmp = 0; cnt = 0; ansli = []; tmpli = []; candili = []; stillset = set()
eps = 1.0 / 10 ** 10; mod = 10 ** 9 + 7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]; ddn = dd + [(-1, 1), (1, 1), (1, -1), (-1, -1)]; ddn9 = ddn + [(0, 0)]
"""for dx, dy in dd:
        nx = j + dx; ny = i + dy
            if 0 <= nx < w and 0 <= ny < h:"""
def wi(): return list(map(int, sys.stdin.readline().split()))
def wip(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def ws(): return sys.stdin.readline().split()
def i(): return int(sys.stdin.readline())
def s(): return input()
def s_list(): return list(input())
def hi(n): return [i() for _ in range(n)]
def hs(n): return [s() for _ in range(n)]
def mi(n): return [wi() for _ in range(n)]
def num__grid(n): return [[int(i) for i in sys.stdin.readline().split()[0]] for _ in range(n)]
def mip(n): return [wip() for _ in range(n)]
def ms(n): return [ws() for _ in range(n)]
def grid(n): return [s_list() for _ in range(n)]


s = list(s())
s_deque = deque(s)
q = i()
query = ms(q)

cnt = 0
v = ["r", "l"]
vector = v[cnt]

for qu in query:
    if qu[0] == "1":
        cnt += 1
        vector = v[cnt % 2]
    else:
        if qu[1] == "1":
            if vector == "r":
                s_deque.appendleft(qu[2])
            else:
                s_deque.append(qu[2])
        elif qu[1] == "2":
            if vector == "r":
                s_deque.append(qu[2])
            else:
                s_deque.appendleft(qu[2])

print(''.join(s_deque)[::(-1) ** (cnt % 2)])