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:
def main():
    S = list(input())
    Q = int(input())
    l = [list(input().split(' ')) for _ in range(Q)]

    FLAG = False

    for li in l:
        if int(li[0]) == 1: # 前後反転
            if FLAG == True:
                FLAG = False
            else:
                FLAG = True
        
        elif int(li[0]) == 2: # 追加, li[1] == 1 => li[2]を先頭に，li[1] == 2 => li[2]を末尾
            if int(li[1]) == 1:
                if FLAG == True:
                    S.append(li[2])
                else:
                    S.insert(0, li[2])
            elif int(li[1]) == 2:
                if FLAG == True:
                    S.insert(0, li[2])
                else:
                    S.append(li[2])
                    
                # S.append(li[2])
        # print(S)
    if FLAG:
        S = S[::-1]
    print(''.join(S))


main()

Submission status: Time Limit Exceeded

Solution code:
def main():
    S = list(input())
    Q = int(input())
    l = [list(input().split(' ')) for _ in range(Q)]

    for li in l:
        if int(li[0]) == 1: # 前後反転
            S = S[::-1]
        
        elif int(li[0]) == 2: # 追加, li[1] == 1 => li[2]を先頭に，li[1] == 2 => li[2]を末尾
            if int(li[1]) == 1:
                S.insert(0, li[2])
            elif int(li[1]) == 2:
                S.append(li[2])
        # print(S)
    print(''.join(S))

main()

Submission status: Time Limit Exceeded

Solution code:
S=__import__('collections').deque(input())
for _ in range(int(input())):
	T=input()
	if T<'2':S.reverse()
	else:(S.appendleft if T[2]<'2'else S.append)(T[4])
print(*S,sep='')

Submission status: Time Limit Exceeded

Solution code:
S=__import__('collections').deque(input())
for _ in range(int(input())):
	T=input()
	if T<'2':S.reverse()
	else:(S.appendleft if T[2]<'2' else S.append)(T[4])
print(*S,sep='')

Submission status: Time Limit Exceeded

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

for i in range(q):
    tfc = input().split()
    if len(tfc) == 1:
        t = tfc[0]
    else:
        t, f, c = tfc
    if t == '1':
        s.reverse()
    elif t == '2':
        if f == '1':
            s[0:0] = c
        elif f == '2':
            s.append(c)

print("".join(s))

Submission status: Time Limit Exceeded

Solution code:
def main():
  s = input()
  q = int(input())
  t = ""
  for i in range(q):
    tm = input()
    if tm[0] == '1':
      s = s[::-1]
    else:
      if tm[2] == '1':
        s = tm[4] + s
      else:
        s = s + tm[4]
  print(s)


main()

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

mode = 0
for _ in range(q):
    query = input().split()
#     print(s, query, mode)
    if len(query) == 1:
        mode = 1 - mode
    else:
        if mode == 0:
            if query[1] == "1":
                s = query[2] + s
            else:
                s = s + query[2]
        else:
            if query[1] == "1":
                s = s + query[2]
            else:
                s = query[2] + s
if mode == 1:
    s = s[::-1]
print(s)

Submission status: Time Limit Exceeded

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

for _ in range(q):
    query = input().split()
    if len(query) == 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:
S = input()
Q = int(input())
flag = True
for i in range(Q):
    que = input()
    if len(que) == 1:
        if flag:
            flag = False
        else:
            flag = True
    else:
        if que[2] == '1':
            if flag:
                T = que[4]+S
            else:
                T = S+que[4]
        else:
            if flag:
                T = S+que[4]
            else:
                T = que[4]+S
        S = T
if flag:
    print(S)
else:
    for i in range(len(S)):
        print(S[len(S)-i-1], end = '')
    print()

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
flag = True
for i in range(Q):
    que = input()
    if len(que) == 1:
        if flag:
            flag = False
        else:
            flag = True
    else:
        if que[2] == '1':
            if flag:
                S = ''.join([que[4], S])
            else:
                S = ''.join([S, que[4]])
        else:
            if flag:
                S = ''.join([S, que[4]])
            else:
                S = ''.join([que[4], S])
if flag:
    print(S)
else:
    for i in range(len(S)):
        print(S[len(S)-i-1], end = '')
    print()

Submission status: Time Limit Exceeded

Solution code:
S=str(input())
S_list=list(S)

