Problem:


Score : 400 points

Problem Statement

Takahashi has a string S consisting of lowercase English letters.

Starting with this string, he will produce a new one in the procedure given as follows.

The procedure consists of Q operations. In Operation i (1 \leq i \leq Q), an integer T_i is provided, which means the following:

If T_i = 1: reverse the string S.

If T_i = 2: An integer F_i and a lowercase English letter C_i are additionally provided.

If F_i = 1 : Add C_i to the beginning of the string S.

If F_i = 2 : Add C_i to the end of the string S.

Help Takahashi by finding the final string that results from the procedure.

Constraints

1 \leq |S| \leq 10^5

S consists of lowercase English letters.

1 \leq Q \leq 2 \times 10^5

T_i = 1 or 2.

F_i = 1 or 2, if provided.

C_i is a lowercase English letter, if provided.

Input

Input is given from Standard Input in the following format:

S
Q
Query_1
:
Query_Q

In the 3-rd through the (Q+2)-th lines, Query_i is one of the following:

1

which means T_i = 1, and:

2 F_i C_i

which means T_i = 2.

Output

Print the resulting string.

Sample Input 1

a
4
2 1 p
1
2 2 c
1

Sample Output 1

cpa

There will be Q = 4 operations. Initially, S is a.

Operation 1: Add p at the beginning of S. S becomes pa.

Operation 2: Reverse S. S becomes ap.

Operation 3: Add c at the end of S. S becomes apc.

Operation 4: Reverse S. S becomes cpa.

Thus, the resulting string is cpa.

Sample Input 2

a
6
2 2 a
2 1 b
1
2 2 c
1
1

Sample Output 2

aabc

There will be Q = 6 operations. Initially, S is a.

Operation 1: S becomes aa.

Operation 2: S becomes baa.

Operation 3: S becomes aab.

Operation 4: S becomes aabc.

Operation 5: S becomes cbaa.

Operation 6: S becomes aabc.

Thus, the resulting string is aabc.

Sample Input 3

y
1
2 1 x

Sample Output 3

xy



Sample input:
a
4
2 1 p
1
2 2 c
1


Sample output:
cpa

Submission status: Wrong Answer

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

for i in range(Q):
  q = list(input().split())
  if int(q[0]) == 1:
    S[0], S[-1] = S[-1], S[0]
  else:
    if int(q[1]) == 1:
      S.insert(0, q[2])
    else:
      S.append(q[2])

print(''.join(S))

Submission status: Wrong Answer

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

for _ in range(Q):
  q = list(input())
  if len(q) == 1:
    r = S[0]
    l = S[-1]
    S[0] = l
    S[-1] = r
  else:
    if q[2] == "1": S.insert(0, q[4])
    else: S.append(q[4])

print("".join(S))

Submission status: Wrong Answer

Solution code:
import sys
t = input()

flont = []
back = []

if len(t) != 1:
    flont = list(reversed([t[:-1]]))
    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())
inv = False
head = []
tail = []
for i in range(q):
    p = input()
    if p[0]=='1':
        if inv:inv=False
        else:inv=True
    elif p[0]=='2':
        _,f,c= p.split()
        if f=='1':
            if inv:head.append(c)
            else:tail.append(c)
        elif f=='2':
            if inv:tail.append(c)
            else:head.append(c)
news = ''.join(head)[::-1] + s + ''.join(tail)
if inv:print(news)
else:print(news[::-1])

Submission status: Wrong Answer

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

left = []
right = []

for _ in range(q):
    Q = list(input().split())
    if int(Q[0]) == 1:
        left, right = right[::-1], left[::-1]

    else:
        if int(Q[1]) == 1:
            left = [Q[2]] + left
        else:
            right.append(Q[2])

print(''.join(left) + s + ''.join(right))

Submission status: Wrong Answer

