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
s = input()
d = deque()
d.append(s)
n = int(input())
count = 0
for i in range(n):
    j = list(input().split())
    if j[0] == '1':
        count+=1
    else:
        if (count + int(j[1])) % 2 == 1:
            d.appendleft(j[2])
        else:
            d.append(j[2])
ans = ""
if count % 2 == 1:
    while d:
        ans += d.pop()
else:
    while d:
        ans += d.popleft()
print(ans)

Submission status: Wrong Answer

Solution code:

s=input()
q=int(input())
p=0
a=""
b=""
for i in range(q):
    l=list(map(str,input().split()))
    if l[0]=="1":
        p=(p+1)%2
    else:
        if p==0:
            if l[1]=="1":
                b+=l[2]
            else:
                a+=l[2]
        else:
            if l[1]=="1":
                a+=l[2]
            else:
                b+=l[2]
if p==0:
    print(b[::-1]+s[::-1]+a)
else:
    print(a[::-1]+s+b)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
from collections import deque
Q=deque()
a=input()
Q.append(a)
q=int(input())
for i in range(q):
    d=input()
    if d[0]=="1": Q.reverse()
    elif d[0]=="2" and d[2]=="1":
        Q.appendleft(d[4:])
    else:
        Q.append(d[4:])
ans="".join(list(Q))
print(ans)

Submission status: Wrong Answer

Solution code:
from collections import deque
s=input()
Q=int(input())
hanten = 0
mae = deque()
ushiro = deque()
for i in range(Q):
	q=input()
	if q=="1":
		hanten ^= 1
	else:
		if q[2] == "1":
			if hanten == 0:
				mae.append(q[4])
			else:
				ushiro.append(q[4])
		else:
			if hanten == 0:
				ushiro.appendleft(q[4])
			else:
				mae.appendleft(q[4])
if hanten == 0:
	print("".join(mae) + s + "".join(ushiro))
else:
	print(("".join(mae) + s + "".join(ushiro))[::-1])

Submission status: Wrong Answer

Solution code:
from collections import deque
s=input()
Q=int(input())
hanten = 0
mae = deque()
ushiro = deque()
for i in range(Q):
	q=input()
	if q=="1":
		hanten ^= 1
	else:
		if q[2] == "1":
			if hanten == 0:
				mae.appendleft(q[4])
			else:
				ushiro.append(q[4])
		else:
			if hanten == 0:
				ushiro.appendleft(q[4])
			else:
				mae.append(q[4])
if hanten == 0:
	print("".join(mae)[::-1] + s + "".join(ushiro))
else:
	print("".join(ushiro)[::-1] + s +"".join(mae))

Submission status: Wrong Answer

Solution code:
s=input()
Q=int(input())
hanten = 0
mae = []
ushiro = []
for i in range(Q):
	q=input()
	if q=="1":
		hanten ^= 1
	else:
		if q[2] == "1":
			if hanten == 0:
				mae.append(q[4])
			else:
				ushiro.append(q[4])
		else:
			if hanten == 0:
				ushiro.append(q[4])
			else:
				mae.append(q[4])
if hanten == 0:
	print("".join(mae)[::-1] + s + "".join(ushiro))
else:
	print("".join(ushiro)[::-1] + s +"".join(mae))

Submission status: Wrong Answer

Solution code:
import sys


def input(): return sys.stdin.readline().strip()
def I(): return int(input())
def LI(): return list(map(int, input().split()))
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def S(): return input()
def LS(): return input().split()


s = S()
q = I()
queries = SR(q)
for query in queries:
    if query[0] == '1':
        if len(s) <= 1:
            continue
        s = s[-1] + s[1:-1] + s[0]
    else:
        _, f, c = query.split()
        if f == '1':
            s = c + s
        else:
            s = s + c
print(s)

Submission status: Wrong Answer

Solution code:
import sys


def input(): return sys.stdin.readline().strip()
def I(): return int(input())
def LI(): return list(map(int, input().split()))
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def S(): return input()
def LS(): return input().split()


s = S()
q = I()
queries = SR(q)
for query in queries:
    print(query[0])
    if query[0] == '1':
        if len(s) <= 1:
            continue
        s = s[-1] + s[1:-1] + s[0]
    else:
        _, f, c = query.split()
        if f == '1':
            s = c + s
        else:
            s = s + c
