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

rev = False

for _ in range(int(input())):
    ty = input().split()

    if ty[0] == '1':
        rev = not rev
    else:
        f, c = ty[1:]

        if rev:
            if f == '1':
                f = '2'

            else:
                f = '1'

        if f == '1':
            s = c + s
        else:
            s += c

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

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

def main():

    S = np.array(list(input()))

    r = False
    for i in range(int(input())):
        Query = input().split()
        if Query[0] == "1":
            r = not r
        else:
            if (r==False and Query[1] == "1") or (r==True and Query[1] == "2"):
                S = np.append(Query[2],S)
            else:
                S = np.append(S,Query[2])

    if r == True:
        S = np.flipud(S)

    S = "".join(S)

    print(S)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
def main():

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

    Query = []
    for i in range(Q):
        Query.append(list(input().split()))
    for i in range(Q):
        if Query[i][0] == "1":
            S = S[::-1]
        else:
            if Query[i][1] == "1":
                S = "".join([Query[i][2],S])
            else:
                S = "".join([S,Query[i][2]])

    print(S)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:

class Solution:

    def solve(self, S: str, Q: int, Query) -> int:

        S_list = list(S)

        num_reverse = 0
        for q in Query:
            if int(q[0]) == 1:
                num_reverse += 1
            else:
                if (num_reverse + int(q[1])) % 2 == 0:
                    S_list.append(q[2])
                else:
                    S_list.insert(0, q[2])

        if num_reverse % 2 == 1:
            S_list.reverse()

        return ''.join(S_list)


if __name__ == '__main__':

    # standard input
    S = input()
    Q = int(input())
    Query = []
    for _ in range(Q):
        Query.append(tuple(input().split()))

    # solve
    solution = Solution()
    print(solution.solve(S, Q, Query))

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

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

d = deque(['S'])

#d = ['S']

rev = False

for i in range(q):
    if query[i][0] == '2':
        if query[i][1] == '1':
            d.appendleft(query[i][2])
        else:
            d.append(query[i][2])
    else:
        d.reverse()
        rev = not rev

#ans = deque()

if rev % 2 == 1:
    #s = ''.join(list(reversed(s)))
    s = s[::-1]

while len(d):
    tmp = d.popleft()
    if tmp == 'S':
        #ans.append(s)
        print(s,end='')
    else:
        print(tmp,end='')
        #ans.append(tmp)

#print(*ans,sep='')

Submission status: Time Limit Exceeded

Solution code:
# standard input
S = input()
Q = int(input())
Query = []
num_reverse = 0

S_r = ''.join(reversed(S))

rev = False

for _ in range(Q):

    q = tuple(map(str, input().split()))

    if q[0] == '1':
        rev = (not rev)

    else:
        if (rev and q[1] == '1') or (not rev and q[1] == '2'):
            S = S + q[2]
            S_r = q[2] + S_r
        else:
            S = q[2] + S
            S_r = S_r + q[2]

if rev:
    print(S_r)
else:
    print(S)

Submission status: Time Limit Exceeded

Solution code:
# standard input
S = input()
Q = int(input())
Query = []
num_reverse = 0

S_r = ''.join(reversed(S))

for _ in range(Q):

    q = tuple(map(str, input().split()))

    if int(q[0]) == 1:
        num_reverse += 1

    else:
        if (num_reverse + int(q[1])) % 2 == 0:
            S = S + q[2]
            S_r = q[2] + S_r
        else:
            S = q[2] + S
            S_r = S_r + q[2]

if num_reverse % 2 == 1:
    print(S_r)
else:
    print(S)

Submission status: Time Limit Exceeded

Solution code:
# standard input
S = input()
Q = int(input())
Query = []
num_reverse = 0

