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):
from sys import stdin
 
def main():
    S = list(stdin.readline().rstrip())
    Q = int(input())
    rev = 0
    head = []
    tail = []
 
    for _ in range(Q):
        q = stdin.readline()
        if q[0] == "1":
            rev = (rev+1)%2
        else:
            if q[2] == "1":
                if rev == 0:
                    head.append(q[4])
                else:
                    tail.append(q[4])
            else:
                if rev ==0:
                    tail.append(q[4])
                else:
                    head.append(q[4])
 
    if rev == 0:
        ans = "".join(head[::-1] + S + tail)
    else:
        ans = "".join(tail[::-1] + S[::-1] + head)
    
    print(ans)
    
main()

Solution (Python):
from collections import deque
S = deque(input())
Q = int(input())
Query = [[] for _ in range(Q)]
for i in range(Q):
    Query[i] = list(map(str,input().split()))

fl = False
for q in Query:
    if q[0]=='1':
        fl = not fl
    else:
        if (q[1]=='1') ^ fl:
            S.appendleft(q[2])
        else:
            S.append(q[2])
T = ''.join(S)
if fl:
    T = T[::-1]
print(T)

Solution (Python):
from collections import deque

s = deque((str(input())))
loop = int(input())

rev = 0
for i in range(loop):
    queries = input().split()
    if len(queries) == 1:
        rev = (rev+1) % 2
    else:
        type = int(queries[1])
        c = str(queries[2])
        if (type + rev) % 2 == 1:
            s.appendleft(c)
        else:
            s.append(c)
if rev == 1:
    print("".join(reversed(list(s))))
else:
    print("".join(list(s)))

Solution (Python):
from collections import deque
s=deque(input())
q=int(input())
flg=True 
for i in range(q):
    p=list(map(str,input().split()))
    if p[0]=="1":flg=not(flg)
    else:
        z,f,c=p
        if f=="1":
            if flg:s.appendleft(c)
            else:s.append(c)
        else:
            if not(flg):s.appendleft(c)
            else:s.append(c)

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

Solution (Python):
import collections
s = collections.deque(list(input()))
q = int(input())
cnt = 0
for i in range(q):
    fc = input().split()
    if fc[0] == '1':
        cnt += 1
    else:
        if cnt == 1:
            fc[1] = '1' if fc[1] == '2' else '2'
        if fc[1] == '1':
            s.appendleft(fc[2])
        else:
            s.append(fc[2])
    cnt %= 2
if cnt == 1:
    s.reverse()
print(*s, sep='')

Solution (Python):
from collections import deque
S = list(input())
q_num = int(input())
d = deque(S)
rev = True
for _ in range(q_num):
  query = list(input().split())
  if(query[0] == "1"):
    rev = not rev
  if(query[0] == "2"):
    if(rev):
      if(query[1] == "1"):
        d.appendleft(query[2])
      else:
        d.append(query[2])
    else:
      if(query[1] == "1"):
        d.append(query[2])
      else:
        d.appendleft(query[2])
if(rev):
  print("".join(d))
else:
  d.reverse()
  print("".join(d))

Solution (Python):
from collections import deque
S = deque(input())
Q = int(input())
x = 1
l=[]
for i in range(Q):
    a, *b = input().split()
    if a == "1":
        x *= -1
    elif a == "2":
        c, d = b
        if (c == "2" and x == -1) or  (c == "1" and x == 1):
            S.appendleft(d)
        else:
            S.append(d)
if x == -1:
    S.reverse()
print(*S, sep="")

Solution (Python):
S = input().strip()
Q = int(input())
is_reverse = False
A = ""
B = ""
for i in range(Q):
  query = [j for j in input().strip().split()]
  T = int(query[0])
  if T == 1:
    is_reverse = not is_reverse
  else:
    F, C = int(query[1]), query[2]
    if F == 1:
      if not is_reverse:
        A = C + A
      else:
        B = B + C
    else:
      if not is_reverse:
        B = B + C
      else:
        A = C + A

res = A + S + B
if not is_reverse:
  print(res)
else:
  print(res[::-1])

