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:
ans=-1
nums=input().split(" ")
t8,t10=int(nums[0]),int(nums[1])
value=max(round(t8*100/8),round(t10*100/10))+1
for i in range(value):
  v=value-i
  tax8=v*8/100
  tax10=v*10/100
  if tax8>=t8 and tax8<t8+1 and tax10>=t10 and tax10<t10+1:
    ans=v
  elif tax8<t8 or tax10<t10:
    break
print(ans)

Submission status: Runtime Error

Solution code:
def main():
    import sys
    input = sys.stdin.readline

    s = input()
    s = s[0:len(s)-1]
    Q = int(input())
    lq = [input().split() for _ in range(Q)]

    flg = 0 # 0:sonomama 1:gyakuten
    for q in lq:
        if q[0] == "1":
            if flg == 0:
                flg = 1
            else:
                flg = 0
        elif q[0]=="2":
            if (q[1] == "1" and flg ==0) or (q[1] == "2" and flg ==1):
                q[2] += s
                s = q[2]
            elif (q[1] == "2" and flg == 0) or (q[1] == "1" and flg == 1):
                s += q[2]

    if flg==1:
        s = s[::-1]
    print(s)

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
s = input()
q = int(input())
 
cnt = 0
for _ in range(q):
  t = input()
  if t[0] == "1":
    cnt += 1
  else:
    if cnt%2 == 1:
      s = "".join(list(reversed(s)))
      cnt = 0
    _,f,c = t.split(" ")
    if f == "1":
      s = c + s
    else:
      s = s + c
if cnt%2 == 1:
  s = "".join(list(reversed(s)))
  cnt = 0
 
print(s)
s = input()
q = int(input())

cnt = 0
for _ in range(q):
  t = input()
  if t[0] == "1":
    cnt += 1
  else:
    if cnt%2 == 1:
      s = "".join(list(reversed(s)))
      cnt = 0
    _,f,c = t.split(" ")
    if f == "1":
      s = c + s
    else:
      s = s + c
if cnt%2 == 1:
  s = "".join(list(reversed(s)))
  cnt = 0

print(s)

Submission status: Runtime Error

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

for i in range(Q):
    listA=list(map(input().split()))
    if listA[0]=='1':
        S = S[::-1]
    else:
        if listA[1]=='1':
            S = listA[2]+S
        else:
            S = S+listA[2]

Submission status: Runtime Error

Solution code:
S = input()
Q = int(input())
for i in range(Q):
    q = input()
    if q[0] == "1":
        S = S[::-1]
    else:
        if q[2] == "1":
            S = S + q[4]
        else:
            S = p[4] + S
print(S)

Submission status: Runtime Error

Solution code:
s = input()
q = int(input())
for i in range(q):
    l = input()
    if l[0] == '1':
        s=list(reversed(s))
    else:
        if l[2] == '1':
            s = l[4]+s
        else:
            s += l[4]
print(s)

Submission status: Runtime Error

Solution code:
S = input()
Q = int(input())
for i in range(Q):
    q = input()
    if q[0] == "1":
        S = S[::-1]
    else:
        if q[2] == "1":
            S = S + p[4]
        else:
            S = p[4] + S
print(S)

Submission status: Runtime Error

Solution code:
'''
    Auther: ghoshashis545 Ashis Ghosh
    college: jalpaiguri Govt Enggineering College
    Date:07/03/2020
'''
from math import ceil,sqrt,gcd,log,floor
from collections import deque
def ii(): return int(input())
def si(): return input()
def mi(): return map(int,input().strip().split(" "))
def li(): return list(mi())
def msi(): return map(str,input().strip().split(" "))
def lsi(): return list(msi())
#for _ in range(ii()):

s=si()
q=ii()
s=deque(s)
while(q):
    q-=1
    a=lsi()
    if(a[0]=='1'):
        s.reverse()
    else:
        if(a[1]=='1'):
            s.appendleft(a[2])
        else:
            s.append(a[2])
