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())
k = 1
for _ in range(Q):
    query = input()
    if query.startswith('1'):
        k *= -1
    elif query.startswith('2'):
        t, f, c = query.split()
        if f == '1':
            if k > 0:
                S = c + S
            else:
                S = S + c
        elif f == '2':
            if k > 0:
                S = S + c
            else:
                S = c + S
if k > 0:
    print(S)
else:
    print(S[::-1])

Submission status: Time Limit Exceeded

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)

s = input()
q = int(input())
flag = True
for i in range(q):
    qfc = input()
    if qfc[0] == '1':
        if flag:
            flag = False
        else:
            flag = True
    else:
        if qfc[2] == '1':
            if flag:
                s = qfc[4] + s
            else:
                s += qfc[4]
        else:
            if flag:
                s += qfc[4]
            else:
                s = qfc[4] + s
if flag:
    print(s)
else:
    print(s[::-1])

Submission status: Time Limit Exceeded

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)

s = input()
q = int(input())
for i in range(q):
    qfc = input()
    if qfc[0] == '1':
        s = s[::-1]
    else:
        if qfc[2] == '1':
            s = qfc[4] + s
        else:
            s += qfc[4]
print(s)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/python3

s = input()
head = 1 #1:forward, -1: reverse

q = int(input())
sl = [ s[i] for i in range(len(s)) ]

for i in range(q):
    ql = input()
    if ql[0] == '1':
        head *= -1
    elif ql[0] == '2':
        (op, f, c) = ql.split()
        ht = 1
        if f == '1':
            ht = head
        else:
            #f == '2':
            ht = head * -1

        if ht == 1:
            sl.insert(0, c)
        else:
            sl.append(c)


if head == 1:
    print(''.join(sl))
else:
    print(''.join(reversed(sl)))

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/python3

s = input()
head = 1 #1:forward, -1: reverse

q = int(input())

for i in range(q):
    ql = input()
    if ql[0] == '1':
        head *= -1
    elif ql[0] == '2':
        (op, f, c) = ql.split()
        ht = 1
        if f == '1':
            ht = head
        else:
            #f == '2':
            ht = head * -1

        if ht == 1:
            s = c + s
        else:
            s = s + c


if head == 1:
    print(s)
else:
    print(s[::-1])

Submission status: Time Limit Exceeded

Solution code:
def main():
    s = input()
    q = int(input())
    reverse = False
    for _ in range(q):
        query = input().split()
        if query[0] == "1":
            reverse = not(reverse)
        else:
            if query[1] == "1":
                if reverse:
                    s = "".join([s, query[2]])
                else:
                    s = "".join([query[2], s])
            else:
                if reverse:
                    s = "".join([query[2], s])
                else:
                    s = "".join([s, query[2]])
    if reverse:
        print(s[::-1])
    else:
        print(s)
if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
reverse = False
for _ in range(q):
    query = input().split()
    if query[0] == "1":
        reverse = not(reverse)
    else:
        if query[1] == "1":
            if reverse:
                s = s + query[2]
            else:
                s = query[2] + s
        else:
            if reverse:
                s = query[2] + s
            else:
                s = s + query[2]
if reverse:
    print(s[::-1])
else:
    print(s)

Submission status: Time Limit Exceeded

Solution code:
S = str(input())
S = [s for s in S]
Q = int(input())

flip = False

for q in range(0, Q):
    qi = list(input().split())

    if len(qi) == 1:
        flip = not flip
    else: # len(qi) == 3
        Fi, Ci = int(qi[1]), qi[2]
        
        if Fi == 1: # front 
            if not flip:
                S.insert(0, Ci)
            else: # flip == True
                S.append(Ci)
        else:
            if not flip:
                S.append(Ci)
            else:
                S.insert(0, Ci)

S = "".join(S)

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

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8

# 標準入力 <int>
S = str(input())
Q = int(input())

# 標準入力 <str> list[][] 複数行・複数列
Qv = []
for _ in range(Q):
    Qv.append(list(map(str, input().split())))

for _ in range(Q):
    if Qv[_][0] == "1":
        S = S[::-1]
    elif Qv[_][0] == "2":
        if Qv[_][1] == "1":
            S = Qv[_][2] + S
        elif Qv[_][1] == "2":
            S = S + Qv[_][2]

