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: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
rflag = False
l = len(S)

for i in range(Q):
    t = input()
    if t[0] == "1":
        if rflag == False:
            rflag = True

        else:
            rflag = False

    else:
        t_2, f, c = t.split()
        l += 1

        if (f == '1' and rflag == False) or (f == '2' and rflag == True):
            S = c + S
        else:
            S = S + c

if rflag == True:
    S = S[::-1]
print(S)

Submission status: Time Limit Exceeded

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

rev_flag = 0

for i in range(Q):
    if Query[i][0] == '1':
        rev_flag = (rev_flag + 1) % 2
    if Query[i][0] == '2':
        if Query[i][1] == '1':
            if rev_flag == 0:
                S = Query[i][2] + S
            elif rev_flag == 1:
                S = S + Query[i][2]
        elif Query[i][1] == '2':
            if rev_flag == 1:
                S = Query[i][2] + S
            elif rev_flag == 0:
                S = S + Query[i][2]

if rev_flag == 1:
    S = S[::-1]

print(S)

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
rflag = False
l = len(S)

for i in range(Q):
    t = input()
    if len(t) == 1:
        if rflag == False:
            rflag = True

        else:
            rflag = False

    else:
        t_2, f, c = t.split()
        l += 1

        if (f == '1' and rflag == False) or (f == '2' and rflag == True):
            S = c + S
        else:
            S = S + c

if rflag == True:
    S = S[::-1]
print(S)

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
Query = [input() for _ in range(Q)]
flag = True
for i in range(Q):
    if Query[i][0] == "1":
        if flag:
            flag = False
        else:
            flag = True
    else:
        if Query[i][2] == '1':
            if flag:
                S = Query[i][4] + S
            else:
                S = S + Query[i][4]
        else:
            if flag:
                S = S + Query[i][4]
            else:
                S = Query[i][4] + S
if flag:
    print(S)
else:
    print(S[::-1])

Submission status: Time Limit Exceeded

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

l = len(S)

for i in range(Q):
    t = input()
    if len(t) == 1:
        S = S[::-1]

    else:
        t_2, f, c = t.split()
        l += 1

        if f == '1':
            S = c + S
        else:
            S = S + c
print(S)

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
Query = [input() for _ in range(Q)]

for i in range(Q):
    if Query[i][0] == "1":
        # 反転
        S = S[::-1]
    else:
        if Query[i][2] == '1':
            S = Query[i][4] + S
        else:
            S = S + Query[i][4]
print(S)

Submission status: Time Limit Exceeded

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

cnt = 0

for i in range(q):
    if procedure[i][0] == '1':
        cnt += 1
    if procedure[i][0] == '2':
        if (procedure[i][1] == '1' and cnt%2 == 0) or (procedure[i][1] == '2' and cnt%2 == 1):
            S.insert(0,procedure[i][2])
        else:
            S.append(procedure[i][2])

if cnt%2 == 1:
    S.reverse()
    
print(''.join(S))

Submission status: Time Limit Exceeded

Solution code:
s = input()
n = int(input())
query_li = (input().split() for i in range(n))

flag = 1

for query in query_li:
    if int(query[0]) == 1:
        flag = flag * -1
    else:
        if int(query[1]) == 1:
            s = query[2] + s if flag == 1 else s + query[2]
        else:
            s = s + query[2] if flag == 1 else query[2] + s

if flag == -1:
    s = s[::-1]

print(s)

Submission status: Time Limit Exceeded

Solution code:
s = input()
n = int(input())
query_li = (input().split() for i in range(n))

for query in query_li:
    if int(query[0]) == 1:
        s = s[::-1]
    else:
        if int(query[1]) == 1:
            s = query[2] + s
        else:
            s = s + query[2]

print(s)

Submission status: Time Limit Exceeded

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

for query in query_li:
    if int(query[0]) == 1:
        s = s[::-1]
    else:
        if int(query[1]) == 1:
            s = query[2] + s
        else:
            s = s + query[2]

print(s)

Submission status: Time Limit Exceeded

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

for query in query_li:
    if int(query[0]) == 1:
        s = s[::-1]
    else:
        if int(query[1]) == 1:
            s = query[2] + s
        else:
            s = s + query[2]