for _ in range(Q):

    q = tuple(map(str, input().split()))

    S_r = ''.join(reversed(S))

    if int(q[0]) == 1:
        num_reverse += 1

    else:
        if (num_reverse + int(q[1])) % 2 == 0:
            S = S + q[2]
            S_r = q[2] + S_r
        else:
            S = q[2] + S
            S_r = S + q[2]

if num_reverse % 2 == 1:
    print(S_r)
else:
    print(S)

Submission status: Time Limit Exceeded

Solution code:
# standard input
S = input()
Q = int(input())
Query = []
num_reverse = 0

for _ in range(Q):

    q = tuple(map(str, input().split()))

    if int(q[0]) == 1:
        num_reverse += 1

    else:
        if (num_reverse + int(q[1])) % 2 == 0:
            S = S + q[2]
        else:
            S = q[2] + S

if num_reverse % 2 == 1:
    # S = S[::-1]
    S = ''.join(reversed(S))

print(S)

Submission status: Time Limit Exceeded

Solution code:
# standard input
S = input()
Q = int(input())
Query = []
num_reverse = 0

for _ in range(Q):

    q = tuple(map(str, input().split()))

    if int(q[0]) == 1:
        num_reverse += 1

    else:
        if (num_reverse + int(q[1])) % 2 == 0:
            S = S + q[2]
        else:
            S = q[2] + S

if num_reverse % 2 == 1:
    S = S[::-1]

print(S)

Submission status: Time Limit Exceeded

Solution code:

class Solution:

    def solve(self, S: str, Q: int, Query) -> int:

        num_reverse = 0
        for q in Query:
            if int(q[0]) == 1:
                num_reverse += 1
            else:
                if (num_reverse + int(q[1])) % 2 == 0:
                    S = S + q[2]
                else:
                    S = q[2] + S

        if num_reverse % 2 == 1:
            S = S[::-1]

        return S


if __name__ == '__main__':

    # standard input
    S = input()
    Q = int(input())
    Query = []
    for _ in range(Q):
        Query.append(tuple(input().split()))

    # solve
    solution = Solution()
    print(solution.solve(S, Q, Query))

Submission status: Time Limit Exceeded

Solution code:
S=input()
Q=int(input())
r_count=0
top=[1,2]
tail=[2,1]

for i in range(Q):
    q=list(input().split())
    if q[0]=="1":
        r_count+=1
    else:
        q[1]=int(q[1])
        if q[1]==1:
            q[1]=top[r_count%2]
        else:
            q[1]=tail[r_count%2]
        
        if q[1]==1:
            S=q[2]+S
        else:
            S=S+q[2]
if r_count%2==1:
    print(S[::-1])
else:
    print(S)

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:
S=input()
Q=int(input())
for i in range(Q):
    query=input()
    if(query=="1"):
        S=S[::-1]
    else:
        query, F, C =query.split()
        S=C+S if(F=="1") else S+C
print(S)

Submission status: Time Limit Exceeded

Solution code:
import sys

s = sys.stdin.readline().replace('\n','')
q = sys.stdin.readline().replace('\n','')
q = int(q)

flag = 0
for i in range(0,q):
	str = sys.stdin.readline().replace('\n','')
        if( str[0] == '1'):
          flag += 1
        if( str[0] == '2'):
          if( str[2] == '1'):
	    if( flag % 2 == 0):
	      s = str[4] + s
	    else:
	      s = s+ str[4]
          if( str[2] == '2'):
	    if( flag % 2 == 0):
	      s = s+ str[4]
	    else:
	      s = str[4] + s

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

print s
sys.stdout.flush()

Submission status: Time Limit Exceeded

Solution code:
import sys

s = sys.stdin.readline().replace('\n','')
q = sys.stdin.readline().replace('\n','')
q = int(q)

prev_cmd = 0
for i in range(0,q):
	str = sys.stdin.readline().replace('\n','')
        if( str[0] == '1'):
          prev_cmd += 1
        if( str[0] == '2'):
	  if( prev_cmd %2 == 1):
	    s = s[::-1]
	  prev_cmd = 0
          if( str[2] == '1'):
	    s = str[4] + s
          if( str[2] == '2'):
	    s = s + str[4]

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

