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:
import collections as co
S=input()
Q=int(input())
count=0
back=str()
front=str()
judge=True
for i in range(Q):
    a=list(map(str,input().split()))   
    if  int(a[0])==1:
        if judge==True:
            judge=False
        else:
            judge=True
    elif int(a[0])==2 and int(a[1])==1:
        if judge==True:
            front=a[2]+front
        if judge==False:
            back=back+a[2]
    else:
        if judge==True:
            back=back+a[2]
        if judge==False:
            front=a[2]+front
    a.clear
if judge==True:
    S=front+S+back
if judge==False:
    S=S[::-1]
    back=back[::-1]
    front=front[::-1]
    S=back+S+front
print(S)

Submission status: Time Limit Exceeded

Solution code:
import collections as co
S=input()
Q=int(input())
count=0
back=str()
front=str()
for i in range(Q):
    a=list(map(str,input().split()))
    if  int(a[0])==1:
        count+=1
        back_front=front[::-1]
        front_back=back[::-1]
        back=back_front
        front=front_back
    elif int(a[0])!=1 and int(a[1])==1:
        front=a[2]+front
    elif int(a[0])!=1 and int(a[1])==2:
        back=back+a[2]
    a.clear
if count%2==1:
    S=S[::-1]
S=front+S+back
print(S)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
#初期入力
import sys
input = sys.stdin.readline
S = input().split()
S=S[0]
Q = int(input())
FQ_dic =tuple()
rev =0
for i in range(Q):
    FQ_dic =tuple(input().split())

    if      FQ_dic[0] =="1": #逆転する
                rev +=1
    else:                   #逆転なし   
        if      FQ_dic[1]=="1":     #後ろにつける
            if rev %2 ==0:
                    S = FQ_dic[2] + S
            else:
                    S = S +FQ_dic[2] 
        else: #                      #前につける
            if rev %2 ==0:
                    S = S + FQ_dic[2]
            else:        
                    S = FQ_dic[2] + S
if rev %2 ==0:
    print(S)
else:
    print(S[::-1])

Submission status: Time Limit Exceeded

Solution code:
#初期入力
S = input()
Q = int(input())
FQ_dic =tuple()
rev =0
for i in range(Q):
    FQ_dic =tuple(input().split())

    if      FQ_dic[0] =="1": #逆転する
                rev +=1
    else:                   #逆転なし   
        if      FQ_dic[1]=="1":     #後ろにつける
            if rev %2 ==0:
                    S = FQ_dic[2] + S
            else:
                    S = S +FQ_dic[2] 
        else: #                      #前につける
            if rev %2 ==0:
                    S = S + FQ_dic[2]
            else:        
                    S = FQ_dic[2] + S
if rev %2 ==0:
    print(S)
else:
    print(S[::-1])

Submission status: Time Limit Exceeded

Solution code:
#初期入力
S = input()
Q = int(input())
FQ_dic ={i:tuple() for i in range(Q)}
    
for i in range(Q):
    FQ_dic[i] =tuple(input().split())
    
#
rev =0
for fq in FQ_dic.values():
    if      fq[0] =="1": #逆転する
                rev +=1
    else:               #逆転なし   
        if      fq[1]=="1":     #後ろにつける
            if rev %2 ==0:
                    S = fq[2] + S
            else:
                    S = S +fq[2] 
        else: #                 #前につける
            if rev %2 ==0:
                    S = S + fq[2]
            else:        
                    S = fq[2] + S
if rev %2 ==0:
    print(S)
else:
    print(S[::-1])

Submission status: Time Limit Exceeded

Solution code:
#初期入力
S = input()
Q = int(input())
FQ_dic ={i:tuple() for i in range(Q)}
    
for i in range(Q):
    FQ_dic[i] =tuple(input().split())
    
#
rev =0
for fq in FQ_dic.values():
    if      fq[0] =="1":
                rev +=1
    elif    fq[0] =="2":
        if      fq[1]=="1": #後ろにつける
            if rev %2 ==0:
                    S = fq[2] + S
            if rev %2 ==1:
                    S = S +fq[2] 
        elif    fq[1] =="2": #前につける
            if rev %2 ==0:
                    S = S + fq[2]
            if rev %2 ==1:        
                    S = fq[2] + S