print(s)

Submission status: Time Limit Exceeded

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

for i in range(0, Q):
    query = input().split()
    if int(query[0]) == 1:
        S = S[::-1]
    else:
        if int(query[1]) == 1:
            S = query[2] + S
        else:
            S = S + query[2]

print(S)

Submission status: Time Limit Exceeded

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

S_list = list(S)

for i in range(Q):
    Query = input()
    #Query = Query.replace(' ', '')
    #Query = list(Query)
    T, F, C = 0, 0, ''
    if Query[0] == '2':
        T = int(Query[0])
        F = int(Query[2])
        C = Query[4]
    else:
        T = int(Query[0])
    
    if T == 1:
        S_list = S_list[::-1]
        
    if T == 2:
        if F == 1:
            S_list.insert(0, C)
        else:
            S_list.append(C)
            
print(''.join(S_list))

Submission status: Time Limit Exceeded

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

S_list = list(S)

for i in range(Q):
    Query = input()
    #Query = Query.replace(' ', '')
    #Query = list(Query)
    T, F, C = 0, 0, ''
    if Query[0] == '2':
        T = int(Query[0])
        F = int(Query[2])
        C = Query[4]
    else:
        T = int(Query[0])
    
    if T == 1:
        S_list = S_list[::-1]
        
    if T == 2:
        if F == 1:
            S_list = S_list[::-1]
            S_list.append(C)
            S_list = S_list[::-1]
        else:
            S_list.append(C)
            
print(''.join(S_list))

Submission status: Time Limit Exceeded

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

S_list = list(S)

for i in range(Q):
    Query = input()
    Query = Query.replace(' ', '')
    Query = list(Query)
    T, F, C = 0, 0, ''
    if len(Query) == 3:
        T = int(Query[0])
        F = int(Query[1])
        C = Query[2]
    else:
        T = int(Query[0])
    
    if T == 1:
        S_list = S_list[::-1]
        
    if T == 2:
        if F == 1:
            S_list = S_list[::-1]
            S_list.append(C)
            S_list = S_list[::-1]
        else:
            S_list.append(C)
            
print(''.join(S_list))

Submission status: Time Limit Exceeded

Solution code:
a=input()
Q=int(input())
for i in range(Q):
    q=input()
    if len(q)==1:
        a=a[::-1]
    else:
        # q1=q.split(" ")
        if q[2]=='1':
            a=q[4]+a
        else:
            a=a+q[4]
print(a)

Submission status: Time Limit Exceeded

Solution code:
a=input()
Q=int(input())
l=[]
for i in range(Q):
    q=input()
    l.append(q)
for j in l:
    if len(j)==1:
        a=a[::-1]
    else:
        q1=j.split(" ")
        if q1[1]=='1':
            a=q1[2]+a
        else:
            a=a+q1[2]
print(a)

Submission status: Time Limit Exceeded

Solution code:
a=input()
Q=int(input())
for i in range(Q):
    q=input()
    if len(q)==1:
        a=a[::-1]
    else:
        q1=q.split(" ")
        if q1[1]=='1':
            a=q1[2]+a
        else:
            a=a+q1[2]
print(a)

Submission status: Time Limit Exceeded

Solution code:
a=input()
Q=int(input())
for i in range(Q):
    q=input()
    if len(q)==1:
        a=a[::-1]
    else:
        q1=q.split(" ")
        if q1[1]=='1':
            a=q1[2]+a
        elif q1[1]=='2':
            a=a+q1[2]
print(a)

Submission status: Time Limit Exceeded

Solution code:
a=input()
Q=int(input())
for i in range(Q):
    q=input()
    if len(q)==1 and q=='1':
        a=a[::-1]
    else:
        q1=q.split(" ")
        if q1[1]=='1':
            a=q1[2]+a
        elif q1[1]=='2':
            a=a+q1[2]
print(a)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
S = input().strip()
Q = int(input().strip())
flag = 1
for i in range(Q):
  line = input().strip()
  if len(line) == 1:
    flag = 1 - flag
  else:
    _, F, C = line.strip().split()
    if flag:
      if int(F) == 1:
        S = C + S
      else:
        S = S + C
    else:
      if int(F) == 1:
        S = S + C
      else:
        S = C + S

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

