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:
def main():
    s = input()
    q = int(input())
    r = s[::-1]
    buffer_first = ""
    buffer_last = ""
    for i in range(q):
        query = input().split()
        if query[0] == "1":
            s = buffer_first[::-1] + s + buffer_last
            r = buffer_last[::-1] + r + buffer_first
            s,r = r,s
            buffer_first = ""
            buffer_last = ""
        else:
            if query[1] == "1":
                buffer_first += query[2]
            if query[1] == "2":
                buffer_last += query[2]
    s = buffer_first + s + buffer_last
    r = buffer_last + r + buffer_first
    print(s)



if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
from collections import deque
def main():
  S = input()
  Q = int(input())
  r = False
  ans = deque(S)
  for l in sys.stdin:
    TFC = l.split()
    if TFC[0]=="1": r = not r
    elif TFC[1]=="1":
      if r: ans.append(TFC[2])
      else: ans.appendleft(TFC[2])
    elif TFC[1]=="2":
      if r: ans.appendleft(TFC[2])
      else: ans.append(TFC[2])
  print("".join(S[::-1] if r else S))

if __name__ == "__main__":
  main()

Submission status: Wrong Answer

Solution code:
from collections import deque


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

ans = deque()
ans.append(s)

for i in op:
    if i[0] == "1":
        ans.reverse()
    if i[0] == "2":
        if i[1] == "1":
            ans.appendleft(i[2])
        elif i[1] == "2":
            ans.append(i[2])

print("".join(ans))

Submission status: Wrong Answer

Solution code:
S = input()
Q = int(input())
bool = True
import sys
def input1():
	return sys.stdin.readline()[:-1]
for i in range(Q-1):
	s = input1()
	if len(s) == 1:
		bool = not bool
	else:
		t = tuple(s.split(" "))
		print(t)
		if t[1] == "1":
			if bool:
				S = t[2] + S
			else:
				S += t[2]
		else:
			if bool:
				S += t[2]
			else:
				S = t[2] + S

s = input()
if len(s) == 1:
	bool = not bool
else:
	if s[2] == "1":
		if bool:
			S = s[-1] + S
		else:
			S += s[-1]
	else:
		if bool:
			S += s[-1]
		else:
			S = s[-1] + S
if bool:
	print(S)
else:
	print(S[::-1])

Submission status: Wrong Answer

Solution code:
I = input
s = I()
q = int(I())
a = [], []
i = 0
for _ in range(q):
  x = I()
  if x[0] == '1':
    i ^= 1
  else:
    f = int(x[2]) - 1
    a[i ^ f].append(x[4])
print(''.join(reversed(a[i])) + s + ''.join(a[i^1]))

Submission status: Wrong Answer

Solution code:
S =input()
Q = int(input())
T = [0]*Q
for i in range(0,Q):
  T[i] = list(map(str, input().split()))

check = 0
for i in reversed(range(0,Q)):
  if(T[i][0] != 1):
    check = i
    break

  
  
nS = ""
for i in range(0,check):
  if(i != Q-1 and T[i][0] == "1" and T[i+1][0] == "1"):
    T[i][0] = 0
    T[i+1][0] = 0
  elif(T[i][0] == "2" and T[i][1] == "1"):
      S = T[i][2]+S
  elif(T[i][0] == "2" and T[i][1] =="2"):
      S = S+T[i][2]
  elif(T[i][0] == "1"):
    S = S[::-1]
  elif(T[i][0] == 0):
    pass

for i in range(check, Q):
  if(i != Q-1 and T[i][0] == "1" and T[i+1][0] == "1"):
    T[i][0] = 0
    T[i+1][0] = 0
  elif(T[i][0] == "1"):
    S = S[::-1]
  elif(T[i][0] == 0):
    pass


print(S)

Submission status: Wrong Answer

Solution code:
S = input()
Q = list(map(int,input().split()))[0]

S1=S
S2=S
flg=1
count=0
for i in range(Q):
    Query = input().split()
    if Query[0]=='1':
        flg*=-1
        count+=1
        continue
    if flg==1:
        if Query[1]=='1':
            S2+=Query[2]
        else:
            S1+=Query[2]
    else:
        if Query[1]=='1':
            S1+=Query[2]
        else:
            S2+=Query[2]
