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



Sample input:
a
4
2 1 p
1
2 2 c
1


Sample output:
cpa

Submission status: Runtime Error

Solution code:
# ABC158 D
from collections import deque
s=input()
q=int(input())
l=[]

for i in range(q):
    l.append(list(map(str,input().split())))

cnt=0
front_str=''
end_str=''

t_str=deque(s)
front_str=deque(front_str)
end_str=deque(end_str)

for ll in l:
    if ll[0]=='1':
        cnt+=1
    elif ll[0]=='2':
        if cnt%2==0:
            if ll[1]=='1':
                front_str.appendleft(ll[2])
            elif ll[1]=='2':
                end_str.append(ll[2])
        elif cnt%2==1:
            if ll[1]=='1':
                end_str.appendleft(ll[2])
            elif ll[1]=='2':
                front_str.appendleft(ll[2])
ans=front_str+t_str+end_str
ans=list(ans)

if cnt%2==0:
    ans=''.join(ans)
else:
    ans=ans[::-1]
    ans=''.join(ans)
print(ans)

Submission status: Runtime Error

Solution code:
from collections import deque
s = deque(input())
num = int(input())
for i in range(num):
  a = deque(input().split())
  start = int(a[0])
  if start == 2:
    ns = int(a[1])
    if ns == 1:
      s.appendleft(a[2])
      #s = a[2] + s
    else:
      #s = s + a[2]
      s.append(a[2])
  else:
    s = s[::-1]
for i in range(len(s)):
  print(s[i], end='')

Submission status: Runtime Error

Solution code:
from collections import deque

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

for i in range(q):
    que = input().split()
    if que[0] == 1:
        S.reverse()
        
    else:
        if que[1]==1:
            S.appendleft(que[-1])
        else: S.append(que[-1])
        
ans = "".join(S)
print(ans)

Submission status: Runtime Error

Solution code:
from collections import deque

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

for i in range(q):
    que = input().split()
    if que[0] == 1:
        S.reverse()
        
    else:
        if que[1]==1:
            S.appendleft(que[-1])
        else: S.append(que[-1])
        
S = "".join(S)
print(S)

Submission status: Runtime Error

Solution code:
import sys
def input(): return sys.stdin.buffer.readline().rstrip()
S = input()
Q = int(input())
qs = list(input() for _ in range(Q))
rev = False
for q in qs:
    q = q.split()
    if q[0] == '1':
        rev = not(rev)
    else:
        if (q[1]=='1' and rev is False) or (q[1]=='2' and rev is True):
            S = q[2] + S
        else:
            S = S + q[2]
print(S if rev == 0 else S[::-1])

Submission status: Runtime Error

Solution code:
from collections import deque

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

#s = deque(s)

inv = 0
for q in query :
    if q[0] == '1':
        inv += 1
    else:
        _,f,c = q.split()
        if f == '1':
            if inv % 2 == 0:
                s.appendleft(c)
            else:
                s.append(c)
        else:
            if inv % 2 ==0:
                s.append(c)
            else:
                s.appendleft(c)
s = ''.join(s)
print(s if inv % 2 == 0 else s[::-1])

Submission status: Runtime Error

Solution code:
from collections import deque

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

#s = deque(s)

inv = 0
for q in query :
    if q[0] == '1':
        inv += 1
    else:
        _,f,c = q.split()
        if f == '1':
            if inv % 2 == 0:
                s.append(c)
            else:
                s.appendleft(c)
s = ''.join(s)
print(s if inv % 2 == 0 else s[::-1])

Submission status: Runtime Error

Solution code:
s=input()
q=int(input())
from collections import deque
qq=deque(s)
cnt=0
for i in range(q):
    j=input()
    if j=="1":
        cnt+=1
        continue
    _,k,l=j.split()
    if (k=="2")^cnt%2:
        qq.append(l)
    else:
        qq.appendleft(l)

if cnt%2==1:
    print("".join(revesed(list(qq))))
