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 = list(input())
Q = int(input())

for _ in range(Q):
    inpu_ = input()
    if len(inpu_)==1:
        S.reverse()
    else:
        a,f,c = map(str,inpu_.split())
        if f=="1":
            S.insert(0,c)
        else:
            S.append(c)

print("".join(S))

Submission status: Time Limit Exceeded

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees#, log2
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10**9 + 7
#from decimal import *
 
S = input()
Q = INT()
 
for _ in range(Q):
	Que = input().split()
	if Que[0] == "1":
		S = S[::-1]
	else:
		_, F, C = Que
		if F == "1":
			S = C + S
		else:
			S += C
 
print(S)

Submission status: Time Limit Exceeded

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

for _ in range(Q):
  TFC = input()
  if len(TFC) == 1: # T == 1
    S = S[::-1]
  else:
    T, F, C = (i for i in TFC.split())  
    if F == '1':
      S = C + S
    else:
      S = S + C
  
print(S)

Submission status: Time Limit Exceeded

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees#, log2
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10**9 + 7
#from decimal import *

S = input()
Q = INT()

for _ in range(Q):
	Que = input().split()
	if Que[0] == "1":
		S = S[::-1]
	else:
		_, F, C = Que
		if F == "1":
			S = C + S
		else:
			S += C

print(S)

Submission status: Time Limit Exceeded

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

need_reverse = False

for q in Queries:
    if q[0] == "1":
        need_reverse = not need_reverse
    else:
        if q[1] == "1":
            if not need_reverse:
                S = q[2] + S
            else:
                S += q[2]
        else:
            if not need_reverse:
                S += q[2]
            else:
                S = q[2] + S

print(S if not need_reverse else S[::-1])

Submission status: Time Limit Exceeded

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

need_reverse = False

for q in Queries:
    if q[0] == "1":
        need_reverse = not need_reverse
    else:
        if q[1] == "1":
            if not need_reverse:
                S = q[2] + S
            else:
                S = S + q[2]
        else:
            if not need_reverse:
                S = S + q[2]
            else:
                S = q[2] + S

print(S if not need_reverse else S[::-1])

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
flag = True
for _ in range(q):
    query = input().split()
    if query[0] == "1":
        flag = not flag
    else:
        if (query[1] == "1" and flag) or (query[1] == "2" and not flag):
            s = query[2] + s
        else:
            s = s + query[2]
sf = 1 if flag else -1
print(s[::sf])

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
S=input()
Q=int(input())
reverseflag = 0
for _ in range(Q):
    newS = input()
    if int(newS[0]) == 1:
        reverseflag += 1
        reverseflag %= 2
    else:
        if (newS[2] == '1' and reverseflag==0) or (newS[2] != '1' and reverseflag!=0):
            S = newS[4]+S[:]
        else:
            S = S[:]+newS[4]
if reverseflag == 1:
    S = S[::-1]
print(S)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

q = deque(input())
Q = int(input())
reverse = -1

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

if reverse == 1: q.reverse()
for i in range(len(q)):
    print(q[i],end="")
print()

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

q = deque(input())
Q = int(input())
reverse = -1

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

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

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:
num = 0
s = input()
k = int(input())
pre = ""
out = ""
for _ in range(k):
  inp = input()
  if inp[0] == "1":
    num += 1
    num %= 2
  else:
    if (inp[2] == "1" and num == 0) or (inp[2] == "2" and num == 1):
      pre = pre + inp[4]
    else:
      out = out + inp[4]
if num:
      print(out[::-1] + s[::-1] + pre)
else:
  print(pre[::-1] + s + out)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
a = 0
b = 0
for i in range(Q):
  q = list(input().split())
  if q[0] == '1':
    b += 1
    
  elif (q[1] == '1' and b%2 == 0) or (q[1] == '2' and b%2 == 1):
    S= q[2]+S
    
    
  else:
    S = S+q[2]
    
if b%2 == 1 and len(S) > 1:
  S = S[::-1]
    

print(S)

Submission status: Time Limit Exceeded

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

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

print(s)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import math as m
import sys
s=input()
q=int(sys.stdin.readline())
x=0
for _ in range(q):
    a=input()
    if len(a)==1:
        x+=1 
    else:
        a=a.split(' ')
        if x%2==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]
if x%2==0:
    print(s)            
