Problem:
 Score : 400 points    Problem Statement Takahashi has a string S consisting of lowercase English letters.  Starting with this string, he will produce a new one in the procedure given as follows.  The procedure consists of Q operations. In Operation i  (1 \leq i \leq Q) , an integer T_i is provided, which means the following:    If T_i = 1 : reverse the string S .    If T_i = 2 : An integer F_i and a lowercase English letter C_i are additionally provided.   If F_i = 1 : Add C_i to the beginning of the string S .  If F_i = 2 : Add C_i to the end of the string S .     Help Takahashi by finding the final string that results from the procedure.      Constraints  1 \leq |S| \leq 10^5  S consists of lowercase English letters.  1 \leq Q \leq 2 \times 10^5  T_i = 1 or 2 .  F_i = 1 or 2 , if provided.  C_i is a lowercase English letter, if provided.         Input Input is given from Standard Input in the following format:  S  Q  Query_1  :  Query_Q   In the 3 -rd through the (Q+2) -th lines, Query_i is one of the following:  1   which means T_i = 1 , and:  2  F_i  C_i   which means T_i = 2 .      Output Print the resulting string.        Sample Input 1 a
4
2 1 p
1
2 2 c
1      Sample Output 1 cpa  There will be Q = 4 operations. Initially, S is a .    Operation 1 : Add p at the beginning of S . S becomes pa .    Operation 2 : Reverse S . S becomes ap .    Operation 3 : Add c at the end of S . S becomes apc .    Operation 4 : Reverse S . S becomes cpa .    Thus, the resulting string is cpa .       Sample Input 2 a
6
2 2 a
2 1 b
1
2 2 c
1
1      Sample Output 2 aabc  There will be Q = 6 operations. Initially, S is a .    Operation 1 : S becomes aa .    Operation 2 : S becomes baa .    Operation 3 : S becomes aab .    Operation 4 : S becomes aabc .    Operation 5 : S becomes cbaa .    Operation 6 : S becomes aabc .    Thus, the resulting string is aabc .       Sample Input 3 y
1
2 1 x      Sample Output 3 xy

Solution (Python):
S = str(input())
Q = int(input())
Qvery = [0]*Q
for i in range(Q):
    Qvery[i] = list(map(str,input().split()))
frontback = 1
tail = ''
head = ''
for i in range(Q):
    if Qvery[i][0]=='1':
        frontback *= -1
        
    else:
        if Qvery[i][1]=='2':
            if frontback == 1:
                tail +=  str(Qvery[i][2])
            else:
                head = str(Qvery[i][2]) + head
        else:
            if frontback == 1:
                head = str(Qvery[i][2])+head
            else:
                tail += str(Qvery[i][2])
S = head + S + tail
if frontback == -1:
    S = S[::-1]
    
print(S)

Solution (Python):
from collections import deque

poe=deque([])
S=input()
for i in range(len(S)):
    poe.append(S[i])
Q=int(input())
P=[]
cnt=0
for i in range(Q):
    a=list(input().split())
    P.append(a)

for i in range(Q):
    if len(P[i])==1:
        cnt+=1
    else:
        P[i][1]=int(P[i][1])+cnt
        if P[i][1]%2==1:
            poe.appendleft(P[i][2])
        else:
            poe.append(P[i][2])

poe=list(poe)
if cnt%2==1:
    poe=poe[::-1]
print(''.join(poe))

Solution (Python):



import sys

S = input()

Q = int(input())

rev = 0

front = []
end = []

for q in range(Q):
    query = input().split()
    if query[0] == "1":
        rev = 1 - rev
    else:
        if ((rev == 0) & (query[1] == "1")) | ((rev == 1) & (query[1] == "2")):
            front.append(query[2])
        else:
            end.append(query[2])

front = "".join(front)
end = "".join(end)


if rev:
    print(end[::-1] + S[::-1] + front)
else:
    print(front[::-1] + S + end)

