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:
from collections import deque
s = input()
q = int(input())
ans = deque(s)
rev = 0
for i in range(q):
    qi = input()
    if qi[0] == '1':
        rev += 1
    else:
        parity = rev % 2
        if (int(qi[2]) + parity) % 2:
            ans.appendleft(qi[4])
        else:
            ans.append(qi[4])

if parity:
    ans.reverse()
for i in ans:
    print(i, end='')

Submission status: Wrong Answer

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


if parity:
    ans.reverse()
for i in ans:
    print(i, end='')

Submission status: Wrong Answer

Solution code:
from collections import deque

s = deque(list(input()))
n = int(input())
f = False

for i in range(n):
    a = input()
    if a == "1":
        f = not f
    else:
        b,c,d = a.split()
        if f:
            if c == "1":
                s.appendleft(d)
            else:
                s.append(d)
        else:
            if c == "1":
                s.append(d)
            else:
                s.appendleft(d)

if not f:
    s.reverse()

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

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
 
S = list(input())
Q = int(input())
is_forward = True
 
for i in range(Q):
    query = input()
 
    if query == '1':
        is_forward = not is_forward
    else:
        T, F, C = query.split()
        if is_forward:
            if F == '1':
                S.insert(0, C)
            else:
                S.append(C)
        else:
            if F == '1':
                S.append(C)
            else:
                S.insert(0, C)
 
print(''.join(S))

Submission status: Wrong Answer

Solution code:
S=input()
Q=int(input())
left=[]
right=[]
rev=0

for _ in range(Q):
    q=list(input().split())
    if int(q[0])==1:
        rev+=1
    else:
        if (int(q[1])+rev)%2==1:
            left.append(q[2])
            rev=0
        else:
            right.append(q[2])
            rev=0

S=left[::-1]+list(S)+right
if  rev % 2==0:
    print(*S[::-1],sep="")            
else:
    print(*S,sep="")

Submission status: Wrong Answer

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

fflag = 0
ato = ""
mae = ""
for i in range(q):
  qi = input()
  if( qi == "1"):
    fflag += 1 
  else:
    if( fflag%2 == 0 ):
      if( qi[2] == "2" ):
        ato += qi[4]
      else:
        mae += qi[4]
    else:
      if( qi[2] == "2" ):
        mae += qi[4]
      else:
        ato += qi[4]      
    
a = mae[::-1] + a + ato    
    
if( fflag%2==0 ):
  print( a )

Submission status: Wrong Answer

Solution code:
# coding: utf-8
S = input()
Q = int(input())
cnt = 0
for i in range(Q):
    q = input().split()
    if q[0] == "1":
        # S = S[::-1]
        # front, rear = rear, front
        cnt += 1
    else:
        if q[1] == "1":
            if cnt % 2 == 1:
                S = q[2] + S[::-1]
            else:
                S += q[2]
        else:
            if cnt % 2 == 1:
                S += q[2]
            else:
                S = S[::-1] + q[2]
print(S[::-1] if cnt % 2 == 0 else S)

Submission status: Wrong Answer

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

ct = 1

for i in range(n):
    p = input().split()

    if p[0] == '1':
        ct = -ct 
    else:
        if ct == -1:
            if p[1] == "1":
                s = s + p[2]
            else:
                s = p[2] + s
        else:
            if p[1] == "1":
                s = p[2] + s
            else:
                s = s + p[2]

print(s)

Submission status: Wrong Answer

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

rev=0
for _ in range(Q):
    q=list(input().split())
    if int(q[0])==1:
        rev+=1
    else:
        if (int(q[1])+rev)%2==1:
            S=q[2]+S
            rev=0
        else:
            S=S+q[2]
            rev=0

if (rev+1) %2==1:
    print(S[::-1],sep="")
else:
    print(S,sep="")

Submission status: Wrong Answer

Solution code:
s = input()
q = int(input())
inv = False
left =''
right = ''

