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())
Q = []

for i in range(q):
    Q.append([x for x in input().split()])

bef = ""
aft = ""
flag = True

for i in Q:
    if i[0] == "1":
        flag = not(flag)
    else:
        if i[1] == "1":
            if flag == True:
                bef = i[2]+bef
            else:
                aft = aft+i[2]
        else:
            if flag == True:
                aft = aft+i[2]
            else:
                bef = i[2]+bef

if flag:
    print(bef+s+aft)
else:
    print(aft[::-1]+s[::-1]+bef[::-1])

Solution (Python):

s = input()
q_num = int(input())

mae = ""
ushiro = ""
docchi = True

for i in range(q_num):
    query = input()
    if query == '1':
        if docchi:
            docchi = False
        else:
            docchi = True
    else:
        tmp = query.split(' ')
        f = int(tmp[1])
        c = tmp[2]
        if (f == 1 and docchi) or (f == 2 and not docchi):
            mae = c + mae
        else:
            ushiro = ushiro + c

if docchi:
    print(mae + s + ushiro)
else:
    print(''.join(list(reversed(mae + s + ushiro))))

Solution (Python):
a = list(raw_input())
b = []
q = input()
for _ in xrange(q):
    line = raw_input().split()
    if line[0] == "1":
        a, b = b, a
    elif line[1] == "1":
        b.append(line[2])
    else:
        a.append(line[2])
print ''.join(b[::-1] + a)

Solution (Python):
from sys import stdin
from collections import deque
def main():
    readline=stdin.readline
    S=readline().strip()
    d=deque(S)

    Q=int(readline())
    mode=1
    count=0
    for i in range(Q):
        li=readline().split()
        if li[0]=="1":
            mode+=1
            mode%=2
            count+=1
        else:
            o=li[1]
            c=li[2]
            if mode==1 and o=="1":
                d.appendleft(c)
            elif mode==0 and o=="1":
                d.append(c)
            elif mode==1 and o=="2":
                d.append(c)
            elif mode==0 and o=="2":
                d.appendleft(c)
    res=list(d)
    if count%2==1:
        res=res[::-1]
    print("".join(res))
if __name__=="__main__":
    main()

Solution (Python):
S = input()
Q = int(input())
muki = 1
one = ''
two = ''

for i in range(Q):
    I = input().split()
    if I[0] == '1':
        muki *= -1
    elif I[0] == '2':
        F = int(I[1])
        C = I[2]
        if F==1:
            if muki == 1:
                t = [one, C ]
                one = ''.join(t)
            else:
                t = [two, C ]
                two = ''.join(t)
        elif F==2:
            if muki == 1:
                t = [two, C ]
                two = ''.join(t)
            else:
                t = [one, C ]
                one = ''.join(t)
if muki == 1:
    ANS = [one[::-1],S,two]
    print(''.join(ANS))
else:
    ANS = [two[::-1],S[::-1],one]
    print(''.join(ANS))

Solution (Python):
import sys

s = sys.stdin.readline().strip()
q = int(sys.stdin.readline().strip())

heads = []
tails = []
is_reverse = -1

for i in range(q):
    query = sys.stdin.readline().strip()

    if query[0] == '1':
        is_reverse *= -1
    else:
        t, f, c = query.split()
        if f == '1':  
            if is_reverse == 1:
                tails.append(c)
            else:
                heads.append(c)
        else:  
            if is_reverse == 1:
                heads.append(c)
            else:
                tails.append(c)

if is_reverse == 1:
    print(''.join(tails[::-1]) + s[::-1] + ''.join(heads))
else:
    print(''.join(heads[::-1]) + s + ''.join(tails))

Solution (Python):

import sys
read = sys.stdin.readline


def read_ints():
    return list(map(int, read().split()))


def read_a_int():
    return int(read())


def read_tuple(H):
    '''
    H is number of rows
    '''
    ret = []
    for _ in range(H):
        ret.append(tuple(map(int, read().split())))
    return ret