if rev %2 ==0:
    print(S)
else:
    print(S[::-1])

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
back = 1
for i in range(q):
    query = input().split()
    if query[0] == '1':
        back *= -1
    else:
        if query[1] == '1':
            if back == 1:
                s = ('').join(query[2:]) + s
            else:
                s = s + ('').join(query[2:])
        else:
            if back == 1:
                s = s + ('').join(query[2:])
            else:
                s = ('').join(query[2:]) + s
if back == -1:
    s = s[-1::-1]
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::-1]
    else:
        if query[1] == '1':
            s = ('').join(query[2:]) + s
        else:
            s = s + ('').join(query[2:])
print(s)

Submission status: Time Limit Exceeded

Solution code:
#初期入力
S = input()
Q = int(input())
FQ_dic ={i:tuple() for i in range(Q)}

for i in range(Q):
    FQ_dic[i] =tuple(input().split())
    
#
for fq in FQ_dic.values():
    if      fq[0] =="1":
                S=S[::-1]
    elif    fq[0] =="2":
        if      fq[1]=="1":
                    S = fq[2] + S
        elif    fq[1] =="2":
                    S = S + fq[2]
print(S)

Submission status: Time Limit Exceeded

Solution code:
s = list(input())
q = int(input())
judge = 1
for i in range(q):
  d = input()
  if d == '1':
    judge *= -1
  else:
    a, b, c = list(d.split())
    b = int(b)
    if (b == 1 and judge ==1) or (b == 2 and judge == -1):
      s.insert(0, c)
    else:
      s.append(c)
if judge == 1:
  print(*s,sep='')
else:
  print(*s[::-1], sep='')

Submission status: Time Limit Exceeded

Solution code:
s = list(input())
q = int(input())
for i in range(q):
  d = input()
  if d == '1':
    s.reverse()
  else:
    a, b, c = list(d.split())
    b = int(b)
    if b == 1:
      s.insert(0, c)
    else:
      s.append(c)
print(*s,sep='')

Submission status: Time Limit Exceeded

Solution code:
s = list(input())
q = int(input())
for i in range(q):
  d = input()
  if len(d) == 5:
    a, b, c = list(d.split())
    a = int(a)
    b = int(b)
    if b == 1:
      s.insert(0, c)
    else:
      s.append(c)
  else:
    s.reverse()
print(*s,sep='')

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
h = 1
front = ""
back = ""
def act2(f,c,h):
    global front
    global back
    if f==1:
        if h==1:
            front += c
        else:
            back += c
    else:
        if h==1:
            back += c
        else:
            front += c

for i in range(q):
    a = input()
    if len(a)==1:
        h *= -1
    else:
        a = list(a.split())
        act2(int(a[1]),a[2],h)


if h==1:
    print(front[::-1]+s+back)
else:
    print(back[::-1]+s[::-1]+front)

Submission status: Time Limit Exceeded

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

def act2(s,f,c,h):
    if f==1:
        if h==1:
            s = c + s
        else:
            s = s + c
    else:
        if h==1:
            s = s + c
        else:
            s = c + s
    return s

for i in range(q):
    a = input()
    if len(a)==1:
        h *= -1
    else:
        a = list(a.split())
        s = act2(s,int(a[1]),a[2],h)

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

Submission status: Time Limit Exceeded

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

def act1(s):
    return s[::-1]

def act2(s,f,c):
    if f==1:
        s = c + s
    else:
        s = s + c
    return s

for i in range(q):
    a = input()
    if len(a)==1:
        s = act1(s)
    else:
        a = list(a.split())
        s = act2(s,int(a[1]),a[2])

print(s)

Submission status: Time Limit Exceeded

Solution code:
from math import floor
import sys
from copy import copy

from collections import deque
input = sys.stdin.readline
'''
n, m = map(int, input().split())
n = int(input())
A = list(map(int, input().split()))
S = input().strip()
for test in range(int(input())):
'''
inf = 100000000000000000  # 1e17
mod = 998244353




S = input().strip()
D = deque(S)