print(s)

Submission status: Wrong Answer

Solution code:
import sys


def input(): return sys.stdin.readline().strip()
def I(): return int(input())
def LI(): return list(map(int, input().split()))
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def S(): return input()
def LS(): return input().split()


s = S()
q = I()
queries = SR(q)
for query in queries:
    print(query[0])
    if query[0] == '1':
        s = s[-1] + s[1:-1] + s[0]
    else:
        _, f, c = query.split()
        if f == '1':
            s = c + s
        else:
            s = s + c
print(s)

Submission status: Wrong Answer

Solution code:
from collections import deque
t=list(input())
q=int(input())
query=deque([])
cnt=0
for i in range(q):
  s=list(map(str,input().split()))
  if s[0]=='1':
    cnt+=1
  query.append(s)

#print(query)
flag='left'
while query:
  p=query.popleft()
  if len(p)==1:
    flag='right'
    
  else:
    if flag=='left':
      if p[1]==str(1):
        t.insert(0,p[2])

      else:
        t.append(p[2])
        
    else:
      if p[1]==str(1):
        t.append(p[2])

      else:
        t.insert(0,p[2])
      
if cnt%2==0:
  print(''.join(t))

  
else:
  print(''.join(t[::-1]))

Submission status: Wrong Answer

Solution code:
from collections import deque
t=list(input())
q=int(input())
query=deque([])
for i in range(q):
  s=list(map(str,input().split()))
  query.append(s)
#print(query)
while query:
  p=query.popleft()
  if len(p)==1:
    t[::-1]
    
  else:
    if p[1]==str(1):
      t.insert(0,p[2])
      
    else:
      t.append(p[2])
      
print(''.join(t))

Submission status: Wrong Answer

Solution code:
s = input()
q = int(input())
a = []
# for j in range(q):
#     a.extend([list(map(str,input().split()))])
# # print(a)
# for i in a:
pre = ""
nex = ""
c = 1
for j in range(q):
    i = list(map(str,input().split()))
    if i ==["1"]:
        c *= -1
        # s = s[::-1]
    elif i[0]=="2" and i[1]=="1":
        # s = i[2]+s
        if c == 1:
            pre=i[2]+pre
        else:
            nex = nex + i[2]
    elif i[0]=="2" and i[1]=="2":
        # s = s +i[2]
        if c ==1:
            nex = nex +i[2]
        else:
            pre = i[2]+pre
s = pre+s+nex
print(s)

Submission status: Wrong Answer

Solution code:
S = input()
Q = int(input())
x = 0
p = ''
q = ''
for i in range(Q):
    A = list(input().split())
    if A[0] == '1':
        x += 1
    elif x%2 ==0:
        if A[1] == '1':
            p = A[2] + p
        else:
            q = q + A[2]
    else:
        if A[1] == '1':
            q = q + A[2] 
        else:
            p = A[2] + p
if x%2 == 1:
    S = ''.join(reversed(list(S)))
print(S)

Submission status: Wrong Answer

Solution code:
S = input()
Q = int(input())
TFC = [input().split() for _ in range(Q)]
is_reversed = False
head = ''
tail = ''
for tfc in TFC:
    if tfc[0] == '1':
        is_reversed = not is_reversed
    else:
        if tfc[1] == '1':
            if is_reversed:
                tail += tfc[2]
            else:
                head += tfc[2]
        else:
            if is_reversed:
                head += tfc[2]
            else:
                tail += tfc[2]
if is_reversed:
    print(tail[::-1]+S+head)
else:
    print(head[::-1]+S+tail)

Submission status: Wrong Answer

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

for i in range(Q):
    if Query[i][0] == '1':
        order *= -1
    else:
        F = Query[i][1]
        C = Query[i][2]
        if F == '1':
            if order == 1:
                S = S + C
            else:
                S = C + S
        else:
            if order == 1:
                S = C + S
            else:
                S = S + C

print(S[::-order])

Submission status: Wrong Answer

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

s = deque(list(input()))
q = int(input())
rev = -1

