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 collections import deque

S = deque(input())
q = int(input())

reverse = False
for _ in range(q):
    query = input().split()
    if query[0] == '1':
        reverse = not reverse
    else:
        if query[1] == '1':
            if reverse:
                S.append(query[2])
            else:
                S.appendleft(query[2])
        else:
            if reverse:
                S.appendleft(query[2])
            else:
                S.append(query[2])

if reverse:
    while S:
        print(S.pop(), end='')
else:
    while S:
        print(S.popleft(), end='')

Solution (Python):
S=input()
Q=int(input())
A=[list(input().split()) for i in range(Q)]
c=0
R=""
L=""
for i in range(Q):
    if A[i][0]=="1":
        c+=1
        continue
    if c%2==1 and A[i][0]=="2":
        if A[i][1]=="1":
            R=R+A[i][2]
        elif A[i][1]=="2":
            L=A[i][2]+L
    elif c%2==0 and A[i][0]=="2":
        if A[i][1]=="1":
            L=A[i][2]+L
        elif A[i][1]=="2":
            R=R+A[i][2]
S=L+S+R
if c%2==1:
    S=S[::-1]
print(S)

Solution (Python):
s = input()
q = int(input())
flag = True
head = '' 
end = '' 

for i in range(q):
    que = [i for i in input().split()]
    if que[0] == '1':
        flag ^= True 
    elif que[0] == '2' and flag is True:
        if que[1] == '1':
            head = que[2] + head
        elif que[1] == '2':
            end = end + que[2]
    elif que[0] == '2' and flag is False:
        if que[1] == '1':
            end = end + que[2]
        elif que[1] == '2':
            head = que[2] + head
s = head+s+end
if flag is False:
    s = s[::-1]
print(s)

Solution (Python):
from collections import deque
S=input()
d=deque()
for i in S:
    d.append(i)
Q=int(input())
Flag=True
for i in range(Q):
    L=input().split()
    if L[0]=="1":
        Flag=not Flag
    else:
        if L[1]=="1" and Flag or L[1]=="2" and not Flag:
            d.appendleft(L[2])
        else:
            d.append(L[2])
ans=""
for i in d:
    ans+=i
if not Flag:
    ans=ans[::-1]
print(ans)

Solution (Python):
s = input()
q = int(input())
hanten=False
mae=""
usiro=""
for i in range(q):
    tmp=input()
    if len(tmp)==1:
        hanten=not(hanten)
    else:
        if hanten==False:
            if tmp[2]=="1":
                mae=tmp[4]+mae
            else:
                usiro=usiro+tmp[4]
        else:
            if tmp[2]=="1":
                usiro=usiro+tmp[4]
            else:
                mae=tmp[4]+mae
                
ans=mae+s+usiro
if hanten==False:
    print(ans)
else:
    print(ans[::-1])

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

reverse = False
for _ in range(Q):
    q = sys.stdin.readline()
    if q[0] == "1":
        reverse = not reverse
    else:
        _, f, c = q.split()
        if f == "1":
            if reverse:
                S.append(c)
            else:
                S.appendleft(c)
        else:
            if reverse:
                S.appendleft(c)
            else:
                S.append(c)
if reverse:
    S.reverse()
ans = "".join(S)
print(ans)

Solution (Python):
from collections import deque

s = input()

r = False
d = deque()
d.extend(list(s))

q = int(input())
for i in range(q):
    t = input()
    if t == "1":
        r = not r
    else:
        t, f, c = list(t.split())
        f = False if f=="1" else True

        if r == f:
            d.appendleft(c)
        else:
            d.append(c)

if r:
    d.reverse()

print("".join(d))

Solution (Python):
import collections
import sys

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

r=0
for _ in range(Q):
    fs=next(sys.stdin).split()
    T=int(fs[0])
    if T==1:
        r^=1
    else:
        F,C=int(fs[1])-1,fs[2]
        if F==r:
            S.appendleft(C)
        else:
            S.append(C)

ans=''.join(reversed(S) if r else S)
print(ans)

Solution (Python):
import sys
import collections

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

rev=0
for _ in range(Q):
    fields=next(sys.stdin).split()
    T,F,C=(fields+['',''])[:3]
    if T=='1':
        rev^=1
    elif (rev and F=='1') or (not rev and F=='2'):
        S.append(C)
    else:
        S.appendleft(C)