n = int(input())
reverse = False
for operation in range(n):
    data = input().strip()
    if data[0] == "1":
        if reverse is False:
            reverse = True
        else:
            reverse = False
    else:
        f = data[2]
        ele = data[4]
        if (f == "1" and reverse is False) or (f == "2" and reverse == True):
            D.appendleft(ele)
        else:
            D.append(ele)
#    print(S)
# print(reverse)
if reverse is False:
    for d in D:
        print(d,end="")
else:
    ANS=""
    for d in D:
        ANS+=d
    print(ANS[::-1])

Submission status: Time Limit Exceeded

Solution code:
from math import floor
import sys
from copy import copy
input = sys.stdin.readline
'''
n, m = map(int, input().split())
n = int(input())
A = list(map(int, input().split()))
S = input().strip()
for test in range(int(input())):
'''
inf = 100000000000000000  # 1e17
mod = 998244353

S = input().strip()
n = int(input())
reverse = False
for operation in range(n):
    data = input().strip()
    if data[0] == "1":
        if reverse is False:
            reverse = True
        else:
            reverse = False
    else:
        f = data[2]
        ele = data[4]
        if (f == "1" and reverse is False) or (f == "2" and reverse == True):
            S = ele + S
        else:
            S = S + ele
#    print(S)
# print(reverse)
if reverse is False:
    print(S)
else:
    print(S[::-1])

Submission status: Time Limit Exceeded

Solution code:
S=str(input())
Q=int(input())
for j in range(Q):
  L=list(map(str,input().split()))
  if L[0]=="1":
    S="".join(list(reversed(S)))
  else:
    if L[1]=="1":
      S=L[2]+S
    else:
      S=S+L[2]
print(S)

Submission status: Time Limit Exceeded

Solution code:
S=str(input())
Q=int(input())
for j in range(Q):
  L=list(map(str,input().split()))
  if L[0]=="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())
for _ in range(q):
    l = input().split()
    if len(l) == 1:
        s = s[::-1]
    elif l[1] == '1':
        s = l[2] + s
    else:
        s = s+l[2]
print(s)

Submission status: Time Limit Exceeded

Solution code:
import sys
def input(): return sys.stdin.readline().rstrip()
def main():
    s=input()
    q=int(input())
    inv=False
    for i in range(q):
        que=input()
        if que[0]=="1":
            inv=not(inv)
        else:
            f=int(que[2])
            if (f==1 and not(inv)) or (f==2 and inv):
                s=que[4]+s
            else:
                s+=que[4]
    if inv:
        print(s[::-1])
    else:
        print(s)



if __name__=='__main__':
    main()

Submission status: Time Limit Exceeded

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

cnt = 0
pre = []
post = []
a = ['']*n

def list_to_str(x):
    z =''
    for y in range(len(x)):
        z += x[y]
    return z

def rev(t):
    m = len(t)
    b = ['']*m
    for j in range(m):
        b[j] = t[m-j-1]
    c = ''
    c = list_to_str(b)
    return c

for i in range(n):
    a[i] = input()
    if a[i] == '1':
        cnt += 1
    else:
        p,q,r = list(a[i].split())
        if q == '1':
            if cnt%2 == 0:
                pre.insert(0,r)
            else:
                post.append(rev(r))
        else:
            if cnt%2 == 0:
                post.append(r)
            else:
                pre.insert(0,rev(r))         
if cnt%2 == 0:
    output = list_to_str(pre) + s + list_to_str(post)
else:
    output = rev(list_to_str(pre) + s + list_to_str(post))
print(output)

Submission status: Time Limit Exceeded

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

a = ['']*n
def rev(t):
    m = len(t)
    b = ['']*m
    for j in range(m):
        b[j] = t[m-j-1]
    c = ''
    for k in range(len(b)):
        c += b[k]
    return c

for i in range(n):
    a[i] = input()
    if a[i] == '1':
        s = rev(s)
    else:
        p,q,r = list(a[i].split())
        if q == '1':
            s = r+s
        else:
            s = s+r
print(s)

Submission status: Time Limit Exceeded

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

a = ['']*n
def rev(t):
    m = len(t)
    b = ['']*m
    for j in range(m):
        b[j] = t[m-j-1]
    c = ''
    for k in range(len(b)):
        c += b[k]
    return c