else:
    print("".join(qq))

Submission status: Runtime Error

Solution code:
from collections import deque

string = input()
s = deque(string)
iteration = int(input())
reverse = 1

for c in range(0,iteration):
    sousa = input().split()
    #操作１のとき
    if sousa[0] == "1":
        reverse = reverse*(-1)
    #操作２を行うとき
    elif sousa[1] == "1":
        if reverse == 1:
            s.appendleft(sousa[2])
        else:
            s.append(sousa[2])
    else:
        if reverse == 1:
            s.append(sousa[2])
        else:
            s.appendleft(sousa[2])

if reverse == -1:
    s = s[::-1]

result = "".join(s)
print(result)

Submission status: Runtime Error

Solution code:
from collections import deque
s=deque(input())
n=int(input())
r=0
for i in range(n):
    strlist=input()
    if strlist[0]=='1':
        r+=1
    elif strlist[0]=='2' and strlist[2] == '1':
        if r%2==0:
            s.appendleft(strlist[4])
        else:
            s.append(strlist[4])
    elif strlist[0]=='2' and strlist[2] =='2':
        if r%2==0:
            s.append(strlist[4])
        else:
            s.appendleft(strlist[4])
if r%2==0:
    for s_ in s:
        print(s_, end='')
else:
    for s_ in s.reverse():
        print(s_, end='')

Submission status: Runtime Error

Solution code:
from collections import deque
s=deque(input())
n=int(input())
r=0
for i in range(n):
    strlist=input()
    if strlist[0]=='1':
        r+=1
    elif strlist[0]=='2' and strlist[2] == '1':
        if r%2==0:
            s.appendleft(strlist[4])
        else:
            s.append(strlist[4])
    else:
        if r%2==0:
            s.append(strlist[4])
        else:
            s.appendleft(strlist[4])
if r%2==0:
    for s_ in s:
        print(s_, end='')
else:
    for s_ in s[::-1]:
        print(s_, end='')

Submission status: Runtime Error

Solution code:
s=input()
n=int(input())
strlist=[input() for i in range(n)]
for i in range(n):
  if i==1:
    s=s[::-1]
  elif i==2 and strlist[i][2] == 1:
    s = '{}{}'.format(strlist[i][4],s)
  elif i==2 and strlist[i][2] == 2:
    s = '{}{}'.format(s,strlist[i][4])
print(s)

Submission status: Runtime Error

Solution code:
from collections import deque

N = deque(input())
Q = int(input())
Qs = []
for _ in range(Q):
    Qs.append(list(map(str, input().split())) ) 
k=0
for q in Qs:
    if q[0] == '1':
        k+=1
    else:
        if k%2 ==0:
            if q[1]=='1':
                N.appendleft(q[2])
            else:
                N.append(q[2])
        else:
            if q[1]=='2':
                N.appendleft(q[2])
            else:
                N.append(q[2])
if k%2==1:
    ans=''
    for x in N.rotate():
        ans += x
else:
    ans=''
    for x in N:
        ans += x
        
print(ans)

Submission status: Runtime Error

Solution code:
def solve2(s):
    ans = 0
    for i, c in enumerate(s): #見る数字を右に広げていく
        if c in '02468':
            ans += i + 1 # 見ている数字のの一番右の桁が偶数ならその頭に乗っている数字の数だけ
                        # 答えに追加する
                        # 例 2254とし2→偶数、22→偶数（さらに先頭の2をどけても偶数）
                        # →225（奇数）→2254（偶数、上の3つをそれぞれどけても偶数）
    return ans


def solve5(s): # p==2のときと同じ原理
    ans = 0
    for i, c in enumerate(s):
        if c in '05':
            ans += i + 1
    return ans


