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:
from collections import deque
s=input()
q=int(input())
ans_str=deque([s])
cnt=0
for _ in range(q):
    Q=list(input().split())
    if Q[0]=='1':
        cnt=1 if cnt==0 else 0

    else:
        if Q[1]=='1':
            if cnt==0:
                ans_str.appendleft(Q[-1])
            else:
                ans_str.append(Q[-1][::-1])

        else:
            if cnt==0:
                ans_str.append(Q[-1])
            else:
                ans_str.appendleft(Q[-1][::-1])
    print(ans_str,cnt,Q)

ans=list(ans_str) if cnt==0 else list(ans_str)[::-1]
print(*ans,sep='')

Submission status: Runtime Error

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

first = []
last = []
reverse = 0
for _ in range(Q):
    T, F, C = input().split()
    if T == '1':
        reverse = (reverse + 1) % 2
    elif F == '1':
        if reverse == 0:
            first.insert(0, C)
        else:
            last.append(C)
    else:
        if reverse == 0:
            last.append(C)
        else:
            first.insert(0, C)

R = first + S + last

if reverse == 1:
    R.reverse()

print(''.join(R))

Submission status: Runtime Error

Solution code:
s = str(input())
q = int(input())
query = [[str(i) for i in input().split()] for i in range(q)]
direction = 0
for i in range(q):
    if query[i] == ['1']:
        direction += 1
    else:
        if (int(query[i][1]) + direction) % 2 == 0:
            s += query[i][2]
        else:
            s = query[i][2] + s
if direction % 2 == 1:
  	s1 = ''
    for i in range(len(s)):
      	s1 += s[-i-1]
else:
	print(s)

Submission status: Runtime Error

Solution code:
from collections import deque

S=deque(str((input())))
Q=int(input())
t=0
for _ in range(Q):
    T=list(map(str,input().split()))
    if T[0]=="1":
        t+=1
    else:
        if (T[1]=="1" and t%2 == 0) or (T[1]=="2" and t%2 != 0):
            S.insert(0,T[2])
        else:
            S.append(T[2])


if t%2 != 0:
    S.reverse()

print("".join(S))

Submission status: Runtime Error

Solution code:
s = str(input())
q = int(input())
query = [[str(i) for i in input().split()] for i in range(q)]
direction = 0
for i in range(q):
    if query[i] == ['1']:
        direction += 1
    else:
        if (int(query[i][1]) + direction) % 2 == 0:
            s += query[i][2]
        else:
            s = query[i][2] + s
if direction % 2 == 1:
    s1 = ''
    for i in range(n):
        s1 += s[-i-1]
    print(s1)
else:
    print(s)

Submission status: Runtime Error

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

first = []
last = []
normal = True
for _ in range(Q):
    QQ = input().split()
    if QQ[0] == '1':
        normal = not normal
    elif QQ[1] == '1':
        if normal:
            first.insert(0, QQ[2])
        else:
            last.append(QQ[2])
    else:
        if normal:
            last.append(QQ[2])
        else:
            first.insert(0, QQ[2])

R = first + S + last

if normal:
    print(''.join(R))
else:
    print(''.join(R.reverse()))

Submission status: Runtime Error

Solution code:
s = str(input())
q = int(input())
query = [[str(i) for i in input().split()] for i in range(q)]
direction = 0
for i in range(q):
    if query[i] == ['1']:
        direction += 1
    else:
        if (int(query[i][1]) + direction) % 2 == 0:
            s += query[i][2]
        else:
            s = query[i][2] + s
if direction % 2 == 1:
    s1 = ''
    for i in range(n):
        s1 += s[-i]
    print(s1)
else:
    print(s)

Submission status: Runtime Error

Solution code:
from collections import deque
S=input()
s=deque(S)
Q=int(input())
Que=[list(map(str, input().split())) for _ in range(Q)]
#print(Que)
a=0
for i in range(Q):
    if Que[i][0]=="1":
        a+=1
    elif Que[i][0]=="2":
        if a%2==0:
            if Que[i][1]=="2":
                s.append(Que[i][2])
            elif Que[i][1]=="1":
                s.appendleft(Que[i][2])
        else:
            if Que[i][1]=="1":
                s.append(Que[i][2])
            elif Que[i][1]=="2":
                s.appendleft(Que[i][2])