Solution (Python):
S = input()
top = 0
end = len(S)-1
toright = True
N = int(input())
Q = [S[i] if i <= end else "" for i in range(end+1+N)]

for i in range(N):
  q = input().split()
  if q[0]=="1":
    toright = not toright
  else:
    if not(toright ^ (q[1]=="1")):
      Q[top-1]=q[2]
      top -= 1
    else:
      Q[end+1]=q[2]
      end += 1
ans = ""


for i in range(top, end+1):
  ans += Q[i]
if not toright:
  ans = ans[::-1]
print(ans)

Solution (Python):
def sep():
    return map(int,input().strip().split(" "))
def lis():
    return list(sep())

s=input()
q=int(input())
from collections import deque
ar=[]
for i in s:
    ar.append(i)
ar=deque(ar)
r=0
for i in range(q):
    t=[i for i in input().strip().split(" ")]
    if t[0]=="1":
        r=1-r
    else:
        if r==0:
            if t[1]=="1":
                ar.appendleft(t[2])
            else:
                ar.append(t[2])
        else:
            if t[1]=="1":
                ar.append(t[2])
            else:
                ar.appendleft(t[2])
if r==0:
    print("".join(ar))
else:
    print("".join(ar)[::-1])

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

lS = ''
for _ in range(Q):
    query = input().split()
    if query[0] == '1':
        lS, S = S, lS
    else:
        if query[1] == '1':
            lS += query[2]
        else:
            S += query[2]

print(lS[::-1] + S)

Solution (Python):
def resolve():
    s = input()
    query = int(input())
    r = 0
    left = ''
    for i in range(query):
        q = input()
        if q[0]=='2':
            _,f,c = q.split()
            if (int(f)+r)%2 == 0:
                s += c
            else:
                left += c
        else:
            r = (r+1)%2
    print(left[::-1]+s if r==0 else s[::-1]+left)
resolve()

Solution (Python):
from collections import deque
s = str(input())
q = int(input())
stat = 0
l = []
a = deque()
b = deque()
for i in range(q):
    l = list(map(str, input().split()))
    t = int(l[0])
    if t == 1:
        stat += 1
    else:
        f = int(l[1])
        c = l[2]
        if stat % 2 == 0:
            if f == 1:
                a.appendleft(c)
            else:
                b.append(c)
        else:
            if f == 1:
                b.append(c)
            else:
                a.appendleft(c)


ans = ''.join(a)+s+''.join(b)
if stat % 2 == 1:
    ans = ans[::-1]
print(ans)

Solution (Python):
S = input()
N = int(input())
flg = 0
L = []
for i in range(N):
    e = input().split()
    if e[0] == '1':
        flg += 1
        flg = flg%2
    else:
        L.append(((flg+int(e[1]))%2, e[2]))

S = ''.join(list(reversed(S))) if flg else S
s1, s2 = '', ''
for f, v in L:
    if (flg+f)%2 == 1:
        s1 += v
    else:
        s2 += v

ans = ''.join(list(reversed(s1))) + S + s2
print(ans)

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

head = []
tail = []

for i in range(Q):
  line = input()
  if line == '1':
    rev *= -1
  else:
    line = line.split()
    F = int(line[1])
    C = line[2]
    if rev * F in [1, -2]:
      head.append(C)
    else:
      tail.append(C)

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

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

Solution (Python):


def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
from collections import deque


def main():
    mod=10**9+7
    S=input()
    
    from collections import deque
    dq=deque()
    
    for c in S:
        dq.appendleft(c)
        
    rev=0
    Q=I()
    for _ in range(Q):
        q=input().split()
        if q[0]=="1":
            rev=(rev+1)%2
        else:
            f=int(q[1])
            c=q[2]
            
            if (rev+f)%2==1:
                dq.append(c)
            else:
                dq.appendleft(c)
                
    ans=[]
    if rev:
        while dq:
            ans.append(dq.popleft())
    else:
        while dq:
            ans.append(dq.pop())
            
    print(''.join(map(str, ans)))
        

        