def read_col(H, n_cols):
    '''
    H is number of rows
    n_cols is number of cols

    A列、B列が与えられるようなとき
    '''
    ret = [[] for _ in range(n_cols)]
    for _ in range(H):
        tmp = list(map(int, read().split()))
        for col in range(n_cols):
            ret[col].append(tmp[col])

    return ret


def read_matrix(H):
    '''
    H is number of rows
    '''
    return [list(map(int, read().split())) for _ in range(H)]


def read_map(H):
    '''
    H is number of rows
    文字列で与えられた盤面を読み取る用
    '''
    return [read()[:-1] for _ in range(H)]


def read_map_as_int(H):
    '''
    
    '''
    ret = []
    for _ in range(H):
        ret.append([1 if s == '
    return ret


from collections import deque
S = deque(read()[:-1])
Q = read_a_int()


is_flip = 0  
for _ in range(Q):
    q = input().split()
    if len(q) == 1:
        is_flip = 1 - is_flip
    else:
        _, f, c = q
        f = int(f)
        if is_flip:
            if f == 1:
                S.append(c)
            else:
                S.appendleft(c)
        else:  
            if f == 1:
                S.appendleft(c)
            else:
                S.append(c)

if is_flip:
    print(''.join(S)[::-1])
else:
    print(''.join(S))

Solution (Python):
s=list(input())
n=int(input())
from collections import deque
d=deque(s)
cnt=0
for i in range(n):
    a=list(input().split())
    if len(a)==1:
        cnt+=1
    else:
        if cnt%2==0:
            if a[1]=="1":
                d.appendleft(a[2])
            else:
                d.append(a[2])
        else:
            if a[1]=="2":
                d.appendleft(a[2])
            else:
                d.append(a[2])
if cnt%2==1:
    d=list(d)
    d=d[::-1]
    print("".join(d))
else:
    print("".join(d))

Solution (Python):
from collections import deque
X = input()
S = deque()
for e in X:
    S.append(e)
Q = int(input())
flip = 0
for i in range(Q):
    query = input()
    if query == str(1):
        flip += 1
    else:
        f = int(query[2])
        c = query[-1]
        if f == 1:
            if flip%2 == 1:
                S.append(c)
            else:
                S.appendleft(c)
        else:
            if flip%2 == 1:
                S.appendleft(c)
            else:
                S.append(c)
S = "".join(S)
if flip%2 == 1:
    print(S[::-1])
else:
    print(S)

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

Solution (Python):
import sys


S = str(input())
q = int(input())

p = ''
s = ''
cur = 0
for i in range(q):
    query = input().split()
    
    if len(query) == 1:
        if cur == 0:
            cur = 1
        else:
            cur = 0
    else:
        t, f, c = query
        if f == '1':
            if cur == 0:
                p = c + p
            else:
                s = s + c
        else:
            if cur == 0:
                s  = s + c
            else:
                p = c + p
ans = p+S+s
if cur == 1:
    ans = ''.join(list(reversed(ans)))
print(ans)

Solution (Python):
S=[ord(c) for c in input()]
l,r=2*10**5-1,2*10**5+len(S)
S=[-1]*(2*10**5)+S+[-1]*(2*10**5)
x=0
Q=int(input())
qs=[input() for i in range(Q)]
for q in qs:
    if q=='1':
        x^=1
    else:
        _,f,c=q.split()
        if x==0 and int(f)==1 or x==1 and int(f)==2:
            S[l]=ord(c)
            l-=1
        else:
            S[r]=ord(c)
            r+=1
print(''.join([chr(c) for c in S[l+1:r]])[::(-1 if x else 1)])

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=deque(input())
    Q=I()
    dir=0
    
    for _ in range(Q):
        q=list(input().split())
        if q[0]=="1":
            dir+=1
            dir%=2
        else:
            dir2=dir
            if q[1]=="2":
                dir2+=1
                dir2%=2
            if dir2==0:
                S.appendleft(q[2])
            else:
                S.append(q[2])
            
    ans=list(S)
    
    if dir==1:
        ans=ans[::-1]
        
    print(''.join(map(str, ans)))
        

main()

Solution (Python):
import sys
range = xrange
input = raw_input

S = input()
start = []
end = []
par = 1

q = int(input())
for _ in range(q):
    quer = input()
    if quer == '1':
        par ^= 1
    else:
        _, f, c = quer.split()
        f = int(f) - 1
        if par ^ f:
            start.append(c)
        else:
            end.append(c)

T = ''.join([''.join(reversed(start)), S, ''.join(end)])

if par:
    print T
else:
    print T[::-1]

Solution (Python):
import sys
def input(): return sys.stdin.readline().strip()
S = input()
Q = int(input())
flag = True
f = ''
b = ''
for i in range(Q):
    q = list(input().split())
    if q[0] == '1':
        flag = not flag
    else:
        if flag:
            if q[1] == '1':
                f = q[2] + f
            else:
                b += q[2]
        else:
            if q[1] == '1':
                b += q[2]
            else:
                f = q[2] + f
S = f + S + b
if not flag:
    S = S[::-1]
print(S)

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

p = 0

for i in range(Q):
    query = input().split()
    if query[0]=='1':
        p ^= 1
    else:
        f = int(query[1])-1
        c = query[2]
        if p:
            f ^= 1
        if f:
            S.append(c)
        else:
            S.appendleft(c)

if p:
    print(*reversed(S), sep='')
else:
    print(*S, sep='')

Solution (Python):
s=input()
n=int(input())
a,b=[],[]
p=0
for i in range(n):
    l=input().split()
    if len(l)==1:
        p+=1
    else:
        k=int(l[1])
        if k%2==p%2:
            b+=[l[2]]
        else:
            a+=[l[2]]
if p%2==1:
    s=s[::-1]
    a,b=b,a
print(("".join(a)[::-1])+s+"".join(b))

Solution (Python):

S = list(str(input()))
Q = int(input())
T = 0
r1 = [] 
r2 = [] 

for i in range(Q):
    q = list(map(str,input().split()))
    if len(q) == 1:
        T+=1
    else:
        f,c = q[1],q[2]
        if f == "1":
            if T%2 == 0:
                r2.append(c)
            else:
                r1.append(c)
            
        else:
            if T%2 == 0:
                r1.append(c)
            else:
                r2.append(c)
        
if T%2 == 0:
    r2l = len(r2)
    ans = ""
    for i in range(r2l):
        ans+=r2[r2l-i-1]
    for s in S:
        ans+=s
    for r in r1:
        ans+=r
        
else:
    ans = ""
    r1l = len(r1)
    sl = len(S)
    for i in range(r1l):
        ans+=r1[r1l-i-1]
    for i in range(sl):
        ans+=S[sl-i-1]
    for r in r2:
        ans+=r
        
print(ans)

Solution (Python):
s=input()
q=int(input())
flag=1
a=""
b=""
for _ in range(q):
    query=input().split()
    if query[0]=="1":
        flag*=-1
    else:
        if flag==1:
            if query[1]=="1":
                a+=query[2]
            else:
                b+=query[2]
        else:
            if query[1]=="1":
                b+=query[2]
            else:
                a+=query[2]
if flag==1:
    print(a[::-1]+s+b)
else:
    print(b[::-1]+s[::-1]+a)

Solution (Python):
import sys
from collections import deque

s = sys.stdin.readline().rstrip()
Q = int(sys.stdin.readline().rstrip())
Q = [sys.stdin.readline().split() for _ in range(Q)]

def main():
    res = deque(s)
    flag = 0
    cnt = 0
    for q in Q:
        if q[0] == '1':
            flag ^= 1
            cnt += 1
        else:
            f, c = q[1:]
            f = int(f) - 1
            if flag ^ f:
                res.append(c)
            else:
                res.appendleft(c)
    
    res = ''.join(list(res))
    if cnt & 1:
        res = res[::-1]
    return res

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

Solution (Python):
import collections
s = input()
head = []
tail = []
is_reveresed = False
q = int(input())
for i in range(q):
    query = input()
    if query[0] == '1':
        is_reveresed = not is_reveresed
    else:
        f = query[2]
        c = query[4]
        if is_reveresed:
            if f == '1':
                tail.append(c)
            else:
                head.append(c)
        else:
            if f == '1':
                head.append(c)
            else:
                tail.append(c)
if is_reveresed:
    print('{}{}{}'.format(''.join(tail[::-1]), s[::-1], ''.join(head)))
else:
    print('{}{}{}'.format(''.join(head[::-1]), s, ''.join(tail)))

Solution (Python):
from collections import deque

s = input()
d = deque()
for i in s:
    d.append(i)
q = int(input())

state = 0


for i in range(q):
    In = input()
    if In == '1':
        state = (state + 1)%2
    else:
        t,f,c = In.split()
        k = (f=='2')
        if (k+state)%2==0:
            d.appendleft(c)
        else:
            d.append(c)
if state==0:
    print(*list(d),sep='')
else:
    t = list(d)[::-1]
    print(*t, sep='')

Solution (Python):
import sys
def main():
    input = sys.stdin.readline
    S = input().rstrip()
    Q = int(input())
    prefix = []
    suffix = []
    reverse = False
    for _ in range(Q):
        q = input().rstrip().split()
        if q[0] == '1': reverse = not reverse
        else:
            is_pre = (q[1] == '1' and reverse == False) or (q[1] == '2' and reverse)
            if is_pre:
                prefix.append(q[2])
            else:
                suffix.append(q[2])

    if reverse:
        S = S[::-1]
        prefix, suffix = suffix, prefix
    prefix.reverse()
    print(''.join(prefix) + S + ''.join(suffix))

if __name__ == '__main__':
    main()

Solution (Python):
from collections import deque
S=deque(input())
Q=int(input())
h=0
for i in range(Q):
    q=input().split()
    if q[0]=="1":
        h=1-h
    else:
        t=int(q[1])
        if t==h+1:
            S.appendleft(q[2])
        else:
            S.append(q[2])
if h==0:
    for i in S:
        print(i,end="")
else:
    for i in range(len(S)):
        print(S[-i-1],end="")

Solution (Python):
from collections import deque

dq = deque(list(input()))

Q = int(input())

reverse_count = 0

for i in range(Q):
    query = input().split()
    if query[0] == "1":
        reverse_count = 1 - reverse_count
    else:
        F = query[1]
        C = query[2]
        if F == "1":
            if reverse_count == 0:
                dq.appendleft(C)
            else:
                dq.append(C)
        else:
            if reverse_count == 0:
                dq.append(C)
            else:
                dq.appendleft(C)

if reverse_count == 0:
    print("".join(dq))
else:
    print("".join(reversed(dq)))

Solution (Python):
import sys
readline = sys.stdin.readline
sys.setrecursionlimit(20000)

def input():
    return readline().rstrip()

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

prefix = []
suffix = []
flip = 0
for i in range(Q):
    T = input()
    if T == "1":
        prefix, suffix = suffix, prefix
        flip = 1 - flip
    else: 
        _, f, c = T.split()
        if f == "1":
            
            prefix.append(c)
        elif f == "2":
            
            suffix.append(c)

if flip == 1:
    S = S[::-1]
print("".join(prefix[::-1]), end="")
print(S, end="")
print("".join(suffix))

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

S_tmp = str(S)
Sl = []
Sr = []
flg_inv = 0
for i, qi in enumerate(q):
    if qi[0] == '1':
        flg_inv = 1 - flg_inv
    else:
        Fi = int(qi[1])
        Ci = qi[2]
        if (Fi == 1 and flg_inv == 0) or (Fi == 2 and flg_inv == 1):
            Sl.append(Ci)
        else:
            Sr.append(Ci)


if flg_inv == 0:
    ans = ''.join(Sl[::-1] + S + Sr)
else:
    ans = ''.join(Sr[::-1] + S[::-1] + Sl)

print(ans)

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

s = raw_input().strip()
q = input()
inp = stdin.readlines()

Q = deque(list(s))
rev = 0
for line in inp:
    x = line.strip().split()
    if x[0] == '1':
        rev ^= 3
    else:
        f = int(x[1])
        ch = x[2]
        f ^= rev
        if f == 1:
            Q.appendleft(ch)
        else:
            Q.append(ch)

l = []
while Q:
    if rev == 0: l.append(Q.popleft())
    else: l.append(Q.pop())

print ''.join(l)

Solution (Python):
from collections import deque
a = list(input())
a = deque(a)
re = 0
for i in range(int(input())):
  b = input().split()
  if b[0] == "1":
    re = (re+1)%2
  else:
    if (re and b[1] == "2") or (not(re) and b[1] == "1"):
      a.appendleft(b[2])
    else:
      a.append(b[2])
a = list(a)
if re:
  a = a[::-1]
print("".join(a))

Solution (Python):
from collections import deque

Ss = input()
Q = int(input())

anss = deque(Ss)
isRev = False
for _ in range(Q):
    vs = input().split()
    if vs[0] == '1':
        isRev = not isRev
    else:
        F, C = vs[1], vs[2]
        if F == '1':
            if isRev:
                anss.append(C)
            else:
                anss.appendleft(C)
        else:
            if isRev:
                anss.appendleft(C)
            else:
                anss.append(C)



anss = ''.join(anss)
if isRev:
    anss = anss[::-1]

print(anss)

Solution (Python):
from collections import deque

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

inv = 0
for i in range(Q):
  query = input().split()
  if query == ['1']:
    inv = (inv + 1)%2
  else:
    f, c = query[1], query[2]
    if f == '1':
      if inv:
        S.append(c)
      else:
        S.appendleft(c)
    else:
      if inv:
        S.appendleft(c)
      else:
        S.append(c)

S = list(S)
if inv:
  print(''.join(S[::-1]))
else:
  print(''.join(S))

Solution (Python):
from collections import deque
def main():
    s = list(input().rstrip())
    s = deque(s)
    q = int(input())
    f = True
    for _ in range(q):
        a = list(map(str, input().rstrip().split()))
        if a[0] == "1":
            if f:
                f = False
            else:
                f = True
        else:
            if a[1] == "1":
                if f:
                    s.appendleft(a[2])
                else:
                    s.append(a[2])
            else:
                if f:
                    s.append(a[2])
                else:
                    s.appendleft(a[2])
    if f:
        for c in s:
            print(c, end="")
    else:
        for c in reversed(s):
            print(c, end="")

if __name__ == "__main__":
    main()

Solution (Python):
from collections import deque
s=input()
q=int(input())
flg=True
s1=deque()
s2=deque()
for _ in range(q):
    t=list(input().split())
    if t[0]=='1':
        flg=not flg
    else:
        if t[1]=='1':
            if flg:
                s1.appendleft(t[2])
            else:
                s2.append(t[2])
        else:
            if flg:
                s2.append(t[2])
            else:
                s1.appendleft(t[2])
if flg:
    print(''.join(s1)+s+''.join(s2))
else:
    print(''.join(s2)[::-1]+s[::-1]+''.join(s1)[::-1])

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

if __name__ == "__main__":
    S = input()
    Q = int(input())
    mode = 0
    d = deque()
    for s in S:
        d.append(s)
    
    for i in range(Q):
        arr = list(input().split())
        if len(arr) == 1:
            mode ^= 1
        else:
            if arr[1] == '1':
                if mode == 0:
                    
                    d.appendleft(arr[2])
                else:
                    d.append(arr[2])
            else:
                if mode == 0:
                    d.append(arr[2])
                else:
                    d.appendleft(arr[2])
        
    ans = ""
    while len(d):
        if mode == 0:
            ans += d.popleft()
        else:
            ans += d.pop()
    print(ans)

Solution (Python):
from collections import Counter,defaultdict,deque
from bisect import bisect_left
import sys,math,itertools,pprint,fractions,time
sys.setrecursionlimit(10**8)
mod = 10**9+7
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))