Solution (Python):
from collections import deque
t=input()
s=deque(t)
q=int(input())
flag=True
for i in range(q):
  ques=list(input().split())
  if ques[0]=="1":
    flag=not flag
    continue
  if ques[1]=="1":
    if flag:
      s.appendleft(ques[2])
    else:
      s.append(ques[2])
  else:
    if flag:
      s.append(ques[2])
    else:
      s.appendleft(ques[2])
if flag==False:
    print(''.join(reversed(list(s))))
else:
    print(''.join(list(s)))

Solution (Python):
import numpy as np
import math as m
import collections

def main(**kwargs):
    front = collections.deque()
    back = collections.deque()
    now = "f"

    for q in qs:
        if q[0] == "1":
            if now == "b":
                now = "f"
            else:
                now = "b"

    if now == "f":
        now = "f"

        for q in qs:
            if q[0] == "1":
                if now == "b":
                    now = "f"
                else:
                    now = "b"
            else:
                if (q[1] == "1" and now == "f") or (q[1] == "2" and now == "b"):
                    front.appendleft(q[2])
                else:
                    back.append(q[2])

        res = "".join(front) + s + "".join(back)
    else:
        now = "f"

        for q in qs:
            if q[0] == "1":
                if now == "b":
                    now = "f"
                else:
                    now = "b"
            else:
                if (q[1] == "1" and now == "f") or (q[1] == "2" and now == "b"):
                    front.append(q[2])
                else:
                    back.appendleft(q[2])
        res = "".join(back) + s[::-1] + "".join(front)
        
    return res

if __name__ == "__main__":
    cin = np.array(input().split(" "))
    s, *_ = cin
    cin = np.array(input().split(" ")).astype("int")
    n, *_ = cin

    qs = []

    for i in range(n):
        cin = np.array(input().split(" "))
        qs.append(list(cin))
    
    cout = main(s=s, n=n, qs=qs)
    print(cout)

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

flg = True

mae = ''
ato = ''

for i in range(q):
    query = input()
    if query[0] == '1':
        flg = not flg
    else:
        if query[2] == '1':
            if flg:
                mae += query[4]
            else:
                ato += query[4]
        else:
            if flg:
                ato += query[4]
            else:
                mae += query[4]

mae = mae[::-1]
ans = mae + s + ato
if not flg:
    ans = ans[::-1]

print(ans)

Solution (Python):
from collections import deque

_strings = deque(input())
num = int(input())

isReverse = False

for i in range(num):
    query = input().split()
    type = int(query[0])

    if type == 1:
        
        isReverse = not isReverse
    else:
        mode = int(query[1])

        if (mode == 1 and not isReverse) or (mode == 2 and isReverse):
            _strings.appendleft(str(query[2]))
            
        else:
            _strings.append(str(query[2]))
            

if isReverse:
    _strings.reverse()

print(''.join(_strings))

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

s = ""
l = ""
h = 1

for _ in range(Q):
    q = list(map(str, input().split()))
    if q[0] == "1":
        h *= -1
        continue
    if q[1] == "1":
        if h == 1:
            s += q[2]
        else:
            l += q[2]
    else:
        if h == 1:
            l += q[2]
        else:
            s += q[2]

ans = s[::-1] + S + l
print(ans[::h])

Solution (Python):
def main():
  from collections import deque
  
  s,q,*r = [list(k.split()) for k in open(0)]
  que = deque(s[0])
  p = False
  for x in r:
    if len(x)==1:
      p = not p
      continue
    if (p==False and x[1]=='1') or (p==True and x[1]=='2'):
        que.appendleft(x[2])
    else:
        que.append(x[2])
  print("".join(que) if p==False else "".join(que)[::-1])
  
if __name__=='__main__':
  main()

Solution (Python):
s=input()
n=int(input())
cnt=0
l=[]
r=[]
for i in range(n):
    e=list(map(str,input().split()))
    q=int(e[0])
    if len(e)!=1:
        f=int(e[1])
        c=e[2]
    if q==2:
        if (f==1 and cnt%2==0) or (f==2 and cnt%2!=0):
            l.append(c)
        else:
            r.append(c)
    else:
        cnt+=1
if cnt%2==0:
    print(''.join(l[::-1])+s+''.join(r))
else:
    print(''.join(r[::-1])+s[::-1]+''.join(l))

