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:
import sys
import collections
import itertools


def resolve(in_):
    one = ord(b'1')
    two = ord(b'2')
    # print(f'{one=} {two=}')

    S = in_.readline().strip()
    q = collections.deque(S)
    # print(f'{S=} {q=}')

    Q = int(in_.readline())
    for query in itertools.islice(in_, Q):
        T = query[0]
        if T == one and len(q) > 1:
            begin = q.popleft()
            # end = q.pop()
            # q.appendleft(end)
            q.rotate()
            q.append(begin)
        elif T == two:
            F = query[2]
            C = query[4]
            if F == one:
                q.appendleft(C)
            elif F == two:
                q.append(C)

    return bytes(q)


def main():
    answer = resolve(sys.stdin.buffer)
    print(answer.decode('ascii'))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
import collections


def resolve(in_):
    one = ord(b'1')
    two = ord(b'2')
    # print(f'{one=} {two=}')

    S = in_.readline().strip()
    q = collections.deque(S)
    # print(f'{S=} {q=}')

    Q = int(in_.readline())
    for query in in_:
        T = query[0]
        if T == one and len(q) > 1:
            begin = q.popleft()
            # end = q.pop()
            # q.appendleft(end)
            q.rotate()
            q.append(begin)
        elif T == two:
            F = query[2]
            C = query[4]
            if F == one:
                q.appendleft(C)
            elif F == two:
                q.append(C)
            else:
                raise ValueError
        else:
            pass

    return bytes(q)


def main():
    answer = resolve(sys.stdin.buffer)
    print(answer.decode('ascii'))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
import collections


def resolve(in_):
    one = ord(b'1')
    two = ord(b'2')
    # print(f'{one=} {two=}')

    S = in_.readline().strip()
    q = collections.deque(S)
    # print(f'{S=} {q=}')

    Q = int(in_.readline())
    for query in in_:
        T = query[0]
        if T == one and len(q) > 1:
            begin = q.popleft()
            # end = q.pop()
            # q.appendleft(end)
            q.rotate()
            q.append(begin)
        elif T == two:
            F = query[2]
            C = query[4]
            if F == one:
                q.appendleft(C)
            elif F == two:
                q.append(C)
            else:
                raise ValueError
        else:
            raise ValueError

    return bytes(q)


def main():
    answer = resolve(sys.stdin.buffer)
    print(answer.decode('ascii'))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def main():
    S = input()
    Q = int(input())
    flag = True
    for _ in range(Q):
        A = tuple(map(str, input().split()))
        if A[0] == "1":
            flag = not(flag)
        else:
            if (A[1] == "1" and not(flag)) or (A[1] == "2" and flag):
                S = A[2] + S
            else:
                S = S + A[2]
    if flag:
        print(S[::-1])
    else:
        print(S)
if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
from collections import deque

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

ans = deque()
ans.append(S)

A_list =[]

A = list(input().split() for _ in range(Q))

for i in range(Q):
    if i == 0:
        if A[i][0] == '2':
            if A[i][1] == '1':
                ans.appendleft(A[i][2])
            else:
                ans.append(A[i][2])
        else:
            continue
    else:

        if A[i-1][0] == '1' and A[i][0] == '1':
            continue
        elif A[i-1][0] == '1' and A[i][0] == '2':
                if A[i][1] == '1':
                    ans.append(A[i][2])
                else:
                    ans.appendleft(A[i][2])
        elif A[i-1][0] == '2' and A[i][0] == '2':
            if A[i][1] == '1':
                ans.appendleft(A[i][2])
            else:
                ans.append(A[i][2])
        A_list = []
        A_list.append(A[i])

if A[-1][0] == '1':
    ans.reverse()
    print(''.join(ans))
else:
    print(''.join(ans))

Submission status: Wrong Answer

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

q = int(input())
for i in range(q):
    query = list(input().split(" "))
    if len(query) == 1:
        l = queue.popleft()
        r = queue.pop()
        queue.append(l)
        queue.appendleft(r)
    else:
        if query[1] == "1":
            queue.appendleft(query[2])
        else:
            queue.append(query[2])

print("".join(list(queue)))

Submission status: Wrong Answer