Solution code:
S=input()
Q=int(input())
count=0
for i in range(Q):
	K=input().split()
	if K[0]=="1":
		count+=1
	if K[0]=="2":
		if K[1]=="1":
			if count%2==0:
				S=K[2]+S
			else:
				S=S+K[2]
		elif K[1]=="2":
			if count%2==0:
				S=S+K[2]
			else:
				S=K[2]+S
print(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):
    #print(i,len(d))
    if (len(query[i])==1):
        if len(d)>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:
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:
        if len(s) != 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())
seq1 = ''
seq2 = ''

for i in range(q):
    ii = input().split()
    if ii[0] == '1':
        seq1, seq2 = seq2, seq1
    else:
        if ii[1] == '1':
            seq1 += ii[2]
        else:
            seq2 += ii[2]
print(seq1[::-1] + s + seq2)

Submission status: Wrong Answer

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

answer = s
s1 = ""
s2 = ""
state = 0


for _ in range(q):
    Q = input()
    if Q == "1":
        if state == 0:
            state = 1
        else:
            state = 0
        continue

    _,F,C = Q.split()
    if F == "1":
        if state == 0:
            s1 = s1 + C
        else:
            s2 = s2 + C
    else:
        if state == 0:
            s2 = s2 + C
        else:
            s1 = s1 + C




if state == 0:
    answer = s1[::-1] + s[::-1] + s2
else:
    answer = s2[::-1] + s + s1



print(answer)

Submission status: Wrong Answer

Solution code:
import sys
t = input()

flont = []
back = []

if len(t) != 1:
    flont = [t[:-1]]
    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 _ in range(q)]

if len(s) <= 2:
    ans = s
    for i in range(q):
        now = query[i]
        if now == ['1']:
            if len(ans) != 1:
                ans = ans[-1] + ans[1:-1] + ans[0]
            else:
                continue
        else:
            f = now[1]
            c = now[2]
            if f == '1':
                ans = c + ans
            elif f == '2':
                ans = ans + c

    print(ans)

else:
    l = s[0]
    r = s[-1]

    for i in range(q):
        now = query[i]
        if now == ['1']:
            if len(s) != 1:
                l, r = r[-1] + l[1:], r[:-1] + l[0]
        else:
            f = now[1]
            c = now[2]
            if f == '1':
                l = c + l
            elif f == '2':
                r = r + c

    print(l + s[1:-1] + r)

Submission status: Wrong Answer

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

ans = [S]
X = []
Y = []
state = 1

for k in range(Q):
    if Query[k][0]=="1":
        state *= -1
    elif Query[k][0]=="2":
        if Query[k][1]=="1":
            if state == 1:
                X.append(Query[k][2])
            else:
                Y.append(Query[k][2])
        else:
            if state == 1:
                Y.append(Query[k][2])
            else:
                X.append(Query[k][2])

if state == 1:
    X = list(reversed(X))
    ans = X + ans + Y
else:
    Y = list(reversed(Y))
    ans = list(reversed(ans))
    ans = Y + ans + X

print("".join(ans))

Submission status: Wrong Answer

Solution code:
s = input()
# l = ''
# r = ''
c = []
isNormal = True
n = int(input())
query = []

for i in range(n):
    query.append(list(input().split()))

# print(query)

for i in range(n):
    # print(l, r, s)
    # Reverse
    if query[i][0] == '1':
        # s = s[::-1]
        isNormal = not isNormal
    # Add
    else:
        # To Head
        if query[i][1] == '1':
            # print('# toHead')
            if isNormal:
                # print('# normal')
                c.append((query[i][2], 'l'))
            else:
                # print('# not normal')
                c.append((query[i][2], 'r'))

        # To Tail
        else:
            # print('# toTail')
            if isNormal:
                # print('# normal')
                c.append((query[i][2], 'r'))
            else:
                # print('# not normal')
                c.append((query[i][2], 'l'))

# print(c)
l, r = '', ''