main()

Solution (Python):






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

inv = False

fro = ""
end = ""

for _ in range(q):
    query = list(map(str, input().split()))

    if query[0] == "1":
        inv = not inv

    else:
        t = int(query[1])
        instr = query[2]
        if not inv:
            if t == 1:
                fro = instr + fro
            else:
                end = end + instr

        else:
            if t == 1:
                end = end + instr[::-1]
            else:
                fro = instr[::-1] + fro

ans = fro + s + end

if not inv:
    print(ans)

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

Solution (Python):
import sys
S = input()
A = ''
input()
key = 1

for i in sys.stdin:
    if i[0] == '0':
        break

    if i[0] == '1':
        key = -key
    
    elif key == 1:
        if i[2] == '1':
            A = A + i[4]
        else:
            S = S + i[4]

    else:
        if i[2] == '2':
            A = A + i[4]
        else:
            S = S + i[4]

ans = A[::-1] + S
print(ans[::key])

Solution (Python):
import sys
S = input()
Q = int(input())
key = 1

for i in sys.stdin:
    if i[0] == '0':
        break

    if i[0] == '1':
        key = -key
    
    elif key == 1:
        if i[2] == '1':
            S = i[4] + S
        else:
            S = S + i[4]

    else:
        if i[2] == '2':
            S = i[4] + S
        else:
            S = S + i[4]


print(S[::key])

Solution (Python):
from collections import deque
d=deque(input())
q=int(input())
ans=1
for i in range(q):
    a=input()
    if int(a[0])==1:
        ans=ans*(-1)
    else:
        if ans==1:
            if int(a[2])==1:
                d.appendleft(a[4])
            else:
                d.append(a[4])
        else:
            if int(a[2])==1:
                d.append(a[4])
            else:
                d.appendleft(a[4])
if ans==1:
    for i in range(len(d)):
        print(d[i],end="")
else:
    d.reverse()
    for i in range(len(d)):
        print(d[i],end="")

Solution (Python):
s = input()
q = int(input())
flag = 1
f = ''
b = ''
for i in range(q):
  query = list(map(str, input().split()))
  if query[0] == '1':
    flag *= -1
  else:
    if flag == 1:
      if query[1] == '1':
        f += query[2]
      else:
        b += query[2]
    else:
      if query[1] == '1':
        b += query[2]
      else:
        f += query[2]
ans = f[::-1] + s + b
if flag == -1:
  ans = ans[::-1]
print(ans)

Solution (Python):
S = input()
Q = int(input())
query = [list(input().split()) for _ in range(Q)]
forward = 1
f = ''
b = ''
for i in range(Q):
    if query[i][0] == '1':
        forward *= -1
    else:
        if query[i][1] == '1':
            if forward == 1:
                f += query[i][2]
            else:
                b += query[i][2] 
        else:
            if forward == 1:
                b += query[i][2] 
            else:
                f += query[i][2]
S = ''.join(list(reversed(f))) + S + b

if forward == -1:
    ans = ''.join(list(reversed(S)))
else:
    ans = S
print(ans)

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

for q in range(Q):
  que = input().split()
  if que[0]=="1":
        S,T = T,S
  else:
    if que[1]=="1":
      T+=que[2]
    else:
      S+=que[2]

print(T[::-1]+S)

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

    reverse = 0
    head = ""
    tail = ""

    for i in range(q):
        v = list(input().split())
        if int(v[0]) == 1:
            reverse += 1
        elif reverse % 2 == 0:
            if int(v[1]) == 1:
                head += v[2]
            else:
                tail += v[2]
        elif reverse % 2 == 1:
            if int(v[1]) == 1:
                tail += v[2]
            else:
                head += v[2]

    s = head[::-1] + s + tail
    if reverse % 2 == 1:
        s = s[::-1]

    print(s)


if __name__ == "__main__":
    main()