for i in range(n):
    a[i] = input()
    if a[i] == '1':
        s = rev(s)
    else:
        p,q,r = list(a[i].split())
        if q == '1':
            s = r+s
        else:
            s = s+r
print(s)

Submission status: Time Limit Exceeded

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

rev = 0
for i in range(q):
    inp = list(input().split())
    if inp[0] == '1':rev = (rev + 1) % 2
    else:
        if (inp[1] == '1' and rev == 0) or (inp[1] == '2' and rev == 1):
            s = inp[2] + s
        else:
            s = s + inp[2]

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

Submission status: Time Limit Exceeded

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

rev = 0
for i in range(q):
    inp = list(input().split())
    if inp[0] == '1':rev = rev ^ 1
    if inp[0] == '2':
        if (inp[1] == '1' and rev == 0) or (inp[1] == '2' and rev == 1):
            s = inp[2] + s
        else:
            s = s + inp[2]

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

Submission status: Time Limit Exceeded

Solution code:
import sys

s = sys.stdin.readline().rstrip()
q = int(sys.stdin.readline().rstrip())
query = [sys.stdin.readline().rstrip().split() for i in range(q)]
top = 1
s = list(s)
for i in range(q):
    que = query[i]
    if que[0] == '1':
        top *= -1
    else:
        if que[1] == '1':
            if top > 0:
                s.insert(0, que[2])
                # s = ''.join([que[2], s])
                # s = que[2] + s
            else:
                s.append(que[2])
                # s = ''.join([s, que[2]])
                # s = s + que[2]
        else:
            if top > 0:
                s.append(que[2])
                # s = ''.join([s, que[2]])
                # s = s + que[2]
            else:
                s.insert(0, que[2])
                # s = ''.join([que[2], s])
                # s = que[2] + s

s = ''.join(s)
if top < 0:
    s = s[::-1]
print(s)

Submission status: Time Limit Exceeded

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

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

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

Submission status: Time Limit Exceeded

Solution code:
import sys

s = sys.stdin.readline().rstrip()
q = int(sys.stdin.readline().rstrip())
query = [sys.stdin.readline().rstrip().split() for i in range(q)]
top = 1
for i in range(q):
    que = query[i]
    if que[0] == '1':
        top *= -1
    else:
        if que[1] == '1':
            if top > 0:
                s = ''.join([que[2], s])
                # s = que[2] + s
            else:
                s = ''.join([s, que[2]])
                # s = s + que[2]
        else:
            if top > 0:
                s = ''.join([s, que[2]])
                # s = s + que[2]
            else:
                s = ''.join([que[2], s])
                # s = que[2] + s
if top < 0:
    s = s[::-1]
print(s)

Submission status: Time Limit Exceeded

Solution code:
import sys

s = sys.stdin.readline().rstrip()
q = int(sys.stdin.readline().rstrip())
query = [sys.stdin.readline().rstrip().split() for i in range(q)]
top = 1
for i in range(q):
    que = query[i]
    if que[0] == '1':
        top *= -1
    else:
        if que[1] == '1':
            if top > 0:
                s = que[2] + s
            else:
                s = s + que[2]
        else:
            if top > 0:
                s = s + que[2]
            else:
                s = que[2] + s
if top < 0:
    s = s[::-1]
print(s)

Submission status: Time Limit Exceeded

Solution code:
import sys

s = sys.stdin.readline().rstrip()
q = int(sys.stdin.readline().rstrip())
query = [sys.stdin.readline().rstrip().split() for i in range(q)]
for i in range(q):
    que = query[i]
    if que[0] == '1':
        s = s[::-1]
    else:
        if que[1] == '1':
            s = que[2] + s
        else:
            s = s + que[2]
print(s)

Submission status: Time Limit Exceeded

Solution code:
"""import sys
input = sys.stdin.readline
input = sys.stdin.buffer.readline

def RD(): return sys.stdin.read()
def II(): return int(input())
def MI(): return map(int,input().split())
def LI(): return list(map(int,input().split()))
def TI(): return tuple(map(int,input().split()))
"""


def main():
    S = input()
    Q = int(input())
    qi = [[0 for j in range(3)] for i in range(Q)]
    for i in range(Q):
        n = input()
        if n != 1:
            qi[i]= n.split()
        else:
            qi[i][0] = 1
  
