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())

for i in range(Q):
  Query=input()
  if len(Query)==1:
    S=S[::-1]
  else:
    if Query[2]=='1':
      S=S[::-1]
      S+=Query[4]
      S=S[::-1]
    else:
      S+=Query[4]

print(S)

Submission status: Time Limit Exceeded

Solution code:
# problem D 
s = input()
Q = int(input())
    
t = True
    
for i in range(Q):
    # input をforの中に組み込む
    query = input().split()
    if query[0] == '1':
        t = not t
    else:
        if(t and query[1] == '1') or (not t and query[1] == '2'):
            s = ''.join([query[2],s])
        else:
            s = ''.join([s,query[2]])
    
if not t:
    s = s[::-1]
        
print(s)

Submission status: Time Limit Exceeded

Solution code:
# problem D 
s = input()
Q = int(input())
    
summ = 0
    
for i in range(Q):
    # input をforの中に組み込む
    query = input().split()
    if query[0] == '1':
        summ += 1
    else:
        if(summ % 2 == 0 and query[1] == '1') or (summ % 2 == 1 and query[1] == '2'):
            s = ''.join([query[2],s])
        else:
            s = ''.join([s,query[2]])
    
if summ % 2 == 1:
    s = s[::-1]
        
print(s)

Submission status: Time Limit Exceeded

Solution code:
S=input()
Q=int(input())
flag_r=0
for _ in range(Q):
    Query=list(input().split())
    if int(Query[0])==1:
        flag_r=(flag_r+1)%2
    elif int(Query[1])==1+flag_r:
        S=Query[2]+S
    elif int(Query[1])==2-flag_r:
        S=S+Query[2]

if flag_r:# reverseは最後にやる
    S=S[::-1]
print(S)

Submission status: Time Limit Exceeded

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

for _ in range(Q):
    Query=list(input().split())
    if int(Query[0])==1:
        S=S[::-1]#逆順覚える
    elif int(Query[1])==1:
        S=Query[2]+S
    elif int(Query[1])==2:
        S=S+Query[2]
print(S)

Submission status: Time Limit Exceeded

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

for i in range(len(S)):
    L.append(S[i])

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

num = 0

for i in range(q):
    if Q[i][0] == '1':
        num ^= 1
    if Q[i][0] == '2':
        if num == 0:
            if Q[i][1] == '1':
                L.insert(0, Q[i][2])
            else:
                L.append(Q[i][2])
        else:
            if Q[i][1] == '2':
                L.insert(0, Q[i][2])
            else:
                L.append(Q[i][2])

if num:
    L.reverse()

print(''.join(L))

Submission status: Time Limit Exceeded

Solution code:
# problem D
s = input()
Q = int(input())

query = [input().split() for _ in range(Q)]
    
summ = 0
    
for i in query:
    if int(i[0]) == 1:
        summ += 1
    elif (summ % 2 == 0 and int(i[1]) == 1) or (summ % 2 == 1 and int(i[1]) == 2):
        s = ''.join([str(i[2]),s])
    else:
        s = ''.join([s,str(i[2])])
    
if summ % 2 == 1:
    s = s[::-1]
        
print(s)

Submission status: Time Limit Exceeded

Solution code:
# problem D
s = input()
Q = int(input())

query = []
for i in range(0,Q):
    query.append(input().split())
    
summ = 0
hanten = 0
    
for i in query:
    if int(i[0]) == 1:
        summ += 1
    elif (summ % 2 == 0 and int(i[1]) == 1) or (summ % 2 == 1 and int(i[1]) == 2):
        s = ''.join([str(i[2]),s])
    else:
        s = ''.join([s,str(i[2])])
    
if summ % 2 == 1:
    s = s[::-1]
        
print(s)

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
for i in range(Q):
    Queryi = list(map(str, input().split()))
    if Queryi[0] == '1':
        S = S[::-1]
    else: #i[0] == '2'
        if Queryi[1] == '1':
            S = Queryi[2] + S
        else: #i[1] == '2'
            S = S + Queryi[2]