def solve_other(s, p):
    ans = 0
    last_mod=0
    mods = [0]*p
    mods[0] = 1 #初期値として何も選んでない状態を1個加えとく
    digit = 1
    for i in range(n-1,-1,-1): # 一番右のけたから見る
        last_mod = (last_mod + int(s[i])*digit)%p # 最新のmodに新しく見ている桁*(10**x)を足してmod計算
        ans += mods[last_mod] # modリストに直前までのの値を答えに追加
        mods[last_mod] += 1 # modリスト更新
        digit *=10 # 桁上げ
    return ans


n, p = list(map(int, input().split()))
s = input()
if p == 2:
    print(solve2(s))
elif p == 5:
    print(solve5(s))
else:
    print(solve_other(s, p))

Submission status: Runtime Error

Solution code:
s=input()
n=int(input())
rev=1
pre=''
suf=''

for i in range(n):
  q=input().split()
  if q[0]=='1':
    rev+=1
  elif (rev+int(q[1]))%2==0:
    pre=q[2]+pre
  else:
    suf=suf+q[2]
    
ans=pre+s+suf
  
if rev%2==0:
  ans.reverse()

print(ans)

Submission status: Runtime Error

Solution code:
# ABC158 D
from collections import deque
s=input()
q=int(input())
l=[]

for i in range(q):
    l.append(list(map(str,input().split())))

cnt=0
front_str=''
end_str=''
t_str=deque(s)
front_str=deque(front_str)
end_str=deque(end_str)
for ll in l:
    if ll[0]=='1':
        cnt+=1
    elif ll[0]=='2':
        if cnt%2==0:
            if ll[1]=='1':
                front_str.appendleft(ll[2])
            elif ll[1]=='2':
                end_str.append(ll[2])
        elif cnt%2==1:
            if ll[1]=='1':
                end_str.appendleft(ll[2])
            elif ll[1]=='2':
                front_str.appendleft(ll[2])
ans=front_str+t_str+end_str
ans=list(ans)

if cnt%2==0:
    ans=''.join(ans)
else:
    ans=ans[::-1]
    ans=''.join(ans)
print(ans)

Submission status: Runtime Error

Solution code:
s=input()
n=int(input())
rev=1

for i in range(n):
  q=input().split()
  if q[0]=='1':
    rev+=1
  elif (rev+int(q[1]))%2==0:
    s=q[2]+s
  else:
    s=s+q[2]
    
if rev%2==0:
  s.reverse()

print(s)

Submission status: Runtime Error

Solution code:
from collections import deque

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

S1 = deque('')
S2 = deque('')
rev = False

for TFC in Query:
    if TFC[0] == '1':
        rev = not rev
    else:
        if TFC[1] == '1':
            if rev:
                S2.append(TFC[2])
            else:
                S1.appendleft(TFC[2])
        else:
            if rev:
                S1.appendleft(TFC[2])
            else:
                S2.append(TFC[2])
if rev:
    S2 = ''.join(S2[::-1])
    S1 = ''.join(S1[::-1])
    print(S2 + S + S1)
else:
    S1 = ''.join(S1)
    S2 = ''.join(S2)
    print(S1 + S + S2)

Submission status: Runtime Error

Solution code:
s = input()
q = int(input())
isReverse = False
for i in range(0, q):
    query = input()
    if query[0] == '1':
        isReverse = !isReverse 
    elif (query[0] == '2')&(query[2]=='1'):
        if isReverse:
            s = ''.join([s,query[4]])
        else:
            s = ''.join([query[4],s])
    else:
        if isReverse:
            s = ''.join([query[4],s])
        else:
            s = ''.join([s,query[4]])
if isReverse:
    s = s[::-1]

print(s)

Submission status: Runtime Error

Solution code:
from collections import deque

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

S = deque(S)
S1 = deque('')
S2 = deque('')
rev = False

for TFC in Query:
    if TFC[0] == '1':
        rev = not rev
    else:
        if TFC[1] == '1':
            if rev:
                S2.append(TFC[2])
            else:
                S1.appendleft(TFC[2])
        else:
            if rev:
                S1.appendleft(TFC[2])
            else:
                S2.append(TFC[2])