print("".join(s))

Submission status: Runtime Error

Solution code:
s = input()
q = int(input())
is_r = False
f_list = []
b_list = []

for i in range(q):
    q_i = input().split(' ')
    t_i = q_i[0]
    if t_i == '1':
        is_r = not is_r
    else:
        f_i = q_i[1]
        if (f_i == '1' and not is_r) or (f_i == '2' and is_r):
            f_list.append(q_i[2])
        else:
            b_list.append(q_i[2])

f_list = f_list[::-1]
f_list.append(s)
f_list.extend(b_list)
s = f_list.join('') if not is_r else f_list[::-1].join('')
print(s)

Submission status: Runtime Error

Solution code:
def input(): return sys.stdin.readline().strip()
st = input()
q = int(input())
for i in range(q):
    ipt = input()
    if ipt == "1":
        st = st[::-1]
    else:
        _, f, c = input().split()
        if f == "1":
            st = st + c
        else:
            st = c + st
print(st)

Submission status: Runtime Error

Solution code:
S = input()
Q = int(input())
for i in range(Q):
    q = input()
    if q[0] == "1":
        S = S[::-1]
    else:
        if p[2] == "1":
            S = S + p[4]
        else:
            S = p[4] + S
print(S)

Submission status: Runtime Error

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

for query in query_q:
    if query[0] == '1':
        S = S[::-1]
    elif query[0] == '2':
        if query[1] == '1':
            S = ''.join([query[2],*S])
        elif query[1] == '2':
            S = ''.join([*S,query[2]])

print(S)

Submission status: Runtime Error

Solution code:
S = input()
Q = int(input())
query_list = []
for i in range(Q):
    query_list.append(input())
# print(S, Q, query_list)

result = S
reverse_flag = False

for i in range(Q):
    # print(i + 1)
    q = query_list[i]
    # print(q)
    t = int(q[0])
    if t == 1:
        reverse_flag = not reverse_flag
    else:
        t, f, c = q.split()
        f = int(f)
        if f == 1:
            if reverse_flag:
                result = f"{result}{c}"
            else:
                #result = c + result
                result = f"{c}{result}"
        else:
            if reverse_flag:
                #result = c + result
                result = f"{c}{result}"
            else:
                #result = result + c
                result = f"{result}{c}"
    # print(t, f, c, result)
    # print("--------")
if reverse_flag:
    result = result[::-1]
print(result)

Submission status: Runtime Error

Solution code:
import math

A,B = [int(i) for i in input().split()]
emax = (A + 1) / 0.08
emin = math.ceil(A / 0.08)
tmax = (B + 1) / 0.1
tmin = B / 0.1

print(emax,emin)
print(tmax,tmin)

if emin < tmax and emin >= tmin:
    print(emin)
elif tmin < emax and tmin >= emin:
    print(int(tmin))
else:
    print(-1)

Submission status: Runtime Error

Solution code:
from sys import stdin

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

T_list = []
for q in range(Q):
    T = stdin.readline()
    T_list.append(T)

for t in T_list:
    if t == '1':
        S = S[::-1]

    else:
        T, f, c = t.split()

        if f == '1':
            S = c + S
        else:
            S = S + c

print(S)

Submission status: Runtime Error

Solution code:
from collections import deque

s = deque(input())
len_queries = int(input())
queries = [input().split() for _ in range(len_queries)]

is_forward = True
for query in queries:
    if "1" == query[0]:
        is_forward = not is_forward
    else:
        if is_forward:
            insert_index = 0 if "1" == query[1] else len(s)
            s.insert(insert_index, query[2])
        else:
            insert_index = len(s) if "1" == query[1] else 0
            s.insert(insert_index, query[2])
if not is_forward:
    s.reverse()
print("".join(s))

Submission status: Runtime Error