print(S)

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
Query = []
for i in range(Q):
    Queryi = list(map(str, input().split()))
    Query.append(Queryi)
#print(S[::-1])
#print(Query)

for i in Query:
    if i[0] == '1':
        S = S[::-1]
    else: #i[0] == '2'
        if i[1] == '1':
            S = i[2] + S
        else: #i[1] == '2'
            S = S + i[2]

print(S)

Submission status: Time Limit Exceeded

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

for i in range(Q):
    A=Qvery[i].split(" ")
    if len(A)==1:
        S=S[::-1]
    elif len(A)==3:
        if A[1]=="1":
            S=S[::-1]
            S+=A[2]
            S=S[::-1]
        else:
            S+=A[2]
print(S)

Submission status: Time Limit Exceeded

Solution code:
S = str(input())
Q = int(input())
count = 0
for i in range(Q):
    tmp = list(input().split())
    if tmp[0]=="1":
        count += 1
    else:
        if tmp[1]=="1" and count%2==0 or tmp[1]=="2" and count%2==1:
            S = tmp[2]+S
        elif tmp[1]=="2" and count%2==0 or tmp[1]=="1" and count%2==1:
            S = S+tmp[2]
if count%2==0:
    print(S)
else:
    print(S[::-1])

Submission status: Time Limit Exceeded

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

for i in range(Q):
    tmp = list(input().split())
    if tmp[0]=="1":
        S = S[::-1]
    else:
        if tmp[1]=="1":
            S = tmp[2]+S
        else:
            S = S+tmp[2]
print(S)

Submission status: Time Limit Exceeded

Solution code:
# problem D

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

query = []
for i in range(0,Q):
    query.append(input().split())

s = S
    
for i in query:
    if int(i[0]) == 1:
        # s を反転させる
        s = s[::-1]
    elif int(i[1]) == 1:
        # 先頭に i[2] をくっつける
        s = str(i[2]) + s
    else:
        # しっぽに i[2] をくっつける
        s = s + str(i[2])

print(s)

Submission status: Time Limit Exceeded

Solution code:
# problem D

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

query = []
for i in range(0,Q):
    query.append(input().split())

s = S
    
for i in query:
    if int(i[0]) == 1:
        # s を反転させる
        s = s[::-1]
    else:
        if int(i[1]) == 1:
            # 先頭に i[2] をくっつける
            s = str(i[2]) + s
        else:
            # しっぽに i[2] をくっつける
            s = s + str(i[2])

print(s)

Submission status: Time Limit Exceeded

Solution code:
s = str(input())
q = int(input())
w = [list(map(str,input().split())) for i in range(q)]

change = False
str_list = list(s)
for data in w:
    if int(data[0]) == 1:
        change = not change
    else:
        if change:
            change = False
            str_list.reverse()
        if int(data[1]) == 1:
            str_list.insert(0, data[2])
        else:
            str_list.append(data[2])
if change:
    str_list.reverse()
print("".join(str_list))

Submission status: Time Limit Exceeded

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

# for t in try_list:
#     if t[0] == '1':
#         s = ''.join(list(reversed(s)))
#     if t[0] == '2':
#         if t[1] == '1':
#             s = t[2] + s
#         else:
#             s = s + t[2]

# 反転の処理に時間がかかる

head = 0
for i in range(n):
    t = input().split()
    # 先頭
    if t[0] == '1':
        # 末尾
        head += 1
    else:
        if (head + int(t[1])) % 2 == 1:
            s = t[2] + s
        else:
            s = s + t[2]
if head % 2 == 0:
    print(s)
else:
    print(''.join(list(reversed(s))))

Submission status: Time Limit Exceeded

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

try_list = []
for i in range(n):
    t = input().split()
    try_list.append(t)

head = 0
for t in try_list:
    # 先頭
    if t[0] == '1':
        # 末尾
        head += 1
    else:
        if (head + int(t[1])) % 2 == 1:
            s = t[2] + s
        else:
            s = s + t[2]
if head % 2 == 0:
    print(s)
else:
    print(''.join(list(reversed(s))))

Submission status: Time Limit Exceeded

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