for i in range(q):
    a = list(input().split())
    if a[0] == '1': #T1
        rev *= -1 
    
    else: #T2
        if a[1] == '1': #Fi = 1
            if rev == -1:
                s.appendleft(a[2])
            else:
                s.append(a[2])
                
        else: #Fi = 2
            if rev == -1:
                s.append(a[2])
            else:
                s.appendleft(a[2])
                
if rev == -1:
    print(''.join(s))
else:
    s = list(s)
    print(''.join(s[::-1]))

Submission status: Wrong Answer

Solution code:
S=input()
Q=int(input())
l=list(input().split() for i in range(Q))
for j in l:
  if j[0]=="1":
    S=S[::-1]
  else:
    if j[1]=="1":
      S=S+j[2]
    else:
      S=j[2]+S

print(S)

Submission status: Wrong Answer

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

for q in range(Q):
    inpu_ = input()
    if len(inpu_)==1:
        counter += 1
        if q==Q-1 and counter%2==1:
            S.reverse()
    else:
        a,f,c = map(str,inpu_.split())
        if f=="1":
            if counter%2 == 1:
                S.append(c)
                counter += 1
            else:
                S.insert(0,c)

        else:
            if counter%2==1:
                S.reverse()
                counter += 1
            S.append(c)


print("".join(S))

Submission status: Wrong Answer

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees#, log2
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10**9 + 7
#from decimal import *
 
S = input()
Q = INT()
q = deque(S)
flg = 1
for _ in range(Q):
	Que = input().split()
	if Que[0] == "1":
		flg *= -1
	else:
		_, F, C = Que
		if any(int(F)*flg == x for x in [2, -1]):
			q.append(C)
		else:
			q.appendleft(C)

print(q)
print(S)
ans = "".join(q)
if flg == -1:
	ans = ans[::-1]
print(ans)

Submission status: Wrong Answer

Solution code:
from collections import deque

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

q = deque()
rev = 1

for s in S:
  q.append(s)

for _ in range(Q):
  TFC = input()
  if len(TFC) == 1: # T == 1
    rev ^= 1
  else:
    T, F, C = (i for i in TFC.split())  
    if F == '2' and rev:
      q.append(C)
    else:
      q.appendleft(C)
      
ans = ''.join(q)

if rev:
  print(ans[::-1])
else:
  print(ans)

Submission status: Wrong Answer

Solution code:
from collections import deque

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

q = deque()
rev = 0

for s in S:
  q.append(s)

for _ in range(Q):
  TFC = input()
  if len(TFC) == 1: # T == 1
    rev ^= 1
  else:
    T, F, C = (i for i in TFC.split())  
    if F == '1' and rev:
      q.append(C)
    else:
      q.appendleft(C)
      
ans = ''.join(q)

if rev:
  print(ans[::-1])
else:
  print(ans)

Submission status: Wrong Answer

Solution code:
from collections import deque

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

q = deque()
rev = 0

for s in S:
  q.append(s)

for _ in range(Q):
  TFC = input()
  if len(TFC) == 1: # T == 1
    rev ^= 1
  else:
    T, F, C = (i for i in TFC.split())  
    if F == '1' and rev:
      q.append(C)
    else:
      q.appendleft(C)
      
if rev:
  q.reverse()

print(''.join(q))

Submission status: Wrong Answer

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

rev = 0
for _ in range(Q):
  TFC = input()
  if len(TFC) == 1: # T == 1
    rev ^= 1
  else:
    T, F, C = (i for i in TFC.split())  
    if F == '1' and rev:
      S = S + C
    else:
      S = C + S

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

Submission status: Wrong Answer

Solution code:
S=input()
Q=int(input())
reverseflag = 0
for _ in range(Q):
    newS = input()
    if int(newS[0]) == 1:
        reverseflag += 1
        reverseflag %= 2
    else:
        if (newS[2] == '1' and reverseflag==0) or (newS[2] != '1' and reverseflag!=0):
            S = newS[4].join(S[:])
        else:
            S = S[:].join(newS[4])
if reverseflag == 1:
    S = S[::-1]
print(S)

Submission status: Wrong Answer