Solution code:
s = input()
q = int(input())
count = 0
beg = ""
end = ""
for i in range(q):
	a = input().split(" ")
	if len(a) == 1:
		count+=1
	else:
		pp = a[1]
		qq = a[2]
		if count%2 == 0:
			if pp == "1":
				s = qq+s
			else:
				s += qq
		else:
			if pp == "1":
				s += qq
			else:
				s = qq+s
if count%2 == 0:
	print(s)
else:
	print("".join(reversed(map(lambda x:x,s))))

Submission status: Runtime Error

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

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

ans = list()
for s in S:
    ans.append(s)
    
rev = 1
for i in range(Q):
    query = input().split()
    if query[0] == '1':
        rev = -1 * rev
    elif query[1] == '1':
        if rev > 0:
            ans.insert(0, query[2])
        else:
            ans.append(query[2])
    else:
        if rev > 0:
            ans.append(query[2])
        else:
            ans.insert(0, query[2])

if rev > 0:
    print(''.join(ans))
else:
    print(''.join(ans.reverse()))

Submission status: Runtime Error

Solution code:
S = list(input())
Q = int(input())
 
r = False
for i in range(Q):
    x = input().split()
    
    if x[0] == "1":
        r = not r
        
    else:
        if x[1] == "1":
            if r == False :
                S.insert(0, x[2])
            else:
                S.append(x[2])
        else:
            if r == True :
                S.insert(0, x[2])
            else:
                S.append(x[2])
        
if r:
    print("".join(S[::-1]))
else:
    print("".join(S))
S = list(input())
Q = int(input())

r = False
for i in range(Q):
    x = input().split()
    
    if x[0] == "1":
        r = not r
        
    else:
        if x[1] == "1":
            if r == False :
                S.insert(0, x[2])
            else:
                S.append(x[2])
        else:
            if r == True :
                S.insert(0, x[2])
            else:
                S.append(x[2])
        
if r:
    print("".join(S[::-1]))
else:
    print("".join(S))

Submission status: Runtime Error

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

s_deq = deque(s)
reversed = False

for _ in range(q_num):
    query = input().split()
    if query[0] == '1':
        reversed = False if reversed else True
    elif query[0] == '2':
        if not reversed:
            if query[1] == '1':
                s_deq.insert(0, query[2])
            elif query[1] == '2':
                s_deq.append(query[2])
        else:
            if query[1] == '2':
                s_deq.insert(0, query[2])
            elif query[1] == '1':
                s_deq.append(query[2])
if reversed:
    s_deq.reverse()
print(''.join(s_deq))

Submission status: Runtime Error

Solution code:
s=input()
n=int(input())
q=[list(map(int,input().split())) for i in range(n)]
atama=0
x=0
for i in range(n):
  if q[i][0]==2:
    x+=1

oshiri=len(s)+x-1
ans=[None]*(s+x)
x=0
for i in range(n)[::-1]:
  if q[i][0]==1:
    x+=1
  else:
    if (q[i][1]+x)%2==0:
      ans[oshiri]=q[i][2]
      oshiri-=1
    else:
      ans[atama]=q[i][2]
      atama+=1

if x%2==1:
  s=s[::-1]

for i in range(len(s)):
  ans[i+atama]=s[i]

print(ans,sep='')

Submission status: Runtime Error

Solution code:
# coding: utf-8

a, b = map(int, input().split())
# print(a,b)
# x=-1
# y=-1

xr=[]
yr=[]
for i in range(101):
    if i*0.08//1==a:
        # print("x:{}".format(i))
        x=i
        xr.append(x)
for i in range(101):
    if i *0.1//1==b:
        # print("y:{}".format(i))
        y=i
        yr.append(y)
flag=False

for i in xr:
    for j in yr:
        if i==j:
            print(i)
            flag=True
            break
    if flag:
        break
if not flag:
    print(-1)

Submission status: Runtime Error

