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())
inv = False
head = ''
tail = ''
 
for i in range(q):
    a = input()
    if a == '1':
        inv = not inv
        continue
    a = a.split()
    if (a[1] == '1')^inv:
        head += a[2]
    else:
        tail += a[2]

if inv:
    print(tail[::-1]+s[::-1] + head)
else:
    print(head[::-1]+s+tail)

Solution (Python):
s = str(input())
q = int(input())
inv = False
head = ''
tail = ''

for i in range(q):
    a = input()
    if a == '1':
        inv = not inv
        continue
    a = a.split()
    if (a[1] == '1')^inv:
        head = a[2] + head
    else:
        tail += a[2]
s = head + s + tail
if inv:
    print(s[::-1])
else:
    print(s)

Solution (Python):
def main():
    s = input()
    q = int(input())
    r = 1
    front = ""
    back = ""
    for _ in range(q):
        n, *fc = input().split()
        if n == "1":
            r *= -1
        else:
            f, c = fc
            f = 1 if f == "1" else -1
            if f*r == 1:
                front += c
            else:
                back += c
    if r == 1:
        print(front[::-1]+s+back)
    else:
        print(back[::-1]+s[::-1]+front)

if __name__ == "__main__":
    main()

Solution (Python):
from collections import deque
def main():
    s = deque(list(input()))
    q = int(input())
    r = 1
    for _ in range(q):
        n, *fc = input().split()
        if n == "1":
            r *= -1
        else:
            f, c = fc
            f = 1 if f == "1" else -1
            if f*r == 1:
                s.appendleft(c)
            else:
                s.append(c)
    print("".join(list(s)[::r]))

if __name__ == "__main__":
    main()

Solution (Python):
from collections import deque
def main():
    s = deque(list(input()))
    q = int(input())
    r = 1
    for _ in range(q):
        n, *fc = input().split()
        if n == "1":
            r *= -1
        else:
            f, c = fc
            f = 1 if f == "1" else -1
            if f*r == 1:
                s.appendleft(c)
            else:
                s.append(c)
    print("".join(list(s)[::r]))

if __name__ == "__main__":
    main()

Solution (Python):
import math

s = input()
i = int(input())

isR = False
head = []
tail = []

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

    if q[0] == "1":
        isR = not isR
    else:
        if isR:
            if q[1] == "1":
                tail.append(q[2])
            else:
                head.append(q[2])
        else:
            if q[1] == "1":
                head.append(q[2])
            else:
                tail.append(q[2])

ans = "".join(head[::-1]) + s + "".join(tail)

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

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

flag = True
for i in range(q):
    que = li[i]
    if que[0] == '1':
        if flag:
            flag = False
        else:
            flag = True
    else:
        f = que[1]
        c = que[2]
        if flag:
            if f == '1':
                s.appendleft(c)
            else:
                s.append(c)
        else:
            if f == '1':
                s.append(c)
            else:
                s.appendleft(c)

if flag:
    print("".join(s))
else:
    s.reverse()
    print("".join(s))

Solution (Python):


from collections import defaultdict

con = 10 ** 9 + 7

def getlist():
	return list(map(int, input().split()))


def main():
	S = input()
	Q = int(input())
	cnt = 0
	s_start = ""
	s_end = ""
	for i in range(Q):
		q = list(input().split())
		if q[0] == "1":
			cnt += 1
		else:
			if q[1] == "1":
				if cnt % 2 == 0:
					s_start += q[2]
				else:
					s_end += q[2]
			else:
				if cnt % 2 == 0:
					s_end += q[2]
				else:
					s_start += q[2]

	if cnt % 2 == 0:
		ans = s_start[::-1] + S + s_end

	else:
		ans = s_end[::-1] + S[::-1] + s_start

	print(ans)

if __name__ == '__main__':
	main()

Solution (Python):
from collections import deque

s = input()
n = int(input())
a = deque()
b = deque()
reverse_flag = False

