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())
for _ in range(q):
  temp=input().split()
  if int(temp[0])==1:
    s=s[::-1]
  elif int(temp[0])==2:
    if int(temp[1])==1:
      s=s+temp[2]
    elif int(temp[1])==2:
      s=temp[2]+s
print(s)

Submission status: Wrong Answer

Solution code:
import sys
S = str(input())
Q = int(input())
Query = [list(map(str, input().split())) for _ in range(Q)]

sArry = S.split()
for i in range(Q):
#    print(i,Query[i])
    if Query[i][0] == '1':
        tmp_first = sArry[0]
        sArry[0] = sArry[-1]
        sArry[-1] = tmp_first
    else:
        if Query[i][1] == '1':
            sArry.insert(0, Query[i][2])
        else:
            sArry.append(Query[i][2])
            
print("".join(sArry))

Submission status: Wrong Answer

Solution code:
def switch(S):
    tmp = S[0]
    S[0] = S[-1]
    S[-1] = tmp
    return S


def d():
    S = list(str(input()))
    Q = int(input())

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

        if q[0] == '1':
            S = switch(S)
        elif q[0] == '2':
            if q[1] == '1':
                S.insert(0, q[2])
            elif q[1] == '2':
                S.append(q[2])
            else:
                raise ValueError(
                    'F({}) is {}, which is invalid!'.format(i, q[1]))
        else:
            raise ValueError('T({}) is {}, which is invalid!'.format(i, q[0]))

    print(''.join(S))

Submission status: Wrong Answer

Solution code:
S = input()
Q = int(input())
flag = 1
for _ in range(Q):
    query = input()
    if len(query) == 1:
        flag = flag ^ 1
    else:
        F, C = int(query[2])-1, query[4]
        S = [C+S, S+C][flag^F]
print([S, S[::-1]][flag])

Submission status: Wrong Answer

Solution code:
S=input()
b=0
x=''
y=''
for i in range(int(input())):
    q=input()
    if q=='1':b+=1
    else:
        a,f,c=q.split()
        if (int(f)-1)^(b%2):x=c+x
        else:y=y+c
S=x+S+y
print(S if b^1==0 else S[::-1])

Submission status: Wrong Answer

Solution code:
from collections import deque

S  = deque()
S.append(input().strip())

N = int(input().strip())

for _ in range(N):
    ope = input().strip()
    if ope[0] == "1":
        S.reverse()
        continue
    i, s, c = ope.split(" ")
    if s == "1":
        S.appendleft(c)
    else:
        S.append(c)

print(''.join(S))

Submission status: Wrong Answer

Solution code:
import math

s=input()
S=[]
S.append(s)
q=input()
Q=int(q)

#print(S,Q)

for i in range(Q):
  qu = input()
  #print(qu)
  T = int(qu[0])
  
  if T == 1:
    s=[]
    for j in range(len(S)):
      s.append(S[-1-j])
    S=s               
  else:
    F = int(qu[2])
    if F == 1:
      s=[]
      for j in range(len(S)):
        s.append(S[-1-j])
      S=s
      
      #print(S)
      
      S.append(qu[4])

      #print(S)
      
      s=[]
      for j in range(len(S)):
        s.append(S[-1-j])        
      S=s
      #print(S)
    else:
      S.append(qu[4])
  #print(S)
      
L="".join(S)
print(L)
#print(S)

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):
    line = stdin.readline().rstrip().split()
    if line[0] == '1':
        s = s[::-1]
    elif line[1] == '1':
        s.insert(0, line[2])
    elif line[1] == '2':
        s.append(line[2])

print(''.join(s))

Submission status: Wrong Answer

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

forward_flag = 1
for i in range(Q):
    n = list(map(str, input().split()))
#     print(n)
    
    if len(n)==1:
        if forward_flag==1:
            forward_flag = 0
        else:
            forward_flag = 1
    else:
        a, b, c = n
        if (forward_flag==1 and b=="1") or (forward_flag==0 and b=="2"):
            S=c+S
        else:
            S=S+c
#     if forward_flag==1:
#         print(S)
#     else:
#         print(S[::-1])
print(S)

Submission status: Wrong Answer

Solution code:
from collections import deque
S=input()
Q=int(input())
d = deque([S])
flag=0
dic={}
dic[S]=1
for i in range(Q):
    x=input().split()