# print(S1, S2)
if count%2==1:
    print((S1[::-len(S)])[:-1]+S2)
else:
    print((S2[::-len(S)])[:-1]+S1)

Submission status: Wrong Answer

Solution code:
from collections import deque
S=input()
N=int(input())
Q=[input() for i in range(N)]
T=deque(S)
for i in range(N):
    if Q[i][0]=="2":
        if Q[i][2]=="1":
            T.appendleft(Q[i][4])
            S=list(T)
        if Q[i][2]=="2":
            T.append(Q[i][4])
            S=list(T)
    if Q[i][0]=="1":
        S=list(T)
        S=S[::-1]
        T=deque(S)
print(str(S))

Submission status: Wrong Answer

Solution code:
def d():
    S = str(input())
    Q = int(input())

    for i in range(Q):
        # print(S)
        q = list(input().split())

        if q[0] == '1':
            if len(S) > 1:
                S = S[-1] + S[1:-1] + S[0]
            else:
                pass
        elif q[0] == '2':
            if q[1] == '1':
                S = q[2] + S
            elif q[1] == '2':
                S = S + q[2]
            else:
                raise ValueError(
                    'F({}) is {}, which is invalid!'.format(i, q[1]))
        else:
            raise ValueError('T({}) is {}, which is invalid!'.format(i, q[0]))

    print(''.join(S))


##########
d()

Submission status: Wrong Answer

Solution code:
from collections import deque
s = deque(input())
q = int(input())
r = 0
z = 0
to = 0
for i in range(q):
    
    try:
        t,f,c = input().split()
        if (r + int(f)) % 2 == 1:
            s.appendleft(c)
    
        else:
            s.append(c)
        r = 0
        z = 1
    except:
        if z == 0:
            pass
        else:
            r += 1
            to += 1
        z = 1
        
if to % 2 == 1:
    s = reversed(s)
print("".join(s))

Submission status: Wrong Answer

Solution code:
# ANSHUL GAUTAM
# IIIT-D

from math import *
from copy import *
from string import *				# alpha = ascii_lowercase
from random import *				# l.sort(key=lambda l1:l1[0]-l1[1]) => ex: sort on the basis difference
from sys import stdin
from sys import maxsize
from operator import *				# d = sorted(d.items(), key=itemgetter(1))
from itertools import *
from collections import Counter		# d = dict(Counter(l))
from collections import defaultdict # d = defaultdict(list)

'''

'''

def solve(l):
	n = len(l)
	

S = input()
a,b = S,S[::-1]
Q = int(stdin.readline())
zz = 0
s1,s2 = '',''
curr = 0
for _ in range(Q):
	l = list(map(str, stdin.readline().rstrip().split()))
	if(l[0] == '1'):
		a,b = b,a
		zz += 1
		curr += 1
	else:
		q,f,c = l
		f = int(f)
		# print('curr:',curr)
		if(curr&1 == 0):
			b += c
			# a = c + a
			s1 += c
		else:
			a += c
			# b = c + b
			s2 += c
	# print("*",a,b)
# print(a,b)
# print(s1,s2)
if(curr&1 == 1):
	print(a)
else:
	print(b[::-1])
# for i in b:
# 	print(i)

Submission status: Wrong Answer

Solution code:
def main():
    S = input()

    ans = [S]
    for i in range(int(input())):
        tmp = input()
        if len(tmp)==1:
            ans.reverse()
        else:
            t,f,c = map(str, tmp.split())
            if f == '1':
                ans = [c] + ans
            else:
                ans.append(c)
    print(''.join(ans))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

def revers(s, cnt):
    if(cnt % 2):
        s_copy = ""
        for j in range(len(s)):
            s_copy = s_copy + s[(len(s) - 1) - j]
        s = s_copy
        
    cnt = 0
    return s
        
def two_work(s, prototype):
    s = str(s)
    num, f_num, c =  prototype.split()
        
    if(f_num == '1'):
        s = c + s
    else:
        s = s + c
        
    return s