Solution (Python):
def abc158d_string_formation():
    from collections import deque
    s = deque(list(input()))
    q = int(input())
    flip = 0
    for _ in range(q):
        query = list(input().split())
        if query[0] == '1':
            flip = 1-flip
        elif query[1] == '1':
            if flip == 0:
                s.appendleft(query[2])
            else:
                s.append(query[2])
        else:
            if flip == 0:
                s.append(query[2])
            else:
                s.appendleft(query[2])
    if flip == 1:
        s = ''.join(reversed(s))
    else:
        s = ''.join(s)
    print(s)
abc158d_string_formation()

Solution (Python):
def main():
    s = str(input())
    q = int(input())
    lst = [list(map(str, input().split())) for _ in range(q)]
    switch = 0  

    str_lst = [s]
    front_lst = []
    for i in range(q):

        if lst[i][0] == '1':
            switch = 1 - switch

        else:
            f = lst[i][1]
            c = lst[i][2]

            if f == '1':  

                if switch == 0:
                    front_lst.append(c)

                else:
                    str_lst.append(c)

            else:

                if switch == 0:
                    str_lst.append(c)

                else:
                    front_lst.append(c)

    front = ''.join(front_lst)
    front = front[::-1]
    after = ''.join(str_lst)
    answer = front + after

    if switch == 1:
        answer = answer[::-1]

    print(answer)


if __name__ == '__main__':
    main()

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


def main():
    S = input().rstrip()
    Q = int(input())
    reverse_flag = False
    head = ''
    tail = ''
    for _ in range(Q):
        q = input().rstrip()
        if q == '1':
            reverse_flag = not reverse_flag
        else:
            _, F, C = q.split()
            if (F == '1' and reverse_flag is False) or (F == '2' and reverse_flag is True):
                head += C
            else:
                tail += C
    if reverse_flag is True:
        print(tail[::-1] + S[::-1] + head)
    else:
        print(head[::-1] + S + tail)


if __name__ == '__main__':
    main()

Solution (Python):
s = input()
q = int(input())
head = ''
tail = ''
rev = 0

for _ in range(q):
    T = list(input().split())
    if T[0] == '1':
        head, tail = tail, head
        rev += 1
    else:
        if T[1] == '1':
            head += T[2]
        else:
            tail += T[2]

if rev%2 == 0:
    print(head[::-1]+s+tail)
else:
    print(head[::-1]+s[::-1]+tail)

Solution (Python):
def main():
    from collections import deque
    S = input()
    Q = int(input())
    rev = 0  
    que = deque((s for s in S))
    for _ in range(Q):
        Cmd = [i for i in input().split()]
        if Cmd[0] == "1":
            rev ^= 1
        else:
            if rev == 0:
                if Cmd[1] == "1":
                    
                    que.appendleft(Cmd[2])
                else:
                    
                    que.append(Cmd[2])
            else:
                if Cmd[1] == "1":
                    
                    que.append(Cmd[2])
                else:
                    
                    que.appendleft(Cmd[2])
    ans = "".join(que)
    if rev == 1:
        ans = ans[::-1]
    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
from collections import deque
def main():
    s = list(input())
    q = int(input())
    query = []
    que = deque(s)
    f = False
    for i in range(q):
        query.append(input().split(" "))
    for i in range(q):
        if query[i][0] == '1':
            f = not f
        else:
            if f:
                if query[i][1] == '2':
                    que.appendleft(query[i][2])
                else:
                    que.append(query[i][2])
            else:
                if query[i][1] == '1':
                    que.appendleft(query[i][2])
                else:
                    que.append(query[i][2])
    if f:
        que.reverse()
    print("".join(que))

if __name__ == "__main__":
    main()

Solution (Python):
from collections import deque

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

d = deque(s)

cnt = 0
for i in range(q):
  if len(t[i]) == 1:
    cnt += 1
  else:
    if (int(t[i][1])+cnt) % 2 != 0:
      d.appendleft(t[i][2])
    else:
      d.append(t[i][2])
      