for i in range(n):
    command = input()
    
    if len(command) == 1:
        reverse_flag = not reverse_flag
    
    else:
        _, t, f = command.split(' ')
        if t == '1':
            if reverse_flag:
                b.append(f)
            else:
                a.appendleft(f)
        else:
            if reverse_flag:
                
                a.appendleft(f)
            else:
                b.append(f)

a = list(a)
b = list(b)
if reverse_flag:
    print(''.join(b[::-1]) + s[::-1] + ''.join(a[::-1]))
else:
    print(''.join(a) + s + ''.join(b))

Solution (Python):
from collections import deque

s = deque(input())
q = int(input())
first = 1

for i in range(q):
    t = input().split()
    if int(t[0]) == 1:
        first *= -1
    else:
        f = int(t[1])
        c = t[2]
        
        if first == 1:
            if f == 1:
                s.appendleft(c)
            elif f == 2:
                s.append(c)
        else:
            if f == 1:
                s.append(c)
            elif f == 2:
                s.appendleft(c)

S = "".join(s)
if first == 1:
    print(S)
else:
    print(S[::-1])

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

rev_flag = False
head = []
tail = []
for i in range(num_query):
    query = input().split()
    if len(query) == 1:
        if rev_flag == True:
            rev_flag = False
        else:
            rev_flag = True
    else:
        if rev_flag == False:
            if query[1] == "1":
                head.append(query[2])
            else:
                tail.append(query[2])
        else:
            if query[1] == "1":
                tail.append(query[2])
            else:
                head.append(query[2])

head = "".join(head)
head = head[::-1]
tail = "".join(tail)
ans = head + S + tail

if rev_flag == True:
    ans = ans[::-1]

print(ans)

Solution (Python):
from collections import deque
S=deque(input())
Q=int(input())
T=[]
m=0
for i in range(Q):
  T.append(input().split())
for i in range(Q):
  if T[i][0]=="1":
    if len(S)>1:
      if m==0:
        m=1
      else:
        m=0
        
  else:
    if T[i][1]=="1":
      if m==0:
        S.appendleft(T[i][2])
      else:
        S.append(T[i][2])
   
    else:
      if m==0:
        S.append(T[i][2])
      else:
        S.appendleft(T[i][2])
if m==0:
  print("".join(S))
else:
  S.reverse()
  print("".join(S))

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

cnt = 0
llst = []
rlst = []
for i in range(q):
  line = input()
  if line == '1':
    cnt += 1
  else:
    elems = line.split()
    if cnt % 2 == 0:
      
      if elems[1] == '1':
        llst.append(elems[2])
      else:
        rlst.append(elems[2])
    else:
      
      if elems[1] == '1':
        rlst.append(elems[2])
      else:
        llst.append(elems[2])
        
if cnt % 2 == 0:
  res = ''.join(llst[::-1]) + s + ''.join(rlst)
  print(res)
else:
  res = ''.join(llst[::-1]) + s + ''.join(rlst)
  print(res[::-1])

Solution (Python):

S=input()
Q=int(input())
head=[]
tail=[]
rev=False
for i in range(Q):
  tmp=list(input().split())
  if tmp[0]=='1':
    if rev==False:
      rev=True
    else:
      rev=False
  if tmp[0]=='2':
    if rev==False:
      if tmp[1]=='1':
        head.append(tmp[2])
      else:
        tail.append(tmp[2])
    if rev==True:
      if tmp[1]=='1':
        tail.append(tmp[2])
      else:
        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())
head=""
tail=""
rev=False

for _ in range(Q):
    q = input().split()
    if len(q)==1:
        rev = not rev
    else:
        if rev ^ (q[1]=="1"):
            head+=q[2]
        else:
            tail+=q[2]
if rev:
    print(tail[::-1]+S[::-1]+head)
else:
    print(head[::-1]+S+tail)

