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
from heapq import heapify,heappop,heappush,heappushpop
from copy import copy,deepcopy
from itertools import product,permutations,combinations,combinations_with_replacement
from collections import defaultdict,Counter
from bisect import bisect_left,bisect_right
from math import gcd,ceil,floor,factorial
# from fractions import gcd
from functools import reduce
from pprint import pprint
from statistics import mean,median,mode

INF = float("inf")

def mycol(data,col):
    return [ row[col] for row in data ]

def mysort(data,col,reverse_flag):
    data.sort(key=lambda x:x[col],reverse=reverse_flag)
    return data

def mymax(data):
    M = -1*float("inf")
    for i in range(len(data)):
        m = max(data[i])
        M = max(M,m)
    return M

def mymin(data):
    m = float("inf")
    for i in range(len(data)):
        M = min(data[i])
        m = min(m,M)
    return m

def mycount(ls,x):
    # lsはソート済みであること
    l = bisect_left(ls,x)
    r = bisect_right(ls,x)
    return (r-l)

def myoutput(ls,space=True):
    if space:
        if len(ls)==0:
            print(" ")
        elif type(ls[0])==str:
            print(" ".join(ls))
        elif type(ls[0])==int:
            print(" ".join(map(str,ls)))
        else:
            print("Output Error")
    else:
        if len(ls)==0:
            print("")
        elif type(ls[0])==str:
            print("".join(ls))
        elif type(ls[0])==int:
            print("".join(map(str,ls)))
        else:
            print("Output Error")

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

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

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

def CI(n):
    return [ int(input()) for _ in range(n) ]

def LI(n):
    return [ list(map(int,input().split())) for _ in range(n) ]

def S():
    return input()

def MS():
    return input().split()

def RS():
    return list(input())

def CS(n):
    return [ input() for _ in range(n) ]

def LS(n):
    return [ list(input()) for _ in range(n) ]

# ddict = defaultdict(lambda: 0)
# ddict = defaultdict(lambda: 1)
# ddict = defaultdict(lambda: int())
# ddict = defaultdict(lambda: list())
# ddict = defaultdict(lambda: float())

s = S()
q = I()
# Q = LS(q)

# print(Q)

a = 'A'
flag = True
for i in range(q):
    d = input().split()
    if len(d)==1:
        flag = not flag
    elif d[1]=='1':
        if flag:
            a = d[2] + a
        else:
            a = a + d[2]
    elif d[1]=='2':
        if flag:
            a = a + d[2]
        else:
            a = d[2] + a

if flag:
    pass
else:
    a = a[::-1]

print(a.replace("A",s))

Submission status: Wrong Answer

Solution code:
from sys import stdin
def main():
    readline = stdin.readline
    S=readline()
    Q=int(readline())
    Queries=[readline().split() for i in range(Q)]

    for i in Queries:
        if i[0]=='1':
            S=S[::-1]
        else:
            if i[1]=='1':
                S=i[2]+S
            else:
                S=S+i[2]
    print(S)
main()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# FileName: 	D_fix_3
# CreatedDate:  2020-08-30 13:22:07 +0900
# LastModified: 2020-08-30 13:37:04 +0900
#


import os
import sys
# import numpy as np
# import pandas as pd


def main():
    S = input()
    Q = int(input())
    appendix_1 = ''
    appendix_2 = ''
    reverse = 0
    for _ in range(Q):
        q = list(input().split())
        if q[0] == '1':
            reverse += 1
        else:
            if q[1] == '1' and reverse % 2 == 0:
                appendix_1 += q[2]
            elif q[1] == '1' and reverse % 2 == 1:
                appendix_2 += q[2]
            if q[1] == '2' and reverse % 2 == 0:
                appendix_2 += q[2]
            elif q[1] == '2' and reverse % 2 == 1:
                appendix_1 += q[2]

#    print(appendix_1, appendix_1[::-1])
    if reverse % 2 == 0:
        print(appendix_1[::-1] + S + appendix_2)
    else:
        print(appendix_2[::-1] + S + appendix_1)



if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

def main():
    S = input().replace('\n', '')
    Q = int(input())
    S = deque(S)

    flg = 0
    for _ in range(Q):
        query = input().split()
        if len(query) == 1:
            flg += 1
        else:
            T, F, C = query
            if int(F) ^ (flg % 2):
                S.append(C)
            else:
                S.appendleft(C)
    if flg % 2:
        print(reduce(lambda a, b: a + b, S))
    else:
        S.reverse()
        print(reduce(lambda a, b: a + b, S))

