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:
def main():
    s=input()
    q=int(input())
    queryy=[]
    sent="1"
    back="1"
    for i in range(q):
        queryy.append(tuple(input().split()))
    check=-1
    for query in queryy:
    #   print(query)
        if query[0]=="1":
            check*=-1
        else:
            if query[1]=="1":
                if check==-1:
                    sent+=query[2]
                  #  s=str(query[2])+s
                else:
                    back+=query[2]
                   # s+=str(query[2])
                    
            else:
                if check==-1:
                    back+=query[2]
                    #s+=str(query[2])
                else:
                    sent+=query[2]
                    #s=str(query[2])+s
    s=s[::-1]
   # print(type(sent))
    if sent[0]=="1"
        sent=sent[1:]
    sent=sent[::-1]
    if back[0]=="1"
        back=back[1:]

    if check==1:
        
        print((sent+s+back)[::-1]) 
    else:
        print(sent+s+back)

if __name__=="__main__":
    main()

Submission status: Runtime Error

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


class Q:
    def __init__(self, query, state):
        self.q = query
        self.s = state

    def __print__(self, revc):
        if self.q is None:
            print(self.s, end='')
            return

        l = self.q.split()
        if l[0] == '1':
            self.s.__print__(revc + 1)
        elif (revc % 2 == 0 and l[1] == '1') or (revc % 2 == 1 and l[1] == '2'):
            print(l[2], end='')
            self.s.__print__(revc)
        else:
            self.s.__print__(revc)
            print(l[2], end='')

    def print(self):
        self.__print__(0)


s = Q(None, s)
for _ in range(q):
    s = Q(input(), s)
s.print()
print()

Submission status: Runtime Error

Solution code:
from collections import deque

S = deque([input().strip()])
Q = int(input().strip())

inv = False
for i in range(Q):
  inp = input().strip().split()
  if len(inp)==1:
    inv = not inv
  else:
    t, f, c = inp
    if (f=="1" and not inv) or (f=="2" and inv):
      S.appendleft(c)
    elif (f=="2" and not inv) or (f=="1" and inv):
      S.append(c)
if not inv:
  print(''.join(S))
else:
  print(''.join(S[::-1]))

Submission status: Runtime Error

Solution code:
import sys
import math
from decimal import Decimal, ROUND_HALF_UP, ROUND_HALF_EVEN
from collections import deque
from bisect import bisect_left
from itertools import product
def I(): 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 LI2(N): return [list(map(int, sys.stdin.readline().split())) for i in range(N)]
def LSI(): return list(map(int, list(sys.stdin.readline().rstrip())))
def LSI2(N): return [list(map(int, list(sys.stdin.readline().rstrip()))) for i in range(N)]
def S(): return sys.stdin.readline().rstrip()
def LS(): return sys.stdin.readline().split()
def LS2(N): return [sys.stdin.readline().split() for i in range(N)]
def FILL(i,h): return [i for j in range(h)]
def FILL2(i,h,w): return [FILL(i,w) for j in range(h)]
def FILL3(i,h,w,d): return [FILL2(i,w,d) for j in range(h)]
def FILL4(i,h,w,d,d2): return [FILL3(i,w,d,d2) for j in range(h)]
def sisha(num,digit): return Decimal(str(num)).quantize(Decimal(digit),rounding=ROUND_HALF_UP)
#'0.01'や'1E1'などで指定、整数に戻すならintをかます
MOD = 998244353
INF = float("inf")
sys.setrecursionlimit(10**6+10)
input = sys.stdin.readline
S = S()
S = [S[i] for i in range(len(S))]
S = deque(S)
Q = I()
rev = False
for i in range(Q):
    q = LS()
    if q[0] == '1':
        rev = not rev
        continue
    if (q[1] == '1') ^ rev:
        S = [q[2]]+S
    else:
        S = S+q[2]
if rev:
    ans = S.reverse()
else:
    ans = S

print(ans)