else:
    print(s[::-1])

Submission status: Time Limit Exceeded

Solution code:
import math as m
import sys
s=input()
q=int(sys.stdin.readline())
for _ in range(q):
    a=input()
    if len(a)==1:
        s=s[::-1]
    else:
        a=a.split(' ')
        if a[1]=='1':
            s=a[2]+s
        else:
            s+=a[2]
print(s)

Submission status: Time Limit Exceeded

Solution code:
import math as m
s=input()
q=int(input())
for _ in range(q):
    a=input()
    if len(a)==1:
        s=s[::-1]
    else:
        a=a.split(' ')
        if a[1]=='1':
            s=a[2]+s
        else:
            s+=a[2]
print(s)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

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

for i in range(n):
    q = input().split()
    if q[0] == '1':
        s.reverse()
    else:
        if q[1] == '1':
            s.appendleft(q[2])
        else:
            s.append(q[2])

print(''.join(s))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

for q in Que:
    if q[0] == '1':
        S = S[::-1]
        continue
    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()
Q = int(input())
Query = list(input().split() for i in range(Q))
ans = S
sentou = 0
for i in range(Q):
    if Query[i][0] == "1":
        sentou += 1
    if Query[i][0] == "2":
        if Query[i][1] == "1":
            if sentou % 2:
                ans = ans + Query[i][2]
            else:
                ans = Query[i][2] + ans
        if Query[i][1] == "2":
            if sentou % 2:
                ans = Query[i][2] + ans
            else:
                ans = ans + Query[i][2]
if sentou % 2:
    print(ans[::-1])
else:
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
S = list(input())
Q = int(input())
spin = False
S_mae = []
for i in range(Q):
    temp = list(input().split())
    if temp[0] == "1":
        spin = not spin
    else:
        if spin:
            if temp[1] == "1":
                S.append(temp[2])
            else:
                S_mae.append(temp[2])
        else:
            if temp[1] == "1":
                S_mae.append(temp[2])
            else:
                S.append(temp[2])

res = ""
if spin:
    for i in range(len(S)):
        res += S.pop()
    res += "".join(S_mae)
else:
    for i in range(len(S_mae)):
        res += S_mae.pop()
    res += "".join(S)
print(res)

Submission status: Time Limit Exceeded

Solution code:
s=input()
q=int(input())
keep=1
for i in range(q):
  lis=list(map(str,input().split()))
  if lis[0]=="1":
    keep*=-1
  elif lis[1]=="1":
    if keep==1:
      s=lis[2]+s
    else:
      s+=lis[2]
  else:
    if keep==1:
      s+=lis[2]
    else:
      s=lis[2]+s

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

Submission status: Time Limit Exceeded

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

print(s)

Submission status: Time Limit Exceeded

Solution code:
#D
#import sys
#input = sys.stdin.readline
from collections import deque
s = deque(input())
q = int(input())
for _ in range(q):
    t = tuple(map(str,input().split()))
    if t[0] == '1':
        s.reverse()
    else:
        if t[1] == "1":
            s.appendleft(t[2])
        else:
            s.append(t[2])
            
print("".join(s))

Submission status: Time Limit Exceeded

Solution code:
#D
from collections import deque
s = deque(input())
q = int(input())
for _ in range(q):
    t = list(map(str,input().split()))
    if t[0] == '1':
        s.reverse()
    else:
        if t[1] == "1":
            s.appendleft(t[2])
        else:
            s.append(t[2])
            
print("".join(s))

Submission status: Time Limit Exceeded

Solution code:
#D
s = input()
q = int(input())
query = []
for _ 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:
from  collections import deque

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

#Que = [list(map(str, input().split())) for _ in range(Q)]

deque_S = deque(S)
re_flag = 1
for i in range(Q):
    que = str(input())

    if len(que) == 1:
        #S = S[::-1]
        re_flag *= -1

    else:
        if que[2] == '1':
            if re_flag == 1:
                #S = que[4:] + S
                deque_S.appendleft(que[4:])
            else:
                #S =  S + que[-1:3:-1]
                deque_S.append(que[-1:3:-1])
        if que[2] == '2':
            if re_flag == 1:
                #S = S + que[4:]
                deque_S.append(que[4:])
            else:
                #S = que[-1:3:-1] + S
                deque_S.appendleft(que[-1:3:-1])