if rev:
    print(''.join(reversed(S)))
else:
    print(''.join(S))

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

d = 0
X = [[], []]
for i in range(Q):
    q = input()
    if q[0] == "1":
        d = (d + 1) % 2
    else:
        a, f, c = q.split()
        x = (int(f) + d) % 2
        X[x].append(c)

if d == 0:
    v = "".join(X[1])[::-1] + S + "".join(X[0])
else:
    v = "".join(X[0])[::-1] + S[::-1] + "".join(X[1])
print(v)

Solution (Python):
S=input()

Q=int(input())

inst=[]
for i in range(Q):
    array=input().split()
    inst.append(tuple(array))

rev=True
front_str=[]
back_str=[]
for i in range(Q):
    if int(inst[i][0])==1:
        rev=not rev
    elif rev:
        if int(inst[i][1])==1:
            front_str.append(inst[i][2])
        else:
             back_str.append(inst[i][2])
    else:
        if int(inst[i][1])==1:
            back_str.append(inst[i][2])
        else:
            front_str.append(inst[i][2])

if rev:
    front_str.reverse()
else:
    back_str.reverse()
    S=S[::-1]

front="".join(front_str)
back="".join(back_str)

if rev:
    print(front+S+back)
else:
    print(back+S+front)

Solution (Python):
from collections import deque

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


flag = 0
cnt = 0
d  = deque(s)

for _ in range(q):
    lis = list(input().split())
    if lis[0] == "1":
        if flag:
            flag = 0
        else:
            flag = 1
        cnt += 1
    else:
        if lis[1] == '1':
            if flag:
                d.append(lis[2])
            else:
                d.appendleft(lis[2])
        else:
            if flag:
                d.appendleft(lis[2])
            else:
                d.append(lis[2])


d = list(d)
if cnt % 2 != 0:
    d = d[::-1]


print(''.join(d))

Solution (Python):
def solve():
    from collections import deque
    S = input()
    s = deque(S)
    Q = int(input())
    reverse = False
    for i in range(Q):
        T = input().split()
        if T[0] == '1':
            if reverse:
                reverse = False
            else:
                reverse = True
        else:
            if T[1] == '1' and not reverse:
                s.appendleft(T[2])
            elif T[1] == '1' and reverse:
                s.append(T[2])
            elif T[1] == '2' and not reverse:
                s.append(T[2])
            else:
                s.appendleft(T[2])
    if reverse:
        print(''.join(s)[::-1])
    else:
        print(''.join(s))

if __name__ == "__main__":
    solve()

Solution (Python):
import sys
import collections
import itertools


def resolve(in_):
    one = ord(b'1')
    two = ord(b'2')
    

    S = in_.readline().strip()
    q = collections.deque(S)
    

    reverse_flag = False
    Q = int(in_.readline())
    for query in itertools.islice(in_, Q):
        T = query[0]
        if T == one:
            reverse_flag ^= True 
            
        elif T == two:
            F = query[2]
            C = query[4]
            if (F == one) ^ reverse_flag:
                q.appendleft(C)
            else:
                q.append(C)

    return bytes(reversed(q)) if reverse_flag else bytes(q)


def main():
    answer = resolve(sys.stdin.buffer)
    print(answer.decode('ascii'))


if __name__ == '__main__':
    main()

Solution (Python):
import sys
import collections
import itertools


def resolve(in_):
    one = ord(b'1')
    two = ord(b'2')
    

    S = in_.readline().strip()
    q = collections.deque(S)
    

    reverse_flag = False
    Q = int(in_.readline())
    for query in itertools.islice(in_, Q):
        T = query[0]
        if T == one:
            reverse_flag ^= True 
            
        elif T == two:
            F = query[2]
            C = query[4]
            if (F == one) ^ reverse_flag:
                q.appendleft(C)
            else:
                q.append(C)
    else:
        if reverse_flag:
            q.reverse()
            reverse_flag = 0

    return bytes(q)


def main():
    answer = resolve(sys.stdin.buffer)
    print(answer.decode('ascii'))


if __name__ == '__main__':
    main()

Solution (Python):
from collections import deque
S = input()
Q = int(input())
a = deque([])
for i in S:
    a.append(i)