if isNormal:
    # print('isNormal')
    for i in range(len(c)):
        if c[i][1] == 'l':
            l = c[i][0] + l
        elif c[i][1] == 'r':
            r += c[i][0]
    # print(l, s, r)
    print(l + s + r)
else:
    # print('is not Normal')
    for i in range(len(c)):
        if c[i][1] == 'l':
            r += c[i][0]
        elif c[i][1] == 'r':
            l += c[i][0]
    # print(l, s, r)
    print(l + s + r)

Submission status: Wrong Answer

Solution code:
list=[]
list.append(input())
q=int(input())
for i in range(q):
	a=input()
	if a=="1":
		list.reverse()
	else:
		a,f,c = a.split()
		if f=="1":
			list.insert(0,c)
		else:
			list.append(c)
c="".join(list)
print(c)

Submission status: Wrong Answer

Solution code:
S = input()
Q = int(input())
reverse = 0
head = []
tail = []
for i in range(Q):
    s = input().split()
    if int(s[0]) == 1:
        reverse = (reverse + 1) % 2
    else:
        if int(s[1]) == 1:
            if reverse == 1:
                tail.append(s[2])
            else:
                head.append(s[2])
        else:
            if reverse == 1:
                head.append(s[2])
            else:
                tail.append(s[2])
head = ''.join(head)
tail = ''.join(tail)
S = head + S + tail
if reverse == 1:
    print(S[::-1])
else:
    print(S)

Submission status: Wrong Answer

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

def t1():
  s1=s[0]
  sl=s[-1]
  s[0]=sl
  s[-1]=s1

for i in range(q):
  t=input()
  if t=="1":
    t1()
  else:
    ti,f,c=t.split()
    if f=="1":
      s=[c,]+s
    else:
      s.append(c)

print("".join(s))

Submission status: Wrong Answer

Solution code:
def main():
    import sys
    input = sys.stdin.readline
    s = input()
    q = int(input())
    flag = 1
    A = list(input().split())
    if A[0] == '2':
        if flag * int(A[1]) == 1:
            s = A[2]+s
        else:
            s = s+A[2]
    for i in range(q-1):
        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)
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys

stdin = sys.stdin

ns = lambda : stdin.readline().rstrip()
ni = lambda : int(ns())
na = lambda : list(map(int, stdin.readline().split()))
sys.setrecursionlimit(10 ** 7)

def main():
    s = ns()
    Q = ni()

    T1 = 0
    FrontS = ""
    BackS = ""
    for _ in range(Q):
        T = stdin.readline().split()
        if T[0] == "1":
            T1 += 1
        else:
            if T1 % 2 == 0:
                # これは元通り
                if T[1] == "1":
                    FrontS += T[2]
                else:
                    BackS += T[2]
            else:
                # これは逆向き
                if T[1] == "1":
                    BackS = T[2] + BackS
                else:
                    FrontS = T[2] + FrontS
    s = FrontS + s + BackS
    if T1 % 2 == 0:
        print(s)
    else:
        print(s[::-1])

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
S = input()
Q = int(input())
top = ''
end = ''
reverse_flag = 1
for _ in range(Q):
    query = input()
    if len(query) == 1:
        reverse_flag *= -1
    else:
        _, F, C = query.split()
        if (int(F)-1.5) * reverse_flag == 1:
            end += C
        else:
            top += C

if reverse_flag == 1:
    ans = (top[::-1] + S + end)
    print(ans)
else:
    ans = (end[::-1] + S[::-1] + top)
    print(ans)

Submission status: Wrong Answer

Solution code:
import numpy as np
import math as m

def main(**kwargs):
    front = ""
    back = ""
    now = "f"

    for q in qs:
        if q[0] == "1":
            if now == "b":
                now = "f"
            else:
                now = "b"
        else:
            if (q[1] == "1" and now == "f") or (q[1] == "2" and now == "b"):
                front += q[2]
            else:
                back += q[2]

    if now == "f":
        res = front[::-1] + s + back[::-1]
    else:
        res = back + s[::-1] + front
        
    return res

