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


is_reverse = False
pre_s = ""
suf_s = ""

for i in range(q):
  query = input()
  if query == '1':
    is_reverse = not is_reverse
  else:
    query = query.split()
    f, c = int(query[1]), query[2]
    if f == 1:
      if is_reverse:
        suf_s = suf_s + c
      else:
        pre_s = c + pre_s
    else:
      if is_reverse:
        pre_s = c + pre_s
      else:
        suf_s = suf_s + c

if is_reverse:
  print(suf_s[::-1] + s[::-1] + pre_s[::-1])
else:
  print(pre_s + s + suf_s)

Solution (Python):
from collections import deque

d = deque()
inversion = 1

s = input()

for i in range(len(s)):
    d.append(s[i])

q = int(input())

for i in range(q):
    qn = list(input().split())
    if qn[0] == "1":
        inversion *= -1
    elif qn[0] == "2":
        if qn[1] == "1":
            if inversion == 1:
                d.appendleft(qn[2])
            elif inversion == -1:
                d.append(qn[2])
        if qn[1] == "2":
            if inversion == 1:
                d.append(qn[2])
            elif inversion == -1:
                d.appendleft(qn[2])

for i in range(len(d)):
    if inversion == 1:
        print(d.popleft(), end="")
    if inversion == -1:
        print(d.pop(), end="")

print()

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

Q = Q[::-1]

cnt = 0
left = []
right = []
for x in range(q):
    if len(Q[x]) == 1:
        cnt += 1
    else:
        if (int(Q[x][1]) + cnt) % 2 == 0:
            right.append(Q[x][2])
        else:
            left.append(Q[x][2])



for l in left:
    print(l, end="")
if cnt % 2 == 0:
    for s in S:
        print(s, end="")
else:
    for s in S[::-1]:
        print(s, end="")
for r in right[::-1]:
    print(r, end="")

print()

Solution (Python):
S = input()
Q = int(input())
Query = [list(map(str,input().split())) for i in range(Q)]
front = ""
back = ""
hanten_flag = 0
cnt = 0
for i in range(Q):
    if(Query[i][0] == "1"):
        hanten_flag ^= 1
    else:
        if(Query[i][1] == "1" and hanten_flag == 0):
            front = Query[i][2] + front
        elif(Query[i][1] == "1" and hanten_flag == 1):
            back = back + Query[i][2]
        elif(Query[i][1] == "2" and hanten_flag == 0):
            back = back + Query[i][2]
        else:
            front = Query[i][2] + front
SS = front + S + back
if(hanten_flag == 1):
    SS = SS[::-1]
print(SS)

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

T = [None] * Q
F = [None] * Q
C = [None] * Q
for i in range(Q):
    query = input().split()
    T[i] = int(query[0])
    if len(query) == 3:
        F[i] = int(query[1])
        C[i] = query[2]

ans = deque([S])
cnt1 = 0  
for i in range(Q):
    if T[i] == 1:
        cnt1 = (cnt1 + 1) % 2
    elif F[i] == 1:  
        if cnt1 == 0:  
            ans.appendleft(C[i])
        else:   
            ans.append(C[i][::-1])
    else:  
        if cnt1 == 0: 
            ans.append(C[i])
        else:
            ans.appendleft(C[i][::-1])

ans = ''.join(ans)

if cnt1 == 1:
    ans = ans[::-1]

print(ans)

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

reverse_flag = False
s_head = list()

for i in range(n):
    line = list(map(str, input().split()))
    if line[0] == '1':
        if reverse_flag:
            reverse_flag = False
        else:
            reverse_flag = True
    elif line[0] == '2':
        if line[1] == '1':
            if reverse_flag:
                s.append(line[2])
            else:
                s_head.append(line[2])
        elif line[1] == '2':
            if reverse_flag:
                s_head.append(line[2])
            else:
                s.append(line[2])

if reverse_flag:
    ans = s[::-1]
    ans.extend(s_head)
    print(''.join(ans))
else:
    ans = s_head[::-1]
    ans.extend(s)
    print(''.join(ans))

Solution (Python):
from collections import deque
s = deque(list(input()))
q = int(input())
que = [list(input().split()) for _ in range(q)]
counter = 0
for i in que:
    if i[0] == "1":
        counter += 1
    else:
        if (counter+int(i[1]))%2==1:
            s.appendleft(i[-1])
        else:
            s.append(i[-1])