Submission status: Runtime Error

Solution code:
s = input()
q = int(input())
count = 0
top = ""
end = ""
ans = s
for i in range(q):
    tmp = input()
        count += 1
    else:
        _, f, c = map(str, tmp.split())
        if (count % 2 == 0 and f == "1") or (count % 2 == 1 and f == "2"):
            top = c + top
        else:
            end = end + c

ans = top + ans + end
ans = ans[::-1] if count % 2 == 1 else ans
print(ans)

Submission status: Runtime Error

Solution code:
from collections import deque
s = deque(input())
q = int(input())
a = 0
c = ""
for i in range(q):
    b = list(input().split())
    if b[0] == "1":
        if a == 0:
            a = 1
        else:
            a = 0
    else:
        if (a, b[1]) == (0, "1") or (a, b[1]) == (1, "2"):
            s.appendleft(0, b[2])
        else:
            s.append(b[2])
if a == 0:
    for i in range(len(s)):
        c += s[i]
else:
    for i in range(len(s)):
        c += s[-i-1]
print(c)

Submission status: Runtime Error

Solution code:
import copy
import itertools

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

ct = 0
ls = []
rs = []
for i in range(q):
    qtmp = input().split()
    "qtmp = [2,1,'c']
    t = int(qtmp[0])
    if(t == 1):
        ct += 1
        ct = ct % 2
    else:
        f = int(qtmp[1])
        if(ct == f):
            rs.append( qtmp[2])
        else:
            ls.append( qtmp[2])
if(ct == 1):
    tmp = rs
    rs = ls
    ls = tmp
for i in range(len(ls)):
    print(ls[len(ls)-i-1], end='')
print(s, end='')
for i in range(len(rs)):
    print(rs[i], end='')
print("")

Submission status: Runtime Error

Solution code:
S=input()
S_=list(S)
Q=int(input())
F1=[]
F2=[]
Q_=[]
for i in range(4):
  Q_.append(list(input().split()))
count=0  
for i in range(len(Q_)):
  if Q_[i]==['1']:
    count+=1
  elif Q_[i][0:2]==['2','1']:
    if count%2==0:
      F1.append(Q_[i][2])
    else:
      F2.append(Q_[i][2])
  else:
    if count%2==0:
      F2.append(Q_[i][2])
    else:
      F1.append(Q_[i][2])
SS=F1[::-1]+S_+F2                
if count%2==0:
  print(''.join(SS))
else:
  print(''.join(SS[::-1]))

Submission status: Runtime Error

Solution code:
s = input()
q = int(input())
for i in range(q):
  n = int(input())
  if n == 1:
    s = s[::-1]
  else:
    f,c = map(str,input().split())
    if int(f) == 1:
      s = c + s
    else:
      s = s + c
print(s)

Submission status: Runtime Error

Solution code:
s = input()
q = int(input())
for i in range(q):
  n = int(input())
  if n == 1:
    s = s[::-1]
  else:
    continue
print(s)

Submission status: Runtime Error

Solution code:
s = input()
q = int(input())
for i in range(q):
  n = int(input())
  if n == 1:
    s = s[::-1]
  else:
    f,c = input().split()
    if int(f) == 1:
      s = c + s
    else:
      s = s + c
print(s)

Submission status: Runtime Error

Solution code:
s = input()
q = int(input())
for i in range(q):
  n = int(input())
  if n == 1:
    s = s[::-1]
  else:
    f,c = int(input()),input()
    if f == 1:
      s = c + s
    else:
      s = s + c
print(s)

Submission status: Runtime Error

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

ans = ['' for in range(10 ** 5 * 3 + 2)]

for i in range(len(s)):
  ans[10 ** 5 + i + 1] = s[i]