try_list = []
for i in range(n):
    t = input().split()
    try_list.append(t)

head = 0
for t in try_list:
    # 先頭
    if t[0] == '1':
        # 末尾
        head += 1
    else:
        # 先頭
        if head % 2 == 0:
            if t[1] == '1':
                s = t[2] + s
            else:
                s = s + t[2]
        else:
            if t[1] == '1':
                s = s + t[2]
            else:
                s = t[2] + s
if head % 2 == 0:
    print(s)
else:
    print(''.join(list(reversed(s))))

Submission status: Time Limit Exceeded

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

try_list = []
for i in range(n):
    t = input().split()
    try_list.append(t)

for t in try_list:
    if t[0] == '1':
        s = ''.join(list(reversed(s)))
    if t[0] == '2':
        if t[1] == '1':
            s = t[2] + s
        else:
            s = s + t[2]

print(s)

Submission status: Time Limit Exceeded

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

for i in range(n):
    t = input().split()
    if t[0] == '1':
        s = ''.join(list(reversed(s)))
    if t[0] == '2':
        if t[1] == '1':
            s = t[2] + s
        else:
            s = s + t[2]

print(s)

Submission status: Time Limit Exceeded

Solution code:
import sys
s = sys.stdin.readline().rstrip()
q = int(sys.stdin.readline().rstrip())

for _ in range(q):
    t = sys.stdin.readline().rstrip()
    if t == '1':
        s = s[::-1]
    else:
        t, f, c = t.split()
        if f == '1':
            s = c + s
        else:
            s = s + c

print(s)

Submission status: Time Limit Exceeded

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

num = 0

import sys

for _ in range(q):
    t = sys.stdin.readline().rstrip()
    if t == '1':
        if num == 0:
            num = 1
        else:
            num = 0
    else:
        t, f, c = t.split()
        if num == 0:
            if f == '1':
                s = c + s
            else:
                s = s + c
        else:
            if f == '2':
                s = c + s
            else:
                s = s + c

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

print(s)

Submission status: Time Limit Exceeded

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

num = 0

import sys

for _ in range(q):
    t = sys.stdin.readline().rstrip()
    if t == '1':
        num += 1
    else:
        t, f, c = t.split()
        if num%2 == 0:
            if f == '1':
                s = c + s
            else:
                s = s + c
        else:
            if f == '2':
                s = c + s
            else:
                s = s + c

if num%2 == 1:
    s = s[::-1]
print(s)

Submission status: Time Limit Exceeded

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

num = 0

for _ in range(q):
    t = input()
    if t == '1':
        num += 1
    else:
        t, f, c = t.split()
        if num%2 == 0:
            if f == '1':
                s = c + s
            else:
                s = s + c
        else:
            if f == '2':
                s = c + s
            else:
                s = s + c

if num%2 == 1:
    s = s[::-1]
print(s)

Submission status: Time Limit Exceeded

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

for _ in range(q):
    t = input()
    if t == '1':
        s = s[::-1]
    else:
        t, f, c = t.split()
        if f == '1':
            s = c + s
        else:
            s = s + c

print(s)

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
z = 0
front = []
back =[]
for i in range(q):
    qi = input()
    try:
        qi1 = int(qi)
        z = abs(z-1)
    except:
        a = qi.split()
        if (a[1] == "1" and z == 0) or (a[1] == "2" and z == 1):
            front.append(a[2])
        else:
            back.append(a[2])
k = len(front)
l = len(back)
if z == 0:
    for i in range(k):
        s = front[i] + s
    for i in range(l):
        s = s + back[i]
    print(s)
else:
    s = s[::-1]
    for i in range(l):
        s = back[i] + s
    for i in range(k):
        s = s + front[i]
    print(s)

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
front = ["" for i in range(q)]
back  = ["" for i in range(q)]
z = 0
k = 0
l = 0
for i in range(q):
    qi = input()
    try:
        qi1 = int(qi)
        z = abs(z-1)
    except:
        a = qi.split()
        if (a[1] == "1" and z == 0) or (a[1] == "2" and z == 1):
            front[k] = a[2]
            k += 1
        else:
            back[l] = a[2]
            l += 1