s=list(s)
if a%2==1:
    a=a[::-1]
ans="".join(s)
print(ans)

Submission status: Runtime Error

Solution code:
S = list(input())
Q = int(input())
l_list = []
r_list = []
flag = True
for i in range(Q):
    A = list(map(str, input().split()))
    if len(A)==1:
        flag = not flag
    else:
        if flag:
            if A[1]=="1":
                l_list.appendleft(A[2])
            else:
                r_list.append(A[2])
        else:
            if A[1]=="1":
                r_list.append(A[2])
            else:
                l_list.appendleft(A[2])
if flag:
    print("".join(l_list)+"".join(S)+"".join(r_list))
else:
    print("".join(r_list[::-1])+"".join(S[::-1])+"".join(l_list[::-1]))

Submission status: Runtime Error

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

from collections import deque

ss = deque(s)
flg = True

for i in range(q):
  t = input().split()
  if t[0] == '1':
    flg = not flg
  else:
    if t[1] == '2':
        ss.append(t[2]) if flg else ss.appendleft(t[2])
    else:
        ss.append(t[2]) if not flg else ss.appendleft(t[2])



print("".join(ss)) if flg else print("".join(ss[::-1]))

Submission status: Runtime Error

Solution code:
S = list(input())
Q = int(input())
l_list = []
r_list = []
flag = True
for i in range(Q):
    A = list(map(str, input().split()))
    if len(A)==1:
        flag = not flag
    else:
        if flag1:
            if A[1]=="1":
                l_list.appendleft(A[2])
            else:
                r_list.append(A[2])
        else:
            if A[1]=="1":
                r_list.append(A[2])
            else:
                l_list.appendleft(A[2])
if flag:
    print("".join(l_list)+"".join(S)+"".join(r_list))
else:
    print("".join(r_list[::-1])+"".join(S[::-1])+"".join(l_list[::-1]))

Submission status: Runtime Error

Solution code:
from collections import deque
S=input()
s=deque(S)
Q=int(input())
Que=[list(map(str, input().split())) for _ in range(Q)]
#print(Que)
for i in range(Q):
    if Que[i][0]=="1":
        s=s[::-1]
    elif Que[i][0]=="2":
        if Que[i][1]=="2":
            s.append(Que[i][2])
        elif Que[i][1]=="1":
            s.appendleft(Que[i][2])
ans="".join(s)
print(ans)

Submission status: Runtime Error

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

from collections import deque

ss = deque(s)
flg = True

for i in range(q):
  t = input().split()
  if t[0] == '1':
    flg = !flg
  else:
    if t[1] == '2':
        ss.append(t[2]) if flg else ss.appendleft(t[2])
    else:
        ss.append(t[2]) if flg != True else ss.appendleft(t[2])



print("".join(ss)) if flg else print("".join(ss[::-1]))

Submission status: Runtime Error

Solution code:
from collections import deque
S = deque(input())
Q = int(input())
x = 1
l=[]
for i in range(Q):
    l=[]
    l = list(input().split())
    if l[0] == "1":
        x *= -1
    elif l[0] == "2":
        if (l[1] == "2" and x == -1) or  (l[1] == "1" and x == 1):
            S.appendleft(l[2])
        else:
            S.append(l[2])
if x == -1:
    S = S[::-1]
print(*S, sep="")

Submission status: Runtime Error

Solution code:
xxfrom collections import deque
S = deque(input())
Q = int(input())
x = 1
l=[]
for i in range(Q):
    l=[]
    l = list(input().split())
    if l[0] == "1":
        x *= -1
    elif l[0] == "2":
        if (l[1] == "2" and x == -1) or  (l[1] == "1" and x == 1):
            S.appendleft(0, l[2])
        else:
            S.append(l[2])
if x == -1:
    S = S[::-1]
print(*S, sep="")

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.readline

S = input().rstrip()
Q = int(input())
head = ''
tail = ''
rev = False

for _ in range(Q):
    query = list(input().split())
    for i in range(10**5):
        if query[0] == '1':
            if flag:
                rev = False
            else:
                rev = True
            continue
        elif query[0] == '2':
            f = query[1]
            c = query[2].rstrip()
            if f == '1':
                if rev:
                    tail = tail + c
                else:
                    head = c + head
            elif:
                if rev:
                    head = c + head
                else:
                    tail = tail + c
