Problem:


Score : 400 points

Problem Statement

Takahashi has a string S consisting of lowercase English letters.

Starting with this string, he will produce a new one in the procedure given as follows.

The procedure consists of Q operations. In Operation i (1 \leq i \leq Q), an integer T_i is provided, which means the following:

If T_i = 1: reverse the string S.

If T_i = 2: An integer F_i and a lowercase English letter C_i are additionally provided.

If F_i = 1 : Add C_i to the beginning of the string S.

If F_i = 2 : Add C_i to the end of the string S.

Help Takahashi by finding the final string that results from the procedure.

Constraints

1 \leq |S| \leq 10^5

S consists of lowercase English letters.

1 \leq Q \leq 2 \times 10^5

T_i = 1 or 2.

F_i = 1 or 2, if provided.

C_i is a lowercase English letter, if provided.

Input

Input is given from Standard Input in the following format:

S
Q
Query_1
:
Query_Q

In the 3-rd through the (Q+2)-th lines, Query_i is one of the following:

1

which means T_i = 1, and:

2 F_i C_i

which means T_i = 2.

Output

Print the resulting string.

Sample Input 1

a
4
2 1 p
1
2 2 c
1

Sample Output 1

cpa

There will be Q = 4 operations. Initially, S is a.

Operation 1: Add p at the beginning of S. S becomes pa.

Operation 2: Reverse S. S becomes ap.

Operation 3: Add c at the end of S. S becomes apc.

Operation 4: Reverse S. S becomes cpa.

Thus, the resulting string is cpa.

Sample Input 2

a
6
2 2 a
2 1 b
1
2 2 c
1
1

Sample Output 2

aabc

There will be Q = 6 operations. Initially, S is a.

Operation 1: S becomes aa.

Operation 2: S becomes baa.

Operation 3: S becomes aab.

Operation 4: S becomes aabc.

Operation 5: S becomes cbaa.

Operation 6: S becomes aabc.

Thus, the resulting string is aabc.

Sample Input 3

y
1
2 1 x

Sample Output 3

xy



Sample input:
a
4
2 1 p
1
2 2 c
1


Sample output:
cpa

Submission status: Time Limit Exceeded

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

c = 0
R = ""
L = ""
for _ in range(Q):
    T = input().split()
    if len(T) == 1:
        c +=1
        L_d = L
        L = "".join(list(reversed(R)))
        R = "".join(list(reversed(L_d)))
    else:
        if T[1] == "1":
            L = T[2] + L
        else:
            R = R + T[2]
if c % 2 == 0:
    print(L + S + R)
else:
    print(L + "".join(list(reversed(S))) + R)

Submission status: Time Limit Exceeded

Solution code:
def main():
    S = input()
    Q = int(input())
    ir = False
    for _ in range(Q):
        Query = input().split()
        if len(Query) == 1:
            ir = not ir
        elif (Query[1] == '1' and (not ir)) or (Query[1] == '2' and ir):
            S = Query[2] + S
        else:
            S += Query[2]

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

main()

Submission status: Time Limit Exceeded

Solution code:
def inv(s):
    t = ''
    for i in range(len(s)):
        t += s[-1 * (i + 1)]
    return t

s = input()
q = int(input())
ans = s
parity = 0
for i in range(q):
    qi = input()
    if qi[0] == '1':
        parity += 1
    else:
        if qi[2] == '1' and parity % 2 == 0:
            ans = qi[4] + ans
        elif qi[2] == '2' and parity % 2 == 0:
            ans = ans + qi[4]
        if qi[2] == '1' and parity % 2 == 1:
            ans = ans + qi[4]
        elif qi[2] == '2' and parity % 2 == 1:
            ans = qi[4] + ans
if parity % 2:
    print(ans[::-1])
else:
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys

input = sys.stdin.readline
s = str(input())
q = int(input())
qmap = [["0"] * 3 for _ in range(q)]

for i in range(q):
    a = input().split()
    for j in range(len(a)):
        qmap[i][j] = a[j]

for i in range(q):
    if qmap[i][0] == "1":
        s = s[::-1]
    else:
        if qmap[i][1] == "1":
            s = str(qmap[i][2].rstrip()) + s.rstrip()
        else:
            s = s.rstrip() + str(qmap[i][2].rstrip())

print(s)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import sys