reverse_num = 0
tmp1 = 0
tmp2 = 0
for i in range(q):
  if int(Q[i][0]) == 1:
    reverse_num += 1
  else:
    if int(Q[i][1]) == 1:
      if reverse_num % 2 == 0:
        ans[10 ** 5 - tmp1] = Q[i][2]
        tmp1 += 1
      else:
        ans[10 ** 5 + len(s) + tmp2] = Q[i][2]
        tmp2 += 1
    else:
      if reverse_num % 2 != 0:
        ans[10 ** 5 - tmp1] = Q[i][2]
        tmp1 += 1
      else:
        ans[10 ** 5 + len(s) + tmp2] = Q[i][2]
        tmp2 += 1
      
res = ''
for i in range(len(ans)):
  if ans[i] != '':
    res += ans[i]
    
print(ans)

Submission status: Runtime Error

Solution code:
s=list(input())
q=int(input())
rev=0
for i in range(q):
    t=list(input().split())
    if t[0]=="1":
        rev=rev+1
    else:
        if t[1]=="1":
            if rev%2==0:
                s.appendleft(t[2])
            else:
                s.append(t[2])
        else:
            if rev%2==0:
                s.append(t[2])
            else:
                s.appendleft(t[2])
if rev%2==0:
    print("".join(s))
else:
    s.reverse()
    print("".join(s))

Submission status: Runtime Error

Solution code:
s=list(input())
q=int(input())
rev=0
for i in range(q):
    t=list(input().split())
    if t[0]=="1":
        rev=rev+1
    else:
        if t[1]=="1":
            if rev%2==0:
                s.insert(0,t[2])
            else:
                s.append(t[2])
        else:
            if rev%2==0:
                s.append(t[2])
            else:
                s.insert(0,t[2])
if rev%2==0:
    print("".join(s))
else:
    print("".join(s.reverse()))

Submission status: Runtime Error

Solution code:
s=list(int(input()))
q=int(input())
for i in range(q):
    t=list(input())
    if t[0]==1:
        s.reverse()
    else:
        if t[1]==1:
            s.insert(0,t[2])
        else:
            s.append(t[2])
print("".join(s))

Submission status: Runtime Error

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

is_seiichi = True
s_head_append = ''

for _ in range(n):
    ipt = input()

    if ipt[0] == '1':
        is_seiichi = not is_seiichi
    else:
        _, id_, s_ = ipt.split()
        is_head = True if id_ == '1' else False
        if is_seiichi ^ is_head:
            #s = s + s_
            s_head_append += s
        else:
            s = s_ + s_head_append + s
            s_head_append = ''

print(s + s_head_append if is_seiichi else s_head_append[::-1] + s[::-1])

Submission status: Runtime Error

Solution code:
s, _, q = open(0)
g = ""
for q in q:
  p, f, c = q.split()
  if p == "1":
    g, s = s, g
  else:
    if f == "1":
      g += c
    else:
      s += c
print(g[::-1] + s)

Submission status: Runtime Error

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

def exec_word(S,query):
  print(query,S)
  if query[0] == '1':
    return(S[::-1])
  elif query[0] == '2':
    if query[1] == '1':
      return (S + query[2])
    else:
      return(S + query[2])

for i in range(Q):
  query = input().split()
  S = exec_word(S,query)
  
print(S)

Submission status: Runtime Error

Solution code:
S = input()
Q = int(input())
Switch = False
Query = []

import collections
dq = collections.deque(S)

for i in range(Q):
    s = input()
    if s[0] == '1':
        if Switch:
            Switch = False
        else:
            Switch = True
    if s[0] == '2':
        if s[2] == '1' and Switch==False:
            dq.appendleft(s[4])
#             S = s[4]+S
        elif s[2]=='1' and Switch==True:
            dq.append(s[4])
#             S = S+s[4]
        elif s[2] == '2' and Switch==False:
            dq.append(s[4])
#             S = S+s[4]
        else:
#             S = s[4]+S
            dq.appendleft(s[4])

if Switch == False:
    print('',join(dq))
else:
    print(''.join(dq)[::-1])

Submission status: Runtime Error