ans = ''
if re_flag == 1:
    for i in range(len(deque_S)):
        ans += deque_S[i]
else:
    for i in reversed(range(len(deque_S))):
        ans += deque_S[i][::-1]
#print(deque_S)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

S = deque(input())
Q = int(input())
Query = []
for i in range(Q):
    Query.append(list(map(str, input().split())))

for i in range(Q):
    if int(Query[i][0]) == 1:
        S.reverse()
    else:
        if int(Query[i][1]) == 1:
            S.appendleft(Query[i][2])
        else:
            S.append(Query[i][2])
print("".join(S))

Submission status: Time Limit Exceeded

Solution code:
from  collections import deque

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

#Que = [list(map(str, input().split())) for _ in range(Q)]

deque_S = deque(S)
re_flag = 1
for i in range(Q):
    que = str(input())

    if len(que) == 1:
        #S = S[::-1]
        re_flag *= -1

    else:
        if que[2] == '1':
            if re_flag == 1:
                #S = que[4:] + S
                deque_S.appendleft(que[4:])
            else:
                #S =  S + que[-1:3:-1]
                deque_S.append(que[-1:3:-1])
        if que[2] == '2':
            if re_flag == 1:
                #S = S + que[4:]
                deque_S.append(que[4:])
            else:
                #S = que[-1:3:-1] + S
                deque_S.appendleft(que[-1:3:-1])

ans = ''
for i in range(len(deque_S)):
    ans += deque_S[i]

#print(deque_S)

if re_flag == -1:
    S = S[::-1]
    ans = ans[::-1]
print(ans)

Submission status: Time Limit Exceeded

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

#Que = [list(map(str, input().split())) for _ in range(Q)]


re_flag = 1
for i in range(Q):
    que = str(input())

    if len(que) == 1:
        #S = S[::-1]
        re_flag *= -1

    else:
        if que[2] == '1':
            if re_flag == 1:
                S = que[4:] + S
            else:
                S =  S + que[-1:3:-1]
        if que[2] == '2':
            if re_flag == 1:
                S = S + que[4:]
            else:
                S = que[-1:3:-1] + S

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

print(S)

Submission status: Time Limit Exceeded

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


re_flag = 0
for i in range(Q):
    que = str(input())

    if len(que) == 1:
        #S = S[::-1]
        if re_flag == 0:
            re_flag = 1
        else:
            re_flag = 0

    else:
        if que[2] == '1':
            if re_flag == 0:
                S = que[4:] + S
            else:
                S =  S + que[-1:3:-1]
        if que[2] == '2':
            if re_flag == 0:
                S = S + que[4:]
            else:
                S = que[-1:3:-1] + S

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

print(S)

Submission status: Time Limit Exceeded

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

for i in range(Q):
    que = str(input())

    if len(que) == 1:
        S = S[::-1]

    else:
        if que[2] == '1':
            S = que[4:] + S
        if que[2] == '2':
            S = S + que[4:]


print(S)

Submission status: Time Limit Exceeded

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


for i in range(Q):
    que = str(input())

    if que[0] == '1':
        S = S[::-1]

    if que[0] == '2':
        if que[2] == '1':
            S = que[4:] + S
        if que[2] == '2':
            S = S + que[4:]


print(S)

Submission status: Time Limit Exceeded

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

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

for i in range(Q):
    if Que[i][0] == '1':
        S = S[::-1]

    if Que[i][0] == '2':
        if Que[i][1] == '1':
            S = Que[i][2] + S
        if Que[i][1] == '2':
            S = S + Que[i][2]


print(S)

Submission status: Time Limit Exceeded

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

for i in range(Q):
    if int(Query[i][0]) == 1:
        S = S[::-1]
    else:
        if int(Query[i][1]) == 1:
            S = Query[i][2] + S
        else:
            S = S + Query[i][2]
print(S)

Submission status: Time Limit Exceeded

Solution code:
s = [ str( input() ) ]
q = int( input() )
query = []
rev_flag_k = 0
for _ in range( q ):
    query_i = list( input().split() )
    query.append( query_i )
    if query_i[ 0 ] == "1":
        rev_flag_k += 1