Solution code:
S=input()
Q=int(input())
rev=False#前後が反転しているか否か
#毎回律義に足す必要はない。（リストの計算上それはすごい遅くなってしまう）
#Queでやってもよさそうだけど
from collections import deque
front = deque()
back= deque()
for i in range(Q):
    L=list(input().split())
    if L[0]=="1":
        rev=not rev
    elif L[0]=="2":
        if L[1]=="1":
            if rev:
                front.append(L[2])
            else:
                back.append(L[2])
        elif L[1]=="2":
            if rev:
                back.append(L[2])
            else:
                front.append(L[2])
ans= "".join(list(front))+S+"".join(list(back))
if rev:
    print(ans)    
else:
    print(ans[::-1])

Submission status: Wrong Answer

Solution code:
S=input()
Q=int(input())
rev=False#前後が反転しているか否か
#毎回律義に足す必要はない。（リストの計算上それはすごい遅くなってしまう）
#Queでやってもよさそうだけど
front=""
back=""
for i in range(Q):
    L=list(input().split())
    if L[0]=="1":
        rev=not rev
    elif L[0]=="2":
        if L[1]=="1":
            if rev:
                front+=L[2]
            else:
                back+=L[2]
        elif L[1]=="2":
            if rev:
                back+=L[2]
            else:
                front+=L[2]
if rev:
    print(front+S+back)    
else:
    print((front+S+back)[::-1])

Submission status: Wrong Answer

Solution code:
from collections import deque
s = deque([])
s.append(input())
q = int(input())
hanten = 0
for i in range(q):
    t = list(map(str,input().split()))
    if t[0] == '1':
        hanten = (hanten + 1)%2
    else:
        if hanten == 0:
            if t[1] == '1':
                #s = t[2] + s
                s.appendleft(t[2])
            else:
                #s = s + t[2]
                s.append(t[2])
        else:
            if t[1] == '2':
                #s = t[2] + s
                s.appendleft(t[2])
            else:
                #s = s + t[2]
                s.append(t[2])
if hanten == 0:
    for i in range(len(s)):
        print(s.popleft(),end='')
else:
    for i in range(len(s)):
        print(s.pop(),end='')

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
s = str(input())
Q = int(input())
forward = 1
front = ""
back = ""

for i in range(Q):
    q = list(input().split())
    if q[0] == "1":
        forward *= -1
    else:
        if q[1] == "1":
            if forward == 1:
                front = q[2] + front
            else:
                back = back + q[2]        
        else:
            if forward == 1:
                back = back + q[2]
            else:
                front = q[2] + front

print("front:"+front)
print("back:"+back)

s = front + s + back
if forward == -1:
    s = s[::-1]
print(s)

Submission status: Wrong Answer

Solution code:
s = str(input())
Q = int(input())
for i in range(Q):
    q = list(map(str, input().split()))
    if q[0] == "1":
        if len(s) > 1:
            s = s[-1] + s[1:-1] + s[0]
    else:
        if q[1] == "1":
            s = q[2] + s 
        else:
            s = s + q[2]
print(s)

Submission status: Wrong Answer

Solution code:
s = input()
Q = int(input())
for i in range(Q):
    q = list(map(str, input().split()))
    if q[0] == "1":
        if len(s) > 1:
            s = s[-1] + s[1:-1] + s[0]
    else:
        if q[1] == "1":
            s = q[2] + s 
        else:
            s = s + q[2]
print(s)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
s = input()
Q = int(input())
for i in range(Q):
    q = list(input().split())
    if q[0] == "1":
        s = s[-1] + s[1:-1] + s[0]
    else:
        if q[1] == "1":
            s = q[2] + s 
        else:
            s = s + q[2]
print(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
from fractions import gcd
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)])

s=input()
q=inp()

ql = []
for i in range(q):
    tmp = input()
    if tmp[0] == "1":
        ql.append("1")
    else:
        ql.append(tmp.split())

isrev=False
for i in range(q):
  if ql[i][0] == '1':
    isrev = not isrev
    continue
  if (ql[i][1] == '2') == isrev:
    s=ql[i][2]+s
  else:
    s=s+ql[i][2]
if isrev:
  s = s[::-1]

print(s)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
s = input()
Q = int(input())
q = [input().split() for _ in range(Q)]
reverse = 0
start = ""
end = ""
for i in q:
    if i[0] == "1":
        reverse ^= 1
    else:
        if int(i[1]) + reverse >> 0 ^ 0:
            end += i[2]
        else:
            start += i[2]