s = sys.stdin.readline().rstrip()
q = int(input())
cnt = 0
for i in range(q):
    query = input().split()
    if len(query) == 1:
        cnt += 1
    else:
        if (int(query[1]) + cnt%2)%2 == 1:
            s = "".join([query[2], s])
        else:
            s = "".join([s, query[2]])

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

Submission status: Time Limit Exceeded

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

reverse = True
for _ in range(Q):
  q = list(input())
  if q[0] == "1":
    if reverse: reverse = False
    else: reverse = True
  else:
    if reverse:
      if q[2] == "1": S.insert(0, q[4])
      else: S.append(q[4])
    else:
      if q[2] == "1": S.append(q[4])
      else: S.insert(0, q[4])

print("".join(S) if reverse else "".join(S[::-1]))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

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

for i in range(q):
  temp = query[i].split()
  if int(temp[0]) == 1:
    s.reverse()
  else:
    if int(temp[1]) == 1:
      s.appendleft(temp[2])
    else:
      s.append(temp[2])

print(''.join(s))

Submission status: Time Limit Exceeded

Solution code:
s=input()
count_1=0
num =int(input())


for x in range(num):
    i=input().rstrip().split()
    if i[0]=="1":
        count_1+=1
        
    elif i[1]=="1" and count_1%2==0:
        s=i[2]+s
   
    elif i[1]=="1" and count_1%2==1:
        s=s+i[2]

    elif i[1]=="2" and count_1%2==0:
        s=s+i[2]

    elif i[1]=="2" and count_1%2==1:
        s=i[2]+s

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

Submission status: Time Limit Exceeded

Solution code:
s = list(input())
n = int(input())
import numpy as np
import collections
s = np.array(s)
s = collections.deque(s)
L = []
for i in range(n):
    L = (list(map(str,input())))
    #print(L)
    if len(L) >1:
        if int(L[2]) == 1:
            #print(s)
            s = np.append(L[4],s)
        elif int(L[2]) == 2:
            s = np.append(s,L[4])
    
    elif len(L) ==1:
        s = np.flipud(s)
        
print("".join(s))

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
def main():
  s = deque(list(input()))
  q = int(input())
  sr = s.reverse
  sa = s.append
  sal = s.appendleft

  cnt = 0
  for _ in range(q):
    t = input()
    if t[0] == "1":
      cnt += 1
    else:
      if cnt%2 == 1:
        sr()
        cnt = 0
      _,f,c = t.split(" ")
      if f == "1":
        sal(c)
      else:
        sa(c)
  if cnt%2 == 1:
    sr()
  print("".join(list(s)))

if __name__=="__main__":
  main()

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

S_deque = deque(input())
Q = int(input())

for _ in range(Q):
  item = list(input().split())
  if item[0] == '1':
    S_deque.reverse()

  else:
    if item[1] == '1':
      S_deque.appendleft(item[2])

    else:
      S_deque.append(item[2])

S = list(S_deque)
print(''.join(S))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
S = input()
count = int(input())
 
process_list = []
 
for _ in range(0, count):
  process_list.append(input())
  
for elm in process_list:
  process = elm.split()
  if process[0] == '1':
    S = S[::-1]
  else:
    if process[1] == '1':
      S = process[2] + S
    else:
      S = S + process[2]
 
print(S)

Submission status: Time Limit Exceeded

Solution code:
import sys
from collections import deque

s = list(sys.stdin.readline().rstrip())
q = int(sys.stdin.readline())
querys = [sys.stdin.readline().split() for _ in range(q)]
s = deque(s)

for query in querys:
    if len(query) == 1:
        s.reverse()
    else:
        if query[1] == '1':
            s.appendleft(query[2])
        else:
            s.append(query[2])

print(''.join(s))

Submission status: Time Limit Exceeded

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

print("".join(S))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
s = input()
_q = int(input())
rev = 0
l, r = "", ""
for _ in range(_q):
    *q, = map(str, input().split())

    if q[0] == "1":
        rev += 1
        continue

    t = q[0]
    f = q[1]
    c = q[2]

    if rev%2 == 0:
        if f == "1":
            l = c+l
        else:
            r += c
    else:
        if f == "1":
            r += c
        else:
            l = c+l

s = l + s + r

if rev%2 == 1:
    *s, = reversed(s)