print s
sys.stdout.flush()

Submission status: Time Limit Exceeded

Solution code:
import sys

s = sys.stdin.readline().replace('\n','')
q = sys.stdin.readline().replace('\n','')
q = int(q)

prev_cmd = 0
for i in range(0,q):
	str = sys.stdin.readline().replace('\n','')
        if( str[0] == '1'):
	  if( prev_cmd == 1):
            prev_cmd = 0
          else:
            prev_cmd = 1
        if( str[0] == '2'):
	  if( prev_cmd == 1):
	    s = s[::-1]
	  prev_cmd = 0
          if( str[2] == '1'):
	    s = str[4] + s
          if( str[2] == '2'):
	    s = s + str[4]

if( prev_cmd == 1):
  s = s[::-1]

print s
sys.stdout.flush()

Submission status: Time Limit Exceeded

Solution code:
import sys

s = sys.stdin.readline().replace('\n','')
q = sys.stdin.readline().replace('\n','')
q = int(q)

for i in range(0,q):
	str = sys.stdin.readline().replace('\n','')
        if( str[0] == '1'):
		s = s[::-1]
        if( str[0] == '2'):
          if( str[2] == '1'):
	    s = str[4] + s
          if( str[2] == '2'):
	    s = s + str[4]

print s
sys.stdout.flush()

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/python
import sys

s = sys.stdin.readline().replace('\n','')
q = sys.stdin.readline().replace('\n','')
q = int(q)

for i in range(0,q):
	str = sys.stdin.readline().replace('\n','')
        if( str[0] == '1'):
		s = s[::-1]
        if( str[0] == '2'):
          if( str[2] == '1'):
	    s = str[4] + s
          if( str[2] == '2'):
	    s = s + str[4]

print s
sys.stdout.flush()

Submission status: Time Limit Exceeded

Solution code:
#D
S = input()
S = list(S)
Q = int(input())
for i in range(1, Q + 1):
    opt = input().split(' ')
    if int(opt[0]) == 1:
        S = list(reversed(S))
    else:
        if int(opt[1]) == 1:
            S.insert(0, opt[2])
        else:
            S.insert(len(S), opt[2])
x = S[0]
for k in range (1,len(S)):
    x = x + S[k]

print(x)

Submission status: Time Limit Exceeded

Solution code:
S = str(input())
n = int(input())
Q = [list(map(str, input().split())) for _ in range(n)]
cnt = 0
for q in Q:
    if q[0] == "1":
        cnt += 1
    elif q[0] == "2":
        if (int(q[1])+cnt)%2 == 1:
            S = q[2] + S
        else:
            S = S + q[2]

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

Submission status: Time Limit Exceeded

Solution code:
import sys
def main():
	S=input()
	Q=int(sys.stdin.readline())
	flag=0
	for i in range(Q):
		qu=input()
		if qu[0]=='1':
			if flag==0:
				flag+=1
			else:
				flag=0
		else:
			if qu[2]=='1' and flag==0:
				S=qu[4]+S
			elif qu[2]=='1' and flag==1:
				S+=qu[4]
			elif qu[2]=='2' and flag==0:
				S+=qu[4]
			else:
				S=qu[4]+S
	if flag==1:
		S=S[::-1]
	print(S)
if __name__=='__main__':
	main()

Submission status: Time Limit Exceeded

Solution code:
S = list(input())
n = int(input())
Q = [list(map(str, input().split())) for _ in range(n)]
cnt = 0
for q in Q:
    if q[0] == "1":
        cnt += 1
#         S.reverse()
    elif q[0] == "2":
        if (int(q[1])+cnt)%2 == 1:
            S.insert(0,q[2])
        else:
            S.append(q[2])

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