print(start[::-1] + s[::1 - reverse * 2] + end)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
s = input()
Q = int(input())
q = [input().split() for _ in range(Q)]
reverse = 0
start = ""
end = ""
for i in q:
    if i[0] == "1":
        reverse ^= 1
    else:
        if int(i[1]) + reverse >> 0 ^ 0:
            end += i[2]
        else:
            start += i[2]
s = start[::-1] + s + end
print(s[::1 - reverse * 2])

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
s = input()
Q = int(input())
q = [input().split() for _ in range(Q)]
reverse = 0
start = ""
end = ""
for i in q:
    if i[0] == "1":
        reverse ^= 1
    else:
        if int(i[1]) + reverse >> 0 ^ 0:
            end += i[2]
        else:
            start += i[2]
s = start[::-1] + s + end
print(s[::- 1 + reverse * 2])

Submission status: Wrong Answer

Solution code:
from copy import copy
S = input()
Q = int(input())

for _ in range(Q):
    _tmp = input()
    if len(_tmp) == 1:
        t = int(_tmp)
    else:
        t, f, c = _tmp.split()
        t = int(t)
        f = int(f)

    if t == 1:
        if len(S) > 1:
            if len(S) > 2:
                S = copy(S[-1] + S[1:-1] + S[0])
            elif len(S) == 2:
                S = copy(S[-1] + S[0])
            else:
                raise RuntimeError
    elif t == 2:
        if f == 1:
            S = copy(c + S)
        elif f == 2:
            S = copy(S + c)
        else:
            raise RuntimeError
    else:
        raise RuntimeError
print(S)

Submission status: Wrong Answer

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

for _ in range(Q):
    _tmp = input()
    if len(_tmp) == 1:
        t = int(_tmp)
    else:
        t, f, c = _tmp.split()
        t = int(t)
        f = int(f)

    if t == 1:
        if len(S) > 1:
            if len(S) > 2:
                S = S[-1] + S[1:-1] + S[0]
            elif len(S) == 2:
                S = S[-1] + S[0]
            else:
                raise RuntimeError
    elif t == 2:
        if f == 1:
            S = c + S
        elif f == 2:
            S = S + c
        else:
            raise RuntimeError
    else:
        raise RuntimeError
print(S)

Submission status: Wrong Answer

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

for _ in range(Q):
    _tmp = input()
    if len(_tmp) == 1:
        t = int(_tmp)
    else:
        t, f, c = _tmp.split()
        t = int(t)
        f = int(f)

    if t == 1 and len(S) > 1:
        if len(S) > 2:
            S = S[-1] + S[1:-1] + S[0]
        elif len(S) == 2:
            S = S[-1] + S[0]
        else:
            raise RuntimeError
    elif t == 2:
        if f == 1:
            S = c + S
        elif f == 2:
            S = S + c
        else:
            raise RuntimeError
    else:
        raise RuntimeError
print(S)

Submission status: Wrong Answer

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

for _ in range(Q):
    _tmp = input()
    if len(_tmp) == 1:
        t = int(_tmp)
    else:
        t, f, c = _tmp.split()
        t = int(t)
        f = int(f)

    if t == 1 and len(S) > 1:
        S = S[-1] + S[1:-1] + S[0]
    elif t == 2:
        if f == 1:
            S = c + S
        elif f == 2:
            S = S + c
        else:
            raise RuntimeError
    else:
        raise RuntimeError
print(S)

Submission status: Wrong Answer

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

for _ in range(Q):
    _tmp = input()
    if len(_tmp) == 1:
        t = int(_tmp)
    elif len(_tmp) == 5:
        t, f, c = _tmp.split()
        t = int(t)
        f = int(f)

    if t == 1 and len(S) > 1:
        S = S[-1] + S[1:-1] + S[0]
    elif t == 2:
        if f == 1:
            S = c + S
        elif f == 2:
            S = S + c
        else:
            raise RuntimeError
    else:
        raise RuntimeError
print(S)

Submission status: Wrong Answer

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


def main():
  S = input()[:-1]
  Q = int(input())

  q = deque([S])
  reverse = False
  for i in range(Q):
    line = input().split()
    if line[0] == '1':
      reverse = not(reverse)
    else:
      if reverse ^ (int(line[1])-1):
        # S = S + line[2]
        q.append(line[2])
      else:
        # S = line[2] + S
        q.appendleft(line[2])

  print(''.join(reversed(q)) if reverse else ''.join(q))