Solution code:
S=input()
Q=int(input())
for _ in range(Q):
    newS = input()
    if int(newS[0]) == 1:
        S = S[-1]+S[1:-1]+S[0]
    else:
        if newS[2] == '1':
            S = newS[4]+S[:]
        else:
            S = S[:]+newS[4]
print(S)

Submission status: Wrong Answer

Solution code:
from collections import deque

S = input()
Q = int(input())
q = deque([S])
reverse = -1

for i in range(Q):
    X = input().split()
    if X[0] == "1":
        reverse *= -1
    # if X[1] == "1":
    elif X[1] == "1":
        if reverse == -1:
            q.appendleft(X[2])
        else:
            q.append(X[2])
    elif X[1] == "2":
        if reverse == -1:
            q.append(X[2])
        else:
            q.appendleft(X[2])
if reverse == 1: q.reverse()

for i in range(len(q)):
    print(q[i],end="")
print()

Submission status: Wrong Answer

Solution code:
s = list(input())
q = int(input())
qu = [[0] * 3] * q
flg = False
first = []
last = []
hanten = False

for i in range(q):
    qu[i] = input().split()

for i in range(q):
    if qu[i][0] == '1':
        flg = not flg
        hanten = not hanten
        continue

    else:
        hanten = False
        if (flg and qu[i][1] == '2') or (not flg and qu[i][1] == '1'):
            first.append(qu[i][2])

        else:
            last.append(qu[i][2])

if flg:
    s.extend(first)
    last.reverse()
    last.extend(s)
    if hanten:
        last.reverse
    print(''.join(last))
else:
    s.extend(last)
    first.reverse()
    first.extend(s)
    if hanten:
        first.reverse
    print(''.join(first))

Submission status: Wrong Answer

Solution code:
s = list(input())
q = int(input())
qu = [[0] * 3] * q
flg = False
first = []
last = []

for i in range(q):
    qu[i] = input().split()

for i in range(q):
    if qu[i][0] == '1':
        flg = not flg
        continue

    else:
        if (flg and qu[i][1] == '2') or (not flg and qu[i][1] == '1'):
            first.append(qu[i][2])

        else:
            last.append(qu[i][2])

if flg:
    s.extend(first)
    last.reverse()
    last.extend(s)
    print(''.join(last))
else:
    s.extend(last)
    first.reverse()
    first.extend(s)
    print(''.join(first))

Submission status: Wrong Answer

Solution code:
from collections import deque
import sys

S = input()
Q = int(input())
q = deque([S])
reverse = False

for i in range(Q):
    X = sys.stdin.readline().split()
    if X[0] == "1":
        if reverse:
            reverse = False
        else:
            reverse = True
    elif X[0] == "2":
        if reverse:
            X[1] = str(3-int(X[1]))
        if X[1] == "1":
            q.appendleft(X[2])
        else:
            q.append(X[2])

if reverse: q.reverse()

for i in range(len(q)):
    print(q[i],end="")
print()

Submission status: Wrong Answer

Solution code:
from collections import deque
import sys

S = input()
Q = int(input())
q = deque([S])
reverse = False

for i in range(Q):
    X = sys.stdin.readline().split()
    if X[0] == "1":
        if reverse:
            reverse = False
        else:
            reverse = True

    elif X[0] == "2":
        if X[1] == "1" and reverse==False:
            q.appendleft(X[2])
        elif X[1] == "1" and reverse==True:
            q.append(X[2])
        elif X[1] == "2" and reverse==False:
            q.append(X[2])
        else:
            q.appendleft(X[2])

if reverse: q.reverse()

for i in range(len(q)):
    print(q[i],end="")
print()

Submission status: Wrong Answer

Solution code:
S = input()
Q = int(input())
a = 0
b = 0
for i in range(Q):
  q = list(input().split())
  if q[0] == '1':
    a += 1
    b += 1
    
  elif (q[1] == '1' and a%2 == 0) or (q[1] == '2' and a%2 == 1):
    S= q[2]+S
    a = 0
    
    
  else:
    S = S+q[2]
    a = 0
    
if b%2 == 1 and len(S) > 1:
  S = S[::-1]
    

print(S)

Submission status: Wrong Answer

Solution code:
from collections import deque
import sys

S = input()
Q = int(input())
q = deque([S])

