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: Wrong Answer

Solution code:
s = input().split()
n = int(input())
query_li = (input().split() for i in range(n))

flag = 1

for query in query_li:
    if int(query[0]) == 1:
        flag = flag * -1
    else:
        if int(query[1]) == 1:
            if flag == 1:
                s.insert(0, query[2])
            else:
                s.insert(-1, query[2])
        else:
            if flag == 1:
                s.insert(-1, query[2])
            else:
                s.insert(0, query[2])

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

s = ''.join(s)

print(s)

Submission status: Wrong Answer

Solution code:
S = input()
Q = int(input())
is_norm = True  # Sが元の順番かどうか
s_head = ""     # 元のSに対して先頭側につく文字列
s_tail = ""     # 元のSに対して末尾側につく文字列

for i in range(0, Q):
    query = input().split()
    if int(query[0]) == 1:
        is_norm = not is_norm
    else:
        if is_norm:
            if int(query[1]) == 1:
                s_head = query[2] + s_head
            else:
                s_tail = s_tail + query[2]
        else:
            if int(query[1]) == 1:
                s_tail = s_tail + query[2]
            else:
                s_head = query[2] + s_head

if is_norm:
    print(s_head + S + s_tail)
else:
    print(s_tail + S[::-1] + s_head)

Submission status: Wrong Answer

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

S_list = list(S)

r = 1
a = []
b = []

for i in range(Q):
    q = input().split()
    if q[0] == '1':
        r *= -1
        continue
    elif len(q) == 3:
        if q[1] == '1':
            if r == -1:
                b.append(q[2])
            else:
                a.append(q[2])
        else:
            if r == -1:
                a.append(q[2])
            else:
                b.append(q[2])
                
answer = b + S_list + a

if r == 1:
    answer = answer[::-1]
    
print(''.join(answer))

Submission status: Wrong Answer

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

for query in queries:
    if query[0] == "1":
        s = s[-1] + s[1:-1] + s[0]
    else:
        if query[1] == "1":
            s = query[2] + s
        else:
            s = s + query[2]

print(s)

Submission status: Wrong Answer

Solution code:
S = input()
Q = int(input())
flag = False
for i in range(Q):
    T = input()
    if len(T)==1:
        flag = not flag
        continue
    if flag:
        S = S[::-1]
        flag = not flag
        
    if T[2]=="1":
        S = T[4]+S
        continue
    else:
        S = S+T[4]
        continue
    
print(S)

Submission status: Wrong Answer

Solution code:
S = input()
Q = int(input())
flag = False
for i in range(Q):
    T = input()
    if len(T)==1:
        flag = not flag
        continue
    if flag:
        S = S[::-1]
        flag = not flag
        
    if T[2]=="1":
        S = T[4]+S
        continue
    else:
        S = S+T[4]
        continue
    if i == Q-1 and flag:
        S = S[::-1]
    
print(S)

Submission status: Wrong Answer

Solution code:
S = input()
Q = int(input())
flag = False
for i in range(Q):
    T = input()
    if len(T)==1:
        flag = not flag
        continue
    if flag:
        S = S[::-1]

    if T[2]=="1":
        S = T[4]+S
    else:
        S = S+T[4]
    
print(S)

Submission status: Wrong Answer

Solution code:
s=input()
q=int(input())
list=[input() for i in range(q)]
for i in list:
  i=i.split()
  if len(i)==1:
    s=s[::-1]
  else:
    if i[1]==1:
      s=i[2]+s
    else:
      s=s+i[2]

print(s)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
s = input()
q = int(input())
mode = True
l = ''
r = ''
for _ in range(q):
    tfc = input().split()
    if int(tfc[0]) == 1:
        mode = not mode
    else:
        if mode:
            if int(tfc[1]) == 1:
                l += tfc[2]
            else:
                r += tfc[2]
        else:
            if int(tfc[1]) == 1:
                r += tfc[2]
            else:
                l += tfc[2]