cnt = 0
for i in range(q - 1):
    prototype = input()
    if prototype == '1':
        cnt += 1
    else:
        #反転判定
        s = revers(s, cnt)
        cnt = 0
        #2の操作
        s = two_work(s, prototype)

prototype = input()
if prototype == "1":
    cnt += 1
    s = revers(s, cnt)
else:
    s = two_work(s, prototype)

print(s)

Submission status: Wrong Answer

Solution code:
S = list(input().split())
q = int(input())
rev_cnt = 0
for ind in range(q):
    cur_list = list(input().split())
    #print(cur_list)
    if int(cur_list[0]) == 1:
        rev_cnt += 1
    else:
        f = int(cur_list[1])
        c = cur_list[2]
        if (rev_cnt + f) % 2 == 0:
            S = S + [c]
        else:
            S = [c] + S         
if rev_cnt %2 == 1:
    S.reverse()
print("".join(S))

Submission status: Wrong Answer

Solution code:
S = input()
Q = int(input())
r = False
F = []
B = []
for i in range(Q):
    q = input()
    if q[0] == "1":
        if r == False:
            r = True
        else:
            r = False
    elif q[2] == "1":
        if r == False:
            F.append(q[4])
        else:
            B.append(q[4])
    else:
        if r == False:
            B.append(q[4])
        else:
            F.append(q[4])
if r == False:
    S = str(F[::-1]) + str(S) + str(B)
else:
    S = str(B[::-1]) + str(S[::-1]) + str(F)
print(str(S))

Submission status: Wrong Answer

Solution code:
from collections import deque
d=deque()
s=input()
d.append(s)
Q=int(input())
for i in range(Q):
    r=input()
    if len(r.split()) == 3:
        T,F,C=r.split()
    else:
        T='1'
    if T == '1':
        s=d.pop()
        d.rotate(1)
        d.append(s)
        continue
    if T == '2':
        if F == '1':
            d.appendleft(C)
        if F == '2':
            d.append(C)

print("".join(d))

Submission status: Wrong Answer

Solution code:
S = input()
Q = int(input())
ARR = []

mrr = list([])
for i in range(Q):
    arr = input().split(" ")

    mrr.append(int(arr[0]))
    ARR.append(arr)

reverseTotal = mrr.count(1)

def calculate(s,q,arr,reverseTotal):

    for ar in arr:
        if int(ar[0]) == 1:
            reverseTotal = reverseTotal - 1

        if int(ar[0]) == 2:

            if int(ar[1]) == 1:
                if reverseTotal % 2 == 0:
                    s = ar[2] + s
                else:
                    s = s + ar[2]
            else:
                if reverseTotal % 2 == 0:
                    s = s + ar[2]
                else:
                    s = ar[2] + s

    print(s)



calculate(S,Q,ARR,reverseTotal)

Submission status: Wrong Answer

Solution code:
init = input()
num = int(input())

_strings = [''] * (num * 2 + 1)
_strings[num] = init

isReverse = False

queue = []

fi = num - 1
li = num + 1

i = 0
while i < num:
    query = input().split()
    type = int(query[0])

    if type == 1:
        # _strings.reverse()
        isReverse = not isReverse
    else:
        mode = int(query[1])

        if mode == 1 or (mode == 2 and isReverse):
            _strings[fi] = str(query[2])
            fi -= 1
            # _strings.insert(0, str(query[2]))
        else:
            _strings[li] = str(query[2])
            li += 1
            # _strings.append(str(query[2]))
    i += 1

if isReverse:
    _strings.reverse()

print(''.join(_strings))

Submission status: Wrong Answer

Solution code:
S = list(input())
Q = int(input())
S_r = []
S_l = []

n = 0

for i in range(Q):
  q = list(input().split())
  if int(q[0]) == 1:
    n += 1
  else:
      if int(q[1]) == 1:
        S_l.append(q[2])
        S_r = S
        n += 1
        break
      else:
        S_r.append(q[2])
        S_l = S
        n += 1
        break

  if i == Q-1:
    print(''.join(S_l[::-1]), end="")
    print(''.join(S), end="")
    print(''.join(S_r))