for i in range(Q):
    X = sys.stdin.readline().split()
    if X[0] == "1":
        q.reverse()
    elif X[0] == "2":
        if X[1] == "1":
            q.appendleft(X[2])
        else:
            q.append(X[2])

for i in range(len(q)):
    print(q[i],end="")
print()

Submission status: Wrong Answer

Solution code:
S = list(input())
Q = int(input())
a = 0
b = 0
for i in range(Q):
  q = list(input().split())
  if q[0] == '1':
      a += 1
      b += 1
    
  elif (q[1] == '1' and a%2 == 0) or (q[1] == '2' and a%2 == 1):
    S= [q[2]]+S
    a = 0
    
    
  else:
    S = S+[q[2]]
    a = 0
    
if b%2 == 1:
  S.reverse()
    
S="".join(S)
print(S)

Submission status: Wrong Answer

Solution code:
S = list(input())
Q = int(input())
a = 0
b = 0
for i in range(Q):
  q = list(input().split())
  if q[0] == '1':
    if len(S)>1:
      a += 1
      b += 1
    
  elif (q[1] == '1' and a%2 == 0) or (q[1] == '2' and a%2 == 1):
    S= [q[2]]+S
    a = 0
    
    
  else:
    S = S+[q[2]]
    a = 0
    
if b%2 == 1:
  S.reverse()
    
S="".join(S)
print(S)

Submission status: Wrong Answer

Solution code:
S = list(input())
Q = int(input())
a = 0
b = 0
c = 0
A = []
B = []
for i in range(Q):
  q = list(input().split())
  if q[0] == '1':
    if c>0:
      a += 1
      b += 1
    
  elif (q[1] == '1' and a%2 == 0) or (q[1] == '2' and a%2 == 1):
    A = [q[2]] + A
    a = 0
    c += 1
    
  else:
    B = B+[q[2]]
    a = 0
    c += 1
  
S = A + S + B
if b%2 == 1:
  S.reverse()
    
S="".join(S)
print(S)

Submission status: Wrong Answer

Solution code:
S = list(input())
Q = int(input())
a = 0
b = 0
for i in range(Q):
  q = list(input().split())
  if q[0] == '1':
    a += 1
    b += 1
    
  elif (q[1] == '1' and a%2 == 0) or (q[1] == '2' and a%2 == 1):
    S = [q[2]] + S
    a = 0
    
  else:
    S = S+[q[2]]
    a = 0
    
if b%2 == 1:
  S.reverse()
    
S="".join(S)
print(S)

Submission status: Wrong Answer

Solution code:
import sys
from collections import deque

sys.setrecursionlimit(10 ** 7)
f_inf = float('inf')
mod = 10 ** 9 + 7


def resolve():
    s = input()
    q = int(input())
    query = [list(input().split()) for _ in range(q)]

    S = deque([s])
    for i in range(q):
        if query[i][0] == "1":
            S = deque(reversed(S))
        else:
            _, f, c = query[i]
            S.appendleft(c) if f == "1" else S.append(c)

    print("".join(S))


if __name__ == '__main__':
    resolve()

Submission status: Wrong Answer

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

for q in Que:
    if q[0] == '1':
        r += 1
    else:
        if (q[1] == '1' and r % 2 == 0) or (q[1] == '2' and r % 2 == 1):
            S.append(q[2])
        else:
            S.appendleft(q[2])

if r % 2 == 1:
   while len(S) > 0:
        s = S.popleft()
        print(s, end='')    
else:
    while len(S) > 0:
        s = S.pop()
        print(s, end='')
print('')

Submission status: Wrong Answer

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

for q in Que:
    if q[0] == '1':
        r += 1
    else:
        if (q[1] == '1' and r % 2 == 0) or (q[1] == '2' and r % 2 == 1):
            S.append(q[2])
        else:
            S.appendleft(q[2])

if r % 2 == 1:
   while len(S) > 0:
       s = S.popleft()
       print(s, end='')    
else:
    while len(S) > 0:
        s = S.pop()
        print(s, end='')
print('')

Submission status: Wrong Answer

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

for q in Que:
    if q[0] == '1':
        r += 1
    else:
        if (q[1] == '1' and r % 2 == 0) or (q[1] == '2' and r % 2 == 1):
            S.append(q[2])
        else:
            S.appendleft(q[2])

