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:
if __name__ == '__main__':
    s = input()
    q = int(input())

    pref = []
    suff = []
    rev = False
    last_rev = False

    for i in range(q):
        curr = [c for c in input().split()]
        if curr[0] == '1':
            rev = not rev
            if i == q - 1 and rev:
                last_rev = True
        else:
            if curr[1] == '1':
                if not rev:
                    pref.append(curr[2])
                else:
                    suff.append(curr[2])
            else:
                if not rev:
                    suff.append(curr[2])
                else:
                    pref.append(curr[2])

    res = "{}{}{}".format("".join(pref[::-1]), s, "".join(suff))
    if last_rev:
        print(res[::-1])
    else:
        print(res)

Submission status: Wrong Answer

Solution code:
import 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=100000)
    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()
    ans = ''
    for s in S:
        ans += s
    print(ans)
    # print(*S, sep='')
resolve()

Submission status: Wrong Answer

Solution code:
s = input().split()
ans = s
s = input().split()
q = int(s[0])
count=0
for i in range(q):
    s = input().split()
    if s[0]=='1': 
        count+=1
    else:
        if count%2==1:
            ans=ans[::-1]
        count=0
        if s[1]=='1':
            ans=list(s[2])+ans
        else:
            ans.append(s[2])
if count % 2 == 1:
    ans = ans[::-1]
print(''.join(ans))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
from collections import deque
S=input()
Q=int(input())
query=[list(map(str,input().split())) for _ in range(Q)]

ans=deque([S])
for q in query:
  if len(q)==1:
    ans.reverse()
  else:
    _,f,c=q
    if f=='1':
      ans.appendleft(c)
    else:
      ans.append(c)
print(*ans,sep='')

Submission status: Wrong Answer

Solution code:
from collections import deque

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

cur = 1
d = deque([S])

for _ in range(Q):
    xs = input().split()

    if len(xs) == 1:
        cur = 2 if cur == 1 else 1
    else:
        _, f, c = xs
        f = int(f)
        if f == cur == 1 or f == cur == 2:
            d.appendleft(c)
        else:
            d.append(c)

if cur == 1:
    ans = "".join(d)
else:
    d.reverse()
    ans = "".join(d)

print(ans)

Submission status: Wrong Answer

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

import sys
from collections import deque

DEBUG = False

def solve():
    return


def read_int_list(sep = " "):
    return [int(s) for s in sys.stdin.readline().rstrip().split(sep)]

def read_int():
    return int(sys.stdin.readline())

def read_str():
    return sys.stdin.readline().rstrip()
    
def read_str_list(sep = " "):
    return [s for s in sys.stdin.readline().rstrip().split(sep)]


def dprint(*args, **kwargs):
    if DEBUG:
        print(*args, **kwargs)
    return


def main():
    d = deque([read_str()])
    q = read_int()

    reversed_ = False
    for _ in range(0, q):
        query = read_str_list()
        if query[0] == "1":
            reversed_ = not reversed_
            continue
        # query[0] == "2"
        f, c = query[1:]
        if f == "1" or (f == "2" and reversed_):  # insert to the left
            d.appendleft(c)
        else:
            d.append(c)
    if reversed_:
        print("".join(reversed(list(d))))
    else:
        print("".join(list(d)))


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
from heapq import heappush, heappop, heapify
from collections import deque, defaultdict, Counter
import itertools
from itertools import permutations, combinations, accumulate
import sys
import bisect
import string
import math
import time


def I(): return int(input())


def S(): return input()


def MI(): return map(int, input().split())


def MS(): return map(str, input().split())


def LI(): return [int(i) for i in input().split()]


def LI_(): return [int(i)-1 for i in input().split()]


def StoI(): return [ord(i)-97 for i in input()]


def ItoS(nn): return chr(nn+97)


def input(): return sys.stdin.readline().rstrip()


def show(*inp, end='\n'):
    if show_flg:
        print(*inp, end=end)