Solution code:
s = input()
q = int(input())
ans_list1 = []
ans_list2 = []
flag = 1
for i in range(q):
    i = input().split()
    if i[0]=="1":
        flag *= -1
    else:
        if i[1]=='1':
            if flag==1:
                ans_list1.append(i[2])
            else:
                ans_list2.append(i[2])
        else:
            if flag==1:
                ans_list2.append(i[2])
            else:
                ans_list1.append(i[2])
    
    if flag==1:
        print(''.join(ans_list1[::-1]) + s +''.join(ans_list2))
    else:
        print(''.join(ans_list2[::-1]) + s[::-1] +''.join(ans_list1))

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

Submission status: Runtime Error

Solution code:
s = input()
q = int(input())
q_lists = [input().split() for _ in range(q)]
hanten = 0
for i in q_lists:
    if len(i)==1:
        hanten += 1
    else:
        f = i[1]
        c = i[2]
        if hanten%2==0:
            if f=='1':
                s = c + s
            else:
                s.append(c)
        else:
            if f=="1":
                s.append(c)
            else:
                s = c + s
print(s if hanten%2==0 else s[::-1])

Submission status: Runtime Error

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

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

j = ''.join
turn = 0
for _ in range(q):
    query = input()
    if len(query) == 1:
        turn += 1
    else:
        two, f, c = query.split()
        if f == '1':
            if turn % 2 == 0:
                s = j([c, s])
            else:
                s = j([s, c])
        else:
            if turn % 2 == 0:
                s = j([s, c])
            else:
                s = j([c, s])

if turn % 2 == 0:
    print(s)
else:
     print(s[::-1])

Submission status: Runtime Error

Solution code:
x = input()
s = []
n = int(input())
for i in range(n):
    array = list(map(str,input().split()))
    s.append(array)
    
a = ""
b = ""
c = 0
for i in range(n):
    if s[i][0] == "1":
        c += 1
        c = c%2
    else:
        if c == 0:
            if s[i][1] == "1":
                a = s[i][2] + a
            else:
                b = b + s[i][2]
        else:
            if s[i][1] == "1":
                b = b + s[i][2]
            else:
                a = s[i][2] + a
if c == 0:
    print(a + x + b)
else:
    print(reverse(b) + x + reverse(a))

Submission status: Runtime Error

Solution code:
x = input()
s = []
n = int(input())
for i in range(n):
    array = list(map(str,input().split()))
    s.append(array)
    
a = ""
b = ""
c = 0
for i in range(n):
    if s[i][0] == "1":
        c += 1
        c = c%2
    else:
        if c == 0:
            if s[i][1] == "1":
                a = s[i][2] + a
            else:
                b.append(s[i][2])
        else:
            if s[i][1] == "1":
                b.append(s[i][2])
            else:
                a = s[i][2] + a
if c == 0:
    print(a + x + b)
else:
    print(b + x + a)

Submission status: Runtime Error

Solution code:
from collections import deque
import sys
input = sys.stdin.readline


ss = list(input())
n = len(ss)-1

s = deque([])
for i in range(n):
    s.append(ord(ss[i]))

p = 1

q = int(input())
for i in range(q):
    inp = list(input())

    if inp[0] == '1':
        p *= -1
    else:
        c = ord(inp[4])
        f = inp[2]
        if (f == '1' and p == 1) or (f == '2' and p == -1):
            s.appendleft(c)
        else:
            s.append(c)


n = len(s)
if p == 1:
    for e in s:
        print(chr(e),end = '')
else:
    for i in range(n-1,-1,-1):
        print(chr(s[i]),end = '')
')')

Submission status: Runtime Error

Solution code:
from collections import deque

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

tmp = deque(S)
houkou = 1 #1:foward 2:reverse