if counter%2==0:
    print("".join(list(s)))
else:
    print("".join(list(s)[::-1]))

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

Solution (Python):
from collections import deque

s = input()
N = int(input())

S = []
for i in range(len(s)):
    S.append(s[i])

d = deque(S)
    
back_count = 0
for i in range(N):
    Q = list(map(str, input().split()))
    Q[0] = int(Q[0])
    if Q[0] == 1:
        if back_count == 0:
            back_count = 1
        else:
            back_count = 0
    else:
        Q[1] = int(Q[1])
        if back_count == 0:
            if Q[1] == 1:
                d.appendleft(Q[2])
            else:
                d.append(Q[2])
        else:
            if Q[1] == 1:
                d.append(Q[2])
            else:
                d.appendleft(Q[2])

S = list(d)
if back_count == 0:
    print("".join(S))
else:
    S = S[::-1]
    print("".join(S))

Solution (Python):
s  = str(input())
t = ""
u = ""
q = int(input())
count_inv = 0
for i in range(q):
  inlis = list(map(str,input().split())) + [0,0]
  if inlis[0] ==  "1":
    count_inv += 1
    count_inv %= 2
  if inlis[0] == "2":
    if inlis[1]  == "1":
      if count_inv == 0:
        u += str(inlis[2])
      else:
        t += str(inlis[2])
      
    if inlis[1] == "2":
      if count_inv == 0:
        t += str(inlis[2])
      else:
        u += str(inlis[2])
      
  
if count_inv == 0:
  u = u[::-1]
  ans = u+s+t
else:
  t = t[::-1]
  s = s[::-1]
  ans = t+s+u
print(ans)

Solution (Python):

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


t = True
hlist = []
flist = []
for q in Query:
    if q == "1":
        t = not t
    else:
        _, f, c = q.split()
        if f == "1":
            if t:
                hlist.append(c)
            else:
                flist.append(c)
        else:
            if t:
                flist.append(c)
            else:
                hlist.append(c)


ans = "".join(hlist[::-1]) + S + "".join(flist)
if t:
    print(ans)
else:
    print("".join(list(ans)[::-1]))

Solution (Python):
def inp():
    return input()
def iinp():
    return int(input())
def inps():
    return input().split()
def miinps():
    return map(int,input().split())
def linps():
    return list(input().split())
def lmiinps():
    return list(map(int,input().split()))
def lmiinpsf(n):
    return [list(map(int,input().split()))for _ in range(n)]

s = list(inp())
q = iinp()

l = []

p = 0
o = 1

for i in range(q):
  a = inps()
  if o == 1 and a[0] == 1:
    continue
  if a[0] == "2":
    if a[1] == "1":
      if p == 0:
        l.append(a[2])
        o += 1
      else:
        s.append(a[2])
        o += 1
    else:
      if p == 0:
        s.append(a[2])
        o += 1
      else:
        l.append(a[2])
        o += 1
  if a[0] == "1":
    if p == 0:
      p = 1
    else:
      p = 0

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

Solution (Python):
def main():
  S = input()
  Q = int(input())
  import sys
  que = [list(sys.stdin.readline().split()) for _ in range(Q)]
  pre=''
  post=''
  judge = False
  for l in que:
    if int(l[0])==1:
      if judge:
        judge = False
      else:
        judge = True

    elif int(l[0])==2:
      f,c = int(l[1]),l[2]
      if judge:
        if f==1:
          f=2
        elif f==2:
          f=1
      if f==1:
        pre = ''.join([pre,c])
      elif f==2:
        post = ''.join([post,c])

  pre = pre[::-1]
  S = pre+S+post
  if judge:
    S=S[::-1]
  print(S)
  
main()

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

from collections import deque
d = deque(s)

r = 1

for i in range(q):
  inp = input().split()
  if len(inp)==1:
    r = r*-1
  else:
    f = int(inp[1])
    if f==1:
      f=1
    else:
      f=-1
    if f*r==1:
      d.appendleft(inp[2])
    else:
      d.append(inp[2])
    

if r==1:
  for i in range(len(d)):
    print(d.popleft(), end='')