#     print(x)
    if len(x)==1:
        if len(dic.keys()) >1:
            flag=(flag+1)%2
    else:
        if len(dic.keys()) ==1:
            dic[x[2]]=1
            
        if int(x[1])==1:
            if flag==0:
                d.appendleft(x[2])
            else:
                d.append(x[2])
        else:
            if flag==0:
                d.append(x[2])
            else:
                d.appendleft(x[2])
if flag==0:
    print(*d, sep="")
else:
    d=list(reversed(d))
    print(*d, sep="")

Submission status: Wrong Answer

Solution code:
S = input()
S_h = ''
S_t = ''
Q = int(input())

def add_car(S_h,S_t,s,is_left,is_rev):
    if is_rev:
        is_left = not(is_left)
    if is_left:
        S_h = ''.join([S_h,s])
    else:
        S_t = ''.join([S_t,s])
    return S_h,S_t

is_rev = False
for q in range(Q):
    x = input().split(' ')
    if len(x) == 1:
        is_rev = not is_rev
    else:
        S_h,S_t = add_car(S_h,S_t,x[2],x[1]=='1',is_rev)

if is_rev:
    S_t = ''.join(list(reversed(S_t)))
    print(''.join([S_t,S,S_h]))
else:
    S_h = ''.join(list(reversed(S_h)))
    print(''.join([S_h,S,S_t]))

Submission status: Wrong Answer

Solution code:
def swap(s):
    if len(s)==1:
        return s
    elif len(s)==2:
        return s[1]+s[0]
    else:
        return s[-1]+s[1:-1]+s[0]

s=input()
q=int(input())
for i in range(q):
    qu=[i for i in input().split(" ")]
    if len(qu)==1:
        s=swap(s)
    else:
        c=qu[2]
        if int(qu[1])==1:
            s=c+s
        else:
            s=s+c

print(s)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
from collections import deque
s = input()
q = int(input())
tmp = deque()
tmp.append(s)
flag = 0
for _ in range(q):
    _s = input()
    if _s == "1":
        flag += 1
    else:
        t, f, c = _s.split(" ")
        r = flag % 2
        if f == "1" and r == 0:
            #tmp = c + tmp
            tmp.appendleft(c)
        elif f == "1" and r == 1:
            #tmp = tmp + c
            tmp.append(c)
        elif f == "2" and r == 0:
            #tmp = tmp + c
            tmp.append(c)
        elif f == "2" and r == 1:
            #tmp = c + tmp
            tmp.appendleft(c)
if flag % 2 == 0:
    for i in range(len(tmp)):
        print(tmp.popleft(), end="")
else:
    for i in range(len(tmp)):
        print(tmp.pop(), end="")

Submission status: Wrong Answer

Solution code:
S = input()
Q = int(input())
query = ""
list_1 = []
list_2 = []
mod_list = []
counter = 0
for i in range(Q):
    list_1.append(input())

for i in range(Q):
    if list_1[i] == "1":
        counter += 1
        if i == Q-1:
            mod_list.append(str(counter % 2))
    else:
        if counter != 0:
            mod_list.append(str(counter%2))
        mod_list.append(list_1[i])
        counter = 0

for query in mod_list:

    if query == "1":
        S = S[::-1]
    elif query != "0":
        list_2 = query.split()
        if list_2[1] == "1":
            S = list_2[2] + S
        else:
            S = S + list_2[2]

print(mod_list)
print(S)

Submission status: Wrong Answer

Solution code:
S = input() 
Q = int(input())
for i in range(Q):
    lists = input().split()
    if lists[0] == "1":
        pass
    else:
        pass
print(S)

Submission status: Wrong Answer

Solution code:
S=input()
n=int(input())
l = [list(input()) for _ in range(n)]
z=1
for i in range(n):
    if l[i][0]=='1':
        #new_S = S[::-1]
        #S=new_S
        z=z*(-1)
    else:
        a=l[i][4]
        if l[i][2]=='1'and z==1:
            S=a+S
        elif l[i][2]=='1'and z==1:
            S=S+a
        elif  l[i][2]=='1'and z==-1:
            S=S+a
        else:
            S=a+S
if z==1:
  print(S)
else:
  print(S[::-1])

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
fg = True
flag = False
for i in range(q):
    if len(que[i]) == 1:
        if fg:
            if len(list(d)) == 1:
                fg = False
        else:
            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:
