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=str(input())
q=int(input())
rev=1
rg=[]
lf=[]
for i in range(q):
  l=list(map(str,input().split()))
  if l[0]=='1':
    rev*=-1
  else:
    if l[1]=='1' and rev==1:
      lf.append(l[2])
    elif l[1]=='1':
      rg.append(l[2])
    if l[1]=='2' and rev==1:
      rg.append(l[2])
    elif l[1]=='2':
      lf.append(l[2])
res=[]
if rev==1:
  lf.reverse()
  res=lf+[s]+rg
else:
  rg.reverse()
  ns=list(reversed(s))
  res=rg+ns+lf
print(''.join(res))

Solution (Python):
s = input()
q = int(input())
swc = 0
top = ""
bot = ""
for i in range(q):
	a = list(map(str, input().split()))
	if a[0] == "1":
		if swc == 0:
			swc = 1
		else:
			swc = 0
	else:
		if swc == 0:
			if a[1] == "1":
				top += a[2]
			else:
				bot += a[2]
		else:
			if a[1] == "1":
				bot += a[2]
			else:
				top += a[2]
if swc == 0:
	top = ''.join(list(reversed(top)))
	print (top + s + bot)
else:
	bot = ''.join(list(reversed(bot)))
	s = ''.join(list(reversed(s)))
	print (bot + s + top)

Solution (Python):
from collections import deque
s = deque(input())
Q = int(input())
forward = 0
for _ in range(Q):
    q = input()
    if q=='1':
        forward ^= 1
    else:
        _, f, c = q.split()
        if (int(f)-1)^forward==0:
            s.appendleft(c)
        else:
            s.append(c)
s = list(s)
if forward:
    for i in reversed(s):
        print(i, end='')
else:
    for i in s:
        print(i, end='')
print()

Solution (Python):

def main():
  S = input()
  Q = int(input())
  Queries = [input().split() for i in range(Q)]
  
  is_reversed = False
  prefix = ''
  suffix = ''
  for Query in Queries:
    if Query[0] == '1':
      is_reversed = not is_reversed
    else:
      if (Query[1] == '1') ^ is_reversed:
        prefix = Query[2] + prefix
      else:
        suffix += Query[2]
  ans = prefix + S + suffix
  if not is_reversed:
    print(ans)
  else:
    print(''.join(list(reversed(ans))))

main()

Solution (Python):
from collections import deque
S = str(input())
q = int(input())
que =deque()
order = 1
for i in range (len(S)):
  que.append(S[i])
for j in range(q):
  Q = list(map(str, input().split()))
  T = Q[0]
  if T == "1":
        order *= -1
  elif T == "2":
    F = Q[1]
    C = Q[2]
    if F == "1":
      if order == 1:
        que.appendleft(C)
      elif order == -1:
        que.append(C)
    elif F == "2":
      if order == 1:
        que.append(C)
      elif order == -1:
        que.appendleft(C)
if order == 1:
  que_string = "".join(list(que))
  print(que_string)
else:
  que.reverse()
  que_string = "".join(list(que))
  print(que_string)

Solution (Python):
from collections import deque
s = input()
q = int(input())
qu = [input().split() for _ in range(q)]
le = deque()
ri = deque()
for i in range(len(s)):
    le.append(s[i])
    ri.appendleft(s[i])
x = 1
for i in range(q):
    if qu[i] == ['1']:
        x *= -1
    elif x == 1:
        ci = qu[i][2]
        if qu[i][1] == '1':
            le.appendleft(ci)
            ri.append(ci)
        else: 
            le.append(ci)
            ri.appendleft(ci)
    else: 
        ci = qu[i][2]
        if qu[i][1] == '1':
            le.append(ci)
            ri.appendleft(ci)
        else: 
            le.appendleft(ci)
            ri.append(ci)
y = ''
if x == 1:
    for i in le:
        y += i
else: 
    for i in ri:
        y += i
print(y)

Solution (Python):
from collections import deque

left = deque()
right = deque()

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

reverse = False
for i in range(q):
    t_inp = input().split()
    if t_inp[0] == "1":
        reverse = not reverse
    else:
        if t_inp[1] == "1" and reverse:
            right.append(t_inp[2])
        if t_inp[1] == "1" and not reverse:
            left.appendleft(t_inp[2])
        if t_inp[1] == "2" and reverse:
            left.appendleft(t_inp[2])
        if t_inp[1] == "2" and not reverse:
            right.append(t_inp[2])

