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 = input()
Q = int(input())
Query = [list(map(str,input().split())) for i in range(Q)]
hanten_flag = 0
cnt = 0
for i in range(Q):
    if(Query[i][0] == "1"):
        hanten_flag ^= 1
    else:
        if(Query[i][1] == "1" and hanten_flag == 0):
            S = Query[i][2] + S
        elif(Query[i][1] == "1" and hanten_flag == 1):
            S = S + Query[i][2]
        elif(Query[i][1] == "0" and hanten_flag == 0):
            S = S + Query[i][2]
        else:
            S = Query[i][2] + S
if(hanten_flag == 1):
    S = S[::-1]
print(S)

Submission status: Wrong Answer

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

front = ''
back = ''

r_flg = False
for i in range(Q):
    q = input()
    if q == '1':
        r_flg = False if r_flg else True
        continue
        
    t,f,c = q.split()
    if not r_flg:
        if f == '1':
            front += c
        else:
            back += c
    else:
        if f == '1':
            back += c
        else:
            front += c
    
if not r_flg:
    print(front[::-1]+S+back)
else:
    print(back[::-1] + S + front)

Submission status: Wrong Answer

Solution code:
s = str(input())
q = int(input())
flip = -1
strs_f = ''
strs_b = ''

for i in range(q):
    query = list(map(str, input().split()))
    if query[0] == '1':
        flip = flip * -1
    elif query[0] == '2':
        if query[1] == '1':
            if flip == -1:
                strs_f = query[2] + strs_f
            else:
                strs_b = strs_b + query[2]
        else:
            if flip == -1:
                strs_b = query[2] + strs_b
            else:
                strs_f = strs_f + query[2]

if flip == -1:
    ans = strs_f[::-1] + s + strs_b
else:
    ans = strs_b[::-1] + s[::-1] + strs_f

print(ans)

Submission status: Wrong Answer

Solution code:
def main():
  S = input()
  Q = int(input())
  import sys
  que = [list(sys.stdin.readline().split()) for _ in range(Q)]
  judge = False
  for l in que:
    if int(l[0])==1:
      if judge:
        judge = False
      else:
        judge = True

    elif int(l[0])==2:
      f,c = int(l[1]),l[2]
  if judge:
    S=S[::-1]
  print(S)
  
main()

Submission status: Wrong Answer

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

for i in range(Q):
    l = str(input())
    if l[0] == '1':
        rev = (rev + 1) % 2
    else:
        two, F, C = l.split()
        if rev == 0:
            if F == '1':
                S = ''.join([C, S])
            else:
                S = ''.join([S, C])
        else:
            if F == 1:
                S = ''.join([S, C])
            else:
                S = ''.join([C, S])
    # print(S, rev)
        # print(two, F, C)


if rev == 0:
    print(S)
else:
    print(''.join(list(reversed(S))))
# print(''.join(S))

# s = 'oo'
# print(''.join([S, s]))

Submission status: Wrong Answer

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

reverseFlag = False

for i in range(NQ):
    query = input().split()
    if (int(query[0]) == 1):
        reverseFlag = not reverseFlag
    else:
        if (reverseFlag):
            if (int(query[1]) == 1):
                S = query[2] + S
            else:
                S = S + query[2]
        else:
            if (int(query[1]) == 1):
                S = S + query[2]
            else:
                S = query[2] + S

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

Submission status: Wrong Answer

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

mae = []
ushiro = []
for i in range(q):
    query = list(map(str,input().split()))
    if int(query[0]) == 1 :
        mae, ushiro  = ushiro, mae
        mae = mae[::-1]
        ushiro = ushiro[::-1]
        s = s[::-1]
        continue
    else:
        if int(query[1]) == 1:
            tmp = [query[2]]
            mae.extend(tmp)
        else:
            tmp = [query[2]]
            ushiro.extend(tmp)
ans = ""
for i  in mae:
    ans += i
ans += s
for i in ushiro:
    ans+=i
print(ans)

Submission status: Wrong Answer

