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

def answer():
    s = input().split()
    s = deque(s)
    q = int(input())
    rev = False
    for i in range(q):
        query = input().split()
        if int(query[0]) == 2:
            f, c = int(query[1]), query[2]
            if (not rev and f == 1) or (rev and f == 2):
                s.appendleft(c)
            else:
                s.append(c)
        else:
            rev = not rev
    if rev:
        s = list(s)
        s = s[::-1]
    print(''.join(s))
    return

answer()

Submission status: Wrong Answer

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

rev = 0
onenum = 0
for i in range(q):
	g = input()
	if len(g) == 1:
		t = int(g)
	else:
		t = int(g[0])
		f = int(g[2])
		c = g[4]
	if t == 1:
		onenum += 1
		rev += 1
	else:
		if f == 2:
			if rev%2 == 1:
				s = c + s
			else:
				s = s + c
		else:
			if rev%2 == 0:
				s = c + s
			else:
				s = s + c
		rev = 0
if onenum%2 == 1:
	s = s[::-1]
print(s)

Submission status: Wrong Answer

Solution code:
s = input()
Q = int(input())
flag = True  # flagがTrueのとき、偶数回数の反転が行われている
tmp = []
for _ in range(Q):
    query = list(map(str, input().split()))
    tmp.append(query)
    if len(query) == 1:  # 1のqueryのとき
        if flag:
            flag = False
        else:
            flag = True
    else:
        f, c = query[1], query[2]
        if f == "1":  # 先頭に追加するもの
            if flag:
                s = c + s  # 偶数回の反転なら素直に先頭に追加する
            else:
                s += c  # 奇数会の反転なら後ろに追加する
        else:  # 末尾に追加する
            if flag:
                s += c
            else:
                s = c + s


# 最後に反転が必要かどうか
cnt = 0
tmp.reverse()
for t in tmp:
    if len(t) == 1:
        cnt += 1
    else:
        break
if cnt % 2 == 0 and cnt != 0:
    s = s[::-1]
print(s)

Submission status: Wrong Answer

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

r = 0
firstS = []
lastS = []
for q in range(Q):
  row = input().split()
  if len(row) == 1:
    r = (r+1) % 2
    tmp = lastS[::-1]
    lastS = firstS[::-1]
    firstS = tmp
  else:
    if (int(row[1])) %2 == 0:
      lastS.append(row[2])
    else:
      firstS =[row[2]] + firstS
      
S= firstS + S + lastS
print("".join(S))

Submission status: Wrong Answer

Solution code:
S = input()
Q = int(input())
Qs = []
for _ in range(Q):
    Qs.append(input().split())  # T, F, C

rev = 0
for i in range(len(Qs)):
    if Qs[i][0]=='1':
        rev += 1
    else:
        if rev%2 == 1:
            S = S[::-1]
            rev = 0
        
        if Qs[i][1] == '1':
            S = Qs[i][2] + S
        else:
            S = S + Qs[i][2]

print(S)

Submission status: Wrong Answer

Solution code:
import sys
import math
MAX_INT = int(10e15)
MIN_INT = -MAX_INT
mod = 1000000007
sys.setrecursionlimit(1000000)
def IL(): return list(map(int,input().split()))
def SL(): return input().split()
def I(): return int(sys.stdin.readline())
def S(): return input()

s = S()
q = I()
que = [S() for i in range(q)]
#print(que)

st = ""
en = ""
f = 0
for i in range(q):
  n = que[i]
  if n == "1":
    f += 1
  else:
    t,ff,c = list(n.split())
    if (f+int(ff))%2 == 0:
      st += c
    else:
      en += c

#print(st)
#print(en)

if f%2 == 0:
  print(en[::-1] + s + st)
else:
  print(st[::-1] + s + en)

Submission status: Wrong Answer

Solution code:
def main():

    S = input()
    Q = int(input())
    count = 0
    A = ""
    B = ""
    for _ in range(Q):
        U = input()
        if len(U) == 1:
            count += 1
        else:
            _, F, C = U.split()
            F = int(F)
            if F == 1:
                if count % 2 == 0:
                    A += C
                else:
                    B += C
            else:
                if count % 2 == 0:
                    B += C
                else:
                    A += C

    if count % 2 == 0:
        return A[::-1] + S + B
    else:
        return B[::-1] + S + A