Solution (Python):
S=input()
Q=int(input())
head=[]
tail=[]
rev=False
for i in range(Q):
  tmp=list(input().split())
  if tmp[0]=='1':
    if rev==False:
      rev=True
    else:
      rev=False
  if tmp[0]=='2':
    if rev==False:
      if tmp[1]=='1':
        head.append(tmp[2])
      else:
        tail.append(tmp[2])
    if rev==True:
      if tmp[1]=='1':
        tail.append(tmp[2])
      else:
        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):
from collections import deque
S = input()
d = deque()
for s in S:
  d.append(s)
flag = True 
for i in range(int(input())):
  query = input().split()
  if query[0] == '1':
    flag = not flag
  if query[0] == '2':
    if query[1] == '1':
      if flag:
        d.appendleft(query[2])
      if not flag:
        d.append(query[2])
    if query[1] == '2':
      if flag:
        d.append(query[2])
      if not flag:
        d.appendleft(query[2])
if flag:
  for i in d:
    print(i, end='')
if not flag:
  for i in reversed(d):
    print(i, end='')

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

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

migi_matu=''
hidari_matu=''

for i in range(q):
  k=input().split()
  
  if k[0]=='1':
    direction*=(-1)
  if k[0]=='2':
    if k[1]=='1':
      if direction==1:
        hidari_matu+=k[2]
      if direction==-1:
        migi_matu+=k[2]
    else:
      if direction==1:
        migi_matu+=k[2]
      if direction==-1:
        hidari_matu+=k[2]







  
  
if direction==1:
  print(hidari_matu[::-1]+s+migi_matu)
if direction==-1:
  print(migi_matu[::-1]+s[::-1]+hidari_matu)

Solution (Python):
S = list(input())
Q = int(input())
front = []
back = []
ifZyun = True

for _ in range(Q):
    query = input()
    if query == "1":
        ifZyun = not ifZyun
    else:
        query = list(query)
        F = query[2]
        C = query[4]
        if (F == "1" and ifZyun) or (F == "2" and not ifZyun):
            front.append(C)
        else:
            back.append(C)

if not ifZyun:
    S = S[::-1]
    print("".join(back[::-1] + S + front))
else:
    front = front[::-1]
    print("".join(front + S + back))

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

for i in range(q):
    a=list(input().split())
    if a[0]=='1':
        rev*=-1
    else:
        if a[1]=='1':
            if rev>0:
                s.appendleft(a[2])
            else:
                s.append(a[2])
        else:
            if rev>0:
                s.append(a[2])
            else:
                s.appendleft(a[2])

a=[]
for i in range(len(s)):
    a+=s.popleft()    
if rev<0:
    a=a[::-1]
print(''.join(a))

Solution (Python):
from collections import deque
S = input()
Q = int(input())
k = 1
d = deque(S)
for _ in range(Q):
    query = input()
    if query.startswith('1'):
        k *= -1
    elif query.startswith('2'):
        t, f, c = query.split()
        if f == '1':
            if k > 0:
                d.appendleft(c)                
            else:
                d.append(c)
        elif f == '2':
            if k > 0:
                d.append(c)
            else:
                d.appendleft(c)
d = list(d)
if k > 0:
    print(''.join(d))
else:
    print(''.join(d[::-1]))

Solution (Python):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)

s = input()
q = int(input())
flag = True
p = ''
m = ''
for i in range(q):
    qfc = input()
    if qfc[0] == '1':
        if flag:
            flag = False
        else:
            flag = True
    else:
        if qfc[2] == '1':
            if flag:
                p += qfc[4]
            else:
                m += qfc[4]
        else:
            if flag:
                m += qfc[4]
            else:
                p += qfc[4]
if flag:
    print(p[::-1] + s + m)
else:
    print(m[::-1] + s[::-1] + p)

Solution (Python):

from collections import deque

s = input()
head = 1 

q = int(input())
sl = [ s[i] for i in range(len(s)) ]
sq = deque(sl)