Submission status: Time Limit Exceeded

Solution code:
S = input().strip()
Q = int(input().strip())
flag = 1
for i in range(Q):
  line = input().strip()
  if len(line) == 1:
    flag = 1 - flag
  else:
    _, F, C = line.strip().split()
    if flag:
      if int(F) == 1:
        S = C + S
      else:
        S = S + C
    else:
      if int(F) == 1:
        S = S + C
      else:
        S = C + S

if flag:
  print(S)
else:
  print(''.join([c for c in reversed(list(S))]))

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

revFlag = False
s = deque(input())
q = int(input())

for i in range(q):
    a,b,c, *_ = input().split() + [None, None]
    
    if(a == '1'):
        revFlag = not revFlag
    else:
        if(revFlag):
            s.reverse()
            revFlag = False
        
        if(b == '1'):
            s.appendleft(c)
        else:
            s.append(c)

if(revFlag):
    s.reverse()
    
print(*s, sep='')

Submission status: Time Limit Exceeded

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

point = 0
for i in range(n):
  q = input().split()
  if q.pop(0)=='1':
    point = (point+1)%2
  else:
    _q = int(q.pop(0))-1
    if (point+_q)%2 == 0:
      s.insert(0,q.pop(0))
    else:
      s.append(q.pop(0))
      
if point == 1:
  s.reverse()

print(''.join(s))

Submission status: Time Limit Exceeded

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

for i in range(n):
  q = input().split()
  if q.pop(0)=='1':
    s.reverse()
  else:
    if q.pop(0)=='1':
      s.insert(0,q.pop(0))
    else:
      s.append(q.pop(0))

print(''.join(s))

Submission status: Time Limit Exceeded

Solution code:
# https://atcoder.jp/contests/abc158/tasks/abc158_d

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

elements = [None for _ in range(Q * 2 + len(S) * 2)]

head = Q + len(S)
last = len(S)
for i, s in enumerate(S):
    elements[head + i] = s

for _ in range(Q):
    query = input()
    if query[0] == '1':
        elements[head:head + last] = elements[head + last - 1:head - 1:-1]
    else:
        if query[2] == '1':
            head -= 1
            elements[head] = query[4]
        else:
            elements[head + last] = query[4]

        last += 1

elements = filter(lambda a: a is not None, elements)
print(''.join(elements))

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
q_lis = []
for i in range(q):
    tmp_q = input()
    if tmp_q == '1':
        q_lis.append([1])
    else:
        q_num1, q_num2, q_c = tmp_q.split()
        q_lis.append([int(q_num1), int(q_num2), q_c])
rev = False
for i in range(q):
    if q_lis[i][0] == 1:
        rev = not rev
    else:
        if (q_lis[i][1] == 1 and rev == False) or (q_lis[i][1] == 2 and rev == True):
            s = q_lis[i][2] + s
        else:
            s = s + q_lis[i][2]
if rev == True:
    print(s[::-1])
else:
    print(s)

Submission status: Time Limit Exceeded

Solution code:
# https://atcoder.jp/contests/abc158/tasks/abc158_d

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

for _ in range(Q):
    query = input()
    if query[0] == '1':
        S = S[::-1]
    else:
        if query[2] == '1':
            S = query[4] + S
        else:
            S = S + query[4]

print(S)

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
q_lis = []
for i in range(q):
    tmp_q = input()
    if tmp_q == '1':
        q_lis.append([1])
    else:
        q_num1, q_num2, q_c = tmp_q.split()
        q_lis.append([int(q_num1), int(q_num2), q_c])
for i in range(q):
    if q_lis[i][0] == 1:
        s = s[::-1]
    else:
        if q_lis[i][1] == 1:
            s = q_lis[i][2] + s
        else:
            s = s + q_lis[i][2]
print(s)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

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

for i in range(q):
    a,b,c, *_ = input().split() + [None, None]
    
    if(a == '1'):
        s.reverse()
    else:
        if(b == '1'):
            s.appendleft(c)
        else:
            s.append(c)

print(*s, sep='')

Submission status: Time Limit Exceeded

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