if __name__ == '__main__':
    print(main())

Submission status: Wrong Answer

Solution code:
s = input()
q = int(input())
que = [list(input().split()) for _ in range(q)]
counter = 0
for i in que:
    if i[0] == "1":
        counter += 1
    else:
        if (counter+int(i[1]))%2==1:
            s = s+i[2]
        else:
            s = i[2]+s
if counter%2==1:
    print(s)
else:
    print(s[::-1])

Submission status: Wrong Answer

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

for i in range(Q):
    query = input().split()
    if query[0] == "1":
        if len(S) > 1:
            S = S[-1] + S[1: -1] + S[0]
    else:
        f_i = int(query[1])
        c_i = query[2]
        if f_i == 1:
            S = c_i + S
        else:
            S = S + c_i
print(S)

Submission status: Wrong Answer

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

rev = False
fore = []
end = []

for i in range(q):
    l = input().split()
    if l[0] == "1":
        rev ^= True
    else:
        if l[1] == "1":
            if rev == False:
                fore.append(l[2])
            else:
                end.append(l[2])
        else:
            if rev == False:
                end.append(l[2])
            else:
                fore.append(l[2])

if rev == False:
    print("".join(reversed(fore)) + s + "".join(end))
else:
    print("".join(reversed(end)) + s + "".join(fore))

Submission status: Wrong Answer

Solution code:
s=input()
q=int(input())
x=y=z=""
i=0
for _ in range(q):
    a=list(input().split())
    if a[0]=="1":
        i+=1
        y=x[::-1]
        x=z[::-1]
        z=y
    else:
        if a[1]=="1":x+=a[2]
        else:z+=a[2]
print(x+s[::-1]+z if i%2 else x+s+z)

Submission status: Wrong Answer

Solution code:
S = input()
Q = int(input())
prev_t = 0
cnt = 0
for _ in range(Q):
    q = input()
    if len(q)>1:
        _, f, c = q.split()
        if (f=='1' and prev_t==0) or (f=='2' and prev_t==1):
            S = c+S
        else:
            S = S+c
        prev_t = 0
    else:
        prev_t = 1
        cnt+=1
if cnt%2==0:
    print(S)
else:
    print(S[::-1])

Submission status: Wrong Answer

Solution code:
s=input()
q=int(input())
f=0
l,r='',''
for i in range(q):
    a=list(input().split())
    if a[0]=='1':
        f+=1
    else:
        if int(a[1])^(f%2)==0:
            r+=a[2]
        else:
            l+=a[2]
if f%2==1:
    print(r+s+l)
else:
    print(l[::-1]+s+r[::-1])

Submission status: Wrong Answer

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

heads_min_idx = 0
tails_max_idx = 0
heads = {}
tails = {}
rev_flag = 1  # 1 or -
for q in range(Q):
    tmp_q = input()
    if tmp_q == '1':
        rev_flag *= -1
    else:
        _, f, c = tmp_q.split()
        if rev_flag == 1:
            if f == '1':
                heads[heads_min_idx - 1] = c
                heads_min_idx -= 1
            else:
                tails[tails_max_idx + 1] = c
                tails_max_idx += 1
        else:
            if f == '1':
                tails[tails_max_idx + 1] = c
                tails_max_idx += 1
            else:
                heads[heads_min_idx - 1] = c
                heads_min_idx -= 1

if rev_flag == 1:
    head_str = ''.join([s for _, s in sorted(heads.items(), key=lambda x: x[1])])
    tail_str = ''.join([s for _, s in sorted(tails.items(), key=lambda x: x[1])])
    print(head_str + S + tail_str)
else:
    head_str = ''.join([s for _, s in sorted(tails.items(), key=lambda x: x[1], reverse=True)])
    tail_str = ''.join([s for _, s in sorted(heads.items(), key=lambda x: x[1], reverse=True)])
    print(head_str + S[::-1] + tail_str)

Submission status: Wrong Answer

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

front = ''
back = ''

T_count = 0