is_reverse = False
for i in range(Q):
    query = input().split()
    if query[0] == "1":
        is_reverse = not is_reverse
    else:
        if query[1] == "1":
            if is_reverse:
                a.append(query[2])
            else:
                a.appendleft(query[2])
        else:
            if is_reverse:
                a.appendleft(query[2])
            else:
                a.append(query[2])
if is_reverse:
    while len(a)>0:
        print(a.pop(), end="")
else:
    while len(a)>0:
        print(a.popleft(), end="")
print()

Solution (Python):

s = input()
n = int(input())

hanten=0
s1=""
s2=""
for i in range(n):
    Q = input()
    if int(Q[0])==1:
        hanten+=1 
    elif int(Q[0])==2 :
        if hanten%2 == int(Q[2])-1 :
            s1= s1+Q[4]
        else:
            s2= s2+Q[4]

s1 = s1[::-1]
s = s1+s+s2

if hanten%2 :
    s = s[::-1]
print(s)

Solution (Python):
s = input()
q = int(input())
r = 0
S = ['','']
for _ in range(q):
  d = input().split()
  if d[0]=='1':
    r ^= 1
  else:
    S[(int(d[1])+r)%2] += d[2]
s = S[1][::-1] + s + S[0]
if r:
  s = s[::-1]
print(s)

Solution (Python):
from collections import deque
s = input()
q = int(input())
r = False
S = deque(s)
for _ in range(q):
  d = list(input().split())
  if len(d)==1:
    r = ~r
  else:
    if (d[1]=='1' and ~r) or (d[1]=='2' and r):
      S.appendleft(d[2])
    else:
      S.append(d[2])
s = ''.join(S)
if r:
  s = s[::-1]
print(s)

Solution (Python):
s = input()
q = int(input())
cou = 1
frt = ""
bck = ""
for i in range(q):
    t = list(input().split())
    
    if t[0] == "1":
        cou *= -1
    else:
        if (t[1] == "1" and cou == 1)or(t[1] == "2" and cou == -1):
            frt = t[2] + frt
        else:
            bck = bck + t[2]


s = frt + s + bck

if cou == 1:
    print(''.join(s))
else:
    print(''.join(s[::-1]))

Solution (Python):
s=input()
n=int(input())
a=[input() for _ in range(n)]

front=True
f=""
b=""

for q in a:
    if q=="1":front=not front;continue
    l=q.split()
    if l[1]=="1":
        if front:f=l[2]+f
        else:b=b+l[2]
    if l[1]=="2":
        if front:b=b+l[2]
        else:f=l[2]+f

print(f+s+b if front else (f+s+b)[::-1])

Solution (Python):
s = input()
q = int(input())
flip = 1
right_edge = list(s)
left_edge = []
for i in range(q):
    query = input()
    if query[0] == '1':
        flip *= -1
    else:
        if query[2] == '1':
            if flip == 1:
                left_edge.append(query[-1])
            else:
                right_edge.append(query[-1])
        else:
            if flip == 1:
                right_edge.append(query[-1])
            else:
                left_edge.append(query[-1])
left_edge = left_edge[::-1]
if flip == 1:
    print(''.join(left_edge + right_edge))
else:
    print(''.join(right_edge[::-1] + left_edge[::-1]))

Solution (Python):
from collections import deque
def main():
    S = deque(map(str, input()))
    Q = int(input())
    flag = False
    for _ in range(Q):
        A = tuple(map(str, input().split()))
        if A[0] == "1":
            flag = not(flag)
        else:
            if (A[1] == "1" and not(flag)) or (A[1] == "2" and flag):
                S.appendleft(A[2])
            else:
                S.append(A[2])
    if flag:
        print("".join(S)[::-1])
    else:
        print("".join(S))
if __name__ == "__main__":
    main()

Solution (Python):

from collections import deque

q = deque(list(input()))
Q = int(input())

flg = 0
for _ in range(Q):
    i, *x = input().split()
    if i == "1":
        flg = 1 - flg
    else:
        f, c = x
        if flg == 0:
            if f == "1":
                q.appendleft(c)
            else:
                q.append(c)
        else:
            if f == "1":
                q.append(c)
            else:
                q.appendleft(c)

if flg == 0:
    print("".join(q))
else:
    print("".join(reversed(q)))

Solution (Python):
from collections import deque