Submission status: Time Limit Exceeded

Solution code:
import sys
S=input()
Q=int(sys.stdin.readline())
flag=0
for i in range(Q):
	qu=input()
	if qu[0]=='1':
		if flag==0:
			flag+=1
		else:
			flag=0
	else:
		if qu[2]=='1' and flag==0:
			S=qu[4]+S
		elif qu[2]=='1' and flag==1:
			S+=qu[4]
		elif qu[2]=='2' and flag==0:
			S+=qu[4]
		else:
			S=qu[4]+S
if flag==1:
	S=S[::-1]
print(S)

Submission status: Time Limit Exceeded

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

for q in Q:
    if q[0] == "1":
        S.reverse()
    elif q[0] == "2":
        if q[1] == "1":
            S.insert(0,q[2])
        else:
            S.append(q[2])
print("".join(S))

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
for operation in range(q):
    t = 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:
import sys
S=input()
Q=int(sys.stdin.readline())
for i in range(Q):
	qu=input()
	if qu[0]=='1':
		S=S[::-1]
	else:
		if qu[2]=='1':
			S=qu[4]+S
		else:
			S+=qu[4]
print(S)

Submission status: Time Limit Exceeded

Solution code:
s=[input()]
q=int(input())
turn=True
dic={'1':True,'2':False}

for i in range(q):
    qq=input().split()
    if qq[0]=='1':
        turn=not turn
    else:
        if turn==dic[qq[1]]:
            s.insert(0,qq[2])
        else:
            s.append(qq[2])
text="".join(s)
print(text if turn else text[::-1])

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
# 前後反転スイッチ
w = True
for i in range(Q):
    # 入力
    I = list(input().split())
    
    # Ti == 1
    if int(I[0]) == 1:
        w = not w
    # Ti == 2
    else:
        if w:
            # Fi = 1
            if int(I[1]) == 1:
                S = I[2] + S
            # Fi = 2
            else:
                S += I[2]
        else:
            # Fi = 1
            if int(I[1]) == 1:
                S += I[2]
            # Fi = 2
            else:
                S = I[2] + S
    #print(w,S)
    
if not w:
    S = S[::-1]
print(S)

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
for i in range(Q):
    # 入力
    I = list(input().split())
    
    # Ti == 1
    if int(I[0]) == 1:
        S = S[::-1]
        
    # Ti == 2
    else:
        # Fi = 1
        if int(I[1]) == 1:
            S = I[2] + S
        # Fi = 2
        else:
            S += I[2]
print(S)

Submission status: Time Limit Exceeded

Solution code:
s = list(input())
q = int(input())
 
me = 1#1:sei  -1:gyaku
for i in range(q):
    t = input().split()
    if t[0] == '1':
        me *= -1
            
    else:# 0=2
        if t[1]=='1':#1=1
            s.insert(0,t[2]) if me == 1 else s.append(t[2])
        else:#1=2
            s.append(t[2]) if me == 1 else s.insert(0,t[2])
 
if me != 1:
    s = s[::-1]
print(''.join(s))

Submission status: Time Limit Exceeded

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

    is_reversed = False

    for i in range(Q):
        q = input()

        if q[0] == "1":
            is_reversed = not is_reversed
        elif q[0] == "2":
            f, c = q.split()[1:]

            if (f == "1" and not is_reversed) or (f == "2" and is_reversed):
                S = c + S
            else:
                S = S + c

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

    print(S)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

me = 1#1:sei  -1:gyaku
for i in range(q):
    t = input().split()
    if t[0] == '1':
        me *= -1
            
    else:# 0=2
        if t[1]=='1':#1=1
            if me == 1:
                s.insert(0,t[2])
            else:
                s.append(t[2])
        else:#1=2
            if me == 1:
                s.append(t[2])
            else:
                s.insert(0,t[2])

if me != 1:
    s = s[::-1]
print(''.join(s))