for i in range(Q):
    command = input().split()
    if command[0] == '1':
        T_count = (T_count+1)%2
    else:
        if (int(command[1]) + T_count)%2 == 1:
            front += command[2]
        else:
            back += command[2]

if T_count == 0:
    print(front[::-1]+S+back)
else:
    print(back[::-1]+S+front)

Submission status: Wrong Answer

Solution code:
# list(input())
# list(map(int, input().split()))
# int(input())
from collections import deque
S = deque(list(input()))
Q = int(input())
rv_flag = False
queries = []
for _ in range(Q):
    q = list(input().split())
    if len(q) == 1:
        rv_flag = not rv_flag
        continue
    _, f, c = q
    if rv_flag:
        if f == '1':
            queries.append([c, 'head'])
        else:
            queries.append([c, 'tail'])
    else:
        if f == '1':
            queries.append([c, 'tail'])
        else:
            queries.append([c, 'head'])

for q in queries:
    c, flag = q
    if flag == 'head':
        S.appendleft(c)
    else:
        S.append(c)

S = list(S)
if not rv_flag:
    S = S[::-1]
print("".join(S))

Submission status: Wrong Answer

Solution code:
# import bisect
from collections import Counter, deque
# import copy
# from heapq import heappush, heappop, heapify
# from fractions import gcd
# import itertools
# from operator import attrgetter, itemgetter
import math

import sys

# import numpy as np

ipti = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)

def main():
    s = input()
    q = int(input())

    add_head = deque()
    add_tail = deque()
    is_reversed = False

    for _ in range(q):
        q = list(map(str,input().split()))
        if q[0] == "1":
            if is_reversed:
                is_reversed = False
            else:
                is_reversed = True
        else:
            if q[1] == "1":
                if is_reversed:
                    add_tail.append(q[2])
                else:
                    add_head.append(q[2])
            else:
                if is_reversed:
                    add_head.append(q[2])
                else:
                    add_tail.append(q[2])

    ans = []

    if is_reversed:
        add_tail.reverse()
        ans += add_tail
        ans += s
        ans += add_head
    else:
        add_head.reverse()
        add_tail
        ans += add_head
        ans += s
        ans += add_tail

    print(''.join(ans))



if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

reverse_status = False

for i in range(q):
  query = list(map(format, input().split()))
  if int(query[0]) == 1:
    reverse_status = True
  else:
    if int(query[1]) == 1:
      if reverse_status:
        s = s + query[2]
      else:
        s = query[2] + s
    else:
      if reverse_status:
        s = query[2] + s
      else:
        s = s + query[2]

if reverse_status:
  print(''.join(list(reversed(s))))
else:
  print(s)

Submission status: Wrong Answer

Solution code:
from collections import deque
S = input()
Q = int(input())
d = deque()
d.append(S)
reverse = False
for _ in range(Q):
    tmp = input().split()
    q = int(tmp[0])
    if q == 1:
        reverse = not reverse
    elif q == 2:
        f = int(tmp[1])
        if f == 1:
            if reverse:
                d.append(tmp[2])
            else:
                d.appendleft(tmp[2])
        elif f == 2:
            if reverse:
                d.appendleft(tmp[2])
            else:
                d.appendleft(tmp[2])
S = "".join(d)
if reverse:
    print(S[::-1])
else:
    print(S)

Submission status: Wrong Answer

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

inversion = 0
head = []
tail = []

for _ in range(Q):
    queri = input()
    if len(queri) == 1:
        inversion += 1
    else:
        _, f, c = queri.split()
        if inversion % 2 == 0 and f == "1":
            head.append(c)
        elif inversion % 2 == 1 and f == "1":
            tail.append(c)
        elif inversion % 2 == 0 and f == "2":
            tail.append(c)
        else:
            head.append(c)

#print("head={}, tail={}".format(head, tail))

if inversion % 2 == 0:
    head.reverse()
    Head = ''.join(head)
    Tail = ''.join(tail)
    print(Head + S + Tail)
else:
    tail.reverse()
    Head = ''.join(head)
    Tail = ''.join(tail)
    print(Tail + S + Head)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