S = input()
q = int(input())
res = deque([S])
reverseFlag = False
for i in range(q):
  tmp = input()
  t = tmp[0]
  if t == "1":
    reverseFlag ^= True 
  else:
    f,c = tmp[2:].split()
    if f == "1" and reverseFlag == False or f == "2" and reverseFlag == True:
      res.appendleft(c)
    else:
      res.append(c)
if reverseFlag:
  ans = "".join(res)[::-1]
else:
  ans = "".join(res)

print(ans)

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

q = int(input())
rev_count = 0
for i in range(q):
    query = list(input().split(" "))
    if len(query) == 1:
        rev_count += 1
    else:
        if query[1] == "1":
            if rev_count % 2==0:
                queue.appendleft(query[2])
            else:
                queue.append(query[2])
        else:
            if rev_count % 2==0:
                queue.append(query[2])
            else:
                queue.appendleft(query[2])

queue = "".join(list(queue))
if rev_count % 2 == 0:
    print(queue)
else:
    print(queue[::-1])

Solution (Python):
from collections import deque

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

d = deque(S)
flg = True 
for i in range(Q):
    t, *a = input().split()
    if t == "1":
        flg = not flg
    else:
        if (flg and a[0]=="1") or ((not flg) and a[0]=="2"):
            d.appendleft(a[1])
        else:
            d.append(a[1])
d = list(d)
if flg:
    print(''.join(d))
else:
    print(''.join(d[::-1]))

Solution (Python):
from collections import deque

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

d = deque(S)
flg = 1 
for i in range(Q):
    t, *a = input().split()
    if t == "1":
        flg = 1 - flg
    else:
        if flg:
            if a[0] == "1":
                d.appendleft(a[1])
            else:
                d.append(a[1])
        else:
            if a[0] == "2":
                d.appendleft(a[1])
            else:
                d.append(a[1])
d = list(d)
if flg:
    print(''.join(d))
else:
    print(''.join(d[::-1]))

Solution (Python):
from collections import deque

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

d = deque(S)
flg = True 
for i in range(Q):
    t, *a = input().split()
    if t == "1":
        flg = not flg
    else:
        if flg:
            if a[0] == "1":
                d.appendleft(a[1])
            else:
                d.append(a[1])
        else:
            if a[0] == "2":
                d.appendleft(a[1])
            else:
                d.append(a[1])
d = list(d)
if flg:
    print(''.join(d))
else:
    print(''.join(d[::-1]))

Solution (Python):

from collections import defaultdict, Counter
from itertools import product, groupby, count, permutations, combinations
from math import pi, sqrt
from collections import deque
from bisect import bisect, bisect_left, bisect_right
from string import ascii_lowercase
from functools import lru_cache
import sys
sys.setrecursionlimit(10000)
INF = float("inf")
YES, Yes, yes, NO, No, no = "YES", "Yes", "yes", "NO", "No", "no"
dy4, dx4 = [0, 1, 0, -1], [1, 0, -1, 0]
dy8, dx8 = [0, -1, 0, 1, 1, -1, -1, 1], [1, 0, -1, 0, 1, 1, -1, -1]


def inside(y, x, H, W):
    return 0 <= y < H and 0 <= x < W


def ceil(a, b):
    return (a + b - 1) // b


def sum_of_arithmetic_progression(s, d, n):
    return n * (2 * s + (n - 1) * d) // 2


def gcd(a, b):
    if b == 0:
        return a
    return gcd(b, a % b)


def lcm(a, b):
    g = gcd(a, b)
    return a / g * b


def solve():
    S = input()
    Q = int(input())

    que = deque(S)

    rev = False
    for _ in range(Q):
        T = input()

        if len(T) == 1:
            rev = not rev
        else:
            t, F, C = T.split()
            
            if int(F) == 1:
                if not rev:
                    que.appendleft(C)
                else:
                    que.append(C)
            
            else:
                if not rev:
                    que.append(C)
                else:
                    que.appendleft(C)

    if rev:
        que = reversed(que)
    print(*que, sep="")


def main():
    solve()


if __name__ == '__main__':
    main()

Solution (Python):
s = str(input())
Q = int(input())
forward = 1
front = ""
back = ""

for i in range(Q):
    q = list(input().split())
    if q[0] == "1":
        forward *= -1
    else:
        if q[1] == "1":
            if forward == 1:
                front = q[2] + front
            else:
                back = back + q[2]        
        else:
            if forward == 1:
                back = back + q[2]
            else:
                front = q[2] + front