print(l[::-1]+s+r if mode else r[::-1]+s+l)

Submission status: Wrong Answer

Solution code:
s = input()
q = int(input())
mode = True
l = ''
r = ''
for _ in range(q):
    tfc = input().split()
    if int(tfc[0]) == 1:
        mode = not mode
    else:
        if mode:
            if int(tfc[1]) == 1:
                l += tfc[2]
            else:
                r += tfc[2]
        else:
            if int(tfc[1]) == 1:
                r += tfc[2]
            else:
                l += tfc[2]
print(l[::-1]+s[::-1]+r if mode else r+s+l)

Submission status: Wrong Answer

Solution code:
s = input()
n = int(input())
query = []
for i in range(n):
    query.append(input().split())
cond = 1
front = ''
for i in range(n):
    if query[i] == ['1']:
        cond *= -1
    else:
        if cond == 1:
            if query[i][1] == '1':
                s = query[i][2] + s
            else:
                front +=query[i][2]
        else:
            if query[i][1] == '2':
                s = query[i][2] + s
            else:
                front += query[i][2]
if cond == -1:
    s = s[::-1] + front
else:
    s = front[::-1] + s
print(s)

Submission status: Wrong Answer

Solution code:
s = input()
n = int(input())
query = []
for i in range(n):
    query.append(input().split())
counter = 0

for i in range(n):
    if query[i] == ['1']:
        counter += 1
        for j in range(n-(i+1)):
            if query[i+j+1] == ['1']:
                break
            if query[i+j+1][1] == '2':
                query[i+j+1][1] = '1'
            else:
                query[i+j+1][1] = '2'
for i in range(n):
    if query[i] != ['1']:
        if query[i][1] == '1':
            s = query[i][2] + s
        else:
            s = s + query[i][2]
if counter%2 == 1:
    s = s[::-1]
print(s)

Submission status: Wrong Answer

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

list_s = list(s)

a = [0] * q

for i in range(q):
    a[i] = list(map(str, input().split()))
    