print("".join(s))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
def main():
    import sys
    input = sys.stdin.readline

    s = input()
    s = s[0:len(s)-1]
    Q = int(input())

    flg = 0 # 0:sonomama 1:gyakuten
    for _ in range(Q):
        q = input().split()
        if q[0] == "1":
            if flg == 0:
                flg = 1
            else:
                flg = 0
        elif q[0]=="2":
            index,tmp = q[1],q[2]
            if (index == "1" and flg ==0) or (index == "2" and flg ==1):
                tmp += s
                s = tmp
            elif (index == "2" and flg == 0) or (index == "1" and flg == 1):
                s += tmp

    if flg==1:
        s = s[::-1]
    print(s)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8


def reverse():
    pass

def main():
    a = input()
    n = int(input())
    #print(a, n)
    for _ in range(n):
        input_ = input().split(' ')
        if input_[0] == '2':
            if input_[1] == '1':
                a = input_[2] + a
            if input_[1] == '2':
                a = a + input_[2]
        elif input_[0] == '1':
            a = a[::-1]

        #print(a)
            



    print(a)

    return 0


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

list = [input().split() for l in range(q)]

hanten = False

for i in range(q):
    if(list[i][0]=='1'):
        hanten = not hanten
    elif((list[i][1]=='1' and not hanten) or (list[i][1]=='2' and hanten)):
        s = list[i][2] + s
    else:
        s = s + list[i][2]

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


print(s)

Submission status: Time Limit Exceeded

Solution code:

s = input()
n = int(input())

flag = 0
for i in range(n):
    Q = input()
    if int(Q[0]) == 2:
        F = int(Q[2])
        C = Q[4]
        #print(F, C)
        if (F + flag) % 2 == 1:
            s = C + s
        else:
            s = s + C
    else:
        flag = (flag + 1) % 2 

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

Submission status: Time Limit Exceeded

Solution code:
def input_int():
    return map(int, input().split())

def one_int():
    return int(input())

def one_str():
    return input()

def many_int():
    return list(map(int, input().split()))

S= one_str()

Q = one_int()

Qs = [ input() for i in range(Q)]
def ss(S, Qs):
    for temp in Qs:
#         temp = input()
        if len(temp)>=2:
            T, F, C = temp.split()
            if F=="1":
                S = C+S
            else:
                S = S+C
        else:        
            S=S[::-1]

    return S

print(ss(S,Qs))

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
ans = s
for i in range(q):
    t = input().split()
    if t[0] == "1":
        ans = ans[::-1]
    elif t[0] == "2":
        _, f, c = t
        if f == "1":
            ans = c + ans
        elif f == "2":
            ans += c
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import math

a = input()
n = int(input())

'''
rev = 0

for i in range (n):
    li = input().split()
    
    if li[0] == '1':
        rev = rev + 1
    elif li[0] == '2' and li[1] == '1':
        if rev % 2 == 0:
            a = li[2] + a
        else:
            a = a + li[2]
    else:
        if rev % 2 == 1:
            a = li[2] + a
        else:
            a = a + li[2]

if rev % 2 == 1:
    new_str = ''.join(list(reversed(a)))
    print(new_str)
else:
    print(a)'''

for i in range (n):
    li = input().split()
    
    if li[0] == '1':
        a = ''.join(list(reversed(a)))
    elif li[0] == '2' and li[1] == '1':
        a = li[2] + a
    else:
        a = a + li[2]

print(a)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
s = list(input())
q = int(input())
f = []
for i in range(q):
    ff = [i for i in input().split()]
    f.append(ff)
ans = s
cnt = 0
for i in range(q):
    if f[i][0] == '1':
        cnt += 1
    else:
        if f[i][1] == '1':
            if cnt % 2 == 0:
                ans.insert(0,f[i][2])
            else:
                ans.append(f[i][2])
        else:
            if cnt % 2 == 0:
                ans.append(f[i][2])
            else:
                ans.insert(0,f[i][2])
if cnt % 2 == 0:
    print(''.join(map(str,ans)))
else:
    ans2 = []
    for i in (reversed(ans)):
        ans2.append(i)
    print(''.join(map(str,ans2)))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
s=raw_input()
q=int(raw_input())
while(q>0):
    t=raw_input()
    if(t[0]=='1'): s=s[::-1]
    elif(t[2]=='1'):s=(t[4]+s)
    else:s=(s+t[4])
    q=q-1
print s

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
def f(s):
    if s == '1':
        return '1' , '0' , '0'
    if s != '1':
        lst = s.split()
        return lst[0], lst[1], str(lst[2] )