s = front + s + back
if forward == -1:
    s = s[::-1]
print(s)

Solution (Python):
from collections import deque
S = input()
d = deque([S[i] for i in range(len(S))])
Q = int(input())
bol = 0
for i in range(Q):
    a = input().split()
    if a[0] == "1":
        bol = 0 if bol else 1
    elif a[1] == "1":
        if not bol:
            d.appendleft(a[2])
        else:
            d.append(a[2])
    else:
        if not bol:
            d.append(a[2])
        else:
            d.appendleft(a[2])
print("".join(d) if not bol else "".join(reversed(d)))

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

rev = 0
sh = ''
for i in range(n):
    q = input().split()
    if q[0] == '1':
        rev = (rev + 1) % 2
    else:
        head = int(q[1]) % 2
        if head ^ rev:
            sh += q[2]
            
        else:
            s += q[2]
            
ans = sh[::-1] + s
print(ans[::-1]) if rev else print(ans)

Solution (Python):
import sys, bisect, math, itertools, string, queue, copy


from collections import Counter,defaultdict,deque






def inp(): return int(input())
def inpm(): return map(int,input().split())
def inpl(): return list(map(int, input().split()))
def inpls(): return list(input().split())
def inplm(n): return list(int(input()) for _ in range(n))
def inplL(n): return [list(input()) for _ in range(n)]
def inplT(n): return [tuple(input()) for _ in range(n)]
def inpll(n): return [list(map(int, input().split())) for _ in range(n)]
def inplls(n): return sorted([list(map(int, input().split())) for _ in range(n)])

s = input()
q = int(input())
ql = [input().split() for i in range(q)]

ans = deque(s)
isrev=False
for i in range(q):
  if ql[i][0] == '1':
    isrev = not isrev
    continue
  if (ql[i][1] == '2') == isrev:
    ans.appendleft(ql[i][2])
  else:
    ans.append(ql[i][2])

if isrev:
  ans.reverse()

s = ''.join(ans)

print(s)

Solution (Python):
S = input()
N = int(input())
line = [''] * (N+1) * 2
rev = False
pos = N
line[pos] = S
leftPos = pos - 1
rightPos = pos + 1

for i in range(N):
    A = input().split()
    if len(A) == 1:
        rev = not rev
    else:
        if A[1] == '1':
            if rev == False:
                pos = leftPos
                leftPos -= 1
            else:
                pos = rightPos
                rightPos += 1
        else:
            if rev == False:
                pos = rightPos
                rightPos += 1
            else:
                pos = leftPos
                leftPos -= 1
        line[pos] = A[2]

ans = line[leftPos+1:rightPos]
ans = "".join(ans)
if rev == False:
    print(ans)
else:
    print(ans[::-1])

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

head = []
tail = []
forward = True
for query in queries:
    if query[0] == '1':
        forward = not forward
    else:
        if query[1] == '1' and forward or query[1] == '2' and not forward:
            head.append(query[2])
        else:
            tail.append(query[2])

if forward:
    ans = ''.join(head[::-1]) + S + ''.join(tail)
else:
    ans = ''.join(tail[::-1]) + S[::-1] + ''.join(head)
print(ans)

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

Q=int(input())
append_flag=False
Q_=[]

for q in range(Q):
    T = list(input().split())
    if T[0]=='2':
        append_flag=True
    if append_flag:
        Q_.append(T)
        
rev=1
rev_num=0
for q in Q_:
    if q[0]=='1':
        rev = -1*rev
        rev_num+=1
    elif rev == 1:
        if q[1]=='1':
            s.appendleft(q[2])
        elif q[1]=='2':
            s.append(q[2])
    elif rev == -1:
        if q[1]=='2':
            s.appendleft(q[2])
        elif q[1]=='1':
            s.append(q[2])

s=list(s)
if rev_num%2 != 0:
    s.reverse()
    
print(''.join(s))

Solution (Python):

s = input()
Q = int(input())
q = [input().split() for _ in range(Q)]
reverse = 0
start = ""
end = ""
for i in q:
    if i[0] == "1":
        reverse ^= 1
    else:
        if (int(i[1]) + reverse) % 2:
            start += i[2]
        else:
            end += i[2]