Q=int(input())

q=[]
right=[]
left=[]

rev=1

for i in range(Q):
    q.append(input().split())
    if q[i][0]=="1":
        rev=rev*(-1)
    elif q[i][0]=="2":
        if q[i][1]=="1":
            if rev==1:
                right.reverse()
                right.append(q[i][2])
                right.reverse()
            else:
                left.append(q[i][2])
        elif q[i][1]=="2":
            if rev==1:
                left.append(q[i][2])
            else:
                right.reverse()
                right.append(q[i][2])
                right.reverse()

if rev==1:
    S_list=right+S_list+left
else:
    S_list=right+S_list+left
    S_list.reverse()

S=''.join(S_list)
print(S)

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
flag = True
for i in range(Q):
    que = input()
    if len(que) == 1:
        if flag:
            flag = False
        else:
            flag = True
    else:
        if que[2] == '1':
            if flag:
                S = que[4]+S
            else:
                S = S+que[4]
        else:
            if flag:
                S = S+que[4]
            else:
                S = que[4]+S
if flag:
    print(S)
else:
    for i in range(len(S)):
        print(S[len(S)-i-1], end = '')
    print()

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]
	else:
		if query[1] == '1':
			S = query[2] + S
		else:
			S = S + query[2]
print(S)

Submission status: Time Limit Exceeded

Solution code:
S = input('')
Q = int(input(''))
rev_flag = 1
for i in range(Q):
    str = input('')
    if(str.count(' ') == 0 and str == '1'):
        rev_flag = -rev_flag
    elif(str[0] == '2' and ((str[2] == '1' and rev_flag == 1) or (str[2] == '2' and rev_flag == -1))):
        S = str[4] + S
    elif(str[0] == '2' and ((str[2] == '2' and rev_flag == 1) or (str[2] == '1' and rev_flag == -1))):
        S = S + str[4]
if rev_flag == 1:
    print(S)
elif rev_flag == -1:
    print(S[::-1])

Submission status: Time Limit Exceeded

Solution code:
S=str(input())
S_list=list(S)

Q=int(input())

q=[]
for i in range(Q):
    q.append(input().split())
    if q[i][0]=="1":
        S_list.reverse()
    elif q[i][0]=="2":
        if q[i][1]=="1":
            S_list.reverse()
            S_list.append(q[i][2])
            S_list.reverse()
        elif q[i][1]=="2":
            S_list.append(q[i][2])

S=''.join(S_list)
print(S)

Submission status: Time Limit Exceeded

Solution code:
S=str(input())
S_list=list(S)

Q=int(input())

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

for i in range(Q):
    if q[i][0]=="1":
        S_list.reverse()
    elif q[i][0]=="2":
        if q[i][1]=="1":
            S_list.reverse()
            S_list.append(q[i][2])
            S_list.reverse()
        elif q[i][1]=="2":
            S_list.append(q[i][2])

S=''.join(S_list)
print(S)

Submission status: Time Limit Exceeded

Solution code:

S=str(input())
S_list=list(S)

Q=int(input())

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

for i in range(Q):
    if q[i][0]=="1":
        S_list.reverse()
    elif q[i][0]=="2":
        if q[i][1]=="1":
            S_list.insert(0,q[i][2])
        elif q[i][1]=="2":
            S_list.append(q[i][2])

S=''.join(S_list)
print(S)

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
cnt = 0
for i in range(q):
  query = input()
  if query == '1':
    cnt += 1
  else:
    t, f, c = query.split()
    if (f == '1' and cnt % 2 == 0) or (f == '2' and cnt % 2 == 1):
      s = c + s
    else:
      s = s + c
if cnt % 2 == 0:
  print(s)
else:
  print(s[::-1])

Submission status: Time Limit Exceeded

Solution code:
import sys
readline = sys.stdin.readline
s = readline().strip()
n = int(readline())
rev = False
for i in range(n):
    q = readline()
    if q.startswith("1"):
        rev = not rev
    if q.startswith("2 1"):
        if not rev:
            s = q[-2] + s
        else:
            s = s + q[-2]
    if q.startswith("2 2"):
        if not rev:
            s = s + q[-2]
        else:
            s = q[-2] + s
if rev:
    print(s[::-1])
else:
    print(s)

Submission status: Time Limit Exceeded