if rev:
    S2.reverse()
    S.reverse()
    S1.reverse()
    print(''.join(S2 + S + S1))
else:
    print(''.join(S1 + S + S2))

Submission status: Runtime Error

Solution code:
s = input()
q = int(input())
hanten = True
atm = ""
kts = ""
for i in range(q):
    l = input().split()
    if l[0] == "1":
        hanten = not hanten
    else:
        if l[1] == "1":
            if hanten == True:
                atm = l[2] + atm
            else:
                kts = kts + l[2]
        else:
            if hanten == True:
                kts = kts + l[2]
            else:
                atm = l[2] + atm

ans = atn + s + kts
if hanten == False:
    print(ans[::-1])
else:
    print(ans)

Submission status: Runtime Error

Solution code:
s = input()
n = int(input())
q = [list(input().split()) for _ in range(n)]

head = [s]
tail = []
reverse = False
for x in q:
    if x[0]=='1':
        reverse = reverse ^ True
    elif x[0]=='2':
        if x[1]=='1' and not reverse:
            head.insert(0, x[2])
        elif x[1]=='1' and reverse:
            tail.insert(0, x[2])
        elif x[1]=='2' and not reverse:
            tail.append(x[2])
        else:
            head.append(x[2])
if reverse:
    head.reverse()
    tail.reverse()
    print(''.join(tail.extend(head)))
else:
    print(''.join(head.extend(tail)))

Submission status: Runtime Error

Solution code:
s = input()
q = int(input())
r = False#ひっくり返されたか
for i in range(q):
  t=list(input().split())
  if len(t) == 1:#一つしか入力されていない
    r = not(r)
  else:
    if t[1] == '1' and r = False
      s = t[2] + s
    elif t[1] == '2' and r = True:
      s = t[2] + s
    else:
      s = s + t[2]
print(s)

Submission status: Runtime Error

Solution code:
rom collections import deque
s = input()
n = int(input())
s = deque(s)
cnt = 0
def ap(s,a,b):
    if flag ==True:
        if a == "1":
            s.appendleft(b)
        elif a == "2":
            s.append(b)
    if flag == False:
        if a == "1": 
            s.append(b)
        elif a == "2":
            s.appendleft(b)
flag = True
for i in range(n):
    L = list(input())
    if L[0] == "1":
        flag = False
        cnt +=1
    elif L[0] == "2":
        ap(s,L[2],L[4])
        flag = True
#print(flag,cnt,s)
if cnt %2 == 1:
    s.reverse()
print("".join(s))

Submission status: Runtime Error

Solution code:
s=input()
q=int(input())
i=0
for _ in range(q):
    a=list(input().split())
    if a[0]=="1":
        i+=1
    else:
        if a[1]=="1" and i%2==1:
            z=z+a[3]
        elif a[1]=="1" and i%2==0:     
            x=a[2]+x
        elif a[1]=="2" and i%2==1:  
            x=x+a[3]
        else:
            z=z+a[3]
print(z[::-1]+s[::-1]+x[::-1] if i%2 else x+s+z)

Submission status: Runtime Error

Solution code:
from collections import deque

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


for i in range(Q):
    
    q = input()
    
    if (q == 1):
        reverse *= -1
    else:
        integer, judge, cha = q.split()
        
        if (judge == '1'):
            if (reverse == 1):
                S.appendleft(cha)
            else:
                S.append(cha)
        elif (judge == '2'):
            if (reverse == -1):
                S.append(cha)
            else:
                S.appendleft(cha)

if (reverse == -1):
    S.reverse()

print(''.join(list(S)))

Submission status: Runtime Error

Solution code:
from math import ceil, floor

A, B = map(int,input().split())

x8min = ceil(A / 0.08) # 8%でA円になる最小値
x8max = floor((A + 1) / 0.08) # 8%でA円になるのは、これ未満
x10min = ceil(B / 0.1) # 10%でB円になる最小値
x10max = floor((B + 1) / 0.1) # 10%でB円になるのは、これ未満