main()

Submission status: Wrong Answer

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

c1 = 0
for i in range(Q):
    q = [x for x in input().split()]
    T = int(q[0])
    if T == 1:
        c1 = (c1 + 1) % 2
    else: # T == 2
        F, C = int(q[1]), q[2]
        if F + c1 == 1:
            S = C + S
        else: # F + c2 == 2
            S = S + C

if c1 == 1:
    N = len(S)
    S = S[N - 1::-1]

print(S)

Submission status: Wrong Answer

Solution code:
from collections import deque
S = input()
Q = int(input())
reverse = 1

d = deque()
d.append(S)

for i in range(Q):
  temp = input().split()
  if temp[0] == "1":
    reverse = -reverse
  else:
    if temp[1] == "1" :
      if reverse == 1:
        d.appendleft(temp[2])
      else:
        d.append(temp[2])
    else:
      if reverse == 1:
        d.append(temp[2])
      else:
        d.appendleft(temp[2])
      
      
length = len(d)
for _ in range(length):
  temp = 0
  if reverse == 1:
    temp = d.popleft()
  else:
    temp = d.pop()
  print(temp, end="")

Submission status: Wrong Answer

Solution code:
S = [input()]
Q = int(input())
for i in range(Q):
  temp = input().split()
  if temp[0] == "1":
    S.reverse()
  else:
    if temp[1] == "1":
      S.insert(0, temp[2])
    else:
      S.append(temp[2])
for i in S:
  print(i,end="")

Submission status: Wrong Answer

Solution code:
s=input()
q=int(input())
a=[list(map(str,input().split())) for i in range(q)]

ans=[]
ans.append(s)

for i in range(q):
  if len(a[i])==1:
    ans[0],ans[-1]=ans[-1],ans[0]

  else:
    if a[i][1]=='1':
      ans.insert(0,a[i][2])
    else:
      ans.append(a[i][2])

print(''.join(ans))

Submission status: Wrong Answer

Solution code:
s=input()
q=int(input())
a=[list(map(str,input().split())) for i in range(q)]

ans=[]
ans.append(s)

for i in range(q):
  if len(a[i])==1:
    ans.insert(0,ans[-1])
    ans.append(ans[1])
    ans.pop(1)
    ans.pop(-2)

  else:
    if a[i][1]=='1':
      ans.insert(0,a[i][2])
    else:
      ans.append(a[i][2])

print(''.join(ans))

Submission status: Wrong Answer

Solution code:
S=input()
Q=int(input())
p=0
u=0
l=""
r=""
for i in range(Q):
    q=list(input().split())
    if q[0]=="1":
        p+=1
        u+=1
    else:
        if q[1]=="1" and p%2==0 or q[1]=="2" and p%2==1:
            l=l+q[2]
        else:
            r=r+q[2]
        p=0
if u%2==0:
    l=l[::-1]
    S=l+S+r
else:
    S=S[::-1]
    r=r[::-1]
    S=r+S+l
print(S)

Submission status: Wrong Answer

Solution code:
S=input()
Q=int(input())
p=0
u=0
for i in range(Q):
    q=list(input().split())
    if q[0]=="1":
        p+=1
        u+=1
    else:
        if q[1]=="1" and p%2==0 or q[1]=="2" and p%2==1:
            S=q[2]+S
        else:
            S=S+q[2]
        p=0
if u%2==1:
    S=S[::-1]
print(S)

Submission status: Wrong Answer

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

S_list = []
S_list.append(S)
step_list = []

for i in range(Q):
    step = list(input().split())
    step_list.append(step)
#print(step_list)

for i in range(Q):
    if step_list[i][0] == '1':
        S_list.reverse()
    elif step_list[i][0] == '2':
        if step_list[i][1] == '1':
            S_list.insert(0, step_list[i][2])
        elif step_list[i][1] == '2':
            S_list.append(step_list[i][2])

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

Submission status: Wrong Answer

Solution code:
from collections import deque
S = input()
Q = int(input())
R = deque(S)
rev = 0

for i in range(Q):
    T = input().split()
    if T[0] == '1':
        rev += 1
    else:
        if T[1] == '1':
            if rev % 2 == 0:
                R.append(T[2])
            else:
                R.appendleft(T[2])
        else:
            if rev % 2 == 0:
                R.appendleft(T[2])
            else:
                R.append(T[2])