s = input()
n = len(s)
Q = inp()
q = deque()
for i in s:
    q.append(i)
rev = 0
for _ in range(Q):
    tmp = input().split()
    if tmp[0] == '1':
        rev ^= 1
    else:
        if tmp[1] == '1':
            if rev==0: q.appendleft(tmp[2])
            else: q.append(tmp[2])
        else:
            if rev==0: q.append(tmp[2])
            else: q.appendleft(tmp[2])
    

res = list(q)
if rev: res = res[::-1]

print(''.join(res))

Solution (Python):
import collections
S = list(input())
s = collections.deque(S)
Q = int(input())
mode = 1
for _ in range(Q):
    tmp = tuple(input().split())
    if len(tmp) == 1:
        mode = 1 ^ mode
    else:
        t, f, c = tmp
        f = int(f)
        f -= 1
        if mode ^ f:
            s.appendleft(c)
        else:
            s.append(c)
if mode:
    print("".join(list(s)))
else:
    print("".join(list(s)[::-1]))

Solution (Python):
from collections import deque

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

inv_cnt = 0


li = []
r = []

for i in range(q):
    d = [x for x in input().split()]
    if d[0] == '1':
        inv_cnt += 1
        inv_cnt %= 2
    else:
        if inv_cnt == 0:
            if d[1] == '1':
                li.append(d[2])
            else:
                r.append(d[2])
        else:
            if d[1] == '1':
                r.append(d[2])
            else:
                li.append(d[2])