if z == 0:
    for i in range(k):
        s = front[i] + s
    for i in range(l):
        s = s + back[i]
    print(s)
else:
    s = s[::-1]
    for i in range(l):
        s = back[i] + s
    for i in range(k):
        s = s + front[i]
    print(s)

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
front = ["" for i in range(q)]
back  = ["" for i in range(q)]
z = 0
k = 0
l = 0
for i in range(q):
    qi = input()
    try:
        qi1 = int(qi)
        z = abs(z-1)
    except:
        a = qi.split()
        if (a[1] == "1" and z == 0) or (a[1] == "2" and z == 1):
            front[k] = a[2]
            k += 1
        else:
            back[l] = a[2]
            l += 1

for i in range(k):
    s = front[i] + s
for i in range(l):
    s = s + back[i]
if z == 1:
    s = s[::-1]
print(s)

Submission status: Time Limit Exceeded

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

for i in range(q):
    qi = input()
    try:
        qi1 = int(qi)
        s = s[::-1]
    except:
        a = qi.split()
        if a[1] == "1":
            s = a[2] + s
        else:
            s = s + a[2]
print(s)

Submission status: Time Limit Exceeded

Solution code:
str = input()
Q = int(input())
left = []
right = []
flag = 1
for _ in range(Q):
    line = input().split()
    op1 = int(line[0])
    if op1 == 1:
        flag = -flag
    else:
        op2 = int(line[1])
        char = line[2]
        if op2 == 1 and flag == 1:
            left.append(char)
        elif op2 == 1 and flag == -1:
            right.append(char)
        elif op2 == 2 and flag == 1:
            right.append(char)
        elif op2 == 2 and flag == -1:
            left.append(char)

if flag == -1:
    str = str[::-1]
    for l in left:
        str += l
    for r in right:
        str = r + str
else:
    for l in left:
        str = l + str
    for r in right:
        str += r

print(str)

Submission status: Time Limit Exceeded

Solution code:
str = input()
Q = int(input())
for _ in range(Q):
    line = input().split()
    op1 = int(line[0])
    if op1 == 1:
        str = str[::-1]
    else:
        op2 = int(line[1])
        char = line[2]
        if op2 == 1:
            str = char + str
        else:
            str += char
print(str)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
import sys

def main():
    s = sys.stdin.readline().strip()
    qn = int(sys.stdin.readline().strip())
    qs = [input().split() for _ in range(qn)]

    c = cn = 0
    s = deque(s)

    for q in qs:
        if len(q) == 1:
            cn+=1
        else:
            if cn%2 == 1:
                s.reverse()
            cn = 0

        if len(q) == 3:
            if q[1] == '1':
                s.appendleft(q[2])
            else:
                s.append(q[2])
        c+=1

    if cn%2 == 1:
        s.reverse()
    print('%s' % ''.join(s))

if __name__ == "__main__":

    main()

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
import sys

def main():
    s = sys.stdin.readline().strip()
    qn = int(sys.stdin.readline().strip())
    qs = [input().split() for _ in range(qn)]

    c = cn = 0
    s = deque(s)

    for _ in qs:
        if qs[c][0] == '1':
            cn+=1
        else:
            if cn%2 == 1:
                s.reverse()
            cn = 0

        if qs[c][0] == '2':
            if qs[c][1] == '1':
                s.appendleft(qs[c][2])
            else:
                s.append(qs[c][2])
        c+=1
    if cn%2 == 1:
        s.reverse()
    print('%s' % ''.join(s))

if __name__ == "__main__":

    main()

Submission status: Time Limit Exceeded

Solution code:
a = input()
n = int(input())
tmp = 0 
for i in range(n):
    q = input().split()
    if len(q) == 1:
        tmp += 1
    elif len(q) == 3:
        if q[1] == '1':
            if tmp % 2 == 0:
                a = q[2] + a
            else:
                a = a + q[2]
        elif q[1] == '2':
            if tmp % 2 == 0:
                a = a + q[2]
            else:
                a = q[2] + a