for i in range(q):
    ql = input()
    if ql[0] == '1':
        head *= -1
    elif ql[0] == '2':
        (op, f, c) = ql.split()
        ht = 1
        if f == '1':
            ht = head
        else:
            
            ht = head * -1

        if ht == 1:
            sq.appendleft(c)
        else:
            sq.append(c)


if head == 1:
    print(''.join(sq))
else:
    print(''.join(reversed(sq)))

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

flipped = False
head = ''
tail = ''

for _ in range(Q):
    tmp = input()
    if tmp[0] == '1':
        flipped = not flipped
    else:
        _, f, c = tmp.split()

        if (f == '1' and not flipped) or (f == '2' and flipped):
            head += c
        else:
            tail += c

if not flipped:
    print(head[::-1] + S + tail)
else:
    print(tail[::-1] + S[::-1] + head)

Solution (Python):
def main():
    s = input()
    q = int(input())
    reverse = 0
    head = ""
    tail = ""
    for _ in range(q):
        query = input().split()
        if query[0] == "1":
            reverse = not(reverse)
        else:
            if (reverse^int(query[1]))&1:
                head += query[2]
            else:
                tail += query[2]
    s = head[::-1] + s + tail
    if reverse&1:
        print(s[::-1])
    else:
        print(s)
if __name__ == "__main__":
    main()

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

flip = False

head = ""
tail = ""

for q in range(Q):
    Ti = input()
    if Ti == '1':
        flip = not flip
    else:
        _, Fi, Ci = map(str, Ti.split())
        Fi = int(Fi)

        if (Fi^flip)&1:
            head += Ci
        else:
            tail += Ci

S = head[::-1] + S + tail
print( S if not flip else S[::-1])

Solution (Python):
s=input()
q=int(input())
head=''
foot=''

a=0
for i in range(q):
  t=input()
  if t=='1':
    a=a^1
  else:
    t, f, c = map(str, t.split())
    f=int(f)
    if (f^a)&1==0:
      foot+=c
    else:
      head+=c
s=head[::-1]+s+foot
if a==1:
  s=s[::-1]
print(s)

Solution (Python):
s=input()
q=int(input())
head=''
foot=''

a=0
for i in range(q):
  t=input()
  if t=='1':
    a=a^1
  else:
    t, f, c = map(str, t.split())
    f=int(f)
    if (f^a)&1==0:
      foot+=c
    else:
      head=c+head
s=head+s+foot
if a==1:
  s=s[::-1]
print(s)

Solution (Python):
def main():
    S = input()
    Q = int(input())
    H = True
    A = ""  
    B = ""
    for _ in range(Q):  
        q = list(input().split())
        if q[0] == "1": H = not H
        elif q[1] == "1" and H:     A = q[2] + A
        elif q[1] == "1" and not H:     B = B + q[2]
        elif q[1] == "2" and H:     B = B + q[2]
        elif q[1] == "2" and not H:     A = q[2] + A

    ans = A + S + B
    if H:   print(ans)
    else:   print(ans[::-1])

main()

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

dir_changed = False
hd, ta = [], []

for _ in range(Q):
  A = list(input().split())
  if A == ["1"]:
    dir_changed = not dir_changed
    hd, ta = ta, hd
    continue
  F, C = int(A[1]), A[2]
  
  
  if F == 1:
    hd.append(C)
  else:
    ta.append(C)
    
if dir_changed:
  S = S[::-1]
print("".join(hd[::-1]) + S + "".join(ta))

Solution (Python):
import sys
input = sys.stdin.readline
from collections import deque
def main():
    S = input().rstrip("\r\n")
    Q = int(input())
    d = deque(S)
    reverse = False
    for _ in range(Q):
        q = input().rstrip("\r\n")
        if q == "1":
            reverse = not reverse
        else:
            _, f, c = q.split()
            if (f == "1" and not reverse) or (f == "2" and reverse):
                d.appendleft(c)
            else:
                d.append(c)
    l = list(d)
    if reverse:l.reverse()
    print("".join(l))
if __name__ == "__main__":
    main()

Solution (Python):