def getQuery():
    yield input().split()


for _ in range(q):
    for query in getQuery():
        if query[0] == "1":
            s = s[::-1]
        else:
            if query[1] == "1":
                s = query[2] + s
            else:
                s += query[2]

print(s)

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
#lst = [input() for i in range(q)]

for i in range(q):
  inp = input()
  if inp[0] == "1":
    s = s[::-1]
  else:
    cmd = inp.split()
    if cmd[1] == "1":
      s = cmd[2] + s
    else:
      s = s + cmd[2]

print(s)

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
lst = [input() for i in range(q)]

for i in lst:
  if i[0] == "1":
    s = s[::-1]
  else:
    cmd = i.split()
    if cmd[1] == "1":
      s = cmd[2] + s
    else:
      s = s + cmd[2]

print(s)

Submission status: Time Limit Exceeded

Solution code:
s = input()
Q = int(input())
for i in range(Q):
    t = input()
    if len(t) == 1:
        s = s[::-1]
    else:
        if t[2] == '1':
            s = t[4] + s
        else:
            s += t[4]
print(s)

Submission status: Time Limit Exceeded

Solution code:
import sys
import math

S = list(input())
Q = input()

for i in range(int(Q)):
  Query = input().split()
  if len(Query) == 1:
    S.reverse()
  else:
    C = Query[2]
    if int(Query[1]) == 1:
      S.insert(0,C)
    else:
      S.insert(len(S),C)

print(''.join(S))

Submission status: Time Limit Exceeded

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

for i in range(Q):
    TFC = input()
    T = int(TFC.split(' ')[0])

    if T == 1:
        S = S[::-1]#毎回実際に反転処理すると時間がかかるので、現在どちらが先頭かの情報だけ持つようにする
    elif T == 2:
        F = int(TFC.split(' ')[1])
        C = TFC.split(' ')[2]
        if F == 1:
            S = C + S
        elif F ==2:
            S = S + C

print(S)

Submission status: Time Limit Exceeded

Solution code:
s=input()
n=len("s")
q=int(input())
l=[]
start=""
end=""
m=0
for i in range(q):
    l.append(input())
    if l[i]=="1":
        m += 1

    else:
        due,f,c=l[i].split()
        if (f=="1" and m % 2==0) or (f !="1" and m % 2==1) :
            start = start + c
        else:
            end = end + c
if m % 2 ==0:

    start=start[::-1]
    s = start + s + end

else:
    s = s + end
    s = s[::-1]
    s = s + start

print(s)

Submission status: Time Limit Exceeded

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

for i in range(q):
    if procedure[i][0] == '1':
        S.reverse()
    if procedure[i][0] == '2':
        if procedure[i][1] == '1':
            S.insert(0,procedure[i][2])
        else:
            S.append(procedure[i][2])

print(''.join(S))

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
b = True
for _ in range(q):
  query = input()
  if query == "1":
    b = not(b)
  else:
    t, f, c = query.split()
    if int(f) + b == 2:
      s = c + s
    else:
      s = s + c
print(s if b else s[::-1])

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
s=input()
n=len("s")
q=int(input())
l=[]
start=""
end=""
m=0
for i in range(q):
    l.append(input())
    if l[i]=="1":
        m += 1

    else:
        due,f,c=l[i].split()
        if (f=="1" and m % 2==0) or (f !="1" and m % 2==1) :
            start += c
        else:
            end += c
if m % 2 ==0:

    start=start[::-1]
    s = start + s + end

else:
    s = s + end
    s = s[::-1]
    s = s + start

print(s)

Submission status: Time Limit Exceeded

Solution code:
s=input()
n=len("s")
q=int(input())
l=[]
start=""
end=""
m=0
for i in range(q):
    l.append(input())
    if l[i]=="1":
        m += 1

    else:
        due,f,c=l[i].split()
        if (f=="1" and m % 2==0) or (f !="1" and m % 2==1) :
            start += c
        else:
            end += c
start=start[::-1]
s = start + s + end

if m % 2 ==1:

    s=s[::-1]

print(s)

Submission status: Time Limit Exceeded