if flag:
    print(head + S + tail)
else:
    tmp = head + S + tail
    print(tmp[::-1])

Submission status: Runtime Error

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

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

querys = []
op1_cnt = 0
op2_cnt = 0
prefix = deque()
suffix = deque()
for i in range(Q):
    query = input()
    if len(query) == 1:
        op1_cnt += 1

        if op2_cnt != 0:
            prefix.extend(S)
            prefix.extend(suffix)
            S = ''.join(prefix)
            prefix.clear()
            suffix.clear()
            op2_cnt = 0

    else:
        op2_cnt += 1

        if op1_cnt % 2 != 0:
            S = S[::-1]

        op1_cnt = 0

        t, F, C = query.split()
        if F == "1":
            prefix.appendleft(C)
        else:
            suffix.append(C)

if op1_cnt % 2 != 0:
    S = S[::-1]

if op2_cnt != 0:
    prefix.extend(S)
    prefix.extend(suffix)
    S = ''.join(prefix)


print(S)

Submission status: Runtime Error

Solution code:
s = input()
q = int(input)
rev = False
for i in range(q):
  inp = input()
  if int(inp[0]) == 1:
    rev = not rev
  else:
    t, f, c = inp.split()
    if (int(f) == 1 and rev) or (int(f) == 2 and not rev):
      s += c
    else:
      s = c + s
print(reversed(s) if rev else s)

Submission status: Runtime Error

Solution code:
    S = input()
    Q = int(input())
     
    # print(S, Q)
     
    queries = []
    for i in range(Q):
        query = input()
        queries.append(query)
     
    from collections import deque
    slist = deque()
	slist.append(S)
    for query in queries:
        if query == "1":
            isReversed = not isReversed
        else:
            t, f, c = query.split()
            if f == '1':
                if isReversed:
                    slist.append(c)
                else:
                    slist.appendleft(c)
            else:
                if isReversed:
                    slist.appendleft(c)
                else:
                    slist.append(c)
     
    if isReversed:
        slist.reverse()
    print("".join(slist))

Submission status: Runtime Error

Solution code:
S = list(input())
Q = int(input())
x = 1
l=[]
for i in range(Q):
    l = list(input().split())
    if l[0] == "1":
        x *= -1
    elif l[0] == "2":
        if (l[1] == "2" and x == -1) or  (l[1] == "1" and x == 1):
            S.insert(0, d)
        else:
            S.append(d)
if x == -1:
    S = S[::-1]
print(*S, sep="")

Submission status: Runtime Error

Solution code:
from collections import deque
s=deque(list(input().split()))
ans=""
q=int(input())
flag=True
for i in range(q):
  ques=list(input().split())
  if ques[0]=="1":
    flag=not flag
    #print("1",s)
  else:
    if ques[1]=="1":
      if flag:
        s.appendleft(ques[2])
        #print("2",s)
      else:
        s.append(ques[2])
    else:
      if flag:
        s.append(ques[2])
      else:
        s.appendleft(ques[2])
      #print("22",s)
if flag==False:
    s = list(s).reverse()
print("".join(map(str, s)))

Submission status: Runtime Error

Solution code:
S = list(input())
Q = int(input())
x = 1
for i in range(Q):
    a, *b = input().split()
    if a == "1":
        x *= -1
    elif a == "2":
        c, d = b
        if (c == "2" and x == -1) or  (c == "1" and x == 1):
            S.appendleft(d)
        else:
            S.append(d)
if x == -1:
    S = S[::-1]
print(*S, sep="")

Submission status: Runtime Error

Solution code:
s = input()
q = int(input())
q1 = [list(map(str,input().split())) for i in range(q)]

ap = ["", ""]
revCount = 0

for j in q1:
  if j[0] == "1":
    revCount = (revCount+1) % 2

  else:
    if revCount == 0:
      ap[j[1] - 1] = ap[j[1] - 1] + j[2]
    else:
      ap[j[1] - 2] = ap[j[1] - 2] + j[2]
        

if revCount == 0:
  ap[0] = ap[0][::-1]
  print(ap[0] + s + ap[1])

else:
  aps = (s+ap[1])[::-1]
  print(aps + ap[0])

Submission status: Runtime Error