if inv_cnt == 0:
    print(''.join(li[::-1]) + s + ''.join(r))
else:
    print(''.join(r[::-1]) + s[::-1] + ''.join(li))

Solution (Python):
s=input()
a=[]
b=[]
d=0
for i in range(int(input())):
  q=input()
  if q=="1":d^=1
  else:
    _,f,c=q.split()
    f=int(f)-1
    if d==0:
      if f==0:a.append(c)
      else:b.append(c)
    else:
      if f==1:a.append(c)
      else:b.append(c)
if d==0:print("".join(a)[::-1]+s+"".join(b))
else:print("".join(b)[::-1]+s[::-1]+"".join(a))

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):
import sys
input = lambda: sys.stdin.readline().rstrip()
s=input()
q=int(input())
sign=1
F,B="",""
for i in range(q):
  Q=[str(i) for i in input().split()]
  if Q[0]=="1":
    sign*=-1
  else:
    f,c=Q[1:]
    if f=="1" and sign==1:
      F=c+F
    elif f=="2" and sign==-1:
      F=c+F
    else:
      B=B+c
ans=F+s+B
print(ans if sign==1 else ans[::-1])

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

state = 0
start = []
end = []

for i in range(Q):
    x = list(input().split())
    if x[0] == "1":
        state = (state+1)%2
    else:
        x[1] = int(x[1])
        if (state + x[1]) % 2 == 1:
            start.append(x[2])
        else:
            end.append(x[2])