s = input()
q = int(input())
inv_flg = False
for _ in range(q):
    query = input()
    if query[0] == '1':
        inv_flg = not inv_flg
    else:
        _, f, c = query.split()
        if inv_flg:
            if f == '1':
                s = c + s
            else:
                s = s + c
        else:
            if f == '1':
                s = s + c
            else:
                s = c + s
print(s if inv_flg else s[::-1])

Submission status: Wrong Answer

Solution code:
import sys
from collections import *
import heapq
import math
import bisect
from itertools import permutations,accumulate,combinations,product
from fractions import gcd
def input():
    return sys.stdin.readline()[:-1]
def ruiseki(lst):
    return [0]+list(accumulate(lst))
mod=pow(10,9)+7

s=input()
q=int(input())
query=[input().split() for i in range(q)]
# print(query)
now=0
d=deque(s.split())
# print(d)
for i in range(q):
    if query[i][0]=="1":
        now+=1
    else:
        if now%2==0:
            if query[i][1]=="1":
                d.appendleft(query[i][2])
            else:
                d.append(query[i][2])
        else:
            if query[i][1]=="1":
                d.append(query[i][2])
            else:
                d.appendleft(query[i][2])
if now%2==1:
    d.reverse()
print("".join(d))

Submission status: Wrong Answer

Solution code:
import math
from collections import deque
def main():
    s = deque(input())
    q = int(input())

    counter = 0
    for _ in range(q):
        query = [t for t in input().split()]
        if len(query)==1:
            counter +=1

        else:
            f = (counter+int(query[1]))%2
            if query[1]==1:
                s.appendleft(query[2])
            else:
                s.append(query[2])

    s = list(s)
    if counter%2==0:
        s = s[::-1]

    for c in s:
        print(c,end='')

    print()


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
from collections import defaultdict, Counter, namedtuple, deque
from bisect import bisect_left
from functools import reduce
import math

NN = 2020202
MOD = 10**9+7
INF = float("inf")


S = list(input().split())
Q = int(input())
query = [list(input().split()) for i in range(Q)]
que = deque(S)

for q in query:
    if len(q) == 1:
        if len(que) >= 2:
            front = que.popleft()
            back = que.pop()
            que.append(front)
            que.appendleft(back)
    else:
        if q[1] == "1":
            que.appendleft(q[2])
        else:
            que.append(q[2])
    # print(que)

print("".join(que))

Submission status: Wrong Answer

Solution code:
from sys import stdin

s = stdin.readline().rstrip().split()
q = int(stdin.readline().rstrip())

for i in range(q):
    t = stdin.readline().rstrip()
    if t == "1":
        s = s[::-1]
    else:
        t2 = t.split()
        if t2[1] == "1":
            s.insert(0, t2[2])
        else:
            s.append(t2[2])

print("".join(s))

Submission status: Wrong Answer

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

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

A = []
B = []
revFlag = False;

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

    if q[0] == '1':
        revFlag = not revFlag
        A,B = B,A
    else:
        if q[1] == '1':
            A.append(q[2])
        else:
            B.append(q[2])


if revFlag:
    A,B = B,A
    


S2 = ''.join(reversed(A)) + S + ''.join(reversed(B))

if revFlag:
    S2 = ''.join(reversed(S2))

print(S2)

Submission status: Wrong Answer

Solution code:
S = [input()]
q = int(input().strip())

for i in range(q):
  query = input()
  if query[0] == "1":
    S.reverse()
  elif query[0] == "2":
    query = query.strip().split(" ")
    if query[1] == "1":
      S.insert(0, query[2])
    else:
      S.append(query[2])

print("".join(S))

Submission status: Wrong Answer

Solution code:
from collections import deque
d = deque()
s = input()
d.append(s)
rev = 0

Q = int(input())

for q in range(Q):
    ls = input().split()
    print (ls)
    if ls[0] == '1':
        rev = (rev + 1) % 2
    else:
        if (int(ls[1]) + rev) % 2 == 0:
            d.append(ls[2])
        else:
            d.appendleft(ls[2])

ans = ''.join(list(d))
if rev == 1:
    ans = ans[::-1]
print (ans)

Submission status: Wrong Answer

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

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

print(''.join(S) if d == -1 else ''.join(S[::-1]))

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-