Solution (Python):
def main():
  from collections import deque
  s=input()
  q=int(input())
  turn = 2
  d=deque(s)

  for _ in [0]*q:
      query = input()
      if query[0] == "1":
          t = int(query)
          turn = 3 - turn
      else:
          t,f,c = map(str,query.split())
          if (int(f) + turn)% 2  == 0:
              d.append(c)
          else:
              d.appendleft(c)
  if turn == 2:
      print("".join(d))
  else:
      print("".join(list(d)[::-1]))
main()

Solution (Python):
s=input()
q=int(input())
order=1
header=''
footer=''
for _ in range(q):
  tmp=input()
  if tmp=='1':
    order*=-1
  else:
    t, f, c=tmp.split()
    if (int(f)*order)%3==1:
      header=c+header
    else:
      footer=footer+c
s=header+s+footer
print(s) if order==1 else print(s[::-1])

Solution (Python):
from collections import deque

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

R=True
ans=deque(list(s))
count=0
for i in range(q):
    l=list(map(str,input().split()))
    if int(l[0])==1:
        if R:
            R=False
        else:
            R=True
        count+=1
    else:
        f,c=int(l[1]),l[2]
        if f==1 and R:
            ans.appendleft(c)
        elif f==2 and R:
            ans.append(c)
        elif f==1 and (not R):
            ans.append(c)
        elif f==2 and (not R):
            ans.appendleft(c)

if count%2==0:
    print(''.join(ans))
else:
    print(''.join(reversed(ans)))

Solution (Python):


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

query = []

for i in range(Q):
    query.append(input().split())


bool = True
pre =''
suf =''

for i in query:
    if len(i) == 1:
        bool = not(bool)

    else:
        if i[1]=="1":
            if bool:
                pre = i[2]+pre
            else:
                suf += i[2]
        else:
            if bool:
                suf += i[2]
            else:
                pre = i[2]+pre

S = pre+ S +suf
print(S) if bool else print(S[::-1])

Solution (Python):
def Main(S, Q, queries):
    right = S
    left = ""
    flipped = False
    T1 = 0
    for q in queries:
        if int(q[0]) == 1:
            if not flipped:
                flipped = True
            else:
                flipped = False
            continue
        else:
            if int(q[1]) == 1:
                if flipped:
                    right += q[2]
                else:
                    left += q[2]
            else:
                if not flipped:
                    right += q[2]
                else:
                    left += q[2]
    le = list(left)
    le.reverse()
    left = "".join(le)
    ans = left + right
    if flipped:
        l = list(ans)
        l.reverse()
        ans = "".join(l)
    return ans
        

def main():
    S = str(input())
    Q = int(input())
    queries = []
    for i in range(Q):
        queries.append(list(map(str, input().split())))
    print(Main(S, Q, queries))

if __name__ == '__main__':
    main()

Solution (Python):
from collections import deque

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

sign = 1
head = ""
tail = ""
for i in range(Q):
    Q = input().split()
    if Q[0] == "1":
        sign *= -1
    else:
        if int(Q[1]) * sign == 1:
            S.appendleft(Q[2])
        elif int(Q[1]) * sign == -1:
            S.append(Q[2])
        elif int(Q[1]) * sign == 2:
            S.append(Q[2])
        elif int(Q[1]) * sign == -2:
            S.appendleft(Q[2])

if sign==1:
    print("".join(S))
elif sign==-1:
    S.reverse()
    print("".join(S))

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

query_list = []
for _ in range(q):
    query_list += [input()]


def query1(s):
    s = s[::-1]
    return s

def query2_1(letters,s):
    s = letters[4] + s
    return s

def query2_2(letters,s):
    s = s +  letters[4]
    return s

true_position = True
left = ""
right = ""

for i in range(q):
    if len(query_list[i])==1:
        true_position = not true_position
        
    else:
        if query_list[i][2]=="1":
            if true_position:
                left = query_list[i][4] + left
            else:
                right = right + query_list[i][4]
        elif query_list[i][2]=="2":
            if true_position:
                right = right + query_list[i][4]
            else:
                left = query_list[i][4] + left

if true_position:
    ans = left + s + right
else:
    ans = right[::-1]+s[::-1]+left[::-1]

print(ans)