ans = ''.join(R)
if rev % 2 ==0:
    print(ans[::-1])
else:
    print(ans)

Submission status: Wrong Answer

Solution code:
from collections import deque
S = input()
Q = int(input())
R = deque(S)
rev = 0

for i in range(Q):
    T = input().split()
    if T[0] == '1':
        rev += 1
    else:
        if T[1] == '1':
            if rev % 2 == 0:
                R.append(T[2])
            else:
                R.appendleft(T[2])
        else:
            if rev % 2 == 0:
                R.appendleft(T[2])
            else:
                R.append(T[2])
ans = ''.join(R)
if rev % 2 ==0:
    print(ans)
else:
    print(ans[::1])

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
def main():
    import sys

    input = sys.stdin.readline

    S = [input().rstrip()]
    Q = int(input())

    for _ in [0] * Q:
        q = input().split()
        if q[0] == '1':
            S = S[::-1]
        else:
            if q[1] == '1':
                S = [q[2]] + S
            elif q[1] == '2':
                S.append(q[2])
    print(''.join(S))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
def main():
    S = [input()]
    Q = int(input())

    for _ in [0] * Q:
        q = input().split()
        if q[0] == '1':
            S = S[::-1]
        else:
            if q[1] == '1':
                S = [q[2]] + S
            else:
                S.append(q[2])
    print(''.join(S))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Your code here!
from collections import deque
S=input()
s=deque()
s.append(S)
Q=int(input())
count=0
query=[]
for i in range(Q):
    arr=input().split()
    query.append(arr)

for i in range(Q):
    if query[i][0]=="1":
        count+=1
    else:
        if count%2==0:
            if query[i][1]=="1":
                s.appendleft(query[i][2])
            else:
                s.append(query[i][2])
        else:
            if query[i][1]=="1":
                s.append(query[i][2])
            else:
                s.appendleft(query[i][2])
if count%2==0:
    for i in range(len(s)):
        print(s[i],end="")
        
else:
    for i in range(len(s)):
        print(s[len(s)-i-1],end="")

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Your code here!
from collections import deque
S=input()
s=deque()
s.append(S)
Q=int(input())

query=[]
for i in range(Q):
    arr=input().split()
    query.append(arr)

for i in range(Q):
    if query[i][0]=="1":
        s=list(s)
        s.reverse()
        s=deque(s)
    else:
        if query[i][1]=="1":
            s.appendleft(query[i][2])
        else:
            s.append(query[i][2])
for i in s:
    print(i,end="")

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Your code here!
from collections import deque
S=input()
s=deque()
s.append(S)
Q=int(input())

query=[]
for i in range(Q):
    arr=input().split()
    query.append(arr)

for i in range(Q):
    if query[i][0]=="1":
        s[-1],s[0]=s[0],s[-1]
    else:
        if query[i][1]=="1":
            s.appendleft(query[i][2])
        else:
            s.append(query[i][2])
for i in s:
    print(i,end="")

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Your code here!
from collections import deque
S=input()
s=deque()
s.append(S)
Q=int(input())

query=[]
for i in range(Q):
    arr=input().split()
    query.append(arr)

for i in range(Q):
    if query[i][0]=="1":
        s[-1],s[0]=s[0],s[-1]
    else:
        if query[i][1]=="1":
            s.appendleft(query[i][2])
        else:
            s.append(query[i][2])
s=''.join(s)
print(s)

Submission status: Wrong Answer

Solution code:
from collections import deque
S=input()
n=int(input())
a=0
for i in range(n):
  q=input()
  if q=="1":
    a+=1
  else:
    p,b,c=q.split()
    if b=="1":
      if a%2==0:
        S=c+S
      else:
        S=S+c
    else:
      if a%2==1:
        S=c+S
      else:
        S=S+c
print(S)

Submission status: Wrong Answer

Solution code:
from collections import deque

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

qq = [list(input().split()) for i in range(q)]
qq.append([0, 0, 0])

order = 0
cnt = 0
f = []