from collections import deque

s = deque()
s.append(input())
q = int(input())

reverse_status = False

head_str = ''
tail_str = ''

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

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

Submission status: Wrong Answer

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

for i in range(q):
    query = input()
    try:
        int(query)
        t = query
    except:
        t, f, c = query.split()

    if t == "1":
        s = s[-1] + s[1:-1] + s[0]
    else:
        if f == "1":
            s = c + s
        else:
            s = s + c

print(s)

Submission status: Wrong Answer

Solution code:
S = input()
Q = int(input())
reverse = False
front = ""
end = ""

for i in range(Q):
    inp = input()
    if inp[0] == "1":
        reverse = ~reverse
    else:
        _, F, C = inp.split()
        if (F == "1" and reverse == False) or (F == "2" and reverse == True):
            front = front+C
        else:
            end = end+C

if reverse:
    print(end[::-1]+S[::-1]+front)
else:
    print(front[::-1]+S+end)

Submission status: Wrong Answer

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

for _ in range(q):
  in_str = input().split()
  if len(in_str) == 1:
    f = not f
  else:
    if in_str[1] == '1':
      if f:
      	s.insert(0, in_str[2])
      else:
        s.insert(-1, in_str[2])
    else:
      if f:
        s.insert(-1, in_str[2])
      else:
        s.insert(0, in_str[2])

if f:
  print(''.join(s))
else:
  print(''.join(reversed(s)))

Submission status: Wrong Answer

Solution code:
s = input()
q = int(input())
f = []
for i in range(q):
    ff = [i for i in input().split()]
    f.append(ff)
ans = [s]
cnt = 0
for i in range(q):
    if f[i][0] == '1':
        cnt += 1
    else:
        if f[i][1] == '1':
            if cnt % 2 == 0:
                ans.insert(0,f[i][2])
            else:
                ans.append(f[i][2])
        else:
            if cnt % 2 == 0:
                ans.append(f[i][2])
            else:
                ans.insert(0,f[i][2])
if cnt % 2 == 0:
    print(''.join(map(str,ans)))
else:
    ans2 = []
    for i in (reversed(ans)):
        ans2.append(i)
    print(''.join(map(str,ans2)))

Submission status: Wrong Answer

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

ans = [s]
for i in range(q):
    a = input()
    if len(a) == 1:
        ans.reverse()
    else:
        t, p, w = a.split()
        if p == "1":
            ans.insert(0, w)
        else:
            ans.append(w)
print("".join(ans))

Submission status: Wrong Answer

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

print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
s=[input()]
q=int(input())
for i in range(q):
  aa=list(map(str,input().split()))
  if len(aa)==1:
    s.reverse()
  else:
    if aa[1]=="1":
      s.insert(0,aa[2])
    else:
      s.append(aa[2])
print(*s,sep='')

Submission status: Wrong Answer

Solution code:
S = input()
Q = int(input())
Query = [input() for i in range(Q)]
inf_string = [""] * Q
inf_numbers = [0] * Q
#反転回数のトータルを記録
total_numbers = 0
# 0->先頭, 1->末尾
inf_first_position = [0] * Q
index = 0

for i in range(Q-1,-1,-1):
    # Ti = 1の時 反転する
    if len(Query[i]) == 1:
        total_numbers += 1
    else:
        # 先頭に追加
        if Query[i][2] == "1":
            inf_string[index] = Query[i][4]
            inf_numbers[index] = total_numbers
            inf_first_position[index] = 0
        # 末尾に追加
        else:
            inf_string[index] = Query[i][4]
            inf_numbers[index] = total_numbers
            inf_first_position[index] = 1
        index += 1

for i in range(index-1,-1,-1):
    if (inf_numbers[i] + inf_first_position[i]) % 2 == 0:
        S = inf_string[i] + S
    else:
        S = S + inf_string[i]
print(S)

Submission status: Wrong Answer

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

right=[]
left=[]
flag=0

for i in range(q):
    a = input()
    if int(a[0]) == 1:
        if flag == 0:
            flag=1
        elif flag == 1:
            flag=0
    else:
        if int(a[2]) == 1:
            if flag == 0:
                right.append(a[4])
            else:
                left.insert(0,a[4])
        elif int(a[2]) ==2:
            if flag ==0:
                left.insert(0,a[4])
            else:
                right.append(a[4])