#    for i in range(Q):
#        print(qi[i][0])

    for i in range(Q):
        #print("qi", i,  qi[i][0])
        #print("qi", i,  qi[i][0])
        if qi[i][0] == "1":
            S = S[::-1]
        elif qi[i][0] == "2":
            if qi[i][1] == "1":
                S = str(qi[i][2]) + S
            elif qi[i][1] == "2":
                S = S + str(qi[i][2]) 
    print(S)

if __name__ == "__main__":
	main()

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
s=input()
q=int(input())
cnt1=0
s_mae=[]
s_ushiro=[]
for i in range(q):
    query=list(input().split())
    if int(query[0])==1:
        cnt1+=1
    else:
        if (int(query[1])+cnt1)%2==0:
            s_ushiro.append(query[2])
        else:
            s_mae=[query[2]]+s_mae
ans="".join(s_mae)+s+"".join(s_ushiro)
if cnt1%2==0:
    print(ans)
else:
    ans2=""
    for i in range(len(ans)):
        ans2=ans2+ans[-1-i]
    print(ans2)

Submission status: Time Limit Exceeded

Solution code:
s=input()
q=int(input())
cnt1=0
for i in range(q):
    query=list(input().split())
    if int(query[0])==1:
        cnt1+=1
    else:
        if (int(query[1])+cnt1)%2==0:
            s="".join([s,query[2]])
        else:
            s="".join([query[2],s])
if cnt1%2==0:
    print(s)
else:
    s2=""
    for i in range(len(s)):
        s2="".join([s2,s[-1-i]])
    print(s2)

Submission status: Time Limit Exceeded

Solution code:
s=input()
q=int(input())
cnt1=0
for i in range(q):
    query=list(input().split())
    if int(query[0])==1:
        cnt1+=1
    else:
        if (int(query[1])+cnt1)%2==0:
            s="".join([s,query[2]])
        else:
            s="".join([query[2],s])
if cnt1%2==0:
    print(s)
else:
    s2=""
    for i in range(len(s)):
        s2="".join([s2,s[-1-i]])
    print(s2)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
s=input()
k=[]
for i in range(int(input())):
      k.append(list(map(str,input().rstrip().split())))
#[['2', '2', 'a'], ['2', '1', 'b'], ['1'], ['2', '2', 'c'], ['1'], ['1']]
for i in k:
            if len(i)!=1:
                  if int(i[1])==1:
                        s=i[2]+s
                  if int(i[1])==2:
                        s+=i[2]
            else:
                  s=s[::-1]
print(s)

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

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

inverse = 1
for i in range(q):
    query = list(map(str, input().split()))
    if query[0] == '1':
        inverse *= -1
    if query[0] == '2':
        if query[1] == '1' and inverse == 1:
            s = query[2] + s
        elif query[1] == '2' and inverse == 1:
            s = s + query[2]
        elif query[1] == '1' and inverse == -1:
            s = s + query[2]
        elif query[1] == '2' and inverse == -1:
            s = query[2] + s
if inverse == 1:
    print(s)
else:
    for i in range(len(s)):
        print(s[-i-1], end='')
    print()

Submission status: Time Limit Exceeded

Solution code:
import sys

readline = sys.stdin.readline
readlines = sys.stdin.readlines

s = readline().rstrip()
q = readline()
queries = readlines()
tot_1 = 0
for i in queries:
    if i[0] == '1':
        tot_1 += 1
        continue
    if (tot_1 + int(i[2])) % 2 == 0:
        s = s + i[4]
    else:
        s = i[4] + s
if tot_1 % 2 == 1:
    s = s[::-1]
print(s)

Submission status: Time Limit Exceeded

Solution code:
import sys

readline = sys.stdin.readline
readlines = sys.stdin.readlines

s = readline().rstrip()
q = readline()
queries = readlines()
tot_1 = 0
for i in queries:
    if i[0] == '1':
        tot_1 += 1
    else:
        if (tot_1 + int(i[2])) % 2 == 0:
            s = s + i[4]
        else:
            s = i[4] + s
if tot_1 % 2 == 1:
    s = s[::-1]