Solution (Python):
S = input()
Q = int(input())
front = ""
flag = 1
for i in range(Q):
    q =  [ i for i in input().split(' ')]
    if q[0] == '1':
        flag = flag * -1
    else:
        if flag == 1: 
            if q[1] == '1':
                front+=q[2]
            else:
                S += q[2]
        else:
            if q[1] == '1':
                S +=q[2]
            else:
                front+= q[2]
            
if flag ==1:
    print(front[::-1] + S)
else:
    print(S[::-1] + front)

Solution (Python):
from collections import deque

s = deque(input())
q = int(input())
query = [input().split() for _ in range(q)]

reverse = False

for i in range(q):
    now = query[i]
    if now == ['1']:
        reverse = not reverse
    else:
        f = now[1]
        c = now[2]
        if f == '1':
            s.append(c) if reverse else s.appendleft(c)
        else:
            s.appendleft(c) if reverse else s.append(c)

if reverse:
    s.reverse()

print(''.join(s))

Solution (Python):
from collections import deque
S = deque(input())
Q = int(input())
querry = [list(map(str, input().split())) for i in range(Q)]
reverse = False
for i in range(Q):
    if querry[i][0] == '1':
        reverse = not reverse
    elif querry[i][0] == '2':
        if querry[i][1] == '1':
            if reverse:
                S.append(querry[i][2])
            else:
                S.appendleft(querry[i][2])
        elif querry[i][1] == '2':
            if reverse:
                S.appendleft(querry[i][2])
            else:
                S.append(querry[i][2])

ans=list(S)

if reverse:
    print("".join(reversed(ans)))
else:
    print("".join(ans))

Solution (Python):
from collections import deque

s = input()
n = int(input())
ans = deque(s)

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

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

Solution (Python):
from collections import deque


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


reverse_count = 0
for i in range(q):
  y = input().split()
  if y[0] == '1':
    
    reverse_count += 1
    continue
  elif y[0] == '2':
    if y[1] == '1':
      
      if reverse_count % 2 == 1:
        s.append(y[2])
      else:
        s.appendleft(y[2])
    elif y[1] == '2':
      
      if reverse_count % 2 == 1:
        s.appendleft(y[2])
      else:
        s.append(y[2])


if reverse_count % 2 == 0:
  
  print("".join(s))
else:
  s.reverse()
  print("".join(s))

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

S = input().rstrip()
Q = int(input())
head = ''
tail = ''
rev = False

for _ in range(Q):
    query = list(input().split())
    if query[0] == '1':
        if rev:
            rev = False
        else:
            rev = True
    elif query[0] == '2':
        f = query[1]
        c = query[2].rstrip()
        if f == '1':
            if rev:
                tail = tail + c
            else:
                head = c + head
        else:
            if rev:
                head = c + head
            else:
                tail = tail + c
if rev:
    tmp = head + S + tail
    print(tmp[::-1])
else:
    print(head + S + tail)

Solution (Python):
def main():
  w = input()
  s = collections.deque(w)
  n = int(input())
  
  reverse_count = 0
  for i in range(n):
    t = list(input_list_str())
    if len(t) == 1:
      reverse_count = 1 - reverse_count
      continue
    q, f, c = t
    if int(f) + reverse_count == 2:
      s.append(c)
    else:
      s.appendleft(c)
  if reverse_count == 1:
    s.reverse()
  print(''.join(s))

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

def input_list_str():
  return map(str, input().split())

def lcm_base(x, y):
    return (x * y) // fractions.gcd(x, y)

def lcm_list(numbers):
    return reduce(lcm_base, numbers, 1)

def gcd(*numbers):
    return reduce(fractions.gcd, numbers)

def gcd_list(numbers):
    return reduce(fractions.gcd, numbers)

import math
import fractions
import collections
from functools import reduce
main()

Solution (Python):
import sys; sys.setrecursionlimit(2147483647); input = sys.stdin.readline
from math import floor, ceil, sqrt, factorial, log
from collections import Counter, defaultdict, deque
from operator import itemgetter
INF = float('inf'); MOD = 10**9+7
def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(MI())
def LIR(n): return [LI() for i in range(n)]
def S(): return input().rstrip()
def LIR(n): return [S().split() for i in range(n)]