for i in qq:
  if i[0] != order:
    if order == 1:
      if cnt%2 == 1:
        s.reverse()
        cnt = 0
    elif order == 21:
      s.extendleft(f)
      f = []
    else:
      s.extend(f)
      f = []


  if i[0] == '1':
    order = 1
    cnt += 1
  else:
    if i[1] == '1':
      order = 21
      f.insert(0, i[2])
    else:
      order = 22
      f.append(i[2])

print(s)
print(''.join(s))

Submission status: Wrong Answer

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

  cnt=0
  prefix, suffix = '', ''
  for q in Query:
    if q[0]=='1':
      cnt%=2
      prefix, suffix = suffix, prefix
    else:
      if q[1]=='1':
        prefix=q[2]+prefix
      else:
        suffix=suffix+q[2]
  if cnt:
    S=S[::-1]
  print(prefix[::-1]+S+suffix)

main()

Submission status: Wrong Answer

Solution code:
def resolve():
    S = input()
    Q = int(input())
    reverse = False
    import collections
    left = collections.deque([])
    right = collections.deque([])
    for i in range(Q):
        query = input().split()
        if query[0] == "1":
            reverse = (not reverse)
            continue
        if query[1] == "1":
            if reverse:
                right.append(query[2])
            else:
                left.appendleft(query[2])
        elif query[1] == "2":
            if reverse:
                left.appendleft(query[2])
            else:
                right.append(query[2])
    if reverse:
        ans = "{}{}{}".format("".join(reversed(right)), reversed(S), "".join(reversed(left)))
    else:
        ans = "{}{}{}".format("".join(left), S, "".join(right))
    print(ans)


if '__main__' == __name__:
    resolve()

Submission status: Wrong Answer

Solution code:
def resolve():
    S = input()
    Q = int(input())
    reverse = False
    import collections
    left = collections.deque([])
    right = collections.deque([])
    for i in range(Q):
        query = input().split()
        if query[0] == "1":
            reverse = (not reverse)
            continue
        if query[1] == "1":
            if reverse:
                right.append(query[2])
            else:
                left.appendleft(query[2])
        elif query[1] == "2":
            if reverse:
                left.appendleft(query[2])
            else:
                right.append(query[2])
    if reverse:
        ans = "{}{}{}".format("".join(reversed(right)), S, "".join(reversed(left)))
    else:
        ans = "{}{}{}".format("".join(left), S, "".join(right))
    print(ans)


if '__main__' == __name__:
    resolve()

Submission status: Wrong Answer

Solution code:
from collections import deque
S = input()
ans = deque(S)
Q = int(input())
rev = True
for i in range(Q):
    a_list = input().split()
    if a_list[0] == "1":
        ans.reverse()
        if rev == True:
            rev = False
        else:
            rev = True
    elif (a_list[1] == "1" and rev == True) or (a_list[1] == "2" and rev == False):
        ans.appendleft(a_list[2])
    else:
        ans.append(a_list[2])
    if rev:
        ans.reverse()
ans = "".join(ans)
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
def main():
    st = input()
    q = int(input())
    qnt_inv = 0
    for _ in range(q):
        t = sys.stdin.read(1)
        if t == '2':
            if qnt_inv % 2 == 1:
                st = st[::-1]
                qnt_inv = 0
            f, a = input().split()
            if f == '1':
                st = a + st
            else:
                st = st + a
        else:
            t = sys.stdin.read(1)
            qnt_inv += 1

    print(st)

main()

Submission status: Wrong Answer

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

cnt=0
left=""
right=""

for i in range(q):
  qq=[x for x in input().rstrip().split()]

  if qq[0]=="2":
    t=int(qq[0])
    f=int(qq[1])
    c=qq[2]
    
    if cnt%2==1 and f==1:
      right=c+right
    
    elif cnt%2==1 and f==2:

      # left=left+c
      left=c+left
    elif cnt%2==0 and f==2:
  
      right=right+c
    elif cnt%2==0 and f==1:

      left=c+left

  else:
    cnt+=1

s=left+s+right
if cnt%2==1:
  print(s[::-1])
else:
  print(s)

Submission status: Wrong Answer

Solution code:
from collections import deque
if __name__ == '__main__':

    s = input()
    n = int(input())

    rvflg = False

    d_st = deque()
    d_ed = deque()

    for _ in range(n):
        q = input()
        if len(q) == 1:
            if rvflg:
                rvflg = False
            else:
                rvflg = True
        else:
            i,f,c = map(str,q.split())
            if f == "1":#先頭に追加
                if rvflg:
                    #末尾に追加
                    d_ed.append(c)
                else:
                    #先頭に追加
                    d_st.append(c)
            else:#末尾に追加
                if rvflg:
                    #先頭に追加
                    d_st.append(c)
                else:
                    #末尾に追加
                    d_ed.append(c)

    ans = "".join(d_st) + s + "".join(d_ed)
    #最後に反転するかを決定
    if rvflg:
        ans = ans[::-1]
    print(ans)