print(S)

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

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

for q in range(0, Q):
    qi = list(input().split())

    if len(qi) == 1:
        S = S[::-1]
    else: # len(qi) == 3
        Fi, Ci = int(qi[1]), qi[2]

        if Fi == 1: # front
            S = Ci + S
        else: # Fi == 2, end
            S = S + Ci

print(S)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
s=input()
q=int(input())
for i in range(q):
  k=input().split()
  if k[0]=='2' and k[1]=='1':
    s=k[2]+s
  if k[0]=='2' and k[1]=='2':
    s=s+k[2]
  if k[0]=='1':
    s=s[::-1]
print(s)

Submission status: Time Limit Exceeded

Solution code:
s=input()
q=int(input())
x=['0','0']
for i in range(q):
  k=input().split()
  if k[0]=='2' and k[1]=='1':
    x[0]=k[2]
    x[1]=s
    s=''.join(x)
  if k[0]=='2' and k[1]=='2':
    x[0]=s
    x[1]=k[2]
    s=''.join(x)
  if k[0]=='1':
    s=s[::-1]
print(s)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
from collections import deque
def main():
    s = deque(input())
    q = int(input())
    top = True
    for i in range(q):
        query = list(map(str, input().split()))
        if query[0] == "1":
            top = not top
        else:
            if (query[1]=="1" and top==True) or (query[1]=="2" and top==False):
                s.appendleft(query[2])
            else:
                s.append(query[2])
    if top == False:
        for i in range(len(s)):
            print(s[len(s)-1-i],end="")
    else:
        for c in s:
            print(c,end="")
            
if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

tail = len(S)-1

for _ in range(q):
    # print(S)
    Q = list(input().split())
    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())

cnt = 0

#空の文字列にqueryで追加する文字を加えていく
ss = " "
for _ in range(Q):
    query = list(input().split())
    if query[0] == "1":
        cnt += 1
    else:
        if (query[1] == "1" and cnt%2==0) or (query[1] == "2" and cnt%2==1):
            ss = "".join([query[2],ss])
        else:
            ss = "".join([ss,query[2]])

#反転回数が奇数の場合、元の文字列とqueryにより作成した文字列を反転
if cnt%2 == 1:
    S = S[::-1]
    ss = ss[::-1]

#スペースに元の文字列を追加
print(ss.replace(" ", S))

Submission status: Time Limit Exceeded

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

cnt = 0
ss = " "
for _ in range(Q):
    query = list(input().split())
    if query[0] == "1":
        cnt += 1
    else:
        if (query[1] == "1" and cnt%2==0) or (query[1] == "2" and cnt%2==1):
            ss = query[2]+ss
        else:
            ss = ss+query[2]
if cnt%2 == 1:
    S = S[::-1]
    ss = ss[::-1]
print(ss.replace(" ", S))

Submission status: Time Limit Exceeded

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

cnt = 0
for _ in range(Q):
    query = list(input().split())
    if query[0] == "1":
        cnt += 1
    else:
        if (query[1] == "1" and cnt%2==0) or (query[1] == "2" and cnt%2==1):
            S = query[2]+S
        else:
            S = S+query[2]
if cnt%2 == 1:
    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 query[0] == "1":
        S = S[::-1]
    else:
        if query[1] == "1":
            S = query[2]+S
        else:
            S = S+query[2]
print(S)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

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

q = deque(S)

for i in range(Q):
    T = input().split()
    if len(T) == 1:
        q.reverse()
    else:
        F = int(T[1])
        C = T[2]
        if F == 1:
            q.appendleft(C)
        else:
            q.append(C)

print(''.join(q))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

head = ''
tail = ''
flip = 0

for _ in range(q):
	query = input().split()
	if query[0] == '1':
		flip ^= 1
	else:
		f = query[1]
		c = query[2]
		if (f == '1' and flip == 0) or (f == '2' and flip == 1):
			head += c
		else:
			tail += c

if flip:
	head, tail = tail, head
	s = s[::-1]

print(head[::-1] + s + tail)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

s = deque(input())

q = int(input())

for _ in range(q):
    A = list(input().split())
    if A[0] == '1':
        s.reverse()
    else:
        if A[1] == '1':
            s.appendleft(A[2])
        else:
            s.append(A[2])