Solution code:
for i in range(q):
    tfc = input()
    if len(tfc) == 1:
        if rev:
            rev = False
        else:
            rev = True
    else:
        t, f, c = tfc.split()
        if f == '1':
            if rev:
                back += c
            else:
                forward += c
        else:
            if rev:
                forward += c
            else:
                back += c

s = forward[::-1] + s + back
if rev:
    s = s[::-1]

print(s)

Submission status: Runtime Error

Solution code:
s = [_ for _ in input()]
qn = int(input())
rev = False
prevs = ['']*20000
nexts = ['']*20000
previ = 0
nexti = 0

for _ in range(qn):
  q = [_ for _ in input().split()]
  if q[0] == '1':
    rev = not rev
  else:
    if (rev and q[1] == '2') or (not rev and q[1] == '1'):
      prevs[previ] = q[2]
      previ += 1
    else:
      nexts[nexti] = q[2]
      nexti += 1

prevs.reverse()
s = prevs + s + nexts
if rev:
  s.reverse()
print("".join(s))

Submission status: Runtime Error

Solution code:
s=input()
q=int(input())
query=[]
for i in range(q):
    query.append(input())
rev=0
bef=""
aft=""
for i in range(q):
    if query[i][0]=="1":
        rev+=1
    else:
        n,f,c=query[i].split()
        if (rev+int(f))%2==0:
            aft=aft+c
        else:
            bef=c+bef
seqfin=bef+s+aft
if rev%2==0:
    print(seqfin)
else:
    for i in range(len(seqfin)):
        print(seqfin[len(seq)-i-1],end="")

Submission status: Runtime Error

Solution code:
s = input()
Q = int(input())
for i in range(Q):
    tmp = list(input())
    if len(tmp)==1:
        s = s[::-1]
    else:
        f,c = tmp[2], tmp[4]
        if f=='1':
            s = '{}{}'.format(c,s)
        elif f=='2':
            s = '{}{}'.format(s,c)
        print(s)
print(s)

Submission status: Runtime Error

Solution code:
from collections import deque
S = deque(list(input().strip()))
Q = int(input())
cnt = 0
for _ in range(Q):
    q = list(input().split())
    if int(q[0])==1:
        cnt += 1
    else:
        if cnt%2==0:
            if int(q[1])==1:
                S.appendleft(q[2])
            else:
                S.append(q[2])
        else:
            if int(q[1])==1:
                S.append(q[2])
            else:
                S.appendleft(q[2])
if cnt%2==0:
    print("".join(S))
else:
    
    print("".join(S.reverse()))

Submission status: Runtime Error

Solution code:
N, A, B = map(int, input().split())

division = N // (A + B)
count = division * A

count += N - division * (A + B) if A > N - division * (A + B) else A
print(count)

Submission status: Runtime Error

Solution code:
s=input()
q=int(input())
query=[]
for i in range(q):
    query.append(input())
rev=0
before=""
after=""
for i in range(q):
    if query[i][0]=="1":
        rev+=1
    else:
        n,f,c=query[i].split()
        if (rev+int(f))%2==0:
            after=after+c
        else:
            before=c+before
seqfin=before+s+after
if rev%2==0:
    print(seqfin)
else:
    for i in range(len(seqfin)):
        print(seqfin[len(seq)-i-1],end="")

Submission status: Runtime Error

Solution code:
s = str(input())
q = int(input())
state = True
front = ""
ending = ""
for _ in range(q):
  qi = str(input())
  if qi[0] == "2":
    fi,ci = map(str,qi[2:].split())
    if state == True:
      if fi == "1":
        front += ci
      else:
        ending += ci
    else:
      if fi == "1":
        ending += ci
      else:
        front += ci
  else:
    state = not state
if state:
  first = front[::-1]
  lastt = ending
else:
  first = ending[::-1]
  lastt = front
  s = s[::-1]
for f in first:
  print(f,end="")
for si in s:
  print(s,end="")
for l in lastt:
  print(l,end="")

Submission status: Runtime Error

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

