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 resolve():
    s=list(input())
    q=int(input())
    Q=[list(map(str,input().split())) for i in range(q)]

    #print(Q)

    for i in range(q):
        if int(Q[i][0])==1:
            s.reverse()
            #print(s)
        elif int(Q[i][0])==2:
            if int(Q[i][1])==1:
                s.insert(0,Q[i][2])
                #print(s)
            else:
                s.append(Q[i][2])
                #print(s)
    print(*s,sep='')






resolve()

Submission status: Time Limit Exceeded

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

d = 1
c = 1
for i in range(Q):
    que = list(input().split())
    if que[0] == str(1):
        d += c
        c *= -1
    else:
        if (int(que[1]) ^ d) == 0:
            S.insert(0, que[2])
        else:
            S.append(que[2])

print(''.join(S) if d == 1 else ''.join(S[::-1]))

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
for _ in range(q):
    query = list(input().split())
    if int(query[0]) == 1:
        s = s[::-1]
    else:
        if int(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())
cnt=0
qq=[list(input().split()) for _ in range(q)]
for j in qq:
    if  len(j)!=1 :
        if (cnt+int(j[1]))%2==1:
            s=j[2]+s
        else:
            s=s+j[2]
    else:
        cnt+=1
if cnt%2==0:
    print(s)
else:
    print(s[::-1])

Submission status: Time Limit Exceeded

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

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

ans = list(S)
for i in range(Q):
    if query[i][0] == '1':
        ans.reverse()
        continue
    elif query[i][0] == '2':
        f = query[i][1]
        c = query[i][2].rstrip()
        if f == '1':
            ans = [c] + ans
        else:
            ans = ans + [c]
print(''.join(ans))

Submission status: Time Limit Exceeded

Solution code:
import sys
from collections import deque

S = input()
Q = int(input())
QUERY = sys.stdin.readlines()

state = 0
q1 = ''
q2 = ''
for i in range(Q):
    query = QUERY[i]
    if query[0] == '1':
        state ^= 1
    else:
        _, F, C = query.split()
        if (state + int(F)) % 2:
            q1 += C
        else:
            q2 += C

ans = ''
if state == 0:
    ans += ''.join(list(reversed(q1)))
    ans += S
    ans += q2
else:
    ans += ''.join(list(reversed(q2)))
    ans += ''.join(list(reversed(S)))
    ans += q1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
def main():
  S = input()
  Q = int(input())
  que = [list(input().split()) for _ in range(Q)]
  judge = False
  for l in que:
    if int(l[0])==1:
      if judge:
        judge = False
      else:
        judge = True

    elif int(l[0])==2:
      f,c = int(l[1]),l[2]
      if judge:
        if f==1:
          f=2
        elif f==2:
          f=1
      if f==1:
        S=c+S
      elif f==2:
        S=S+c

  if judge:
    S=S[::-1]
  print(S)
  
main()

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
for i in range(Q):
    tmp = input()
    if tmp[0] == '1':
        S = S[::-1]
    else:
        tmpt, tmpf, tmpc = tmp.split(' ')
        if tmpf == '1':
            S = tmpc + S
        else:
            S = S + tmpc
print(S)

Submission status: Time Limit Exceeded

Solution code:
s  = str(input())

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

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

s=list(input())
Q=int(input())

d=deque(s)

for i in range(Q):
    tmp=list(input())
    if tmp[0] == "1":
        d.reverse()
    else:
        if tmp[2] == "1":
            d.appendleft(tmp[4])
        else:
            d.append(tmp[4])
ans=list(d)
answer="".join(ans)
print(answer)

Submission status: Time Limit Exceeded

Solution code:
s=input()
q=int(input())
n=0
for i in range(q):
    li=input().split()
    if int(li[0])==1:
        n+=1
    elif int(li[0])==2:
        if (n%2==0 and int(li[1])==1) or (n%2==1 and int(li[1])==2):
            s=li[2]+s
        else:
            s=s+li[2]
if n%2==0:
    print(s)
else:
    st=list(s)
    st.reverse()
    print(''.join(st))

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
from sys import stdin