for i in range(Q - n):
  q = list(input().split())
  if int(q[0]) == 1:
    temp = S_r[-1]
    S_r[-1] = S_l[-1]
    S_l[-1] = temp
  else:
    if int(q[1]) == 1:
      S_l.append(q[2])
    else:
      S_r.append(q[2])

print(S_l)
print(S_r)
print(''.join(S_l[::-1]), end="")
print(''.join(S_r))

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-

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

op1_cnt = 0
for i in range(Q):
    query = input()
    if len(query) == 1:
        op1_cnt += 1
    else:
        if op1_cnt % 2 != 0 and len(S) != 1:
            S = ''.join([S[-1], S[1:-1], S[0]])
        op1_cnt = 0
        t, F, C = query.split()
        if F == "1":
            S = ''.join([C, S])
        else:
            S = ''.join([S, C])

if op1_cnt % 2 != 0 and len(S) != 1:
    S = ''.join([S[-1], S[1:-1], S[0]])

print(S)

Submission status: Wrong Answer

Solution code:
s = input()
n = input()
for i in range(int(n)-1):
    tmp = input()
    print(tmp)
    if len(tmp) == 1:
        s = s[::-1]
    else:
        t, f, c = tmp.split()
        if int(f) == 1:
            s = c + s
        else:
            s = s + c
print(s)

Submission status: Wrong Answer

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

rev = False
for i in range(Q):
  q = input()
  if q[0] == '1':
    rev = not rev
  else:
    if q[2] == '1' and rev or q[2] == '2' and not rev:
      S.append(q[4])
    else:
      S.insert(0, q[4])
      
print(S[::-1] if rev else S)

Submission status: Wrong Answer

Solution code:

from collections import deque

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

DQ = deque()
DQ.appendleft(S)


for i in range(Q):
    # q = list(input().split())
    q = deque(input().split())
    

    if (len(q) == 1):
        # S = S[::-1]
        DQ.reverse()
    else:
        cha = q.pop()
        judge = q.pop()
        
        if (int(judge) == 1):
            DQ.appendleft(cha)
        else:
            DQ.append(cha)


ans = list(DQ)
print(''.join(ans))

Submission status: Wrong Answer

Solution code:
S = input()
Q = list(map(int,input().split()))[0]

S1=S
S2=S
flg=1
count=0
for i in range(Q):
    Query = input().split()
    if Query[0]=='1':
        flg*=-1
        count+=1
        continue
    if flg==1:
        if Query[1]=='1':
            S2+=Query[2]
        else:
            S1+=Query[2]
    else:
        if Query[1]=='1':
            S1+=Query[2]
        else:
            S2+=Query[2]
print(S1, S2)
if count%2==1:
    print((S1[::-len(S)])[:-1]+S2)
else:
    print((S2[::-len(S)])[:-1]+S1)

Submission status: Wrong Answer

Solution code:
from collections import deque
S = deque()
S.append(input())
N = int(input())
X = []
for i in range(N):
    X.append(input())

for i in range(N):
    if len(X[i]) == 1:
        S.reverse()
    else:
        Y = list(X[i].split())
        if int(Y[1]) == 1:
            S.appendleft(Y[2])
        else:
            S.append(Y[2])

print(''.join(S))

Submission status: Wrong Answer

Solution code:
s=input()
q=int(input())
flg = 0
s1 = s
s2 = s

for i in range(q):
    tmp = input()
    if tmp == '1':
        if flg == 0:
            flg = 1
        else:
            flg = 0
    else:
        t1,t2,t3 = tmp.split()
        if int(t2) == 1:
            if flg == 0:
                s1 = t3 + s1
                s2 = s2 + t3
            else:
                s1 = s1 + t3
                s2 = t3 + s2
        if int(t2) == 2:
            if flg == 0:
                s1 = s1 + t3
                s2 = t3 + s2
            else:
                s1 = t3 + s1
                s2 = s2 + t3

if flg == 1:
    print(s2)
print(s1)

Submission status: Wrong Answer

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

t = [0] * q
f = [0] * q
c = [""] * q