Solution code:
s=input()
n=len("s")
q=int(input())
l=[]
m=0
for i in range(q):
    l.append(input())
    if l[i]=="1":
        m += 1

    else:
        due,f,c=l[i].split()
        if (f=="1" and m % 2==0) or (f !="1" and m % 2==1) :
            s = c+s
        else:
            s = s+c
if m % 2 ==1:
    s=s[::-1]

print(s)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
S = input()
lQ = list(range(int(input())))
def main(S):
  T = True
  for i in lQ:
    q = list(input().split())
    if q[0]=='1':
      T = not(T)
    else:
      if (T and q[1]=='1') or (not(T) and q[1]=='2'):
        S = q[2] + S
      elif (T and q[1]=='2') or (not(T) and q[1]=='1'):
        S = S + q[2]
  if T:
    print(S)
  else:
    print(S[::-1])
main(S)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
T = True
for i in range(Q):
  q = list(input().split())
  if q[0]=='1':
    T = not(T)
  else:
    if (T and q[1]=='1') or (not(T) and q[1]=='2'):
      S = q[2] + S
    elif (T and q[1]=='2') or (not(T) and q[1]=='1'):
      S = S + q[2]
if T:
  print(S)
else:
  print(S[::-1])

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
mode = True
for _ in range(q):
    tfc = input().split()
    if int(tfc[0]) == 1:
        mode = not mode
    else:
        if mode:
            s = tfc[2]+s if int(tfc[1]) == 1 else s+tfc[2]
        else:
            s = s+tfc[2] if int(tfc[1]) == 1 else tfc[2]+s
print(s if mode else s[::-1])

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
mode = True
for _ in range(q):
    tfc = input().split()
    if int(tfc[0]) == 1:
        mode = not mode
    else:
        if mode == 1:
            s = tfc[2]+s if int(tfc[1]) == 1 else s+tfc[2]
        else:
            s = s+tfc[2] if int(tfc[1]) == 1 else tfc[2]+s
print(s if mode else s[::-1])

Submission status: Time Limit Exceeded

Solution code:
str = input()
times = int(input())
for i in range(times):
    com = input().split()
    if com[0] == "1":
        str = str[::-1]
    else :
        if com[1] == "1":
            str = com[2]+str
        else :
            str += com[2]            

print(str)

Submission status: Time Limit Exceeded

Solution code:
s = input()
n = int(input())
query = []
for i in range(n):
    query.append(input().split())
cond = 1

for i in range(n):
    if query[i] == ['1']:
        cond *= -1
    else:
        if cond == 1:
            if query[i][1] == '1':
                s = query[i][2] + s
            else:
                s = s + query[i][2]
        else:
            if query[i][1] == '2':
                s = query[i][2] + s
            else:
                s = s + query[i][2]
if cond == -1:
    s = s[::-1]
print(s)

Submission status: Time Limit Exceeded

Solution code:
s = input()
n = int(input())
query = []
for i in range(n):
    query.append(input().split())
counter = 0

for i in range(n):
    if query[i] == ['1']:
        counter += 1
        for j in range(n-(i+1)):
            if query[i+j+1] == ['1']:
                continue
            if query[i+j+1][1] == '2':
                query[i+j+1][1] = '1'
            else:
                query[i+j+1][1] = '2'
for i in range(n):
    if query[i] != ['1']:
        if query[i][1] == '1':
            s = query[i][2] + s
        else:
            s = s + query[i][2]
if counter%2 == 1:
    s = s[::-1]
print(s)

Submission status: Time Limit Exceeded

Solution code:
s = input()
n = int(input())
query = []
for i in range(n):
    query.append(input().split())
for i in range(n):
    if query[i] == ['1']:
        s = s[::-1]
    elif query[i][1] == '1':
        s = query[i][2] + s
    else:
        s = s + query[i][2]
print(s)

Submission status: Time Limit Exceeded

Solution code:

string = input()
N=int(input())
Que = []
for i in range(N):
	Que.append(input())

for Q in Que:	
	if Q[0] == '1':
		string = string[::-1]
	else:
		T,F,C = Q.split(' ')
		if F == '1':
			string = C + string
		elif F == '2':
			string += C
print(string)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

sList = deque(list(input()))
numOp = int(input())
finEnd = []
beg = []
end = []
instList = [input().split(' ') for i in range(numOp)]