right_result="".join(right)
left_result="".join(left)

if flag==1:
    print(str(left_result[::-1]+s+right_result))
else:
    print(str(right_result[::-1]+s[::-1]+left_result))

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**6)


def main():
    s = list(input())
    q = int(input())
    s.pop(-1)
    a = [''] * (10**5*2)
    a.extend(list(s))
    a.extend([''] * (10**5*2))
    start = 10**5*2
    end = start + len(s) - 1
    for i in range(q):
        query = input().split()
        if query[0] == '1':
            tmp = a[start]
            a[start] = a[end]
            a[end] = tmp
        elif query[0] == '2':
            if query[1] == '1':
                a[start-1] = query[2]
                start -= 1
            elif query[1] == '2':
                a[end+1] = query[2]
                end += 1
    print(''.join(a[start:end+1]))


main()

Submission status: Wrong Answer

Solution code:
S = input().split()
N = int(input())
reverse = 0
r_b = []
r_f = []
for i in range(N):
    line = input().split()
    if line[0] == '1' :
        reverse = (reverse + 1) % 2
    else :
        if reverse == 0 :
            if line[1] == '1' :
                r_f = [line[2]] + r_f
            else :
                r_b.append(line[2])
        else:
            if line[1] == '1':
                r_b.append(line[2])
            else:
                r_f = [line[2]] + r_f

if reverse == 0 :
    S = r_f + S + r_b
    print("".join(S))
else:
    S.reverse()
    r_b.reverse()
    r_f.reverse()
    S = r_b + S + r_f
    print("".join(S))

Submission status: Wrong Answer

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

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


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

print(ans)

Submission status: Wrong Answer

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

string_formation(s, q_list)

Submission status: Wrong Answer

Solution code:
s=input()
q = int(input())
toggle = "ok"
top = ""
last = ""

for i in range(q):
    si = input().split()
    if si[0] == "1":
        if toggle == "ok":
            toggle = "re"
        else:
            toggle = "ok"
    else:
        if si[1] == "1" and toggle == "ok":
            top += si[2] 
        elif si[1] == "2" and toggle == "re":
            top += si[2] 
        elif si[1] == "1" and toggle == "re":
            last += si[2]
        elif si[1] == "2" and toggle == "ok":
            last += si[2]
if toggle == "ok":
    retop = top[::-1]
    print(retop+s+last)
else:
    relast = last[::-1]
    print(relast+s+top)

Submission status: Wrong Answer

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

Q_list = []

for i in range(Q):
  Q_list.append(list(map(str, input().split())))

for i in range(Q):                
  if len(Q_list[i]) == 1:
    if len(S) >= 2:
      S_list = list(S)
      S_list[0], S_list[-1] = S_list[-1], S_list[0]
      S = "".join(S_list)
  
  else:
    F = Q_list[i][1]
    C = Q_list[i][2]
    
    if int(F) == 1:
      S = C + S
    else:
      S = S + C
      
print(S)

Submission status: Wrong Answer

Solution code:
S=[]
s=input()
S.append(s)
Q=int(input())
for i in range(Q):
    r=input()
    if len(r.split()) == 3:
        T,F,C=r.split()
    else:
        T='1'
    if T == '1':
        tmp=S[0]
        S[0]=S[len(S)-1]
        S[len(S)-1]=tmp
        continue
    if T == '2':
        if F == '1':
            #S.insert(0,C)
            pass
        if F == '2':
            S.append(C)

print("".join(S))

Submission status: Wrong Answer

Solution code:
import sys
from collections import deque

input = sys.stdin.readline


class Solution:
    def main(self):
        s = input().rstrip()
        q = int(input())

        deq = deque()

        for c in s:
            deq.append(c)

        for i in range(q):
            tfc = list(input().split())
            if int(tfc[0]) == 1:
                if len(deq) == 1:
                    continue
                f = deq.popleft()
                b = deq.pop()
                deq.appendleft(b)
                deq.append(f)
                continue
            if int(tfc[1]) == 1:
                deq.appendleft(tfc[2])
            else:
                deq.append(tfc[2])

        print("".join(deq))


# Run main
if __name__ == '__main__':
    Solution().main()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