Solution code:
from collections import deque
def main():
  d=deque(list(input()))
  Q=int(input())
  for i in range(Q):
    q=list(map(str, input().split()))
    if q[0]=='1':
      l=d.popleft()
      r=d.pop()
      d.append(l)
      d.appendleft(r)
    else:
      if q[1]=='1':
        d.appendleft(q[2])
      else:
        d.append(q[2])
  print(''.join(list(d)))
main()

Submission status: Wrong Answer

Solution code:
s=input()
q=int(input())
trans=0
aft=""
bef=""
for i in range(q):
  que=input()
  if que[0]=="1":
    trans=1-trans
  else:
    if (que[2]=="1" and trans==0) or (que[2]=="2" and trans==1):
      bef=bef+que[4]
    else:
      aft=aft+que[4]
if trans==0:
  print("".join(bef[::-1])+s+"".join(aft))
else:
  print("".join(aft[::-1])+s+"".join(bef))

Submission status: Wrong Answer

Solution code:
s = input()
q = int(input())
isReverce = False
ans = q
for x in range(q):
    query = list(map(str, input().split()))
    if query[0] == "1": isReverce = not isReverce
    else:
        if (query[1] == "1" and isReverce == False) or (query[1] == "2" and isReverce == True):
            s = query[2] + s
        else: s += query[2]
if isReverce == False: print(s[::-1])
else: print(s[::-1])

Submission status: Wrong Answer

Solution code:
def main():
  S = input()
  Q = int(input())
  import sys
  que = [list(sys.stdin.readline().split()) for _ in range(Q)]
  judge = False
  for l in que:
    if int(l[0])==1:
      if judge:
        judge = False
      else:
        judge = True

    elif int(l[0])==2:
      f,c = int(l[1]),l[2]
      if judge:
        if f==1:
          f=2
        elif f==2:
          f=1
  if judge:
    S=S[::-1]
  print(S)
  
main()

Submission status: Wrong Answer

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

for i in range(Q):
    input_val = input().split()
    if input_val[0] == "1":
        if len(S) == 1:
            continue

        S = S[-1:] + S[1:-1] + S[:1]
        continue

    if input_val[1] == "1":
        S = input_val[2] + S
    else:
        S = S + input_val[2]

print(S)

Submission status: Wrong Answer

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

    if type == 1:
        _strings.reverse()
    else:
        mode = int(query[1])
        if mode == 1:
            _strings.insert(0, str(query[2]))
        else:
            _strings.append(str(query[2]))

    i += 1

print(''.join(_strings))

Submission status: Wrong Answer

Solution code:
#import sys
MOD = 10 ** 9 + 7
INFI = 10**10
#input = sys.stdin.readline
import math

import itertools

#import bisect

import copy
from functools import lru_cache

def sosuhante(n):
    for k in range(2, int(math.sqrt(n))+1):
        if n% k ==0:
            return False
    return True
def cmb(n, r):
    if n - r < r: r = n - r
    if r == 0: return 1
    if r == 1: return n

    numerator = [n - r + k + 1 for k in range(r)]
    denominator = [k + 1 for k in range(r)]

    for p in range(2,r+1):
        pivot = denominator[p - 1]
        if pivot > 1:
            offset = (n - r) % p
            for k in range(p-1,r,p):
                numerator[k - offset] /= pivot
                denominator[k] /= pivot

    result = 1
    for k in range(r):
        if numerator[k] > 1:
            result *= int(numerator[k])

    return result

def kingaku(a,b,n):
    keta=len(str(n))
    return a*n+b*keta

def my_index(l, x, default=False):
    if x in l:
        return l.index(x)
    else:
        return default

#    h,w,a,b = map(int, input().split())
#    c = [[0 for j in range(n)] for i in range(n)]



def main():
    s=input()
    q=int(input())
    even=1
    right=[]
    left=[]
    for i in range(q):
        temp=list(input().split())
        temp[0]=int(temp[0])
        if temp[0]==2:
            a=int(temp[1])
            b=temp[2]
            if a==1:
                left.append(b)
            else:
                right.append(b)
        else:
            even=-(even-1)
            left,right=right,left