print(a if tmp % 2 == 0 else a[::-1])

Submission status: Time Limit Exceeded

Solution code:
S=list(input())
Q=int(input())
r=1
for i in range(Q):
    T=input().split()
    if T[0]=="1":
        r*=-1
        continue
    if T[1]=="1":
        if r==1:
            S=[T[2]]+S
        else:
            S=S+[T[2]]
        continue
    if r==1:
        S=S+[T[2]]
    else:
        S=[T[2]]+S
if r==1:
    print("".join(S))
else:
    print("".join(list(reversed(S))))

Submission status: Time Limit Exceeded

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

print(a)

Submission status: Time Limit Exceeded

Solution code:
S=list(input())
Q=int(input())
for i in range(Q):
    T=input().split()
    if T[0]=="1":
        S=list(reversed(S))
        continue
    if T[1]=="1":
        S=[T[2]]+S
        continue
    S=S+[T[2]]
print("".join(S))

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

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

flg_reverse = False


def query_2(s, a, b):
    global flg_reverse
    if flg_reverse is False:
        if a == 2:
            s.append(b)
        elif a == 1:
            s.appendleft(b)
    else:
        if a == 1:
            s.append(b)
        elif a == 2:
            s.appendleft(b)


for i in range(q):
    array = list(input().split())
    if len(array) == 1:
        flg_reverse = not flg_reverse
    else:
        query_2(s, int(array[1]), array[2])

if flg_reverse is False:
    for i in s:
        print(i, end='')
else:
    for i in range(len(s)-1, -1, -1):
        print(s[i], end='')

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

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

flg_reverse = False


def query_1():
    global flg_reverse
    if flg_reverse is True:
        flg_reverse = False
    else:
        flg_reverse = True



def query_2(s, a, b):
    global flg_reverse
    if flg_reverse is False:
        if a == 2:
            s.append(b)
        elif a == 1:
            s.appendleft(b)
    else:
        if a == 1:
            s.append(b)
        elif a == 2:
            s.appendleft(b)


for i in range(q):
    array = list(input().split())
    if len(array) == 1:
        query_1()
    else:
        query_2(s, int(array[1]), array[2])

if flg_reverse is False:
    for i in s:
        print(i, end='')
else:
    for i in range(len(s)-1, -1, -1):
        print(s[i], end='')

Submission status: Time Limit Exceeded

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

s = list(s)

flg_reverse = False


def query_1():
    global flg_reverse
    if flg_reverse is True:
        flg_reverse = False
    else:
        flg_reverse = True



def query_2(s, a, b):
    global flg_reverse
    if flg_reverse is False:
        if a == 2:
            s.append(b)
        elif a == 1:
            s.insert(0, b)
    else:
        if a == 1:
            s.append(b)
        elif a == 2:
            s.insert(0, b)


for i in range(q):
    array = list(input().split())
    if len(array) == 1:
        query_1()
    else:
        query_2(s, int(array[1]), array[2])

if flg_reverse is False:
    for i in s:
        print(i, end='')
else:
    for i in range(len(s)-1, -1, -1):
        print(s[i], end='')

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

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

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

for i in range(q) :
  t = list(map(str,input().split()))
  if t[0] == '1' :
    s.reverse()
  if t[0] == '2' :
    f = t[1] ; c = t[2]
    if f == '1' :
      s.appendleft(c)
    else :
      s.append(c)
      
print(*s,sep='')

Submission status: Time Limit Exceeded

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

query = []
rev=1

for i in range(q):
    query.append(input().split())

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

if rev==-1:
    s = s[::-1]
print(s)

Submission status: Time Limit Exceeded

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

for i in range(q) :
  t = list(map(str,input().split()))
  if t[0] == '1' :
    s = s[::-1]
  if t[0] == '2' :
    f = t[1] ; c = t[2]
    if f == '1' :
      s = c + s
    else :
      s = s + c
      
print(s)

Submission status: Time Limit Exceeded

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

for i in range(q) :
  t = list(map(str,input().split()))
  if t[0] == '1' :
    s = s[::-1]
  if t[0] == '2' :
    f = t[1] ; c = t[2]
    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 = [""]*q