def main():
    s = S(); Q = I()
    mode = 2
    d = deque(s)

    for i in range(Q):
        query = S()
        if query[0] == "1": mode = 3 - mode
        else:
            t, f, c = map(str, query.split())
            if (int(f) + mode)% 2  == 0: d.append(c)
            else: d.appendleft(c)
    if mode == 2: print(''.join(d))
    else: print(''.join(list(d)[::-1]))

if __name__ == '__main__':
    main()

Solution (Python):
s=input()
a=[]
b=[]
now=True
q=int(input())
for i in range(q):
    x=list(input().split())
    if x[0]=="1":
        now=not now
    else:
        if now and x[1]=="2" or not now and x[1]=="1":
            b.append(x[2])
        else:
            a.append(x[2])
if now:
    ans=a[::-1]+list(s)+b
else:
    ans=b[::-1]+list(s[::-1])+a
print(*ans,sep="")

Solution (Python):
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
from collections import deque

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




    













swap = 1
head = deque()
tail = deque()
for _ in range(Q):
    Query = list(input().split())

    if Query[0] == "1":
        swap *= -1
    
    else:
        F, C = Query[1], Query[2]
        if F == "1":
            if swap == 1:
                head.appendleft(C)
            else:
                tail.append(C)
        else:
            if swap == -1:
                head.appendleft(C)
            else:
                tail.append(C)         


print(("".join(head) + S + "".join(tail))[::swap])

Solution (Python):
s = input()
q = int(input())
string = []
is_normal = 1
saq = ""
sol = ""
for i in range(q):
    emir = input()
    if emir[0] == '1':
        is_normal *= -1
    elif emir[0] == '2':
        if is_normal == 1:
            if emir[2] == '1':
                sol += emir[4]
            elif emir[2] == '2':
                saq += emir[4]
        else:
            if emir[2] == '1':
                saq += emir[4]
            elif emir[2] == '2':
                sol += emir[4]
s = sol[::-1] + s + saq
if is_normal == -1:
    s = s[::-1]

print(s)

Solution (Python):
from collections import deque

S = input()
Q = int(input())
ans = deque(S)
revcnt = 0
for i in range(Q):
    data = list(input().split())
    if data[0] == '1':
        revcnt += 1
        continue

    if data[1] == '1':
        if revcnt % 2 == 0:
            ans.appendleft(data[2])
        else:
            ans.append(data[2])
    else:
        if revcnt % 2 == 0:
            ans.append(data[2])
        else:
            ans.appendleft(data[2])

if revcnt % 2 != 0:
    print(''.join(reversed(list(ans))))
else:
    print(''.join(list(ans)))

Solution (Python):
s = str(input())
n = int(input())
q_list = [list(map(str, input().strip().split())) for i in range(n)]

rev = 0
for_s = ''
rev_s = ''

out = ''
for q in q_list:
    if q[0] == '1':
        rev = 1 - rev
    elif q[1] == '1':
        if rev == 0:
            rev_s += q[2]
        else:
            for_s += q[2]
    else:
        if rev == 0:
            for_s += q[2]
        else:
            rev_s += q[2]

if rev == 0:
    out = rev_s[::-1] + s + for_s
else:
    out = for_s[::-1] + s[::-1] + rev_s
print(out)

Solution (Python):
from collections import deque

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

d = deque(S)
head = "l"

for _ in range(Q):
    q = input()
    if len(q) == 1:
        if head == "l":
            head = "r"
        else:
            head = "l"
    else:
        T, F, C = q.split()
        if int(F) == 1:
            if head == "l":
                d.appendleft(C)
            else:
                d.append(C)
        else:
            if head == "l":
                d.append(C)
            else:
                d.appendleft(C)
if head == "l":
    print("".join(list(d)))
else:
    print("".join(list(d)[::-1]))

Solution (Python):
S = input()
N = int(input())
rev_flg=False
head = []
tail = []
for i in range(N):
	Q = input().split()
	if Q[0] == "1":
		if not rev_flg:
			rev_flg = True
		else:
			rev_flg = False
	elif Q[0] == "2":
		if Q[1] == "1":
			if not rev_flg:
				head.append(Q[2])
			else:
				tail.append(Q[2])
		if Q[1] == "2":
			if not rev_flg:
				tail.append(Q[2])
			else:
				head.append(Q[2])
