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

S = deque(sys.stdin.readline().rstrip())
Q = int(sys.stdin.readline().rstrip())
flip = False

for i in range(Q):
    Que = sys.stdin.readline().rstrip()
    if Que == "1":
        flip = not(flip)
    else:
        Query = Que.split()
        F = Query[1]
        if (F == "1") ^ flip:
            S.appendleft(Query[2])
        else:
            S.append(Query[2])
print("".join(S) if not flip else "".join(S)[::-1])

Solution (Python):
from collections import deque

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

que = deque()

for s in S:
    que.append(s)

swi = 0
for _ in range(Q):
    query = input().split()

    if query[0] == "1":
        swi ^= 1
    else:
        if query[1] == "1":
            if swi == 0:
                que.appendleft(query[2])
            else:
                que.append(query[2])
        else:
            if swi == 0:
                que.append(query[2])
            else:
                que.appendleft(query[2])

if swi == 1:
    que.reverse()

print("".join(list(que)))

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

top=""
btm=""

iturn=0
for iq in range(1,q+1):
	t=input().split()
	if t[0]=='1':
		iturn=iturn+1   	
	else:
		iturn2=iturn%2
		if (t[1]=='1' and iturn2==0) or (t[1]=='2' and iturn2==1):


			top=t[2]+top
		else:


			btm=btm+t[2]

if iturn%2==0:
	s=top+s+btm
else:
	s=btm[::-1]+s[::-1]+top[::-1]
print (s)

Solution (Python):
s = input()
q = int(input())
head = []
tail = []
state = 1

for i in range(q):
    query = input().split()
    if len(query) == 1:
        state *= -1

    elif state == 1:
        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])

if state == 1:
    print("".join(head[::-1])+s+"".join(tail))
else:
    print("".join(tail[::-1])+s[::-1]+"".join(head))

Solution (Python):
from collections import deque
s = input()
q = int(input())
flip = 0
d = deque(list(s))
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 == 1:
            d.append(c)
        else:
            d.appendleft(c)
if flip:
    d.reverse()
print(''.join(d))

Solution (Python):
from collections import deque
d = deque()

S = input()
Q = int(input())
d.extend(S)




front = True

for i in range(Q):
    inp = input().split()
    if inp[0] == '1':
        front = not front
    else:
        F = int(inp[1])
        C = inp[2]
        if F == 1:
            if front:
                d.appendleft(C)
            else:
                d.append(C)
        elif F == 2:
            if front:
                d.append(C)
            else:
                d.appendleft(C)

if not front:
    d.reverse()

d_list = list(d)
str = "".join(d_list)

print(str)

Solution (Python):
from collections import deque
 
S = input()
L = deque([])
for c in S:
    L.append(c)
Q = int(input())
flag = 1
for _ in range(Q):
    q = list(input().split())
    if q[0] == "1":
        flag *= -1
    else:
        F = q[1]
        if F == "1":
            if flag == 1:
                L.appendleft(q[2])
            else:
                L.append(q[2])
        else:
            if flag == 1:
                L.append(q[2])
            else:
                L.appendleft(q[2])
if flag == 1:
    print("".join(map(str, L)))
else:
    L.reverse()
    print("".join(map(str, L)))

Solution (Python):
from collections import deque

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

reverse = 1 

d = deque(S)

for i in range(Q):
    Query_tmp = list(map(str, input().split()))
    
    if Query_tmp[0] == "1":
        reverse *= -1
        
    else:
        Ci = Query_tmp[2]

        if Query_tmp[1] == "1":
            if reverse == 1:
                d.appendleft(Ci)
            else:
                rev_Ci = Ci[::-1]
                d.append(rev_Ci)
        else:
            if reverse == 1:
                d.append(Ci)
            else:
                rev_Ci = Ci[::-1]
                d.appendleft(rev_Ci)
    
    
    
    
    

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

print("".join(d))

Solution (Python):

S = input()
Q = int(input())
P = [[]] * Q
for i in range(Q):
    P[i] = list(map(str, input().split()))


flip = 0
cap = ""
tail = ""