rev=1

for i in range(q):
    query[i] = input()
    if query[i]=="1":
        rev*=-1
    else:
        r = query[i].split()
        if rev==1:
            if r[1]=="1":
                s = r[2]+s
            else:
                s += r[2]
        else:
            if r[1]=="2":
                s = r[2]+s
            else:
                s += r[2]

if rev==-1:
    s = s[::-1]
print(s)

Submission status: Time Limit Exceeded

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

query = [""]*q

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

print(s)

Submission status: Time Limit Exceeded

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

query = [""]*q
f = [""]*q
c = [""]*q

for i in range(q):
    query[i] = input()

for i in range(q):
    if query[i]=="1":
        s = s[::-1]
    else:
        r = query[i].split()
        if r[1]=="1":
            s = r[2]+s
        else:
            s += r[2]

print(s)

Submission status: Time Limit Exceeded

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

query = [""]*q
f = [""]*q
c = [""]*q

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

print(s)

Submission status: Time Limit Exceeded

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

query = [""]*q
f = [""]*q
c = [""]*q

for i in range(q):
    query[i] = input()
    if query[i]=="1":
        s = s[::-1]
    else:
        r = query[i].split()
        if r[1]=="1":
            s = r[2]+s
        elif r[1]=="2":
            s += r[2]

print(s)

Submission status: Time Limit Exceeded

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

query = [""]*q
f = [""]*q
c = [""]*q

for i in range(q):
    query[i] = input()
    if query[i]=="1":
        s = s[::-1]
    else:
        r = query[i].split()
        if r[1]=="1":
            s = r[2]+s
        elif r[1]=="2":
            s = s+r[2]

print(s)

Submission status: Time Limit Exceeded

Solution code:
s = list(input())
q = int(input())
sento = 0
for i in range(q):
    line = list(input().split(" "))
    if line[0] == "1":
        sento += 1
    elif line[1] == "1":
        if sento % 2 == 0:
            s.insert(0,line[2])
        else:
            s.append(line[2])
    else:
        if sento % 2 == 0:
            s.append(line[2])
        else:
            s.insert(0,line[2])
if sento % 2 ==1:
    s.reverse()
for i in range(len(s)):
    print(s[i],end ="")
print()

Submission status: Time Limit Exceeded

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

query = [""]*q
t = [0]*q
f = [0]*q
c = [""]*q

for i in range(q):
    query[i] = input()
    if query[i]=="1":
        t[i] = 1
    else:
        r = query[i].split()
        t[i] = 2
        f[i] = int(r[1])
        c[i] = r[2]

for i in range(q):
    if t[i]==1:
        s = s[::-1]
    elif t[i]==2:
        if f[i]==1:
            s = c[i]+s
        elif t[i]==2:
            s = s+c[i]
print(s)

Submission status: Time Limit Exceeded

Solution code:
s = list(input())
q = int(input())
for i in range(q):
    line = list(input().split(" "))
    if line[0] == "1":
        s.reverse()
    elif line[1] == "1":
        s.insert(0,line[2])
    else:
        s.append(line[2])
for i in range(len(s)):
    print(s[i],end ="")
print()

Submission status: Time Limit Exceeded

Solution code:

s = input()
q = int(input())
ht = 1
count = 0
for i in range(q):
    txt3 = input().split()
    if txt3[0] == '1':
        ht = 3 - ht
        count += 1
    else:
        if int(txt3[1]) == ht:
            s = txt3[2] + s
        else:
            s = s + txt3[2]
if count % 2 == 1:
    s = s[::-1]
print(s)

Submission status: Time Limit Exceeded

Solution code:
s = input()
count = int(input())
flg = False
for i in range(count):
    tfc = input().split()
    if tfc[0] == "1":
        flg = not flg
    elif tfc[0] == "2":
        if (not flg and tfc[1] == "1") or (flg and tfc[1] == "2"):
            s = tfc[2] + s
        elif (flg and tfc[1] == "1") or (not flg and tfc[1] == "2"):
            s = s + tfc[2]