if __name__ == "__main__":
    cin = np.array(input().split(" "))
    s, *_ = cin
    cin = np.array(input().split(" ")).astype("int")
    n, *_ = cin

    qs = []

    for i in range(n):
        cin = np.array(input().split(" "))
        qs.append(list(cin))
    
    cout = main(s=s, n=n, qs=qs)
    print(cout)

Submission status: Wrong Answer

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

s = input().split()[0]
q = int(input())

left, right = [], []
state = 1
for _ in range(q):
    q_i = input().split()
    if len(q_i) == 1:
        state = 2
    else:
        _, f, c = q_i
        f = int(f)
        if (f+state)%2 == 0:
            left += [c]
        else:
            right += [c]
if state == 1:
    print(''.join(left[::-1]) + s + ''.join(right))
else:
    print(''.join(right[::-1]) + s + ''.join(left))

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
from collections import deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random

# List of ints
def LI(): return list(map(int, sys.stdin.readline().split()))

# Single int
def I(): return int(sys.stdin.readline())

# List, which contains Lists of chars (not strings)
def LS():return list(map(list, sys.stdin.readline().split()))

# Single List of chars (not strings)
def S(): return list(sys.stdin.readline())[:-1]

# List of "n" integers from "n" lines.
def IR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = I()
    return l
# List of "n" Lists of integers
def LIR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = LI()
    return l
def SR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = S()
    return l
def LSR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = LS()
    return l


# Runtime Complexity: O(?)
# Space Complexity: O(?)
# Solution
s = sys.stdin.readline().split()
lines_count = I()

chars = list(s)

def prepend(chars, ch):
    chars.insert(0, ch)

def append(chars, ch):
    chars.append(ch)

actions = {
    1: prepend,
    2: append
}

for i in range(lines_count):
    line = LS()

    if len(line) == 1:
        actions[1], actions[2] = actions[2], actions[1]
    else:
        if line[1][0] == '1':
            actions[1](chars, line[2][0])
        else:
            actions[2](chars, line[2][0])

if actions[1] == append:
    chars.reverse()

print(''.join(chars))

Submission status: Wrong Answer

Solution code:
"""
// Author : snape_here - Susanta Mukherjee
     
"""
 
from __future__ import division, print_function
 
import os,sys
from io import BytesIO, IOBase
 
if sys.version_info[0] < 3:
    from __builtin__ import xrange as range
    from future_builtins import ascii, filter, hex, map, oct, zip
 
 
def ii(): return int(input())
def si(): return input()
def mi(): return map(str,input().split())
def li(): return list(mi())
 
 
def gcd(x, y):
    while y:
        x, y = y, x % y
    return x
    
 
def read():
    sys.stdin = open('input.txt', 'r')  
    sys.stdout = open('output.txt', 'w') 
 
 
import math

mod=1000000007 

def main():

    s=si()
    c=0
    n=ii()
    d=0
    for i in range(n):
        a=li()
        if i==n-1 and len(a)==1 and d%2==0:
            s=s[::-1]
        if len(a)==1:
            c+=1
            d+=1
        else:
            if a[1]=='1':
                if c%2:
                    s=a[2]+s 
                else:
                    s+=a[2]
            else:
                if c%2==0:
                    s=a[2]+s 
                else:
                    s+=a[2]
            d=0
    print(s)

# region fastio#
 
BUFSIZE = 8192
 
 
class FastIO(IOBase):
    newlines = 0
 
    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None
 
    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()
 
    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()
 
    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)
 
 
class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")
 
 
def print(*args, **kwargs):
    """Prints the values to a stream, or to sys.stdout by default."""
    sep, file = kwargs.pop("sep", " "), kwargs.pop("file", sys.stdout)
    at_start = True
    for x in args:
        if not at_start:
            file.write(sep)
        file.write(str(x))
        at_start = False
    file.write(kwargs.pop("end", "\n"))
    if kwargs.pop("flush", False):
        file.flush()
 
 