for i in range(1, q+1):
    try:
        t, f, c = input().split()
    except ValueError:
        t = '1'
    if t == '1':
        inv = not inv
    else:
        if inv:
            if f == '1':
                left = c + left
            else:
                right = right + c
        else:
            if f == '1':
                right = right + c
            else:
                left = c + left
#     print('%s, %s, %s, %s, %s' %(left, right, inv, t, f))

# print(left)
# print(right)
if inv:
    print(left + s + right)
else:
    right = right[::-1]
    left = left[::-1]
    s = s[::-1]
    print(right + s + left)

Submission status: Wrong Answer

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

S = input()
L = []
R = []
rev = 0
for _ in range(int(input())):
    A = input().split()
    if A[0] == "1":
        rev ^= 1
    else:
        if (int(A[1]) ^ rev) & 1:
            L.append(A[2])
        else:
            R.append(A[2])

if rev:
    print("".join(R[::-1]) + S[::-1] + "".join(L))
else:
    print("".join(L[::-1]) + S + "".join(R))

Submission status: Wrong Answer

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

ct = 0

for i in range(n):
    p = input().split()

    if p[0] == '1':
        ct += 1
    else:
        if p[1] == "2":
            ct += 1
        
        if ct % 2 :
            s = s + p[2]
        else:
            s = p[2] + s
        ct = 0

if ct % 2 :
    s = s[::-1]
    
print(s)

Submission status: Wrong Answer

Solution code:
s = input()
q = int(input())
x = 1
f, b = '', ''

for i in range(q):
    a = input().split()
    if a[0] == '1':
        x = -x
    elif (1.5 - int(a[1])) * x > 0:
        f = a[2] + f
    else:
        b = b + a[2]
        s = f + s + b
    if x == -1:
        s = s[::-1]

print(s)

Submission status: Wrong Answer

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

ct = 0

for i in range(n):
    p = input().split()

    if p[0] == '1':
        ct += 1
    else:
        if p[1] == "2":
            ct += 1
        
        if ct % 2 :
            s = s + p[2]
        else:
            s = p[2] + s
        ct = 0


print(s[::-1])

Submission status: Wrong Answer

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

for i in range(Q):
    X = list(input().split(" "))
    if int(X[0]) == 1:
        rev = True

    elif int(X[0]) == 2 and int(X[1]) == 1:
        if rev is False:
            S.insert(0, X[2])
        elif rev is True:
            S.append(X[2])
            rev = False

    elif int(X[0]) == 2 and int(X[1]) == 2:
        if rev is False:
            S.append(X[2])
        elif rev is True:
            S.insert(0, X[2])
            rev = False

if rev is True:
    S.reverse()

print(*S, sep="")

Submission status: Wrong Answer

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

ct = 0

for i in range(n):
    p = input().split()

    if p[0] == '1':
        ct += 1
    else:
        if p[1] == "2":
            ct += 1
        
        if ct % 2 :
            s = s + p[2]
        else:
            s = p[2] + s
        ct = 0

print(s)

Submission status: Wrong Answer

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

ct = 0

for i in range(n):
    p = input().split()

    if p[0] == '1':
        ct += 1
    else:
        if p[1] == "2":
            ct += 1
        
        if ct % 2 :
            s = s + p[2]
        else:
            s = p[2] + s

print(s)

Submission status: Wrong Answer

Solution code:
import sys
s = input()
q = int(input())
a = ['', '']
r = 0
for l in sys.stdin:
    if l[0] == '1':
        r += 1
    else:
        a[0 if l[2] == '21'[r % 2] else 1] += l[4]
if r % 2 == 0:
    print(''.join(reversed(a[1])) + ''.join(reversed(s)) + ''.join(a[0]))
else:
    print(''.join(reversed(a[0])) + s + ''.join(a[1]))

Submission status: Wrong Answer