if cnt%2 != 0:
  d = list(d)[::-1]
print(''.join(d))

Solution (Python):
S = input()
Q = int(input())
bs = ''
fs = ''
hanten = 0
for i in range(Q):
    query = list(input().split())
    if query[0] == '1':
        hanten += 1
    else:
        if query[1] == '1':
            if hanten % 2 == 0:
                
                fs = ''.join([fs,query[2]])
            else:
                
                bs = ''.join([bs, query[2]])
        else:
            if hanten % 2 == 0:
                
                bs = ''.join([bs, query[2]])
            else:
                
                fs = ''.join([fs, query[2]])
if hanten % 2 == 0:
    
    print(''.join([fs[::-1], S, bs]))
else:
    
    print(''.join([bs[::-1], S[::-1], fs]))

Solution (Python):
S = input()
Q = int(input())
bs = ''
fs = ''
hanten = 0
for i in range(Q):
    query = list(input().split())
    if query[0] == '1':
        hanten += 1
    else:
        if query[1] == '1':
            if hanten % 2 == 0:
                fs += query[2]
            else:
                bs += query[2]
        else:
            if hanten % 2 == 0:
                bs += query[2]
            else:
                fs += query[2]
if hanten % 2 == 0:
    print(fs[::-1]+S+bs)
else:
    print(bs[::-1]+S[::-1]+fs)

Solution (Python):
S = input()
Q = int(input())
bs = ''
fs = ''
hanten = 0
for i in range(Q):
    query = list(input().split())
    if query[0] == '1':
        hanten += 1
    else:
        if query[1] == '1':
            if hanten % 2 == 0:
                fs += query[2]
            else:
                bs += query[2]
        else:
            if hanten % 2 == 0:
                bs += query[2]
            else:
                fs += query[2]
if hanten % 2 == 0:
    print(fs[::-1]+S+bs)
else:
    print(bs[::-1]+S[::-1]+fs)

Solution (Python):
S = input()
Q = int(input())
bs = ''
fs = ''
hanten = 0
for i in range(Q):
    query = list(input().split())
    if query[0] == '1':
        hanten += 1
    else:
        if query[1] == '1':
            if hanten % 2 == 0:
                fs += query[2]
            else:
                bs += query[2]
        else:
            if hanten % 2 == 0:
                bs += query[2]
            else:
                fs += query[2]
if hanten % 2 == 0:
    print(fs[::-1]+S+bs)
else:
    print(bs[::-1]+S[::-1]+fs)

Solution (Python):
from collections import deque
que1 = deque([])
que2 = deque([])
s = input()
q = int(input())
rev = 1
for i in range(q):
  t = input()
  
  if t == '1':
    rev *= -1
  else:
    x,y,sr = t.split()
    if (rev == 1 and y == '2') or (rev == -1 and y == '1'):
      que2.append(sr)
    else:
      que1.appendleft(sr)

s = ''.join(que1+deque([s])+que2)     
if rev == -1:
  s = s[::-1]
  
print(s)

Solution (Python):
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time, copy

sys.setrecursionlimit(10**7)
inf = 10**20
mod = 10**9 + 7

stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()  

ans = collections.deque(ns())
q = ni()
now = 0
for i in range(q):
    query = ns().split(' ')
    if query[0] == '1':
        now = 1 - now
    else:
        f, c = query[1], query[2]
        if f == '1':
            if now == 1:
                ans.append(c)
            else:
                ans.appendleft(c)
        else:
            if now != 1:
                ans.append(c)
            else:
                ans.appendleft(c)

if now == 1:
    for i in range(len(ans)):
        print(ans[-(i+1)], end="")
else:
    for i in range(len(ans)):
        print(ans[i], end="")
print()