YNL = {False: 'No', True: 'Yes'}
YNU = {False: 'NO', True: 'YES'}
MOD = 10**9+7
inf = float('inf')
IINF = 10**10
l_alp = string.ascii_lowercase
u_alp = string.ascii_uppercase
ts = time.time()
sys.setrecursionlimit(10**6)
nums = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']

show_flg = False
# show_flg = True


def main():
    st = S()
    Q = I()
    queries = []
    # print(st[0])
    # s = [st[i] for i in range(len(st))]
    # s = [None for i in range(3 * 10**5 + 2)]
    START = 10 ** 5 + (10 ** 5 // 2)
    s = [None] * (3 * START + 2)
    for i in range(START, START + len(st)):
        # print(st[START - i], START - i)
        s[i] = st[START - i]

    for i in range(Q):
        queries.append(list(MS()))

    op = True
    first = START - 1
    last = START + len(st) + 1
    # print(s)

    for i in range(Q):
        query = queries[i]
        if len(query) == 1:
            op = not op
        else:
            if (query[1] == '1' and op is True) or (query[1] == '2' and op is False):
                s[first] = query[2]
                first -= 1
                # s.insert(0, query[2])
            elif (query[1] == '1' and op is False) or (query[1] == '2' and op is True):
                s[last] = query[2]
                last += 1
                # s.append(query[2])
            else:
                exit(-1)
                return

    # print(''.join([t for t in s if t is not None]))
    # print(s)
    if op is True:
        s = [t for t in s if t is not None]
        print(''.join(s))
    else:
        s = [t for t in s if t is not None]
        print(''.join(s[::-1]))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
from collections import deque
s = deque(input())
q = int(input())
for _ in range(q):
    Q = list(map(str,input().split()))
    if int(Q[0]) == 1:
        a = s.popleft()
        b = s.pop()
        s.append(a)
        s.appendleft(b)
    else:
        if int(Q[1]) == 1:
            s.appendleft(Q[2])
        else:
            s.append(Q[2])
            
print("".join(s))

Submission status: Wrong Answer

Solution code:
S=input()
Q=int(input())
str_list_ = []
for q in range(Q):
    str_list_.append( input().split(' ') )

for q in range(Q):
    #print(q)
    #str_list = input().split(' ')
    str_list = str_list_[q]
    #print(str_list)
    if str_list[0]=='1':
        #head=S[0]
        #tail=S[len(S)-1]
        #S=S[1:]
        #S=tail+S
        #S=S[0:-1]
        #S=S+head
        #S=tail+S[1:-1]+head
        #S=S[len(S)-1]+S[1:-1]+S[0]
        S='%s%s%s'%(S[len(S)-1],S[1:-1],S[0])
        #print(S)
    elif str_list[0]=='2':
        if str_list[1]=='1':
            #S=str_list[2]+S
            S='%s%s'%(str_list[2], S)
        elif str_list[1]=='2':
            #S=S+str_list[2]
            S='%s%s'%(S, str_list[2])
        #print(S)
print(S)

Submission status: Wrong Answer

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

def rev(s):
    re = ""
    for k in range(len(s)):
        re = re + s[len(s) - 1 - k]
    return re

flag = False
count = 0

for i in range(Q):
    k = input()
    if k == "1":
        if i == Q -1:
            if count % 2 == 0:
                print(count)
                S = rev(S)
        else:
            flag = True
            count += 1
    else:
        if flag:
            if count % 2 == 1:
                S = rev(S)
            flag = False
            count = 0
        if k[2] == "1":
            S = k[4] + S
        else:
            S = S + k[4]

print(S)

Submission status: Wrong Answer

Solution code:
def main():
  S = input()
  Q = int(input())
  import sys
  que = [list(sys.stdin.readline().split()) for _ in range(Q)]
  judge = False
  for l in que:
    if int(l[0])==1:
      if judge:
        judge = False
      else:
        judge = True

    elif int(l[0])==2:
      f,c = int(l[1]),l[2]
      if judge:
        if f==1:
          f=2
        elif f==2:
          f=1
      if f==2:
        S = ''.join([S,c])

  if judge:
    S=S[::-1]
  print(S)
  
main()

Submission status: Wrong Answer

Solution code:
#D
from collections import deque
Str = str(input())
Q = int(input())
S = deque()
S.append(Str)
A = 0
B = []
C = []

for i in range(Q):
    a = list(map(str, input().split()))
    if a[0] == '1':
        if A%2==0:
            S.extendleft(B)
            B = []
            S.extend(C)
            C = []
        else:
            S.extendleft(C)
            C = []
            S.extend(B)
            B = []
        A += 1
    elif a[1] == '1':
        B.append(a[2])
    else:
        C.append(a[2])
if A%2 != 0:
    S.reverse()
print(''.join(S))

Submission status: Wrong Answer

Solution code:
from sys import stdin
input = stdin.readline
s=input()
q=int(input())
cnt=0
qq=[list(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])

Submission status: Wrong Answer

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

x = []
isone = False
for i in range(q):
    x = input().split()
    if x[0] == '1':
        if isone:
            pass
        elif i == q-1:
            s = s[::-1]
        else:
            isone = True
    elif x[1] == '1':
        if isone:
            s = x[2] + s[::-1]
            isone = False
        else:
            s = x[2] + s
    else:
        if isone:
            s = s[::-1] + x[2]
            isone = False
        else:
            s = s + x[2]
print(s)

Submission status: Wrong Answer

Solution code:
from collections import deque
s = list(input())
s = deque(s)
n = int(input())
cnt = 0
for i in range(n):
    q = list(map(str,input().split()))
    if q[0] == '1':
        cnt += 1
    else:
        if cnt % 2 == 0:
            if q[1] == '1':
                s.appendleft(q[2])
            else:
                s.append(q[2])
        else:
            if q[1] == '1':
                s.append(q[2])
            else:
                s.appendleft(q[2])
    print(q)
if cnt % 2 != 0:
    s.reverse()
print(''.join(list(s)))

Submission status: Wrong Answer

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

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

def addflont(s,c):
    return c+s
def addback(s,c):
    return s+c

reverse = 0

for i in range(q):
    t = list(map(str,input().split()))
    if t[0]=="1":
        reverse += 1
    if t[0]=="2":
        if t[1]=="1":
            if reverse%2==0:
                s = addflont(s,t[2])
            else:
                s = addback(s,t[2])
        elif t[1]=="2":
            if reverse%2==0:
                s = addback(s,t[2])
            else:
                s = addflont(s,t[2])

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

Submission status: Wrong Answer

Solution code:
s = input().split()
ans = s
s = input().split()
q = int(s[0])
count=0
for i in range(q):
    s = input().split()
    if s[0]=='1': 
        count+=1
    else:
        if count%2==1:
            ans=ans[::-1]
        count=0
        if s[1]=='1':
            ans=list(s[2])+ans
        else:
            ans.append(s[2])
print(''.join(ans))

Submission status: Wrong Answer

Solution code:
from collections import deque
S = list(input())
S = deque(S)
Q = int(input())
qlis = []
for _ in range(Q):
    qlis.append(input().split())

for i in range(Q):
    if len(qlis[i]) == 1:
        mae = S[0]
        usiro = S[-1]
        S[0] = usiro
        S[-1] = mae
    else:
        if int(qlis[i][1]) == 2:
            S.append(qlis[i][2])
        else:
            S.appendleft(qlis[i][2])

answer = "".join(S)
print(answer)

Submission status: Wrong Answer

Solution code:
import sys
t = input()

flont = []
back = []

if len(t) != 1:
    flont = [t[0]]
    back = [t[-1]]
else:
    flont = [t[0]]

q = int(input())
for i in range(q):
    s = input().split()
    if int(s[0]) == 1:
        sw = flont
        flont = back
        back = sw
    else:
        f = int(s[1])
        c = s[2]
        if f == 1:
            flont.append(c)
        else:
            back.append(c)
[sys.stdout.write(i) for i in list(reversed(flont))]
[sys.stdout.write(i) for i in back]
print("")

Submission status: Wrong Answer

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

ans = [S]
mode = True  # True: Normal False: Reverse
for query in Query:
    T = query[0]
    if T == '1':
        # モードを反転
        mode = not mode
    else:
        F = query[1]
        if (mode and (F == '1')) or (not mode and (F == '2')):
            ans.insert(0, query[2])
        else:
            ans.insert(-1, query[2])
            
res = ''.join(ans)
    
if not mode:
    res = res[::-1]

print(res)

Submission status: Wrong Answer

Solution code:
s = input().split()
q = int(input())
l = []
for i in range(q):
    l = input().split()
    if l[0] == '1':
        s.reverse()
    else:
        if 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
d=deque()
S=input()
Q=int(input())
query=[list(map(str,input().split())) for _ in range(Q)]
flag=0
#print(query)
s=S
s2=""
d.append(S)
"""
for i in range(Q):
    if len(query[i])==1:
        flag=(flag+1)%2 #反転フラグをひっくり返す
    else:
        if flag==0:
            if query[i][1]=="1":
                s="".join((query[i][2],s))
            else:
                s = "".join((s,query[i][2]))
        else:
            if query[i][1]=="2":
                s="".join((query[i][2],s))
            else:
                s = "".join((s,query[i][2]))
if flag==0:
    print(s)
else:
    for i in range(len(s)):
        s2="".join(())
    print(s[::-1])
"""

for i in range(Q):
    if len(query[i])==1:
        flag=(flag+1)%2 #反転フラグをひっくり返す
    else:
        if (flag==0)or(len(d)==1):
            if query[i][1]=="1":
                d.appendleft(query[i][2])
            else:
                d.append(query[i][2])
        else:
            if query[i][1]=="2":
                d.appendleft(query[i][2])
            else:
                d.append(query[i][2])
    #print(d)
if flag==0:
    print("".join(list(d)))
else:
    d.reverse()
    print("".join(list(d)))

Submission status: Wrong Answer

Solution code:
s = input()
q = int(input())
st = 0
str1 = ""
str2 = ""
for _ in range(q):
    query = list(input().split())
    if query[0] == "1":
        st = 1 if st == 0 else 0
    else:
        if query[1] == "1":
            if st == 0:
                str1 += query[2]
            else:
                str2 += query[2] 
        else:
            if st == 0:
                str2 += query[2] 
            else:
                str1 += query[2] 

if st == 0:
    ans = str1[::-1] + s + str2
else:
    ans = str2[::-1] + s + str1
    
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
reverseFlag = False

S = input()
Q = int(input())
S_mae = ''
S_ato = ''

for i in range(Q):
    TFC = input().split(' ')
    if TFC[0] == '1':
        reverseFlag = not reverseFlag
    else:
        if TFC[1] == '1':
            if reverseFlag:
                S_ato += TFC[2]
            else:
                S_mae = TFC[2] + S_mae
        else:
            if reverseFlag:
                S_mae = TFC[2] + S_mae
            else:
                S_ato = TFC[2] + S_ato
if reverseFlag:
    print(S_ato[::-1] + S[::-1] + S_mae[::-1])
else:
    print(S_mae + S + S_ato)

Submission status: Wrong Answer

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


def main():

    s = input()
    q = int(input())
    ls = [input().split() for _ in range(q)]

    for t in ls:
        if t[0] == '1':
            s = s[::-1]
        elif t[0] == '2':
            if t[1] == '1':
                s = t[2] + s
            elif t[1] == '2':
                s = s + t[2]
    
    print(s[:-1])

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

an=[]
an.append(s)

for i in range(q):
    li=input().split()

    if li[0]=="1":
        an.reverse()
    else:
        if li[1]=="1":
            an.insert(0,li[2])
        else:
            an.append(li[2])

print(*an,sep="")

Submission status: Wrong Answer

Solution code:
from collections import deque

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

conr = 0

for i in range(Q):
  q = list(input())
  if q[0] == "1":
    conr += 1
  else:
    s = q[-1]
    D.append(s)
    D.appendleft(s)

D = list(D)

total = len(D)

if conr % 2 == 0:
  last = total // 2
  if total % 2 == 1:
    last += 1
  ans = "".join(D[:last])
  print(ans)
else:
  start = total // 2 - 1
  if total % 2 == 1:
    start += 1
  ans = "".join(D[start:])
  print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
def main():
  S = input()
  Q = int(input())
  import sys
  que = [list(sys.stdin.readline().split()) for _ in range(Q)]
  judge = False
  for l in que:
    if int(l[0])==1:
      if judge:
        judge = False
      else:
        judge = True

    elif int(l[0])==2:
      f,c = int(l[1]),l[2]
      if judge:
        if f==1:
          f=2
        elif f==2:
          f=1
      if f==1:
        S = ''.join([c,S])
  if judge:
    S=S[::-1]
  print(S)
  
main()

Submission status: Wrong Answer

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

num_t1 = 0
is_forward = True
for query in queries:
    if "1" == query[0]:
        num_t1 += 1
        is_forward = not is_forward
    else:
        if is_forward:
            insert_index = 0 if "1" == query[1] else -1
            s.insert(insert_index, query[2])
        else:
            insert_index = -1 if "1" == query[1] else 0
            s.insert(insert_index, query[2])
is_reverse = (0 != num_t1 % 2)
if is_reverse:
    s.reverse()
print("".join(s))

Submission status: Wrong Answer

Solution code:
import sys, bisect, math, itertools, string, queue, copy
# import numpy as np
# import scipy
from collections import Counter,defaultdict,deque
from itertools import permutations, combinations
from heapq import heappop, heappush
# input = sys.stdin.readline
sys.setrecursionlimit(10**8)
mod = 10**9+7
def inp(): return int(input())
def inpm(): return map(int,input().split())
def inpl(): return list(map(int, input().split()))
def inpls(): return list(input().split())
def inplm(n): return list(int(input()) for _ in range(n))
def inplL(n): return [list(input()) for _ in range(n)]
def inplT(n): return [tuple(input()) for _ in range(n)]
def inpll(n): return [list(map(int, input().split())) for _ in range(n)]
def inplls(n): return sorted([list(map(int, input().split())) for _ in range(n)])

def main():
    s = str(input())
    q = inp()
    before = []
    after = []
    flag = False
    for _ in range(q):
        query = inpls()
        if query[0] == '1':
            if flag:
                flag = False
            else:
                flag = True
        else:
            f = query[1]
            S = query[2]
            if (flag and f=='2') or ((not flag) and f=='1'):
                after.append(S)
            else:
                before.append(S)
    ans = []
    for i in range(len(before)-1,-1,-1):
        ans.append(before[i])
    ans.append(s)
    for i in range(len(after)):
        ans.append(after[i])
    if not flag:
        ans.reverse()
    print(''.join(ans))
    
if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
from heapq import heappush, heappop, heapify
from collections import deque, defaultdict, Counter
import itertools
from itertools import permutations, combinations, accumulate
import sys
import bisect
import string
import math
import time


def I(): return int(input())


def S(): return input()


def MI(): return map(int, input().split())


def MS(): return map(str, input().split())


def LI(): return [int(i) for i in input().split()]


def LI_(): return [int(i)-1 for i in input().split()]


def StoI(): return [ord(i)-97 for i in input()]


def ItoS(nn): return chr(nn+97)


def input(): return sys.stdin.readline().rstrip()


def show(*inp, end='\n'):
    if show_flg:
        print(*inp, end=end)


YNL = {False: 'No', True: 'Yes'}
YNU = {False: 'NO', True: 'YES'}
MOD = 10**9+7
inf = float('inf')
IINF = 10**10
l_alp = string.ascii_lowercase
u_alp = string.ascii_uppercase
ts = time.time()
sys.setrecursionlimit(10**6)
nums = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']

show_flg = False
# show_flg = True


def main():
    st = S()
    Q = I()
    queries = []
    # print(st[0])
    # s = [st[i] for i in range(len(st))]
    # s = [None for i in range(3 * 10**5 + 2)]
    START = 10 ** 5
    s = [None] * (3 * START + 2)
    for i in range(START, START + len(st)):
        # print(st[START - i], START - i)
        s[i] = st[START - i]

    for i in range(Q):
        queries.append(list(MS()))

    op = True
    first = START - 1
    last = START + len(st) + 1
    # print(s)

    for i in range(Q):
        query = queries[i]
        if len(query) == 1:
            op = not op
        else:
            if (query[1] == '1' and op is True) or (query[1] == '2' and op is False):
                s[first] = query[2]
                first -= 1
                # s.insert(0, query[2])
            elif (query[1] == '1' and op is False) or (query[1] == '2' and op is True):
                s[last] = query[2]
                last += 1
                # s.append(query[2])
            else:
                exit(-1)
                return

    # print(''.join([t for t in s if t is not None]))
    # print(s)
    if op is True:
        s = [t for t in s if t is not None]
        print(''.join(s))
    else:
        s = [t for t in s if t is not None]
        print(''.join(s[::-1]))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

forward = []
backend = []
reverse = 0

for i in range(Q):
        s = input()
        s1 = list(s)

        if s1[0] == '1':
            reverse += 1
        else:
            s2 = s.split(" ")
            if s2[1] == '1':
                if reverse % 2 == 0:
                    forward.append(s2[2])
                else:
                    backend.append(s2[2])
            else:
                if reverse % 2 == 0:
                    backend.append(s2[2])
                else:
                    forward.append(s2[2])

#文字列の出力

if reverse % 2 == 0:
    for i in forward[::-1]:
        print(i, end="")
    
    print(S,end="")

    for i in backend:
        print(i,end="")

    print()
else:
    for i in backend[::-1]:
        print(i,end="")
    
    print(S,end="")

    for i in forward:
        print(i,end="")

    print()

Submission status: Wrong Answer

Solution code:
def solve():
    S = input()
    M = int(input())
    Q = [list(input().split()) for _ in range(M)]
    
    bef = ''
    s = S
    aft = ''
    for q in Q:
        if q[0] == '1':
            s = s[::-1]
            tmp = bef
            bef = aft[::-1]
            aft = tmp[::-1]
        else:
            ad = q[2]
            if q[1] == '1':
                bef += ad
            else:
                aft += ad
    ans = bef + s + aft
    return ans

print(solve())

Submission status: Wrong Answer

Solution code:
#import sys
MOD = 10 ** 9 + 7
INFI = 10**10
#input = sys.stdin.readline
import math

import itertools

#import bisect

import copy
from functools import lru_cache

def sosuhante(n):
    for k in range(2, int(math.sqrt(n))+1):
        if n% k ==0:
            return False
    return True
def cmb(n, r):
    if n - r < r: r = n - r
    if r == 0: return 1
    if r == 1: return n

    numerator = [n - r + k + 1 for k in range(r)]
    denominator = [k + 1 for k in range(r)]

    for p in range(2,r+1):
        pivot = denominator[p - 1]
        if pivot > 1:
            offset = (n - r) % p
            for k in range(p-1,r,p):
                numerator[k - offset] /= pivot
                denominator[k] /= pivot

    result = 1
    for k in range(r):
        if numerator[k] > 1:
            result *= int(numerator[k])

    return result

def kingaku(a,b,n):
    keta=len(str(n))
    return a*n+b*keta

def my_index(l, x, default=False):
    if x in l:
        return l.index(x)
    else:
        return default

#    h,w,a,b = map(int, input().split())
#    c = [[0 for j in range(n)] for i in range(n)]



def main():
    s=input()
    q=int(input())
    even=1
    right=[]
    left=[]
    for i in range(q):
        temp=list(input().split())
        temp[0]=int(temp[0])
        if temp[0]==2:
            a=int(temp[1])
            b=temp[2]
            if a==1:
                left.append(b)
            else:
                right.append(b)
        else:
            even=-(even-1)
            left,right=right,left
#    print(left,s,right,even)
    if even==0:
        s=s[::-1]
 #       right=right[::-1]
    else:
        left=left[::-1]
    ans="".join(left)+s+"".join(right)
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
s = input()
q = int(input())
once = False
rev = False
count = 0
for i in range(q):
    query = input().split()
    if query[0] == "1":
        once = True
        rev = not rev
        count += 1
        continue

    count = 0
    if query[1] == "1":
        if rev:
            s = s + query[2]
        else:
            s = query[2] + s
    else:
        if rev:
            s = query[2] + s
        else:
            s = s + query[2]

if once or count & 0:
    print(s[::-1])
else:
    print(s)

Submission status: Wrong Answer

Solution code:
def input_int():
    return map(int, input().split())

def one_int():
    return int(input())

def one_str():
    return input()

def many_int():
    return list(map(int, input().split()))

S= one_str()

Q = one_int()

is_not_ac=False
head = ""
tail = ""
for i in range(Q):
    temp = input()
    if len(temp)>=2:
        T, F, C = temp.split()
        if F=="1":
            if is_not_ac:
                tail += C
            else:
                head += C
        else:
            if is_not_ac:
                head += C
            else:
                tail += C
    else:        
        is_not_ac = not is_not_ac
        

if is_not_ac:
    print(tail[::-1] + S + head)
else:
    print(head[::-1] + S + tail)

Submission status: Wrong Answer

Solution code:
#for _ in range(int(input())):
s = input()
q = int(input())
b=0
for _ in range(q):
    qi = input().split(' ')
    print(len(qi))
    if(len(qi)==1):
        b=b^1
    else:
        fi = int(qi[1])
        if(fi==1):
            if(b==0):
                s=qi[2]+s
            else:
                s=s+qi[2]
        elif(fi==2):
            if(b==0):
                s=s+qi[2]
            else:
                s=qi[2]+s
if(b==1):
    s=s[::-1]

print(s)

Submission status: Wrong Answer

Solution code:
s = list(input())
q = int(input())
front = []
back = []

for i in range(q):
    buf = input().split()
    if(len(buf) == 1):
        s = "".join(front) + "".join(s) + "".join(back)
        s = s[::-1]
        front = []
        back = []
    else:
        if buf[1] == '1':
            tmp = [buf[2]]
            front += tmp
        else:
            tmp = [buf[2]]
            back += tmp

print("".join(front) + "".join(s) + "".join(back))

Submission status: Wrong Answer

Solution code:
import sys
input=sys.stdin.readline
s=input().strip()
Q=int(input())

flag=1
before=()
after=()
for i in range(Q):
    a_list=list(input().split())

    if a_list[0]=="1":
        flag*=-1
    else:
        if flag==1:
            if a_list[1]=="1":
                before+=(a_list[2],)
            else:
                after+=(a_list[2],)
        else:
            if a_list[1]=="1":
                after+=(a_list[2],)
            else:
                before+=(a_list[2],)

if flag:
    ans="".join(list(before[::-1]))+s+"".join(after)
else:
    ans="".join(list(after[::-1]))+s[::-1]+"".join(before)

print(ans)