Solution code:
S = input()
Q = int(input())
flag = False
cnt = 0
for i in range(Q):
  a = list(input().split())
  if len(a)==1:
    flag = True
    cnt += 1
  else:
    if int(a[1]) == 1 and not flag:
      S = a[2]+S
    elif int(a[1]) == 1 and flag:
      S = S+a[2]
      flag = False
    elif int(a[1]) == 2 and not flag:
      S = S+a[2]
    elif int(a[1]) == 2 and flag:
      S = a[2]+S
      flag = False
if flag and cnt%2==0:
  print(S)
elif flag and cnt%2!=0:
  print(S[::-1])
else:
  print(S)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
S = str(input())
Q = int(input())

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

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

Submission status: Wrong Answer

Solution code:
# coding: utf-8
S = input()
Q = int(input())
front = ""
rear = ""
cnt = 0
for i in range(Q):
    q = input().split()
    if q[0] == "1":
        S = S[::-1]
        front, rear = rear, front
    else:
        if q[1] == "1":
            front += q[2]
        else:
            rear += q[2]
print(front + S + rear)

Submission status: Wrong Answer

Solution code:
from sys import stdin,stdout
s=stdin.readline()
ss=s
q=int(stdin.readline())
c=0
for i in range(q):
    a=[str(x) for x in stdin.readline().split()]
    if len(a)==1:
        c+=1
    else:
        if a[1]=="1":
            if c%2==0:
                ss=a[2]+ss 
            else:
                ss+=a[2]
        else:
            if c%2!=0:
                ss=a[2]+ss 
            else:
                ss+=a[2] 
if c%2==0:
    stdout.write(ss)
else:
    stdout.write(ss[::-1])

Submission status: Wrong Answer

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

front = []
back = []
is_reverse = False

for query in query_list:
    if query[0] == '1':
        is_reverse = not is_reverse
    elif query[0] == '2':
        key = int(query[1])
        value = query[2]
        if (key == 1 and not is_reverse) or (key == 2 and is_reverse):
            front.append(value)
        else:
            back.append(value)

ans = ''
if is_reverse:
    ans = back[::-1]+[s][::-1]+front
else:
    ans = front[::-1]+[s]+back
print(''.join(ans))

Submission status: Wrong Answer

Solution code:
import sys
S = [i for i in input()]
Q = int(input())
head = 0

class Node:  
    def __init__(self, x, y = None, z = None):  
        self.data = x  
        self.prev = y  
        self.next = z  
    def set_next(self, x): self.next = x  
    def set_prev(self, x): self.prev = x  

class LinkedList:  
    def __init__(self):  
        self.top = Node("")  
        self.top.set_next(self.top)  
        self.top.set_prev(self.top)  
    def insert(self, x): 
        a = Node(x) 
        self.top.next.set_prev(a)  
        a.set_next(self.top.next)  
        self.top.set_next(a)  
        a.set_prev(self.top) 
    def append(self, x): 
        a = Node(x) 
        self.top.prev.set_next(a)  
        a.set_prev(self.top.prev)  
        self.top.set_prev(a)  
        a.set_next(self.top) 

def output(L):
  R = []
  a = L.top
  while a.next != L.top:
    a = a.next
    R.append(a.data)
  R = "".join(R)
  return R

def output_rev(L):
  R = []
  a = L.top
  while a.prev != L.top:
    a = a.prev
    R.append(a.data)
  R = "".join(R)
  return R

L = LinkedList()

for i in S:
  L.insert(i)

for q in sys.stdin:
  q = q.strip("\n")
  if q == "1":
    head += 1
  elif head % 2 ==0:
    if q[2] == "1":
      L.insert(q[4])
    else:
      L.append(q[4])
  elif head % 2 ==1:
    if q[2] == "1":
      L.append(q[4])
    else:
      L.insert(q[4])

if head % 2 == 1:
  print(output_rev(L))