if state == 0:
    start.reverse()
    ans = start + S + end
    
else:
    end.reverse()
    S.reverse()
    ans = end + S + start

print("".join(ans))

Solution (Python):
import sys
from collections import deque

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def SI(): return sys.stdin.readline()[:-1]

def main():
    s=SI()
    q=II()
    aa=deque(ord(c) for c in s)
    rev=0
    for _ in range(q):
        t=SI().split()
        if t[0]=="1":rev=1-rev
        else:
            f=int(t[1])-1
            if f^rev:aa.append(ord(t[2]))
            else:aa.appendleft(ord(t[2]))
    if rev:aa.reverse()
    print("".join(chr(a) for a in aa))

main()

Solution (Python):
import sys
sys.setrecursionlimit(1000000000)
import math
from fractions import gcd
def lcm(a, b): return a * b // gcd(a, b)
from itertools import count, permutations
from functools import lru_cache
from collections import deque, defaultdict
from pprint import pprint
ii = lambda: int(input())
mis = lambda: map(int, input().split())
lmis = lambda: list(mis())
INF = float('inf')
N1097 = 10**9 + 7

def meg(f, ok, ng):
    while abs(ok-ng)>1:
        mid = (ok+ng)//2
        if f(mid):
            ok=mid
        else:
            ng=mid
    return ok