def solve(inp):
    S = inp.readline().strip()
    Q = int(inp.readline().strip())
    Queries = [inp.readline().strip() for i in range(Q)]

    r1 = []
    r2 = []
    dir = False
    for Query in Queries:
        if Query == "1":
            dir = not dir
        else:
            (T, F, C) = Query.split(' ')
            if dir == (F == "1"):
                r1.append(C)
            else:
                r2.append(C)

    if dir:
        return "".join(reversed(r1)) + S + "".join(r2)
    else:
        return "".join(reversed(r2)) + S + "".join(r1)


def main():
    import sys
    result = solve(sys.stdin)
    if result is not None and result != '':
        print(result)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

L = list(input().strip())
Q = int(input())
rev = True
for i in range(Q):
    q = list(map(str, input().split()))
    if len(q) == 1:
        rev = not rev
    else:
        t, f, c = q
        if rev is False and f == '1':
            L.insert(0, c)
        if rev is False and f == '2':
            L.append(c)
        if rev is True and f == '1':
            L.append(c)
        if rev is True and f == '2':
            L.insert(0, c)
if rev:
    L.reverse()

print(''.join(L))

Submission status: Wrong Answer

Solution code:
a = input()
q = int(input())
key = [0]*len(a)
for i in range(len(a)):
    key[i] = a[i]

    
for i in range(q):
    qinput = [str(x) for x in input().split()]
    if len(qinput) == 1:
        key.reverse()
    else:
        k = qinput[1]
        if k == "1":
            key.append(qinput[2])
        else:
            key.reverse()
            key.append(qinput[2])
            key.reverse()

print("".join(key))

Submission status: Wrong Answer

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

s = input()
q = int(input())
hanten = 0
ans = deque(s)
for _ in range(q):
    a = list(input().split())
    if a[0] == "1":
        hanten += 1
    else:
        if hanten % 2 == 0:
            if a[1] == "1":
                ans.appendleft(a[2])
            if a[1] == "2":
                ans.append(a[2])
        else:
            if a[1] == "1":
                ans.append(a[2])
            if a[1] == "2":
                ans.appendleft(a[2])
if hanten % 2 == 0:
    print(*ans, sep="")
else:
    ans.reverse()
    print(*ans, sep="")

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
from collections import deque
def main():
    S = deque( [input().strip()])
    Q = int( input())
    Query = [ input().strip() for _ in range(Q)]
    now = 0

    for q in Query:
        if q == '1':
            now ^= 1
            continue
        _, f, c = q.split()
        if f == '1':
            if now == 0:
                S.appendleft(c)
            else:
                S.append(c)
        else:
            if now == 0:
                S.append(c)
            else:
                S.appendleft(c)

    if now == 1:
        S.reverse()
    print("".join(S))
    
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
from collections import deque

def answer():
    s = input().split()
    s = deque(s)
    q = int(input())
    rev = False
    for i in range(q):
        query = input().split()
        if int(query[0]) == 2:
            f, c = int(query[1]), query[2]
            if (not rev and f == 1) or (rev and f == 2):
                s.appendleft(c)
            else:
                s.append(c)
        else:
            rev = not rev
    if rev:
        s = list(s)
        for c in s[::-1]:
            print(c, end='')
        print()
        return
    for c in s:
        print(c, end='')
    print()
    return

answer()

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**6)
from math import floor,ceil,sqrt,factorial,log
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict,deque
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
from copy import deepcopy
from operator import itemgetter
from fractions import gcd
mod = 10 ** 9 + 7
inf = float('inf')
ninf = -float('inf')
 
#整数input
def ii(): return int(sys.stdin.readline().rstrip()) #int(input())
def mii(): return map(int,sys.stdin.readline().rstrip().split())
def limii(): return list(mii()) #list(map(int,input().split()))
def lin(n:int): return [ii() for _ in range(n)]
def llint(n: int): return [limii() for _ in range(n)]
#文字列input
def ss(): return sys.stdin.readline().rstrip() #input()
def mss(): return sys.stdin.readline().rstrip().split()
def limss(): return list(mss()) #list(input().split())
def lst(n:int): return [ss() for _ in range(n)]
def llstr(n: int): return [limss() for _ in range(n)]