for i in range(Q):
  a=input().split()
  if a[0]=="1":
    S=S[::-1]
  else:
    if a[1]=="1":
      S="".join((a[2],S))
    else:
      S="".join([(S,a[2]))
      
print(S)

Submission status: Runtime Error

Solution code:
s = input()
q = int(input())
query_d = {}
for i in range(q):
    if i == 0:
        query_d[0] = list(input().split())
    else:
        query = list(input().split())
        if (query[0] == '1') and (query_d[i-1][0] == '1'):
            query_d.pop(i-1)
        else:
            query_d[i] = query

for query in query_d.values():
    if int(query[0]) == 1:
        s = s[::-1]
    else:
        if int(query[1]) == 1:
            s = query[2] + s
        else:
            s = s + query[2]
print (s)

Submission status: Runtime Error

Solution code:
 S = input()
Q = int(input())
query = ""
list_1 = []

for i in range(Q):
    query = input()

    if query == "1":
        S = S[::-1]
    else:
        list_1 = query.split()
        if list_1[1] == "1":
            S = list_1[2] + S
        else:
            S = S + list_1[2]

print(S)

Submission status: Runtime Error

Solution code:
ii = lambda : int(input())
mi = lambda : map(int,input().split())
li = lambda : list(input().split())

s = input()
q = int(input())
flag = 1

from collections import deque

s1 = deque()
s2 = deque()

ss = s[-1]
for i in range(len(s)):
    s1.append(s[i])
    s2.append(ss[i])
for i in range(q):
    a = li()
    if int(a[0]) == 1:
        flag *= -1
    else:
        tmp = 1
        if int(a[1]) == 2:
            tmp = -1
        ff = flag * tmp
        if ff == 1:
            s1.appendleft(a[2])
            s2.append(a[2])
        else:
            s1.append(a[2])
            s2.appendleft(a[2])
ans = ""
if flag == 1:
    for i in range(len(s1)):
        ans += s1[i]
    print(ans)
else:
    for i in range(len(s2)):
        ans += s2[i]
    print(ans)

Submission status: Runtime Error

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


def rev_recursion(original_string):
    if len(original_string) == 1:
        return original_string
    else:
        return rev_recursion(original_string[1:]) + original_string[0]


for i in range(Q):
    q = list(map(str, input().split()))
    if int(q[0]) == 1:
        S = rev_recursion(S)
    elif int(q[0]) == 2:
        if int(q[1]) == 1:
            S = q[2] + S
        elif int(q[1]) == 2:
            S = S + q[2]

print(S)

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
s = str(input())
q = int(input())
state = True
front = ""
end = ""
for _ in range(q):
  qi = str(input())
  if qi[0] == "2":
    fi,ci = map(str,qi[2:].split())
    if state == True:
      if fi == "1":
        front += ci
      else:
        end += ci
    else:
      if fi == "1":
        end += ci
      else:
        front += ci
  else:
    state = not state
if state:
  first = front[::-1]
  last = end
else:
  first = end[::-1]
  last = front
  s = s[::-1]
for f in first:
  print(f,end="")
for si in s:
  print(s,end="")
for l in last:
  print(l,end="")

Submission status: Runtime Error

Solution code:
for i in range(Q):
    r = sys.stdin.readline().strip('\n')
    if r == '1':
        S = S[::-1]
    else:
        ni, f, c = r.split()
        if f=='1':
            S = c + S
        else:
            S+=c

print(S)

Submission status: Runtime Error

Solution code:
def main():
  s=input()
  q=int(input())
  for _ in [0]*q:
    a=list(input().split())
    if len(a)==1:
      s=reversed(s)
    else:
      if a[1]=='1':
        s=a[2]+s
      else:
        s=s+a[2]
  print(s)
if __name__=='__main__':
  main()

Submission status: Runtime Error

Solution code:
s=input()
q=int(input())
cnt=0
qq=[tuple(input().split()) for _ in range(q)]
for j in qq:
    if  len(j)!=1 :
        if (cnt+int(j[1]))%2==1:
            s=j[2]+s
        else:
            s=s+j[2]
    else:
        cnt+=1
if cnt%2==0:
    print(s)
else:
    print(s[::-1])
A

Submission status: Runtime Error

Solution code:
S = input()
k = []
Q = int(input())
now = 0
ans = ""
for i in range(Q):
    K = input().split()
    if K[0] == "1":
        if mode == 1:
            mode = 0
        else:
            mode = 1
    else:
        if K[1] == "1":
            if mode == 0:
                #k[left] = K[2]
                S = K[2]+ S
            else:
                S = S+K[2]
        else:
            if mode == 0:
                S = S+K[2]
            else:
                S = K[2]+S
if mode == 1:
    for i in range(1,len(S)+1):
        ans += S[-i]
    print(ans)
else:
    print(S)

Submission status: Runtime Error

Solution code:
s = str(input())
q = int(input())
state = True
front = ""
end = ""
for _ in range(q):
  qi = str(input())
  if qi[0] == "2":
    fi,ci = map(str,qi[2:].split())
    if state == True:
      if fi == "1":
        front += ci
      else:
        end += ci
    else:
      if fi == "1":
        end += ci
      else:
        front += ci
  else:
    state = not state
if state:
  first = front[::-1]
  last = end
else:
  first = end[::-1]
  last = front
  s = s[::-1]
for f in first:
  print(f,end="")
for si in s:
  print(s,end="")
for l in last:
  print(l,end="")

Submission status: Runtime Error

Solution code:
s=input()
n=int(input())
for i in range(n):
  l=input().split()
  q=int(l[0])
 
  
  if q==1:
    ll=list(s.reversed())
    s="".join(ll)
  elif q==2:
    f=int(l[1])
    c=l[2]
    if f==1:
      s=c+s
    elif f==2:
      s=s+c
print(s)

Submission status: Runtime Error

Solution code:
from collections import deque
s=deque(list(input()))
q=int(input())
r = False
for i in range(q):
    tmp = list(input().split(' '))
    if tmp[0]=='1':
        r = not r
    elif (not r and tmp[1]=='1') or (r and tmp[1] =='2'):
        s.appendleft(tmp[2])
    else:
        s.append(tmp[2])
if not r:
    for i in s:
        print(i,end='')
else:
    for i in s[::-1]:
        print(i,end='')

Submission status: Runtime Error

Solution code:
from collections import deque
S = deque(list(input().strip()))
Q = int(input())
cnt = 0
for _ in range(Q):
    q = list(input().split())
    if int(q[0])==1:
        cnt += 1
    else:
        if cnt%2==0:
            if int(q[1])==1:
                S.appendleft(q[2])
            else:
                S.append(q[2])
        else:
            if int(q[1])==1:
                S.append(q[2])
            else:
                S.appendleft(q[2])
if cnt%2==0:
    print("".join(S))
else:
    S = S[::-1]
    print("".join(S))

Submission status: Runtime Error

Solution code:
S = str(input())
Q = int(input())
st = [0] * Q
n = [0] * Q
p = 0
for i in range(Q):
  q = str(input())
  if int(q[0]) == 1:#Ti = 1
    s_h += 1
  else:
    if int(q[2]) == 1:#先頭
      st[p] = q[4]
      n[p] = s_h
      p += 1
    else:
      st[p] = q[4]
      n[p] = s_h + 1
      p += 1

print(S)

Submission status: Runtime Error

Solution code:
import sys

S = input()

Q = int(input())
flag=0
mae = ''
matsu = ''
for i in range(Q):
    r = sys.stdin.readline().strip('\n')
    if r == '1':
        flag = 1- flag
    else:
        ni, f, c = r.split()
        if f=='1':
            if flag == 0:
                mae += c
            else:
                matsu += c
        else:
            if flag == 0:
                matsu += c
            else:
                mae += c
    if flag ==0:
        print(mae[::-1]+S+matsu)
    else:
        print(matsu[::-1]+S+mae)

Submission status: Runtime Error

Solution code:
# print('input >>')
S = input()
Q = int(input())
queries = []
for _ in range(Q):
    queries.append(input())

is_reverse = False

for q in queries:
    ql = list(q.split())
    if len(ql) == 1:
        is_reverse = not is_reverse
    else:
        if ql[1] == '1': # 先頭に加える
            if not is_reverse:
                S = ql[2] + S
            else:
                S = S + ql[2]
        else: # 末尾に加える
            if not is_reverse:
                S = S + ql[2]

            else:
                S = ql[2] + S
    print(S, is_reverse)

# print('-----output-----')
if not is_reverse:
    print(S)
else:
    print(S[::-1])

Submission status: Runtime Error

Solution code:
s = [_ for _ in input()]
qn = int(input())
rev = False
prevs = ['']*20000
nexts = ['']*20000
previ = 1
nexti = 0

for _ in range(qn):
  q = [_ for _ in input().split()]
  if q[0] == '1':
    rev = not rev
  else:
    if (rev and q[1] == '2') or (not rev and q[1] == '1'):
      prevs[20000-previ] = q[2]
      previ += 1
    else:
      nexts[nexti] = q[2]
      nexti += 1

if rev:
  print("".join(nexts + s + prevs))
else:
  print("".join(prevs + s + nexts))

Submission status: Runtime Error

Solution code:
S = input()
Q = int(input())
reverse = 0
for i in range(Q):
  Query = list(input().split())
  if Query[0] == "1":
    reverse = (reverse + 1) % 2
  if Query[0] == "2":
    f = int(Query[1])
    c = query[2]
    if (f + reverse) % 2 == 1:
      S = c + S
    else:
      S = S + c

if reverse == 0:
  print(S)
else:
  print(S[::-1])

Submission status: Runtime Error

Solution code:
head=deque()
tail=deque()
for _ in range(q):
    Q=input().split()
    if len(Q)==1:
        rev=1-rev
    if len(Q)==3:
        if(rev==0 and Q[1]=="1" )or (rev==1 and Q[1]=="2"):
            head.appendleft(Q[2])
        else :
            tail.append(Q[2])
if rev==0:
    ans="".join(head)+s+"".join(tail)

else:
    ans="".join(tail)[::-1]+s[::-1]+"".join(head)[::-1]

print(ans)

Submission status: Runtime Error

Solution code:
import sys
from pprint import pprint

S = sys.stdin.readline().strip()
Q = int(sys.stdin.readline().strip())

n_rev = 0
for _ in range(Q):
    tmp = sys.stdin.readline().strip().split()
    if tmp[0] == "1":
        n_rev += 1
    else:
        f, c = tmp[1], tmp[2]
        if f == "1":
            if n_rev % 2 == 0:
                S = c + S
            else:
                S = S + s
        else:
            if n_rev % 2 == 0:
                S = S + c
            else:
                S = c + S

if n_rev % 2 == 0:
    print(S)
else:
    print(S[::-1])

Submission status: Runtime Error

Solution code:
# coding: utf-8

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


rev = False
for _ in range(Q):
    line = input()

    if line[0] == "1":
        rev = not rev

    else:
        if line[2] == "1":
            if rev:
                S = "".join(S, line[4])
            else:
                S = "".join(line[4], S)
        else:
            if rev:
                S = "".join(line[4], S)
            else:
                S = "".join(S, line[4])

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

Submission status: Runtime Error

Solution code:
S = input()
N = int(input())
Q = [input().split() for _ in range(N)]
start = ''
end = ''

# Sの向きを定義1:前から0:後ろから
direct = 0
for i in range(N):
  if Q[i][0] =='1':
    direct^=1
  else:
    if direct == int(Q[i][1])-1:
      start += Q[i][2]
      print('start',start)
    else:
      end += Q[i][2]
      print('end',end)
      
ans = start[::-1]+S+end
print(ans) if direct == 0 else print(ans[::-1])

Submission status: Runtime Error

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

flag=0

for i in range(Q):
	x=input().split()
	if x[0]=="1": flag+=1
	else:
		if x[1]=="1":
			if flag%2==0: S=[x[2]]+S
			else: S.append([2])
		else:
			if flag%2==0: S.append(x[2])
			else: S=[x[2]]+S

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

print("".join(S))

Submission status: Runtime Error

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

qs = [list(input().split()) for _ in range(q)]

left = ''
right = ''


front = False
for i in qs:
    if(i[0] == '1'):
        front = not front
        continue

    f = int(i[1])
    c = i[2]
    if(front + f == 2):
        right = right + c
    else:
        left = left + c

if(not front):
    ans = left[::-1] + s + right
else:
    ans = right[::-1] + s[::-1] + left

print(ans)

Submission status: Runtime Error

Solution code:
#include <iostream>
#include <algorithm>
using namespace std;
#define length 40000000010

int main(void){
  char* S=(char*)malloc(sizeof(char)*length);
  string s;
  cin>>s;
  long left=length/2;
  long right=left+s.size()-1;
  for(int i=0;i<s.size();i++){
    S[length/2+i]=s[i];
  }
  int t,f;
  char c;
  int rev=1;
  int Q;
  cin>>Q;
  for(int i=0;i<Q;i++){
    cin>>t;
    if(t==1){
      rev++;
      rev=rev%2;
    }
    else{
      cin>>f>>c;
      if((f+rev)%2==1){
        left--;
        S[left]=c;
      }
      else{
        right++;
        S[right]=c;
      }
    }
  }
  if(rev==0){
    for(long i=left;i<=right;i++){
      cout<<S[i];
    }
  }
  else{
    for(long i=right;i>=left;i--){
      cout<<S[i];
    }
  }
  cout<<endl;
  return 0;
}

Submission status: Runtime Error

Solution code:
mport sys
sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))
from collections import deque
def resolve():
    S = deque(list(sr().split()), maxlen=1000000)
    Q = ir()
    rev = 1
    for i in range(Q):
        T = list(sr().split())
        if int(T[0]) == 1:
            rev *= -1
        else:
            x, F, C = T
            if int(F) == 1:
                if rev == 1:
                    S.appendleft(C)
                else:
                    S.append(C)
            else:
                if rev == 1:
                    S.append(C)
                else:
                    S.appendleft(C)
    if rev == -1:
        S.reverse()
    print(*S, sep='')