#print(str(x8min))
#print(str(x8max))
#print(str(x10min))
#print(str(x10max))

if (x8min > x10max) or (x8max < x10min):
    print('-1')
else:
    print(str(x8min))

Submission status: Runtime Error

Solution code:
import sys
from collections import deque

# sys.stdin = open('input.txt', 'r')

s = deque([s for s in input()])
q = int(input())
rev = False

for query in sys.stdin:
    if query.strip() == '1': # sys.stdin does not remove line breaks. (vs. input() removes line breaks)
        if rev:
            rev = False
        else:
            rev = True
    else:
        t,f,c = query.split()
        if f == '1':
            if rev == True:
                s.append(s)
            else:
                s.appendleft(c)
        elif f == '2':
            if rev == True:
                s.appendleft(c)
            else:
                s.append(c)
if rev == True:
    s.reverse()
print(''.join(s))

Submission status: Runtime Error

Solution code:
import sys
from collections import deque

s = deque([s for s in input()])
q = int(input())
rev = False

for query in sys.stdin:
    if query.strip() == '1': # sys.stdin does not remove line breaks. (vs. input() removes line breaks)
        if rev:
            rev = False
        else:
            rev = True
    else:
        t,f,c = query.split()
        if f == '1':
            if rev == True:
                s.append(s)
            else:
                s.appendleft(c)
        elif f == '2':
            if rev == True:
                s.appendleft(c)
            else:
                s.append(c)
if rev == True:
    s.reverse()
print(''.join(s))

Submission status: Runtime Error

Solution code:
import sys
from collections import deque

s = deque([s for s in input()])
q = int(input())
rev = False

for query in sys.stdin:
    if query.strip() == '1': # sys.stdin does not remove line breaks. (vs. input() removes line breaks)
        if rev:
            rev = False
        else:
            rev = True
    else:
        t,f,c = query.split()
        if f == '1':
            if rev == True:
                s.append(s)
            else:
                s.appendleft(c)
        elif f == '2':
            if rev == True:
                s.appendleft(c)
            else:
                s.append(c)
if rev == True:
    ans = []
    for i in range(len(s)):
        ans.append(s.pop())
    print(''.join(ans))
else:
    print(''.join(s))

Submission status: Runtime Error

Solution code:
import sys
from collections import deque

s = deque([s for s in input()])
q = int(input())
rev = False

for query in sys.stdin:
    if query.strip() == '1': # sys.stdin does not remove line breaks. (vs. input() removes line breaks)
        if rev:
            rev = False
        else:
            rev = True
    else:
        t,f,c = query.split()
        if f == '1':
            if rev == True:
                s.append(s)
            else:
                s.appendleft(c)
        elif f == '2':
            if rev == True:
                s.appendleft(c)
            else:
                s.append(c)
if rev == True:
    s = s[::-1]
print(''.join(s))

Submission status: Runtime Error

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

flag = True

for i in query:
    if i[0]=="1":
        flag = not flag
#        S=S[::-1]
    else:
        if flag:
            if i[1]=="1":
                S=i[2]+S
            else:
                S=S+i[2]
        else:
            if i[1]=="1":
                S=S+i[2][::-1]
            else:
                S=i[2][::-1]+S
if flag:
    print(S)