for i in range(Q):
    inp = input().split()
    if inp[0] == '1':
        if houkou == 1:
            houkou = 2
        else:
            houkou = 1
    else:
        if houkou == 1 and inp[1] == '1':
            tmp.appendleft(inp[2])
        elif houkou == 1 and inp[1] == '2':
            tmp.append(inp[2])
        elif houkou == 2 and inp[1] == '1':
            tmp.append(inp[2])
        else:
            tmp.appendleft(inp[2])
    
    print(houkou, tmp)

output = ''.join(list(tmp))
if houkou == 2:
    output = output[::-1]

print(output)

Submission status: Runtime Error

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

ok = True
for i in range(q):
    query = input()
    if query[0] == '1':
        if ok:
            ok = False
        else:
            ok = True
    else:
        query = query.split()
        if ok:
            if query[1] == '1':
                s.appendleft(query[2])
            else:
                s.append(query[2])
        else:
            if query[1] == '1':
                s.append(query[2])
            else:
                s.appendleft(query[2])
if not ok:
    s = s[::-1]
print(''.join(list(s)))

Submission status: Runtime Error

Solution code:

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

is_reverse = False
before = ""
after = ""

for _ in range(q):
  lst = input().split()
  if lst[0] == "1":
    is_reverse = not is_reverse
  else:
    _, f, c = lst
    if f == "1":
      if not is_reverse:
        before = c + before
      else:
        after = after + c
    else:
      if not is_reverse:
        after = after + c
      else:
        before = c + before

if is_reverse:
  before, after = after.reverse, before.reverse

print(before + s + after)

Submission status: Runtime Error

Solution code:
from collections import deque
d = deque()
S = input()
d.append(S)
s = []
n = int(input())
for i in range(n):
    array = list(map(str,input().split()))
    s.append(array)

for i in range(N):
    if s[i][0] == "1":
        d.reverse()
    else:
        if s[i][1] == "1":
            d.extendleft(s[i][2])
        else:
            d.extend(s[i][2])

print("".join(list(d)))

Submission status: Runtime Error

Solution code:
from collections import deque

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

reverse = False
cnt = 0
for i in range(Q):
    inp = input().split()
    if inp[0] == "1":
        cnt += 1
        reverse = (cnt % 2 != 0)
        #S = S[::-1]
    else:
        f, c = inp[1:]
        if int(f) == 1:
            if reverse:
                S.append(c)
            else:
                S.leftappend(c)
        else:
            if reverse:
                S.leftappend(c)
            else:
                S.append(c)
S = "".join(S)
print(S[::-1] if reverse else S)

Submission status: Runtime Error

Solution code:
from collections import deque
d = deque(input())
flag = 0
for T_F_C in (input().split() for _ in range(int(input()))):
    if T_F_C[0] == '1':
        flag ^= 1
        continue
    _, f, c = T_F_C
    if (flag+int(f)) % 2:
	d.appendleft(c)
    else:
        d.append(c)
if flag:
    d = reversed(d)
print("".join(d))

Submission status: Runtime Error

Solution code:
from collections import deque
d = deque(input())
flag = 0
for T_F_C in (input().split() for _ in range(int(input()))):
    print("".join(d) if flag else "".join(reversed(d)))
    if T_F_C[0] == '1':
        flag ^= 1
        continue
    _, f, c = T_F_C
    if (flag+int(f)) % 2:
        d.append(c)
    else:
        d.appendleft(c)
if flag:
    print("".join(d))
else:
    print("".join(reversed(d)))

Submission status: Runtime Error

Solution code:
#タプルを深くしたまま進め、速度アップ？

def flatten_list_tuple_range(l):#深いパックをアンパック
    for el in l:
        if isinstance(el, (list, tuple, range)):
            yield from flatten_list_tuple_range(el)
        else:
            yield el