if(__name__ == '__main__'):
  main()

Submission status: Wrong Answer

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

s = deque(s)
t = 0
for _ in range(q):
  query = list(map(str, input().split()))
  if query[0] == "1": t = 1 - t
  else:
    if t == 0:
      if query[1] == "1": s.appendleft(query[2])
      elif query[1] == "2": s.append(query[2])
    elif t == 1:
      if query[1] == "1": s.append(query[2])
      elif query[1] == "2": s.appendleft(query[2])
if query[0] == "1":print("".join(s)[::-1])
else: print("".join(s))

Submission status: Wrong Answer

Solution code:
# ABC158
# D - String Formation

from collections import deque

def ChangeState(Is_normal):
    if Is_normal==True:
        return False
    else:
        return True

S=deque(input().split())
Q=int(input())
Is_normal=True

for _ in range(Q):
    q = input()
    if len(q)== 1 :
        Is_normal = ChangeState(Is_normal)
    else:
        if q[2]=='1':
            if Is_normal==True:
                # S=q[4]+S
                S.appendleft(q[4])
            else :
                # S=S+q[4]
                S.appendleft(q[4])

        else :
            if Is_normal==True:
                # S=S+q[4]
                S.appendleft(q[4])

            else:
                # S=q[4]+S
                S.appendleft(q[4])

    # print(Is_normal, S)
S=''.join(S)
if Is_normal==False:
    S=S[::-1]
print(S)

Submission status: Wrong Answer

Solution code:
# ABC158
# D - String Formation

def ChangeState(Is_normal):
    if Is_normal==True:
        return False
    else:
        return True



S=input()
Q=int(input())
print('-'*10)
Is_normal=True

for _ in range(Q):
    q = list(map(str,input().split()))
    if len(q)== 1 :
        Is_normal = ChangeState(Is_normal)
    else:
        if q[1]=='1':
            if Is_normal==True:
                S=q[2]+S
            else :
                S=S+q[2]
        else :
            if Is_normal==True:
                S=S+q[2]
            else:
                S=q[2]+S
    # print(Is_normal, S)
if Is_normal==False:
    S=S[::-1]
print(S)

Submission status: Wrong Answer

Solution code:
# -*= coding: utf-8 -*-
from collections import deque

S = input()
Q = int(input())
query = list()
for i in range(Q):
  query.append(list(map(str, input().split())))


d = deque()
d.append(S) # 右からSを追加
is_reverse = False

for q in query:
  if len(q) == 1:
    # Tiが1の場合
    
    is_reverse = not is_reverse
    
  else:
    # Tiが2の場合
    
    if q[1] == '1':
      # Fiが1の場合
      
      if is_reverse == False:
        d.appendleft(q[2])
      else:
        d.append(q[2])
      
    else:
      # Fiが2の場合
      
      if is_reverse == False:
        d.append(q[2])
      else:
        d.appendleft(q[2])

ans = ''
if is_reverse == True:
  d.reverse()
ans = ''.join(d)
print(ans)

Submission status: Wrong Answer

Solution code:
if __name__ == '__main__':
    S = list(input())
    Q = int(input())
    for _ in range(Q):
        Query = list(map(str, input().split()))
        if len(Query)==1:
            S[0], S[-1] = S[-1], S[0]
        else:
            if Query[1]=='1':
                S.insert(0, Query[2])
            else:
                S.append(Query[2])
        

    
    print(''.join(S))

Submission status: Wrong Answer

Solution code:
# -*= coding: utf-8 -*-
from collections import deque

S = input()
Q = int(input())
query = list()
for i in range(Q):
  query.append(list(map(str, input().split())))


d = deque()
d.append(S) # 右からSを追加
is_reverse = False

for q in query:
  if len(q) == 1:
    # Tiが1の場合
    
    is_reverse = not is_reverse
    
  else:
    # Tiが2の場合
    
    if q[1] == '1':
      # Fiが1の場合
      
      if is_reverse == False:
        d.appendleft(q[2])
      else:
        d.append(q[2])
      
    else:
      # Fiが2の場合
      
      if is_reverse == False:
        d.append(q[2])
      else:
        d.appendleft(q[2])

ans = ''
while len(d) > 0:
  if is_reverse == False:
    ans += d.popleft()
  else:
    ans += d.pop()
print(ans)

Submission status: Wrong Answer