for i in range(q):
    lis = list(input().split())
    t[i] = int(lis[0])
    if t[i] == 2:
        f[i] = int(lis[1])
        c[i] = lis[2]

idx = 0
chs = ""
flip = False
ins = 0
while idx < q:
    if t[idx] == 1:
        flip = not flip
        while idx + 1 < q and t[idx + 1] == 1:
            idx += 1
            flip = not flip
        if flip:
            chs = chs[::-1]
            ins = len(chs) - ins
    else:
        if f[idx] == 1:
            ch = c[idx]
            while idx + 1 < q and t[idx + 1] == 2 and f[idx + 1] == 1:
                idx += 1
                ch = c[idx] + ch
            chs = ch + chs
            ins += len(ch)
        else:
            ch = c[idx]
            while idx + 1 < q and t[idx + 1] == 2 and f[idx + 1] == 2:
                idx += 1
                ch = ch + c[idx]
            chs = chs + ch
    idx += 1

ans = chs[:ins]
ans += s[::-1] if flip else s
ans += chs[ins:]
print(ans)

Submission status: Wrong Answer

Solution code:
from collections import deque

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

conr = 0

for i in range(Q):
  q = list(input().split())
  if q[0] == "1":
    conr += 1
  else:
    s = q[-1]
    f = q[1]
    if f == "1" and conr % 2 == 0:
      D.appendleft(s)
      C.append(s)
    elif f == "1" and conr % 2 == 1:
      D.append(s)
      C.appendleft(s)
    elif f == "2" and conr % 2 == 0:
      D.append(s)
      C.appendleft(s)
    else:
      D.appendleft(s)
      C.append(s)


if conr % 2 == 0:
  ans = "".join(D)
else:
  ans = "".join(C)

print(ans)

Submission status: Wrong Answer

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

s = deque(input().rstrip())

nq = int(input().rstrip())
for _ in range(nq):
    query = input().rstrip()
    if query[0] == '1':
        if s[0] == s[-1]:
            continue
        s[-1], s[0] = s[0], s[-1]
    else:
        c = query[4]
        if query[2] == '1':
            s.appendleft(c)
        else:
            s.append(c)
print("".join(s))

Submission status: Wrong Answer

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

r = 0
sort = 0
for i in range(q):
  List=list(input().split())
  if len(List) > 1 :
    if List[1] == '1' and r == 0 :
      s = List[2] + s 
    elif List[1] == '1' and r == 1 :
      s = s + List[2] 
    elif List[1] == '2' and r == 0 :
      s = s + List[2] 
    elif List[1] == '2' and r == 1 :
      s = List[2] + s 
    r = 0
  else:
    
    if r == 1 :
      r=0
      sort += 1
    elif len(s) > 0 and r == 0:
      r=1
      sort += 1



ans = ''
if sort % 2 ==1:
  for j in range(len(s)):
    ans += s[len(s)-j-1:len(s)-j]
else:
  ans = s

print(ans)

Submission status: Wrong Answer

Solution code:
s=str(input())
q=int(input())
a=[input()for i in range(q)]
b=0
for i in range(q):
    if a[i]=="1":
        if b==0:
            b=1
        else:
            b=0
    else:
        if (b==0 and a[i][2]==1) or (b==1 and a[i][2]==2):
            s=a[i][4]+s
        else:
            s=s+a[i][4]
if b==1:
    print(s)
    exit()
s= ''.join(list(reversed(s)))
print(s)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
def q1(s):
    #reverse
    return s[::-1]
def q2(s,f,char):
    if int(f) == 1:
        s = char + s
    else:
        s= s + char
    return s
 

s = input()
q = int(input())
for i in range(q):
    query = input().split()
    if len(query)==1:
        s=q1(s)
    else:
        print(query)
        s=q2(s,query[1],query[2])
print(s)

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)

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