else:
  for i in range(len(d)):
    print(d.pop(), end='')

Solution (Python):
from collections import deque


def main():
    S = list(input())
    dec_s = deque(S)
    flag = True  
    for _ in range(int(input())):
        t = list(input().split(' '))
        if len(t) == 1:
            flag = not flag
        else:
            if (t[1] == '1' and flag) or (t[1] == '2' and not flag):
                dec_s.appendleft(t[2])
            else:
                dec_s.append(t[2])
    if not flag: dec_s.reverse()
    print(''.join(dec_s))


if __name__ == '__main__':
    main()

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

for i in range(q):
    query=input()
    if query[0]=='1':
        direction=direction^1
    elif query[0]=='2':
        i,f,c=query.split()
        if f=='1' and direction==0:
            s.appendleft(c)
        elif f=='1' and direction==1:
            s.append(c)
        elif f=='2' and direction==0:
            s.append(c)
        elif f=='2' and direction==1:
            s.appendleft(c)


if direction==0:
    print(''.join(s))
else:
    x=[]
    for i in range(len(s)):
        x.append(s[-1-i])
    print(''.join(x))

Solution (Python):

import re
import math
import itertools
from copy import deepcopy
import fractions
import random
from functools import lru_cache
from heapq import heappop,heappush
import time
import sys
input=readline = sys.stdin.readline
sys.setrecursionlimit(2000)

alphabet="abcdefghijklmnopqrstuvwxyz"
mod=int(10**9+7)
inf=int(10**20)
def yn(b):
    if b:
        print("yes")
    else:
        print("no")
def Yn(b):
    if b:
        print("Yes")
    else:
        print("No")
def YN(b):
    if b:
        print("YES")
    else:
        print("NO")
class union_find():
    def __init__(self,n):
        self.n=n
        self.P=[a for a in range(N)]
        self.rank=[0]*n
 
    def find(self,x):
        if(x!=self.P[x]):self.P[x]=self.find(self.P[x])
        return self.P[x]
 
    def same(self,x,y):
        return self.find(x)==self.find(y)
 
    def link(self,x,y):
        if self.rank[x]<self.rank[y]:
            self.P[x]=y
        elif self.rank[y]<self.rank[x]:
            self.P[y]=x
        else:
            self.P[x]=y
            self.rank[y]+=1
 
    def unite(self,x,y):
        self.link(self.find(x),self.find(y))
 
    def size(self):
        S=set()
        for a in range(self.n):
            S.add(self.find(a))
        return len(S)
def bin_(num,size):
    A=[0]*size
    for a in range(size):
        if (num>>(size-a-1))&1==1:
            A[a]=1
        else:
            A[a]=0
    return A
def fac_list(n,mod_=0):
    A=[1]*(n+1)
    for a in range(2,len(A)):
        A[a]=A[a-1]*a
        if(mod>0):A[a]%=mod_
    return A
def comb(n,r,mod,fac):
    if(n-r<0):return 0
    return (fac[n]*pow(fac[n-r],mod-2,mod)*pow(fac[r],mod-2,mod))%mod
def next_comb(num,size):
    x=num&(-num)
    y=num+x
    z=num&(~y)
    z//=x
    z=z>>1
    num=(y|z)
    if(num>=(1<<size)):return False
    else:
        return num
def get_primes(n,type="int"):
    A=[True]*(n+1)
    A[0]=False
    A[1]=False
    for a in range(2,n+1):
        if A[a]:
            for b in range(a*2,n+1,a):
                A[b]=False
    if(type=="bool"):return A
    B=[]
    for a in range(n+1):
        if(A[a]):B.append(a)
    return B
def is_prime(num):
    if(num<=2):return False
    i=2
    while i*i<=num:
       if(num%i==0):return False
       i+=1
    return True
def join(A,c=" "):
    n=len(A)
    A=list(map(str,A))
    s=""
    for a in range(n):
        s+=A[a]
        if(a<n-1):s+=c
    return s


s=input()
Q=int(input())
l=""
r=""
count=0
for a in range(Q):
    tmp=input().split()
    if tmp[0]=="1":
        count+=1
    else:
        if tmp[1]=="1":
            if(count%2==0):l=tmp[2]+""+l
            else:r+=tmp[2]
        else:
            if(count%2==0):r+=tmp[2]
            else:l=tmp[2]+""+l