resolve()

Submission status: Runtime Error

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

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

for i in range(q):
  tfc = input().split()
  if tfc[0] == '1':
    s.reverse()
    #s = s[::-1]
  else:
    if tfc[1] == '1':
      s = tfc[2] + s
    else:
      s = s + tfc[2]
print(s)

Submission status: Runtime Error

Solution code:
#D
from collections import deque
Str = str(input())
Q = int(input())
S = deque()
S.append(Str)
for i in range(Q):
    a = list(map(str, input().split()))
    if a[0] == '1':
        S.reverse()
    elif a[1] == '1':
        S.insert(0, a[2])
    else:
        S.append(a[2])

print(''.join(S))

Submission status: Runtime Error

Solution code:

S=input()
Q=int(input())
qls=[list(map(str,input().split())) for _ in range(Q)]
res=['']*(2*Q)
res[Q]=S
ins = [Q+1, Q-1]
flg=0 #0:right, 1:left
for q in qls:
    if q[0]=='1':
        flg = (flg+1)%2
    else:
        x = (flg+int(q[1]))%2
        idx = ins[x]
        res[idx] = q[2]
        if x==0:
            ins[x]+=1
        else:
            ins[x]-=1
if flg==0:
    print(''.join(res))
else:
    print(''.join(res[::-1]))