from collections import deque
def main():
    s = deque(input())
    q = int(input())
    top = True
    for i in range(q):
        query = list(map(str, input().split()))
        if query[0] == "1":
            top = not top
        else:
            if (query[1]=="1" and top==True) or (query[1]=="2" and top==False):
                s.appendleft(query[2])
            else:
                s.append(query[2])
    if top == False:
        for i in range(len(s)):
            print(s[len(s)-1-i],end="")
    else:
        for c in s:
            print(c,end="")
            
if __name__ == '__main__':
    main()

Solution (Python):






def main():
    S = input()
    Q = int(input())
    H = True
    A = ''
    B = ''
    for i in range(Q):
        q = list(input().split())
        if q[0] == '1':
            H = not H
        elif q[1]=='1' and H:
            A = q[2] + A
        elif q[1]=='2' and not H:
            A = q[2] + A
        elif q[1]=='1' and not H:
            B = B + q[2]
        elif q[1]=='2' and H:
            B = B + q[2]
    ret = A + S + B
    if H:
        print(ret)
    else:
        print(ret[::-1])

if __name__ == '__main__':
    main()

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

dq1 = deque(S)
dq2 = deque(S[::-1])
isReversed = False

for _ in range(Q):
    l = input().split()
    if l[0] == "1":
        isReversed = not isReversed
    else:
        s = l[2]
        if l[1]=="1":
            if isReversed:
                dq1.append(s)
                dq2.appendleft(s)
            else:
                dq1.appendleft(s)
                dq2.append(s)
        else:
            if isReversed:
                dq1.appendleft(s)
                dq2.append(s)
            else:
                dq1.append(s)
                dq2.appendleft(s)

print("".join(dq2 if isReversed else dq1))

Solution (Python):
s = input()
q = int(input())
f = []
b = []
h = 0

for i in range(q):
    a = list(input().split())
    if a[0] == "1":
        f, b = b, f
        h = 1 - h
    elif len(a) >= 3:
        if a[1] == "1":
            f.append(a[2])
        elif a[1] == "2":
            b.append(a[2])

if h == 0:
    print("".join(f[::-1]) + s + "".join(b))
else:
    print("".join(f[::-1]) + s[::-1] + "".join(b))

Solution (Python):
from collections import deque

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

q = deque(S)

parity = 0
for i in range(Q):
    T = input().split()
    if len(T) == 1:
        parity = (parity + 1) % 2
        
    else:
        F = int(T[1])
        C = T[2]

        if parity == 0:
            if F == 1:
                q.appendleft(C)
            else:
                q.append(C)
        else:
            if F == 1:
                q.append(C)
            else:
                q.appendleft(C)

if parity == 1:
    q.reverse()

print(''.join(q))

Solution (Python):

S=input()
Q=int(input())
Query=[]
for i in range(Q):
    Query.append(list(input().split()))

muki=0
ans_right=[]
ans_left=[]
for i in range(Q):
    if Query[i][0]=="1":
        if muki==0:
            muki=1
        elif muki==1:
            muki=0
    else:
        if muki==0:
            if Query[i][1]=="1":
                ans_left.append(Query[i][2])
                
            elif Query[i][1]=="2":
                ans_right.append(Query[i][2])
                
        elif muki==1:
            if Query[i][1]=="1":
                ans_right.append(Query[i][2])
                
            elif Query[i][1]=="2":
                ans_left.append(Query[i][2])
                
ans_left.reverse()

S=list(S)
ans=ans_left+S+ans_right

if muki==0:
    ans="".join(ans)
    print(ans)
else:
    ans.reverse()
    ans="".join(ans)
    print(ans)

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

head = []
tail = []
flip = 0

for _ in range(q):
	query = input().split()
	if query[0] == '1':
		flip ^= 1
	else:
		f = query[1]
		c = query[2]
		if (f == '1' and flip == 0) or (f == '2' and flip == 1):
			head.append(c)
		else:
			tail.append(c)