Submission status: Time Limit Exceeded

Solution code:
s = list(input())

q = int(input())

me = 1#1:sei  -1:gyaku
for i in range(q):
    t = input().split()
    if t[0] == '1':
        me *= -1
            
    else:# 0=2
        if t[1]=='1':#1=1
            if me == 1:
                s.insert(0,t[2])
            else:
                s.append(t[2])
        else:#1=2
            if me == 1:
                s.append(t[2])
            else:
                s.insert(0,t[2])

if me == 1:
    print(''.join(s))
else:
    for i in range((len(s)-1),-1,-1):
        print(s[i],end='')

Submission status: Time Limit Exceeded

Solution code:
s = input()
n = int(input())
top = 0
for i in range(n):
    x = input()
    if x == "1":
        top += 1
        top = top%2
    else:
        if x.split()[1] == '1':
            if top == 0:
                s = x.split()[2] + s
            else:
                s = s + x.split()[2]
        else:
            if top == 1:
                s = x.split()[2] + s
            else:
                s = s + x.split()[2]
if top == 0:
    print(s)
else:
    print(s[::-1])

Submission status: Time Limit Exceeded

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

tcount = 0
rlist = ""
llist = ""

for i in range(q):
    qret = input()
    if qret[0] == "1":
        tcount += 1
    else:
        f = qret[2]
        c = qret[4]
        if f == "1":
            if tcount % 2 == 0:
                llist = c + llist
            else:
                rlist += c
        else:
            if tcount % 2 == 0:
                rlist += c
            else:
                llist = c + llist

if tcount % 2 == 0:
    print(llist + s + rlist)
else:
    print(rlist[::-1] + s[::-1] + llist[::-1])

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
s = input()
count = int(input())
queries = [input() for _ in range(count)]
for q in queries:
    query = q.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:
s = input()
count = int(input())
queries = [input() for _ in range(count)]
for q in queries:
    query = q.split(' ')
    if len(query) == 1 and query[0] == '1':
        s = s[::-1]
    elif len(query) == 3 and 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:
s = input()
count = int(input())
for _ in range(count):
    query = input().split(' ')
    if len(query) == 1 and query[0] == '1':
        s = s[::-1]
    elif len(query) == 3 and 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 sys import stdin 


S ,= input().split()
Q = int(input())



for _ in range(Q):
	ins = stdin.readline().split()
	if len(ins) == 1:
		S = S[::-1]
	else:
		T, F, C = ins
		F = int(F) 
		if F == 1:
			S = str(C) + S

		else:
			S += str(C)


print (S)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

for i in query:
    if i[0] == '1':
        S.reverse()
    elif i[0] == '2':
        if i[1] == '1':
            S.insert(0, i[2])
        elif i[1] == '2':
            S.append(i[2])
print(''.join(S))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

reversed = 0
for n in range(q):
  query = input()
  # reverse
  if (query[0] == '1'):
    reversed = (reversed+1) % 2
    continue
  # add new alphabet
  # f = query[2], c = query[4]
  
  if (int(query[2])-1) == reversed:
    s = query[4]+ s
  else:
  	s = s + query[4]
    
if (reversed == 1):
  print(s[::-1])
else:
  print(s)

Submission status: Time Limit Exceeded

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

reversed = False
for n in range(q):
  query = input()
  # reverse
  if (query[0] == '1'):
    reversed = not reversed
    continue
  # add new alphabet
  # f = query[2], c = query[4]
  if reversed:
    if (query[2] == '1'):
      s = s + query[4]
    else:
      s = query[4] + s
  else:
    if (query[2] == '1'):
      s = query[4]+ s
    else:
      s = s + query[4]
    
if reversed:
  print(s[::-1])
else:
  print(s)

Submission status: Time Limit Exceeded

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