else:
    print(S[::-1]

Submission status: Runtime Error

Solution code:
from collections import deque

S = list(input())
Q = int(input())
d = deque(S)
flag = -1
for _ in range(Q):
    q = list(input().split())
    if q[0] == '1':
        flag *= -1
    else:
        f = int(q[1])
        c = q[2]
        if flag == 1 and f == 1 or flag == -1 and f == 2:
            d.appendleft(c)
        else:
            d.append(c)

if flag == -1:
    d = d[::-1]

print(''.join(d))

Submission status: Runtime Error

Solution code:
# Problem D - String Formation

# input process
S = input()
Q = int(input()) # query number

# initialization
sento = 1
mae = [] # mae queue
usiro = [] # usiro

# process
for i in range(Q):
    query_num = input()
    query_1 = int(query_num[0])
    # クエリが１の時
    if query_1==1 and sento==1:
        sento = 10
    elif query_1==1:
        sento = 1
    else: # query num 2
        n, f, c = query_num.split()
        f = int(f)
        if f==1: # 先頭追加処理
            # 先頭の位置によって変わる
            if sento==1:
                mae.append(c)
            else:
                usiro.append(c)
        else: # 末尾追加処理
            if sento==1:
                usiro.append(c)
            else:
                mae.append(c)

# output process
if sento==1:
    ans = ""
    mae.reverse()
    for m in mae:
        ans = ans + m
    ans = ans + S
    for u in usiro:
        ans = ans + s
    print(ans)
else:
    ans = ""
    usiro.reverse()
    for u in usiro:
        ans = ans + u
    ans = ans + S[::-1]
    for m in mae:
        ans = ans + m
    print(ans)

Submission status: Runtime Error

Solution code:
S = input()
Slist = [S[i] for i in range(len(S))]
Q = int(input())
Query = [list(input().split()) for i in range(Q)]
Qlen = len(Query)
reverse = False
back = []
head = []

for idx, value in enumerate(Query):
    if value[0] == "1":
        reverse ^= True
        head, back = back, head
    else:
        if value[1] == "1":
            back = back + [value[2]]
        else:
            head = head + [value[2]]
if reverse == False:
    print( "".join(head.reverse()) + "".join(Slist) + "".join(back) )

else:
    print( ''.join(reversed(head)) + "".join(Slist[::-1]) + "".join(back))

Submission status: Runtime Error

Solution code:
from collections import deque
a = deque()
a = a.append(input())
b = int(input())
c = 0
for i in range(b):
	d = input().split(" ")
	if d[0] == "2":
		if c == 0:
			if d[1] == "1":
				a = a.appendleft(d[2])
			else:
				a = a.append(d[2])
		else:
			if d[1] == "2":
				a = a.appendleft(d[2])
			else:
				a = a.append(d[2])
	else:
		if c == 0:
			c = 1
		else:
			c = 0
if c % 2 == 1:
	a = a[::-1]
for i in a:
	print(i, end = "")

Submission status: Runtime Error

Solution code:
resolve()
def resolve():
    S = str(input())
    Q = int(input())

    rev_flag = 1
    head_str = ""
    tail_str = ""
    for _ in range(Q):
        query = input() # contains spaces
        if query[0] == "1":
            rev_flag *= -1
        else:
            h_or_t_flag = 1 if query[2] == "1" else -1
            h_or_t_flag *= rev_flag

            if h_or_t_flag == 1:
                head_str += query[4]
            else:
                tail_str += query[4]

    ans = head_str[::-1] + S + tail_str
    print(ans if rev_flag == 1 else ans[::-1])

Submission status: Runtime Error

Solution code:
a = [input()]
b = int(input())
c = 0
for i in range(b):
	d = input().split(" ")
	if d[0] == "2":
		if c % 2 == 0:
			if d[1] == "1":
				a = a.insert(0, d[2])
			else:
				a = a.append(d[2])
		else:
			if d[1] == "2":
				a = a.insert(0, d[2])
			else:
				a = a.append(d[2])
	else:
		c += 1
if c % 2 == 1:
	a = a[::-1]
for l in a:
	print(l, end = "")

Submission status: Runtime Error

Solution code:
# -*- coding:utf-8 -*-
from collections import deque

def solve():
    d = deque(input())
    Q = int(input())

    is_reverse = False # 反転中か？

    for _ in range(Q):
        query = input()
        if query == "1":
            is_reverse = not is_reverse
        else:
            _, f, c = query.split()

            if f == "1":
                if is_reverse:
                    # 末尾にcを追加
                    d.append(c)
                else:
                    # 先頭にcを追加
                    d.appendleft(c)
            else:
                if is_reverse:
                    # 先頭に追加
                    d.appendleft(c)
                else:
                    # 末尾に追加
                    d.append(c)

    if is_reverse:
        print("".join(reversed(list(d))))
    else:
        print("".join(d))


def solve2():
    S = list(input())
    Q = int(input())
    M = 2*10**5+10
    data = [None]*M
    data.extend(S)
    data.extend([None]*M)
    left, right = M-1, M+len(S)

    is_reverse = False

    for _ in range(Q):
        query = input()
        if query == "1":
            is_reverse = not is_reverse
        else:
            t, f, c = query.split()
            if f == "1":
                if is_reverse:
                    # 末尾にcを追加
                    data[right] = c
                    right += 1
                else:
                    # 先頭にcを追加
                    data[left] = c
                    left -= 1
            else:
                if is_reverse:
                    # 先頭に追加
                    data[left] = c
                    left -= 1
                else:
                    # 末尾に追加
                    data[right] = c
                    right += 1

    # 出力
    if is_reverse:
        print("".join(data[right-1:left:-1])
        # for i in range(right-1, left, -1):
        #     print(data[i], end="")
    else:
        # for i in range(left+1, right):
        #     print(data[i], end="")
        print("".join(data[left+1:right]))
    # print()


if __name__ == "__main__":
    solve()

Submission status: Runtime Error

Solution code:
a = input()
b = int(input())
c = input()
for i in range(b-1):
	d = input().split(" ")
	if d[0] == "1":
		c[::-1]
	elif d[0] == "2":
		if d[1] == "1":
			c = d[2] + c
		elif d[1] == "2":
			c = d + d[2]
print(c)

Submission status: Runtime Error

Solution code:
def main():
  from collections import deque
  s=deque(input())
  q=int(input())
  count=0
  for _ in [0]*q:
    a=list(input().split())
    if len(a)==1:
      count+=1
    else:
      if count%2==0:
        if a[1]=='1':
           s.appendleft(a[2])
        else:
          s.append(a[2])
      else:
        if a[1]=='1':
          s.append(a[2])
        else:
          l=(a[2],s)
          s.appendleft(a[2])
  if count%2==1:
    s=s[::-1]
  print(''.join(s))
if __name__=='__main__':
  main()

Submission status: Runtime Error

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

for i in range(0,Q):
    A.append(list(map(str, input().split())))

for i in range(0,Q):
    if len(A[i]) == 1:
        S = S[::-1]
    else:
        if A[i][1] == '1':
            S = A[i][2] + S
        if A[i][1] == '2':
            S = S + A[i][2]
print(S)

Submission status: Runtime Error

Solution code:
from collections import deque
s = input()
q = int(input())
ans = deque(s)
rev = 0
for i in range(q):
    qi = input()
    if qi[0] == '1':
        rev += 1
    else:
        if qi[2] == '1' and rev % 2 == 0:
            ans.appendleft(qi[4])
        elif qi[2] == '2' and rev % 2 == 0:
            ans.append(qi[4])
        if qi[2] == '1' and rev % 2 == 1:
            ans.append(qi[4])
        elif qi[2] == '2' and rev % 2 == 1:
            ans.appendleft(qi[4])

parity = rev % 2
if parity:
    for i in ans[::-1]:
        print(i, end='')
else:
    for i in ans:
        print(i, end='')

Submission status: Runtime Error

Solution code:
s = collections.deque(list(input()))
q = int(input())
rev = 0
for i in range(q):
    tfc = list(input().split())
    if tfc[0]=='1':
        rev = abs(rev-1)
    else:
        t,f,c = tfc
        if rev:
            if f=='1':
                s.append(c)
            else:
                s.appendleft(c)
        else:
            if f=='1':
                s.appendleft(c)
            else:
                s.append(c)
if rev:
    t = []
    for i in range(len(s)):
        t.append(s.pop())
    print(''.join(t))
else:
    print(''.join(s))

Submission status: Runtime Error

Solution code:
from collections import deque

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

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

deq = deque(s)
true_position = True

for i in range(q):
    if len(query_list[i])==1:
        true_position = not true_position
        # print(s)
    else:
        if query_list[i][2]=="1":
            if true_position:
                deq.appendleft(query_list[i][4])
            else:
                deq.append(query_list[i][4])
        elif query_list[i][2]=="2":
            if true_position:
                deq.append(query_list[i][4])
            else:
                deq.appendleft(query_list[i][4])

if true_position:
    ans = "".join(deq)
else:
    ans = "".join(deq.reverse())

print(ans)

Submission status: Runtime Error

Solution code:
import numpy as np
S=input()
Q=int(input())
onecntr=0
before=[]
after=[S[-1]]
S=S[:-1]
for i in range(Q):
    tmp=input().split()
    if(tmp[0]=="1"):
        onecntr+=1
    else:
        if(onecntr%2==0):
            if(tmp[1]=="1"):
                before.append(tmp[2])
            else:
                after.append(tmp[2])
        else:
            if(tmp[1]=="2"):
                before.append(tmp[2])
            else:
                after.append(tmp[2])
if(len(before)!=1):
    ans=[before.pop()]
    for i in range(len(before)):
        ans.append(before.pop())
ans.append(S)
ans+=after
if(onecntr%2==0):
    print("".join(ans))
else:
    print("".join(ans[::-1]))

Submission status: Runtime Error

Solution code:
S=input()
Q=int(input())
onecntr=0
before=["$"]
after=[S[-1]]
S=S[:-1]
for i in range(Q):
    tmp=input().split()
    if(tmp[0]=="1"):
        onecntr+=1
    else:
        if(onecntr%2==0):
            if(tmp[1]=="1"):
                before.append(tmp[2])
            else:
                after.append(tmp[2])
        else:
            if(tmp[1]=="2"):
                before.append(tmp[2])
            else:
                after.append(tmp[2])
if(len(before)!=1):
    ans=[before.pop()]
    for i in range(len(before)-1):
        ans.append(before.pop())
ans.append(S)
ans+=after
if(onecntr%2==0):
    print("".join(ans))
else:
    print("".join(ans[::-1]))

Submission status: Runtime Error

Solution code:
import numpy as np
S=input()
Q=int(input())
onecntr=0
before=["$"]
after=[S[-1]]
S=S[:-1]
for i in range(Q):
    tmp=input().split()
    if(tmp[0]=="1"):
        onecntr+=1
    else:
        if(onecntr%2==0):
            if(tmp[1]=="1"):
                before.append(tmp[2])
            else:
                after.append(tmp[2])
        else:
            if(tmp[1]=="2"):
                before.append(tmp[2])
            else:
                after.append(tmp[2])
if(len(before)!=1):
    ans=[before.pop()]
    for i in range(len(before)-1):
        ans.append(before.pop())
ans.append(S)
ans+=after
if(onecntr%2==0):
    print("".join(ans))
else:
    print("".join(ans[::-1]))

Submission status: Runtime Error

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

rev = False

for i in range(Q):
  if int(Q_list[i][0]) == 1:
    rev = not rev
  
  else:
    F = Q_list[i][1]
    C = Q_list[i][2]
    
    if rev ^ (int(F) == 1): # 排他的論理和片方真片方ぎの時真になる
      S.appendleft(C)
    else:
      S.append(C)
      
if rev:
  S = S[::-1]
  
print(''.join(S))

Submission status: Runtime Error

Solution code:
if __name__ == '__main__':
    d = input()
    n = int(input())
    for _ in range(n):
        q = input()
        d = d[::-1] if q[0] == ('1') else d = q[-1]+d if q[:3] == ("2 1") else d=d+q[-1]      
    print(d)