if sys.version_info[0] < 3:
    sys.stdin, sys.stdout = FastIO(sys.stdin), FastIO(sys.stdout)
else:
    sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
 
input = lambda: sys.stdin.readline().rstrip("\r\n")
 
# endregion
 
 
if __name__ == "__main__":
    #read()
    main() 
#Comment read()

Submission status: Wrong Answer

Solution code:
S = list(input())
N = int(input())
flg = 0
count = 0
for i in range(N):
    A = list(input().split())
    if int(A[0]) == 1:
        flg = 1
        count += 1
        continue
    elif int(A[1]) == 1 and flg == 0:
        S.insert(0,A[2])
    elif int(A[1]) == 1 and flg == 1:
        S.append(A[2])
    elif int(A[1]) == 2 and flg == 0:
        S.append(A[2])
    elif int(A[1]) == 2 and flg == 1:
        S.insert(0,A[2])
    else:
        S.append(A[2])
    flg = 0
if count % 2 == 1:
     S.reverse()
print(''.join(S))

Submission status: Wrong Answer

Solution code:
S = input()
Q = int(input())
Atama = []
Osiri = []
rev = [0]*Q
for i in range(Q):
    a = list(input().split())
    if len(a) == 1:##T_i=1
        rev[i] = 1
    else:#T_i == 2
        if a[1] == "1":
            Atama.append([i,a[2],1])
        else:
            Osiri.append([i,a[2],-1])

##revの累積和
Rev = []
tmp = 0
r = sum(rev)
for i in range(len(rev)):
    r -= rev[i]
    Rev.append(r)
    
for i in range(len(Atama)):
    Atama[i][2] *= (-1)**Rev[Atama[i][0]]
for i in range(len(Osiri)):
    Osiri[i][2] *= (-1)**Rev[Osiri[i][0]]

Ans = Atama+Osiri
##print(Ans)
fw = ""
aw = ""
N = len(Ans)
for i in range(N):
    if Ans[i][2] == 1:
        fw += Ans[i][1]
    else:
        aw += Ans[i][1]

if sum(rev)%2 == 1:
    S = S[::-1]
fw = fw[::-1]

print(fw+S+aw)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
def main():
    s=input()
    q=int(input())
    queries = []
    rev = [0]*q
    flag=0
    for i in range(q):
        query = list( input().split() )
        queries.append( query )
        if query==["1"]:
            flag+=1
        rev[i]=flag
    
    n_rev = queries.count(["1"])
    rev = [rr+n_rev for rr in rev]
    left=""
    right=""
    for i,query in enumerate(queries):
        if query!=["1"]:
            q1 = query[1]
            q2 = query[2]
            if rev[i]%2==0:
                if q1=="1":
                    left=q2+left
                else:
                    right=right+q2
            else:
                if q1=="1":
                    right=right+q2
                else:
                    left=q2+left 
    #     print(i,left,s,right)
    if n_rev%2==1:
        s=s[::-1]
    print("".join([left,s,right]) )
main()

Submission status: Wrong Answer

Solution code:
import sys
def input(): return sys.stdin.readline().rstrip()

def main():
    S = input()
    Q = int(input())

    reverse = False
    header = []
    footer = []
    for _ in range(Q):
        query = input()
        if query[0] == '1':
            reverse = not reverse
        else:
            _, f, c = query.split()
            if reverse:
                if f == '1':
                    footer = [c] + footer
                else:
                    header.append(c)
            else:
                if f == '1':
                    header.append(c)
                else:
                    footer = [c] + footer
    if reverse:
        print("".join(footer + list(S)[::-1] + header))
    else:
        print("".join(header) + S + "".join(footer))

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
s = list(input())
n = int(input())
sl = []
fl = []
flag = True
for _ in range(n):
    q = list(map(str, input().split()))
    if len(q) == 1:
        if flag:
            flag = False
        else:
            flag = True
    if len(q) == 3:
        if q[1] == "1":
            if flag:
                sl.insert(0, q[2])
            else:
                fl.append(q[2])
        else:
            if flag:
                fl.append(q[2])
            else:
                sl.insert(0, q[2])