S = "".join(head[::-1])+S+"".join(tail)


if rev_flg:
	S =S[::-1]
print(S)

Solution (Python):

 
s = list(input())
Q = int(input())
 
reverse = False
leading = []
trailing = []
 
for q in range(Q):
    query = input().split()
 
    if len(query) == 1:
        reverse = not reverse
    else:
        direction = int(query[1])
        if (direction == 1 and not reverse) or (direction == 2 and reverse):
            leading.append(query[2])
        else:
            trailing.append(query[2])
 
if reverse:
    print(''.join((leading[::-1] + s + trailing)[::-1]))
else:
    print(''.join(leading[::-1] + s + trailing))

Solution (Python):
s = list(input().strip())
q = int(input().strip())
queries = []
one_count = 0
one_counts = []
for i in range(q):
    inp = input().split()
    queries.append(inp)
    if inp[0] == "1":
        one_count += 1
    one_counts.append(one_count)
one_counts = list(map(lambda x: (one_count - x), one_counts))
if one_count % 2 == 1:
    s = s[::-1]
pre = []
suf = []
for i in range(q):
    if queries[i][0] == "1":
        continue
    elif queries[i][0] == "2":
        if queries[i][1] == "1":
            if one_counts[i] % 2 == 0:
                pre.append(queries[i][2])
            else:
                suf.append(queries[i][2])
        elif queries[i][1] == "2":
            if one_counts[i] % 2 == 0:
                suf.append(queries[i][2])
            else:
                pre.append(queries[i][2])
print("".join(pre[::-1] + s + suf))

Solution (Python):
from collections import deque

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

d = deque(S)
head = "l"

for _ in range(Q):
    q = input()
    if len(q) == 1:
        if head == "l":
            head = "r"
        else:
            head = "l"
    else:
        T, F, C = q.split()
        if int(F) == 1:
            if head == "l":
                d.appendleft(C)
            else:
                d.append(C)
        else:
            if head == "l":
                d.append(C)
            else:
                d.appendleft(C)
if head == "l":
    print("".join(list(d)))
else:
    print("".join(list(d)[::-1]))

Solution (Python):
from collections import deque

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

S = deque(S)

Query = [list(input()) for i in range(Q)]

rev_cnt = 0

for i in range(Q):
    if Query[i][0] == "1":
        rev_cnt += 1
    else:
        if Query[i][2] == "1":
            if rev_cnt % 2 == 1:
                S.append(Query[i][4])
            else:
                S.appendleft(Query[i][4])
        else:
            if rev_cnt % 2 == 1:
                S.appendleft(Query[i][4])
            else:
                S.append(Query[i][4])

if rev_cnt % 2 == 1:
    while len(S) != 0:
        print(S.pop(), end="")
else:
    while len(S) != 0:
        print(S.popleft(), end="")


print()

Solution (Python):

import sys
from collections import deque

ans = str(input())
n = int(input())
r = 2
be=deque()
af=deque()

for i in range(n) :
    x = input().split(" ")
    if int(x[0]) == 1 :
        r = r + 1
    else :
        pp = int(x[1])
        if r % 2 == 1 :
            pp = pp + 1
        if pp % 2  == 1 :
            be.appendleft(str(x[2]))
        else :
            af.append(str(x[2]))

ans = ''.join(be) + ans + ''.join(af)
if r % 2 == 1 :
    ans = ans[::-1]

print(ans)

Solution (Python):
from collections import deque

S = input()
Q = int(input())
ans = deque(S)
reverse = False

for _ in range(Q):
    query = input().split()
    if query[0] == "1":
        reverse = not reverse
    if query[0] == "2":
        F, C = query[1:]
        if (F == "1" and not reverse) or (F == "2" and reverse):
            ans.appendleft(C)
        else:
            ans.append(C)

if reverse:
    print("".join(reversed(ans)))
else:
    print("".join(ans))

Solution (Python):
from collections import deque
texts = deque(str(input()))
Q = int(input())


reverse = False
start_text = deque('')
end_text = deque('')
input_texts = []
for _ in range(Q):
    input_texts.append(list(input().split()))