print("".join(list(s)))

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

s = deque(input())

q = int(input())

for _ in range(q):
    A = list(input().split())
    if A[0] == '1':
        s.reverse()
    else:
        if A[1] == '1':
            s.appendleft(A[2])
        else:
            s.append(A[2])
print("".join(s))

Submission status: Time Limit Exceeded

Solution code:

from collections import deque
s = deque(input())
q = int(input())
r = 0
b = ""
c = ""
for i in range(q):
    A = list(input().split())
    if A[0] == '1':
        s.reverse()
    else:
        if (A[1] == '1'):
            s.appendleft(A[2])
        else:
            s.append(A[2])
print("".join(s))

Submission status: Time Limit Exceeded

Solution code:

from collections import deque
s = deque(input())
q = int(input())
r = 0
b = ""
c = ""
for i in range(q):
    A = list(input().split())
    if A[0] == '1':
        s.reverse()
    else:
        if (A[1] == '1'):
            s.appendleft(A[2])
        else:
            s.append(A[2])
print("".join(s))

Submission status: Time Limit Exceeded

Solution code:
s = input()

q = int(input())
r = 0
b = ""
c = ""
for i in range(q):
    A = list(input().split())
    if A[0] == '1':
        r += 1
    else:
        if (A[1] == '1' and r % 2 == 0) or (A[1] != '1' and r % 2 != 0):
            b = A[2] + b
        else:
            c = c + A[2]
# s = b + s + c
if r % 2 == 0:
    print(b+s+c)
else:
    # print(s[::-1])
    print(c[::-1] + s[::-1] + b[::-1])

Submission status: Time Limit Exceeded

Solution code:
s = input()

q = int(input())
r = 0
b = ""
c = ""
for i in range(q):
    A = list(input().split())
    if A[0] == '1':
        r += 1
    else:
        if (A[1] == '1' and r % 2 == 0) or (A[1] != '1' and r % 2 != 0):
            b = A[2] + b
        else:
            c = c + A[2]
s = b + s + c
if r % 2 == 0:
    print(s)
else:
    print(s[::-1])

Submission status: Time Limit Exceeded

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

for _ in range(q):
	query = list(map(str, input().split()))
	if query[0] == '1':
		s = s[::-1]
	elif query[0] == '2':
		if query[1] == '1':
			s = query[2] + s
		elif query[1] == '2':
			s = s + query[2]

print(s)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
s = input()
q = int(input())
array = [list(map(str, input().split())) for i in range(q)]
d = deque(s)
for i in range(q):
    if array[i][0] == "1":
        d.reverse()
    elif array[i][1] == "1":
        d.appendleft(array[i][2])
    elif array[i][1] == "2":
        d.append(array[i][2])
d_str = "".join(list(d))
print(d_str)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import math
from collections import deque
def ma(): return map(str,input().split())

#input
s=input()
q=int(input())
#print(deque(s))
#s_now.append(s)
s_state=1
for _ in range(q):
    t=list(ma())
    t_len=len(t)
    if t_len==1:
        s_state*=-1
    else:
        t_edit=3-2*int(t[1])
        if t_edit==s_state:
            s=t[2]+s
            #s_now.appendleft(t[2])
        else:
            s=s+t[2]
            #s_now.append(t[2])

if s_state == 1:
    print("".join(s))
else:
    s_last=list(s)
    print("".join(s_last[::-1]))

Submission status: Time Limit Exceeded

Solution code:
import math
from collections import deque
def ma(): return map(str,input().split())

#input
s=input()
q=int(input())
#print(deque(s))
#s_now.append(s)
s_state=1
for _ in range(q):
    t=list(ma())
    t_len=len(t)
    if t_len==1:
        s_state*=-1
    else:
        t_edit=3-2*int(t[1])
        if t_edit==s_state:
            s=t[2]+s
            #s_now.appendleft(t[2])
        else:
            s=s+t[2]
            #s_now.append(t[2])

if s_state == 1:
    print("".join(s))
else:
    s_last=list(s)
    print("".join(s_last[::-1]))

Submission status: Time Limit Exceeded

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