rev = -1 # -1: False, 1: True
for n in range(q):
  query = input()
  # add new alphabet
  if (len(query) > 1):
    f, c = query[2], query[4]
    # reverse
    if (rev == 1):
      if (f == '1'):
        s = s + c
      else:
        s = c + s
    else:
      if (f == '1'):
        s = c + s
      else:
        s = s + c
	# reverse
  else:
    rev *= -1
    
if (rev == -1):
  print(s)
else:
  print(s[::-1])

Submission status: Time Limit Exceeded

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

for n in range(q):
  query = input()
  # add new alphabet
  if (len(query) > 1):
    f, c = query[2], query[4]
    if (f == '1'):
      s = c + s
    else:
      s = s + c
	# reverse
  else:
    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 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 = list(input())
Q = int(input())

for i in range(Q):
    T = list(map(str,input().split()))
    if int(T[0]) == 1:
        S.reverse()
    elif int(T[1]) == 1:
        S.insert(0,T[2])
    else:
        S.append(T[2])

print("".join(S))

Submission status: Time Limit Exceeded

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

flag = 0
for _ in range(Q):
    q = input()
    if q == "1":
        flag += 1
    else:
        _, t, c = q.split()
        if flag % 2 == 0:
            if t == "1":
                S.insert(0, c)
            else:
                S.append(c)
        else:
            if t == "2":
                S.insert(0, c)
            else:
                S.append(c)

if flag % 2 == 1:
    S.reverse()
print("".join(S))

Submission status: Time Limit Exceeded

Solution code:
moji = input()
n = int(input())
count = 0
for i in range(n):
  query = input().split()
  if len(query) == 1:
    count += 1
  else:
    if count % 2 == 1:
      moji = moji[::-1]
      count = 0
    if query[1] == "1":
      moji = query[2] + moji
    else:
      moji = moji + query[2]
if count % 2 == 1:
  moji = moji[::-1]
print(moji)

Submission status: Time Limit Exceeded

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

for _ in range(Q):
    q = input()
    if q == "1":
        S.reverse()
    else:
        _, t, c = q.split()
        if t == "1":
            S.insert(0, c)
        else:
            S.append(c)

print("".join(S))

Submission status: Time Limit Exceeded

Solution code:
moji = input()
n = int(input())
for i in range(n):
  query = input().split()
  if query[0] == "1":
    moji = moji[::-1]
  else:
    if query[1] == "1":
      moji = query[2] + moji
    else:
      moji = moji + query[2]
print(moji)

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
revese_flag = 0
for i in range(Q):
    inp = input()
    if inp[0] == "1":
        revese_flag = revese_flag ^ 1
    elif inp[2] == "1":
        #import IPython;IPython.embed()
        if revese_flag:
            S = "".join([S, inp[4]])
        else:
            S = "".join([inp[4], S])
    else:
        if revese_flag:
            S = "".join([inp[4], S])
        else:
            S = "".join([S, inp[4]])
    #print("step is ", i)
    #print("input is ", inp)
    #if revese_flag:
    #    print(S[::-1])
    #else:
    #    print(S)
if revese_flag:
    print(S[::-1])
else:
    print(S)

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
revese_flag = 0
for i in range(Q):
    inp = input()
    if inp[0] == "1":
        revese_flag = (revese_flag + 1) % 2
    elif inp[2] == "1":
        #import IPython;IPython.embed()
        if revese_flag:
            S = S + inp[4]
        else:
            S = inp[4] + S
    else:
        if revese_flag:
            S = inp[4] + S
        else:
            S = S + inp[4]
    #print("step is ", i)
    #print("input is ", inp)
    #if revese_flag:
    #    print(S[::-1])
    #else:
    #    print(S)
if revese_flag:
    print(S[::-1])
else:
    print(S)

Submission status: Time Limit Exceeded

Solution code:
S = list(input())

Q = int(input())


for i in range(Q):
    s = list(input().split())
    if len(s) == 1:
        S = S[::-1]
    else:
        if s[1] == '1':
            S.insert(0,s[2])
        else:
            S.append(s[2])