if flag:
    s = sl+s
    s = s+fl
else:
    sl.reverse()
    fl.reverse()
    s = fl+s
    s = s+sl
print("".join(s))

Submission status: Wrong Answer

Solution code:
S = [input()]
Q = int(input())
for _ in range(Q):
  query = input().split()
  if query[0] == '1':
    S = S[::-1]
  else:  # query[0] == 2
    F, C = query[1:]
    if F == '1':
      S = [C] + S
    else:  # F == '2'
      S += [C]
print(''.join(S))

Submission status: Wrong Answer

Solution code:
S=input()
Q=int(input())
c=0
left=""
right=""
for i in range(Q):
    T=list(map(str,input().split()))
    if len(T)==1:
        c+=1
    else:
        if (T[1]=="1" and c%2==0) or (T[1]=="2" and c%2==1):
            left+=T[2]
        else:
            right+=T[2]

if c%2==0:
    ans=left[::-1]+S+right
else:
    ans=right[::-1]+S+left
print(ans)

Submission status: Wrong Answer

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

def solve():
    d = deque(list(input().split()))
    Q = int(input())

    is_reverse = False # 反転中か？

    for _ in range(Q):
        query = input()
        if query == "1":
            is_reverse = not is_reverse
        else:
            t, f, c = query.split()
            if f == "1":
                if is_reverse:
                    # 末尾にcを追加
                    d.append(c)
                else:
                    # 先頭にcを追加
                    d.appendleft(c)
            else:
                if is_reverse:
                    # 先頭に追加
                    d.appendleft(c)
                else:
                    # 末尾に追加
                    d.append(c)

    if is_reverse:
        d.reverse()
        for moji in d:
            print(moji, end="")
    else:
        for moji in d:
            print(moji, end="")
    print()


if __name__ == "__main__":
    solve()

Submission status: Wrong Answer

Solution code:
s=input()
q=int(input())
n=0
for i in range(q):
    li=input().split()
    if int(li[0])==1:
        n+=1
    elif int(li[0])==2:
        if (n+int(li[1]))%2==1:
            s=li[2]+s
        else:
            s=s+li[2]
if n%2==0:
    print(s)
else:
    print(-1)

Submission status: Wrong Answer

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

num_turn = 0

front = []
end = []

for i in range(Q):
    query = input()
    if query == '1':
        num_turn += 1
    else:
        _, f, c = query.split()
        f = int(f)
        if f == 1:
            if num_turn%2==0:
                front.append(c)
            else:
                end.append(c)
        else:
            if num_turn%2==0:
                end.append(c)
            else:
                front.append(c)

S = list(S)
if num_turn%2==1:
    ans = end + S[::-1] + front
else:
    ans = front[::-1] + S + end[::-1]
print(''.join(ans))

Submission status: Wrong Answer

Solution code:
S = list(input())
Q = int(input())
Qry = [input().split() for i in range(Q)]
for q in Qry:
    if q[0] == '1':
        S = [S[-1]] + S[1:-1] + [S[0]]
    else:
        if q[1] == '1':
            S = [q[2]] + S
        else:
            S += [q[2]]
print(*S, sep='')

Submission status: Wrong Answer

Solution code:
from collections import deque
S = deque([input()])

dic = 1
Q = int(input())
for i in range(0,Q):
    Query = input()
    if Query == '1':
        dic = 1*(-1)
    else:
        T, F, C = Query.split()
        if (F*dic == 2) or (F*dic == -1):
            S.append(C)
        else:
            S.appendleft(C)
print(''.join(list(S)[::dic]))

Submission status: Wrong Answer

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