Solution (Python):
S = input()
Q = int(input())
T = 0
s = ""
for i in range(Q):
    q = list(input().split())
    if q[0] == "1":
        T = (T+1) % 2
    elif (T == 0 and q[1] == "1") or (T == 1 and q[1] == "2"):
        s += q[2]
    else:
        S += q[2]
if T == 0:
    S = s[::-1] + S
    print(S)
else:
    S = S[::-1] + s
    print(S)

Solution (Python):
from collections import deque
s = input()
q = int(input())
inv = False
pr, su = deque(), deque()
for _ in range(q):
    query = input()
    if query[0] == '1':
        inv = not inv
    else:
        _, f, c = query.split()
        if inv:
            if f == '1': su.append(c)
            else: pr.append(c)
        else:
            if f == '1': pr.append(c)
            else: su.append(c)
ans = ''.join(pr)[::-1] + s + ''.join(su)
print(ans[::-1] if inv else ans)

Solution (Python):

from collections import deque

S=list(input())
q=deque(S)
Q=int(input())
inv=0
for _ in [0]*Q:
    s=input()
    if s=='1':
        inv+=1
    else:
        t,f,c=s.split()
        if ((f=='1') ^ (inv%2==1)):
            q.appendleft(c)
        else:
            q.append(c)
S=[]
if inv%2:
    while q:
        S.append(q.pop())
else:
    while q:
        S.append(q.popleft())
    
print(*S,sep='')

Solution (Python):
def main():
    s = list(input())
    t = []
    q = int(input())

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

        if query[0] == "1":
            s, t = t, s
        elif query[0] == "2":
            if query[1] == "1":
                t.append(query[2])
            elif query[1] == "2":
                s.append(query[2])

    print(*(t[::-1] + s), sep="")


if __name__ == "__main__":
    main()

Solution (Python):
from collections import deque


def main():
    s = input()
    q = int(input())
    d = deque(s)
    is_reversed = False

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

        if query[0] == "1":
            if is_reversed:
                is_reversed = False
            else:
                is_reversed = True

        elif query[0] == "2":
            if query[1] == "1":
                if is_reversed:
                    d.append(query[2])
                else:
                    d.appendleft(query[2])
            elif query[1] == "2":
                if is_reversed:
                    d.appendleft(query[2])
                else:
                    d.append(query[2])

    if is_reversed:
        print(*(list(d)[::-1]), sep="")
    else:
        print(*list(d), sep="")


if __name__ == "__main__":
    main()

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

rev=0

left=""
right=""

for i in range(q):
    
    x = list(map(str,input().split()))
    
    if int(x[0]) == 1:
        rev ^= 1
        
    else:
        if int(x[1])-1 ^ rev == 0:
            left = x[2] + left
        else:
            right = right + x[2]
            
ans = left + s + right

if rev==1:
    ans=ans[::-1]
        
print(ans)

Solution (Python):
S = list(input())
Q = int(input())
reverse, front, tail = False, [], []

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

    if len(query) == 1:
        reverse ^= 1

    elif len(query) == 3:
        f, c = int(query[1]), query[2]
        f -= 1

        if f ^ reverse:
            tail.append(c)
        else:
            front.append(c)

if reverse:
    print(''.join((front[::-1] + S + tail)[::-1]))
else:
    print(''.join(front[::-1] + S + tail))

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

from collections import deque
s = deque(s)
count = 0
for i in range(q):
    query = input()
    if query == "1":
        count += 1
    else:
        t,f,c = query.split()
        if f == "1":
            if count % 2 == 0:
                s.appendleft(c)
            else:
                s.append(c)
        else:
            if count % 2 == 0:
                s.append(c)
            else:
                s.appendleft(c)
s = "".join(s)
if count % 2 == 0:
    print(s)
else:
    print(s[::-1])

Solution (Python):
from collections import deque

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

flag = True
for _ in range(q):
    temp = input().split()
    if len(temp) == 1:
        flag = not flag
    else:
        if flag:
            if temp[1] == "1":
                s.appendleft(temp[2])
            else:
                s.append(temp[2])
        else:
            if temp[1] == "1":
                s.append(temp[2])
            else:
                s.appendleft(temp[2])