s=l+s+r
s=s.replace("\n","")
if(count%2==1):
    s=s[::-1]
print(s)

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

before = [0] * (2 * (10**5))
after = [0] * (2 * (10**5))

count1 = True
before_count = 0
after_count = 0


for i in range(Q):
    q = input().split()
    if int(q[0]) == 1:
        if count1:
            count1 = False
        else:
            count1 = True
    else:
        if int(q[1]) == 1:
            if count1:
                before[before_count] = q[2]
                before_count += 1
            else:
                after[after_count] = q[2]
                after_count += 1
        else:
            if count1:
                after[after_count] = q[2]
                after_count += 1
            else:
                before[before_count] = q[2]
                before_count += 1


for i in range(len(before)):
    if before[i] == 0:
        before = before[:i]
        break

for i in range(len(after)):
    if after[i] == 0:
        after = after[:i]
        break

before = ''.join(before)
after = ''.join(after)

before = before[::-1]
if count1:
    middle = S
    ans = before + middle + after
else:
    middle = S
    ans = before + middle + after
    ans = ans[::-1]

print(ans)

Solution (Python):
from collections import deque

d = deque(input())
q = int(input())
reverse_flag = False

for i in range(q):
  query = input().split()
  if query[0] == "1":
    reverse_flag = not reverse_flag
  else:
    if (query[1] == "1") ^ reverse_flag:
      d.appendleft(query[2])
    else:
      d.append(query[2])

print("".join(list(d)[::-1]) if reverse_flag else "".join(d))

Solution (Python):
s = input()
Q = int(input())
reverse = False
top = []
bottom = []
for _ in range(Q):
    query = list(map(str, input().split()))
    if len(query) == 1:
        reverse = not reverse
    else:
        if (reverse and query[1] == "1") or (not reverse and query[1] == "2"):
            bottom.append(query[2])
        else:
            top.append(query[2])

ans = "".join(reversed(top)) + s + "".join(bottom)
if reverse:
    print(ans[::-1])
else:
    print(ans)

Solution (Python):
from collections import deque

S = input()
Q = int(input())
d = deque()
for i in S:
    d.append(i)
flag = True
for _ in range(Q):
    q = [i for i in input().split()]
    if int(q[0]) == 1:
        flag = not flag
    else:
        if (int(q[1]) == 1 and flag) or (int(q[1]) == 2 and not flag):
            d.appendleft(q[2])
        else:
            d.append(q[2])
S = ""
while(len(d)>0):
    if flag:
        S += d.popleft()
    else:
        S += d.pop()
print(S)

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

count_rev = 0
state = True
front_s = []
for i in range(q):
    input_list = input().split()
    t = int(input_list[0])
    if t == 1:
        state = not state
        count_rev += 1
    elif t == 2:
        f = int(input_list[1])
        c = input_list[2]
        if f == 1 and state:
            front_s.append(c)
        elif f == 1 and not state:
            s.append(c)
        elif f == 2 and state:
            s.append(c)
        elif f == 2 and not state:
            front_s.append(c)

front_s.reverse()
new_s = front_s + s
if not state:
    new_s.reverse()
print(''.join(new_s))

Solution (Python):
import sys
import math
import copy
import queue
input = sys.stdin.readline
def LI(): return [int(x) for x in input().split()]
INF = 10**15
DD = ((1,0),(0,1),(-1,0),(0,-1))

S = [x for x in input().rstrip()]
n = int(input())

a = [[],[]]
d = 0
for _ in range(n):
    q = list(input().split())
    if q[0] == '1':
        d = 1-d
    else:
        if q[1] == '1':
            a[d].append(q[2])
        else:
            a[1-d].append(q[2])

if d == 1:
    a[1].reverse()
    S.reverse()
    S = ''.join(a[1]) + ''.join(S) + ''.join(a[0])
else:
    a[0].reverse()
    S = ''.join(a[0]) + ''.join(S) + ''.join(a[1])
print (S)

Solution (Python):
from collections import deque

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

dq = deque(s)
rev = False
fcond = {"1": True, "2": False}
rcond = {True: False, False: True}
for i in range(q):
    t = input()
    if t.startswith("1"):
        rev = rcond[rev]
    elif t.startswith("2"):
        _, f, c = t.split()
        if fcond[f] is not rev:
            dq.appendleft(c)
        else:
            dq.append(c)