#本当に貪欲法か？ DP法では？？
#本当に貪欲法か？ DP法では？？
#本当に貪欲法か？ DP法では？？

s=ss()
q=ii()

arr=deque([s])
chk=0
for i in range(q):
    x=input()
    if x=="1":
        if chk==1:
            chk=0
        else:
            chk=1
    
    else:
        j,k,l=map(str,x.split())
        if chk==0 and k=="1":
            arr.appendleft(l)
        elif chk==0 and k=="2":
            arr.append(l)
        elif chk==1 and k=="1":
            arr.append(l)
        
        elif chk==1 and k=="2":
            arr.appendleft(l)
if len(list(arr))==1:
    print(s)
    exit()

if chk==0:
    print("".join(list(arr)))
else:
    print("".join(reversed(list(arr))))

Submission status: Wrong Answer

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

import sys
input = sys.stdin.readline

qs = [list(input().split()) for _ in range(q)]

left = ''
right = ''


front = True
for i in qs:
    if(i[0] == '1'):
        front = not front
        continue

    f = int(i[1])
    c = i[2]
    if(front + f == 2):
        right = right + c
    else:
        left = left + c

if(not front):
    ans = left[::-1] + s + right
else:
    ans = right[::-1] + s[::-1] + left

print(ans)

Submission status: Wrong Answer

Solution code:
# ABC158d


def main():
    import sys
    sys.setrecursionlimit(10**6)
    # 再帰関数を使わない限りPypyで出すこと

    def dump(*args):
        sys.stderr.write(str(args))

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

    mode = 0
    from collections import deque
    que = deque()
    que.append(s)
    for i in query:
        if len(i) == 1:
            mode = 1-mode
            continue
        f = int(i[1])
        if mode == 0:
            if f == 1:
                que.appendleft(i[2])
            else:
                que.append(i[2])
        else:
            if f == 1:
                que.append(i[2])
            else:
                que.appendleft(i[2])
    mode = 1 if mode == 0 else - 1
    qq = list(que)
    print(*(qq[::mode]), sep='')


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
s = input()
sr = s
q = int(input())
T_F = True

for i in range(q):
  query = list(input().split())
  if query[0] == '1':
    T_F = not T_F
  elif not((query[1] == '1') ^ T_F):
    s = query[2] + s
    sr = sr + query[2]
  else:
    s = s + query[2]
    sr= query[2] + sr
if T_F:
  print(s)
else:
  print(sr)

Submission status: Wrong Answer

Solution code:
import sys
sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))
from collections import deque
def resolve():
    S = deque(list(sr().split()), maxlen=1000000)
    Q = ir()
    rev = 1
    for i in range(Q):
        T = list(sr().split())
        if T[0] == '1':
            rev *= -1
        else:
            x, F, C = T
            if F == '1':
                if rev == 1:
                    S.appendleft(C)
                else:
                    S.append(C)
            else:
                if rev == 1:
                    S.append(C)
                else:
                    S.appendleft(C)
    if rev == -1:
        S.reverse()
    print(*S, sep='')
resolve()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/python3
# -*- coding: utf-8 -*-
s = list(map(str, input().split()))
n = int(input())
for i in range(0, n):
    q_list = list(map(str, input().split()))
    if len(q_list) == 1:
        s.reverse()
    else:
        if q_list[1] == "1":
            s.insert(0, q_list[2])
        else:
            s.append(q_list[2]) 

print(''.join(s))

Submission status: Wrong Answer

Solution code:
s = input()
q = int(input())
appendix = [[], []]
reverse = False
for _ in range(q):
  query = input().split()
  if query[0] == '1':
    reverse ^= True
  else:
    f = bool(int(query[1]) - 1)
    appendix[int(f ^ reverse)].append(query[2])
print(''.join(appendix[int(reverse)][::-1]) + s + ''.join(appendix[int(not reverse)]))

Submission status: Wrong Answer

Solution code:
import sys
fastinput = sys.stdin.readline
from collections import deque
S = input()
Q = int(input())
ans = deque([S])
Reverse = False
for q in range(Q):
    query = fastinput().split()
    if query[0]=="1":
        Reverse = not Reverse
    elif (query[1]=="1") ^ Reverse:#左側にくっつく
        ans.appendleft(query[2])
    else:
        ans.append(query[2])