#初期入力
import sys
input = sys.stdin.readline
S = input().split()
S=S[0]
#print(S)
Q = int(input())
FQ_dic =tuple()
rev =0
for i in range(Q):
    FQ_dic =tuple(input().split())

    if      FQ_dic[0] =="1": #逆転する
                rev +=1
    else:                   #逆転なし  
        if      FQ_dic[1]=="1":     #前=左につける 
            if rev %2 ==0:
                    S = FQ_dic[2] ,S 
            else:
                    S = S,FQ_dic[2]
        else: #                      #後ろ=右につける
            if rev %2 ==0:
                    S = S ,FQ_dic[2] 
            else:        
                    S = FQ_dic[2] ,S
#print(S)
S =flatten_list_tuple_range(S)
S=[i for i in S] #理由は？だが1回リストにすると見える
#print(S)
S="".join(S) #最初の文字も連動するため
#print(S)
#逆転の回数で場合分け
if rev %2 ==0:
    print(*S,sep="")
else:
    print(*S[::-1],sep="")

Submission status: Runtime Error

Solution code:
import numpy as np
s = input()
q = int(input())
rbef = []
aft = []
sgn = 1
for i in range(q):
    qi = np.array(input().split(),dtype=str)
    if qi[0] == "1":
        sgn *= -1
    else:
        if (qi[1] == "1" or sgn == -1) and (qi[1] == "2" or sgn == 1):
            rbef.append(qi[2])
        else:
            aft.append(qi[2])


bef = ''.join(list(reversed(rbef)))
ans = (''.join(reversed(rbef))+s).join(aft)

if sgn == 1:
    print(ans)
else:
    rans = ''.join(list(reversed(ans)))
    print(rans)

Submission status: Runtime Error

Solution code:
#タプルを深くしたまま進め、速度アップ？
#初期入力
import sys
input = sys.stdin.readline
S = input().split()
S=S[0]
Q = int(input())
FQ_dic =tuple()
rev =0
for i in range(Q):
    FQ_dic =tuple(input().split())

    if      FQ_dic[0] =="1": #逆転する
                rev +=1
    else:                   #逆転なし  
        if      FQ_dic[1]=="1":     #後ろにつける
            if rev %2 ==0:
                    S = FQ_dic[2] , S
            else:
                    S = S ,FQ_dic[2] 
        else: #                      #前につける
            if rev %2 ==0:
                    S = S , FQ_dic[2]
            else:        
                    S = FQ_dic[2] , S

#深いパックをアンパック
def flatten_list_tuple_range(l):
    for el in l:
        if isinstance(el, (list, tuple, range)):
            yield from flatten_list_tuple_range(el)
        else:
            yield el
S =flatten_list_tuple_range(S)
S=[i for i in S]

#逆転の回数で場合分け
if rev %2 ==0:
    print(*S,sep="")
else:
    print(*S[::-1],sep="")

Submission status: Runtime Error

Solution code:
#初期入力
import sys
input = sys.stdin.readline
S = input().split()
S=S[0]
Q = int(input())
FQ_dic =tuple()
rev =0
for i in range(Q):
    FQ_dic =tuple(input().split())

    if      FQ_dic[0] =="1": #逆転する
                rev +=1
    else:                   #逆転なし   
        if      FQ_dic[1]=="1":     #後ろにつける
            if rev %2 ==0:
                    S = FQ_dic[2] , *S
            else:
                    S = *S ,FQ_dic[2] 
        else: #                      #前につける
            if rev %2 ==0:
                    S = *S , FQ_dic[2]
            else:        
                    S = FQ_dic[2] , *S
if rev %2 ==0:
    print(*S,sep="")
else:
    print(*S[::-1],sep="")

Submission status: Runtime Error

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

Submission status: Runtime Error

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