if rev:
    print("".join(reversed(dq)))
else:
    print("".join(dq))

Solution (Python):
s = [_ for _ in input()]
qn = int(input())
rev = False
prevs = ['']*200000
nexts = ['']*200000
previ = 0
nexti = 0

for _ in range(qn):
  q = [_ for _ in input().split()]
  if q[0] == '1':
    rev = not rev
  else:
    if (rev and q[1] == '2') or (not rev and q[1] == '1'):
      prevs[previ] = q[2]
      previ += 1
    else:
      nexts[nexti] = q[2]
      nexti += 1

prevs.reverse()
s = prevs + s + nexts
if rev:
  s.reverse()
print("".join(s))

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

forward = []
backend = []
reverse = 0

for i in range(Q):
        s = input()
        s1 = list(s)

        if s1[0] == '1':
            reverse += 1
        else:
            if s1[2] == '1':
                if reverse % 2 == 0:
                    forward.append(s1[4])
                else:
                    backend.append(s1[4])
            else:
                if reverse % 2 == 0:
                    backend.append(s1[4])
                else:
                    forward.append(s1[4])

if reverse % 2 == 0:
    for i in forward[::-1]:
        print(i, end="")
    
    print(S,end="")

    for i in backend:
        print(i,end="")

    print()
else:
    for i in backend[::-1]:
        print(i,end="")
    
    for i in list(S)[::-1]:
        print(i,end="")

    for i in forward:
        print(i,end="")

    print()

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

rev = 0
head = ""
foot = ""
for i in range(Q) :
    query = input()
    if query == "1" :
        rev += 1
    else :
        a, F, C = query.split()
        if F == "1" and rev%2 == 0:
            head = C + head
        elif F == "1" and rev%2 == 1:
            foot = foot + C
        elif F == "2" and rev%2 == 0:
            foot = foot + C
        else :
            head = C + head

if rev%2 == 0 :
    print("".join(list(head + S + foot)))
else :
    print("".join(list(reversed(head + S + foot))))

Solution (Python):
from collections import deque
S=deque(input())
Q=int(input())
snt=1
for i in range(Q):
    inp=input().split()
    if inp[0]=="1":
        snt*=(-1)
    else:
        F=int(inp[1])
        C=inp[2]
        if (2*F-3)*snt>0:
            
            S.append(C)
        else:
            
            S.appendleft(C)
if snt==1:
    
    for i in range(len(S)):
        print(S.popleft(),end="")
    print()
else:
    for i in range(len(S)):
        
        print(S.pop(),end="")
    print()

Solution (Python):
from collections import deque
s = input()
que = deque(list(s))
q = int(input())
flag = True
for _ in range(q):
    l = input()
    if flag:
        if l[0] == "1":
            flag = False
        else:
            if l[2] == "1":
                que.appendleft(l[4])
            else:
                que.append(l[4])
    else:
        if l[0] == "1":
            flag = True
        else:
            if l[2] == "1":
                que.append(l[4])
            else:
                que.appendleft(l[4])
if flag:
    ans = list(que)
    print("".join(ans))
else:
    ans = list(que)[::-1]
    print("".join(ans))

Solution (Python):
S = str(input())
Q = int(input())
st = [0] * Q
n = [0] * Q
p = 0
s_h = 0
for i in range(Q):
  q = str(input())
  if int(q[0]) == 1:
    s_h += 1
  else:
    if int(q[2]) == 1:
      st[p] = q[4]
      n[p] = s_h
      p += 1
    else:
      st[p] = q[4]
      n[p] = s_h + 1
      p += 1

odd = ["".join(st[a]) for a in range(Q) if ((n[a] + s_h) % 2 == 1) and (str(st[a]) != "0")]
odd = "".join(odd)
even =  ["".join(st[a]) for a in range(Q) if ((n[a] + s_h) % 2 == 0) and (str(st[a]) != "0")]
even = "".join(even)[::-1]
if s_h % 2 == 1:
  S = S[::-1]

b = [even, S, odd]

print("".join(b))

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

rev = False

forward = ''
back = ''