def main():
    input = lambda: stdin.readline()[:-1]
    S = input()
    Q = int(input())

    s = deque(S)
    for _ in [0] * Q:
        T = input().split()
        if T[0] == '1':
            s.reverse()
        else:
            f, c = T[1], T[2]
            if f == '1':
                s.appendleft(c)
            else:
                s.append(c)
    print(''.join(s))


main()

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
def try_int(s):
    try:
        return int(s)
    except:
        return s

s = input()
nq = int(input())
query = []
for i in range(nq):
    query.append([try_int(i) for i in input().split()])
    
for q in query:
    if len(q) == 1:
        s = s[::-1]
    else:
        if q[1] == 1:
            s = q[2]+s
        else:
            s = s+q[2]
print(s)

Submission status: Time Limit Exceeded

Solution code:
s = input()
t = s[::-1]
isS = True
n = int(input())
query = []

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

# print(query)

for i in range(n):
    # Reverse
    if query[i][0] == '1':
        # s = s[::-1]
        isS = not isS
    # Add
    else:
        # To Head
        if query[i][1] == '1':
            if isS:
                s = query[i][2] + s
                t += query[i][2]
            else:
                s += query[i][2]
                t = query[i][2] + t
        # To Tail
        else:
            if isS:
                s += query[i][2]
                t = query[i][2] + t
            else:
                s = query[i][2] + s
                t += query[i][2]

if isS:
    print(s)
else:
    print(t)

Submission status: Time Limit Exceeded

Solution code:
def main():
  s=input()
  q=int(input())
  for _ in [0]*q:
    a=list(input().split())
    if len(a)==1:
      s=s[::-1]
    else:
      if a[1]=='1':
        s=a[2]+s
      else:
        s=s+a[2]
  print(s)
if __name__=='__main__':
  main()

Submission status: Time Limit Exceeded

Solution code:
s=input()
q=int(input())
hanten=0
mae=[]
ato=[]
 
for i in range(q):
    query=input()
    if query=="1":
        hanten=(hanten+1)%2
    else:
        if query[2]=="1":
            if hanten==0:
                mae.append(query[4])
            else:
                ato.append(query[4])
        else:
            if hanten==0:
                ato.append(query[4])
            else:
                mae.append(query[4])
 
answer=""
if hanten==0:
    lem=len(mae)
    for i in range(lem):
        answer+=mae[-i-1]
    answer+=s
    lea=len(ato)
    for i in range(lea):
        answer+=ato[i]
else:
    lea=len(ato)
    les=len(s)
    lem=len(mae)
    for i in range(lea):
        answer+=ato[-i-1]
    for i in range(les):
        answer+=s[les-i-1]
    for i in range(lem):
        answer+=mae[i]
    
print(answer)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
s = input()
Q = int(input())
for i in range(Q):
    tmp = list(input())
    if len(tmp)==1:
        s = s[::-1]
    else:
        f,c = tmp[2], tmp[4]
        if f=='1':
            s = c + s
        elif f=='2':
            s = s + c
print(s)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
S = input()
S = [S[i] for i in range(len(S))]
S = deque(S)                         
Q = int(input())
rev = 1
for i in range(Q):
  comm = input()
  if comm == "1":
    rev *= -1
  else:
    T, F, C = comm.split()
    F = int(F) * 2 - 3
    if rev * F == 1:
      S.append(C)
    else:
      S.appendleft(C)