Solution code:
def main():
  from collections import deque
  s=input()
  q=int(input())
  turn = 2
  d=deque(s)

  for _ in [0]*q:
      query = input()
      if query[0] == "1":
          t = int(query)
          turn = 3 - turn
      else:
          t,f,c = map(str,query.split())
          if (int(f) + turn)% 2  == 0:
              d.append(c)
          else:
              d.appendleft(c)
  if turn == 2:
      print("".join(d))
  else:
      print("".join(d[::-1]))
main()

Submission status: Runtime Error

Solution code:
s=input()
a=[]
b=[]
now=True
q=int(input())
for i in range(q):
    x=list(input().split())
    if x[0]=="1":
        now=not now
    else:
        if now and x[1]=="2" or not now and x[1]=="1":
            b.append(x[2])
        else:
            a.append(x[2])
if now:
    print(*a[::-1],s,*b,sep="")
else:
    print(*b[::-1],s[::-1],*a,sep="")

Submission status: Runtime Error

Solution code:
from collections import deque

def main():
    S = deque(input().split())
    Q = int(input())

    hantei = 0
    
    for i in range(Q):
        QList = list(input().split())
    
        if QList[0] == '1':
            if hantei == 0:
                hantei = 1
            else:
                hantei = 0
        else:
            if QList[1] == '1':
                if hantei == 0:
                    S.appendleft(QList[2])
                else:
                    S.append(QList[2])
            else:
                if hantei == 0:
                    S.append(QList[2])
                else:
                    S.appendleft(QList[2])
    
    if hantei == 0:
        print("".join(list(S)))
    else:
        print("".join(list(S).reverse()))


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
from collections import deque
s=deque(list(input()))
q=int(input())
direction=0 #0:順 1:逆

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

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

Submission status: Runtime Error

Solution code:
from collections import deque

S = input()
pre, post = deque(), deque()
Q = int(input())
reversed = False
for _ in range(Q):
  query = input().split()
  if query[0] == '1':
    reversed = not reversed
    pre, post = post, pre
  else:  # query[0] == 2
    F, C = query[1:]
    if F == '1':
      if reversed:
        pre.append(C)
      else:
        pre.appendleft(C)
    else:  # F == '2'
      if reversed:
        post.appendleft(C)
      else:
        post.append(C)
if reversed:
  print(''.join(pre[::-1]) + S[::-1] + ''.join(post[::-1]))
else:
  print(''.join(pre) + S + ''.join(post))

Submission status: Runtime Error

Solution code:

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

# print(S, Q)

queries = []
for i in range(Q):
    query = input()
    queries.append(query)

from collections import deque
slist = deque()
slist.append(S)
isReversed = False
for query in queries:
    if query == "1":
        isReversed = not isReversed
    else:
        t, f, c = query.split()
        if f=="1"
            if isReversed:
                slist.append(c)
            else:
                slist.appendleft(c)
        else:
            if isReversed:
                slist.appendleft(c)
            else:
                slist.append(c)
if isReversed:
    slist.reverse()

print("".join(slist))

Submission status: Runtime Error

Solution code:
from collections import deque

S = input()
pre, post = deque(), deque()
Q = int(input())
reversed = False
for _ in range(Q):
  query = input().split()
  if query[0] == '1':
    reversed = not reversed
    pre, post = post, pre
  else:  # query[0] == 2
    F, C = query[1:]
    if F == '1':
      if reversed:
        pre.append(C)
      else:
        pre.appendleft(C)
    else:  # F == '2'
      if reversed:
        post.appendleft(C)
      else:
      	post.append(C)
if reversed:
  print(''.join(pre[::-1]) + S[::-1] + ''.join(post[::-1]))
else:
  print(''.join(pre) + S + ''.join(post))

Submission status: Runtime Error

Solution code:
from collections import deque

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

reversing = False

for i in range(Q):
    query = list(input().split())
    if query[0] == '1':
        reversing = not reversing
    else:
        if query[1] == '1':
            if not reversing:
                S.appendleft(query[2])
            else:
                S.append(query[2])
        else:
            if reversing:
                S.appendleft(query[2])
            else:
                S.append(query[2])

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

Submission status: Runtime Error