for _ in range(q):
	t=input()
	if t[0]=="1":
		s.reverse()
	else:
		t,f,c=t.split()
		if f=="1":
			s.insert(0,c)
		else:
			s.append(c)
print(*s,sep="")

Submission status: Wrong Answer

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

for _ in range(Q):
  item = list(input().split())
  if item[0] == '1':
    S[0], S[-1] = S[-1], S[0]

  else:
    if item[1] == '1':
      S.insert(0, item[2])

    else:
      S.append(item[2])

print(''.join(S))

Submission status: Wrong Answer

Solution code:
s = input()

q = int(input())
r = 0
b = ""
c = ""
for i in range(q):
    A = list(input().split())
    if A[0] == '1':
        r += 1
    else:
        if (A[1] == '1' and r % 2 == 0) or (A[1] != '1' and r % 2 != 0):
            s = A[2] + s
            b = A[2] + b
        else:
    
            c += A[2]

if r % 2 == 0:
    print(b + s + c)
else:
    s = b + s + c
    print(s[::-1])

Submission status: Wrong Answer

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

for i in range(Q):
    q =  [ i for i in input().split(' ')]
    if q[0] == '1':
        if len(S) != 1:
            head = S[0]
            tail = S[-1]
            S = S.lstrip(head).rstrip(tail)
            S = tail + S + head
    elif 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())
jun = 1
f = t = ""
for _ in range(q):
  
  tmp = input()
  if len(tmp)==1:
    jun = 1 if jun==0 else 0 
  else:
    _,f,c = tmp.split()
    if jun+int(f) ==2:
      f = c+f
    else:
      t += c
if jun==1:
  print(f+s+t)
else:
  print((f+s+t)[::-1])

Submission status: Wrong Answer

Solution code:
import sys
import os


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

    ret = [sys.stdin.readline().rstrip()]
    Q = int(sys.stdin.readline().rstrip())

    for q in range(Q):
        line = sys.stdin.readline().split()
        if line[0] == '1':
            ret.reverse()
        elif line[0] == '2' and line[1] == '1':
            ret.insert(0, line[2])
        else:
            ret.append(line[2])

    print("".join(ret))



if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

S = input()
Q = int(input())
A = [(input().split()) for _ in range(Q)]

for i in range(Q):
  if A[i] == ["1"]:
    S = ''.join(list(reversed(S)))
  else:
    if A[i][1] ==1:
      S = S + A[i][2]
    else:
      S = A[i][2] + S
      
print(S)

Submission status: Wrong Answer

Solution code:
import sys
input = lambda: sys.stdin.readline().strip()

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

rev = False

first = ""
last = ""

for i in range(Q):
    q = input().split()
    m = int(q[0])
    if m == 1:
        rev = not rev
    else:
        fs = int(q[1])
        s = q[2]
        if fs == 1:
            if rev:
                last += s
            else:
                first += s
        else:
            if rev:
                first += s
            else:
                last += s
if rev:
    print(last[::-1]+ S + first)
else:
    print(first[::-1] + S + last)

Submission status: Wrong Answer

Solution code:
s= input()
q= int(input())
inv = False
head = []
tail = []
for i in range(q):
    p = input()
    if p[0]=='1':
        if inv:inv=False
        else:inv=True
    elif p[0]=='2':
        _,f,c= p.split()
        if f=='1':
            if inv:head.append(c)
            else:tail.append(c)
        elif f=='2':
            if inv:tail.append(c)
            else:head.append(c)
news = ''.join(head) + s + ''.join(tail)
if inv:print(news)
else:print(news[::-1])

Submission status: Wrong Answer

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

for x in q:
    if x[0]=='1':
        l = len(s)-1
        a = s[0]
        b = s[l]
        s = b + s[1:l] + a
    elif x[0]=='2':
        if x[1]=='1':
            s = x[2] + s
        elif x[1]=='2':
            s += x[2]
print(s)