Solution code:
S = input('')
Q = int(input(''))
for i in range(Q):
    str = input('')
    if(str.count(' ') == 0 and str == '1'):
        S = S[::-1]
    elif(str[0] == '2' and str[2] == '1'):
        S = '{0}{1}'.format(str[4], S)
    elif(str[0] == '2' and str[2] == '2'):
        S = '{0}{1}'.format(S, str[4])
print(S)

Submission status: Time Limit Exceeded

Solution code:
S = input('')
Q = int(input(''))
for i in range(Q):
    str = input('')
    if(str.count(' ') == 0 and str == '1'):
        S = S[::-1]
    else:
        if(str[0] == '2' and str[2] == '1'):
            S = '{0}{1}'.format(str[4], S)
        elif(str[0] == '2' and str[2] == '2'):
            S = '{0}{1}'.format(S, str[4])
print(S)

Submission status: Time Limit Exceeded

Solution code:
import sys
readline = sys.stdin.readline
s = readline().strip()
n = int(readline())
for i in range(n):
    q = readline()
    if q.startswith("1"):
        s = s[::-1]
    else:
        a = q.strip().split(" ")
        if a[1] == "1":
            s = a[2] + s
        if a[1] == "2":
            s = s + a[2]
print(s)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
for _ in range(Q):
    q = list(input().split())
    if int(q[0]) == 1:
        S = S[::-1]
    else:
        if int(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())
rev = False
for i in range(Q):
    q = input()
    if q == '1':
        rev = not rev
    else:
        F, C = q.split()[1:]
        if (F == '1' and not rev) or (F == '2' and rev):
            S = C + S
        else:
            S = S + C
if rev:
    S = S[::-1]
print(S)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
head = True # True: left side is head
for i in range(Q):
  Query_i = list(map(str, input().split()))
  if Query_i[0]=='1':
    head = not head
  elif Query_i[1] == '1' and head:
    S = Query_i[2] + S
  elif Query_i[1] == '1' and not head:
    S = S + Query_i[2]
  elif Query_i[1] == '2' and head:
    S = S + Query_i[2]
  else: # Query_i[1] == '2' and not head:
    S = Query_i[2] + S
if not head:
  S = S[::-1]
print(S)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
rev = 1
for _ in range(q):
  ope = input().split()
  if ope[0] == "1":
    s = s[::-1]
    #rev *= -1
  else:
    if ope[1]=="1":
      s = ope[2]+s
    else:
      s = s+ope[2]
print(s)

Submission status: Time Limit Exceeded

Solution code:
ans=input()
n=int(input())
for i in range(n):
  t=input()
  if t[0]=="1":
    ans=ans[::-1]
  else:
    if t[2]=="1":
      ans=t[4]+ans
    else:
      ans=ans+t[4]
print(ans)

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]
    continue
  else:
    t, f, c = query.split()
    if f == '1':
      s = c + s
    else:
      s = s + c
print(s)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
a = input()
Q = int(input())
Qy = [list(map(str,input().split())) for i in range(Q)]
cnt = 0
han = 0

for i in range(Q):
    if Qy[i][0] == "1":
        cnt += 1
        if han == 0:
            han = 1
        else:
            han = 0
    elif Qy[i][1] == "1":
        if han == 1:
            a += Qy[i][2]
        else:
            a = Qy[i][2] + a
    else :
        if han == 1:
            a = Qy[i][2] + a
        else:
            a += Qy[i][2]

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

print(a)

Submission status: Time Limit Exceeded

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

for i in range(Q):
    if Qy[i][0] == "1":
        a = a[::-1]
    elif Qy[i][1] == "1":
        a = Qy[i][2] + a
    else :
        a += Qy[i][2]
print(a)

Submission status: Time Limit Exceeded

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

for i in range(Q):
    if Qy[i][0] == "1":
        a = a[::-1]
    elif Qy[i][1] == "1":
        a = Qy[i][2] + a
    elif Qy[i][1] == "2":
        a += Qy[i][2]
print(a)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

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

for i in range(Q):
    if List[i][0] == "1":
        S.reverse()
    else:
        if List[i][1] == "1":
            S.insert(0, List[i][2])
        else:
            S.append(List[i][2])

print("".join(list(S)))

Submission status: Time Limit Exceeded

Solution code:

s = input()
q = int(input())
t = []
 
def F(f,ans,c):
  if f == '1':
     return c + ans
  if f == '2':
    return ans + c
 
def R(r):
  if r == '1':return '2'
  if r == '2':return '1'
 
ans = s
r = '1'
for n in range(q):
  t = input().split()
  if t[0] == '1':
    r = R(r)
  else:
    if r =='2':
      t[1] = R(t[1])
    ans = F(t[1],ans,t[2])
 
    
if r == '1': 
   print(ans)
else:
  print(ans[::-1])

Submission status: Time Limit Exceeded

Solution code:
S=input()
Q=int(input())
s=False
count=0
def plus(x,num,q,s):
    if num=='1':
        if not(s):
            return q+x
        if s:
            return x+q
    else:
        if not(s):
            return x+q
        if s:
            return q+x
def play(x,Qlist,s,count):
    if Qlist[0]=='1':
        s=not(s)
        xx=x
        count+=1
        return s,xx,count
    else:
        num=Qlist[1]
        q=Qlist[2]
        xx=plus(x,num,q,s)
        return s,xx,count

for i in range(Q):
    Qi=input().split()
    s,S,count=play(S,Qi,s,count)

if count%2==0:
    print(S)
else:
    print(S[::-1])

Submission status: Time Limit Exceeded

Solution code:

S = input()

ql = []
for i in range(int(input())):
    ql.append(input())


for query in ql:
    if query == "1":
        S = S[::-1]
    else:
        _, tp, s2 = query.split(" ")
        if tp == "1":
            S = s2 + S
        else:
            S = S + s2
print(S)

Submission status: Time Limit Exceeded

Solution code:
S=input()
Q=int(input())
def plus(x,num,q):
    if num=='1':
        return q+x
    else:
        return x+q
def play(x,Qlist):
    if Qlist[0]=='1':
        n=len(x)
        xx=x[::-1]
        return xx
    else:
        num=Qlist[1]
        q=Qlist[2]
        xx=plus(x,num,q)
        return xx

for i in range(Q):
    Qi=input().split()
    S=play(S,Qi)



print(S)

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
t = []

def F(f,fr,ba,c):
  if f == '1':
     return c + fr, ba
  if f == '2':
    return fr, ba + c

def R(r):
  if r == '1':return '2'
  if r == '2':return '1'

fr = ''
ba = ''
r = '1'
for n in range(q):
  t = input().split()
  if t[0] == '1':
    r = R(r)
  else:
    if r =='2':
      t[1] = R(t[1])
    fr,ba = F(t[1],fr,ba,t[2])


    
if r == '1': 
   print(fr + s + ba)
else:
  a = ''
  ans = fr + s + ba
  for n in ans:
    a = n + a
  print(a)

Submission status: Time Limit Exceeded

Solution code:
s = input()
n = int(input())
x = 1
p = ['','']
for i in range(n):
    t = list(map(str, input().split()))
    if(t[0] == '1'):
        x *= -1
    elif(int(t[1]) == 1):
        if(x == 1):
            p[0] += t[2]
        else:
            p[1] += t[2]
    else:
        if(x == 1):
            p[1] += t[2]
        else:
            p[0] += t[2]        
if(x == -1):
    print(p[1][::-1] + s[::-1] + p[0])
else:
    print(p[0][::-1] + s + p[1])

Submission status: Time Limit Exceeded

Solution code:
import sys
s = input()
q = int(input())
t = []

def F(f,ans,c):
  if f == '1':
     return c + ans
  if f == '2':
    return ans + c

def R(r):
  if r == '1':return '2'
  if r == '2':return '1'

ans = s
r = '1'
for n in range(q):
  t = input().split()
  if t[0] == '1':
    r = R(r)
  else:
    if r =='2':
      t[1] = R(t[1])
    ans = F(t[1],ans,t[2])

    
if r == '1': 
   print(ans)
else:
  a = ''
  for n in ans:
    a = n + a
  print(a)

Submission status: Time Limit Exceeded

Solution code:
s = input()
n = int(input())
x = 1
p = ['','']
for i in range(n):
    t = list(map(str, input().split()))
    if(t[0] == '1'):
        x *= -1
    elif(int(t[1])*x == 1):
        p[0] += t[2]
    elif(int(t[1])*x == -2):
        p[0] += t[2]
    else:
        p[1] += t[2]