l_li = list(left)
r_li = list(right)



ans = "".join(l_li)+s+"".join(r_li)

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

Solution (Python):


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


c = 0
l = ''
r = ''

for _ in range(Q):
    q = [str(x) for x in input().split()]
    if q[0] == '1':
        c += 1
        l, r = r, l
    elif q[1]=='1':
        l = l + q[2]
    else:
        r = r + q[2]

if c%2==1:
    S = ''.join(reversed(S))

l = ''.join(reversed(l))

print(l+S+r)

Solution (Python):
from collections import deque

if __name__ == "__main__":
    s = input()
    ans = deque()
    for ss in s:
        ans.append(ss)
    q = int(input())
    revflag = False

    for i in range(q):
        tmp = input().split()
        if tmp[0] == '1':
            if revflag:
                revflag = False
            else:
                revflag = True
        else:
            if tmp[1] == '1' and revflag:
                ans.append(tmp[2])
            elif tmp[1] == '1' and not revflag:
                ans.appendleft(tmp[2])
            elif tmp[1] == '2' and revflag:
                ans.appendleft(tmp[2])
            else:
                ans.append(tmp[2])

    if revflag:
        print(''.join(reversed(ans)))
    else:
        print(''.join(ans))

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

rev_flag = False

heads = []
tails = []

for i in range(Q):
  in_ = input()
  if len(in_) > 1:
    _, f, c = in_.split()
    f = int(f)
    
    if rev_flag:
      if f==1:
        
        tails.append(c)
      elif f==2:
        
        heads.append(c)
    else:
      if f==2:
        
        tails.append(c)
      elif f==1:
        
        heads.append(c)
    
  else:
    
    rev_flag = not rev_flag

if rev_flag:
  ans = "".join(tails[-1::-1]) + S[-1::-1] + "".join(heads)
else:
  ans = "".join(heads[-1::-1]) + S + "".join(tails)

print(ans)

Solution (Python):
import sys
s=sys.stdin.readline().strip()

q=int(input())

p=1
front=''
back=''

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

    if len(slst)==1:
        p*=-1
    else:
        t,f,c=slst

        f=int(f)

        if (f*2-3)*p==1:
            back=back+c
        else:
            front=c+front

s=front+s+back

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

print(s)

Solution (Python):
from collections import deque

s = deque(input())
q = int(input())
is_reversed = False
for i in range(q):
    query = input().split()
    if query[0] == '1':
        is_reversed = not is_reversed
    elif (query[1] == '2') ^ is_reversed:
        s.append(query[2])
    else:
        s.appendleft(query[2])
if is_reversed:
    s.reverse()
print(''.join(s))

Solution (Python):
import sys
input = sys.stdin.buffer.readline
S = input()
Q = int(input())


pre = ""
suf = ""

rev = 0
for i in range(Q):
    q = input().split()
    '''
    if i % 3 == 0:
        q = ('1')
    if i % 3 == 1:
        q = (2,'1','b')
    if i % 3 == 2:
        q = (2,'2','b')
        '''

    if len(q) == 1:
        rev = (rev + 1) % 2
    else:
        _, f, c = q
        f = f.decode()
        c = c.decode()
        if f == '1':
            if rev == 0:
                pre += c
            else:
                suf += c
        else:
            if rev == 0:
                suf += c
            else:
                pre += c

S = S.decode()[:-1]
ans = pre[::-1] + S + suf
if rev == 0:
    print(ans)
else:
    print(ans[::-1])

Solution (Python):
from collections import deque

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

d = 1
c = 1
for i in range(Q):
    que = list(input().split())
    if que[0] == str(1):
        d += c
        c *= -1
    else:
        if (int(que[1]) ^ d) == 0:
            S.appendleft(que[2])
        else:
            S.append(que[2])

print(''.join(S) if d == 1 else ''.join(S)[::-1])

Solution (Python):
s = input()
q = int(input())
x = 1
f, b = '', ''
for i in range(q):
  a = input().split()
  if a[0] == '1':
    x = -x
  elif (1.5-int(a[1]))*x > 0:
    f = a[2] + f
  else:
    b = b + a[2]
s = f + s + b
if x == -1:
  s = s[::-1]
print(s)

Solution (Python):
s=input()
q=int(input())
hanten=0
s_1 = ""
s_2 = ""