for i in range(q):
    if a[i] == ['1']:
        for j in range(len(list_s) // 2):
            z = list_s[j]
            y = list_s[-1-j]
            list_s[-1-j] = z
            list_s[j] = y
    else:
        if a[i][1] == '1':
            list_s.insert(0, a[i][2])
        else:
            list_s.append(a[i][2])
    print(list_s)

s = ''.join(list_s)
            
print(s)

Submission status: Wrong Answer

Solution code:
s = input()
q = int(input())
r = 1
for _ in range(q):
    l = input()
    if l[0] == '1':
        r = -r
    elif l[2] == '1' or (l[2] == '2' and r):
        s = l[4] + s
    else:
        s = s + l[4]
print(s[::r])

Submission status: Wrong Answer

Solution code:
s = input().split()
q = int(input())
for _ in range(q):
    l = input().split()
    if l[0] == '1':
        s = s[::-1]
    elif l[1] == '1':
        s.insert(0, l[2])
    else:
        s.append(l[2])
print(''.join(s))

Submission status: Wrong Answer

Solution code:
from collections import deque
S = deque([input()])
N = int(input())
is_inversed = False
for i in range(N):
    Q = input()
    if Q[0] == '1':
        is_inversed = not (is_inversed)
    else:
        if (Q[2] == '1' and is_inversed) or (Q[2] == '2' and not (is_inversed)):
            S.append(Q[4])
        else:
            S.appendleft(Q[4])

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

Submission status: Wrong Answer

Solution code:
S=input()
Q=int(input())
while Q>=1:
  tfc=list(input().split())
  if int(tfc[0])==1:
    box1=S[0]
    box2=S[-1]
    S=box2+S[1:-1]+box1
  elif int(tfc[0])==2:
    if int(tfc[1])==1:
      S=tfc[2]+S
    elif int(tfc[1])==2:
      S=S+tfc[2]
  Q-=1
ans=''.join(S)
print(ans)

Submission status: Wrong Answer

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

top_list = []
end_list = []

flag = 1

for q in q_list:
    if q[0] == '1':
        flag *= -1
    else:
        if flag == 1:
            if q[1] == '1':
                top_list.append(q[2])
            else:
                end_list.append(q[2])
        else:
            if q[1] == '2':
                top_list.append(q[2])
            else:
                end_list.append(q[2])
if flag == 1:            
    print(''.join(top_list[::-1]) + s + ''.join(end_list))
else:
    print(''.join(end_list[::-1]) + s + ''.join(top_list))

Submission status: Wrong Answer

Solution code:
S=input()
Q=int(input())
F=0
C=''
while Q>=1:
  tfc=list(input().split())
  if int(tfc[0])==1:
    box1=S[0]
    box2=S[-1]
    S=box2+S[1:-1]+box1
  elif int(tfc[0])==2:
    if int(tfc[1])==1:
      S=tfc[2]+S
    elif int(tfc[1])==2:
      S=S+tfc[2]
  Q-=1
ans=''.join(S)
print(ans)

Submission status: Wrong Answer

Solution code:
# 158-D ver2
from collections import deque

s = deque()
s.append(input())
q = int(input())
is_not_reverse = True
for _ in range(q):
    command = [e for e in input().split()]
    if command[0] == '1':
        is_not_reverse = not is_not_reverse      
    elif command[1] == '1':
        if is_not_reverse:
            s.appendleft(command[2])
        else:
            s.append(command[2])
    elif command[1] == '2':
        if is_not_reverse:
            s.append(command[2])
        else:
            s.appendleft(command[2])
print(''.join(s if is_not_reverse else reversed(s)))

Submission status: Wrong Answer

Solution code:
S = []
S.append(input("S"))
Q = int(input("Q"))
query = []
for i in range(Q) :
    query.append(input())
    if query[i] == "1" : #Ti = 1 reverse
        S[0], S[len(S)-1] = S[len(S)-1], S[0]
    else : #Ti = 2 str +
        T, F, C = query[i].split()
        if F == "1" :#Fi = 1 str + top
            S.insert(0, C)
        else :#Fi = 2 str + end
            S.append(C)
print(''.join(S))

Submission status: Wrong Answer

Solution code:
from collections import deque

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

for _ in range(q):
    x = input().split()
    
    if(x[0]=='1'):
        reflag = not reflag
        #s.reverse()
    else:
        if(reflag):
            s.reverse()
            reflag = False
        
        s.appendleft(x[2]) if(x[1]=='1') else s.append(x[2])

if(reflag):
    s.reverse()

print(''.join(s))

Submission status: Wrong Answer

Solution code:
reply = input()
num = int(input())
hanten = False
for i in range(num):
    text = input().split()
    if text[0] == "1":
        hanten = False if hanten else True
        if i == num:
            reply = reply[::-1]
    else:
        if text[1] == "1":
            if hanten:
                reply = reply + text[2]
            else:
                reply = text[2] + reply
        else:
            if hanten:
                reply = text[2] + reply
            else:
                reply = reply + text[2]

print(reply)

Submission status: Wrong Answer

Solution code:
reply = input()
num = int(input())
hanten = False
for i in range(num):
    text = input().split()
    if text[0] == "1":
        hanten = False if hanten else True
        if i == num - 1:
            reply = reply[::-1]
    else:
        if text[1] == "1":
            if hanten:
                reply = reply + text[2]
            else:
                reply = text[2] + reply
        else:
            if hanten:
                reply = text[2] + reply
            else:
                reply = reply + text[2]

print(reply)

Submission status: Wrong Answer

Solution code:
rflag = 0
s = [i for i in input()]
q = int(input())

for i in range(q):
    x = input().split()
    
    if(x[0] == '1'):
        rflag = rflag + 1
        # s = s[::-1]
    else:
        if((rflag % 2) == 0):
            rflag = 0
        else:
            s = s[::-1]
            rflag = 0
            
        s.insert(0,x[2]) if(x[1]=='1') else s.append(x[2])

print(''.join(s))

Submission status: Wrong Answer

Solution code:
def msf(s,q,ql):
    ql = ['0'] + ql
    ql.append(list('0'))
    
    for i in range(3 - len(ql) % 3):
        ql.append(list('0'))

    len_ql = len(ql)
    for i in range(1,len_ql-1):
        t0 = ql[i-1][0]
        t1 = ql[i][0]
        t2 = ql[i+1][0]
        if t0 == "1" and t1 == "1":
            pass
        elif t1 == "1" and t2 == "1":
            pass
        elif t1 == "1":
            s = list(reversed(s))
        elif t1 == "2":
            f = ql[i][1]
            c = list(ql[i][2])
            if f == "1":
                s = c + s
            elif f == "2":
                s = s + c
    return s
    
if __name__ == '__main__':
    s = list(input())
    q = int(input())
    ql = [list(input().split()) for _ in range(q)]
    
    print(''.join(msf(s,q,ql)))

Submission status: Wrong Answer

Solution code:
s = input()
n = int(input())
li = []
for _ in range(n):
  l = list(map(str,input().split()))
  li.append(l)
for lis in li:
  if lis[0] == "1":
    if len(s) % 2 == 0:
      s = s[int(len(s)/2):len(s)] + s[0:int(len(s)/2)]
    else:
      s = s[int((len(s)+1)/2):len(s)] + s[int((len(s)-1)/2)] + s[0:int((len(s)-1)/2)]
  else:
    if lis[1] == "1":
      s = lis[2]+s
    else:
      s = s + lis[2]
print(s)

Submission status: Wrong Answer

Solution code:
from collections import deque

def solve():
    S = input()
    Q = int(input())
    length = len(S)
    mid = list(S[1:-1]) if S[1:-1] else []
    mid = deque(mid)
    first,last = S[0],S[-1]
    
    for _ in range(Q):
        query = input().split()
        if len(query) == 1:
            first,last = last,first
        else:
            T,F,C = query
            T = int(T)
            F = int(F)
            if F == 1:
                if length > 1: mid.appendleft(first)
                first = C
            else:
                if length > 1: mid.append(last)
                last = C
                
            length += 1
    
    if length == 1:
        print(S)
    else:
        mid.appendleft(first)
        mid.append(last)
        print(''.join(mid))
        
solve()

Submission status: Wrong Answer

Solution code:
s=input()
n=int(input())
tmp=0
suffix=""
prefix=""
for i in range(n):
	inp=input()
	l=list(inp.split(" "))
	if(len(l)==1):
		tmp+=1
	else:
		ch=l[2]
		op=int(l[1])
		if(tmp%2==1):
			op=3-op
		if(op==1):
			prefix=ch+prefix
		else:
			suffix=suffix+ch
if(tmp%2==1):
	s=s[::-1]
ans=prefix+s+suffix
if(tmp%2==1):
	ans=ans[::-1]
print(ans)

Submission status: Wrong Answer

Solution code:
S = input()
l1 = []
l2 = []
rev = False
for _ in range(int(input())):
    l = [s for s in input().split()]
    if l[0] == "1":
        rev = not rev
    elif l[1] == "1":
        if rev:
            l2.append(l[2])
        else:
            l1.append(l[2])
    else:
        if rev:
            l1.append(l[2])
        else:
            l2.append(l[2])

if rev:
    ret = "".join(l2)[::-1] + S + "".join(l1)
else:
    ret = "".join(l1)[::-1] + S + "".join(l2)

print(ret)

Submission status: Wrong Answer

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

for i in range(q):
    tfc = input().split()
    if len(tfc) == 1:
        t = tfc[0]
    else:
        t, f, c = tfc
    if t == '1':
        s[0], s[-1] = s[-1], s[0]
    elif t == '2':
        if f == '1':
            s[0:0] = c
        elif f == '2':
            s.append(c)

print("".join(s))

Submission status: Wrong Answer

Solution code:
from collections import deque
S=deque(input())
q=int(input())
rev=False
for i in range(q):
  c=list(input().split())
  if c[0]=='1':
    rev=not rev
  elif c[1]=='1' and rev:
    S.appendleft(c[2])
  elif c[1]=='2' and not rev:
    S.appendleft(c[2])
  else:
    S.append(c[2])
if not rev:
  S.reverse()
ans=''.join(S)
print(ans)

Submission status: Wrong Answer

Solution code:
from collections import deque
S=deque(input())
q=int(input())
rev=False
for i in range(q):
  c=list(input().split())
  if c[0]=='1':
    rev=not rev
  elif c[1]=='1' and rev:
    S.appendleft(c[2])
  elif c[1]=='2' and not rev:
    S.appendleft(c[2])
  else:
    S.append(c[2])
if rev:
  S.reverse()
ans=''.join(S)
print(ans)

Submission status: Wrong Answer

Solution code:
from collections import deque
S=deque(input())
q=int(input())
for i in range(q):
  c=list(input().split())
  print(c)
  if c[0]=='1':
    S.reverse()
  elif c[0]=='2':
    if c[1]=='1':
      S.appendleft(c[2])
    elif c[1]=='2':
      S.append(c[2])
ans=''.join(S)
print(ans)

Submission status: Wrong Answer

Solution code:
S = input()
T = ''
Q = int(input())
flag = True
for i in range(Q):
    que = input()
    if len(que) == 1:
        if flag:
            flag = False
        else:
            flag = True
    else:
        if que[2] == '1':
            if flag:
                S = S+que[4]
            else:
                T = T+que[4]
        else:
            if flag:
                T = T+que[4]
            else:
                S = S+que[4]
if flag:
    U = ''
    for i in range(len(S)):
        U += S[len(S)-i-1]
    print(U+T)
else:
    U = ''
    for i in range(len(T)):
        U += T[len(T)-i-1]
    print(U+S)

Submission status: Wrong Answer

Solution code:
S=str(input())
S_list=list(S)

Q=int(input())

q=[]
right=[]
left=[]

rev=1

for i in range(Q):
    q.append(input().split())
    if q[i][0]=="1":
        rev=rev*(-1)
    elif q[i][0]=="2":
        if q[i][1]=="1":
            if rev==1:
                right.insert(0,q[i][2])
            else:
                left.append(q[i][2])
        elif q[i][1]=="2":
            if rev==1:
                left.append(q[i][2])
            else:
                right.insert(0,q[i][2])
print(right,left)

if rev==1:
    S_list=right+S_list+left
else:
    S_list=right+S_list+left
    S_list.reverse()

S=''.join(S_list)
print(S)

Submission status: Wrong Answer

Solution code:
S = input()
Q = int(input())
head = True # True: left side is head
for i in range(Q):
  Query_i = list(map(str, input().split()))
  if Query_i[0]=='1':
    head = not head
  elif Query_i[1] == '1' and head:
    S = Query_i[2] + S
  elif Query_i[1] == '1' and not head:
    S = S + Query_i[2]
  elif Query_i[1] == '2' and head:
    S = S + Query_i[2]
  else: # Query_i[1] == '2' and not head:
    S = Query_i[2] + S
  if not head:
    S = S[::-1]
print(S)

Submission status: Wrong Answer

Solution code:
s = input()
q = int(input())
rev = 1
fo = ""
for _ in range(q):
  ope = input().split()
  if ope[0] == "1":
    #s = s[::-1]
    rev *= -1
  else:
    if (ope[1]=="1") ^ rev==1:
      s = s+ope[2]
    else:
      s = ope[2]+s
print(s[::rev])

Submission status: Wrong Answer

Solution code:
s=input()
n=int(input())
front=""
back=""
reverse=False

for i in range(n):
  t=input().split()
  if t[0]=="1":
    reverse=not reverse
  else:
    if (t[1]=="1")^reverse:
      front=front+t[2]
    else:
      back=back+t[2]
if reverse:
  print(back[::-1]+s+front)
else:
  print(front[::-1]+s+back)

Submission status: Wrong Answer

Solution code:
s=input()
n=int(input())
front=""
back=""
reverse=False

for i in range(n):
  t=input().split()
  if t==["1"]:
    reverse=not reverse
  else:
    if (t[1]=="1")^reverse:
      front=front+t[2]
    else:
      back=back+t[2]
if reverse:
  print(back[::-1]+s+front)
else:
  print(front[::-1]+s+back)

Submission status: Wrong Answer

Solution code:
s=input()
n=int(input())
front=""
end=""
reverse=False

for i in range(n):
  t=input().split()
  if t==["1"]:
    reverse=not reverse
  else:
    if (t[1]=="1")^reverse:
      front=front+t[2]
    else:
      end=end+t[2]
if reverse:
  print(end[::-1]+s+front)
else:
  print(front[::-1]+s+end)

Submission status: Wrong Answer

Solution code:
S = input()
l1 = []
l2 = []
rev = False
for _ in range(int(input())):
    l = [s for s in input().split()]
    if l[0] == "1":
        rev = not rev
    elif l[1] == "1":
        if rev:
            l2.append(l[2])
        else:
            l1.append(l[2])
    else:
        if rev:
            l1.append(l[2])
        else:
            l2.append(l[2])

if rev:
    ret = "".join(l2) + S + "".join(l1)
else:
    ret = "".join(l1)[::-1] + S + "".join(l2)

print(ret)

Submission status: Wrong Answer

Solution code:
S = input()
Q = int(input())
reverse = False
front = ""
end = ""
 
for i in range(Q):
    inp = input().split()
    if inp[0] == "1":
        reverse = ~reverse
    else:
        if (inp[1] == "1") ^ reverse:
            front = front+inp[2]
        else:
            end = end+inp[2]
 
if reverse:
    print(end[::-1]+S[::-1]+front)
else:
    print(front[::-1]+S+end)

Submission status: Wrong Answer

Solution code:
S = input()
Q = int(input())
reverse = False
front = ""
end = ""
 
for i in range(Q):
    inp = input()
    if inp[0] == "1":
        reverse = ~reverse
    else:
        _, F, C = inp.split()
        if (F == "1") ^ reverse:
            front = front+C
        else:
            end = end+C
 
if reverse:
    print(end[::-1]+S[::-1]+front)
else:
    print(front[::-1]+S+end)

Submission status: Wrong Answer

Solution code:
S = input()
Q = int(input())
 
A = ""
B = ""
count = 0

Que = []
ope = []

def rev(s):
    re = ""
    for i in range(len(s)):
        re = re + s[len(s) - 1 - i]
    return s
 
for i in range(Q):
    k = input()
    if k == "1":
        count += 1
        Que.append("1")
        ope.append("0")
    else:
        Que.append("2")
        ope.append([k[2],k[4]])
 
cou = 0
 
if count % 2 == 0:
    for i in range(Q):
        if Que[i] == "1":
            cou += 1
        else:
            if (int(ope[i][0]) + cou) % 2 == 1:
                A = ope[i][1] + A
            else:
                B = B + ope[i][1]
else:
    for i in range(Q):
        if Que[i] == "1":
            cou += 1
        else:
            if (int(ope[i][0]) + cou) % 2 == 1:
                A = A + ope[i][1]
            else:
                B = ope[i][1] + B
 
if count % 2 == 0:
    S = A + S + B
else:
    S = rev(S)
    S = B + S + A
 
print(S)

Submission status: Wrong Answer

Solution code:
a = input()
Q = int(input())
Qy = [list(map(str,input().split())) for i in range(Q)]
cnt = 0
han = 0

for i in range(Q):
    if Qy[i][0] == "1":
        cnt += 1
        han = 1
    elif Qy[i][1] == "1":
        if han == 1:
            a += Qy[i][2]
            han = 0
        else:
            a = Qy[i][2] + a
    else :
        if han == 1:
            a = Qy[i][2] + a
            han = 0
        else:
            a += Qy[i][2]

if cnt%2 == 1:
    a = a[::-1]

print(a)

Submission status: Wrong Answer

Solution code:
S = input()
f = False
for _ in range(int(input())):
    l = input().split()
    if len(l) == 1:
        if f:
            f = False
        else:
            f = True
    else:
        if (l[1] == "1" and f == True) or (l[1] == "2" and f == False):
            S = l[2] + S
        else:
            S = S + l[2]

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

Submission status: Wrong Answer

Solution code:
S = input()
for _ in range(int(input())):
    l = input().split()
    print(l)
    if len(l) == 1:
        S = S[::-1]
    else:
        if l[1] == "1":
            S = l[2] + S
        else:
            S = S + l[2]
print(S)

Submission status: Wrong Answer

Solution code:
s = input()
q = int(input())
t = []
 
def F(f,fr,ba,c):
  if f == '1':
     return c + fr, ba
  if f == '2':
    return fr, ba + c
 
def R(r):
  if r == '1':return '2'
  if r == '2':return '1'

fr = ''
ba = ''
r = '1'
for n in range(q):
  t = input().split()
  if t[0] == '1':
    r = R(r)
  else:
    if r =='2':
      t[1] = R(t[1])
    fr,ba = F(t[1], fr, ba,t[2])
 
    
if r == '1': 
   print(fr+s+ba)
else:
  print(fr+s+ba[::-1])

Submission status: Wrong Answer

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

List = [list(map(str, input().split())) for i in range(Q)]

for i in range(Q):
    if List[i][0] == "1":
        S.reverse()
    else:
        if List[i][1] == "1":
            S.append(List[i][2])
        else:
            S.insert(0, List[i][2])

S.reverse()

print("".join(list(S)))

Submission status: Wrong Answer

Solution code:

s = input()
q = int(input())
t = []

def F(f,fr,ba,c):
  if f == '1':
     return [c]+fr, ba
  if f == '2':
    return fr, ba+[c]

def R(r):
  if r == '1':return '2'
  if r == '2':return '1'

fr = []
ba = []
r = '1'
for n in range(q):
  t = input().split()
  if t[0] == '1':
    r = R(r)
  else:
    if r =='2':
      t[1] = R(t[1])
    fr,ba = F(t[1],fr,ba,t[2])


a = ''
ans = fr + [s] + ba
for n in ans:
  if r == '1': 
    a = a + n
  else:
    a = n + a
print(a)

Submission status: Wrong Answer

Solution code:
from sys import stdin
from collections import deque

d = deque()

S = stdin.readline().rstrip().split()
Q = [int(x) for x in stdin.readline().rstrip().split()]

d.append(S[0])
# print(d)
flag = 0b0

for i in range(Q[0]):
    s = input().split()

    if len(s) == 1:
        # print(len(d[0]))
        if len(d[0]) != 1:
            flag = ~flag
            # print(flag)
        # flag = ~flag
        # S=S[::-1]
        # d.reverse()

    else:
        if int(s[1]) == 1:
            if flag == 0:
                d.appendleft(s[2])
            else:
                s[2] = s[2][::-1]
                # print(s[2])
                d.append(s[2])
                # print(d)
            # d.appendleft(s[2])
            # S.insert(0,s[2])

        else:
            if flag == 0:
                d.append(s[2])
            else:
                s[2] = s[2][::-1]
                # print(s[2])
                d.appendleft(s[2])
            # d.append(s[2])
            # S.append(s[2])
# print(flag)
ans = "".join(d)
# print(ans)
if flag == -1:
    ans = ans[::-1]
    # d.reverse()
    # print(d)

# print("".join(d))
print(ans)

Submission status: Wrong Answer

Solution code:
from sys import stdin
from collections import deque

d = deque()

S = stdin.readline().rstrip().split()
Q = [int(x) for x in stdin.readline().rstrip().split()]

d.append(S[0])
# print(d)
flag = 0b0

for i in range(Q[0]):
    s = input().split()

    if len(s) == 1:
        if len(d) != 1:
            flag = ~flag
        # flag = ~flag
        # S=S[::-1]
        # d.reverse()

    else:
        if int(s[1]) == 1:
            if flag == 0:
                d.appendleft(s[2])
            else:
                d.append(s[2])
            # d.appendleft(s[2])
            # S.insert(0,s[2])

        else:
            if flag == 0:
                d.append(s[2])
            else:
                d.appendleft(s[2])
            # d.append(s[2])
            # S.append(s[2])
if flag == -1:
    d.reverse()

print("".join(d))

Submission status: Wrong Answer

Solution code:
from collections import deque

s = input()
q = int(input())
query = [input().split() for i in range(q)]

d = deque(['S'])

#d = ['S']

rev = False

for i in range(q):
    if query[i][0] == '1':
        rev = not rev
        continue
    
    if not rev:
        if query[i][0] == '2':
            if query[i][1] == '1':
                d.appendleft(query[i][2])
            else:
                d.append(query[i][2])
    else:
        if query[i][0] == '2':
            if query[i][1] == '1':
                d.append(query[i][2])
            else:
                d.appendleft(query[i][2])

#ans = deque()
"""                
print(d)
if rev:
    d = ''.join(list(reversed(d)))
    #d = d[::-1]
"""
while len(d):
    if rev:
        tmp = d.pop()
    else:
        tmp = d.popleft()
    if tmp == 'S':
        #ans.append(s)
        print(s,end='')
    else:
        print(tmp,end='')
        #ans.append(tmp)

#print(*ans,sep='')

Submission status: Wrong Answer

Solution code:
from sys import stdin
from collections import deque

d = deque()

S = stdin.readline().rstrip().split()
Q = [int(x) for x in stdin.readline().rstrip().split()]

d.append(S[0])
# print(d)
flag = 0b0

for i in range(Q[0]):
    s = input().split()

    if len(s) == 1:
        flag = ~flag

        # S=S[::-1]
        # d.reverse()

    else:
        if int(s[1]) == 1:
            if flag == 0:
                d.appendleft(s[2])
            else:
                d.append(s[2])
            # d.appendleft(s[2])
            # S.insert(0,s[2])

        else:
            if flag == 0:
                d.append(s[2])
            else:
                d.appendleft(s[2])
            # d.append(s[2])
            # S.append(s[2])
if flag == -1:
    d.reverse()

print("".join(d))

Submission status: Wrong Answer

Solution code:
if __name__ == '__main__':
    s = input()
    q = int(input())
    ans = s
    rev_count = 0
    leftList = []
    rightList =[]
    alist =[]

    for i in range(q):
        a = [i for i in input().split()]
        alist.append(a)
        if len(a) == 1:
            rev_count = rev_count +1

    if rev_count % 2 ==1:
        ans = ans[::-1]

    for i in alist:
        if len(i) == 1:
            rev_count = rev_count -1
        else:
            if int(i[1]) == 1:
                if rev_count % 2 == 1:
                    rightList.append(i[2])
                else:
                    leftList.append(i[2])

            else:
                if rev_count % 2 == 1:
                    leftList.append(i[2])
                else:
                    rightList.append(i[2])

    ans = ''.join(leftList)+ ans+''.join(rightList)

    print(ans)