if flg:
  s = s[::-1]
print(s)

Submission status: Time Limit Exceeded

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

for i in range(count):
    tfc = input().split()
    if tfc[0] == "1":
        s = s[::-1]
    elif tfc[0] == "2":
        if tfc[1] == "1":
            s = tfc[2] + s
        elif tfc[1] == "2":
            s = s + tfc[2]

print(s)

Submission status: Time Limit Exceeded

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


flip = 0
for i in range(len(q)):

    if q[i][0] == "1":
        flip ^= 1
    else:
        if flip == 0 and q[i][1] == "1":
            S = q[i][2] + S
        elif flip == 0 and q[i][1] == "2":
            S = S + q[i][2]
        elif flip == 1 and q[i][1] == "1":
            S = S + q[i][2]
        else:
            S = q[i][2] + S

if flip == 1:
    print(S[::-1])
else:
    print(S)

Submission status: Time Limit Exceeded

Solution code:
S = input()

Q = int(input())

for i in range(Q):
  *T, = map(str,input().split())
  if int(T[0]) == 1:
    S = S[::-1]
  elif int(T[1]) == 1:
    S = T[2]+S
  else:
    S = S+T[2]
    
print(S)

Submission status: Time Limit Exceeded

Solution code:
s=input()
n=int(input())
for i in range(n):
  a=list(map(str,input().split()))
  if a[0]=="1":
    s=s[::-1]
  else:
    if a[1]=="1":
      s=a[2]+s
    else:
      s+=a[2]
print(s)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
"""
    Author: _YL_
"""
s = input()
n = int(input())

i = 0
while i < n:
    lst = input().split()

    if lst[0] == "1":
        s = s[::-1]
    elif lst[1] == "1":
        s = lst[2] + s
    else:
        s += lst[2]

    i += 1

print(s)

Submission status: Time Limit Exceeded

Solution code:
"""
    Author: _YL_
"""
s = input()
n = int(input())

i = 0
while i < n:
    lst = input().split()

    if lst[0] == "1":
        temp = ""
        for letter in s:
            temp = letter + temp
        s = temp
    elif lst[1] == "1":
        s = lst[2] + s
    else:
        s += lst[2]

    i += 1

print(s)

Submission status: Time Limit Exceeded

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

guuki=0
ANS=S
for i in range(Q):
    A=list(input().split())
    if A[0]=="1":
        guuki+=1
    elif int(A[1])*guuki%2==1 or (A[1]=="2" and guuki%2==0):
        ANS=ANS+A[2]
    else:
        ANS=A[2]+ANS
if guuki%2==1:
    ANS=ANS[::-1]
print(ANS)

Submission status: Time Limit Exceeded

Solution code:
import copy
import math
import string

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

is_reverse = False
for i in range(Q):
    A = list(map(str,input().split()))
    A[0] = int(A[0])
    if A[0] == 1:
        is_reverse = (not is_reverse)
    elif A[0] == 2:
        A[1] = int(A[1])
        if A[1] == 1:
            if is_reverse:
                S += A[2]
            else:
                S = A[2] + S
        else:
            if is_reverse:
                S = A[2] + S
            else:
                S += A[2]
if is_reverse:
    S = S[::-1]
print(S)

Submission status: Time Limit Exceeded

Solution code:
import copy
import math
import string

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

is_reverse = False
front = ""
back = ""
for i in range(Q):
    A = list(map(str,input().split()))
    A[0] = int(A[0])
    if A[0] == 1:
        is_reverse = (not is_reverse)
    elif A[0] == 2:
        A[1] = int(A[1])
        if A[1] == 1:
            if is_reverse:
                back += A[2]
            else:
                front += A[2]
        else:
            if is_reverse:
                front += A[2]
            else:
                back += A[2]
front = front[::-1]
S = front + S + back
if is_reverse:
    S = S[::-1]
print(S)

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
before_chr = []
after_chr = []
for i in range(Q):
    query = list(input().split())
    if query[0] == "1":
        S = S[::-1]
    elif query[1] == "1":
        S = query[2] + S
    else:
        S += query[2]
print(S)