for i in range(q):
    s1=input()
    if len(s1)==1:
        hanten+=1
    else:
        a,f,c=s1.split()
        f=int(f)
        if (f+hanten)%2==0:
            s_2 += c
        else:
            s_1 = c + s_1     

if hanten%2==0:
    print(s_1 + s + s_2)
else:
    s_3 = s_1 + s + s_2
    print(s_3[::-1])

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

rvs = False
front = []
back = []
for i in range(q):
    op = input().split()
    if op[0] == "1":
        rvs = rvs ^ 1
    else:
        if op[1] == "1":
            if rvs:
                back.append(op[2])
            else:
                front.append(op[2])
        else:
            if rvs:
                front.append(op[2])
            else:
                back.append(op[2])

if rvs:
    for c in reversed(back):
        print(c, end='')
    for c in reversed(s):
        print(c, end='')
    for c in front:
        print(c, end='')
else:
    for c in reversed(front):
        print(c, end='')
    print(s, end='')
    for c in back:
        print(c, end='')

print()

Solution (Python):


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


heads = {}
tails = {}
h = 0
t = 0
rev = False
for _ in range(Q):
    line = input()

    if line[0] == "1":
        rev = not rev

    else:
        if line[2] == "1":
            if rev:
                
                tails[t] = line[4]
                t += 1
            else:
                
                heads[h] = line[4]
                h += 1
        else:
            if rev:
                
                heads[h] = line[4]
                h += 1
            else:
                
                tails[t] = line[4]
                t += 1

head = [None] * h
tail = [None] * t

for i in range(h):
    head[i] = heads[i]
head = head[::-1]

for i in range(t):
    tail[i] = tails[i]

head_joint = "".join(head)
tail_joint = "".join(tail)
joint = "".join([head_joint, S, tail_joint])

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

Solution (Python):
S = input()
Q = int(input())
QL = [input().split() for _ in range(Q)]

h = ''
t = ''
reverse = False

for q in QL:
    if q[0] == '1':
        reverse = not reverse
    else:
        if q[1] == '1':
            if reverse:
                t = '{}{}'.format(t, q[2])
            else:
                h = '{}{}'.format(q[2], h)
        else:
            if reverse:
                h = '{}{}'.format(q[2], h)
            else:
                t = '{}{}'.format(t, q[2])

s = '{}{}{}'.format(h, S, t)
if reverse:
    print(s[::-1])
else:
    print(s)

Solution (Python):
from collections import deque
s=deque(list(input()))
q=int(input())
r = False
for i in range(q):
    tmp = list(input().split(' '))
    if tmp[0]=='1':
        r = not r
    elif (not r and tmp[1]=='1') or (r and tmp[1] =='2'):
        s.appendleft(tmp[2])
    else:
        s.append(tmp[2])
if not r:
    for i in s:
        print(i,end='')
else:
    for i in reversed(s):
        print(i,end='')

Solution (Python):



def main():
    s=list(input())
    r=s[:]
    r.reverse()
    ss,rr=[],[]
    n=int(input())
    flg=0
    for i in range(n):
        q=list(map(str,input().split()))
        if q[0]=='1':
            flg=(flg+1)%2
        else:
            f,c=q[1],q[2]
            if f=='1':
                if flg==0:
                    rr.append(c)
                else:
                    ss.append(c)
            else:
                if flg==0:
                    ss.append(c)
                else:
                    rr.append(c)
    if flg==1:
        ss.reverse()
        ans=''.join(ss+r+rr)
        print(ans)
    else:
        rr.reverse()
        ans=''.join(rr+s+ss)
        print(ans)
if __name__ == '__main__':
    main()

Solution (Python):
s = str(input())
q = int(input())
now = True
before = str()
after = str()

for i in range(q):
    que = list(map(str,input().split()))
    if que[0] == "1":
        now = not now
    else:
        if que[1] == "1":
            if now:
                before += (que[2])
            else:
                after += (que[2])
        else:
            if not now:
                before += (que[2])
            else:
                after += (que[2])

ans = (before[::-1] + s + after)
if now:
    print(ans)
else:
    print(ans[::-1])

Solution (Python):
from collections import deque