SS = "".join(list(ans))
if Reverse:
    SS = "".join(reversed(ans))
print(SS)

Submission status: Wrong Answer

Solution code:
s = input()
now = 1
q = int(input())
Query = [ list(map(str,input().split())) for _ in range(q)]
#print(*Query)
adhere = ""
back = ""
for i in range(q):
    if len(Query[i]) == 3:
        t = int(Query[i][0])
        f = int(Query[i][1])
        c = Query[i][2]
        if now == 1:
            back += c
        else:
            adhere = c + adhere
    else:
        now *= -1
        tmp_adhere = back
        back = adhere[::-1]
        adhere = tmp_adhere[::-1]
        
if now == -1:
    ans = adhere + s[::-1] + back
    #print(-1)
else:
    ans = adhere + s + back
    #print(1)
print(ans[::-1])

Submission status: Wrong Answer

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

for i in range(n):
  Q = [i for i in input().split()]
  if Q[0] =='1':
    S = S[-1] + S[1:-1]+S[0]
    #print(S)
  else:
    if Q[1]=='1':
      S = Q[2]+S
    else:
      S =S+Q[2]
    #print(S)
print(S)

Submission status: Wrong Answer

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

s = readline().rstrip().decode('utf-8')
q = int(readline())
flag = 0
lst1 = []
lst2 = []
for i in range(q):
    t = readline().rstrip().decode('utf-8')
    if t[0] == "1":
        #reverse
        flag = 1-flag
    else:
        com,f,c = t.split()
        if f == "1": #先頭にc追加
            if not flag:
                lst1.append(c)
            else:
                lst2.append(c)
            

        else: #末尾にc追加
            if not flag:
                lst2.append(c)
            else:
                lst1.append(c)

if not flag:
    lst1.reverse()
    s = "".join(lst1) + s + "".join(lst2)
else:
    lst2.reverse()
    s = "".join(lst2) + s + "".join(lst1)

print(s)

Submission status: Wrong Answer

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


import numpy as np
num_reverses = np.zeros(shape=(Q,))
add_char = []
f_list = []
for i in range(Q):
  in_ = input()
  if len(in_) > 1:
    flag, f, c = in_.split()
    add_char.append(c)
    f_list.append(int(f))
  else:
    # reverse
    flag = "1"
    num_reverses[:i] += 1
    f_list.append(0)
    add_char.append("")
    
ans = S

for num_r, c, f in zip(num_reverses, add_char, f_list):
  if num_r % 2 == 0:
    if f == 1:
      ans = c + ans
    elif f == 2:
      ans = ans + c
  elif num_r % 2 == 1:
    if f == 2:
      ans = c + ans
    elif f == 1:
      ans = ans + c 
  
    
    
print(ans)

Submission status: Wrong Answer

Solution code:
from collections import deque
s = deque(input())
q = int(input())
for i in range(q):
    query = input()
    if query[0] == '1':
        a = s.popleft()
        b = s.pop()
        s.appendleft(b)
        s.append(a)
    else:
        _, f, c = query.split()
        if f == '1':
            s.appendleft(c)
        else:
            s.append(c)
print(''.join(list(s)))

Submission status: Wrong Answer

Solution code:
s = input()
n = int(input())
isReverse=False
for i in range(n):
  A = input().split(" ")
  if A[0]=="1":
    isReverse = not isReverse
  else:
    if (A[1]=="1" and isReverse) or (A[1]=="0" and not isReverse):
      s = s+A[2]  
    else:
      s = A[2]+s

if isReverse:
  s = ''.join(list(reversed(s)))
print(s)

Submission status: Wrong Answer

Solution code:
s=input()
q=int(input())
f=0
l,r='',''
for i in range(q):
    a=list(input().split())
    if a[0]=='1':
        f+=1
    else:
        if int(a[1])^f==0:
            r+=a[2]
        else:
            l+=a[2]
if f%2==1:
    print(r+s+l)
else:
    print(l[::-1]+s+r[::-1])