else:
  print(output(L))

Submission status: Wrong Answer

Solution code:
from collections import deque

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

sArry = deque(S.split())
rFlag = False
for i in range(Q):
    if Query[i][0] == '1':
        rFlag = (rFlag == False)
        
    else:
        if rFlag:
            if Query[i][1] == '1':
                sArry.append(Query[i][2])
            else:
                sArry.appendleft(Query[i][2])
        else:
            if Query[i][1] == '1':
                sArry.appendleft(Query[i][2])
            else:
                sArry.append(Query[i][2])
            
#    print("  ", i, rFlag, "".join(sArry), Query[i])


if rFlag:
    sArry.reverse()
print("".join(sArry))

Submission status: Wrong Answer

Solution code:
from collections import deque
s=input()
q=int(input())
ans_str=deque([s])
cnt=0
for _ in range(q):
    Q=list(input().split())
    if Q[0]=='1':
        cnt=1 if cnt==0 else 0

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

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

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

Submission status: Wrong Answer

Solution code:
import numpy as np
S=input()
Q=int(input())
q=[[0 for i in range(3)] for j in range(Q)]
onecntr=0
for i in range(Q):
    tmp=input().split()
    if(tmp[0]=="1"):
        onecntr+=1
    else:
        if(onecntr%2==0):
            if(tmp[1]==1):
                S=tmp[2]+S
            else:
                S=S+tmp[2]
        else:
            if(tmp[1]==2):
                S=tmp[2]+S
            else:
                S=S+tmp[2]
    
    
if(onecntr%2==1):
    S=S[::-1]

print(S)

Submission status: Wrong Answer

Solution code:
import numpy as np
S=input()
Q=int(input())
q=[[0 for i in range(3)] for j in range(Q)]
onecntr=0
for i in range(Q):
    tmp=input().split()
    if(tmp[0]=="1"):
        q[i][0]=1
    else:
        q[i][0]=2
        q[i][1]=int(tmp[1])
        q[i][2]=tmp[2]
    

for i in range(Q):
    if(q[i][0]==1):
        onecntr+=1
    else:
        
        if(onecntr%2==0):
            if(q[i][1]==1):
                S=q[i][2]+S
            else:
                S=S+q[i][2]
        else:
            if(q[i][1]==2):
                S=q[i][2]+S
            else:
                S=S+q[i][2]
        onecntr=0
    
if(onecntr%2==1):
    S=S[::-1]

print(S)

Submission status: Wrong Answer

Solution code:
from collections import deque

s = list(input())
d = deque(s)
forward = True
q = int(input())
for i in range(q):
    l = input().split()
    if len(l) == 1:
        forward = not forward
    else:
        pos = int(l[1])
        c = l[2]
        if pos == 1:
            if forward:
                d.appendleft(c)
            else:
                d.append(c)
        else:
            if forward:
                d.append(c)
            else:
                d.appendleft(c)
print("".join(s))

Submission status: Wrong Answer

Solution code:
from collections import deque
s = input()
q = int(input())
query = []
ans = [s]
for i in range(q):
    l = list(input().split())
    query.append(l)