if __name__ == '__main__':
    s = input()
    q = int(input())
    hanten_flag = 0

    d_mae = deque()
    d_ushiro = deque()

    for i in range(q):
        query = input().split(" ")
        if query[0] == "1":
            hanten_flag = (hanten_flag + 1) % 2
        else:
            if query[1] == "1":
                if hanten_flag == 0:
                    d_mae.appendleft(query[2])
                else:
                    d_ushiro.append(query[2])
            else:
                if hanten_flag == 0:
                    d_ushiro.append(query[2])
                else:
                    d_mae.appendleft(query[2])

    if hanten_flag == 0:
        print("".join(list(d_mae)) + s + "".join(list(d_ushiro)))

    else:
        print("".join(reversed(list(d_ushiro))) + s[::-1] + "".join(reversed(list(d_mae))))

Solution (Python):
import sys
input = sys.stdin.readline
from collections import deque
def main():
    S = input().strip()
    Q = int( input())
    Query = [ input().strip() for _ in range(Q)]
    now = 0
    ANS = deque(['1'])
    t = 0
    for q in Query:
        if q == '1':
            if now == 0:
                now = 1
            else:
                now = 0
            continue
        t += 1
        _, f, c = q.split()
        if f == '1':
            if now == 0:
                ANS.appendleft(c)
            else:
                ANS.append(c)
        else:
            if now == 0:
                ANS.append(c)
            else:
                ANS.appendleft(c)
    for i in range(t+1):
        if ANS[i] == '1':
            if now == 0:
                ANS[i] = S
            else:
                ANS[i] = S[::-1]
    if now == 1:
        ANS.reverse()
    print("".join(ANS))
    
if __name__ == '__main__':
    main()

Solution (Python):
from collections import deque

S = deque(list(input()))
Q = int(input())
l = []
for i in range(Q):
  l.append(list(input().split()))
    
flag = 1 


for i in range(Q):
    if l[i][0] == "1":
        flag *= -1
    elif flag == 1:
        if l[i][1] == "1":
            S.appendleft(l[i][2])
            
        else:
            S.append(l[i][2])
            
    else:
        if l[i][1] == "2":
            S.appendleft(l[i][2])
            
        else:
            S.append(l[i][2])


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

Solution (Python):
import sys
stdin = sys.stdin
sys.setrecursionlimit(10**9)
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
nn = lambda: list(stdin.readline().split())
ns = lambda: stdin.readline().rstrip()

s = ns()
q = ni()
h = 0

from collections import deque
ss=deque(list(s))

for i in range(q):
  que = nn()
  if int(que[0]) == 1:
    h += 1
  else:
    b = int(que[1])
    c = que[2]
    if (h+b)%2 == 0:
      ss.append(c)
    else:
      ss.appendleft(c)

ss = list(ss)

if h%2 == 1:
  ss = reversed(ss)

print(''.join(ss))