#    print(left,s,right,even)
    if even==0:
        s=s[::-1]
        right=right[::-1]
    else:
        left=left[::-1]
    ans="".join(left)+s+"".join(right)
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
s  = str(input())
t = ""
u = ""
q = int(input())
count_inv = 0
for i in range(q):
  inlis = list(map(str,input().split())) + [0,0]
  if inlis[0] ==  "1":
    count_inv += 1
    count_inv %= 2
  if inlis[0] == "2":
    if inlis[1]  == "1":
      if count_inv == 0:
        u += str(inlis[2])
      else:
        t += str(inlis[2])
      # print(1)
    if inlis[1] == "2":
      if count_inv == 0:
        t += str(inlis[2])
      else:
        u += str(inlis[2])
      # print(2)
  # print(s)
if count_inv == 0:
  u = u[::-1]
  ans = u+s+t
else:
  t = t[::-1]
  ans = t+s+u
print(ans)

Submission status: Wrong Answer

Solution code:
S=input()
Q=int(input())
inv=False
head=""
last=""
for i in range(Q):
    qu=input().split(" ")
    if int(qu[0])==1:
        inv=not inv
    else:
        F=int(qu[1])
        if inv:
            F=3-F
        if F==1:
            head+=qu[2]
        else:
            last+=qu[2]

if inv:
    print(last[::-1]+S+head)
else:
    print(head[::-1]+S+last)

Submission status: Wrong Answer

Solution code:
import sys
s = sys.stdin.readline()
q = int(input())
Flag = True
for i in range(q):
    xxx = input()
    if len(xxx) == 1:
        if Flag == True:
            Flag = False
        else:
            Flag = True
    else:#T=2
        T,F,C=xxx.split()
        F = int(F)
        if F == 1:
            if Flag == True:
                s = C+s
            else:
                s = s+C
        else:#F=2
            if Flag == True:
                s = s + C
            else:
                s = C + s
if Flag == True:
    print(s)
else:
    s = s[::-1]
    print(s)

Submission status: Wrong Answer

Solution code:
from collections import deque
S = input()
L = deque()
L.append(S)
Q = int(input())
is_lookforward = True
for i in range(Q):
    query = input()
    if query[0] == '1':
        is_lookforward = not is_lookforward
    else:
        F = int(query[2])
        C = query[4]
        if (F == 1 and is_lookforward) or (F == 2 and not is_lookforward):
            L.appendleft(C)

        else:
            L.append(C)

print(''.join(L) if is_lookforward else ''.join(reversed(L)))

Submission status: Wrong Answer

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

for i in range(Q):
    q=Qlst[i]
    if q[0]=='1':
        S[0],S[-1]=S[-1],S[0]
    else:
        if q[1]=='1':
            S=[q[2]]+S[:]
        else:
            S.append(q[2])
print(''.join(S))

Submission status: Wrong Answer

Solution code:
from collections import deque

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

cur = 1
d = deque([S])

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

    if len(xs) == 1:
        cur = 2 if cur == 1 else 1
    else:
        _, f, c = xs
        if f == cur == 1 or f == cur == 2:
            d.appendleft(c)
        else:
            d.append(c)

if cur == 2:
    ans = "".join(d)
else:
    d.reverse()
    ans = "".join(d)

print(ans)

Submission status: Wrong Answer

Solution code:
from collections import deque
s = input()
q = int(input())
que = []
for _ in range(q):
    que.append(list(input().split()))
d = deque([s])
cnt = 0
flag = False
for i in range(q):
    if len(que[i]) == 1:
        cnt += 1
        if i == q - 1 and cnt % 2 == 1:
            flag = True
    else:
        f = int(que[i][1])
        c = que[i][2]
        if (f + cnt) % 2 == 1:
            d.appendleft(c)
        else:
            d.append(c)
if flag:
    d = reversed(list(d))