if r % 2 == 1:
   while len(S) > 1:
       s = S.popleft()
       print(s, end='')    
else:
    while len(S) > 1:
        s = S.pop()
        print(s, end='')
s = S.pop()
print(s)

Submission status: Wrong Answer

Solution code:
#D
from collections import deque
s = deque(input())
q = int(input())
#flg:反転してるときtrue,してないときFalse
flg = False

for _ in range(q):
    t = tuple(map(str,input().split()))
    if t[0] == '1':
        if flg:
            flg = False
        else:
            flg = True
    else:
        if t[1] == "1" and flg == False:
            s.appendleft(t[2])
        elif t[1] == "1" and flg == True:
            s.append(t[2])
        elif t[1] == "0" and flg == False:
            s.append(t[2])
        else:
            s.appendleft(t[2])
 
    
if flg:
    s.reverse()
print("".join(s))

Submission status: Wrong Answer

Solution code:
#D
#import sys
#input = sys.stdin.readline
from collections import deque
s = deque(input())
q = int(input())
#flg:反転してるときtrue,してないときFalse
flg = False

for _ in range(q):
    t = tuple(map(str,input().split()))
    if t[0] == '1':
        if flg:
            flg = False
        else:
            flg = True
    else:
        if t[1] == "1" and flg == False:
            s.appendleft(t[2])
        elif t[1] == "1" and flg == True:
            s.append(t[2])
        elif t[1] == "0" and flg == False:
            s.append(t[2])
        else:
            s.appendleft(t[2])
 
if flg:
    s.reverse()
print("".join(s))

Submission status: Wrong Answer

Solution code:
#D
import sys
input = sys.stdin.readline
s = input()
q = int(input())
query = []
for _ in range(q):
    t = list(map(str,input().split()))
    if t[0] == '1':
        s = s[::-1]
    else:
        if t[1] == "1":
            s = t[2] + s
        else:
            s = s + t[2]
            
print(s)

Submission status: Wrong Answer

Solution code:
s = input()
q = int(input())
t = 0
f = []
b = []
for _ in range(q):
    a = input().split()
    if len(a) == 1:
        t += 1
    else:
        m, n = int(a[1]), a[2]
        if t % 2 == 0:
            if m == 1:
                f.append(n)
            else:
                b.append(n)
        else:
            if m == 1:
                b.append(n)
            else:
                f.append(n)
if t % 2 == 0:
    x = ''
    for i in f:
        x = i + x
    x = x + s
    for i in b:
        x = x + i
    print(x)
else:
    x = ''
    for i in b:
        x = i + x
    x = x + s
    for i in f:
        x = x + i
    print(x)

Submission status: Wrong Answer

Solution code:
s = str( input() )
q = int( input() )
query = []
rev_flag_k = 0
for _ in range( q ):
    query_i = list( input().split() )
    query.append( query_i )
    if query_i[ 0 ] == "1":
        rev_flag_k += 1

rev_flag = 0
for query_i in query:
    t = query_i[ 0 ]
    if t == "1":
        rev_flag += 1
    else:
        f = query_i[ 1 ]
        c = query_i[ 2 ]
        if f == "1" and rev_flag % 2 == 1:
            s = "".join( [ s, str( c ) ] )
        elif f == "2" and rev_flag % 2 == 0:
            s = "".join( [ str( c ), s ] )
        else:
            s = "".join( [ str( c ), s ] )
if rev_flag_k % 2 == 1:
    s = s[::-1]

print( s )

Submission status: Wrong Answer

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

def main():
    s = [input()]
    n = int(input())
    qu = []
    for i in range(n):
        #qu.append(list(input().split()))
        q = input().split()
        if q[0] == '1':
            s.reverse()
            continue
        elif q[0] == '2':
            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:
import sys

def main():
    s = [input()]
    n = int(input())
    qu = []
    for i in range(n):
        #qu.append(list(input().split()))
        q = input().split()
        if q[0] == '1':
            s.reverse()
            continue
        elif q[0] == '2':
            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:
s = [input()]
n = int(input())
qu = []
for i in range(n):
    #qu.append(list(input().split()))
    q = input().split()
    if q[0] == '1':
        s.reverse()
        continue
    elif q[0] == '2':
        if q[1] == '1':
            s = [q[2]] + s
        elif q[1] == '2':
            s.append(q[2])

print(''.join(s))

Submission status: Wrong Answer

Solution code:
s = [input()]
n = int(input())
qu = []
for i in range(n):
    #qu.append(list(input().split()))
    q = input().split()
    if q[0] == '1':
        s.reverse()
        continue
    elif q[0] == '2':
        if q[1] == '1':
            s.reverse()
            s.append(q[2])
            s.reverse()
        elif q[1] == '2':
            s.append(q[2])

print(''.join(s))

Submission status: Wrong Answer

Solution code:
s = [input()]
n = int(input())
qu = []
for i in range(n):
    #qu.append(list(input().split()))
    q = input().split()
    if q[0] == '1':
        s.reverse()
    elif q[0] == '2':
        if q[1] == '1':
            s.reverse()
            s.append(q[2])
            s.reverse()
        elif q[1] == '2':
            s.append(q[2])

print(''.join(s))

Submission status: Wrong Answer

Solution code:
import math

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

l = [s]
k = 0
for i in range(q):
    a = input().split()
    if(a[0] == '1'):
        k += 1
    if(a[0] == '2'):
        if(k %2 == 1):
            l = l[::-1]
            k = 0
        else:
            k = 0
        if(a[1] == '1'):
            l.insert(0,a[2])
        else:
            l.append(a[2])

if(k%2 ==1):
    l = l[::-1]
l = ''.join(l)
print(l)

Submission status: Wrong Answer

Solution code:
from collections import deque
S = deque([input(), ])
q = int(input())
r = False
for _ in range(q):
    t = input().split()
    if len(t) == 1:
        r = not r
    else:
        t[1] = int(t[1]) - 1
        if (not t[1] and not r) or (t[1] and r):
            S.appendleft(t[2])
        else:
            S.append(t[2])
if r:
    S = list(S)
    print(''.join(S[::-1]))
else:
    print(''.join(S))

Submission status: Wrong Answer

Solution code:
import math

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

l = [s]

for i in range(q):
    a = input().split()
    if(a[0] == '1'):
        l = l[::-1]
    if(a[0] == '2'):
        if(a[1] == '1'):
            l.insert(0,a[2])
        else:
            l.append(a[2])

l = ''.join(l)
print(l)

Submission status: Wrong Answer

Solution code:
import math

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

l = [s]

for i in range(q):
    a = input().split()
    if(a[0] == '1'):
        l = l[::-1]
    if(a[0] == '2'):
        if(a[1] == '1'):
            l.insert(0,a[2])
        else:
            l.append(a[2])


for i in range(len(l)):
    print(l[i],end = '')

Submission status: Wrong Answer

Solution code:
s = str(input())
q = int(input())
cnt = 0
L = []
R = []
for _ in range(q):
    query = list(input().split())
    if query[0] == '1':
        cnt += 1
    else:
        if query[1] == '1':
            if cnt % 2:
                R.append(query[2])
            else:
                L.append(query[2])
        else:
            if cnt % 2:
                L.append(query[2])
            else:
                R.append(query[2])
if cnt % 2:
    s = s[::-1]
s =  ''.join(L[::-1]) + s + ''.join(R)
if cnt % 2:
    s = s[::-1]
print(s)

Submission status: Wrong Answer

Solution code:
# import sys
# import math
# import itertools
from collections import deque
# from collections import defaultdict
# import heapq
# import copy
# import bisect
# import numpy as np
# from scipy.special import comb

# def my_input(): return sys.stdin.readline().rstrip()
# sys.setrecursionlimit(100000)
# INF = 1001001001001
# MOD = 1000000007


def main():

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

    str_q = deque()

    for s in S:
        str_q.append(s)

    for _ in range(Q):
        t, *query = input().split()
        if t == '1':
            first = str_q.popleft()
            last = str_q.pop()
            str_q.append(first)
            str_q.appendleft(last)
        else:
            f, c = query
            if f == '1':
                str_q.appendleft(c)
            else:
                str_q.append(c)

    print(''.join(list(str_q)))


if __name__ == '__main__':
    main()