def get_inv(n, modp):
    return pow(n, modp-2, modp)

def factorials_list(n, modp):    
    fs = [1]
    for i in range(1, n+1):
        fs.append(fs[-1] * i % modp)
    return fs

def invs_list(n, fs, modp):     
    invs = [get_inv(fs[-1], modp)]
    for i in range(n, 1-1, -1):
        invs.append(invs[-1] * i % modp)
    invs.reverse()
    return invs

def comb(n, k, modp):
    num = 1
    for i in range(n, n-k, -1):
        num = num * i % modp
    den = 1
    for i in range(2, k+1):
        den = den * i % modp
    return num * get_inv(den, modp) % modp

def comb_from_list(n, k, modp, fs, invs):   
    return fs[n] * invs[n-k] * invs[k] % modp



class UnionFindEx:
    def __init__(self, size):
        
        self.roots = [-1] * size
    def getRootID(self, i):
        r = self.roots[i]
        if r < 0:   
            return i
        else:
            r = self.getRootID(r)
            self.roots[i] = r
            return r
    def getGroupSize(self, i):
        return -self.roots[self.getRootID(i)]
    def connect(self, i, j):
        r1, r2 = self.getRootID(i), self.getRootID(j)
        if r1 == r2:
            return False
        if self.getGroupSize(r1) < self.getGroupSize(r2):
            r1, r2 = r2, r1
        self.roots[r1] += self.roots[r2]    
        self.roots[r2] = r1
        return True