Solution code:
S = input()
Q = int(input())
rev_num = 0
for i in range(Q):
  query = [i for i in map(str,input().split())]
  if query[0]=='1':
    rev_num += 1
  else:
    if query[1]=='1':
      if rev_num%2==1:
        S=''.join([S,query[2]])
      else:
        S=''.join([query[2],S])
    else:
      if rev_num%2==1:
        S=''.join([query[2],S])
      else:
        S=''.join([S,query[2]])
        
if rev_num%2:
  S.reverse()
print(''.join(S))

Submission status: Runtime Error

Solution code:
s = input()
q = input()
q = int(q)
flag
for i in range(0,q):
    line = input()
    if line == "1":
        if flag = 1:
            flag = 0
        else:
            flag = 1
#         s = s[::-1]
    else:
        t, f, c = line.split(" ")
        if int(f) == 1:
            if flag = 0:
                s = c + s
            else:
                s = s + c
        else:
            if flag = 0:
                s = s + c
            else:
                s = c + s
print(s)

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)]
 
for i in range(Q):                
  if int(Q_list[i][0]) == 1:
    if len(S) >= 2:
      S = S[::-1]
  
  else:
    F = Q_list[i][1]
    C = Q_list[i][2]
    
    if int(F) == 1:
      S.appendleft(C)
    else:
      S.append(C)
      
print(''.join(S))

Submission status: Runtime Error

Solution code:
S = str(input())
Q = int(input())
querry = [list(map(str, input().split())) for i in range(Q)]
for i in range(Q):
    if querry[i][0] == '1':
        S = S[::-1]
        A_S = A_S[::-1]
        S_A = S_A[::-1]
    elif querry[i][0] == '2':
        if querry[i][1] == '1':
            A_S += querry[i][2] 
        elif querry[i][1] == '2':
            S_A += querry[i][2]

print(A_S + S + S_A)

Submission status: Runtime Error

Solution code:
S = input()
Q=int(input())
Query=S
flag = True
for i in range(Q):
  S2=input()
  if S2=="1":
    flag = not flag
  else:
    x=S2.split()
    if flag:
      if x[1]=="1":
        Query=x[2]+Query
      else:
        Query=Query+x[2]
    else:
      if x[1]=="1":
        Query=Query+x[2]
      else:
        Query=x[2]+Query
if flag==False:
  Query.reverse()

print(Query)

Submission status: Runtime Error

Solution code:


#E 
n,p=map(int,input().split())
s=input()



#D
def main():
  s=input()
  q=int(input())
  turn = 2

  for _ in [0]*q:
      query = input()
      try:
          t = int(query)
          turn = 3 - turn
      except ValueError:
          t,f,c = map(str,query.split())
          if (int(f) + turn)% 2  == 0:
              s = s + c
          else:
              s = c + s
  if turn == 2:
      print(s)
  else:
      print(s[::-1])
main()

Submission status: Runtime Error

Solution code:
from collections import deque

S = input()
Q = deque(int(input()))
 
Q_list = [list(map(str, input().split())) for i in range(Q)]
 
for i in range(Q):                
  if int(Q_list[i][0]) == 1:
    if len(S) >= 2:
      S = S[::-1]
  
  else:
    F = Q_list[i][1]
    C = Q_list[i][2]
    
    if int(F) == 1:
      S.appendleft(C)
    else:
      S.append(C)
      
print(''.join(S))

Submission status: Runtime Error

Solution code:
from queue import deque

s = deque([i for i in input()])
Q = int(input())
state = 0
for _ in range(Q):
    q = input()
    if q[0] == '1':
        if state == 0:
            state = 1
        else:
            state = 0
    else:
        if q[2] == '1':
            if state == 0:
                s.appendleft(q[-1])
            else:
                s.append(q[-1])
        else:
            if state == 0:
                s.append(q[-1])
            else:
                s.appendleft(q[-1])

if state == 1:
    print(s[::-1])
else:
    print(s)

Submission status: Runtime Error

Solution code:
S = list(input())
N = int(input())
X = []
for i in range(N):
    X.append(input())

T = []
for i in range(N):
    if len(X[i]) == 1:
        S.reverse()
        T.reverse()
        if T:
            S.extend(T)
            T.clear()
            print(S)
    else:
        Y = list(X[i].split())
        if int(Y[1]) == 1:
            T.append(Y[2])
        else:
            S.append(Y[2])