if flag:
    print("".join(s))
else:
    print("".join(list(s)[::-1]))

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

    flag = 0
    query = []

    for _ in range(q):
        temp = input().split()
        if len(temp) == 1:
            flag += 1
        else:
            query.append(temp + [flag%2])

    for q in query:
        if q[-1] == 0:
            if q[1] == "1":
                s.appendleft(q[2])
            else:
                s.append(q[2])
        else:
            if q[1] == "1":
                s.append(q[2])
            else:
                s.appendleft(q[2])

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

main()

Solution (Python):
from collections import deque

def main():

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

    flag = 0
    query = []

    for _ in range(q):
        temp = input().split()
        if len(temp) == 1:
            flag += 1
        else:
            query.append(temp + [flag%2])

    for q in query:
        if q[-1] == 0:
            if q[1] == "1":
                s.appendleft(q[2])
            else:
                s.append(q[2])
        else:
            if q[1] == "1":
                s.append(q[2])
            else:
                s.appendleft(q[2])

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

main()

Solution (Python):

"""
Created on Tue Sep  8 14:42:59 2020

@author: liang
"""

from collections import deque
S = deque(input())
Q = int(input())

reverse = False

for i in range(Q):
    t = input().split()
    if int(t[0]) == 1:
        if reverse == False:
            reverse = True
        else:
            reverse = False
    else:
        F = int(t[1])
        C = t[2]
        if F == 1:
            if reverse == False:
                S.appendleft(C)
            else:
                S.append(C)
        else:
            if reverse == False:
                S.append(C)
            else:
                S.appendleft(C)

                
if reverse == False:
    print(''.join(S))
else:
    print(''.join(reversed(S)))

Solution (Python):

from collections import deque 
S = deque(input())
Q = int(input())
Query = [list(input().split()) for _ in range(Q)]


rev = 0
for Q in Query:
    if Q[0] == '1':
        if rev == 0:
            rev = 1
        else:
            rev = 0
        continue
    if Q[1] == '1':
        if rev == 0:
            S.appendleft(Q[2])
        else:
            S.append(Q[2])
    else:
        if rev == 0:
            S.append(Q[2])
        else:
            S.appendleft(Q[2])

if rev == 1:
    ans = list(reversed(list(S)))
else:
    ans = list(S)
print(''.join(ans))

Solution (Python):
import sys
from sys import exit
from collections import deque
from bisect import bisect_left, bisect_right, insort_left, insort_right 
from heapq import heapify, heappop, heappush
from itertools import combinations, permutations, product
from math import *

sys.setrecursionlimit(10**6)
INF = 10**20
eps = 1.0e-20
MOD = 10**9+7

def lcm(x,y):
    return x*y//gcd(x,y)
def mint():
    return map(int,input().split())
def lint():
    return list(map(int,input().split()))
def ilint():
    return int(input()), list(map(int,input().split()))
def judge(x, l=['Yes', 'No']):
    print(l[0] if x else l[1])
def lprint(l, sep='\n'):
    for x in l:
        print(x, end=sep)
def ston(c, c0='a'):
    return ord(c)-ord(c0)
def ntos(x, c0='a'):
    return chr(x+ord(c0))
class counter(dict):
    def __init__(self, *args):
        super().__init__(args)
    def add(self,x):
        self.setdefault(x,0)
        self[x] += 1

S = input()
L = deque()
for i in range(len(S)):
    L.append(S[i])
Q = int(input())
rev = False
for _ in range(Q):
    q = input()
    if q[0]=='1':
        rev = not rev
    else:
        _,f,c = q.split()
        if rev==(f=='1'):
            L.append(c)
        else:
            L.appendleft(c)
L = list(L)
if rev:
    L = L[::-1]
print(''.join(L))