qu = [f(input()) for i in range(q)]

def hanten(c):
    new_c = ''.join(list(reversed(c)))
    return new_c
def f1(c, d):
    return d + c
def f2(c, d):
    return c + d

for a, b, c in qu:
    if a == '1':
        s= hanten(s)
    if a == '2':
        if b == '1':
            d = c
            s = f1(s, d)
        if b == '2':
            d = c
            s = f2(s,d)
print(s)

Submission status: Time Limit Exceeded

Solution code:
# D
s = input()
q = int(input())
for i in range(q):
    try:
        n, f, c = map(str, input().split())
    except ValueError:
        s = s[::-1]
        n = '1'
    if int(n) == 2:
        if int(f) == 1:
            s = c + s
        elif int(f) == 2:
            s = s + c
            
print(s)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

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

print(S)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
reverse_flg = False
for q in range(Q):
  r = input().split()
  if len(r) == 1:
    reverse_flg = not reverse_flg
  else:
    if int(r[1]) == 1:
      S = r[2] + S if not reverse_flg else S + r[2]
    else:
      S = S + r[2] if not reverse_flg else r[2] + S

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

print(S)

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
count = 1
while count < q+1:
    line = list(input())
    if line[0] == "2":
        if line[2] == "1":
            s = line[4] + s
        else:
            s = s + line[4]
    else:
        s = s[::-1]
    count += 1
print(s)

Submission status: Time Limit Exceeded

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

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

for i in range(Q):
    if Qu[i][0] == '1':
        #S = S[::-1]
        S.reverse()
    elif Qu[i][0] == '2':
        if Qu[i][1] == '1':
            S.insert(0,Qu[i][2])
        else:
            S.append(Qu[i][2])
            
ans = ''.join(S)
print(ans)

Submission status: Time Limit Exceeded

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

process_list = []

for _ in range(0, count):
  process_list.append(input())
  
for elm in process_list:
  process = elm.split()
  if process[0] == '1':
    S = S[::-1]
  else:
    if process[1] == '1':
      S = process[2] + S
    else:
      S = S + process[2]

print(S)

Submission status: Time Limit Exceeded

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

flg = True

def add(s , query , flg):
    if flg:
        if query[2] == '1':
            s = query[4] + s
        else:
            s = s + query[4]
    else:
        if query[2] == '1':
            s = s + query[4]
        else:
            s = query[4] + s
    return s

for i in range(q):
    query = input()
    if query[0] == '1':
        flg = not flg
    else:
        s = add(s ,query,flg)

if not flg:
    ans = s[::-1]
    print(ans)
else:
    print(s)

Submission status: Time Limit Exceeded

Solution code:
s = list(input())
Q = int(input())
q = []
f = []
c = []

for _ in range(Q):
    tmp = list(map(str,input().split()))
    if len(tmp) == 1:
        q.append(int(tmp[0]))
        f.append(0)
        c.append('')
    else:
        q.append(int(tmp[0]))
        f.append(int(tmp[1]))
        c.append(list(tmp[2]))

s2 = list(reversed(s))
for i in range(Q):
    if q[i] == 1:
        tmp = s
        s = s2
        s2 = tmp
    else:
        if f[i] == 1:
            s2.append(c[i][0])
            c[i].extend(s)
            s = c[i]
        else:
            s.append(c[i][0])
            c[i].extend(s2)
            s2 = c[i]

print("".join(s))

Submission status: Time Limit Exceeded

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

li = [input() for i in range(q)]

li2 = [0]*q
k= 0

for i in range(q):
    if len(li[i]) == 1:
        k += 1
        li2[i] = k
    else:
        li2[i] = k
        li3 = li[i].split()
        if (int(li3[1])+k) % 2 == 0:
            s = s + li3[2]
        else:
            s = li3[2] + s

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

Submission status: Time Limit Exceeded

Solution code:
a = list(input())
n = int(input())
h,b = [1],[1]
c = 0
for i in range(n):
  s = list(input().split())
  if s[0] == "1":
    c += 1
    b.reverse()
    h.reverse()
    b,h = h,b
  else:
    if s[1] == "1":
      if h[0] == 1: h[0] = s[2]
      else:  h.insert(0,s[2])
    else: 
      if b[0] == 1: b[0] = s[2]
      else:  b.append(s[2])
if c%2 == 1:
  a.reverse() 
if h[0] == 1:
  h = []