for i in range(q):
    tfc = input()
    if len(tfc) == 1:
        if rev:
            rev = False
        else:
            rev = True
    else:
        t, f, c = tfc.split()
        if f == '1':
            if rev:
                back += c
            else:
                forward += c
        else:
            if rev:
                forward += c
            else:
                back += c

s = forward[::-1] + s + back
if rev:
    s = s[::-1]

print(s)

Solution (Python):
import sys
import math
MAX_INT = int(10e15)
MIN_INT = -MAX_INT
mod = 1000000007
sys.setrecursionlimit(1000000)
def IL(): return list(map(int,input().split()))
def SL(): return input().split()
def I(): return int(sys.stdin.readline())
def S(): return input()

s = S()
q = I()
que = [S() for i in range(q)]


st = []
en = []
f = 0
for i in range(q):
  n = que[i]
  if n == "1":
    f += 1
  else:
    t,ff,c = list(n.split())
    if (f+int(ff))%2 == 0:
      st.append(c)
    else:
      en.append(c)




if f%2 == 0:
  print("".join(en[::-1]) + s + "".join(st))
else:
  print("".join(st[::-1]) + s[::-1] + "".join(en))

Solution (Python):



from collections import Counter,deque


from sys import stdin,setrecursionlimit










setrecursionlimit(10**6+1)
input=stdin.readline

s=input().rstrip()
q=int(input().rstrip())
qu=deque(s)
rev=0
for _ in range(q):
    que,*C=input().split()
    que=int(que)
    if que==2:
        f,c=C
        f=int(f)
        if f==1:
            if rev%2==0:
                qu.appendleft(c)
            else:
                qu.append(c)
        else:
            if rev%2==0:
                qu.append(c)
            else:
                qu.appendleft(c)
    else:
        rev+=1
print("".join(qu) if rev%2==0 else "".join(qu)[::-1])

Solution (Python):
import bisect

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

S = {i: s for i, s in enumerate(S)}


head = -1
tail = len(S)

is_filp = False
for i in range(Q):
    q = input()

    if q[0] == '1':
        if is_filp:
            is_filp = False
        else:
            is_filp = True
    elif q[0] == '2':
        _, f, c = q.split(' ')
        if f == '1':
            if is_filp:
                S[tail] = c
                tail += 1
            else:
                S[head] = c
                head -= 1
        else:
            if is_filp:
                S[head] = c
                head -= 1
            else:
                S[tail] = c
                tail += 1

if is_filp:
    print(''.join([S[i] for i in range(tail - 1, head, -1)]))
else:
    print(''.join([S[i] for i in range(head + 1, tail)]))

Solution (Python):
from collections import deque

s = input()
Q = int(input())
query = [list(map(str, input().split())) for _ in range(Q)]

d = deque(s)

cnt = 0
for q in query:
    if q[0] == '1':
        cnt += 1
    else:
        if q[1] == '1':
            if cnt % 2:
                d.append(q[2])
            else:
                d.appendleft(q[2])
        else:
            if cnt % 2:
                d.appendleft(q[2])
            else:
                d.append(q[2])
        
d_list = list(d)
s = ''.join(d_list)
if cnt % 2 == 0:
    print(s)
else:
    print(s[::-1])

Solution (Python):










import math
import statistics
import string
import sys
from array import array
from collections import deque

def intinput():
    return int(input())
def listintinput():
    return list(map(int, input().split()))
def splitintinput():
    return map(int, input().split())
def factorialsurplus(x, y, z):  
    ret = 1
    for i in range(x, y + 1):
        ret = (ret * i) % z
    return (ret)
def isprime(x):
    if x == 1:
        return False
    if x == 2:
        return True
    sq = int(math.sqrt(x))
    for i in range(2,sq+1):
        if x % i == 0:
            return False
    return True
def hcfnaive(a,b):
    if(b==0):
        return a
    else:
        return hcfnaive(b,a%b)

s = deque()
s.append(input())
Q = int(input())
rev_count = 0

for i in range(Q):
    a = input()
    if a[0] == "1":
        rev_count += 1
    else:
        b = a.split()
        if rev_count % 2 == 0:
            if b[1] == "1":
                s.appendleft(b[2])
            else:
                s.append(b[2])
        else:
            if b[1] == "1":
                s.append(b[2])
            else:
                s.appendleft(b[2])