rev_flag = 0
for query_i in query:
    t = query_i[ 0 ]
    if t == "1":
        rev_flag += 1
    else:
        f = query_i[ 1 ]
        c = query_i[ 2 ]
        if f == "1" and rev_flag % 2 == 1:
            s.append( str( c ) )
        elif f == "2" and rev_flag % 2 == 0:
            s.append( str( c ) )
        else:
            s.insert( 0, str( c ) )
s = "".join( s )
if rev_flag_k % 2 == 1:
    s = s[::-1]

print( s )

Submission status: Time Limit Exceeded

Solution code:
s = str( input() )
q = int( input() )
query = []
rev_flag_k = 0
for _ in range( q ):
    query_i = list( input().split() )
    query.append( query_i )
    if query_i[ 0 ] == "1":
        rev_flag_k += 1

rev_flag = 0
for query_i in query:
    t = query_i[ 0 ]
    if t == "1":
        rev_flag += 1
    else:
        f = query_i[ 1 ]
        c = query_i[ 2 ]
        if f == "1" and rev_flag % 2 == 1:
            s = s + str( c )
        elif f == "2" and rev_flag % 2 == 0:
            s = s + str( c )
        else:
            s = str( c ) + s
if rev_flag_k % 2 == 1:
    s = s[::-1]

print( s )

Submission status: Time Limit Exceeded

Solution code:
s = str( input() )
q = int( input() )
query = []
for _ in range( q ):
    query.append( list( input().split() ) )

for query_i in query:
    t = query_i[ 0 ]
    if t == "1":
        s = s[::-1]
    else:
        f = query_i[ 1 ]
        c = query_i[ 2 ]
        if f == "1":
            s = str( c ) + s
        else:
            s = s + str( c )

print( s )

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
for _ in range(q):
    a = list(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())
Query=[list(input().split()) for i in range(Q)]
for i in Query:
  if len(i)==1:
    S=S[::-1]
  else:
    tmp=i[2]
    if i[1]=="1":
      S=tmp+S
    else:
      S=S+tmp
print(S)

Submission status: Time Limit Exceeded

Solution code:
s = list(input())
q = int(input())
ones = 0
for _ in range(q):
	inp = list(input().split())
	if inp[0] == '1':
		ones+=1	
	else:
		if ones % 2 == 1:
			s = s[::-1]
		ones = 0
		if inp[1] == '1':
			s.insert(0, inp[2])	
		else:
			s.append(inp[2])
if ones % 2 == 1:
	s = s[::-1]
print(''.join(s))

Submission status: Time Limit Exceeded

Solution code:
s = list(input())
q = int(input())
for _ in range(q):
	inp = list(input().split())
	if inp[0] == '1':
		s = s[::-1]
	else:
		if inp[1] == '1':
			s.insert(0, inp[2])	
		else:
			s.append(inp[2])
print(''.join(s))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import math

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

mae = []
usi = []
k = 0
m = 0
for i in range(q):
    a = input().split()
    if(a[0] == '1'):
        k += 1
    if(a[0] == '2'):
        if(k %2 == 0):
            if(a[1] == '1'):
                mae.insert(0,a[2])
            else:
                usi.append(a[2])
        else:
            if(a[1] == '1'):
                usi.append(a[2])
            else:
                mae.insert(0,a[2])

if(k % 2 == 1):
    mae = mae[::-1]
    usi = usi[::-1]
    s = s[::-1]
    s = ''.join(usi+s+mae)
else:
    s = ''.join(mae+s+usi)
print(s)

Submission status: Time Limit Exceeded

Solution code:
import math

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

l = ['']
k = 0
m = 0
cnt = 0
str = 0
for i in range(q):
    a = input().split()
    if(a[0] == '1'):
        k += 1
        m += 1
    if(a[0] == '2'):
        if(k %2 == 1):
            l = l[::-1]
            k = 0
            cnt = str -cnt
        else:
            k = 0
        if(a[1] == '1'):
            l.insert(0,a[2])
            str += 1
            cnt += 1
        else:
            l.append(a[2])
            str += 1
if(k%2 ==1):
    l = l[::-1]
    cnt = str -cnt
if(m%2 == 1):
    s = s[::-1]
l[cnt] = s
l = ''.join(l)
print(l)

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
r = False
for _ in range(q):
    t = input().split()
    if len(t) == 1:
        r = not r
    else:
        t[1] = int(t[1]) - 1
        if (not t[1] and not r) or (t[1] and r):
            s = t[2] + s
        else:
            s = s + t[2]