if b[0] == 1:
  b = []
ans = "".join(h+a+b)      
print(ans)

Submission status: Time Limit Exceeded

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

reverse = False
for _ in range(Q):
    query= list(input().split()) 
    if query[0] == '1':
        reverse = not reverse
    elif query[0] == '2':
        if query[1] == '1':
            if reverse:
                S = ''.join([S,query[2]])
            else:
                S = ''.join([query[2],S])
        elif query[1] == '2':
            if reverse:
                S = ''.join([query[2],S])
            else:
                S = ''.join([S,query[2]])

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

print(S)

Submission status: Time Limit Exceeded

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

S = str(input())
Q = int(input())
# print(S, Q)

S_copy = S
is_reverse = False
for i in range(Q):
    query = list(input().split())
    # print(query)
    if int(query[0]) == 1:
        # print("reverse")
        # Sを反転
        # S_copy = S_copy[::-1]
        is_reverse = not is_reverse
    else:
        if int(query[1]) == 1:
            # Sの先頭にC_iを追加
            # print("prepend C_i")
            if is_reverse:
                S_copy = S_copy + query[2]
            else:
                S_copy = query[2] + S_copy
        else:
            # Sの末尾にC_iを追加
            # print("append C_i")
            if is_reverse:
                S_copy = query[2] + S_copy
            else:
                S_copy = S_copy + query[2]

if is_reverse:
    print(S_copy[::-1])
else:
    print(S_copy)

Submission status: Time Limit Exceeded

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

for i in range(q):
    x.append(list(input().split()))
    if len(x[i]) == 1:
        s = s[::-1]
    else:
        if x[i][1] == '1':
            s = x[i][2] + s
        elif x[i][1] == '2': 
            s = s + x[i][2] 

print(s)

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
T = [input() for i in range(Q)]

count = True

for i in range(Q):
  if(int(T[i][0]) == 1):
    count = not count
  else:
    if(int(T[i][2]) == 1):
      S = T[i][4] + S if count else S + T[i][4]
    else:
      S = S + T[i][4] if count else T[i][4] + S

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

Submission status: Time Limit Exceeded

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

rev = 0

for _ in range(q):
    t = [x for x in input().split()]
    if t[0] == "1":
        rev += 1
    else:
        if t[1] == "1" and rev % 2 == 0:
            s = "".join([t[2],s])
        elif t[1] == "2" and rev % 2 != 0:
            s = "".join([t[2],s])
        else:
            s = "".join([s,t[2]])

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

Submission status: Time Limit Exceeded

Solution code:
s1 = input()
s2 = s1[::-1]
q = int(input())
x = 0
for i in range(q):
    query = input()
    if query == '1':
        x += 1
    else :
        if (x + int(query[2])) % 2 == 0:
            s1 +=  query[-1]
            s2 = query[-1] + s2
        else :
            s1 = query[-1] + s1
            s2 += query[-1]
if x % 2 == 0:
    print(s1)
else :
    print(s2)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

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

d = deque(['S'])

#d = ['S']

rev = 0

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

#ans = deque()

if rev % 2 == 1:
    s = ''.join(list(reversed(s)))

for i in range(len(d)):
    if d[i] == 'S':
        d[i] = s

print(*d,sep='')

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
S=input()
Q=int(input())
query=[list(map(str,input().split())) for _ in range(Q)]

ans=deque()
for s in S:
  ans.append(s)
for i,q in enumerate(query):
  if len(q)==1:
    tmp=''
    for a in ans:
      tmp+=a
    ans=deque(tmp[::-1])
  else:
    _,f,c=q
    if f=='1':
      ans.appendleft(c)
    else:
      ans.append(c)
print(*ans,sep='')

Submission status: Time Limit Exceeded

Solution code:
def main():
  s=list(input())
  q=int(input())
  ok=1
  for i in range(q):
    query=list(input().split())
    if query[0]=='1':
      ok=(ok+1)%2
    else:
      f,c=query[1],query[2]
      if ok:
        if f=='1':
          s=[c]+s
        else:
          s+=[c]
      else:
        if f=='1':
          s=s+[c]
        else:
          s=[c]+s
  if ok:
    print(''.join(s))
  else:
    print(''.join(s[::-1]))
if __name__=='__main__':
  main()

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

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

for i in range(q):
    if len(Q[i]) == 1:
        S.reverse()
    elif Q[i][1] == "1":
        S.appendleft(Q[i][2])
    else:
        S.append(Q[i][2])

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

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

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