Submission status: Wrong Answer

Solution code:
from collections import deque
if __name__ == '__main__':

    s = input()
    n = int(input())

    rvflg = False

    d = deque()
    d.append(s)

    for _ in range(n):
        q = input()
        if len(q) == 1:
            if rvflg:
                rvflg = False
            else:
                rvflg = True
        else:
            i,f,c = map(str,q.split())
            if f == "1":#先頭に追加
                if rvflg:
                    #末尾に追加
                    d.append(c)
                else:
                    #先頭に追加
                    d.appendleft(c)
            else:#末尾に追加
                if rvflg:
                    #先頭に追加
                    d.appendleft(c)
                else:
                    #末尾に追加
                    d.append(c)

    #最後に反転するかを決定
    if rvflg:
        d.reverse()
    ans = "".join(d)
    print(ans)

Submission status: Wrong Answer

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

ct=0
reverse=[]
string=[deque(''), deque('')]
# 先頭に付け足す->string[0]にappendleft
# 最後尾に付け足す->strint[1]にappend
# ctの偶奇が変わる毎に付け足す方を変える

for i in range(Q):
    l=list(input().split())
    if l==['1']:
        reverse.append(i)
        ct+=1
    elif (int(l[1])+ct)%2==1:
        string[0].appendleft(l[2])
    else:
        string[1].append(l[2])

# print(string)
# print(ct)
if ct%2==0:
    top=''.join(string[0])
    bottom=''.join(string[1])
else:
    top=''.join(reversed(string[1]))
    bottom=''.join(reversed(string[0]))
    S=S[::-1]
    print(S)
print(top+S+bottom)

Submission status: Wrong Answer

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

ct=0
reverse=[]
string=[deque(''), deque('')]
# 先頭に付け足す->string[0]にappendleft
# 最後尾に付け足す->strint[1]にappend
# ctの偶奇が変わる毎に付け足す方を変える

for i in range(Q):
    l=list(input().split())
    if l==['1']:
        reverse.append(i)
        ct+=1
    elif (int(l[1])+ct)%2==1:
        string[0].appendleft(l[2])
    else:
        string[1].append(l[2])

# print(string)
print(ct)
if ct%2==0:
    top=''.join(string[0])
    bottom=''.join(string[1])
else:
    top=''.join(reversed(string[1]))
    bottom=''.join(reversed(string[0]))
    S=S[::-1]
    print(S)
print(top+S+bottom)

Submission status: Wrong Answer

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

ct=0
reverse=[]
string=[deque(''), deque('')]
# 先頭に付け足す->string[0]にappendleft
# 最後尾に付け足す->strint[1]にappend
# ctの偶奇が変わる毎に付け足す方を変える

for i in range(Q):
    l=list(input().split())
    if l==['1']:
        reverse.append(i)
        ct+=1
    elif (int(l[1])+ct)%2==1:
        string[0].appendleft(l[2])
    else:
        string[1].append(l[2])

# print(string)


# print(top,bottom)

if ct%2==0:
    top=''.join(string[0])
    bottom=''.join(string[1])
else:
    top=''.join(reversed(string[1]))
    bottom=''.join(reversed(string[0]))
print(top+S+bottom)

Submission status: Wrong Answer

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

f1 = 1
f2 = 2
cnt = 0
st = ""
se = ""

for _ in range(q):
    q = input().split()
    if int(q[0]) == 1:
        cnt += 1
    elif int(q[0]) == 2:
        if cnt % 2 == 0:
            if int(q[1]) == 1:
                st = q[2] + st
            elif int(q[1]) == 2:
                se += q[2]
        else:
            if int(q[1]) == 1:
                se = q[2] + se
            elif int(q[1]) == 2:
                st += q[2]


ans = st + s + se
if cnt % 2 == 0:
    print(ans)
else:
    ans = list(ans)
    ans.reverse()
    ans = "".join(ans)
    print(ans)

Submission status: Wrong Answer

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