for i in range(q):
    t = input()
    if len(t) == 1:        
        if rev:
            rev = False
        else:
            rev = True
    else:
        q2, f, c = t.split()
        if f == "1":
            if rev:
                s = s + c
            else:
                s = c + s
        if f == "2":
            if rev:
                s = c + s
            else:
                s = s + c

if rev:
    s = s[::-1]
else:
    pass

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:
        q2, f, c = t.split()
        if f == "1":
            s = c + s
        if f == "2":
            s = s + c

print(s)

Submission status: Time Limit Exceeded

Solution code:
s = input()

for Q in range(int(input())):
	A = list(input().split())
	T = int(A[0])
	F = 0
	C = ""
	if len(A) == 3:
		F = int(A[1])
		C = A[2]
	if T == 1:
		s = s[::-1]
	else:
		if F == 1:
			s = C + s
		else:
			s = s + C
print(s)

Submission status: Time Limit Exceeded

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

for i in range(q):
    t = input()
    if len(t) == 1:
        s.reverse()
    else:
        if t[2] == "1":
            s.appendleft(t[4])
        else:
            s.append(t[4])

print(*s, sep="")

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 = s + t[4]

print(s)

Submission status: Time Limit Exceeded

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

cnt = 0

for i in range(q):
    a = input()
    if a[0] == "1":
        cnt += 1
    elif a[0] == "2":
        if (a[2] == "1" and cnt % 2 == 0) or (a[2] == "2" and cnt % 2 == 1):
            s = a[4] + s
        elif (a[2] == "2" and cnt % 2 == 0) or (a[2] == "1" and cnt % 2 == 1):
            s = s + a[4]

if cnt % 2 == 1:
    s = s[::-1]

print(s)

Submission status: Time Limit Exceeded

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

for i in range(q):
    a = input()
    if a[0] == "1":
        s = s[::-1]
    elif a[0] == "2":
        if a[2] == "1":
            s = a[4] + s
        elif a[2] == "2":
            s = s + a[4]

print(s)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
S = input()
Q = int(input())
not_reversed = True
for _ in range(Q):
    query = list(input().split())
    if query[0] == "1":
        not_reversed = not not_reversed 
        continue
    
    else:
        if not_reversed == 1 and query[1] == "1":
            S = query[2] + S

        elif not not_reversed == -1 and query[1] == "1":
            S = S + query[2]

        if not_reversed == 1 and query[1] == "2":
            S = S + query[2]
            
        elif not not_reversed == -1 and query[1] == "2":
            S = query[2] + S

if not_reversed == False:
    S = list(S)
    S.reverse()
    print("".join(map(str, S)))
else:
    print(S)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
S = input()
Q = int(input())
add_left = ""
add_right = ""
not_reversed = True
for _ in range(Q):
    query = list(input().split())
    if query[0] == "1":
        not_reversed = not not_reversed 
        continue
    
    else:
        if not_reversed == 1 and query[1] == "1":
            add_left = query[2] + add_left

        elif not not_reversed == -1 and query[1] == "1":
            add_right = add_right + query[2]

        if not_reversed == 1 and query[1] == "2":
            add_right = add_right + query[2]
            
        elif not not_reversed == -1 and query[1] == "2":
            add_left = query[2] + add_left

if not_reversed == False:
    ans = list(add_left) + list(S) + list(add_right)
    ans.reverse()
    print("".join(map(str, ans)))
else:
    ans = add_left + S + add_right
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
S = input()
d = deque(S)
Q = int(input())
for i in range(Q):
    q = input().split()
    if q[0] == "1":
        d.reverse()
    else:
        if q[1]=="1":
            d.appendleft(q[-1])
        else:
            d.append(q[-1])
print(''.join(d))

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
S = input()
d = deque(S)
Q = int(input())
for i in range(Q):
    q = input().split()
    if q[0] == "1":
        d.reverse()
    else:
        if q[1]=="1":
            d.appendleft(q[2])
        else:
            d.append(q[2])
print(''.join(d))

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
S = input()
d = deque(S)
Q = int(input())
for i in range(Q):
    q = input().split()
    if len(q)==3:
        c = q[2]
    if q[0] == "1":
        d.reverse()
    else:
        if q[1]=="1":
            d.appendleft(c)
        else:
            d.append(c)