if r:
    print(s[::-1])
else:
    print(s)

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
r = False
for _ in range(q):
    t = input().split()
    if len(t) == 1:
        r = not r
    else:
        t[1] = int(t[1]) - 1
        if (not t[1] and not r) or (t[1] and r):
            s = t[2] + s
        else:
            s = s + t[2]
if r:
    print(s[::-1])
else:
    print(s)

Submission status: Time Limit Exceeded

Solution code:
import bisect,collections,copy,heapq,itertools,math,numpy,string
import sys
sys.setrecursionlimit(10**7)

def _S(): return sys.stdin.readline().rstrip()
def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def LS(): return list(sys.stdin.readline().rstrip().split())

def reverse(S):
    return S[::-1]

def main():
    S = _S()
    Q = I()
    Querys = [LS() for _ in range(Q)]
    s = S
    rev_flg = -1
    for q in Querys:
        # print(q)
        if q[0]=='1':
            rev_flg *= -1
            # s = reverse(s)
        else:
            ti,fi,ci = q
            # print(ti,fi,ci)
            # print(int(fi)*rev_flg)
            if int(fi)*rev_flg in (1,-2):
                s = s+ci
            else:
                s = ci + s
            # elif int(fi)*rev_flg in (-1,2):
            #     s = ci+s
            # else:
            #     pass

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

main()

Submission status: Time Limit Exceeded

Solution code:
import bisect,collections,copy,heapq,itertools,math,numpy,string
import sys
sys.setrecursionlimit(10**7)

def _S(): return sys.stdin.readline().rstrip()
def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def LS(): return list(sys.stdin.readline().rstrip().split())

def reverse(S):
    return S[::-1]

def main():
    S = _S()
    Q = I()
    Querys = [LS() for _ in range(Q)]
    s = S
    rev_flg = -1
    for q in Querys:
        # print(q)
        if q[0]=='1':
            rev_flg *= -1
            # s = reverse(s)
        elif q[0]=='2':
            ti,fi,ci = q
            # print(ti,fi,ci)
            # print(int(fi)*rev_flg)
            if int(fi)*rev_flg in (1,-2):
                s = s+ci
            elif int(fi)*rev_flg in (-1,2):
                s = ci+s
            else:
                pass
        else:
            pass
    print(s[::-1] if rev_flg==1 else s)

main()

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(10**7)

def _S(): return sys.stdin.readline().rstrip()
def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def LS(): return list(sys.stdin.readline().rstrip().split())

def reverse(S):
    return S[::-1]

def main():
    S = _S()
    Q = I()
    Querys = [LS() for _ in range(Q)]
    s = S
    for q in Querys:
        # print(q)
        if q[0]=='1':
            s = reverse(s)
        elif q[0]=='2':
            ti,fi,ci = q
            # print(ti,fi,ci)
            if fi=='1':
                s = ci + s
            elif fi=='2':
                s = s + ci
            else:
                pass
        else:
            pass
    print(s)

main()

Submission status: Time Limit Exceeded

Solution code:
import bisect,collections,copy,heapq,itertools,math,numpy,string
import sys
sys.setrecursionlimit(10**7)

def _S(): return sys.stdin.readline().rstrip()
def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def LS(): return list(sys.stdin.readline().rstrip().split())

def reverse(S):
    return S[::-1]

def main():
    S = _S()
    Q = I()
    Querys = [LS() for _ in range(Q)]
    s = S
    for q in Querys:
        # print(q)
        if q[0]=='1':
            s = reverse(s)
        elif q[0]=='2':
            ti,fi,ci = q
            # print(ti,fi,ci)
            if fi=='1':
                s = ci + s
            elif fi=='2':
                s = s + ci
            else:
                pass
        else:
            pass
    print(s)

main()

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
querys = [list(map(str,input().split())) for _ in range(q)]

direction = 0
mae = ""
ato = ""

for i,query in enumerate(querys):
    if query[0] == "1":
        direction = (direction+1)%2
    else:
        if (int(query[1])-1 + direction)%2 == 0:
            mae += query[2]
        else:
            ato += query[2]


s = mae[::-1] + s
s = s + ato

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

Submission status: Time Limit Exceeded

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