for i in range(q):
    if len(query[i]) == 3:
        if query[i][1] == "1":
            ans.insert(0,query[i][2])
        else:
            ans.append(query[i][2])
    else:
        for j in range(len(ans)//2):
            temp = ans[len(ans)-j-1]
            ans[len(ans)-j-1] = ans[j]
            ans[j] = temp
d = ""
for i in range(len(ans)):
    d += ans[i]
print(d)

Submission status: Wrong Answer

Solution code:
from collections import deque

def resolve():
    s = input()
    s = deque(s)
    q = int(input())
    for i in range(q):
        list = input().split()
        t = list[0]
        if t == "1":
            if len(s) > 1:
                l, r = s.popleft(), s.pop()
                s.appendleft(r)
                s.append(l)
        elif list[1] == "1":
            s.appendleft(list[2])
        else:
            s.append(list[2])
    s = ''.join(s)
    print(s)

if __name__ == '__main__':
    resolve()

Submission status: Wrong Answer

Solution code:
from collections import deque


def resolve():
    s = input()
    s = deque(s)
    q = int(input())
    for i in range(q):
        list = input().split()
        t = list[0]
        if t == "1":
            if len(s) > 1:
                l, r = s.popleft(), s.pop()
                s.appendleft(r)
                s.append(l)
        elif list[1] == "1":
            s.appendleft(list[2])
        else:
            s.append(list[2])
    s = ''.join(s)
    print(s)

Submission status: Wrong Answer

Solution code:
from collections import deque
s=input()
q=int(input())
ans_str=deque([s])
cnt=0
for _ in range(q):
    Q=list(input().split())
    if Q[0]=='1':
        cnt=1 if cnt==0 else 0

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

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

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

Submission status: Wrong Answer

Solution code:
def main():
    from collections import deque
    S = deque(input())
    Q = int(input())

    q_list = [input().split() for _ in range(Q)]

    flag = False
    for q in q_list:
        if len(q) == 1:
            t = q[0]
        else:
            t, f, c = q

        if t == '1':
            flag = not flag
        else:
            if (f == '1') ^ flag:
                S.appendleft(c)
            else:
                S.append(c)
    if flag:
        print("".join(S))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
turn = 0
alpha = []
beta = []
s = [input()]
q = int(input())
for _ in range(q):
  pre = input().split()
  if len(pre) == 3:
    t, f, c = list(map(int, pre[0:2])) + [pre[2]]
  elif len(pre) == 1:
    t = int(pre[0])
  if t == 1:
    turn += 1
  else:
    if f ==1:
      if turn % 2 == 0:
        alpha.append(c)
      else:
        beta.append(c)
    else:
      if turn % 2 == 0:
        beta.append(c)
      else:
        alpha.append(c)
if turn % 2 == 0:
  alpha.reverse()
  print( ''.join(alpha) +  ''.join(s) +  ''.join(beta))

else:
  s.reverse()
  beta.reverse()
  print( ''.join(beta) +  ''.join(s) +  ''.join(alpha))

Submission status: Wrong Answer

Solution code:
#D
s=input()
q=int(input())
l=[]
left=[]
right=[]
ans=[s]
for i in range(q):
    l.append(list(input().split()))
    if int(l[i][0])==1:
        left,right=right,left
    elif int(l[i][1])==1:#2 1
        left.append(l[i][2])
    else:#2 2
        right.append(l[i][2])
ans.extend(right)
ans.reverse()
ans.extend(left)
ans.reverse()
for i in ans:
    print(i,end="")

Submission status: Wrong Answer

Solution code:
from collections import deque
def resolve():
    s = input()
    s = deque(s)
    q = int(input())
    for i in range(q):
        list = input().split()
        t = list[0]
        if t == "1":
            if len(s) > 1:
                l, r = s.popleft(), s. pop()
                s.appendleft(r)
                s.append(l)
        else:
            f, c = list[1], list[2]
            if f == "1":
                s.appendleft(c)
            else:
                s.append(c)
    s = ''.join(s)
    print(s)

if __name__ == '__main__':
    resolve()

Submission status: Wrong Answer

Solution code:
turn = 0
alpha = []
beta = []
s = [input()]
q = int(input())
for _ in range(q):
  pre = input().split()
  print(pre)
  if len(pre) == 3:
    t, f, c = list(map(int, pre[0:2])) + [pre[2]]
  elif len(pre) == 1:
    t = int(pre[0])
  if t == 1:
    turn += 1
  else:
    if f ==1:
      if turn % 2 == 0:
        alpha.append(c)
      else:
        beta.append(c)
    else:
      if turn % 2 == 0:
        beta.append(c)
      else:
        alpha.append(c)
if turn % 2 == 0:
  alpha.reverse()
  print( ''.join(alpha) +  ''.join(s) +  ''.join(beta))

else:
  s.reverse()
  beta.reverse()
  print( ''.join(beta) +  ''.join(s) +  ''.join(alpha))

Submission status: Wrong Answer

Solution code:
import sys
import os


def main():
    if os.getenv("LOCAL"):
        sys.stdin = open("input.txt", "r")

    ret = sys.stdin.readline().rstrip()
    Q = int(sys.stdin.readline().rstrip())
    m = ""
    u = ""
    r = 0
    for q in range(Q):
        line = sys.stdin.readline().split()
        if line[0] == '1':
            m, u = u, m
            r == 0 if r == 1 else r == 1
        elif line[0] == '2' and line[1] == '1':
            m += line[2]
        else:
            u += line[2]

    if r == 0:
        print(m[::-1] + ret[::1] + u)
    else:
        print(m[::-1] + ret[::-1] + u)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
S=input()
Q=int(input())
list=[]
mojiretu=[z for z in S]
for a in range(Q):
    b=input().split()
    list.append(b)

number=0

left=[]
right=[]

for c in list:
    if len(c)==1:
        number+=1
    else:
        if number%2==0:
            if c[1]=='1':
                left.insert(0,c[2])
            else:
                right.append(c[2])
        else:
            if c[1]=='1':
                right.append(c[2])
            else:
                left.insert(0,c[2])

if number%2==0:
    print(''.join(left) + ''.join(mojiretu) + ''.join(right))
else:
    mojiretu.reverse()
    z=left+mojiretu+right
    z.reverse()
    print(''.join(z))

Submission status: Wrong Answer

Solution code:
from collections import deque
s=input()
q=int(input())
sd=deque()
sd.append(s)
#print(sd)
for i in range(q):
    que=list(map(str,input().split()))
    if int(que[0])==1:
        sd.reverse()
        #print(sd)
    else:
        if int(que[1])==1:
            sd.appendleft(str(que[2]))
        else:
            sd.append(str(que[2]))

ans=''.join(sd)
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import sys
import os


def main():
    if os.getenv("LOCAL"):
        sys.stdin = open("input.txt", "r")

    ret = sys.stdin.readline().rstrip()
    Q = int(sys.stdin.readline().rstrip())
    m = ""
    u = ""
    r = 0
    for q in range(Q):
        line = sys.stdin.readline().split()
        if line[0] == '1':
            m, u = u, m
            m = m[::-1]
            u = u[::-1]
            r == 0 if r == 1 else r == 1
        elif line[0] == '2' and line[1] == '1':
            m += line[2]
        else:
            u += line[2]

    if r == 0:
        print(m + ret[::1] + u)
    else:
        print(m + ret[::-1] + u)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

front = []
back = []
is_reverse = False

for query in query_list:
    if query[0] == '1':
        is_reverse = not is_reverse
    elif query[0] == '2':
        key = int(query[1])
        value = query[2]
        if (key == 1 and not is_reverse) or (key == 2 and is_reverse):
            front.append(value)
        else:
            back.append(value)

ans = ''
if is_reverse:
    ans = back[::-1]+[s]+front
else:
    ans = front[::-1]+[s]+back
print(''.join(ans))

Submission status: Wrong Answer

Solution code:
#D
s=input()
q=int(input())
l=[]
ans=[s]
for i in range(q):
    l.append(list(input().split()))
for i in range(q):
    if int(l[i][0])==1:
        ans.reverse()
    elif int(l[i][1])==1:#2 1
        ans.reverse()
        ans.append(l[i][2])
        ans.reverse()
    else:#2 2
        ans.append(l[i][2])
for i in ans:
    print(i,end="")

Submission status: Wrong Answer

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

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

R = first + S + last

print(''.join(R))

Submission status: Wrong Answer

Solution code:
s = list(input())
q = int(input())
for i in range(q):
    l = input().split()
    if len(l) == 1:
        s[0], s[-1] = s[-1], s[0]
    else:
        pos = int(l[1])
        c = l[2]
        if pos == 1:
            s.insert(1, c)
        else:
            s.append(c)
print("".join(s))

Submission status: Wrong Answer

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

front = []
back = []

for query in query_list:
    if query[0] == '1':
        front, back = back, front[::-1]
    elif query[0] == '2':
        key = int(query[1])
        value = query[2]
        if key == 1:
            front.append(value)
        else:
            back.append(value)
print(''.join(front[::-1]+[s]+back))

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
S = input()
Q = int(input())

reverse = 0
head = []
tail = []
for i in range(Q):
    query = input()
    if len(query) == 1:
        reverse = (reverse + 1) % 2

    else:
        t, F, C = query.split()
        if reverse == 0:
            if F == "1":
                head.append(C)
            else:
                tail.append(C)
        else:
            if F == "1":
                tail.append(C)
            else:
                head.append(C)

if reverse == 0:
    S = ''.join(head[::-1] + list(S[::-1]) + tail)
else:
    S = ''.join(tail[::-1] + list(S) + head)


print(S)

Submission status: Wrong Answer

Solution code:
S = input().split()
Q = int(input())
switch = 1
for i in range(Q):
    a =input().split()
    if int(a[0])==1:
        switch *=-1
    else:
        if (int(a[1])==1 and switch ==1) or (int(a[1])==2 and switch ==-1):
            S.insert(0,a[2])
        else:
            S.append(a[2])
if switch ==1:
    S = ''.join(S)
    print(S)
else:
    S.reverse()
    S = ''.join(S)
    print(S)

Submission status: Wrong Answer

Solution code:
s = input()
nq = int(input())
flg = 0
front = []
back = []
for i in range(nq):
    q = input()
    if len(q) == 1:
        #flg += 1
        flg = (flg+1)%2
    else:
        if flg == 0:
            if q[2] == '1':
                #front = [q[-1]] + front
                front.append(q[-1])
            else:
                #back = back + [q[-1]]
                back.append(q[-1])
        else:
            if q[2] == '1':
                #back = back + [q[-1]]
                back.append(q[-1])
            else:
                #front = [q[-1]] + front
                front.append(q[-1])
                
s = ''.join(front) + s + ''.join(back)
if flg % 2 == 1:
    s = s[::-1]
print(s)

Submission status: Wrong Answer

Solution code:
def main2(s,x,q):
    res = s
    rev = 1 #-1なら反転中
    lstA = []
    lstB = []
    for op in q:
        if int(op[0]) == 1:
            rev = rev * -1
        elif int(op[1])*rev == 1 or int(op[1])*rev == -2:
            lstA.append(op[2])
        elif int(op[1])*rev == -1 or int(op[1])*rev == 2:
            lstB.append(op[2])
    if rev == 1:
        res = "".join(lstA[::-1])+res+"".join(lstB[::-1])
    else :
        res = "".join(lstB[::-1])+res+"".join(lstA[::-1])
    return res
 
s = input()
x = int(input())
q = [input().split() for _ in range(x)]
print(main2(s,x,q))

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3

import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(2147483647)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

s = list(input())
q = I()
front = []
back = []
cnt = 0
for _ in range(q):
    query = input().split()
    if query[0] == "1":
        cnt += 1
    else:
        if query[1] == "1":
            if cnt % 2 == 0:
                front.append(query[2])
            else:
                back.append(query[2])
        else:
            if cnt % 2 == 0:
                back.append(query[2])
            else:
                front.append(query[2])

if cnt % 2 == 0:
    print(''.join(front[::-1])+''.join(s)+''.join(back))
else:
    print(''.join(back[::-1])+''.join(s)+''.join(front))