def RD(): return sys.stdin.read()
def II(): return int(input())
def MI(): return map(int,input().split())
def LI(): return list(map(int,input().split()))
def TI(): return tuple(map(int,input().split()))
"""
import numpy as np

def main():
    S = input()
    Q = int(input())
    #qi = [[0 for j in range(3)] for i in range(Q)]
    qi = np.zeros((Q, 3))
    for i in range(Q):
        n = input()
        if n != 1:
            qi[i]= n.split()
        else:
            qi[i][0] = 1

    for i in range(Q):

        if qi[i][0] == "1":
            S = S[::-1]
        elif qi[i][0] == "2":
            if qi[i][1] == "1":
                S = str(qi[i][2]) + S
            elif qi[i][1] == "2":
                S = S + str(qi[i][2]) 
    print(S)

if __name__ == "__main__":
	main()

Submission status: Runtime Error

Solution code:
s=input()
q=int(input())
cnt1=0
s_mae=[]
s_ushiro=[]
for i in range(q):
    query=list(input().split())
    if int(query[0])==1:
        cnt1+=1
    else:
        if (int(query[1])+cnt1)%2==0:
            s_ushiro.append(query[2]])
        else:
            s_mae=[query[2]]+s_mae
ans="".join(s_mae)+s+"".join(s_ushiro)
if cnt1%2==0:
    print(ans)
else:
    ans2=""
    for i in range(len(ans)):
        ans2=ans2+ans[-1-i]
        print(ans2)

Submission status: Runtime Error

Solution code:
q=int(input())
cnt1=0
for i in range(q):
    query=list(input().split())
    if int(query[0])==1:
        cnt1+=1
    else:
        if (int(query[1])+cnt1)%2==0:
            s.join(query[2])
        else:
            query[2].join(s)
if cnt1%2==0:
    print(s)
else:
    s2=""
    for i in range(len(s)):
        s2.join(s[-1-i])
    print(s2)

Submission status: Runtime Error

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

for _ in range(q):
  query = input()
  if query == '1':
    excnange += 1
  else:
    t, f, c = query.split()
    if f == '1':
      s = c + s
    else:
      s = s + c
      
if exchange % 2 == 0:
  print(s)
else:
  print(s[::-1])

Submission status: Runtime Error

Solution code:
import sys
from collections import deque

readline = sys.stdin.readline
readlines = sys.stdin.readlines

s = readline().rstrip()
q = readline()
queries = readlines()
s = deque(s)
tot_1 = 0
for i in queries:
    if i[0] == '1':
        tot_1 += 1
        continue
    c = i[4]
    if (tot_1 + int(i[2])) % 2 == 0:
        s.append(c)
    else:
        s.appendleft(c)
ans = "".join(queue) if tot_1 % 2 == 0 else "".join(reversed(queue))
print(ans)

Submission status: Runtime Error

Solution code:
import sys

readline = sys.stdin.readline
readlines = sys.stdin.readlines

s = readline().rstrip()
queries = readlines()
tot_1 = 0
for i in queries:
    if i[0] == '1':
        tot_1 += 1
    else:
        if (tot_1 + int(i[2])) % 2 == 0:
            s = s + i[4]
        else:
            s = i[4] + s
if tot_1 % 2 == 1:
    s = s[::-1]
print(s)

Submission status: Runtime Error

Solution code:
s = input()
q = int(input())
queries = []
for _ in range(q):
    query = input().split()
    if query[0] == "1":
        queries.append(query[0])
    if query[0] == "2":
        queries.append([query[0],query[1],query[2]])
done = True
for i in queries:
    if i == "1":#もしTiが１ならば先頭と末尾の文字を交換します
        if done == True: done = False
        else: done = True
    else:
        if done:
            if i[1] == "1":#もしFiが１ならば先頭にi[2]を追加します
                s = i[2] + s
            if i[1] == "2":#もしFiが２ならば末尾にi[2]を追加します
                s = s + i[2]
        if not done:
            if i[1] == "1":
                s = s + i[2]
            if i[1] == "2":
                s = i[2] + s
if done:
    print(s)
else:
    slist = list(s)
    for i in range(len(slist)//2):
        x = s[i]
        s[i] = s[len(slist)-i]
        s[len(slist)-i] = x
    print(s)

Submission status: Runtime Error

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

jun = True

for _ in range(Q):
    T = input().split()
    if T[0] == "1":
        jun = not jun
    
    else:

        if (T[1] == "1" and jun) or (T[1] == "2" and not jun):
            S = T[2] + S
            continue

        else:
            (T[1] == "2" and jun) or (T[1] == "1" and not jun):
            S = S + T[2]

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

Submission status: Runtime Error

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

S = input().strip()
Q = int(input())

for i in range(Q):
    q = input().split()
    if q[0] == '1':
        S.reverse()
    elif q[0] == '2':
        if q[1] == '1':
            S = '{}{}'.format(q[2], S)
        elif q[1] == '2':
            S = '{}{}'.format(S, q[2])

print(S)

Submission status: Runtime Error

Solution code:
from collections import deque
import sys
input = lambda: sys.stdin.readline().rstrip() 
def main():
    moji=input()
    d=deque(moji)
    cycle=int(input())
    flont_or_back=0
    for i in range(cycle):
        line=input()
        if line[0]=="1":
            flont_or_back+=1
        else:
            fi=line[2]
            ci=line[4]
            if flont_or_back%2==0:
                if fi=="1":
                    d.appendleft(ci)
                else:
                    d.append(ci)
            else:
                if fi=="1":
                    d.append(ci)
                else:
                    d.appendleft(ci)
    if flont_or_back%2==0:
        print("".join(d))
    else:
        print("".join(d[::-1]))

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
import sys
s=input()
q=int(input())
for i in range(q):
  query=sys.stdin.readline()
  if query=='1':
    s=s[::-1]
  else:
    t,f,c=map(str,query.split())
    f=int(f)
    if f==1:
      s = c + s
    else:
      s = s + c
print(s)

Submission status: Runtime Error

Solution code:
from collections import deque, Counter as cnt
from collections import defaultdict as dd
from operator import itemgetter as ig
from bisect import bisect_right as bsr
from math import factorial, ceil, floor
import sys
sys.setrecursionlimit(1000000)

# お約束
args = None
INF = float("inf")
MOD = int(1e9 + 7)


def input(*ps):
    if type(ps[0]) is list:
        return [input(*ps[0][:-1]) for _ in range(ps[0][-1])]
    elif len(ps) == 1:
        return ps[0](next(args))
    else:
        return [p(next(args)) for p in ps]


def nlist(n, v):
    if not n:
        return [] if type(v) is list else v
    return [nlist(n[1:], v) for _ in range(n[0])]


def yesno(v):
    print(["Yes", "No"][not v])


# エントリーポイント
def main():
    S, Q = input(str, int)
    front, end, state = "", "", 0
    for _ in range(Q):
        t = int(next(args))
        if t == 1:
            state = (state + 1) % 2
            continue
        F, C = int(next(args)), str(next(args))
        if state == 0:
            if F == 1:
                front = C + front
            else:
                end = end + C
        else:
            if F == 1:
                end = end + C
            else:
                front = C + front
        print(front, end, state)
    ans = front + S + end
    if state == 1:
        ans = end[::-1] + S[::-1] + front[::-1]
    print(ans)


if __name__ == '__main__':
    args = iter(sys.stdin.read().split())
    main()

Submission status: Runtime Error

Solution code:
s=input()
n=int(input())
q=[]
for _ in range(n):
  q.append(input().split())
sw=True #奇数回文字列の前後を入れ替えているときFalseになる
sl=''
sr=''
for i in range(n):
  if q[i][0]=='1':
    sw = not sw
  else:
    if q[i][1]=='1' and sw==True:
      for j in range(len(q[i][2])):
        sl += q[i][2][len(q[i][2])-1-j]
    elif q[i][1]=='2' and sw==False:
        sl += q[i][2][len(q[i][2])-1-j]
    else:
      sr +=q[i][2]

ans=''
if sw:
  for i in range(len(sl)):
    ans+=sl[len(sl)-1-i]
  print(ans+s+sr)
else:
  for i in range(len(sr)):
    ans+=sr[len(sr)-1-i]
  print(ans+s+sl)