if flip:
	head, tail = tail, head
	s = s[::-1]

head = ''.join(head)
tail = ''.join(tail)

print(head[::-1] + s + tail)

Solution (Python):
from collections import deque

s = deque(input())

q = int(input())
r = 0
for _ in range(q):
    A = list(input().split())
    if A[0] == '1':
        r = (r + 1)%2
    else:
        if (A[1] == '1' and r % 2 == 0) or (A[1] == '2' and r % 2 == 1):
            s.appendleft(A[2])
        else:
            s.append(A[2])

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

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

head = []
tail = []
flip = 0

for i in range(q):
	query = input().split()
	if query[0] == '1':
		flip ^= 1
	else:
		f = int(query[1]) - 1
		c = query[2]
		if (f ^ flip) == 0:
			head.append(c)
		else:
			tail.append(c)

if flip:
	head, tail = tail, head
	s = s[::-1]

head = ''.join(head)
tail = ''.join(tail)

print(head[::-1] + s + tail)

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

flip = False
s_0 = ''
s_n = ''
for q in queries:
  if q[0] == '1':
    flip = not flip
  else:
    if (q[1] == '1' and not flip) or (q[1] == '2' and flip):
      s_0 += q[2]
    else:
      s_n += q[2]
if flip:
  print(s_n[::-1] + S[::-1] + s_0)
else:
  print(s_0[::-1] + S + s_n)

Solution (Python):
from collections import deque
s = input()
q = int(input())
array = [list(map(str, input().split())) for i in range(q)]

d = deque(s)
reverse = 1
for i in range(q):
    if array[i][0] == "1":
        reverse *= -1
    elif (array[i][1] == "1") and (reverse == 1):
        d.appendleft(array[i][2])
    elif (array[i][1] == "1") and (reverse == -1):
        d.append(array[i][2])
    elif (array[i][1] == "2") and (reverse == 1):
        d.append(array[i][2])
    elif (array[i][1] == "2") and (reverse == -1):
        d.appendleft(array[i][2])

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

d_str = "".join(list(d))
print(d_str)

Solution (Python):
from collections import deque

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

Solution (Python):
s=input()
q=int(input())
q_=[input() for i in range(q)]
l=[]
r=list(s)
reverse=False
for i in q_:
  if i=="1":
    reverse=not reverse
  elif (i.split()[1]=="1" and not reverse) or (i.split()[1]=="2" and reverse):
    l.append(i.split()[2])
  else:
    r.append(i.split()[2])


if reverse:
  print(''.join(r[::-1]+l))
else:print(''.join(l[::-1]+r))

Solution (Python):
s=input()
q=int(input())
q_=[input() for i in range(q)]
l=[]
r=list(s)
reverse=False
for i in q_:
  if i=="1":
    reverse=not reverse
  elif (i.split()[1]=="1" and not reverse) or (i.split()[1]=="2" and reverse):
    l.append(i.split()[2])
  else:
    r.append(i.split()[2])


if reverse:
  print(''.join(r[::-1]+l))
else:print(''.join(l[::-1]+r))

Solution (Python):
from collections import deque


S = deque(input())
Q = int(input())
QUERY = [input().split(" ") for i in range(Q)]
reverse_f = 0

for query in QUERY:
    if query[0] == "1":
        reverse_f = (reverse_f+1)%2
    else:
        if query[1] == "1":
            if reverse_f:
                S.append(query[2])
            else:
                S.appendleft(query[2])

        else:
            if reverse_f:
                S.appendleft(query[2])
            else:
                S.append(query[2])

if reverse_f:
    S.reverse()
print("".join(list(S)))

Solution (Python):
s = list(input())
q = int(input())
rev = False
fwd =[]

for i in range(q):
    t = input()
    if len(t) == 1:        
        if rev:
            rev = False
        else:
            rev = True
    else:
        q2, f, c = t.split()
        if f == "1":
            if rev:
                s.append(c)
            else:
                fwd.append(c)
        if f == "2":
            if rev:
                fwd.append(c)
            else:
                s.append(c)