Solution code:
if __name__ == '__main__':
    S = list(input())
    Q = int(input())

    for _ in range(Q):
        Query = list(map(str, input().split()))
        if len(Query)==1:
            S[0], S[-1] = S[-1], S[0]
        else:
            if Query[1]=='1':
                S.insert(0, Query[2])
            else:
                S.append(Query[2])
        

    
    ans= ''
    for s in S:
        ans += s
    print(ans)

Submission status: Wrong Answer

Solution code:
if __name__ == '__main__':
    S = list(input())
    Q = int(input())

    for _ in range(Q):
        Query = list(map(str, input().split()))
        if len(Query)==1:
            S[0], S[-1] = S[-1], S[0]
        else:
            if Query[1]=='1':
                S.insert(0, Query[2])
            else:
                S.append(Query[2])
        

    
    ans= ''
    for s in S:
        ans += s
    print(ans)

Submission status: Wrong Answer

Solution code:
inversion = 1 #1 は反転していない、 -1 は反転している。
s = input()
s1 = ""
s2 = ""
n_query = int(input())
for i in range(n_query):
    q = input().split()
    
    if q[0] == '1':
        inversion *= -1
    elif q[0] == '2':
        if inversion == 1:
            if q[1] == '1':
                s1 = s1 + q[2]
            elif q[1] == '2':
                s2 = s2 + q[2]
        elif inversion == -1:
            if q[1] == '1':
                s2 = s2 + q[2]
            elif q[1] == '2':
                s1 = s1 + q[2]

if inversion == 1:
    s = s1[::-1] + s + s2
elif inversion == -1:
    s = s2[::-1] + s + s1

print(s)

Submission status: Wrong Answer

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

forward = True
que0 = []
que1 = []

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

    if query[0] == '1':
        forward = not forward

    else:
        if query[1] == '1':
            if forward:
                # S = query[2] + S
                que1.append(query[2])
            else:
                # S = S + query[2]
                que0.append(query[2])


        # at tail
        else:
            if forward:
                # S = S + query[2]
                que0.append(query[2])

            else:
                # S = query[2] + S
                que1.append(query[2])

S = ''.join(que0[::-1]) + S + ''.join(que1)

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

Submission status: Wrong Answer

Solution code:
s=list(input())
top=bool()
t=[]
u=[]
n=int(input())
for i in range(n):
  q=list(map(str,input().split()))
  if q[0]=='1':
    top=not top
  else:
    if (q[1]=="1") ^ top:
      t.insert(0,q[2])
    else:
      u.append(q[2])
s=t+s+u
if top:
  print(*list(reversed(s)),sep='')
else:
  print(s)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
from collections import deque


def main():
    d = deque()
    d.append(input())

    Q = int(input())
    reflag = False

    for i in range(Q):
        query = list(input().split())
        if query[0] == '1':
            reflag = not reflag
        elif (query[1] == '1' and not reflag) or (query[1] == '2' and reflag):
            d.appendleft(query[2])
        else:
            d.append(query[2])

    if reflag:
        print("".join(reversed(d)))
    else:
        print("".join(d))


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
from collections import deque


def main():
    d = deque()
    d.append(input())

    Q = int(input())
    reflag = False

    for i in range(Q):
        query = list(input())
        if query[0] == '1':
            reflag = not reflag
        elif (query[2] == '1' and not reflag) or (query[2] == '2' and reflag):
            d.appendleft(query[4])
        else:
            d.append(query[4])

    if reflag:
        d.reverse()
        print("".join(d))
    else:
        print("".join(d))


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
from collections import deque


def main():
    S = input()
    Q = int(input())
    reflag = False
    d = deque()
    d.append(S)
    for i in range(Q):
        query = list(input())
        if query[0] == '1':
            reflag = not reflag
        elif query[2] == '1':
            if reflag:
                d.append(query[4])
            else:
                d.appendleft(query[4])

        else:
            if reflag:
                d.appendleft(query[4])
            else:
                d.append(query[4])
    if reflag:
        d.reverse()
        print("".join(d))
    else:
        print("".join(d))


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

def main():
    S = input()
    Q = int(input())
    reflag = False
    for i in range(Q):
        query = list(input())
        if query[0] == '1':
            reflag = not reflag
        elif query[2] == '1':
            if reflag:
                S = query[4] + S
            else:
                S = S + query[4]
        else:
            if reflag:
                S = S + query[4]
            else:
                S = query[4] + S
    if reflag:
        print(S)
    else:
        print(S[::-1])


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
from collections import deque