ans = ""
for i in range(len(S)):
  ans += S[i * rev + (rev-1) // 2]
print(ans)

Submission status: Time Limit Exceeded

Solution code:
def f(t):
    return t

s=input()
q=int(input())
queri=[]
rv=0

for i in range(q):
    tmp=list(map(f,input().split()))
    queri.append(tmp)
    if tmp[0]=="1": rv+=1
rv=rv%2

if rv==1: s=s[::-1]
ans=[s]

for i in range(q):
    if queri[i][0]=="2":
        rvt=(rv+int(queri[i][1]))%2
        if rvt==1: ans=[queri[i][2]]+ans
        else: s=ans.append(queri[i][2])

    else: rv=(rv+1)%2

for i in range(len(ans)):
    print(ans[i],end="")

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    # Failed to predict input format
    S = input()
    Q = int(input())
    Queries = []
    for i in range(Q):
        q = input().split()
        if len(q) == 1:
            Queries.append(int(q[0]))
        else:
            Queries.append([int(q[0]), int(q[1]), q[2]])
    for i in range(Q):
        if Queries[i] == 1:
            S = S[::-1]
        else:
            if Queries[i][1] == 1:
                S = Queries[i][2] + S
            elif Queries[i][1] == 2:
                S = S + Queries[i][2]
    print(S)
    pass

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
s=input()
n=int(input())
for i in range(n):
  l=input().split()
  q=int(l[0])
 
  
  if q==1:
    ll=[""]*len(s)
    c=-1
    for j in s:
      ll[c]=j
      c=c-1
    s="".join(ll)
  elif q==2:
    f=int(l[1])
    c=l[2]
    if f==1:
      s=c+s
    elif f==2:
      s=s+c
print(s)

Submission status: Time Limit Exceeded

Solution code:
#akash mandal: jalpaiguri government engineering college
#take a look on special cases: test cases

def ii(): return int(input())
def mii(): return map(str,input().split())
def lmii(): return list(mii())


def main():
    s=input()
    q=ii()
    for  i in range(q):
        a=lmii()
        if len(a)==1:
            s=s[::-1]
        else:
            if a[1]=="1":
                s=a[2]+s;
            else:
                s=s+a[2]
        a.clear()
        
    print(s)            
        
        
        
if __name__=="__main__": 
    main()

Submission status: Time Limit Exceeded

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

reverse_status = False

for i in range(q):
  query = list(map(format, input().split()))
  if int(query[0]) == 1:
    if reverse_status:
      reverse_status = False
    else:
      reverse_status = True
  else:
    if int(query[1]) == 1:
      if reverse_status:
        s = s + query[2]
      else:
        s = query[2] + s
    else:
      if reverse_status:
        s = query[2] + s
      else:
        s = s + query[2]

if reverse_status:
  print(''.join(list(reversed(s))))
else:
  print(s)

Submission status: Time Limit Exceeded

Solution code:
def odd(S):
    SS=""
    for i in range(len(S)):
        SS+=S[-i-1]
    return SS

def even(S,F,C):
    if F==1:
        C+=S
        return C
    else:
        S+=C
        return S

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

for i in range(Q):
    T=list(map(str,input().split()))
    if len(T)==1:
        temp=odd(S)
        S=temp
    else:
        temp=even(S,int(T[1]),T[2])
        S=temp
        
print(S)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
#n = int(input())
#l = list(map(int, input().split()))

'''
A=[]
B=[]
for i in range():
    a, b = map(int, input().split())
    A.append(a)
    B.append(b)'''

import sys
input = sys.stdin.readline

s = input().rstrip("\n")
q= int(input())
han=0
for i in range(q):
    que=input().split()
    if len(s)==1 and que[0]=="1":
        continue

    if que[0]=="1":
        han+=1
        #print(han, s, que)
    else:
        if han%2==0:#反転なし
            if que[1]=="1":
                s=que[2]+s
            else:
                s+=que[2]
        else:#あり
            if que[1] == "1":
                s+=que[2]
            else:
                s=que[2]+s
        #print(han, s, que)

if han%2==0:
    print(s)
else:
    print(s[::-1])

Submission status: Time Limit Exceeded

Solution code:

S = list(input())
Q = int(input())
l = []
for i in range(Q):
  l.append(list(input().split()))
    
flag = 1 #1ならふつう, -1なら逆


for i in range(Q):
    if l[i][0] == "1":
        flag *= -1
    elif flag == 1:
        if l[i][1] == "1":
            S = list(l[i][2]) + S
            
        else:
            S = S + list(l[i][2])
            
    else:
        if l[i][1] == "2":
            S = list(l[i][2]) + S
            
        else:
            S = S + list(l[i][2])
            
if flag == 1:
    print("".join(S))
else:
    S.reverse()
    print("".join(S))

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
rev = 0
for i in range(q):
    qvery = input()
    if len(qvery) == 1: #t == 1
        rev += 1
    else:
        qq, f, c = qvery.split(' ')
        f = int(f)
        if rev % 2 == 0:
            if f == 1:
                s = c + s
            else:
                s = s + c
        else:
            if f == 2:
                s = c + s
            else:
                s = s + c
if rev % 2 == 1:
    s = s[::-1]
print(s)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = lambda: sys.stdin.readline().rstrip() 

def resolve():
    S = [i for i in input()]
    Q = int(input())
    Query = [input().split() for _ in range(Q)]

    for q in Query:
        if q[0]=='1':
            S.reverse()
        elif q[0]=='2':
            if q[1]=='1':
                S.insert(0, q[2])
            elif q[1]=='2':
                S.append(q[2])

    print(*S, sep='')

if __name__ == '__main__':
    resolve()

Submission status: Time Limit Exceeded

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

h = ''
t = ''
reverse = False

for q in QL:
    if q[0] == '1':
        reverse = not reverse
    else:
        if q[1] == '1':
            if reverse:
                t = '{}{}'.format(t, q[2])
            else:
                h = '{}{}'.format(q[2], h)
        else:
            if reverse:
                h = '{}{}'.format(q[2], h)
            else:
                t = '{}{}'.format(t, q[2])

s = '{}{}{}'.format(h, S, t)
if reverse:
    print(s[::-1])
else:
    print(s)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

ans = S
r = 0  # r=0: order, r=1: reverse
for i in range(Q):
    q, *x = X[i].split()
    if q == "1":
        r = abs(r - 1)
    else:
        f, c = x
        if (r == 0 and f == "1") or (r == 1 and f == "2"):
            ans = c + ans
        else:
            ans += c
            
print(ans if r == 0 else ans[::-1])

Submission status: Time Limit Exceeded

Solution code:
s = list(input())
q = int(input())
f = False #さかさま
for i in range(q):
    x = list(input().split())
    if x[0]=='1':
        f = not f
    else:
        if f:
            if x[1]=='1': s = s+[x[2]]
            else: s = [x[2]]+s
        else:
            if x[1]=='1': s = [x[2]]+s
            else: s = s+[x[2]]
if f: s.reverse()
print(''.join(s))

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
S=str(input())
Q=int(input())
for i in range(Q):
    T=list(map(str,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:
def solve(S, Q):
    flg = 0
    for i in range(Q):
        que = input().split()
        if len(que) == 1:
            flg += 1
        else:
            if flg % 2 == 0:
                if que[1] == '1':
                    S = que[2] + S
                elif que[1] == '2':
                    S = S + que[2]
            elif flg % 2 == 1:
                if que[1] == '1':
                    S = S + que[2]
                elif que[1] == '2':
                    S = que[2] + S
    if flg % 2 == 1:
        S = S[::-1]
    print(S)

def main():
    S = input()
    Q = int(input())
    solve(S, Q)

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

def solve():
    S = list(input())
    M = int(input())
    Q = [list(input().split()) for _ in range(M)]
    
    s = deque(S)
    for q in Q:
        if q[0] == '1':
            s.reverse()
        else:
            ad = q[2]
            if q[1] == '1':
                s.appendleft(ad)
            else:
                s.append(ad)
    ans = ''.join(s)
    return ans

print(solve())

Submission status: Time Limit Exceeded

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

for j in range(q):
    i = input().split()
    if i[0] == '1':
        s = s[::-1]
    if i[0] == '2':
        if i[1] == '1':
            s = i[2] + s
        else:
            s = s + i[2]

print(s)

Submission status: Time Limit Exceeded

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

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

rev = False
for i in range(q):

    if qs[i][0] == '1':
        rev = not rev
    else:
        if qs[i][1] == '1' and rev:
            s = s + qs[i][2] 
        if qs[i][1] == '1' and not rev:
            s = qs[i][2] + s
        if qs[i][1] == '2' and rev:
            s = qs[i][2] + s
        if qs[i][1] == '2' and not rev:
            s = s + qs[i][2] 


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

Submission status: Time Limit Exceeded

Solution code:
a = input()
l = list(a)

q = int(input())

default_order = True
for i in range(q):
  x = input().split()
  if x[0] == "1":
    default_order = default_order ^ True
  elif x[0] == "2":
    insert_type = default_order ^ False
    if x[1] == "2":
      insert_type = not(insert_type)
    if insert_type:
      l.insert(0,x[2])
    else:
      l.append(x[2])
if not(default_order):
  l.reverse()

print(''.join(l))

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
is_lookforward = True
for i in range(Q):
    query = input()
    if query[0] == '1':
        is_lookforward = not is_lookforward
    else:
        F = int(query[2])
        C = query[4]
        if (F == 1 and is_lookforward) or (F == 2 and not is_lookforward):
            S = C + S
        else:
            S = S + C

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

Submission status: Time Limit Exceeded

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

    rule = 0
    for _ in range(q):
        query = input()
        if query[0] == '1':
            rule += 1
        else:
            if rule % 2 == 0:
                if query[2] == '1':
                    s = query[4] + s
                else:
                    s += query[4]
            else:
                if query[2] == '1':
                    s += query[4]
                else:
                    s = query[4] + s

    print(s if rule % 2 == 0 else s[::-1])


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

for _ in range(q):
    X = input().split()
    if X[0] == '1':
        s.reverse()
    else:
        if X[1] == '1':
            s.insert(0, X[2])
        else:
            s.append(X[2])

print(''.join(map(str, s)))

Submission status: Time Limit Exceeded

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

flag=0

for i in range(Q):
	x=input().split()
	if x[0]=="1": flag+=1
	else:
		if x[1]=="1":
			if flag%2==0: S=[x[2]]+S
			else: S.append(x[2])
		else:
			if flag%2==0: S.append(x[2])
			else: S=[x[2]]+S

if flag%2!=0:
	S.reverse()

print("".join(S))

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
S=input()
S_list=deque()
for k in range(len(S)):
    S_list.append(S[k])
Q=int(input())
kouho=deque()
for i in range(Q):
    kari=input().split()
    kouho.append(kari)
for j in range(len(kouho)):
    if kouho[j][0]=="1":
        S_list=list(S_list)
        S_list.reverse()
        S_list=deque(S_list)
    else:
        if kouho[j][1]=="1":
            S_list.appendleft(kouho[j][2])
        else:
            S_list.append(kouho[j][2])
print(*S_list,sep="")

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin
S = (stdin.readline().rstrip())
Q = int(stdin.readline().rstrip())
for i in range(Q):
    data = [(x) for x in stdin.readline().rstrip().split()]
    if data[0] == "2":
        if data[1] == "1":
            S = data[2] + S
        else:
            S = S + data[2]
    else:
        S = S[::-1]
print(S)

Submission status: Time Limit Exceeded

Solution code:
import math
import sys

m=10**9 + 7 
sys.setrecursionlimit(1000010)
S = input()
Q = int(input())
inv = 0
head=''
tail=''
for _ in range(Q):
    q = input()
    if q == '1':
        inv = 1 - inv
    else:
        (T,F,C) = list(q.split())
        #print(T,F,C)
        if inv == 0:
            if F == '1':
                head += C
            else:
                tail += C
        else:
            if F == '1':
                tail +=C
            else:
                head +=C
    # print(head,S,tail)
k = ''.join(list(reversed(head))) + S + tail
if inv == 0:
    print(k)
else:
    print(''.join(list(reversed(k))))

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-

#############
# Libraries #
#############

import math
from functools import lru_cache

#############
# Constants #
#############

MOD = 10**9 +7
INF = float('inf')

#############
# Functions #
#############

kaijo_memo = []
def kaijo(n):
  if(len(kaijo_memo) > n):
    return kaijo_memo[n]
  if(len(kaijo_memo) == 0):
    kaijo_memo.append(1)
  while(len(kaijo_memo) <= n):
    kaijo_memo.append(kaijo_memo[-1] * len(kaijo_memo) % MOD)
  return kaijo_memo[n]

gyaku_kaijo_memo = []
def gyaku_kaijo(n):
  if(len(gyaku_kaijo_memo) > n):
    return gyaku_kaijo_memo[n]
  if(len(gyaku_kaijo_memo) == 0):
    gyaku_kaijo_memo.append(1)
  while(len(gyaku_kaijo_memo) <= n):
    gyaku_kaijo_memo.append(gyaku_kaijo_memo[-1] * pow(len(gyaku_kaijo_memo),MOD-2,MOD) % MOD)
  return gyaku_kaijo_memo[n]

def nCr(n,r):
  if(n == r):
    return 1
  if(n < r or r < 0):
    return 0
  ret = 1
  ret = ret * kaijo(n) % MOD
  ret = ret * gyaku_kaijo(r) % MOD
  ret = ret * gyaku_kaijo(n-r) % MOD
  return ret

def factorization(n):
    arr = []
    temp = n
    for i in range(2, int(-(-n**0.5//1))+1):
        if temp%i==0:
            cnt=0
            while temp%i==0:
                cnt+=1
                temp //= i
            arr.append([i, cnt])
    if temp!=1:
        arr.append([temp, 1])

    if arr==[]:
        arr.append([n, 1])
    return arr

#############
# Main Code #
#############

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

head = ""
tail = ""
main = ""

main = S

status = 0

def query():
  global head
  global tail
  global main
  global status
  l = list(map(str,input().split()))
  if l[0] == "1":
    status = (status+1)%2
  else:
    letter = l[2]
    if l[1] == "1":
      if status == 0:
        head = head + letter
      else:
        tail = tail + letter
    else:
      if status == 0:
        tail = tail + letter
      else:
        head = head + letter

      


while Q:
  Q -= 1
  query()

if status == 0:
  print(head[::-1]+main+tail)
else:
  print(tail[::-1]+main[::-1]+head)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

S = deque(list(input()))
Q = int(input())

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

for i in S:
    print(i, end='')
print()

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
from functools import reduce
from operator import add
import sys

input = lambda: sys.stdin.readline().rstrip()

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

rev_cnt = 0
for i in range(q):
    query = input().split()
    if query[0] == '2':
        if (query[1] == '1' and rev_cnt % 2 == 0) or ((query[1] == '2' and rev_cnt % 2 == 1)):
            s.appendleft(query[2])
        else:
            s.append(query[2])
    else:
        rev_cnt += 1

res = reduce(add, s)
if rev_cnt % 2 == 1:
    res = res[::-1]

print(res)

Submission status: Time Limit Exceeded

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

for i in range(Q):
    query = input()
    ti = list(query)[0]
    if ti == '1':
        s.reverse()
    else:
        ti, fi, ci = query.split()
        if fi == '1':
            s = list(ci) + s
        else:
            s = s + list(ci)
print(''.join(s))

Submission status: Time Limit Exceeded

Solution code:
s = list(input())
n = int(input())
for _ in range(n):
    q = list(map(str, input().split()))
    if len(q) == 1:
        s.reverse()
    if len(q) == 3:
        if q[1] == "1":
            s.insert(0, q[2])
        else:
            s.append(q[2])
print("".join(s))

Submission status: Time Limit Exceeded

Solution code:
import sys

stdin = sys.stdin

ns = lambda : stdin.readline().rstrip()
ni = lambda : int(ns())
na = lambda : list(map(int, stdin.readline().split()))
sys.setrecursionlimit(10 ** 7)

def main():
    s = ns()
    Q = ni()

    for _ in range(Q):
        T = stdin.readline().split()
        if T[0] == "1":
            s = s[::-1]
        else:
            if T[1] == "1":
                s = T[2] + s
            else:
                s += T[2]
    print(s)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

reverse = False
for i in range(Q):
    q = input()
    if q == "1":
        reverse = False if reverse else True
    else:
        t, f, c = q.split(" ")
        if f == "1":
            if reverse:
                s = "%s%s" % (s, c)
            else:
                s = "%s%s" % (c, s)
        else:
            if reverse:
                s = "%s%s" % (c, s)
            else:
                s = "%s%s" % (s, c)
if reverse:
    print(s[::-1])
else:
    print(s)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
s = input()
q = int(input())

for i in range(q):
    query = input()
    if query == "1":
        s = s[::-1]
    else:
        x, q2_1, q2_2 = query.split()
        if q2_1 == "1":
            s = q2_2+s
        else:
            s += q2_2
print(s)

Submission status: Time Limit Exceeded

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

S = input().rstrip()

st = ''
ed = ''

flag = 1

q = int(input())

for _ in range(q):
    Q = input().split()
    if len(Q) == 1:
        flag ^= 1

    else:
        if int(Q[1]) == 1:
            if flag: st += Q[2]
            else: ed += Q[2]
        else:
            if flag: ed += Q[2]
            else: st += Q[2]

if flag: print(st[::-1] + S + ed)
else: print(ed[::-1] + S[::-1] + st)