print('%s' % ''.join(S))

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

d = deque(input())

q = int(input())

headflag = 0

for i in range(q):
    s = input()
    if len(s) == 1:
        if headflag == 0:
            headflag = 1
        else:
            headflag = 0
    else:
        l = list(s)
        if headflag == 0:
            if l[2] == '1':
                d.appendleft(l[4])
            else:
                d.append(l[4])
        else:
            if l[2] == '2':
                d.appendleft(l[4])
            else:
                d.append(l[4])

dl = len(d)

if headflag == 0:
    for i in range(len(d)):
        print(d[i], end='')
else:
    for i in range(len(d)):
        print(d[dl-1-i], end='')

print()

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

d = deque()

d.extend(list(input()))

q = int(input())

headflag = 0

for i in range(q):
    s = input()
    if len(s) == 1:
        if headflag == 0:
            headflag = 1
        else:
            headflag = 0
    else:
        l = list(s)
        if headflag == 0:
            if l[2] == '1':
                d.appendleft(l[4])
            else:
                d.append(l[4])
        else:
            if l[2] == '2':
                d.appendleft(l[4])
            else:
                d.append(l[4])

dl = len(d)

if headflag == 0:
    for i in range(len(d)):
        print(d[i], end='')
else:
    for i in range(len(d)):
        print(d[dl-1-i], end='')

print()

Submission status: Time Limit Exceeded

Solution code:
def StrConv(S, T, F, C):
  ans = ''
  if T == '1':
    ans = S[::-1]
  else:
    if F == '1':
      ans = C + S
    else:
      ans = S + C
  return ans
  
#パラメータの取得
S = input()
Q = int(input())
dumy = '|' #仮の文字列くみ上げ変数
cnt = 0   #反転回数カウンタ

#処理実体処理
for i in range(Q):
  tmp = str(input())
  #Q各行の値を、T, F, Cに基づいて処理
  if tmp[0] == '1':
    T = tmp[0]
    F = '-'
    C = '-'
    cnt += 1
  else:
    T, F, C = tmp.split(' ')
  #変換処理本体
  dumy = StrConv(dumy, T, F, C)

#文字列結合処理
if (cnt % 2) == 0:
  ans = dumy[0:dumy.find('|')] + S + dumy[dumy.find('|')+1:]
else:
  ans = dumy[0:dumy.find('|')] + S[::-1] + dumy[dumy.find('|')+1:]
print(ans)

Submission status: Time Limit Exceeded

Solution code:
def StrConv(S, T, F, C):
  ans = ''
  if T == '1':
    ans = S[::-1]
  else:
    if F == '1':
      ans = C + S
    else:
      ans = S + C
  return ans
  
#パラメータの取得
S = input()
Q = int(input())

#処理実体処理
for i in range(Q):
  tmp = str(input())
  #Q各行の値を、T, F, Cに基づいて処理
  if tmp[0] == '1':
    T = tmp[0]
    F = '-'
    C = '-'
  else:
    T, F, C = tmp.split(' ')
  
  #変換処理本体
  S = StrConv(S, T, F, C)
  
print(S)

Submission status: Time Limit Exceeded

Solution code:
S =input()

input_line = int(input())

for i in range(input_line):
    action = list(input())
    # print(action)
    if action[0]=="1":
        S = S[::-1]
    else:
        if action[2] == "2":
            S += action[4]
        else:
            S = action[4]+S
print(S)

Submission status: Time Limit Exceeded

Solution code:
S =input()

input_line = int(input())

# alpha = alpha[::-1]

for i in range(input_line):
    action = list(input())
    # print(action)
    if action[0]=="1":
        S = S[::-1]
    else:
        if action[2] == "2":
            S += action[4]
        elif action[2] == "1":
            S = action[4]+S

print(S)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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