def main():
    S = deque(input())
    right = True
    for _ in range(ii()):
        q = input()
        if len(q) == 1:
            right ^= 1
        else:
            t, f, c = q.split()
            if right ^ (f=='1'):
                S.append(c)
            else:
                S.appendleft(c)
    if not right:
        S.reverse()
    print(*S, sep='')


main()

Solution (Python):
def main():
    from collections import deque
    S = input()
    Q = int(input())
    S = deque(S)
    flag = False
    for query in range(Q):
        a, *bc = (i for i in input().split())
        
        if a == "1":
            flag = not(flag)
        else:
            if flag:
                if bc[0] == "1":
                    S.append(bc[1])
                elif bc[0] == "2":
                    S.appendleft(bc[1])
            else:
                if bc[0] == "1":
                    S.appendleft(bc[1])
                elif bc[0] == "2":
                    S.append(bc[1])
    S = "".join(S)
    if flag:
        S = S[::-1]
    print(S)


if __name__ == '__main__':
    main()

Solution (Python):


import collections


def prepare_str(s, queries):
    flip = False
    dq = collections.deque(s)
    for qid, fc in queries:
        if qid == 1:
            flip = not flip
        else:
            f = int(fc[0])
            c = fc[1]
            if flip:
                f = 3 - f
            if f == 1:
                dq.appendleft(c)
            else:
                dq.append(c)
    if flip:
        res = "".join(reversed(dq))
    else:
        res = "".join(dq)
    return res


def main():
    s = input()
    q = int(input())
    queries = []
    for _ in range(q):
        ps = input().split()
        qid = int(ps[0])
        fc = tuple(ps[1:])
        queries.append((qid, fc))
    res = prepare_str(s, queries)
    print(res)


if __name__ == "__main__":
    main()

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

from collections import deque
s = deque(S)

mode = True
for query in queries:
    if len(query) == 1:
        mode = False if mode else True
        continue
    f = int(query[1])
    c = query[2]
    if mode:
        if f == 1:
            s.appendleft(c)
        else:
            s.append(c)
    else:
        if f == 2:
            s.appendleft(c)
        else:
            s.append(c)

if mode:
    print(*s, sep='')
else:
    print(*list(s)[::-1], sep='')