if(x == -1):
    print(p[1][::-1] + s[::-1] + p[0])
else:
    print(p[0][::-1] + s + p[1])

Submission status: Time Limit Exceeded

Solution code:
s = input()
n = int(input())
x = 1
p = ['','']
for i in range(n):
    t = list(map(str, input().split()))
    if(t[0] == '1'):
        x *= -1
    elif(int(t[1])*x == 1 or int(t[1])*x == -2):
        p[0] += t[2]
    else:
        p[1] += t[2]
if(x == -1):
    print(p[1][::-1] + s[::-1] + p[0])
else:
    print(p[0][::-1] + s + p[1])

Submission status: Time Limit Exceeded

Solution code:
s = input()
n = int(input())
x = 1
p = ['','']
for i in range(n):
    t = list(map(str, input().split()))
    if(t[0] == '1'):
        x *= -1
    elif(int(t[1])*x == 1 or int(t[1])*x == -2):
        p[0] = t[2] + p[0]
    else:
        p[1] += t[2]
if(x == -1):
    print(p[1][::-1] + s[::-1] + p[0][::-1])
else:
    print(p[0] + s + p[1])

Submission status: Time Limit Exceeded

Solution code:
s = list(input())
q = int(input())
rv = False
fr = []
re = []
for i in range(q):
    t = input().split(' ')
    if t[0] == '1':
        rv = True if not rv else False
    else:
        if t[1] == '1' and not rv or t[1] == '2' and rv:
            fr.insert(0, t[2])
        else:
            if t[1] == '1' and rv or t[1] == '2' and not rv:
                re.append(t[2])
if rv:
    re.reverse()
    s.reverse()
    fr.reverse()
    print(''.join(re + s + fr))
else:
    print(''.join(fr + s + re))

Submission status: Time Limit Exceeded

Solution code:
s = input()
n = int(input())
x = 1
p = ['','']
for i in range(n):
    t = list(map(str, input().split()))
    if(t[0] == '1'):
        x *= -1
    elif(int(t[1])*x == 1 or int(t[1])*x == -2):
        p[0] = t[2] + p[0]
    else:
        p[1] += t[2]
if(x == -1):
    s = p[1][::-1] + s[::-1] + p[0][::-1]
else:
    s = p[0] + s + p[1]
print(s)

Submission status: Time Limit Exceeded

Solution code:
s = input()
n = int(input())
x = 1
for i in range(n):
    t = list(map(str, input().split()))
    if(t[0] == '1'):
        x *= -1
    elif(int(t[1])*x == 1 or int(t[1])*x == -2):
        s = t[2] + s
    else:
        s += t[2]
if(x == -1):
    s = s[::-1]
print(s)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
if __name__ == '__main__':
    s = input()
    q = int(input())
    ans = s
    rev_count = 0
    leftList = []
    rightList =[]
    alist =[]

    for i in range(q):
        a = [i for i in input().split()]
        alist.append(a)
        if len(a) == 1:
            rev_count = rev_count +1

    if rev_count % 2 ==1:
        ans = ans[::-1]

    for i in alist:
        if len(i) == 1:
            rev_count = rev_count -1
        else:
            if int(i[1]) == 1:
                if rev_count % 2 == 1:
                    rightList.append(i[2])
                else:
                    leftList.insert(0,i[2])

            else:
                if rev_count % 2 == 1:
                    leftList.insert(0,i[2])
                else:
                    rightList.append(i[2])

    ans = ''.join(leftList)+ ans+''.join(rightList)

    print(ans)

Submission status: Time Limit Exceeded

Solution code:
s = list(input())
q = int(input())
rv = False
for i in range(q):
    t = input().split(' ')
    if t[0] == '1':
        rv = True if not rv else False
    else:
        if t[1] == '1' and not rv or t[1] == '2' and rv:
            s.insert(0, t[2])
        else:
            if t[1] == '1' and rv or t[1] == '2' and not rv:
                s.append(t[2])
print(''.join(s) if not rv else ''.join(s[::-1]))

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
rv = False
for i in range(q):
    t = input().split(' ')
    if t[0] == '1':
        rv = True if not rv else False
    else:
        if t[1] == '1' and not rv or t[1] == '2' and rv:
            s = t[2] + s
        else:
            if t[1] == '1' and rv or t[1] == '2' and not rv:
                s = s + t[2]