s=deque(list(input()))
flag=True
for i in range(int(input())):
  dir=input().split()
  if int(dir[0])==1:flag=False
  else:
    if int(dir[1])==1:
      if flag:s.appendleft(dir[2])
      else:s.append(dir[2])
    else:
      if flag:s.append(dir[2])
      else:s.appendleft(dir[2])
s=list(s)
if flag==False:s.reverse()
print(*s,sep='')

Submission status: Wrong Answer

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines


s = readline().decode().rstrip()
q = int(readline())

cnt1 = 0
t2 = []
for _ in range(q):
    t = list(readline().decode().rstrip().split())
    t2.append(t)
    if t[0] == '1':
        cnt1 += 1

s1 = []
s2 = []

for t in t2:
    if t[0] == '1':
        cnt1 -= 1
    else:
        if cnt1 % 2 == 0 and t[1] == '1':
            s1.append(t[2])
        elif cnt1 % 2 != 0 and t[1] == '2':
            s1.append(t[2])
        elif cnt1 % 2 == 0 and t[1] == '2':
            s2.append(t[2])
        else:
            s2.append(t[2])

print(''.join(s1[::-1])+s+''.join(s2))

Submission status: Wrong Answer

Solution code:
import sys
import math
import itertools
import bisect
from copy import copy
from collections import deque,Counter
from decimal import Decimal
import functools
def s(): return input()
def k(): return int(input())
def S(): return input().split()
def I(): return map(int,input().split())
def X(): return list(input())
def L(): return list(input().split())
def l(): return list(map(int,input().split()))
def lcm(a,b): return a*b//math.gcd(a,b)
sys.setrecursionlimit(10 ** 9)
mod = 10**9+7
cnt = 0
ans = 0
inf = float("inf")

S = X()
Q = k()
a = [s() for i in range(Q)]
for i in a:
    if i == "1":
        S.reverse()
    else:
        a,b,c = map(str,i.split())
        if b == "1":
            S.append(c)
            S[0],S[-1] = S[-1],S[0]
        else:
            S.append(c)

print("".join(S))

Submission status: Wrong Answer

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

c = list(input())
c = c[:len(c)-1]
q = int(input())


num = len(c)

for i in range(q):
    q_i= list(input())
    #print(q_i)
    if q_i[0] == '1':
        temp = c[0]
        c[0] = c[num-1]
        c[num-1] = temp
    else:
        if q_i[2] =='2':
            c += [q_i[4]]
        else:
            c = [q_i[4]] + c
        num += 1

    #print(c)

ans = c[0]

for i in range(1,len(c)):
    ans += c[i]

print(ans)

Submission status: Wrong Answer

Solution code:
#158_D
s = input()
q = int(input())

from collections import deque

s = deque(s)

rev = 1
for _ in range(q):
    Q = list(input().split())
    if Q[0] == "1":
        rev *= -1
    elif Q[0] == "2":
        if (Q[1]=="1" and rev==1) or (Q[1]=="2" and rev==-1):
            s.append(Q[2])
        elif (Q[1]=="2" and rev==1) or (Q[1]=="1" and rev==-1):
            s.appendleft(Q[2])
            
s = "".join(s)
            
if rev == -1:
    s = s[::-1]

print(s)

Submission status: Wrong Answer

Solution code:
#158_D
s = input()
q = int(input())

from collections import deque

s = deque(s)

rev = 1
for _ in range(q):
    Q = list(input().split())
    if Q[0] == "1":
        rev *= -1
    elif Q[0] == "2":
        if (Q[1]=="1" and rev==1) or (Q[1]=="2" and rev==-1):
            s.append(Q[2])
        elif (Q[1]=="2" and rev==1) or (Q[1]=="1" and rev==-1):
            s.appendleft(Q[2])
            
s = "".join(list(s))
            
if rev == -1:
    s = s[::-1]

print(s)

Submission status: Wrong Answer

Solution code:
S1=S2=input()
N=int(input())
count=0
for _ in range(N):
    *Q,=input().split()
    if len(Q)==1:
        count+=1
    else:
        if (count+int(Q[1]))%2:
            S1=Q[2]+S1
            S2=S2+Q[2]
        else:
            S1=S1+Q[2]
            S2=Q[2]+S2
print(S2 if count%2 else S1)