Solution (Python):
import math,itertools,fractions,heapq,collections,bisect,sys,queue,copy

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7
dd=[(-1,0),(0,1),(1,0),(0,-1)]
ddn=[(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return input()

def main():
  s=list(S())
  s=collections.deque(s)
  f=True
  q=I()

  for _ in range(q):
    l=LS()
    if l==['1']:
      f=not f
    else:
      if l[1]=='1':
        if f:
          s.appendleft(l[2])
        else:
          s.append(l[2])
      else:
        if f:
          s.append(l[2])
        else:
          s.appendleft(l[2])

  s=list(s)

  if f:
    return ''.join(s)
  s.reverse()
  return ''.join(s)


print(main())

Solution (Python):
from collections import deque

s=input()

d=deque()
for i in s: 
  d.append(i)
  
v=1

q=int(input())

for i in range(q): 
  a=input()
  if len(a)==1:
    if v:
      v=0
    else:
      v=1
  else:
    b,f,c=a.split()
    if f=="1":
      if v:
        d.appendleft(c)
      else:
        d.append(c)
    else:
      if v:
        d.append(c)
      else:
        d.appendleft(c)
        
if v:
  for i in range(len(d)):
    print(d[i],end="")
else:
  for i in range(len(d)):
    print(d[len(d)-1-i],end="")
    
print()

Solution (Python):
S = list(input())
Q = int(input())
left, right = [], []
flag = 0
for q in range(Q):
    query = input().split()
    if query[0] == '1':
        flag = (flag + 1) % 2
        left, right = right, left
    else:
        F, C = query[1], query[2]
        if F == '1':
            left.append(C)
        else:
            right.append(C)
if flag == 1:
    S.reverse()
left.reverse()
left = ''.join(left)
right = ''.join(right)
S = ''.join(S)
print(left + S + right)

Solution (Python):
import sys

def read():
    return sys.stdin.readline().strip()

def main():
    s = read()
    pre = ""
    sur = ""
    rev = False
    for _ in range(int(read())):
        q = read()
        if q == "1":
            rev = not rev
            continue
        _, f, c = q.split()
        if rev ^ (f == "1"):
            pre += c
        else:
            sur += c
    print((pre[::-1] + s + sur)[::1 - 2 * rev])

if __name__ == '__main__':
    main()

Solution (Python):
s = list(input())
q = int(input())
c = 0
s1 = []
s2 = []
for _ in range(q):
    t = input()
    if t != "1":
        t = t.split()
        if t[1] == "1":
            if c % 2 == 0:
                s1.append(t[2])
            else:
                s2.append(t[2])
        else:
            if c % 2 == 0:
                s2.append(t[2])
            else:
                s1.append(t[2])
    else:
        c += 1

if c % 2 == 0:
    s1.reverse()
    s = s1 + s + s2
else:
    s2.reverse()
    s3 = s
    s3.reverse()
    s = s2 + s3 + s1
print("".join(s))

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

inversion = 0
head = []
tail = []

for _ in range(Q):
    queri = input()
    if len(queri) == 1:
        inversion += 1
    else:
        _, f, c = queri.split()
        if inversion % 2 == 0 and f == "1":
            head.append(c)
        elif inversion % 2 == 1 and f == "1":
            tail.append(c)
        elif inversion % 2 == 0 and f == "2":
            tail.append(c)
        else:
            head.append(c)



if inversion % 2 == 0:
    head.reverse()
    Head = ''.join(head)
    Tail = ''.join(tail)
    print(Head + S + Tail)
else:
    tail.reverse()
    S = S[::-1]
    Head = ''.join(head)
    Tail = ''.join(tail)
    print(Tail + S + Head)

Solution (Python):
S=input()
Q=int(input())
q=[input().split() for _ in range(Q)]

count=0
f=""
b=""

for i in range(Q):
    if q[i][0]=="1":
        count+=1
    else:
        if q[i][1]=="1":
            if count%2==0:
                f=q[i][2]+f
            else:
                b+=q[i][2]
        elif q[i][1]=="2":
            if count%2==1:
                f=q[i][2]+f
            else:
                b+=q[i][2]

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

Solution (Python):
from collections import deque

s = deque(input())

q = int(input())

check = 1
for _ in range(q):
    t = list(input().split())
    if t[0] == '1':
        check = 1 - check
    else:
        if t[1] == '1':
            if check == 1:
                s.appendleft(t[2])
            else:
                s.append(t[2])
        else:
            if check == 1:
                s.append(t[2])
            else:
                s.appendleft(t[2])



if check == 1:
    print(''.join(s))
else:
    s = list(reversed(s))
    print(''.join(s))

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

x=0
lf=[]
lb=[]

for i in range(q):
    if l[i][0]=="1":
        if x==0:
            x+=1
        else:
            x=0
    else:
        if x==0:
            if l[i][1]=="1":
                lf.append(l[i][2])
            else:
                lb.append(l[i][2])
        else:
            if l[i][1]=="1":
                lb.append(l[i][2])
            else:
                lf.append(l[i][2])


if x==0:
    for i in range(len(lf)):
        print(lf[len(lf)-i-1],end="")
    for i in range(len(s)):
        print(s[i],end="")
    for i in range(len(lb)):
        print(lb[i],end="")
else:
    for i in range(len(lb)):
        print(lb[len(lb)-i-1],end="")
    for i in range(len(s)):
        print(s[len(s)-i-1],end="")
    for i in range(len(lf)):
        print(lf[i],end="")

Solution (Python):
s=input()
qq=int(input())
re=0
l=''
r=''
f=1
for _ in range(qq):
  q=list(input().split())
  if len(q)==1:
    re+=1
    if f:
      f=0
    else:
      f=1
  else:
    t=q[2]
    if f:
      if q[1]=='1':
        l+=t
      else:
        r+=t
    else:
      if q[1]=='1':
        r+=t
      else:
        l+=t
if re%2==0:
  print(l[::-1]+s+r)
else:
  print(r[::-1]+s[::-1]+l)

Solution (Python):
from collections import deque

s = input()
que = deque(s)

T_F = True

for i in range(int(input())):
  query = input()
  if query[0] == '1':
    T_F = not T_F
  elif (query[2] == '1' and T_F) or (not query[2] == '1' and not T_F):
    que.appendleft(query[4])
  else:
    que.append(query[4])
if T_F:
  print(''.join(que))
else :
  print(''.join(reversed(que)))

Solution (Python):
S = input()
Q = int(input())
A_1 = "" 
A_2 = "" 
k = 1 
for q in range(Q):
  x_q = input()
  if x_q == "1":
    k *= -1
  else:
    [a,b,c] = x_q.rstrip().split(" ")
    if b == "1": 
      if k == 1:
        A_1 = c + A_1
      else:
        A_2 = A_2 + c
    else: 
      if k == 1:
        A_2 = A_2 + c
      else:
        A_1 = c + A_1
if k == 1:
  print(A_1 + S + A_2)
else:  
  print((A_1 + S + A_2)[::-1])

Solution (Python):







import math

import sys



ipti = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)

def main():
    s = input()
    q = int(input())

    add_head = []
    add_tail = []
    is_reversed = False

    for _ in range(q):
        q = list(map(str,input().split()))
        if q[0] == "1":
            if is_reversed:
                is_reversed = False
            else:
                is_reversed = True
        else:
            if q[1] == "1":
                if is_reversed:
                    add_tail.append(q[2])
                else:
                    add_head.append(q[2])
            else:
                if is_reversed:
                    add_head.append(q[2])
                else:
                    add_tail.append(q[2])

    ans = []

    if is_reversed:
        add_head.reverse()
        ans += add_head
        ans += s
        ans += add_tail
        ans.reverse()
    else:
        add_head.reverse()
        ans += add_head
        ans += s
        ans += add_tail

    print(''.join(ans))


if __name__ == '__main__':
    main()

Solution (Python):
from collections import deque
deq = deque(input())
rev = False
for _ in range(int(input())):
    query = input().split()
    if query[0] == '1':
        rev = not rev
    else:
        if (query[1] == '1') ^ rev:
            deq.appendleft(query[2])
        else:
            deq.append(query[2])
if rev:
    deq.reverse()
print(''.join(deq))

Solution (Python):
from collections import deque

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

que = deque(s)
rev = 1
for qi in range(q):
    tmp = input()
    if tmp[0] == '1':
        rev *= -1
    if tmp[0] == '2':
        _, f, c = tmp.split()
        fi = 2 * (int(f) - 1) - 1
        if fi * rev == 1:
            que.append(c)
        else:
            que.appendleft(c)
lque = list(que)

if rev == 1:
    print(''.join(map(str, lque)))
    exit()

ans = ''
for i in range(len(lque)):
    ans += lque.pop()
print(ans)

Solution (Python):



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

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

preffix = ""
suffix = ""
revcnt = 0
for i in range(Q):
    query = input()
    if query[0] == "1":
        wk = preffix
        preffix = suffix
        suffix = wk
        revcnt += 1
    elif query[2] == "1":
        if revcnt %2 ==0:
            preffix = (query[4]+preffix)
        else:
            preffix += query[4]
    else:
        if revcnt %2 ==1:
            suffix = (query[4]+suffix)
        else:
            suffix += query[4]

if revcnt%2 == 1:
    S = S[::-1]
    suffix = suffix[::-1]
    preffix = preffix[::-1]


print(preffix+S+suffix)

Solution (Python):
from collections import deque

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

ans = deque(S)
r = 0  
for i in range(Q):
    q, *x = X[i].split()
    if q == "1":
        r = abs(r - 1)
    else:
        f, c = x
        if (r == 0 and f == "1") or (r == 1 and f == "2"):
            ans.appendleft(c)
        else:
            ans.append(c)
            
print("".join(ans) if r == 0 else "".join(ans)[::-1])

Solution (Python):
S = input()
N = len(S)
Q = int(input())
sign = 1
L = ["" for i in range(N + 2 * Q)]
for i in range(N):
  L[i+Q] = S[i]
x, y = Q - 1, N + Q
for i in range(Q):
  T = list(map(str, input().split()))
  if T[0] == "1":
    sign *= -1
  else:
    F = int(T[1]) * 2 - 3
    if sign * F == 1:
      L[y] = T[2]
      y += 1
    else:
      L[x] = T[2]
      x -= 1
newS = ""
for i in range(N+2*Q):
  newS = newS[:] + L[i]
if sign == 1:
  print(newS)
else:
  M = len(newS)
  for i in range(M):
    print(newS[M-i-1], end = '')
  print()

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

    rule = 0
    pre, suf = '', ''
    for _ in range(q):
        query = input()
        if query[0] == '1':
            rule += 1
        else:
            if rule % 2 == 0:
                if query[2] == '1':
                    pre = query[4] + pre
                else:
                    suf += query[4]
            else:
                if query[2] == '1':
                    suf += query[4]
                else:
                    pre = query[4] + pre

    s = pre + s + suf
    print(s if rule % 2 == 0 else s[::-1])


if __name__ == '__main__':
    main()

Solution (Python):
s = list(input())
q = int(input())
front = []
end = []
flip = False
for i in range(q):
    x = input()
    if x == "1":
        flip = flip ^ True
    else:
        z,f,c = x.split()
        f = bool(int(f)-1)
        if flip ^ f:
            end.append(c)
        else:
            front.append(c)
if flip:
    s = s[::-1]
    ans = end[::-1] + s + front
else:
    ans = front[::-1] + s + end
print("".join(ans))

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

front = ''
back = ''

T_count = 0

for i in range(Q):
    command = input().split()
    if command[0] == '1':
        T_count = (T_count+1)%2
    else:
        if (int(command[1]) + T_count)%2 == 1:
            front += command[2]
        else:
            back += command[2]

if T_count == 0:
    print(front[::-1]+S+back)
else:
    print(back[::-1]+S[::-1]+front)

Solution (Python):
from collections import deque

def main():
    s = list(input())
    Q = int(input())
    Qry = []
    for i in range(Q):
        q = list(input().split())
        Qry.append(q)
    d = deque(s)

    flg = 0
    l = 0
    r = len(s) -1
    for i, q in enumerate(Qry):
        if int(q[0]) == 1:
            tmp = l
            l = r
            r = tmp
            if flg == 0:
                flg =1
            else:
                flg = 0
        if int(q[0]) == 2:
            if int(q[1]) == 1:
                if flg == 0:
                    d.appendleft(q[2])
                else:
                    d.append(q[2])
            if int(q[1]) == 2:
                if flg ==1:
                    d.appendleft(q[2])
                else:
                    d.append(q[2])
    ans = []
    if flg == 0:
        for _ in range(len(d)):
            ans.append(d.popleft())
    if flg == 1:
        for _ in range(len(d)):
            ans.append(d.pop())
    print(''.join(ans))

if __name__=='__main__':
    main()

Solution (Python):
s = input()
q = int(input())
query = [input() for i in range(q)]
rvs = 1
s1 = ""
for i in range(q):
    if query[i] == "1":
        rvs = rvs * (-1)
    else:
        if query[i][2:3] == "1":
            if rvs == 1:
   
                s1 = s1 + query[i][4:5]
            else:
                s = s + query[i][4:5]
        else:
            if rvs == -1:
    
                s1 = s1 + query[i][4:5]
            else:
                s = s + query[i][4:5]
if rvs == -1:
    s = s[::-1] + s1
else:
    s = s1[::-1] + s

print(s)

Solution (Python):
from collections import deque
S = list(map(str, input()))
Q = int(input())

ans_lst = deque(S)
rev = False
for i in range(Q):
  QUERY = list(map(str, input().split()))
  if QUERY[0] == '1':
    if rev:
      rev = False
    else:
      rev = True
  else:
    if rev:
      if QUERY[1] == '1':
        ans_lst.append(QUERY[2])
      else:
        ans_lst.appendleft(QUERY[2])
    else:
      if QUERY[1] == '1':
        ans_lst.appendleft(QUERY[2])
      else:
        ans_lst.append(QUERY[2])    

if rev:
  ans_lst.reverse()
  print(''.join(ans_lst))
else:
  print(''.join(ans_lst))

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

head = []
tail = []

mode = 0
for i in range(q):
  read = list(map(str,input().split()))
  if read[0]=='1':
    mode = (mode+1)%2
  else:
    if (read[1]=='1' and mode == 0) or (read[1]=='2' and mode == 1):
      head.append(read[2])
    else:
      tail.append(read[2])

s = head[::-1] + s
s = s + tail
if mode == 0:
  print(''.join(s))
else:
  print(''.join(s[::-1]))