for _ in range(q):
    a = list(input().split())
    if len(a) == 1:
        s.reverse()
    else:
        if a[1] == "1":
            b = deque(s)
            b.appendleft(a[-1])
            s = list(b)
        else:
            s.append(a[-1])
print("".join(s))

Submission status: Time Limit Exceeded

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

query=[""]*Q
for i in range(Q):
    query[i]=input()

#print(query)

#Q is length
flag=0
for i in query:
    
        a=i.split(" ")
        if(len(a)==1):
            #S=S[::-1]
            flag+=1
        else:
            if(flag==0):
                if(a[1]=="1"):
                    S=a[2]+S
                else:
                    S+=a[2]
            else:
                if(a[1]=="2"):
                    S=a[2]+S
                else:
                    S+=a[2]

        flag=flag%2

if(flag==1):
    S=S[::-1]
print(S)

Submission status: Time Limit Exceeded

Solution code:
s=list(input())
q=int(input())
qr = list(input().split() for _ in range(q))
for i in range(len(qr)):
    if qr[i][0] == "1":
        s.reverse()
    if qr[i][0] == "2":
        if qr[i][1] == "1":
            s = list(qr[i][2])+s
        if qr[i][1] == "2":
            s = s+list(qr[i][2])
else:
    print("".join(s))

Submission status: Time Limit Exceeded

Solution code:
s = list(input())
q = int(input())
ans = ''
flag = False
cnt = 0

for i in range(q):
    l = list(map(str,input().split()))
    t = int(l[0])
    if t == 1:
        flag = not flag
        cnt += 1
    elif t == 2:
        f = int(l[1])
        c = str(l[2])
        if f + flag == 2:
            s.append(c)
        else:
            s.insert(0,c)

if cnt % 2 != 0:
    s.reverse()

print(''.join(s))

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
query = ""
list_1 = []
list_2 = []
mod_list = []
counter = 0
for i in range(Q):
    list_1.append(input())

for i in range(Q):
    if list_1[i] == "1":
        counter += 1
        if i == Q-1:
            mod_list.append(str(counter % 2))
    else:
        if counter != 0:
            mod_list.append(str(counter%2))
        mod_list.append(list_1[i])
        counter = 0

for query in mod_list:

    if query == "1":
        S = S[::-1]
    elif query != "0":
        list_2 = query.split()
        if list_2[1] == "1":
            S = list_2[2] + S
        else:
            S = S + list_2[2]

print(S)

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
cont = 0
for _ in range(q):
    a = input().split()
    if len(a) == 1:
        cont = (cont + 1) % 2
    else:
        f, c = a[1], a[2]
        if cont == 0:
            if f == '1':
                s = c + s
            else:
                s = s + c
        else:
            if f == '1':
                s = s + c
            else:
                s = c + s
if cont == 1:
    print(s[::-1])
else:
    print(s)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
s=list(str(input()))
s=deque(s)
q=int(input())
cnt=0
for i in range(q):
    que=list(input().split())
    if len(que)==1:
        cnt+=1
    else:
        if cnt%2!=0:
            s.reverse()
        if int(que[1])==1:
            s=deque(s)
            s.appendleft(que[2])
            cnt=0
        elif int(que[1])==2:
            s=deque(s)
            s.append(que[2])
            cnt=0
if cnt%2!=0:
    s.reverse()
print(''.join(s))

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
z = 0
F = ""
R = ""
for i in range(q):
    a = tuple(input().split())
    if a[0] == "1":
        z += 1
        z %= 2
    else:
        if (int(a[1]) + z) % 2 == 0:
            R = "".join([R, a[2]])
        else:
            F = "".join([a[2], F])

ans = "".join([F, s, R])
if z % 2 == 0:
    print(ans)
else:
    print(ans[::-1])

Submission status: Time Limit Exceeded

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

for i in range(Q):
    Query = input()
    if Query[0] == "1":
        Flag = not Flag
    else:
        QueryList = Query.split()
        if Flag:
            if QueryList[1] =="1":
                S = QueryList[2]+S
            else:
                S = S+QueryList[2]
        else:
            if QueryList[1] =="2":
                S = QueryList[2]+S
            else:
                S = S+QueryList[2]

if Flag:
    print(S)
else:
    for i in range(len(S)):
        print(S[len(S)-i-1], end="")