s = start[::-1] + s + end
print(s[::1 - reverse * 2])

Solution (Python):
s=input()
q=int(input())
flag=0
t=['' for _ in range(2)]
cnt=0
for _ in range(q):
  a=list(input().split())
  if a[0]=='1':
    flag=1-flag
    cnt+=1
  else:
    if a[1]=='1':t[flag]+=a[2]
    else:t[1-flag]+=a[2]
ans=t[0][::-1]+s+t[1]
if cnt%2:ans=ans[::-1]
print(ans)

Solution (Python):
S = input()
Q = int(input())
reverse = False
before = []
after = []
for i in range(Q):
    q = input().split()
    if len(q) == 1:
        reverse = not reverse
    else:
        T, F, C = q
        if (F == '1' and not reverse) or (F == '2' and reverse):
            before.append(C)
        else:
            after.append(C)

if not reverse:
    answer = ''.join(before[::-1]) + S + ''.join(after)
else:
    answer = ''.join(after[::-1]) + S[::-1] + ''.join(before)
print(answer)

Solution (Python):
from collections import deque

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

deq = deque([s])
flag = 1
for i in range(q):
    query = input().split()
    if query[0]==str(1):
        flag*=-1 
    if query[0]==str(2):
        x = 1 if query[1]==str(1) else -1
        if flag*x==1:
            deq.appendleft(query[2])
        else:
            deq.append(query[2])

if flag==-1:
    print(''.join(deq)[::-1])
else:
    print(''.join(deq))

Solution (Python):
from sys import stdin
import sys
import math
from functools import reduce
import functools
import itertools
from collections import deque

s = deque(stdin.readline().rstrip())
n = int(stdin.readline().rstrip())

flag = 0

for i in range(n):
    q = [x for x in stdin.readline().rstrip().split()]

    if len(q) == 1:
        flag = 1 - flag
    else:
        if (q[1] == "1" and flag == 0) or (q[1] == "2" and flag == 1):
            s.appendleft(q[2])
        else:
            s.append(q[2])

if flag == 1:
    s = ''.join(s)[::-1]
else:
    s = ''.join(s)
print (s)

Solution (Python):
from collections import deque

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

d = deque()
for v in s:
    d.append(v)
r = False
for _ in range(q):
    xs = input().split()
    if xs[0] == "1":
        r = not r
    else:
        if (not r and xs[1] == "1") or (r and xs[1] == "2"):
            d.appendleft(xs[2])
        else:
            d.append(xs[2])
s = "".join(d)
print(s[::-1] if r else s)

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


def main():
  S = input()[:-1]
  Q = int(input())

  q = deque([S])
  reverse = False
  for i in range(Q):
    line = input().split()
    if line[0] == '1':
      reverse = not(reverse)
    else:
      if reverse ^ (int(line[1])-1):
        
        q.append(line[2])
      else:
        
        q.appendleft(line[2])

  print(''.join(list(reversed(''.join(q)))) if reverse else ''.join(q))


if(__name__ == '__main__'):
  main()

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

s = deque(s)
t = 0
for _ in range(q):
  query = list(map(str, input().split()))
  if query[0] == "1": t = 1 - t
  else:
    if t == 0:
      if query[1] == "1": s.appendleft(query[2])
      elif query[1] == "2": s.append(query[2])
    elif t == 1:
      if query[1] == "1": s.append(query[2])
      elif query[1] == "2": s.appendleft(query[2])
if t == 1:print("".join(s)[::-1])
else: print("".join(s))

Solution (Python):



from collections import deque

def ChangeState(Is_normal):
    if Is_normal==True:
        return False
    else:
        return True

S=deque(input().split())
Q=int(input())
Is_normal=True

for _ in range(Q):
    q = input()
    if len(q)== 1 :
        Is_normal = ChangeState(Is_normal)
    else:
        if q[2]=='1':
            if Is_normal==True:
                
                S.appendleft(q[4])
            else :
                
                S.append(q[4])

        else :
            if Is_normal==True:
                
                S.append(q[4])

            else:
                
                S.appendleft(q[4])

    
S=''.join(S)
if Is_normal==False:
    S=S[::-1]
print(S)

Solution (Python):



def ChangeState(Is_normal):
    if Is_normal==True:
        return False
    else:
        return True