print(''.join(d))

Submission status: Time Limit Exceeded

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

for j in q_list:
    if len(j)==1:
        s = s[::-1]
    else:
        if j[1] == '1':
            s = j[2] + s
        else:
            s = s + j[2]

print(s)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
S = input()
Q = int(input())

for _ in range(Q):
    query = deque(input().split())
    if query[0] == "1":
        S = list(S)
        S.reverse()
        S = "".join(map(str, S))
    elif query[1] == "1":
        S = query[2] + S
    elif query[1] == "2":
        S = S + query[2]

print(S)

Submission status: Time Limit Exceeded

Solution code:
def main():
    from collections import deque
    S = deque(list(input()))
    Q = int(input())

    for _ in range(Q):
        query = deque(input().split())
        if query[0] == "1":
            S.reverse()
        elif query[1] == "1":
            S.appendleft(query[2])
        elif query[1] == "2" :
            S.append(query[2])

    print("".join(map(str, S)))

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
S = deque(list(input()))
Q = int(input())

for q in range(Q):
    query = list(input().split())
    if query[0] == "1":
        S.reverse()
    elif query[1] == "1":
        S.appendleft(query[2])
    elif query[1] == "2" :
        S.append(query[2])

print("".join(map(str, S)))

Submission status: Time Limit Exceeded

Solution code:
s=input()
q=int(input())
m=1
for i in range (q):
    a=list(input().split())
    if a[0]=="1":
        m=m*(-1)
    else:
        if m==1:
            if a[1]=="1":
                s=a[2]+s
            else:
                s=s+a[2]
        else:
            if a[1]=="2":
                s=a[2]+s
            else:
                s=s+a[2]


print(s[::m])

Submission status: Time Limit Exceeded

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

print(s)

Submission status: Time Limit Exceeded

Solution code:
import sys
def input(): return sys.stdin.readline().rstrip()

def main():
  S = input()
  N = int(input())

  ans = ""
  for i in range(N):
    list = input().split()
    if list[0] == '1':
      S = S[::-1]
      continue
    if list[1] == '1':
      S = list[2] + S
      continue
    S += list[2]
  print(S)

if __name__ == '__main__':
  main()

Submission status: Time Limit Exceeded

Solution code:
"""
int(input())
map(int,input().split())
list(map(int,input().split()))

"""
s=input()
q=int(input())
rev=0
for i in range(q):
    x=input()
    if(x=="1"):
        rev+=1
    else:
        t,f,c=map(str,x.split())
        if((f=="1" and rev%2==0) or (f=="2" and rev%2==1)):
            s=c+s
        else:
            s+=c
if(rev%2==0):
    print(s)
else:
    print(s[::-1])

Submission status: Time Limit Exceeded

Solution code:
"""
int(input())
map(int,input().split())
list(map(int,input().split()))

"""
s=input()
q=int(input())
for i in range(q):
    x=input()
    if(x=="1"):
        s=s[::-1]
    else:
        t,f,c=map(str,x.split())
        if(f=="1"):
            s=c+s
        else:
            s+=c
print(s)

Submission status: Time Limit Exceeded

Solution code:
s=input()
q=int(input())
for i in range(q):
	inp1=list(map(str,input().strip().split()))
	if(len(inp1)==1):
		s=s[::-1]
	else:
		if(inp1[1]=='1'):
			s=inp1[2]+s
		else:
			s+=inp1[2]
print(s)

Submission status: Time Limit Exceeded

Solution code:
S = input()
char_S = list(S)
Q = int(input())
for i in range(Q):
    query = input().split()
    if query[0] == "1":
        char_S.reverse()
    else:
        if query[1] == "1":
            char_S.insert(0,query[2])
        else:
            char_S.insert(len(char_S),query[2])
S = ''.join(char_S)
print(S)

Submission status: Time Limit Exceeded

Solution code:
import sys

def main():
    ans = [input()]
    n = int(input())
    vec = 1
    for i in range(n):
        q = sys.stdin.readline().rstrip()
        if q == "1":
            vec *= -1
        else:
            if ((q[2] == "1") and (vec == 1)) or ((q[2] == "2") and (vec == -1)):
                ans.insert(0, q[4])
            else:
                ans.append(q[4])
    ans = ''.join(ans)
    if vec == -1:
        ans = ans[::-1]
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys

def main():
    ans = input()
    n = int(input())
    vec = 1
    for i in range(n):
        q = sys.stdin.readline().rstrip()
        if q == "1":
            vec *= -1
        else:
            if ((q[2] == "1") and (vec == 1)) or ((q[2] == "2") and (vec == -1)):
                ans = q[4] + ans
            else:
                ans += q[4]
    if vec == -1:
        ans = ans[::-1]
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
ans = input()
n = int(input())
vec = 1
for i in range(n):
    query = input()
    if query == "1":
        vec *= -1
    else:
        _,option,str = query.split()
        if ((option == "1") and (vec == 1)) or ((option == "2") and (vec == -1)):
            ans = str + ans
        else:
            ans += str
if vec == -1:
    ans = ans[::-1]
print(ans)

Submission status: Time Limit Exceeded

Solution code:
ans = input()
n = int(input())
for i in range(n):
    query = input()
    if query == "1":
        ans = ''.join(reversed(list(ans)))
    else:
        _,option,str = query.split()
        if option == "1":
            ans = str + ans
        else:
            ans += str
print(ans)

Submission status: Time Limit Exceeded

Solution code:
ans = input()
n = int(input())
for i in range(n):
    query = input()
    if query == "1":
        ans = ans[::-1]
    else:
        _,option,str = query.split()
        if option == "1":
            ans = str + ans
        else:
            ans += str
print(ans)

Submission status: Time Limit Exceeded

Solution code:
s = input()
q_ = int(input())
query = [input().split() for _ in range(q_)]

for q in query:
    if q[0] == '1':
        s = s[::-1]
    elif q[0] == '2':
        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())
reverse_count = 0
for i in range(Q):
  query = list(input().split())
  if(query[0] == '1'):
    reverse_count += 1
  else:
    if((int(query[1]) + reverse_count) % 2 == 1):
      S = query[2] + S
    else:
      S = S + query[2]
if(reverse_count % 2 == 1):
  S = S[::-1]
print(S)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

    reverse = False;

    for i in range(Q):
        query = input()
        if query[0] == '1':
            reverse = not reverse
        else:
            if query[2] == '1':
                if reverse == False:
                    S = query[4] + S
                else:
                    S = S + query[4]
            else:
                if reverse == False:
                    S = S + query[4]
                else:
                    S = query[4] + S

    if reverse == False:
        print(S)
    else:
        print(''.join(list(reversed(S))))

main()

Submission status: Time Limit Exceeded

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

for i in range(Q):
    aa = [S]
    query = input().split()

    if query[0] == '1':
        S = S[::-1]
    elif query[0] == '2':
        aa.append(query[2])
        if query[1] == '1':
            aa = reversed(aa)
            S = ''.join(aa)
        elif query[1] == '2':
            S = ''.join(aa)

print(S)

Submission status: Time Limit Exceeded

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

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

    if query[0] == '1':
        S = S[::-1]
    elif query[0] == '2':
        if query[1] == '1':
            S = query[2] + S
        elif query[1] == '2':
            S += query[2]

print(S)

Submission status: Time Limit Exceeded

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

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

def addFirst(base, s):
    return s + base

def addLast(base, s):
    return base + s

def reverse(base):
    return ''.join(list(reversed(base)))

for ls in qs:
    if ls[0] == "1":
        S = reverse(S)
    else:
        if ls[1] == "1":
            S = addFirst(S, ls[2])
        else:
            S = addLast(S, ls[2])

print(S)

Submission status: Time Limit Exceeded

Solution code:
class S_Takahashi:
  def __init__(self):
    self.string = None
  def reverse(self):
    self.string=self.string[::-1]
  def insert(self,C):
    self.string = C+self.string
  def append(self,C):
    self.string = self.string+C

def run():
  S=S_Takahashi()
  S.string =input()
  Q =int(input())
  for i in range(Q):
    input_list = input().split()
    #print(input_list)
    T = int(input_list[0])
    if T ==1:
      S.reverse()
    elif T==2:
      F=int(input_list[1])
      C=input_list[2]
      if F==1:
        S.insert(C)
      elif F==2:
        S.append(C)
  return S.string
print(run())