def solve2():
    S = list(input().split())
    Q = int(input())
    M = 2*10**5+10
    data = [None]*M
    data.extend(S)
    data.extend([None]*M)
    left, right = M-1, M+len(S)

    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を追加
                    data[right] = c
                    right += 1
                else:
                    # 先頭にcを追加
                    data[left] = c
                    left -= 1
            else:
                if is_reverse:
                    # 先頭に追加
                    data[left] = c
                    left -= 1
                else:
                    # 末尾に追加
                    data[right] = c
                    right += 1

    # 出力
    if is_reverse:
        for i in range(right-1, left, -1):
            print(data[i], end="")
    else:
        for i in range(left+1, right):
            print(data[i], end="")
    print()


if __name__ == "__main__":
    solve()

Submission status: Wrong Answer

Solution code:
s = input()
Q = int(input())
count = 0
right = []
left = []
for i in range(Q):
    q = input().split()
    if int(q[0]) == 1:
        count += 1
    else:
        if count%2 == 0:
            if int(q[1]) == 1:
                left.append(q[2])
            else:
                right.append(q[2])
        else:
            if int(q[1]) == 1:
                right.append(q[2])
            else:
                left.append(q[2])

left = "".join(left)
right = "".join(right)
if count%2 == 0:
    left = left[::-1]
    print(left+s+right)
else:
    s = s[::-1]
    print(right+s+left)

Submission status: Wrong Answer

Solution code:
import sys

stdin = sys.stdin

ns = lambda : stdin.readline().rstrip()
ni = lambda : int(ns())

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

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

                s = T[2] + s
    
    if T1 % 2 == 0:
        print(s)
    else:
        print(s[::-1])

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

S = input().rstrip()
Q = int(input())
query = [list(input().split()) for _ in range(Q)]

ans = S
flag = True

for i in range(Q):
    if query[i][0] == '1':
        if flag:
            flag = False
        else:
            flag = True
        continue
    elif query[i][0] == '2':
        f = query[i][1]
        c = query[i][2].rstrip()
        if f == '1' and flag:
            ans = c + ans
        elif f == '1' and not flag:
            ans = ans + c
        elif f == '2' and flag:
            ans = ans + c
        elif f == '2' and not flag:
            ans = c + ans
print(ans)

Submission status: Wrong Answer

Solution code:
# coding: utf-8
import sys
sys.setrecursionlimit(4100000)
input = sys.stdin.readline

ans = str(list(input())[0])
n = int(list(input())[0])
r = 2

#print(ans)
#print(n)
for i in range(n) :
    x = list(input())
    #print(x)
    if x[0] == "1" :
        r = r + 1
    else :
        pp = int(x[2])
        if r % 2 == 1 :
            pp = pp + 1
        if pp % 2  == 1 :
            ans = x[4] + ans
        else :
            ans = ans + x[4]

if r % 2 == 1 :
    ans = ans[::-1]

print(ans)

Submission status: Wrong Answer

Solution code:
S=str(input())
Q=int(input())
for i in range(Q):
    T=str(input().split())
    if T[0]=="1":
        S.reverse()
    elif T[0]=="2":
        if T[1]=="1":
            S=T[2]+S
        elif T[1]=="2":
            S=S+T[2]
print(S)

Submission status: Wrong Answer

Solution code:
def main():
    s = input()
    q = int(input())
    r = s[::-1]
    buffer_first = ""
    buffer_last = ""
    for i in range(q):
        query = input().split()
        if query[0] == "1":
            s = buffer_first + s + buffer_last
            r = buffer_last + r + buffer_first
            s,r = r,s
            buffer_first = ""
            buffer_last = ""
        else:
            if query[1] == "1":
                buffer_first += query[2]
            if query[1] == "2":
                buffer_last += query[2]
    s = buffer_first + s + buffer_last
    r = buffer_last + r + buffer_first
    print(s)