print(s if not rv else s[::-1])

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
s = str(input())
q = int(input())  
st = s
sth = s
stm = ""
count = 0
for i in range(q):
  k = input().split()
  if(k[0] == '1'):
    count = (count + 1) % 2
  else:
    c = count - 2
    d = int(k[1]) 
    e = k[2]
    if(c + d != 0):
      sth = e + sth
    else:
      stm = stm + e

st = sth + stm
if(count % 2 == 1):
  st = st[::-1]
      
print(st)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
s = str(input())
q = int(input())  
st = s
count = 0
for i in range(q):
  k = input().split()
  if(k[0] == '1'):
    count = (count + 1) % 2
  else:
    c = count - 2
    d = int(k[1]) 
    e = k[2]
    if(c + d != 0):
      st = e + st
    else:
      st = st + e

if(count % 2 == 1):
  st = st[::-1]
      
print(st)

Submission status: Time Limit Exceeded

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

for i in range(n):
	com = input().split()
	if len(com) == 1:
		status = not status
	else:
		s = com[2]*bool(((com[1]=='1')+status-1)) + s + com[2]*bool(((com[1]=='2')+status-1))
        
if not status:
	s = s[::-1]
print(s)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
s = str(input())
q = int(input())  
st = s
count = 0
for i in range(q):
  k = input().split()
  if(k[0] == '1'):
    count = (count + 1) % 2
  else:
    c = count
    d = int(k[1]) 
    e = k[2]
    if(c + d == 1 or c + d == 3):
      st = e + st
    elif(c + d == 2):
      st = st + e

if(count % 2 == 1):
  st = st[::-1]
      
print(st)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
s = str(input())
q = int(input())
l = []
for i in range(q):
  qy = input().split()
  l.append(qy)
  
st = s
count = 0
for i in range(q):
  k = l[i][0]
  if(k == '1'):
    count = (count + 1) % 2
  else:
    c = count
    d = int(l[i][1]) 
    e = l[i][2]
    if(c + d == 1 or c + d == 3):
      st = e + st
    elif(c + d == 2):
      st = st + e

if(count % 2 == 1):
  st = st[::-1]
      
print(st)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
s = str(input())
q = int(input())
l = []
for i in range(q):
  qy = input().split()
  l.append(qy)
  
st = s
count = 0
for i in range(q):
  k = l[i][0]
  if(k == '1'):
    count += 1
  else:
    c = count % 2
    d = int(l[i][1]) 
    e = l[i][2]
    if(c + d == 1 or c + d == 3):
      st = e + st
    elif(c + d == 2):
      st = st + e

if(count % 2 == 1):
  st = st[::-1]
      
print(st)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
s = str(input())
q = int(input())
l = []
for i in range(q):
  qy = input().split()
  l.append(qy)
  
st = s
count = 0
for i in range(q):
  k = l[i][0]
  if(k == '1'):
    count += 1
  elif(k == '2'):
    c = count % 2
    d = int(l[i][1]) 
    e = l[i][2]
    if(c + d == 1 or c + d == 3):
      st = e + st
    elif(c + d == 2):
      st = st + e

if(count % 2 == 1):
  st = st[::-1]
      
print(st)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
s = str(input())
q = int(input())
l = []
for i in range(q):
  qy = input().split()
  l.append(qy)
  
st = s
count = 0
for i in range(q):
  k = l[i][0]
  if(k == '1'):
    count += 1
  elif(k == '2'):
    c = count
    d = int(l[i][1])
    e = l[i][2]
    if((c + d) % 2 == 1):
      st = e + st
    elif((c + d) % 2 == 0):
      st = st + e

if(count % 2 == 1):
  st = st[::-1]
      
print(st)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
s = str(input())
q = int(input())
l = []
for i in range(q):
  qy = input().split()
  l.append(qy)
  
st = s
count = 0
for i in range(q):
  k = l[i][0]
  if(k == '1'):
    count += 1
  elif(k == '2'):
    c = count
    d = int(l[i][1])
    if((c + d) % 2 == 1):
      e = l[i][2]
      st = e + st
    elif((c + d) % 2 == 0):
      e = l[i][2]
      st = st + e

if(count % 2 == 1):
  st = st[::-1]
      
print(st)