res = ""
for i in d:
    print(i, end="")

Submission status: Wrong Answer

Solution code:
import numpy as np 

S = str(input())

Q = int(input())

q = np.ones(Q,dtype = object)

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

for i in range(Q):
    if q[i][0] == '1':
        if len(S)==1:
            pass
        else:
            S = S[-1]+S[1:-1]+S[0]
    else:
        if q[i][1] == '1':
            S = str(q[i][2])+S
        else:
            S = S + str(q[i][2])
            
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):
    if len(query[i])==1:
        flag=(flag+1)%2 #反転フラグをひっくり返す
    else:
        if flag==0:
            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:
    for i in range(len(d)):
        s2="".join((s2,d[i]))
    print(s2)
else:
    d.reverse()
    for i in range(len(d)):
        s2="".join((s2,d[i]))
    print(s2)

Submission status: Wrong Answer

Solution code:
from collections import deque


S = input()
N = int(input())

d = deque()
d.append(S)

# Trueであれば順方向
direction = True

for i in range(N):
    query = input()
    if query[0] == "1":
        t = int(query)
    else:
        t, f, c = query.split()
        t = int(t)
        f = int(f)

    if t == 1:
        #True-Falseの逆転
        direction = not direction

    if t == 2:
        if direction == True:
            if f == 1:
                d.appendleft(c)
            else:
                d.append(c)

        else:
            if f == 1:
                d.append(c)
            else:
                d.appendleft(c)
  
if direction == True:
    print(''.join(d))
else:
    print(''.join(reversed(d)))

Submission status: Wrong Answer

Solution code:
from collections import deque
S = input()
Q = int(input())
d = deque()
d.append(S)
rev = 1

for i in range(Q):
  List = list(input().split())
  a = int(List[0])
  if a == 1:
    rev = rev * -1
  else:
    b = int(List[1])
    if (b == 1 and rev == 1) or (b == 2 and rev == -1):
      d.appendleft(List[2])
    else:
      d.append(List[2])

if rev == -1:
  d.reverse()
string = "".join(list(d))
print(string)

Submission status: Wrong Answer

Solution code:
s = input().strip()
q = int(input().strip())
queries = [input().split() for _ in range(q)]
one_count = 0
one_counts = []
for i in range(q):
    if queries[i][0] == "1":
        one_count += 1
    one_counts.append(one_count)
one_counts = list(map(lambda x: (one_count - x), one_counts))
for i in range(q):
    if queries[i][0] == "1":
        continue
    elif queries[i][0] == "2":
        if queries[i][1] == "1":
            if one_counts[i] % 2 == 0:
                s = queries[i][2] + s
            else:
                s += queries[i][2]
        elif queries[i][1] == "2":
            if one_counts[i] % 2 == 0:
                s += queries[i][2]
            else:
                s = queries[i][2] + s
print(s)

Submission status: Wrong Answer

Solution code:
ans = input()
odr = 0

N = int(input())

for _ in range(N):
    Q = input()

    if Q == '1':
        if odr == 0:
            odr = 1
        else:
            odr = 0
            
        continue
    
    A, F, C = Q.split()
    if ((odr == 0 and F == 1) or (odr == 1 and F == 0)):
        ans = ans + C
    else:
        ans = C + ans
    

if odr == 1:
    print(ans[::-1])
else:
    print(ans)

Submission status: Wrong Answer

Solution code:
import sys
s = input()

flont = []
back = []

if len(s) != 1:
    flont = [s[0]]
    back = [s[-1]]