if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
#coding:utf-8
import sys,os
sys.setrecursionlimit(10**6)
write = sys.stdout.write
dbg = (lambda *something: print(*something)) if 'TERM_PROGRAM' in os.environ else lambda *x: 0
def main(given=sys.stdin.readline):
    input = lambda: given().rstrip()
    LMIIS = lambda: list(map(int,input().split()))
    II = lambda: int(input())
    XLMIIS = lambda x: [LMIIS() for _ in range(x)]
    YN = lambda c : print('Yes') if c else print('No')
    MOD = 10**9+7
    S = input()
    Slist = [S]
    Q = II()
    Slist = ['']*Q+Slist+['']*Q
    left = Q-1
    right = Q+1
    r = False
    for _ in range(Q):
        query = input().split()
        if query[0] == '1':
            r ^= True
        else:
            if query[1] == '1':
                    if r:
                        # S = ''.join([S,query[2]])
                        Slist[right] = query[2]
                        right += 1

                    else:
                        # S = ''.join([query[2],S])
                        Slist[left] = query[2]
                        left -=1
            else:
                if r:
                    Slist[left] = query[2]
                    left -=1
                else:
                    # S = ''.join([S,query[2]])
                    Slist[right] = query[2]
                    right += 1
    # print(S[::-1] if r else S)
    print(''.join(Slist[::-1]) if r else ''.join(Slist))
                



    

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

A = ""
B = ""
count = 0

Que = []
ope = []

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

for i in range(Q):
    k = input()
    if k == "1":
        count += 1
        Que.append("1")
        ope.append("0")
    else:
        Que.append("2")
        ope.append([k[2],k[4]])

cou = 0

if count % 2 == 0:
    for i in range(Q):
        if Que[i] == "1":
            cou += 1
        else:
            if (int(ope[i][0]) + cou) % 2 == 1:
                A = ope[i][1] + A
            else:
                B = B + ope[i][1]
else:
    for i in range(Q):
        if Que[i] == "1":
            cou += 1
        else:
            if (int(ope[i][0]) + cou) % 2 == 1:
                A = A + ope[i][1]
            else:
                B = ope[i][1] + B

if count % 2 == 0:
    S = A + S + B
else:
    S = rev(S)
    S = B + S + A

print(S)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
def string_formation(s, q_list):
    ans = []
    for x in s:
        ans.append(x)
    for query in q_list:
        if query[0] == '1':
            ans[0], ans[-1] = ans[-1], ans[0]
        else:
            if query[1] == '1':
                ans = [query[2]] + ans
            else:
                ans = ans + [query[2]]
    print(''.join(ans))
    
s = input()
q = int(input())
q_list = []
for _ in range(q):
    tmp = input().split()
    q_list.append(tmp)

string_formation(s, q_list)

Submission status: Wrong Answer

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

if len(s) >= 3:
  s1 = list(s[0])
  s2 = list(s[len(s)-1])
  s = s[1:len(s)-1]
elif len(s) == 1:
  s1 = list(s)
  s2 = []
  s = ''
else:
  s1 = list(s[0])
  s2 = list(s[1])
  s = ''
  
for q in range(Q):
  query = input().split()
  if len(query) == 1:
    if s1 and s2:
      s1[-1], s2[-1] = s2[-1], s1[-1]
    else:
      s1[0],s1[-1] = s1[-1],s1[0]
  else:
    T,F,c = list(query)
    if F == '1':
      s1.append(c)
    else:
      s2.append(c)
      
print(''.join(s1[::-1]) + s + ''.join(s2))

Submission status: Wrong Answer

Solution code:
s = list(input())
q = int(input())
i = 0
for i in range(q):
    arr = list(input().split(' '))
    if int(arr[0]) == 2:
        f, c = int(arr[1]), arr[2]
        if f == 1:
            s.insert(0, c)
        else:
            s.append(c) 
    else:
        s[0], s[-1] = s[-1], s[0]
    i += 1

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

Submission status: Wrong Answer

Solution code:
def main(s,end,start,perse):
    start.reverse()
    start.append(''.join(s))
    start.append(''.join(end))
    print(start)
    print(''.join(start))
def conv(s):
    a = []
    for i in range(len(s)):
        a.append(s[i])
    return a
perse = 1
start = []
end = []
  
s = input()
q = int(input())
for i in range(q):
    query = input().split()
    query[0] = int(query[0])
    if query[0] == 1:
        perse = -perse
    else:
        query[1] = int(query[1])
        if (int(query[1])*2-3)*perse == 1:
            end.append(query[2])
        else:
            start.append(query[2])
s =  conv(s)
if perse == -1:
    start.reverse()
    s.reverse()
    end.reverse()
main(s,end,start,perse)