S=input()
Q=int(input())
Is_normal=True

h=''
t=[]

for _ in range(Q):
    q = input()
    if len(q)== 1 :
        Is_normal = ChangeState(Is_normal)
    else:
        if q[2]=='1':
            if Is_normal==True:
                h=h+q[4]
            else :
                S=S+q[4]
        else :
            if Is_normal==True:
                S=S+q[4]
            else:
                h=h+q[4]
    
h=h[::-1]
S=h+S
if Is_normal==False:
    S=S[::-1]
print(S)

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

dir = 0
mae = []
usi = []

for _ in range(p):
  Q = input().split()
  if Q[0] == "1":
    dir = not dir
  else:
    if (int(Q[1]) - 1) == dir:
      mae.append(Q[2])
    else:
      usi.append(Q[2])
      
ans = "".join(mae[::-1]) + s + "".join(usi)
      
if not dir:
  print(ans)
else:
  print(ans[::-1])

Solution (Python):

from collections import deque

S = input()
Q = int(input())
query = list()
for i in range(Q):
  query.append(list(map(str, input().split())))


d = deque()
for s in S:
  d.append(s) 
is_reverse = False

for q in query:
  if len(q) == 1:
    
    
    is_reverse = not is_reverse
    
  else:
    
    
    if q[1] == '1':
      
      
      if is_reverse == False:
        d.appendleft(q[2])
      else:
        d.append(q[2])
      
    else:
      
      
      if is_reverse == False:
        d.append(q[2])
      else:
        d.appendleft(q[2])

ans = ''
if is_reverse == True:
  d.reverse()
ans = ''.join(d)
print(ans)

Solution (Python):
from collections import deque
s = deque(input())
n = int(input())
now: bool = True

for i in range(n):
    q = list(input().split())
    if q[0] == '1':
        now = not now
    else:
        if (q[1] == '1' and now == True) or (q[1] == '2' and now == False):
            s.appendleft(q[2])
        elif (q[1] == '1' and now == False) or (q[1] == '2' and now == True):
            s.append(q[2])
if now == True:
    print(''.join(s))
else:
    print(''.join(reversed(s)))

Solution (Python):
INT = lambda: int(input())
INTM = lambda: map(int,input().split())
STRM = lambda: map(str,input().split())
STR = lambda: str(input())
LIST = lambda: list(map(int,input().split()))
LISTS = lambda: list(map(str,input().split()))
def do():
    s=STR()
    q=INT()
    l=''
    r=''
    ct=0
    for i in range(q):
        fc=LISTS()
        if fc[0]=="1":
            ct+=1
        else:
            if fc[1]=="1":
                if ct%2==0:
                    l=fc[2]+l
                else:
                    r=r+fc[2]
            else:
                if ct%2==1:
                    l=fc[2]+l
                else:
                    r=r+fc[2]
    
    sums=l+s+r
    if ct%2==1:
        ans=""
        for i in range(len(sums)-1,-1,-1):
            ans=ans+sums[i]
    else:
        ans=sums
    
    print(ans)
                
if __name__ == '__main__':
    do()

Solution (Python):

from collections import defaultdict, deque, Counter, OrderedDict
from functools import reduce, lru_cache
import collections, heapq, itertools, bisect
import math, fractions
import sys, copy

def L(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline().rstrip())
def S(): return list(sys.stdin.readline().rstrip())
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI1(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def LS(): return [list(x) for x in sys.stdin.readline().split()]
def IR(n): return [I() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def LIR1(n): return [LI1() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
def LR(n): return [L() for _ in range(n)]

sys.setrecursionlimit(1000000)
dire = [[1, 0], [0, 1], [-1, 0], [0, -1]]
dire8 = [[1, 0], [1, 1], [0, 1], [-1, 1], [-1, 0], [-1, -1], [0, -1], [1, -1]]
MOD = 1000000007

def main():
    s = S()
    q = I()
    Q = LR(q)

    turned = False
    q = deque()
    for c in s:
        q.append(c)

    for query in Q:
        if query[0] == "1":
            turned = not turned
        else:
            _, f, c = query
            if (not turned and f == "1") or (turned and f == "2"):
                q.appendleft(c)
            else:
                q.append(c)


    if turned:
        q.reverse()
    ans = "".join(q)
    print(ans)


if __name__ == '__main__':
    main()