s = list(reversed(fwd)) + s

if rev:
    s = "".join(list(reversed(s)))
else:
    s = "".join(s)

print(s)

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

n = len(s)
rev = False
fwd = []

for i in range(q):
    t = input()
    if len(t) == 1:
        if rev:
            rev = False
        else:
            rev = True
    else:
        q2, f, c = t.split()
        if rev:
            if int(f) == 2:
                fwd.append(c)
                n += 1
            else:
                s.append(c)
                n += 1
        else:
            if int(f) == 1:
                fwd.append(c)
                n += 1
            else:
                s.append(c)
                n += 1

fwd = list(reversed(fwd))

s = fwd + s

if rev:
    s = "".join(list(reversed(s)))
else:
    s = "".join(s)

print(s)

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

for i in range(q):
    t = input()
    if rev == 0:
        if len(t) == 1:
            rev = 1
        else:
            if t[2] == "1":
                s.appendleft(t[4])
            else:
                s.append(t[4])
    else:
        if len(t) == 1:
            rev = 0
        else:
            if t[2] == "1":
                s.append(t[4])
            else:
                s.appendleft(t[4])

if rev == 1:
    s.reverse()
print(*s, sep="")

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

x = []
y = []
cnt = 0

for i in range(q):
    a = list(input().split())
    if a[0] == "1":
        x, y = y, x
        cnt += 1
    else:
        if a[1] == "1":
            x.append(a[2])
        else:
            y.append(a[2])

if cnt % 2 == 1:
    s = s[::-1]

s = ''.join(x[::-1]) + s + ''.join(y)

print(s)

Solution (Python):
s = input()
q = int(input())
a = []
b = []
c = 0
for i in range(q):
    init = list(input().split())
    if init[0] == '1':
        a, b = b, a
        c += 1
    else:
        if init[1] == '1':
            a.append(init[2])
        else:
            b.append(init[2])
if c % 2 == 1:
    s = s[::-1]
print(''.join(a[::-1]) + s + ''.join(b))

Solution (Python):
from collections import deque
S = deque(input())
Q = int(input())
is_reversed = 1
for _ in range(Q):
    query = list(input().split())
    if query[0] == "1":
        is_reversed *= -1
    
    else:
        if is_reversed == 1:
            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])

S = "".join(S)
print(S[::is_reversed])

Solution (Python):
import sys
s=input()
q=int(input())
a=[]
b=[]
c=0
for i in sys.stdin:
  if '1' in i.split()[0]:
    a,b=b,a
    c+=1
  else:
    if '1' in i.split()[1]:
      a.append(i.split()[2])
    else:
      b.append(i.split()[2])
if c%2==1:
  s=''.join(list(s)[::-1])
print(''.join(a[::-1])+s+''.join(b))

Solution (Python):
from collections import deque

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

def start(S):
    state = {
        "S": S,
        "is_reversed": False,
        "left": deque(),
        "right": deque(),
    }

    return state


def T1(state, Query):
    state['is_reversed'] = not state['is_reversed']


def T2(state, Query):
    F = Query[1]
    C = Query[2]

    if not state['is_reversed']:
        if F == '1':
            state['left'].appendleft(C)
        else:
            state['right'].append(C)
    else:
        if F == '1':
            state['right'].append(C)
        else:
            state['left'].appendleft(C)


def output(state):
    output = ''.join(state['left']) + state['S'] + ''.join(state['right'])
    if state['is_reversed']:
        output = ''.join(list(reversed(output)))

    return output

def fetch(Query):
    operations = {
        '1': T1,
        '2': T2,
    }

    index = Query[0]
    operation = operations[index]

    return operation

def operate(state, Query):
    T = fetch(Query)
    T(state, Query)


def execute(state, Queries):
    state = start(S)

    for Query in Queries:
        operate(state, Query)

    result = output(state)

    return result


answer = execute(S, Queries)

print(answer)