for m in range(numOp):
  if instList[m - 1] != ['1'] and instList[m] == ['1']:
    beg.append(m)
  if (m + 1) < numOp:
    if instList[m] == ['1'] and instList[m + 1] != ['1']:
      end.append(m)
  else:
    end.append(m)
  finEnd = [e for b, e in zip(beg, end) if (e - b + 1) % 2 != 0 or (e - b + 1) == 0]
  if '2' in instList[m]:
    if instList[m][1] == '2':
      sList.append(instList[m][2])
    else:
      sList.appendleft(instList[m][2])
  elif m in finEnd:
    sList.reverse()

print(''.join(sList))

Submission status: Time Limit Exceeded

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

F, B = [], []
reverse = False
for i in range(Q):
	Query = input().split()
	if Query[0] == '1':
		reverse = not reverse
	else:
		if not reverse:
			if Query[1] == '1':
				F.insert(0, Query[2])
			else:
				B.append(Query[2])
		else:
			if Query[1] == '1':
				B.append(Query[2])
			else:
				F.insert(0, Query[2])

if not reverse:
	ans = "".join(F)
	ans += "".join(S)
	ans += "".join(B)
	print(ans)
else:
	ans = "".join(reversed(B))
	ans += "".join(reversed(S))
	ans += "".join(reversed(F))
	print(ans)
a = 0

Submission status: Time Limit Exceeded

Solution code:
string = input()
for i in range(int(input())):
	Q = input()
	if Q[0] == '1':
		string = string[::-1]
	else:
		T,F,C = Q.split(' ')
		if F == '1':
			string = C + string
		elif F == '2':
			string += C
print(string)

Submission status: Time Limit Exceeded

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

FLAG = 0

for _ in range(Q):
  TMP = list(map(str, input().split()))
  if TMP[0] == "1":
    if FLAG == 0:
      FLAG = 1
      
    else:
      FLAG = 0
    
  else:
    if FLAG == 0:
      if TMP[1] == "1":
        S = [TMP[2]] + S

      else:
        S.append(TMP[2])
        
    else:
      if TMP[1] == "2":
        S = [TMP[2]] + S

      else:
        S.append(TMP[2])

if FLAG == 0:
  print("".join(S))
    
else:
  print("".join(S[::-1]))

Submission status: Time Limit Exceeded

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

FLAG = 0

for _ in range(Q):
  TMP = list(map(str, input().split()))
  if TMP[0] == "1":
    if FLAG == 0:
      FLAG = 1
      
    else:
      FLAG = 0
    
  else:
    if FLAG == 0:
      if TMP[1] == "1":
        S.insert(0, TMP[2])

      else:
        S.append(TMP[2])
        
    else:
      if TMP[1] == "2":
        S.insert(0, TMP[2])

      else:
        S.append(TMP[2])

if FLAG == 0:
	print("".join(S))
    
else:
  print("".join(S[::-1]))

Submission status: Time Limit Exceeded

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

for _ in range(Q):
  TMP = list(map(str, input().split()))
  if TMP[0] == "1":
    S = S[::-1]
    
  else:
    if TMP[1] == "1":
      S.insert(0, TMP[2])
      
    else:
      S.append(TMP[2])
      
print("".join(S))

Submission status: Time Limit Exceeded

Solution code:
S = input()
N = int(input())
is_inversed = False
for i in range(N):
    Q = input()
    if Q[0] == '1':
        is_inversed = not (is_inversed)
    else:
        if (Q[2] == '1' and is_inversed) or (Q[2] == '2' and not (is_inversed)):
            S = S + Q[4]
        else:
            S = Q[4] + S

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

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin
def ip():
    return stdin.readline().split()
s = input()
c = 0
for _ in range(int(input())):
    t = list(ip())
    if t[0] == '1':
        s = s[::-1]
    else:
        if t[1] == '1':
            s = t[2] + s
        else:
            s += t[2]
print(s)

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
reverse = False
for _ in range(Q):
    cmd = input().split()
    if cmd[0] == "1":
        reverse = not reverse
    else:
        top = (cmd[1] == "1") ^ reverse
        S = cmd[2] + S if top else S + cmd[2]