for i in range(Q):
    if int(P[i][0]) == 1:
        flip += 1
    else:
        n = int(P[i][1])
        v = P[i][2]
        if flip % 2 == 0:
            if n == 1:
                cap = v + cap
            else:
                tail = tail + v
        else:
            if n == 1:
                tail = tail + v
            else:
                cap = v + cap

answer = ""
if flip % 2 == 0:
    answer = cap + S + tail
else:
    answer = tail[-1::-1] + S[-1::-1] + cap[-1::-1]

print(answer)

Solution (Python):
from collections import deque
s = deque(input())
q = int(input())
lis = [list(input().split()) for _ in range(q)]
rev = 0
z = 0

for i in lis:
    if i[0] == "2":
        if (rev + int(i[1])) % 2 == 1:
            s.appendleft(i[2])
        else:
            s.append(i[2])
        z = 1
    else:
        if z == 1:
            rev += 1
        
if rev % 2 == 1:
    s = reversed(s)
print("".join(s))

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

k = 0
fo = ''
re = ''
for i in range(q):
    if quer[i][0] == '1':
        k += 1
    else:
        if quer[i][1] == '1':
            if k % 2 == 0:
                re = re + quer[i][2]
            else:
                fo = fo + quer[i][2]
        else:
            if k % 2 == 0:
                fo = fo + quer[i][2]
            else:
                re = re + quer[i][2]

if k % 2 ==0:
    print( ''.join(list(reversed(re))) + s + fo)
else:
    print(''.join(list(reversed(fo))) + ''.join(list(reversed(s))) + re)

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

Solution (Python):
from collections import deque

s = deque(list(input()))
q = int(input())
rev = False
for _ in range(q):
    qr = list(map(str, input().split()))
    if qr[0] == '1':
        rev = 1 - rev
    else:
        if qr[1] == '1':
            if rev: s.append(qr[2])
            else  : s.appendleft(qr[2])
        else:
            if rev: s.appendleft(qr[2])
            else  : s.append(qr[2])
if rev:
    s.reverse()
    print(''.join(s))
else:
    print(''.join(s))

Solution (Python):
s = input()
q = int(input())
a, b = "", ""
rev = False
for _ in range(q):
    qr = list(map(str, input().split()))
    if qr[0] == '1':
        rev = 1 - rev
    else:
        if qr[1] == '1':
            if rev: b += qr[2]
            else  : a += qr[2]
        else:
            if rev: a += qr[2]
            else  : b += qr[2]
if rev:
    print(b[::-1] + s[::-1] + a)
else:
    print(a[::-1] + s + b)

Solution (Python):
from collections import deque
s = deque(input())
q = int(input())
lis = [list(input().split()) for _ in range(q)]
rev = 0
z = 0

for i in lis:
    if i[0] == "2":
        if (rev + int(i[1])) % 2 == 1:
            s.appendleft(i[2])
        else:
            s.append(i[2])
        z = 1
    else:
        if z == 1:
            rev += 1
        
if rev % 2 == 1:
    s = reversed(s)
print("".join(s))

Solution (Python):
from collections import deque

S = input()
L = deque([])
for c in S:
    L.append(c)
Q = int(input())
flag = 1
for _ in range(Q):
    q = list(input().split())
    if q[0] == "1":
        flag *= -1
    else:
        F = q[1]
        if F == "1":
            if flag == 1:
                L.appendleft(q[2])
            else:
                L.append(q[2])
        else:
            if flag == 1:
                L.append(q[2])
            else:
                L.appendleft(q[2])
if flag == 1:
    print("".join(map(str, L)))
else:
    L.reverse()
    print("".join(map(str, L)))

Solution (Python):
s = input()
top = ''
n = int(input())
isReverse = False
for i in range(n):
    Q = input().split()
    if(Q[0] == '1'):
        isReverse = not isReverse
    else:
        if Q[1] == '1':
            if isReverse: s += Q[2]
            else: top += Q[2]
        else:
            if isReverse: top += Q[2]
            else: s += Q[2]
if isReverse: s = s[::-1] + top
else: s = top[::-1] + s
print(s)

Solution (Python):
from collections import deque
s = input()
Q = int(input())
s = deque([s])
rev = 1
for i in range(Q):
    tmp = list(input().split())
    if len(tmp)==1:
        rev *= -1
    else:
        f,c = tmp[1], tmp[2]
        if rev > 0:
            if f=='1':
                s.appendleft(c)
            else:
                s.append(c)
        else:
            if f=='1':
                s.append(c)
            else:
                s.appendleft(c)