print(s)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
counter = 1
lista = [list(input().split()) for i in range(q)]

def rever(i):
    if i == '1':
        return 1
    else:
        return -1
for i in lista:
    if i[0] == '1':
        counter = -1 * counter
    else:
        if counter * rever(i[1]) > 0:
            s = i[2] + s
        else:
            s = s + i[2]
if counter > 0:
    print(s)
else:
    print(s[::-1])

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

d = deque(list(input()))
q = int(input())
flag = 1
for i in range(q):
    query = input().split()
    if query[0] == '1':
        flag = - flag
    elif query[0] == '2':
        if flag == 1:
            if query[1] == '1':
                d.appendleft(query[2])
            elif query[1] == '2':
                d.append(query[2])
        else:
            if query[1] == '1':
                d.append(query[2])
            elif query[1] == '2':
                d.appendleft(query[2])
if flag == -1:
    d.reverse()
ans = ''
l = len(d)
for i in range(l):
    ans += d[i]
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

s = list(input())
q = int(input())
d = deque(s)
flag = 1
for i in range(q):
    query = input().split()
    if query[0] == '1':
        flag = - flag
    elif query[0] == '2':
        if flag == 1:
            if query[1] == '1':
                d.appendleft(query[2])
            elif query[1] == '2':
                d.append(query[2])
        else:
            if query[1] == '1':
                d.append(query[2])
            elif query[1] == '2':
                d.appendleft(query[2])
if flag == -1:
    d.reverse()
ans = ''
l = len(d)
for i in range(l):
    ans += d[i]
print(ans)

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

Submission status: Time Limit Exceeded

Solution code:
S = input()
r = input()

for x in range(200000):
    try:
        q = input().split()
        if len(q) == 1:
            S = S[::-1]

        elif len(q) == 3:
            f = int(q[1])
            c = q[2]
            if f == 1:
                S = c + S
            elif f == 2:
                S += c
    except EOFError:
        break
print(S)

Submission status: Time Limit Exceeded

Solution code:
s_list = list(input())
q = int(input())
reverse = False

for _ in range(q):
	query= input().split()
	if int(query[0]) == 1:
		if reverse: reverse = False
		else: reverse = True
	elif int(query[0]) == 2:
		if int(query[1]) == 1:
			if reverse:
				s_list.append(query[2])
			else:
				s_list.insert(0, query[2])
		elif int(query[1]) == 2:
			if reverse:
				s_list.insert(0, query[2])
			else:	
				s_list.append(query[2])


if reverse:
	s_list.reverse()
for s in s_list:
  print(s, end ="")

Submission status: Time Limit Exceeded

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


for _ in range(q):
	query= input().split()
	if int(query[0]) == 1:
		s_list.reverse()
	elif int(query[0]) == 2:
		if int(query[1]) == 1:
			s_list.insert(0, query[2])
		elif int(query[1]) == 2:
			s_list.append(query[2])

for s in s_list:
  print(s, end ="")

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
queries = []
for _ in range(q):
    query = input().split()
    if query[0] == "1":
        queries.append(query[0])
    if query[0] == "2":
        queries.append([query[0],query[1],query[2]])
done = True
for i in queries:
    if i == "1":#もしTiが１ならば先頭と末尾の文字を交換します
        if done == True: done = False
        else: done = True
    else:
        if done:
            if i[1] == "1":#もしFiが１ならば先頭にi[2]を追加します
                s = i[2] + s
            if i[1] == "2":#もしFiが２ならば末尾にi[2]を追加します
                s = s + i[2]
        if not done:
            if i[1] == "1":
                s = s + i[2]
            if i[1] == "2":
                s = i[2] + s
if done:
    print(s)
else:
    slist = list(s)
    for i in range(len(slist)//2):
        x = slist[i]
        slist[i] = slist[len(slist)-i-1]
        slist[len(slist)-i-1] = x
    s = "".join(slist)
    print(s)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
S = input()
Q = int(input())
ans = deque(S)
for i in range(Q):
    query = input()
    if query == "1":
        ans.reverse()
    elif query[2] == "1":
        ans.appendleft(query[4])
    else:
        ans.append(query[4])

print("".join(ans))