print(S[::-1]) if reverse else print(S)

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
reverse = False
for _ in range(Q):
    cmd = input().split()
    op = cmd[0]
    if op == "1":
        reverse = not reverse
    else:
        top = (cmd[1] == "1") ^ reverse
        if top:
            S = cmd[2] + S
        else:
            S = S + cmd[2]
print(S[::-1]) if reverse else print(S)

Submission status: Time Limit Exceeded

Solution code:
S=input()
Q=int(input())
m=1
while Q>=1:
  tfc=list(input().split())
  if int(tfc[0])==1:
    m+=1
  elif int(tfc[0])==2:
    if (int(tfc[1])==1 and m%2==1) or (int(tfc[1])==2 and m%2==0):
      S=tfc[2]+S
    elif (int(tfc[1])==2 and m%2==1) or (int(tfc[1])==1 and m%2==0):
      S=S+tfc[2]
  Q-=1
if m%2==0:
  S= ''.join(list(reversed(S)))
print(S)

Submission status: Time Limit Exceeded

Solution code:
S=input()
Q=int(input())
m=1
while Q>=1:
  tfc=list(input().split())
  if int(tfc[0])==1:
    m+=1
  elif int(tfc[0])==2:
    if (int(tfc[1])==1 and m%2==1) or (int(tfc[1])==2 and m%2==0):
      S=tfc[2]+S
    elif (int(tfc[1])==2 and m%2==1) or (int(tfc[1])==1 and m%2==0):
      S=S+tfc[2]
  Q-=1
if m%2==0:
  S=S[::-1]
print(S)

Submission status: Time Limit Exceeded

Solution code:
S=input()
Q=int(input())
m=1
n=2
while Q>=1:
  tfc=list(input().split())
  if int(tfc[0])==1:
    m,n=n,m
  elif int(tfc[0])==2:
    if (int(tfc[1])==1 and m==1) or (int(tfc[1])==2 and m==2):
      S=tfc[2]+S
    elif (int(tfc[1])==2 and m==1) or (int(tfc[1])==1 and m==2):
      S=S+tfc[2]
  Q-=1
if m==2:
  S=S[::-1]
print(S)

Submission status: Time Limit Exceeded

Solution code:
S=input()
Q=int(input())
while Q>=1:
  tfc=list(input().split())
  if int(tfc[0])==1:
    S=S[::-1]
  elif int(tfc[0])==2:
    if int(tfc[1])==1:
      S=tfc[2]+S
    elif int(tfc[1])==2:
      S=S+tfc[2]
  Q-=1
print(S)

Submission status: Time Limit Exceeded

Solution code:
S=input()
Q=int(input())
while Q>=1:
  tfc=list(input().split())
  if int(tfc[0])==1:
    S=S[::-1]
  elif int(tfc[0])==2:
    if int(tfc[1])==1:
      S=tfc[2]+S
    elif int(tfc[1])==2:
      S=S+tfc[2]
  Q-=1
ans=''.join(S)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
q_list = [list(input().split()) for _ in range(q)]

flag = 1

for q in q_list:
    if q[0] == '1':
        flag *= -1
    else:
        if flag == 1:
            if q[1] == '1':
                s = q[2] + s
            else:
                s = s + q[2]
        else:
            if q[1] == '2':
                s = q[2] + s
            else:
                s = s + q[2]
if flag == 1:            
    print(s)
else:
    print(s[::-1])

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
q_list = [list(input().split()) for _ in range(q)]

for q in q_list:
    if q[0] == '1':
        s = s[::-1]
    else:
        if q[1] == '1':
            s = q[2] + s
        else:
            s = s + q[2]

print(s)

Submission status: Time Limit Exceeded

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

for i in range(Q):
  TFC = input().split()
  T = int(TFC[0])
  if T == 1:
    # turn over
    turnover = not turnover
  elif T == 2:
    F = int(TFC[1])
    C = TFC[2]
    if turnover:
      if F == 1:
        F = 2
      else:
        F = 1
    if F == 1:
      S = C + S
      # put C before S
    elif F == 2:
      S = S + C
      # put C after S
if turnover:
  S = S[-1::-1]

print(S)