if T:
    T.extend(S)
    print(''.join(T))
else:
    print(''.join(S))

Submission status: Runtime Error

Solution code:
def main():

    S = input().split()
    s = S
    Q = int(input())
    s_ind = 0

    q_list = [input().split() for _ in range(Q)]
    for q in q_list:
        if len(q) == 1:
            t = q[0]
        else:
            t, f, c = q

        if t == '1' or t == 1:
            front = S[:s_ind]
            end = S[s_ind+1:]
            s_ind = len(S)-s_ind
            end.append(s)
            end.extend(front)
            S = end
        else:
            if f == '1' or f == 1:
                S.insert(0, c)
                s_ind += 1
            else:
                S.append(c)

    print("".join(S))


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
S = input()
Q = int(input())
T = [input() for i in range(Q)]

count = True

for i in range(Q):
  t = T[i]
  if(int(t[0]) == 1):
    count = not count
  else:
    if(int(t[2]) == 1):
      if(count):
        S = f"{t[4]}{S}"
      else:
        S = f"{S}{t[4]}"
    else:
      if(count):
        S = f"{S}{t[4]}"
      else:
        S = f"{t[4]}{S}"

if(count):
  print(S)
else:
  print(S[::-1])

Submission status: Runtime Error

Solution code:
import sys
import math
sys.setrecursionlimit(int(1e6))

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

bef = list()
aft = list()
rev = False
for i in range(Q):
    query = input().split()
    if query[0] == '1':
        rev = not rev
    elif query[1] == '1':
        if not rev:
            bef.append(query[2])
        else:
            aft.append(query[2])
    else:
        if not rev:
            aft.append(query[2])
        else:
            bef.append(query[2])

if not rev:
    if len(bef) > 0:
        print(''.join(reversed(bef)), end="")

    print(S, end="")

    if len(aft) > 0:
        print(''.join(aft), end="")
else:
    if len(aft) > 0:
        print(''.join(reversed(aft)), end="")

    print(S.reverse(), end="")
    
    if len(bef) > 0:
        print(''.join(bef), end="")

print("", flush=True)

Submission status: Runtime Error

Solution code:
import sys
from collections import deque
def main():
  S = input()
  Q = int(input())
  r = False
  ans = deque(S)
  for l in sys.stdin:
    TFC = l.split()
    if TFC[0]=="1": r = not r
    elif TFC[1]=="1":
      if r: ans.append(TFC[2])
      else: ans.appendleft(TFC[2])
    elif TFC[1]=="2":
      if r: ans.appendleft(TFC[2])
      else: ans.append(TFC[2])
  print("".join(reversed(ans) if r else ))

if __name__ == "__main__":
  main()

Submission status: Runtime Error

Solution code:
S = str(input())
Q = int(input())
querry = [list(map(str, input().split())) for i in range(Q)]
for i in range(Q):
    if querry[i][0] == '1':
        S = S[::-1]
    elif querry[i][0] == '2':
        if querry[i][1] == '1':
            A_S += querry[i][2] 
        elif querry[i][1] == '2':
            S_A += querry[i][2]

print(A_S + S + S_A)

Submission status: Runtime Error

Solution code:
#include <iostream>
#include <cmath>
#include <vector>
#include <string>
#include <algorithm>

using namespace std;

#define INF 1e9
#define MAXQ 200005
#define MAXM 200005
#define ll long long
#define vi vector<int>
#define vll vector<long long>
#define rep(i,n) for(int i=0, i##_len=(n); i<i##_len; ++i)
#define pii pair<int, int>

ll ans;
int Q, T[MAXQ], F[MAXQ], cnt = 0, curR = 0, curL = 0;
char C[MAXQ];
string S, l, r;

void solve(){
    rep(i, Q){
        if(T[i]==1){
            swap(curR, curL);
            cnt--;
        } else {
            if(F[i]==1){
                if(cnt%2==0)
                    l[curL] = C[i];
                else
                    r[curL] = C[i];
                curL++;
            } else {
                if(cnt%2==0)
                    r[curR] = C[i];
                else
                    l[curR] = C[i];
                curR++;
            }
        }
    }

    for(char c: l) {
        c += S;
    }
    for(char c: r) {
        S += c;
    }

    cout << S << endl;
}