for input_text in input_texts:
    if len(input_text) == 1:
        if reverse:
            reverse = False
        else:
            reverse = True
    else:
        if input_text[1] == '1':  
            if reverse:
                end_text.append(str(input_text[2]))
            else:
                start_text.appendleft(str(input_text[2]))
        else:  
            if reverse:
                start_text.appendleft(input_text[2])
            else:
                end_text.append(input_text[2])




texts.appendleft(''.join(start_text))
texts.append(''.join(end_text))



if reverse:
    texts = deque("".join(texts))
    texts.reverse()
    print("".join(texts))
else:
    
    print(''.join(texts))

Solution (Python):
import sys

stdin = sys.stdin

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

def main():
    s = ns()
    Q = ni()

    T1 = 0
    FrontS = ""
    BackS = ""
    for _ in range(Q):
        T = stdin.readline().split()
        if T[0] == "1":
            FrontS, BackS = BackS, FrontS
            T1 += 1
        else:
            if T[1] == "1":
                FrontS += T[2]
            else:
                BackS += T[2]
                
    FrontS = FrontS[::-1]

    if T1 % 2 == 0:
        print(FrontS + s + BackS)
    else:
        print(FrontS + s[::-1] + BackS)

if __name__ == '__main__':
    main()

Solution (Python):


s = list(input())
Q = int(input())

reverse = False
leading = []
trailing = []

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

    if len(query) == 1:
        reverse = not reverse
    else:
        direction = int(query[1])
        if (direction == 1 and not reverse) or (direction == 2 and reverse):
            leading.append(query[2])
        else:
            trailing.append(query[2])

if reverse:
	print(''.join((leading[::-1] + s + trailing)[::-1]))
else:
	print(''.join(leading[::-1] + s + trailing))

Solution (Python):
from sys import stdin

def main():

    input = stdin.readline

    S = input()[:-1]
    Q = int(input())
    
    query = [0] * Q
    ans_list = []

    reverse_flag = False

    mae = []
    ushiro = []

    for i in range(Q):
        query = input().split()
        if(query[0] == '1'):
            reverse_flag = not reverse_flag
        else:
            if reverse_flag is True:
                if(query[1] == '1'):
                    ushiro.append(query[2])
                else:
                    mae.append(query[2])                
            else:
                if(query[1] == '1'):
                    mae.append(query[2])
                else:
                    ushiro.append(query[2])
    
    ans_list = "".join(mae[::-1]) + S + "".join(ushiro)
    
    if(reverse_flag is not True):
        print(''.join(ans_list))
    else:
        print(''.join(ans_list[::-1]))


if __name__ == "__main__":
    main()

Solution (Python):


T = 0
F = 2
C = 4

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

cnt = 0

front = ""
back = ""

for i in range(q):
    query = input()

    if query[T] == "1":
        cnt += 1
        continue

    
    if cnt % 2 == 0:
        if query[F] == "1":
            front +=  query[C]
        else:
            back += query[C]

    
    else:
        if query[F] == "1":
            back += query[C]
        else:
            front +=  query[C]









if cnt % 2 == 0:
    print(front[::-1]+s+back)
    pass
else:
    print(back[::-1]+s[::-1]+front)

Solution (Python):
def main():
  from collections import deque
  import sys
  input = sys.stdin.readline
  s=input().rstrip()
  q=int(input().rstrip())
  turn = 2
  d=deque(s)

  for _ in [0]*q:
      query = input().rstrip()
      if query[0] == "1":
          t = int(query)
          turn = 3 - turn
      else:
          t,f,c = map(str,query.split())
          if (int(f) + turn)% 2  == 0:
              d.append(c)
          else:
              d.appendleft(c)
  if turn == 2:
      print("".join(d))
  else:
      print("".join(list(d)[::-1]))
main()

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

ap = ["", ""]
revCount = 0

for j in q1:
  if j[0] == "1":
    revCount = (revCount+1) % 2

  else:
    if revCount == 0:
      ap[int(j[1]) - 1] = ap[int(j[1]) - 1] + j[2]
    else:
      ap[int(j[1]) - 2] = ap[int(j[1]) - 2] + j[2]
        

if revCount == 0:
  ap[0] = ap[0][::-1]
  print(ap[0] + s + ap[1])

else:
  aps = (s+ap[1])[::-1]
  print(aps + ap[0])