f1 = 1
f2 = 2
cnt = 0
st = ""
se = ""

for _ in range(q):
    q = input().split()
    if int(q[0]) == 1:
        cnt += 1
        f1 = f2
        f2 = f1
    elif int(q[0]) == 2:
        if int(q[1]) == f1:
            st = q[2] + st
        elif int(q[1]) == f2:
            se += q[2]



if cnt % 2 == 0:
    ans = st + s + se
    print(ans)
else:
    ans = st + s + se
    ans = list(ans)
    ans.reverse()
    ans = "".join(ans)
    print(ans)

Submission status: Wrong Answer

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

f1 = 1
f2 = 2

st = ""
se = ""

for _ in range(q):
    q = input().split()
    if int(q[0]) == 1:
        f1 = f2
        f2 = f1
    elif int(q[0]) == 2:
        if int(q[1]) == f1:
            st = q[2] + st
        elif int(q[1]) == f2:
            se += q[2]


ans = st + s + se
if f1 == 2:
    ans = list(ans)
    ans.reverse()
    ans = "".join(ans)
print(ans)

Submission status: Wrong Answer

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

t1 = 1
t2 = 2

st = ""
se = ""

for _ in range(q):
    q = input().split()
    if int(q[0]) == 1:
        t1 = t2
        t2 = t1
    elif int(q[0]) == 2:
        if int(q[1]) == t1:
            st += q[2]
        elif int(q[1]) == t2:
            se += q[2]

st = list(st)            
st.reverse()
st = "".join(st)
ans = st + s + se
if t1 == 2:
    ans = list(ans)
    ans.reverse()
    ans = "".join(ans)
print(ans)

Submission status: Wrong Answer

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

t1 = 1
t2 = 2

st = ""
se = ""

for _ in range(q):
    q = input().split()
    if int(q[0]) == 1:
        t1 = t2
        t2 = t1
    elif int(q[0]) == 2:
        if int(q[1]) == t1:
            st += q[2]
        elif int(q[1]) == t2:
            se += q[2]

st = list(st)            
st.reverse()
st = "".join(st)
ans = st + s + se
print(ans)

Submission status: Wrong Answer