int main(){
    cin >> S >> Q;

    l.resize(Q);
    r.resize(Q);

    rep(i ,Q){
        cin >> T[i];
        if(T[i]==2){
            cin >> F[i] >> C[i]; 
        } else {
            cnt++;
        }
    }

    if(cnt % 2 != 0)
        reverse(S.begin(), S.end());

    solve();
}

Submission status: Runtime Error

Solution code:
s = str(input())
q = int(input())
hanten_flg = False
command_list= [input() for i in range(0,q)]
for i in command_list:
    commands = command_list[i].split()
    if(commands[0] == 1):
        if(hanten_flg == True):
            hanten_flg = False
        if(hanten_flg == False):
            hanten_flg = True
    else:
        if(hanten_flg == True):
            s = s[::-1]
        if(commands[1] == 1):
            s = commands[2] + s
        else:
            s = s + commands[2]
print(s)

Submission status: Runtime Error

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

r = 0

B = []
A = []

for i in range(Q):
  if int(Qu[i][0]) == 1:
    r = 1 - r
  else:
    F = int(Qu[i][1])
    C = Qu[i][2]
    F -= 1
    if F == r:
      B.append(C)
    else:
      A.append(C)

if r == 0:
  B.reverse()
  print("".join(B + S + A))
else:
  A.reverse()
  print("".join(A + S.reverse() + B))

Submission status: Runtime Error

Solution code:
import sys
from collections import deque

def main():
    input = sys.stdin.readline

    s = input()[:-1]
    q = int(input())
    tmp = [input().split() for _ in range(q)]

    flag = 0
    ans = deque([s])
    for t in tmp:
        if len(t) == 1:
            flag = ~flag 
        else:
            f = int(t[1])
            c = t[2]
            if (f == 1 and flag == 0) or (f ==2 and fag == -1):
                ans.appendleft(c)
            else:
                ans.append(c)
    if flag == 0:
        print(''.join(ans))
    else:
        print(''.join(ans[::0]))

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
S = list(input().split())
q = int(input())
d = deque(S)
rev_cnt = 0
for ind in range(q):
    cur_list = list(input().split())
    #print(cur_list)
    if int(cur_list[0]) == 1:
        rev_cnt += 1
    else:
        f = int(cur_list[1])
        c = cur_list[2]
        if (rev_cnt + f) % 2 == 0:
            d.append(c)
        else:
            d.appendleft(c)         
S = list(d)
if rev_cnt %2 == 1:
    S.reverse()
print("".join(S))

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)
cnt = 0
last = []
head = []
for i in range(Qlen):
    if int(Query[i][0]) == 1:
        cnt +=1
    else:
        if int(Query[i][1]) == 1: #頭につける
            if cnt % 2 != 0:   #逆さ
                last = last + [Query[i][2]]
            else:
                head = [Query[i][2]] + head
        else: #けつにつける
            if cnt % 2 != 0:
                head = [Query[i][2]] + head
            else:
                last = last + [Query[i][2]]
if cnt % 2 == 0:
    print(head + "".join(Slist) + last)
else:
    Slist = Slist[::-1]
    head = head[::-1]
    last = last[::-1]
    print("".join(last + Slist + head))
    
# head Slist last

Submission status: Runtime Error

Solution code:
def main():
  mode = True
  s = input()
  for i in range(int(input())):
    temp = input()
    if temp[0] == '1':
      mode = not mode
      continue
    
    if temp[2] == 1:
      s = temp[4] + s if mode else s + temp[4]
    else:
      s = s + temp[4] if mode else temp[4] + s
      
  return s if mode else s.reverse()

print(main())

Submission status: Runtime Error

Solution code:
from collections import deque

def main():
    S = deque(input().split())
    Q = int(input())

    hantei = 0
    
    for i in range(Q):
        QList = list(input().split())
    
        if QList[0] == '1':
            if hantei == 0:
                hantei = 1
            else:
                hantei = 0
        else:
            if QList[1] == '1':
                if hantei == 0:
                    S.appendleft(QList[2])
                else:
                    S.append(QList[2])
            else:
                if hantei == 0:
                    S.append(QList[2])
                else:
                    S.appendleft(QList[2])
    
    if hantei == 0:
        print("".join(list(S)))
    else:
        print("".join(list(S.reverse())))


if __name__ == '__main__':
    main()