from collections import deque
s = input()
q = int(input())
result_q = deque(s)
t = False
for _ in range(q):
    List=[i for i in input().split()]
    if List[0] == '1':
        t = True
    else:
        if List[1] == '1': # F=1
            if t: # 反転時
                result_q.append(List[2])
            else:
                result_q.appendleft(List[2])
        else:
            if t: # 反転時
                result_q.appendleft(List[2])
            else:
                result_q.append(List[2])
if t:
    result_q.reverse()
for s in list(result_q):
    print(s, end="")

Submission status: Wrong Answer

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

rev=0
for _ in range(Q):
    q=list(input().split())
    if int(q[0])==1:
        rev+=1
    else:
        if (int(q[1])+rev)%2==1:
            S=q[2]+S
            rev=0
        else:
            S=S+q[2]
            rev=0

if (rev+1) %2==1:
    print(S[::-1])
else:
    print(S)

Submission status: Wrong Answer

Solution code:
import sys
if __name__ == '__main__':
    lines = sys.stdin.readlines()
    line = [a.strip('\n').split(' ') for a in lines]
    # print(line)
    right = []
    left = []
    count = 0

    for i,v in enumerate(line):
        if i == 0:
            st = v
        if i == 1:
            q = v
        if i >= 2:
            if len(v)==3:
                for j,k in enumerate(v):
                    if j == 1:
                        ichi = int(k)
                    if j==2:
                        alp = k
                if count%2 == 0:
                    if ichi == 1:
                        left.append(alp)
                        # print('left'+str(left))
                    if ichi == 2:
                        right.append(alp)
                        # print('right'+str(right))
                else:
                    if ichi == 1:
                        right.append(alp)
                        # print('right'+str(right))
                    if ichi == 2:
                        left.append(alp)
                        # print('left' + str(left))

            if len(v)==1:
                count += 1
    
    # print(left)
    # print(right)
    if count%2 == 0:
        d = list(reversed(left))+st+right
    if count%2 == 1:
        d = list(reversed(right))+list(reversed(st))+left
    print(''.join(d))

Submission status: Wrong Answer

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

c = 0
R = ""
L = ""
for _ in range(Q):
    T = input().split()
    if len(T) == 1:
        c +=1
        L_d = L
        L = "".join(list(reversed(R)))
        R = "".join(list(reversed(L_d)))
    else:
        if T[1] == "1":
            L = T[2] + L
        else:
            R = R + T[2]
if c % 2:
    print(L + S + R)
else:
    print(L + "".join(list(reversed(S))) + R)

Submission status: Wrong Answer

Solution code:
s = list(input())
q = int(input())
s0 = []
s1 = []
for _ in range(q):
    ql = list(input().split())
    if ql[0] == '1':
        s0, s1 = list(reversed(s1)), list(reversed(s0))
        # s0, s1 = s1[::-1], s0[::-1]
    else:
        if ql[1] == '1':
            s0 += ql[2]
        else:
            s1 += ql[2]
    # print(s0 + s + s1)

ans = s0 + s + s1
print(*ans, sep='')

Submission status: Wrong Answer

Solution code:
S = input()
Q = int(input())
ans = S
## vector が1なら正方向、-1なら逆方向
vector = 1
before = ""
after = ""
for i in range(Q):
    Query = input()
    if Query[0] == "1":
        vector *= -1
    elif vector == 1 and Query[0:3] == "2 1":
        before += Query[4]
    elif vector == 1 and Query[0:3] == "2 2":
        after = Query[4]+after
    elif vector == -1 and Query[0:3] == "2 1":
        after += Query[4]
    elif vector == -1 and Query[0:3] == "2 2":
        before = Query[4] + before
#     print(before)
#     print(after)
if vector==1:
    print(before + S + after)
else:
    print(after[::-1]+S[::-1]+before[::-1])

Submission status: Wrong Answer

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

forb = 1

for i in range(q):
    order = str(input())
    if len(order) == 1:
        forb = forb * (-1)

    elif order[2] == '1':
        if forb == 1:
            s = order[4] + s
        else:
            s = s + order[4]
    else:
        if forb == 1:
            s = s + order[4]
        else:
            s = order[4] + s
print(s)

Submission status: Wrong Answer

Solution code:
from heapq import heappush, heappop, heapify
from collections import deque, defaultdict, Counter
import itertools
from itertools import permutations, combinations, accumulate
import sys
import bisect
import string
import math
import time


def I(): return int(input())


def S(): return input()