s = ''.join(list(s))
print(s if rev > 0 else s[::-1])

Solution (Python):
s = input()
q_num = int(input())
head = []
tail = []
flip = 0
for i in range(q_num):
    q = tuple(input().split())
    if q[0] == '1':
        flip ^= 1
    else:
        f = int(q[1]) - 1
        c = q[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()
q_num = int(input())
head = []
tail = []
flip = 0
for i in range(q_num):
    q = tuple(input().split())
    if q[0] == '1':
        flip ^= 1
    else:
        f = int(q[1]) - 1
        c = q[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()
top = ''
n = int(input())
isReverse = False
for i in range(n):
    Q = input().split()
    if(Q[0] == '1'):
        isReverse = not isReverse
    else:
        if Q[1] == '1':
            if isReverse: s += Q[2]
            else: top += Q[2]
        else:
            if isReverse: top += Q[2]
            else: s += Q[2]
if isReverse: s = s[::-1] + top
else: s = top[::-1] + s
print(s)

Solution (Python):
from collections import deque

S = deque(input())
q = int(input())
Q = [list(map(str, input().split())) for _ in range(q)]

flip = False

for qi in Q:
    if int(qi[0]) == 1:
      flip = not flip
    else:
      f = int(qi[1]) 
      c = qi[2]
      if flip:
        f = 3-f    
      if f == 1:
        S.appendleft(c)
      else:
        S.append(c)

if flip:
    S.reverse()
print(''.join(S))

Solution (Python):
import sys
import collections


def solve():
    input = sys.stdin.readline
    mod = 10 ** 9 + 7
    s = str(input().rstrip('\n'))
    s = collections.deque(s)
    q = int(input().rstrip('\n'))
    r = False
    for _ in range(q):
        tfc = list(map(str, str(input().rstrip('\n')).split()))
        if tfc[0] == "1":
            r = not r
        else:
            if not r:
                if tfc[1] == "1":
                    s.appendleft(tfc[2])
                else:
                    s.append(tfc[2])
            else:
                if tfc[1] == "1":
                    s.append(tfc[2])
                else:
                    s.appendleft(tfc[2])
    print("".join(list(s)) if not r else "".join(reversed(s)))


if __name__ == '__main__':
    solve()

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

N=int(input())
cnt=0
f=''
b=''
for i in range(N):
  inp=str(input())
  if len(inp)==1:
    cnt+=1
  else:
    F,c=int(inp[2]),inp[4]
    if F==1:
      if cnt%2==0:
        f=c+f
      else:
        b+=c
    else:
      if cnt%2==0:
        b+=c
      else:
        f=c+f
if cnt%2==0:
  print(f+s+b)
else:
  ans=f+s+b
  L=len(ans)
  if L==1:
    print(ans)
  else:
    if L%2==1:
      f=ans[0:L//2]
      s=ans[L//2]
      b=ans[L//2+1:L]
      f_=''.join(list(reversed(b)))
      b_=''.join(list(reversed(f)))
      print(f_+s+b_)
    else:
      f=ans[0:L//2]
      b=ans[L//2:L]
      f_=''.join(list(reversed(b)))
      b_=''.join(list(reversed(f)))
      print(f_+b_)

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

t = ''
for qi in Q:
  if int(qi[0]) == 1:
    s, t = t, s
  else:
    if int(qi[1])  == 1:
      t += qi[2]
    else:
      s += qi[2]
      
t = t[::-1]
t += s
print(t)

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

text = deque(s)
readleft = True


for _ in range(q):
  query = input().rstrip().split()
  if query[0] == "1":
    readleft = not(readleft)
  else:
    addtop = 2 - int(query[1])
    addleft = (addtop == readleft)
    if addleft:
      text.appendleft(query[2])
    else:
      text.append(query[2])

if readleft:
  while text != deque():
    print(text.popleft(), end="")
  print()
else:
  while text != deque():
    print(text.pop(), end="")
  print()

Solution (Python):
from collections import deque

S = input()
Q = int(input())
ARR = []

for i in range(Q):
    arr = input().split(" ")
    ARR.append(arr)


def calculate(s,q,arr):
    s = deque(list(s))

    kk = 0
    for ar in arr:
        if int(ar[0]) == 1:
            kk = kk + 1
        if int(ar[0]) == 2:
            if int(ar[1]) == 1:
                if kk % 2 == 0:
                    s.appendleft(ar[2])
                else:
                    s.append(ar[2])
            else:
                if kk % 2 == 0:
                    s.append(ar[2])
                else:
                    s.appendleft(ar[2])

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

    print("".join(list(s)))

calculate(S,Q,ARR)

Solution (Python):










import sys

sys.setrecursionlimit(10000000)
from collections import Counter, deque

from itertools import combinations, permutations

from bisect import bisect_left,bisect_right
from math import floor, ceil




def main():
    from sys import stdin
    
    sys.setrecursionlimit(10000000)
    from collections import Counter, deque
    
    from itertools import combinations, permutations
    
    from bisect import bisect_left,bisect_right
    from math import floor, ceil
    

    

    S = list(stdin.readline().rstrip())
    Q = int(input())
    mae = []
    usiro =[]
    s = True
    for _ in range(Q):
        q = stdin.readline()
        if q[0]=='1':
            s = not s
        else:
            if q[2]=='1' and s:
                mae.append(q[4])
            elif q[2]=='2' and s:
                usiro.append(q[4])
            elif q[2]=='2' and not s:
                mae.append(q[4])
            else:
                usiro.append(q[4])
    if s:
        print(''.join(mae[::-1]+S+usiro))
    else:
        print(''.join(usiro[::-1]+S[::-1]+mae))


if __name__ == '__main__':
    main()

Solution (Python):
def main():
    from sys import stdin
    
    
    from collections import Counter, deque
    
    from itertools import combinations, permutations
    
    from bisect import bisect_left,bisect_right
    from math import floor, ceil
    

    

    S = input()
    Q = int(input())
    mae = ''
    usiro = ''
    s = True
    for _ in range(Q):
        q = stdin.readline()
        if q[0]=='1':
            s = not s
        else:
            if q[2]=='1' and s:
                mae = q[4] + mae
            elif q[2]=='2' and s:
                usiro = usiro + q[4] 
            elif q[2]=='2' and not s:
                mae = q[4] + mae
            else:
                usiro = usiro + q[4]
    if s:
        print(mae+S+usiro)
    else:
        print(usiro[::-1]+S[::-1]+mae[::-1])


if __name__ == '__main__':
    main()

Solution (Python):
from collections import deque
s = deque(input())
n = int(input())
f = 0
for i in range(n):
  l = list(map(str, input().split()))
  if l[0] == "2":
    if (l[1] == "1" and f == 0) or (l[1] == "2" and f == 1):
      s.appendleft(l[2])
    else:
      s.append(l[2])
  else:
    if f == 0:
      f = 1
    else:
      f = 0
if f == 1:
  s.reverse()
print("".join(s))

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

n = int(input())
f = 1
for i in range(n):
  l = list(map(str, input().split()))
  if l[0] == "2":
    tmp = int(l[1])*f
    if tmp == 1 or tmp == -2:
      s.appendleft(l[2])
    else:
      s.append(l[2])
  else:
    f *= -1

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

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

t = ''
for qi in Q:
  if int(qi[0]) == 1:
    s, t = t, s
  else:
    if int(qi[1])  == 1:
      t += qi[2]
    else:
      s += qi[2]
      
t = t[::-1]
t += s
print(t)

Solution (Python):
class Node:
    def __init__(self, v):
        self.v = v
        self.p = None
        self.n = None

class LinkedList:
    def __init__(self, v):
        self.head = Node(v)
        self.tail = self.head
        self.d = 1

    def reverse(self):
        self.d = self.d * -1

    def insertTail(self, v):
        node = Node(v)
        if self.d == 1:
            self.tail.n = node
            node.p = self.tail
            self.tail = node
        else:
            self.head.p = node
            node.n = self.head
            self.head = node

    def insertHead(self, v):
        self.reverse()
        self.insertTail(v)
        self.reverse()

    def toString(self):
        res = ""
        if self.d == 1:
            pos = self.head
            while pos != None:
                res = res + pos.v
                pos = pos.n
        else:
            pos = self.tail
            while pos != None:
                res = res + pos.v
                pos = pos.p
        return res

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

l = LinkedList(S[0])
for i in range(1,len(S)):
    l.insertTail(S[i])

for _ in range(Q):
    query = input()
    if query[0] == "1":
        l.reverse()
    else:
        T,F,C = query.split()
        if F=="1":
            l.insertHead(C)
        else:
            l.insertTail(C)
print(l.toString())

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

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

count = 0
for i in range(q):
    t = int(Q[i][0])
    if t==1:
        count += 1
    else:
        f = int(Q[i][1])
        new = Q[i][2]
        if count%2==0 and f==1:
            s.appendleft(new)
        elif count%2==0 and f==2:
            s.append(new)
        elif count%2==1 and f==1:
            s.append(new)
        elif count%2==1 and f==2:
            s.appendleft(new)
        else:
            pass



if count%2==0:
    print("".join(s))
    exit()
else:
    s = list(s)[::-1]
    print("".join(s))
    exit()

Solution (Python):
import sys
from collections import deque 
d=deque(input())

q=int(input())
x=0 
for _ in range(q):
  ss=list(input().split())
  if ss[0]=='1':
    x=1-x
  else:
    f=int(ss[1])
    c=ss[2]
    if x==0:
      if f==1:
        d.appendleft(c)
      else:
        d.append(c)
    else:
      if f==1:
        d.append(c)
      else:
        d.appendleft(c)
if x==0:
  print(''.join(d))
else:
  ans=[]
  for i in d:
    ans+=i
  print(''.join(ans[::-1]))

Solution (Python):
from collections import deque
s = input()
rev = s[::-1]
curs = [deque(list(s)), deque(list(rev))]
f = 0
b = 1
q = int(input())
for _ in range(q):
    stuff = input().split()
    if stuff[0] == '1':
        f, b = b, f
    else:
        op, fi, c = stuff
        fi = int(fi)
        if fi == 1:
            curs[f].appendleft(c)
            curs[b].append(c)
        else:
            curs[f].append(c)
            curs[b].appendleft(c)
print(''.join(curs[f]))

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

L=''
R=''
rev=-1
for i in range(int(Q)):
  tmp=input().split()
  if tmp[0]=='1':
    rev = abs(rev)-1
  else:
    if rev+int(tmp[1])==1:
      R = R + tmp[2]
    else:
      L = L + tmp[2]
if rev==-1:
  print(L[::-1]+S+R)
else:
  print(R[::-1]+S[::-1]+L)

Solution (Python):
s=input()
q=int(input())
c=1
u=''
for i in range(q):
    inp=input()
    if inp[0]=="1":
        c*=-1
    else:
        t,f,p=inp.split()
        if (f=="1" and c!=1) or (f=="2" and c==1):
            s+=p
        else:
        	u+=p
if c!=1:
    print("".join(reversed(s))+u)
else:
    print("".join(reversed(u))+"".join(s))

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

flag = 1
for i in range(q):
  st = input().split()
  if int(st[0]) == 1:
    flag *= -1
  else:
    tmp = int(st[1])*flag
    if tmp == 1 or tmp == -2:
      s.appendleft(st[2])
    else:
      s.append(st[2])

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

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

flag = 1
for i in range(q):
  st = input().split()
  if int(st[0]) == 1:
    flag *= -1
  else:
    tmp = int(st[1])*flag
    if tmp == 1 or tmp == -2:
      s.appendleft(st[2])
    else:
      s.append(st[2])

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

Solution (Python):
from collections import deque
s = deque(list(input()))
n = int(input())
q = deque()
for _ in range(n):
    q.append(list(input().split()))
d = 0
for r in q:
    if r[0] == '1':
        d = d ^ 1
    else:
        if int(r[1]) & (d+1):
            s.appendleft(r[2])
        else:
            s.append(r[2])
if d == 1:
    s.reverse()
ans = "".join(list(s))
print(ans)

Solution (Python):
S=input()
q=int(input())
rev=False
pre=''

for i in range(q):
    inp=input()
    if inp[0]=="1":
        rev=not rev
    else:
        t,f,c=inp.split()
        if (f=='1' and rev) or (f=='2' and not rev):
            S+=c
        else:
            pre+=c
if not rev:
    print(''.join(list(reversed(pre)))+S)
else:
    print(''.join(list(reversed(S)))+pre)

Solution (Python):
from collections import deque

S = input()
dq = deque(S)

Q = int(input())
inverse = False
for _ in range(Q):
    query = list(input().split())
    if query[0] == '1':
        inverse = not inverse
    else:
        tmp = (int(query[1]) - 1) ^ inverse
        if tmp: dq.append(query[2])
        else: dq.appendleft(query[2])

if inverse: dq.reverse()
for c in dq:
    print(c, end='')
print('')

Solution (Python):
S=input()
q=int(input())
rev=False
pre=''
for i in range(q):
    inp=input()
    if inp[0]=="1":
        rev=not rev
    else:
        t,f,c=inp.split()
        if (f=='1' and rev) or (f=='2' and not rev):
            S+=c
        else:
            pre+=c
if not rev:
    print(pre[::-1]+S)
else:
    print(S[::-1]+pre)

Solution (Python):
S=input()
q=int(input())
RV=0
A="A"
for i in range(q):
    IN=input()
    if IN=="1":
        RV+=1
    else:
        t,f,c=IN.split()
        if RV%2==0:
            if f=="1":
                A=c+A               
            else:
                A=A+c
        else:
            if f=="1":
                A=A+c
            else:
                A=c+A
if RV%2==0:
    for i in range(len(A)):
        if A[i]=="A":
            print(A[:i]+S+A[i+1:])
else:
    A=A[::-1]
    for i in range(len(A)):
        if A[i]=="A":
            print(A[:i]+S[::-1]+A[i+1:])

Solution (Python):
Query=list(input())
Q=int(input())
flag = True
strF=[]
strB=[]
for i in range(Q):
  S2=input()
  if S2=="1":
    flag = not flag
  else:
    x=S2.split()
    if flag:
      if x[1]=="1":
        strF.append(x[2])
      else:
        strB.append(x[2])
    else:
      if x[1]=="1":
        strB.append(x[2])
      else:
        strF.append(x[2])

if flag:
  strF.reverse()
  Query=strF+Query+strB
else:
  strB.reverse()
  Query.reverse()
  Query=strB+Query+strF
 
print("".join(Query))

Solution (Python):
s = input()
q = int(input())
Flag = False
right=''
left=''

for i in range(q):
    t = input()
    if t[0] == '1':
        Flag = not Flag
    else:
        _, f, c = t.split()
        if Flag == False and f == '2' or Flag == True and f == '1':
            right += c
        else:
            left += c

ans = left[::-1] + s + right
print(ans if not Flag else ans[::-1])

Solution (Python):
S = input()
Q = int(input())
cond = 1
pre = ''
epi = ''
for i in range(Q):
	q = list(map(str, input().split()))
	if q[0] == "1":
		cond *= -1
	else:
		if q[1] == "1":
			if cond == 1:
				pre += q[2]
			else:
				epi += q[2]
		else:
			if cond == 1:
				epi += q[2]
			else:
				pre += q[2]

if cond == 1:
	print(pre[::-1]+S+epi)
else:
	print(epi[::-1]+S[::-1]+pre)

Solution (Python):
from collections import deque
from sys import stdin

def main():
    S = deque([s for s in input()])
    Q = int(input())
    order = True
    for i in range(Q):
        query = stdin.readline()
        t = int(query[0])
        if t == 1:
            order = not order
        else:
            f = int(query[2])
            c = query[4]
            if not order:
                f = 3  - f 
            left = True
            if f == 1:
                S.appendleft(c)
            else:
                S.append(c)
    if not order:
        S.reverse()
    print(''.join(S))

if __name__ == '__main__':
    main()

Solution (Python):
from collections import deque
from sys import stdin

def main():
    S = deque([s for s in input()])
    Q = int(input())
    order = True
    for i in range(Q):
        query = stdin.readline().split()
        t = int(query[0])
        if t == 1:
            order = not order
        else:
            f = int(query[1])
            c = query[2]
            if not order:
                f = 3  - f 
            left = True
            if f == 1:
                S.appendleft(c)
            else:
                S.append(c)
    if not order:
        S.reverse()
    print(''.join(S))

if __name__ == '__main__':
    main()

Solution (Python):
import sys
input = lambda: sys.stdin.readline().rstrip()
s=[i for i in input()]
x=0 ; st=[[],[]]
for _ in range(int(input())):
    inp=list(map(str,input().split()))
    if len(inp)==1: x^=1
    else: st[(int(inp[1])-1)^x].append(inp[2])
if x==0:
    st[0].reverse()
    ans=st[0]+s+st[1]
elif x==1:
    st[1].reverse()
    s.reverse()
    ans=st[1]+s+st[0]
print(''.join(ans))

Solution (Python):
s=[i for i in input()]
x=0 ; st=[[],[]]
for _ in range(int(input())):
    inp=list(map(str,input().split()))
    if len(inp)==1: x^=1
    else: st[(int(inp[1])-1)^x].append(inp[2])
if x==0:
    st[0].reverse()
    ans=st[0]+s+st[1]
elif x==1:
    st[1].reverse()
    s.reverse()
    ans=st[1]+s+st[0]
print(''.join(ans))

Solution (Python):
S = input()
N = input()
N = int(N)
re = False
foward = ""
back = ""

for i in range(N):
    que = input().split(" ")
    if que[0] == "1":
        if re:
            re = False
        else:
            re = True
    else:
        if re:
            if que[1] == "1":
                back += que[2]
            else:
                foward += que[2]
        else:
            if que[1] == "1":
                foward += que[2]
            else:
                back += que[2]
S = foward[::-1]+S+back
if re:
    print(S[::-1])
else:
    print(S)

Solution (Python):
from collections import deque
def main():
    S = deque([s for s in input()])
    Q = int(input())
    order = True
    for i in range(Q):
        query = input().split()
        t = int(query[0])
        if t == 1:
            order = not order
        else:
            f = int(query[1])
            c = query[2]
            left = True
            if f == 1:
                if order:
                    left = True
                else:
                    left = False
            else:
                if order:
                    left = False
                else:
                    left = True
            if left:
                S.appendleft(c)
            else:
                S.append(c)
    if not order:
        S.reverse()
    print(''.join(S))

if __name__ == '__main__':
    main()

Solution (Python):
from collections import deque

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

c=0
S0=deque(S)

for i in range(Q):
    if Query[i][0]=='1':
        c=c+1
    else:
        if ((Query[i][1]=='1')and(c%2==0)) or ((Query[i][1]=='2')and(c%2==1)):
            S0.appendleft(Query[i][2])
        else:
            S0.append(Query[i][2])

if c%2==0:
    print(''.join(S0))
else:
    print(''.join(reversed(S0)))

Solution (Python):

from collections import deque
x=deque()
flag=True 
s=input()
x.append(s)
q=int(input())
cnt=0
for i in range(q):
    line=list(input().split())
    if len(line)==1:
        cnt+=1
        if cnt%2==1:
            flag=False
        else:
            flag=True
    else:
        t=line[2]
        if line[1]=="1":
            if flag==True:
                x.appendleft(t)
            else:
                x.append(t)
        else:
            if flag==True:
                x.append(t)
            else:
                x.appendleft(t)
ans="".join(x)
if flag==False:
    ans=ans[::-1]
print(ans)

Solution (Python):
s = input()
q = int(input())
rev = False
pre = ''
for i in range(q):
  inp = input()
  if int(inp[0]) == 1:
    rev = not rev
  else:
    t, f, c = inp.split()
    if (int(f) == 1 and rev) or (int(f) == 2 and not rev):
      s += c
    else:
      pre += c
print(s[::-1] + pre if rev else pre[::-1] + s)

Solution (Python):
S = input()
Q = int(input())
front = ''
end = ''
reverse = False
for i in range(Q):
    Query = input()
    if Query[0] == '1':
        reverse = not reverse 
    else:
        if (Query[2] == '1' and reverse == False) or (Query[2] == '2' and reverse == True):
            front = Query[4] + front
        else:
            end = end + Query[4]

S = front + S + end
if reverse == True:
    S = S[::-1]


print(S)
print('\n')