Solution code:
ini = lambda : int(input())
inm = lambda : map(int,input().split())
inl = lambda : list(map(int,input().split()))
gcd = lambda x,y : gcd(y,x%y) if x%y else y
def factorization(n):
    arr = []
    temp = n
    for i in range(2, int(-(-n**0.5//1))+1):
        if temp%i==0:
            cnt=0
            while temp%i==0:
                cnt+=1
                temp //= i
            arr.append([i, cnt])
    if temp!=1:
        arr.append([temp, 1])
    if arr==[]:
        arr.append([n, 1])
    return arr
def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)
    divisors.sort()
    return divisors

#maincode-------------------------------------------------
s = input()
q = ini()
for i in range(q):
    a = input()
    if a[0] == '1':
        tmp1 = s[0]
        tmp2 = s[-1]
        s = tmp2 + s[1:]
        s = s[:-1] + tmp1
    else:
        if a[2] == '1':
            s = a[4] + s
        else:
            s = s + a[4]
print(s)

Submission status: Wrong Answer

Solution code:
s = input()
n = int(input())
c = 0
left = ''
right = ''
origin_l = [s, s[::-1]]
for _ in range(n):
  lst = [i for i in input().split()]
  if lst[0] == '1':
    if c == 1:
      s, tmp = tmp, s
      #print(s)
      #print(tmp)
    else:
      tmp = s
      origin = origin_l[1]
      left, right = right[::-1], left[::-1]
      s = left + origin + right
      c = 1
      #print(s)
      #print(tmp)
  elif lst[0] == '2':
    c = 0
    if lst[1] == '1':
      s = lst[2] + s
      left = lst[2] + left
    elif lst[1] == '2':
      s = s + lst[2]
      right = right + lst[2]
print(s)

Submission status: Wrong Answer

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

querys = []
is_reversed = False
for _ in range(Q):
    tmp = input().split()
    if len(tmp)==1:
        is_reversed = not(is_reversed)
    else:
        if tmp[1]==str(1):
            if is_reversed:
                S = S+tmp[2]
            else:
                S = tmp[2]+S
        elif tmp[1]==str(2):
            if is_reversed:
                S = tmp[2]+S
            else:
                S = S+tmp[2]
print(S)

Submission status: Wrong Answer

Solution code:
S = str(input())
Q = int(input())
flip = 0
for i in range(Q):
  j = input()
  if len(j) == 1:
    flip += 1
    continue
  else:
    if flip % 2 == 0:
      if j[2] == '1':
        S = j[4] + S
        continue
      elif j[2] == '2':
        S = S + j[4]
        continue
    elif flip % 2 == '1':
      if j[2] == 1:
        S = S + j[4]
        continue
      elif j[2] == '2':
        S = j[4] + S
        continue
if flip % 2 == 1:
  S = S[::-1]
print(S)

Submission status: Wrong Answer

Solution code:
S=input()
Q=int(input())
A=[list(input().split()) for i in range(Q)]
c=0
for i in range(Q):
    if A[i][0]=="1":
        c+=1
        continue
    if c%2==1 and A[i][0]=="2":
        if A[i][1]=="1":
            S=S+A[i][2]
        elif A[i][1]=="2":
            S=A[i][2]+S
    elif c%2==0 and A[i][0]=="2":
        if A[i][1]=="1":
            S=A[i][2]+S
        elif A[i][1]=="2":
            S=A[i][2]+S
if c%2==1:
    S=S[::-1]
print(S)

Submission status: Wrong Answer

Solution code:
from collections import deque
S=input()
d=deque()
for i in S:
    d.append(i)
Q=int(input())
Flag=True
for i in range(Q):
    L=input().split()
    if L[0]=="1":
        Flag=not Flag
    else:
        if L[1]=="1" and Flag or L[1]=="2" and not Flag:
            d.append(L[2])
        else:
            d.appendleft(L[2])
ans=""
for i in d:
    ans+=i
if Flag:
    ans=ans[::-1]
print(ans)

Submission status: Wrong Answer

Solution code:
S=input()
Q=int(input())
r=0
for i in range(Q):
    L=input().split()
    if L[0]=="1":
        r+=1
    else:
        if L[1]=="1" and r==0 or L[1]=="2" and r==1:
            S=L[2]+S
        else:
            S=S+L[2]
        if r==1:
            r-=0
if r%2==1:
    S=S[::-1]
print(S)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
S=input()
Q=int(input())
for i in range(Q):
    L=list(input().split())
    if L[0]=="1":
        S=S[::-1]
    else:
        if L[1]=="1":
            S=L[2]+S
        else:
            S=S+L[2]
print(S)

Submission status: Wrong Answer

Solution code:
from collections import deque

s = input()

r = False
d = deque()
d.append(s)

q = int(input())
for i in range(q):
    t = input()
    if t == "1":
        r = not r
    else:
        t, f, c = list(t.split())
        f = False if f=="1" else True

        if r == f:
            d.appendleft(c)
        else:
            d.append(c)

if r:
    d.reverse()

print("".join(d))

Submission status: Wrong Answer

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

d = 0
X = [[], []]
for i in range(Q):
    q = input()
    if q[0] == "1":
        d = (d + 1) % 2
    else:
        a, f, c = q.split()
        x = (int(f) + d) % 2
        X[x].append(c)

if d == 0:
    v = "".join(X[1])[::-1] + S + "".join(X[0])
else:
    v = "".join(X[0])[::-1] + S + "".join(X[1])
print(v)

Submission status: Wrong Answer

Solution code:
from sys import stdin

S = stdin.readline()
n = int(stdin.readline())
flipNumber = 0
for _ in range(n):
  li = list(map(str, stdin.readline().split()))
  if len(li) == 1: flipNumber += 1
  else:
    letter = li[2]
    if flipNumber%2 == 0:
      if int(li[1]) == 1: S = letter + S
      else: S = S + letter
    else:
      if int(li[1]) == 1: S = S + letter
      else: S = letter + S

if flipNumber%2 == 1: S = "".join(list(reversed(S)))
print(S)

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=}')

    reverse_flag = False
    Q = int(in_.readline())
    for query in itertools.islice(in_, Q):
        T = query[0]
        if T == one:
            reverse_flag ^= True 
            # q.reverse()
        elif T == two:
            F = query[2]
            C = query[4]
            if (F == one) ^ reverse_flag:
                q.appendleft(C)
            else:
                q.append(C)

    return bytes(reversed(q)) if reversed else bytes(q)


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


if __name__ == '__main__':
    main()