def MI(): return map(int, input().split())


def MS(): return map(str, input().split())


def LI(): return [int(i) for i in input().split()]


def LI_(): return [int(i)-1 for i in input().split()]


def StoI(): return [ord(i)-97 for i in input()]


def ItoS(nn): return chr(nn+97)


def input(): return sys.stdin.readline().rstrip()


def show(*inp, end='\n'):
    if show_flg:
        print(*inp, end=end)


YNL = {False: 'No', True: 'Yes'}
YNU = {False: 'NO', True: 'YES'}
MOD = 10**9+7
inf = float('inf')
IINF = 10**10
l_alp = string.ascii_lowercase
u_alp = string.ascii_uppercase
ts = time.time()
sys.setrecursionlimit(10**6)
nums = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']

show_flg = False
# show_flg = True


def main():
    st = S()
    Q = I()
    queries = []
    # print(st[0])
    # s = [st[i] for i in range(len(st))]
    s = deque()

    for i in range(len(st)):
        s.append(st[i])

    for i in range(Q):
        queries.append(list(MS()))

    op = True

    for i in range(Q):
        query = queries[i]
        if len(query) == 1:
            op = not op
        else:
            if (query[1] == '1' and op is True) or (query[1] == '2' and op is False):
                s.appendleft(query[2])
            elif (query[1] == '1' and op is False) or (query[1] == '2' and op is True):
                s.append(query[2])
            else:
                exit(-1)
                return

    if op is True:
        print(''.join(s))
    else:
        n = []
        while len(s):
            n.append(s.pop())
        print(''.join(n[::-1]))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys; input = sys.stdin.readline
def main():
  S = input()[0]
  Q = int(input())
  for i in range(Q):
    TFC = input().split()
    S = S[::-1] if TFC[0]=="1" else (TFC[2]+S if TFC[1]=="1" else S+TFC[2])
  print(S)

if __name__ == "__main__":
  main()

Submission status: Wrong Answer

Solution code:
from sys import stdin

def main():

    input = stdin.readline

    S = input()[:-1]
    Q = int(input())

    Query = [0] * Q
    
    ans_list = [S]

    for _ in range(Q):
        query = input().split()
        if(query[0] == '1'):
            ans_list.reverse()
        else:
            if(query[1] == '1'):
                ans_list.insert(0,query[2])
            else:
                ans_list.append(query[2])
    
    print(''.join(ans_list))


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
s=input()
n=int(input())
l=[]

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


def re(s):
    return s[::-1]
    
def fa(s,a):
    a=a+s
    return a

def la(s,a):
    s=s+a
    return s

for i in range(n):
    if len(l[i])==1:
        print("1")
        s=re(s)
    elif l[i][1]=='1':
        s=fa(s,l[i][2])
    else:
        s=la(s,l[i][2])
   
print(s)

Submission status: Wrong Answer

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

hanten = 0
t = [S]
for l in L:
    if len(l) == 1:
        hanten += 1
    elif (l[1] == '1' and hanten % 2 == 0) or (l[1] == '2' and hanten % 2 == 1):
        t.insert(0, l[2])
    else:
        t.append(l[2])
if hanten % 2 == 1:
    t.reverse()
ans = ''.join(t)

print(ans)

Submission status: Wrong Answer

Solution code:
S = list(input())
Q = int(input())
Y = [0, 0, 0]

for i in range(Q):
    X = list(input())
    if X[0] == "1":
        A = S[0]
        B = S[-1]
        S[0] = B
        S[-1] = A
    elif X[0] == "2":
        Y[0] = int(X[0])
        Y[1] = int(X[2])
        Z = X[4]
        if Y[1] == 1:
            S.insert(0, Z)
        elif Y[1] == 2:
            S.append(Z)


print(*S, sep="")

Submission status: Wrong Answer

Solution code:
from collections import deque
import sys
sys.setrecursionlimit(10 ** 7)
readline = sys.stdin.readline


def main():
    a = input()
    d = deque(a)
    n = int(input())
    for i in range(n):
        j = list(readline().split())
        if len(j) == 3 and j[1] == "1":
            d.appendleft(j[2])
        elif len(j) == 3 and j[1] == "2":
            d.append(j[2])
        else:
            d.reverse()
    ans = ""
    e = list(d)
    for j in e:
        ans = ans + j
    print(ans)