Solution (Python):
import re
import sys
import math
import itertools
import bisect
from copy import copy
from collections import deque,Counter
from decimal import Decimal
import functools
def v(): return input()
def k(): return int(input())
def S(): return input().split()
def I(): return map(int,input().split())
def X(): return list(input())
def L(): return list(input().split())
def l(): return list(map(int,input().split()))
def lcm(a,b): return a*b//math.gcd(a,b)
sys.setrecursionlimit(10 ** 9)
mod = 10**9+7
cnt = 0
ans = 0
inf = float("inf")
al = "abcdefghijklmnopqrstuvwxyz"
AL = al.upper()

s = deque(X())
n = k()
normal = 0
for i in range(n):
  a = input().split()
  if int(a[0]) == 1:
    normal ^= 1
  else:
    k = int(a[1]) - 1
    k ^= normal
    if k == 1:
      s.append(a[2])
    else:
      s.appendleft(a[2])
print(''.join(s) if normal == 0 else ''.join(list(s)[::-1]))

Solution (Python):
from collections import deque

S = list(input())
Q = int(input())
answer = deque(S)
reverse = False
for _ in range(Q):
  query = input()
  if query == '1':
    reverse ^= True
  else:
    ope, types, alphabet = query.split()
    if types == '1':
      if reverse:
        answer.append(alphabet)
      else:
        answer.appendleft(alphabet)
    else:
      if reverse:
        answer.appendleft(alphabet)
      else:
        answer.append(alphabet)
if reverse:
  result = ''
  for i in range(len(answer)):
    result += answer.pop()
  print(result)
else:
  result = ''
  for i in range(len(answer)):
    result += answer.popleft()
  print(result)

Solution (Python):
from collections import deque
s = deque(list(input()))
n = int(input())
normal = 0
for i in range(n):
  li = input().split()
  if int(li[0]) == 1:
    normal ^= 1
  else:
    k = int(li[1]) - 1
    k ^= normal
    if k == 1:
      s.append(li[2])
    else:
      s.appendleft(li[2])
print(''.join(s) if normal == 0 else ''.join(list(s)[::-1]))

Solution (Python):
def main():
	S = list(input())
	T = []
	reverse = False
	n_q = int(input())
	for i in range(n_q):
		q = input().split(" ")
		if q[0] == "1":
			reverse = not reverse
		elif q[0] == "2":
			f = q[1]
			c = q[2]
			if (f == "1" and not reverse) or (f == "2" and reverse):
				T.append(c)
			elif (f == "1" and reverse) or (f == "2" and not reverse):
				S.append(c)
	if reverse:
		S.reverse()
		ans = S + T
	elif not reverse:
		T.reverse()
		ans = T + S
	print("".join(ans))

main()

Solution (Python):
from queue import deque
S = input()
Q = int(input())
Query = list(input().split() for _ in range(Q))

count = 0
L, R = deque(), deque()

for i in range(Q):
  if Query[i][0] == "1": count += 1
  else:
    if Query[i][1] == "1":
      if count % 2 == 0: L.appendleft(Query[i][2])
      else: R.append(Query[i][2])
    else:
      if count % 2 == 0: R.append(Query[i][2])
      else: L.appendleft(Query[i][2])
L, R = "".join(L), "".join(R)
if count % 2 == 0: print(L + S + R)
else: print(R[::-1] + S[::-1] + L[::-1])

Solution (Python):
S = input()
Q = int(input())
Query = list(input().split() for _ in range(Q))

count = 0
L, R = "", ""

for i in range(Q):
  if Query[i][0] == "1": count += 1
  else:
    if Query[i][1] == "1":
      if count % 2 == 0: L = Query[i][2] + L
      else: R += Query[i][2]
    else:
      if count % 2 == 0: R += Query[i][2]
      else: L = Query[i][2] + L

if count % 2 == 0: print(L + S + R)
else: print(R[::-1] + S[::-1] + L[::-1])