else:
    flont = [s[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:
import sys
import math
from collections import defaultdict

sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]

mod = 10**9 + 7

def I(): return int(input())
def LI(): return list(map(int, input().split()))
def LIR(row,col):
    if row <= 0:
        return [[] for _ in range(col)]
    elif col == 1:
        return [I() for _ in range(row)]
    else:
        read_all = [LI() for _ in range(row)]
        return map(list, zip(*read_all))

#################

from collections import deque
from bisect import bisect_right
S = deque(list(str(input())))
Q = I()

one = []
isone = []
F = [0]*Q
C = [0]*Q
for i in range(Q):
    q = input().split()
    if q[0]=='1':
        one.append(i)
        isone.append(True)
    else:
        F[i] = (int(q[1]))
        C[i] = q[2]
        isone.append(False)


num = sum(isone)
if num%2==1:
    S = deque(list(reversed(S)))

bef = deque([])
aft = deque([])
for i,now in enumerate(isone):
    if now==1:
        if num%2==1:
            for b in reversed(bef):
                S.append(b)
            for a in aft:
                S.appendleft(a)
        else:
            for b in bef:
                S.appendleft(b)
            for a in reversed(aft):
                S.append(a)
        bef = deque([])
        aft = deque([])
        num -= 1
    else:
        if F[i]==1:
            bef.appendleft(C[i])
        else:
            aft.append(C[i])
for b in bef:
    S.appendleft(b)
for a in reversed(aft):
    S.append(a)
print(*S,sep='')

Submission status: Wrong Answer

Solution code:
# li = [[0] * 4 for i in range(3)]

# A = list.map(int, input().split())

S = input()

Q = int(input())
hanten = 0

ato = []
mae = []

for i in range(Q):
    li = input().split()

    one = int(li[0])
    if one == 1:
        hanten += 1
    else:
        F = int(li[1])
        C = li[2]
        if (hanten + F) % 2 == 1:
            mae.append(C)
        else:
            ato.append(C)

if hanten % 2 == 0:
    mae.reverse()
    print("".join(mae) + S + "".join(ato))
else:
    ato.reverse()
    print("".join(ato) + S + "".join(mae))

Submission status: Wrong Answer

Solution code:
from collections import deque

S = input()
Q = int(input())
W = 0
ans = deque(list(S.rstrip()))

for _ in range(Q):
#    print(W,ans)
    qry = list(input().rstrip().split())
    if qry[0] == "1":
        W = 1-W
        continue
    T,F,C = qry
    T = int(T)
    F = int(F)
    C.rstrip()
    if (W == 0 and F=="2") or (W==1 and F=="1"):
        ans.append(C)
    else:
        ans.appendleft(C)
ans = "".join(map(str,ans))
if W == 1:
    ans = ans[::-1]
print(ans)

Submission status: Wrong Answer

Solution code:
from collections import deque

s = input()
Q = int(input())
query = [list(map(str, input().split())) for _ in range(Q)]

d = deque(s)

flag = False
cnt = 0
for q in query:
    if q[0] == '1':
        flag = True
        cnt += 1
    else:
        d = deque(s)
        if q[1] == '1':
            if flag:
                d.append(q[2])
            else:
                d.appendleft(q[2])
        else:
            if flag:
                d.appendleft(q[2])
            else:
                d.append(q[2])
        d_list = list(d)
        s = ''.join(d_list)
        flag = False

if cnt % 2 == 0:
    print(s)
else:
    print(s[::-1])

Submission status: Wrong Answer

Solution code:
s =input()
Q = int(input())
cnt = 0
for _ in range(Q):
    qs = input().split()
    if qs[0] == '1':
        cnt+=1
    else:
        f = int(qs[1])
        c = qs[2]
        if f == 1:
            if cnt % 2 == 0:
                s = s + c
            else:
                s = c + s
        else:
            if cnt % 2 == 0:
                s = c + s
            else:
                s = s + c
if cnt % 2 == 0:
    print(s[::-1])
else:
    print(s)

Submission status: Wrong Answer

Solution code:
from collections import deque
S=deque(maxlen=10**6)
S.append(input())
Q=int(input())
for _ in range(Q):
    target = input()
    if target == '1':
        S.reverse()
    else:
        a,b,c=target.split()
        if b=='1':
            S.appendleft(c)
        else:
            S.append(c)
print(''.join(list(S)))

Submission status: Wrong Answer

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

flag = True
ans = [s]


for i in range(q):
  query = list(input().split())
  if int(query[0]) == 1:
    flag = not flag
  else:
    if int(query[1]) == 1:
      if flag:
        ans.insert(0, query[2])
      else:
        ans.insert(-1, query[2])
    else:
      if flag:
        ans.insert(-1, query[2])
      else:
        ans.insert(0, query[2])

if flag:
  print("".join(ans))
else:
  ans.reverse()
  print("".join(ans))

Submission status: Wrong Answer

Solution code:
from sys import stdin
sin = stdin.readline().rstrip()
q = int(stdin.readline().rstrip())
li = [list(stdin.readline().rstrip().split()) for _ in range(q)]
point = 0
omote = ""
ura = ""
for i in li:
    if i[0] == "1":
        point += 1
    elif i[0] == "2":
        if i[1] == "1":
            if point%2 == 0:
                omote += i[2]
            else:
                ura += i[2]
        else:
            if point%2 == 0:
                ura += i[2]
            else:
                omote += i[2]
if point%2 == 0:
    omote = omote[::-1]
    sin = omote + sin + ura
    print(sin)
else:
    ura = ura[::-1]
    sin = ura + sin + omote
    print(sin)

Submission status: Wrong Answer

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

    if type == 1:
        back = _strings.pop()
        first = _strings.pop(0)
        _strings.insert(0, back)
        _strings.append(first)
    else:
        mode = int(query[1])
        if mode == 1:
            _strings.insert(0, str(query[2]))
        else:
            _strings.append(str(query[2]))

    i += 1

print(''.join(_strings))

Submission status: Wrong Answer

Solution code:
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"]:
        if rev[i]%2==0:
            if query[1]=="1":
                left=query[2]+left
            else:
                right=right+query[2]
        else:
            if query[1]=="1":
                right=right+query[2]
            else:
                left=query[2]+left 
print(left+s+right)

Submission status: Wrong Answer

Solution code:
S = input()
q = int(input())
Q = []
for _ in range(q):
    Q.append(input().split())

Q = Q[::-1]

cnt = 0
left = []
right = []
for x in range(q):
    if len(Q[x]) == 1:
        cnt += 1
    else:
        if (int(Q[x][1]) + cnt) % 2 == 0:
            right.append(Q[x][2])
        else:
            left.append(Q[x][2])

#print(left, right)

for l in left:
    print(l, end="")
print(S, end="")
for r in right[::-1]:
    print(r, end="")

print()

Submission status: Wrong Answer

Solution code:
s = input()
Q = int(input())
mode = 1
for i in range(Q):
    tmp = list(input().split())
    if len(tmp)==1:
        mode *= -1
    else:
        f,c = tmp[1], tmp[2]
        if int(f)*mode > 0:
            s = c + s
        else:
            s = c + s
print(s if mode>0 else s[::-1])

Submission status: Wrong Answer

Solution code:
S=input()
Q=int(input())
str_list_ = []
for q in range(Q):
    str_list_.append( input().split(' ') )

for q in range(Q):
    #print(q)
    #str_list = input().split(' ')
    str_list = str_list_[q]
    #print(str_list)
    if str_list[0]=='1':
        #head=S[0]
        #tail=S[len(S)-1]
        #S=S[1:]
        #S=tail+S
        #S=S[0:-1]
        #S=S+head
        #S=tail+S[1:-1]+head
        S=S[len(S)-1]+S[1:-1]+S[0]
        #print(S)
    elif str_list[0]=='2':
        if str_list[1]=='1':
            S=str_list[2]+S
        elif str_list[1]=='2':
            S=S+str_list[2]
        #print(S)
print(S)

Submission status: Wrong Answer

Solution code:
s = list(input())
q = int(input())
q_list = []
for i in range(q):
    e = list(input().split(" "))
    q_list += [e]
print(q_list)
for i in range(q):
    if q_list[i][0] == "1":
        s = s[::-1]
    elif q_list[i][0] == "2":
        if q_list[i][1] == "1":
            s.insert(0, q_list[i][2])
        else:
            s.append(q_list[i][2])
    print(s)
print("".join(s))

Submission status: Wrong Answer

Solution code:
s=input()
q=int(input())
for _ in range(q):
    query = input()
    try:
        t = int(query)
        s = s[::-1]
    except ValueError:
        t,f,c = map(str,query.split())
        if f == 1:
            s = c + s
        if f == 2:
            s = s + c
print(s)

Submission status: Wrong Answer

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

sg = 1
fr = []
ba = []
for x in range(q):
  query = [s for s in input().split()]
  if query[0] == '1':
    sg *= -1
  else:
    if int(query[1])*2 + sg == 3:
      fr.append(query[2])
    else:
      ba.append(query[2])
        
if sg == 1:
  fr.reverse()
  frn = ''.join(fr)
  ban = ''.join(ba)
  print(frn + s + ban)
else:
  ba.reverse()
  frn = ''.join(fr)
  ban = ''.join(ba)
  print(ban + s + frn)

Submission status: Wrong Answer

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

cnt = 0
left = ''
right = ''
for i in range(q):
    a = [i for i in input().split()]
    if a[0] == '1':
        cnt += 1
    else:
        if (a[1] == '1' and cnt % 2 == 0) or (a[1] == '2' and cnt % 2 == 1):
            right = right + a[2]
        else:
            left = left + a[2]

if cnt % 2 == 0:
    print(''.join(list(reversed(right))) + s + left)
else:
    print(''.join(list(reversed(left + s))) + right)

Submission status: Wrong Answer

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

right = []
left = []

reverse = 0

r = 2
for _ in range(q):
    Q = list(input().split())
    if int(Q[0]) == 1:
        if r == 2:
            r = 1
        else:
            r = 2
        reverse += 1
    else:
        if r == 2:
            if int(Q[1]) == 2:
                right.append(Q[2])
            else:
                left.append(Q[2])
        else:
            if int(Q[1]) == 2:
                left.append(Q[2])
            else:
                right.append(Q[2])
                
                
if reverse%2 == 0:
    print(''.join(left[::-1]) + s + ''.join(right))
else:
    print(''.join(right[::-1]) + s + ''.join(left))

Submission status: Wrong Answer

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

for _ in range(q):
    ql = list(input().split())
    if ql[0] == '1':
        s[0], s[-1] = s[-1], s[0]
    else:
        if ql[1] == '1':
            # s = ql[2] + s
            s.insert(0, ql[2])
        else:
            s += ql[2]
            # s.append(ql[2])


print(*s, sep='')

Submission status: Wrong Answer

Solution code:
item = []
item.append(input())
n = int(input())
for i in range(n):
    tmp = input()
    if tmp[0] == '1':
        item.reverse()
    else:
        _, q, s = tmp.split()
        if q == '1':
            item = [s] + item
        else:
            item += [s]
print(''.join(item))

Submission status: Wrong Answer

Solution code:
s = input().split()
ans = s
s = input().split()
q = int(s[0])
for i in range(q):
    s = input().split()
    if s[0]=='1':
        ans=ans[::-1]
    else:
        if s[1]=='1':
            ans=list(s[2])+ans
        else:
            ans.append(s[2])
print(''.join(ans))

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 = deque(s)

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

    if len(line) == 1:
        chars.reverse()
    else:
        if line[1][0] == '1':
            chars.appendleft(line[2][0])
        else:
            chars.append(line[2][0])

print(''.join(list(chars)))

Submission status: Wrong Answer

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

S = input()
Q = int(input())
W = 0
ans = deque(list(S.rstrip()))

for _ in range(Q):
#    print(W,ans)
    qry = list(input().rstrip().split())
    if qry[0] == "1":
        W = 1-W
        continue
    T,F,C = qry
    T = int(T)
    F = int(F)
    C.rstrip()
    if (W == 0 and F=="2") or (W==1 and F=="1"):
        ans.append(C)
    else:
        ans.appendleft(C)
ans = "".join(map(str,ans))
if W == 1:
    ans = ans[::-1]
print(ans)