if rev_count % 2 == 0:
    print(''.join(s))
else:
    a = ''.join(s)
    print(a[::-1])

Solution (Python):
S = input()
Q = int(input())
front = ''
end = ''
cnt = 0
for _ in range(Q):
    T = input().split()
    if int(T[0]) == 1:
        cnt += 1
    else:
        F = int(T[1])
        C = T[2]
        if F == 1:
            if cnt%2==0:
                front = C+front
            else:
                end = end+C
        else:
            if cnt%2==0:
                end = end+C
            else:
                front=C+front
if cnt % 2 == 0:
    S = front+S+end
    print(S)
else:
    S = list(reversed(S))
    end = list(reversed(end))
    front = list(reversed(front))
    S = end+S+front
    print(''.join(S))

Solution (Python):
from collections import deque
def main():
    S=input()
    Q=int(input())
    d = deque(S)
    is_forward = True
    for _ in range(Q):
        q=input()
        if q[0] == '1':
            is_forward = not is_forward
        else:
            _, F, C = q.split()
            if F == '1':
                if is_forward:
                    d.appendleft(C)
                else:
                    d.append(C)
            else:
                if is_forward:
                    d.append(C)
                else:
                    d.appendleft(C)
    if is_forward:
        for s in d:
            print(s, sep="", end="")
    else:
        for s in reversed(d):
            print(s, sep="", end="")
    print()
    return

if __name__ == '__main__':
    main()

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

reverse_flag = False

top = []
last = []
for i in range(q):
  q_list = list(input().split())
  
  if len(q_list) == 1:
    reverse_flag = not reverse_flag
    
  elif q_list[1] == '1' and (not reverse_flag):
    top.append(q_list[2])
    
  elif q_list[1] == '2' and (not reverse_flag):
    last.append(q_list[2])
    
  elif q_list[1] == '1' and reverse_flag:
    last.append(q_list[2])
    
  elif q_list[1] == '2' and reverse_flag:
    top.append(q_list[2])
    
if reverse_flag:
  print(''.join(reversed(last)) + ''.join(reversed(s)) + ''.join(top))
else:
  print(''.join(reversed(top)) + ''.join(s) + ''.join(last))

Solution (Python):

MOD = 10 ** 9 + 7
INFI = 10**10

import math

import itertools



import copy
from functools import lru_cache

def sosuhante(n):
    for k in range(2, int(math.sqrt(n))+1):
        if n% k ==0:
            return False
    return True
def cmb(n, r):
    if n - r < r: r = n - r
    if r == 0: return 1
    if r == 1: return n

    numerator = [n - r + k + 1 for k in range(r)]
    denominator = [k + 1 for k in range(r)]

    for p in range(2,r+1):
        pivot = denominator[p - 1]
        if pivot > 1:
            offset = (n - r) % p
            for k in range(p-1,r,p):
                numerator[k - offset] /= pivot
                denominator[k] /= pivot

    result = 1
    for k in range(r):
        if numerator[k] > 1:
            result *= int(numerator[k])

    return result

def kingaku(a,b,n):
    keta=len(str(n))
    return a*n+b*keta

def my_index(l, x, default=False):
    if x in l:
        return l.index(x)
    else:
        return default






def main():
    s=input()
    q=int(input())
    even=1
    right=[]
    left=[]
    for i in range(q):
        temp=list(input().split())
        temp[0]=int(temp[0])
        if temp[0]==2:
            a=int(temp[1])
            b=temp[2]
            if a==1:
                left.append(b)
            else:
                right.append(b)
        else:
            even=-(even-1)
            left,right=right,left

    if even==0:
        s=s[::-1]
 
    left=left[::-1]
    ans="".join(left)+s+"".join(right)
    print(ans)


if __name__ == "__main__":
    main()

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

former = ""
latter = ""
is_rev = False

for _ in range(0, num_query):
    query = input()
    if query[0] == "1":
        is_rev = not is_rev
    else:
        pos = query[2]
        character = query[4]
        if pos == "1":
            if is_rev:
                latter += character
            else:
                former = character + former
        else:
            if is_rev:
                former = character + former
            else:
                latter += character

if is_rev:
    ans = latter[::-1] + s[::-1] + former[::-1]
else:
    ans = former + s + latter
print(ans)