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:
#from statistics import median
#import collections
#aa = collections.Counter(a) # list to list || .most_common(2)で最大の2個とりだせるお a[0][0]
from fractions import gcd
from itertools import combinations,permutations,accumulate, product # (string,3) 3回
#from collections import deque
from collections import deque,defaultdict,Counter
import decimal
import re
import math
import bisect
#
#
#
# pythonで無理なときは、pypyでやると正解するかも！！
#
#
# my_round_int = lambda x:np.round((x*2 + 1)//2)
# 四捨五入g
#
# インデックス系
# int min_y = max(0, i - 2), max_y = min(h - 1, i + 2);
# int min_x = max(0, j - 2), max_x = min(w - 1, j + 2);
#
#
import sys
sys.setrecursionlimit(10000000)
mod = 10**9 + 7
#mod = 9982443453
#mod = 998244353
from sys import stdin
readline = stdin.readline
def readInts():
  return list(map(int,readline().split()))
def readTuples():
    return tuple(map(int,readline().split()))
def I():
  return int(readline())
s = input()
q = I()
straight = True
for i in range(q):
    A = input().split()
    if A[0] == '1':
        straight = not straight
    else:
        if straight:
            if A[1] == '1':
                s = A[2] + s
            else:
                s = s + A[2]
        else:
            if A[1] == '1':
                s = s + A[2]
            else:
                s = A[2] + s
if straight:
    print(''.join(s))
else:
    print(''.join(s[::-1]))

Submission status: Time Limit Exceeded

Solution code:
import bisect, collections, copy, heapq, itertools, math, string
import sys
def I(): return int(sys.stdin.readline().rstrip())
def MI(): return map(int, sys.stdin.readline().rstrip().split())
def LI(): return list(map(int, sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())

from collections import deque
def main():
    S_ = S()
    S_que = deque(S_)
    Q = I()

    for i in range(Q):
        q = LS()
        if int(q[0]) == 1:
            S_que.reverse()
        elif int(q[0]) == 2:
            if int(q[1]) == 1:
                S_que.appendleft(q[2])
            else:
                S_que.append(q[2])
    print(''.join(map(str, S_que)))

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

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

input = sys.stdin.readline


def ST():
    return input().rstrip()


def I():
    return int(input())


def MI():
    return map(int, input().split())


def LI():
    return list(MI())


S = ST()
Q = I()

for _ in range(Q):
    query = list(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:
import bisect,collections,copy,itertools,math,string
import sys
def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())
def main():
    s = S()
    q = I()
    lst = collections.deque(list(s))
    state = False

    for _ in range(q):
        query = LS()
        if len(query) == 1:
            state = not state
        else:
            if state:
                if query[1] == "1":
                    lst.append(query[2])
                else:
                    lst.appendleft(query[2])
            else:
                if query[1] == "1":
                    lst.appendleft(query[2])
                else:
                    lst.append(query[2])
    ans = ""
    if state:
        for _ in range(len(lst)):
            ans += lst.pop()
    else:
        ans = "".join(lst)

    print(ans)
main()

Submission status: Time Limit Exceeded

Solution code:
s=input()
q=int(input())
quary=[]
for i in range(q):
    temp=list(input().split())
    quary.append(temp)

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

Submission status: Time Limit Exceeded

Solution code:
S=input()
Q=int(input())
Q=[input().split() for i in range(Q)]
 
for i in Q:
    if i[0]=='1':
        S=S[::-1]
    else:
        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())
Q=[input().split() for i in range(Q)]

for i in Q:
    if i[0]=='1':
        S=S[::-1]
    else:
        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())
q = []
for i in range(Q):
    a = input().split(" ")
    if len(a) == 1:
        q.append([int(a[0])])
    else:
        q.append([int(a[0]), int(a[1]), a[2]])

#print(S)
#print(q)


direction = 1 #通常の向き　-1:反対方向

for t in range(len(q)):
    T = q[t]
    if T[0] == 1:
        direction *= -1
    else:
        F = T[1]
        C = T[2]
        if F == 1:
            if direction == 1:
                S = C + S
            else:
                S = S + C
        else:
            if direction == 1:
                S = S + C
            else:
                S = C + S

if direction == 1:
    print(S)
else:
    SS = ""
    for i in range(len(S)):
        SS += S[len(S) - i - 1]
    print(SS)

Submission status: Time Limit Exceeded

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

Ans = list(S)

for i in range(Q):
    query = list(map(str, input().split()))
    if len(query) == 1:
        Ans.reverse()
    else:
        if query[1]=="1":
            Ans.insert(0,query[2])
        else:
            Ans.append(query[2])
print("".join(Ans))

Submission status: Time Limit Exceeded

Solution code:
import sys
from collections import deque


inint = lambda: int(sys.stdin.readline())
inintm = lambda: map(int, sys.stdin.readline().split())
inintl = lambda: list(inintm())
instrm = lambda: map(str, sys.stdin.readline().split())
instrl = lambda: list(instrm())

s = input()
q = inint()

S = deque()

for i in range(len(s)):
    S.append(s[i])

for i in range(q):
    Q = instrl()
    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:
import sys
input = sys.stdin.readline

def main():
    S = input().replace('\n', '')
    Q = int(input())

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


main()

Submission status: Time Limit Exceeded

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

c1 = 0
for i in range(Q):
    q = [x for x in input().split()]
    T = int(q[0])
    if T == 1:
        c1 += 1
    else: # T == 2
        F, C = int(q[1]), q[2]
        if (F + c1) % 2 == 1:
            S = C + S
        else: # (F + c2) % 2 == 0
            S = S + C

if c1 % 2 == 1:
    N = len(S)
    S = S[N - 1::-1]

print(S)

Submission status: Time Limit Exceeded

Solution code:
S = input()
T = ""
Q = int(input())
d = 0 # d=0:right, d=1:left
for i in range(Q):
  I = list(input().split())
  if I[0] == "1":
    d ^= 1
  else:
    if d + int(I[1]) == 2:
      S += I[2]
    else:
      T += I[2]
if d == 0:
  for i in range(len(T)-1, -1, -1):
    print(T[i], end="")
  for s in S:
    print(s, end="")
else:
  for i in range(len(S)-1, -1, -1):
    print(S[i], end="")
  for t in T:
    print(t, end="")
print()

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
d = 0 # d=0:right, d=1:left
for i in range(Q):
  I = list(input().split())
  if I[0] == "1":
    d ^= 1
  else:
    if d + int(I[1]) == 2:
      S = S + I[2]
    else:
      S = I[2] + S
if d == 1:
  S = S[::-1]
print(S)

Submission status: Time Limit Exceeded

Solution code:
import sys
# sys.setrecursionlimit(100000)


def input():
    return sys.stdin.readline().strip()


def input_int():
    return int(input())


def input_int_list():
    return [int(i) for i in input().split()]


def main():
    s = input()
    q = input_int()
    inverse = False
    for _ in range(q):
        data = input().split()
        if len(data) == 1:
            inverse = not inverse
        else:
            if (data[1] == "1" and not inverse) or (data[1] == "2" and inverse):
                s = data[2] + s
            else:
                s = s + data[2]
    if inverse:
        print(s[::-1])
    else:
        print(s)
    return


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys
# sys.setrecursionlimit(100000)


def input():
    return sys.stdin.readline().strip()


def input_int():
    return int(input())


def input_int_list():
    return [int(i) for i in input().split()]


def main():
    s = input()
    q = input_int()
    for _ in range(q):
        data = input().split()
        if len(data) == 1:
            s = s[::-1]
        else:
            if data[1] == "1":
                s = data[2] + s
            else:
                s = s + data[2]
    print(s)
    return


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
S = list(input())

Q = int(input())

for i in range(Q):
  T = input().split()
  t = int(T[0])
  
  if t == 1:
    S = S[::-1]
  elif t == 2:
    f = int(T[1])
    c = T[2]
    if f == 1:
      S.insert(0,c)
    elif f == 2:
      S.append(c)

SS = ''.join(S)

print(SS)

Submission status: Time Limit Exceeded

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

for i in range(Q):
    q=list(input().split())
    if q[0]=="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()
Q = int(input())

S_list = []
S_list.extend(list(S))
step_list = []

for i in range(Q):
    step = list(input().split())
    step_list.append(step)
#print(step_list)

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

ans = "".join(S_list)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
S = input()
S  = deque(S)
Q = int(input())

for _ in range(Q):
    string = (input())
    Query = string.replace(' ', '')
    Query = list(Query)

    if Query[0] == "1":
        S = list(S)
        S = S[::-1]
        S= deque(S)
    else:
        if Query[1] == "1":
            S.appendleft(Query[2])
        else:
            S.append(Query[2])

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

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
S = input()
S  = deque(S)
Q = int(input())

for _ in range(Q):
    string = (input())
    Query = string.replace(' ', '')
    Query = list(Query)

    if Query[0] == "1":
        S = list(S)
        S = S[::-1]
        S= deque(S)
    else:
        if Query[1] == "1":
            S.appendleft(Query[2])
        else:
            S.append(Query[2])

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

Submission status: Time Limit Exceeded

Solution code:
# import itertools
# import math
# import sys
# sys.setrecursionlimit(500*500)
# import numpy as np

# N = int(input())
S = input()
# n, *a = map(int, open(0))
# N, M = map(int, input().split())
# A = list(map(int, input().split()))
# B = list(map(int, input().split()))
# tree = [[] for _ in range(N + 1)]
# B_C = [list(map(int,input().split())) for _ in range(M)]
# S = input()

# B_C = sorted(B_C, reverse=True, key=lambda x:x[1])
# all_cases = list(itertools.permutations(P))
# a = list(itertools.combinations_with_replacement(range(1, M + 1), N))
# itertools.product((0,1), repeat=n)

# A = np.array(A)
# cum_A = np.cumsum(A)
# cum_A = np.insert(cum_A, 0, 0)

# def dfs(tree, s):
#     for l in tree[s]:
#         if depth[l[0]] == -1:
#             depth[l[0]] = depth[s] + l[1]
#             dfs(tree, l[0])
# dfs(tree, 1)

Q = int(input())
query = [list(input().split()) for _ in range(Q)]
direction = 1
for l in query:
    if l[0] == "1":
        direction *= -1
    else:
        if (l[1] == "1" and direction == 1) or (l[1] == "2" and direction == -1):
            S = l[2] + S
        else:
            S += l[2]

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

Submission status: Time Limit Exceeded

Solution code:
# import itertools
# import math
# import sys
# sys.setrecursionlimit(500*500)
# import numpy as np

# N = int(input())
S = input()
# n, *a = map(int, open(0))
# N, M = map(int, input().split())
# A = list(map(int, input().split()))
# B = list(map(int, input().split()))
# tree = [[] for _ in range(N + 1)]
# B_C = [list(map(int,input().split())) for _ in range(M)]
# S = input()

# B_C = sorted(B_C, reverse=True, key=lambda x:x[1])
# all_cases = list(itertools.permutations(P))
# a = list(itertools.combinations_with_replacement(range(1, M + 1), N))
# itertools.product((0,1), repeat=n)

# A = np.array(A)
# cum_A = np.cumsum(A)
# cum_A = np.insert(cum_A, 0, 0)

# def dfs(tree, s):
#     for l in tree[s]:
#         if depth[l[0]] == -1:
#             depth[l[0]] = depth[s] + l[1]
#             dfs(tree, l[0])
# dfs(tree, 1)

Q = int(input())
direction = 1
for i in range(Q):
    l = input().split()
    if l[0] == "1":
        direction *= -1
    else:
        if (l[1] == "1" and direction == 1) or (l[1] == "2" and direction == -1):
            S = l[2] + S
        else:
            S += l[2]

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

Submission status: Time Limit Exceeded

Solution code:
# import itertools
# import math
# import sys
# sys.setrecursionlimit(500*500)
# import numpy as np

# N = int(input())
S = input()
# n, *a = map(int, open(0))
# N, M = map(int, input().split())
# A = list(map(int, input().split()))
# B = list(map(int, input().split()))
# tree = [[] for _ in range(N + 1)]
# B_C = [list(map(int,input().split())) for _ in range(M)]
# S = input()

# B_C = sorted(B_C, reverse=True, key=lambda x:x[1])
# all_cases = list(itertools.permutations(P))
# a = list(itertools.combinations_with_replacement(range(1, M + 1), N))
# itertools.product((0,1), repeat=n)

# A = np.array(A)
# cum_A = np.cumsum(A)
# cum_A = np.insert(cum_A, 0, 0)

# def dfs(tree, s):
#     for l in tree[s]:
#         if depth[l[0]] == -1:
#             depth[l[0]] = depth[s] + l[1]
#             dfs(tree, l[0])
# dfs(tree, 1)

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

Submission status: Time Limit Exceeded

Solution code:
def t_1(s):
	return	s[::-1]
    
def t_2(s,f,c,flag):
  if f == 1:
    if flag==True:
      return c+s
    elif flag== False:
      return s+c
  
  if f == 2:
    if flag == True:
      return s+c
    elif flag == False:
      return c+s
 
 
S = input()
Q = int(input())
"""flag = True 正しい、false 逆方向"""
flag = True
 
for i in range(Q):
  inputs = list(input().split())
  
  if int(inputs[0]) == 1:
    flag = not(flag)
  elif int(inputs[0]) == 2:
    S = t_2(S,int(inputs[1]),inputs[2],flag)
 
if flag:
  print(S)
elif not(flag):
  print(t_1(S))

Submission status: Time Limit Exceeded

Solution code:
def t_1(s):
	return	s[::-1]
    
def t_2(s,f,c,flag):
  if f == 1 and flag==True:
    return c+s
  elif f == 2 and flag == True:
    return s+c
  elif f == 1 and flag== False:
    return s+c
  elif f == 2 and flag == False:
    return c+s


S = input()
Q = int(input())
"""flag = True 正しい、false 逆方向"""
flag = True

for i in range(Q):
  inputs = list(input().split())
  
  if int(inputs[0]) == 1:
    flag = not(flag)
  elif int(inputs[0]) == 2:
    S = t_2(S,int(inputs[1]),inputs[2],flag)

if flag:
  print(S)
elif not(flag):
  print(t_1(S))

Submission status: Time Limit Exceeded

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

qq = [list(input().split()) for i in range(q)]
qq.append([0, 0, 0])

order = 0
cnt = 0
f = []

for i in qq:
  if i[0] != order:
    if order == 1:
      if cnt%2 == 1:
        s.reverse()
        cnt = 0
    elif order == 21:
      s = f + s
      f = []
    else:
      s = s + f
      f = []


  if i[0] == '1':
    order = 1
    cnt += 1
  else:
    if i[1] == '1':
      order = 21
      f.insert(0, i[2])
    else:
      order = 22
      f.append(i[2])

print(''.join(s))

Submission status: Time Limit Exceeded

Solution code:
def t_1(s):
	return	s[::-1]
    
def t_2(s,f,c):
  if f == 1:
    return c+s
  elif f == 2:
    return s+c


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

for i in range(Q):
  inputs = list(input().split())
  
  if int(inputs[0]) == 1:
    S = t_1(S)
  elif int(inputs[0]) == 2:
    S = t_2(S,int(inputs[1]),inputs[2])
    
print(S)

Submission status: Time Limit Exceeded

Solution code:
s = list(input())
q = int(input())
for _ in range(q):
    try:
        t,f,c = input().split()
        if f == "1":
            s.insert(0,c)
        else:
            s.append(c)
    except ValueError:
        s = s[::-1]
print(''.join(s))

Submission status: Time Limit Exceeded

Solution code:
import re
import sys
import math
import itertools
import bisect
from copy import copy
from collections import deque,Counter
from decimal import Decimal
import functools
def v(): return input()
def k(): return int(input())
def S(): return input().split()
def I(): return map(int,input().split())
def X(): return list(input())
def L(): return list(input().split())
def l(): return list(map(int,input().split()))
def lcm(a,b): return a*b//math.gcd(a,b)
sys.setrecursionlimit(10 ** 9)
mod = 10**9+7
cnt = 0
ans = 0
inf = float("inf")
al = "abcdefghijklmnopqrstuvwxyz"

s = v()
q = k()
q_list = deque(deque(L()) for i in range(q))

while q_list:
    a = q_list.popleft()
    while a:
        b = a.popleft()
        if b == "1":
            s = s[::-1]
        else:
            c = a.popleft()
            d = a.popleft()
            if c == "1":
                s = d+s
            else:
                s = s+d

print(s)

Submission status: Time Limit Exceeded

Solution code:
import re
import sys
import math
import itertools
import bisect
from copy import copy
from collections import deque,Counter
from decimal import Decimal
import functools
def v(): return input()
def k(): return int(input())
def S(): return input().split()
def I(): return map(int,input().split())
def X(): return list(input())
def L(): return list(input().split())
def l(): return list(map(int,input().split()))
def lcm(a,b): return a*b//math.gcd(a,b)
sys.setrecursionlimit(10 ** 9)
mod = 10**9+7
cnt = 0
ans = 0
inf = float("inf")
al = "abcdefghijklmnopqrstuvwxyz"

s = v()
q = k()
q_list = deque(L() for i in range(q))

while q_list:
    a = q_list.popleft()
    a = deque(a)
    while a:
        b = a.popleft()
        if b == "1":
            s = s[::-1]
        else:
            c = a.popleft()
            d = a.popleft()
            if c == "1":
                s = d+s
            else:
                s = s+d

print(s)

Submission status: Time Limit Exceeded

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

for i, q in enumerate(Query):
  if q[0]=='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:
import sys
def main():
    st = input()
    q = int(input())
    qnt_inv = 0
    for _ in range(q):
        t = sys.stdin.read(1)
        if t == '2':
            if qnt_inv % 2 == 1:
                st = st[::-1]
                qnt_inv = 0
            f, a = input().split()
            if f == '1':
                st = a + st
            else:
                st = st + a
        else:
            t = sys.stdin.read(1)
            qnt_inv += 1

    if qnt_inv > 0 and qnt_inv % 2 == 1:
        st = st[::-1]
    print(st)

main()

Submission status: Time Limit Exceeded

Solution code:
import sys
def main():
    st = input()
    q = int(input())

    for _ in range(q):
        t = sys.stdin.read(1)
        if t == '2':
            f, a = input().split()
            if f == '1':
                st = a + st
            else:
                st = st + a
        else:
            t = sys.stdin.read(1)
            st = st[::-1]

    print(st)

main()

Submission status: Time Limit Exceeded

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

    for _ in range(q):
        try:
            t, f, a = input().split()
            if f == '1':
                st = a + st
            else:
                st = st + a
        except:
            st = st[::-1]

    print(st)

main()

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
S = input()
ans = deque(S)
Q = int(input())
for i in range(Q):
    a_list = input().split()
    if a_list[0] == "1":
        ans.reverse()
    elif a_list[1] == "1":
        ans.appendleft(a_list[2])
    else:
        ans.append(a_list[2])
print(*ans, sep = "")

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
S = input().rstrip()
Q = int(input().rstrip())
a = -1
for _ in range(Q):
    stdin = input().rstrip()
    if int(stdin[0]) == 1:
        a = ~a
    else:
        T,F,C = stdin.split()
        if int(F) == 2+a:
            S = C + S
        else:
            S = S + C
if a == 0:
    S = S[::-1]
print(S)

Submission status: Time Limit Exceeded

Solution code:
S = input().rstrip()
Q = int(input().rstrip())
for _ in range(Q):
    stdin = input().rstrip()
    if int(stdin[0]) == 1:
        S = S[::-1]
    else:
        T,F,C = stdin.split()
        if int(F) == 1:
            S = C + S
        else:
            S = S + C
print(S)

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
t, f, c = [0]*q, [0]*q, [0]*q

for _ in range(q):
    x = input().split()
    if x[0] == "1":
        s = s[::-1]
    elif x[1] == "1":
        s = x[2] + s
    else:
        s += x[2]

print(s)

Submission status: Time Limit Exceeded

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

cnt=0
left=""
right=""

for i in range(q):
  qq=[x for x in input().rstrip().split()]

  if qq[0]=="2":
    t=int(qq[0])
    f=int(qq[1])
    c=qq[2]
    
    if cnt%2==1 and f==1:
      s=s+c
    elif cnt%2==1 and f==2:
      s=c+s
    elif cnt%2==0 and f==2:
      s=s+c
    elif cnt%2==0 and f==1:
      s=c+s

  else:
    cnt+=1


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

Submission status: Time Limit Exceeded

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

cnt=0

for i in range(q):
  qq=[x for x in input().rstrip().split()]
  # print(len(qq))
  if len(qq)==3:
    t=int(qq[0])
    f=int(qq[1])
    c=qq[2]
    
    if cnt%2==1 and f==1:
      s=s+c
    elif cnt%2==1 and f==2:
      s=c+s
    elif cnt%2==0 and f==2:
      s=s+c
    elif cnt%2==0 and f==1:
      s=c+s

  else:
    cnt+=1


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

Submission status: Time Limit Exceeded

Solution code:
s=input()
q=int(input())
ls=[input() for _ in range(q)]
cnt=True

for i in range(q):
  if ls[i][0]=="1":
    cnt=True if cnt==False else False
    
  else:
    if cnt==True:
      if ls[i][2]=="1":
        s=ls[i][4]+s
        
      else:
        s=s+ls[i][4]
        
    else:
      if ls[i][2]=="1":
        s=s+ls[i][4]
        
      else:
        s=ls[i][4]+s
        
print(s if cnt==True else s[::-1])

Submission status: Time Limit Exceeded

Solution code:
s=input()
q=int(input())
h=0
for _  in range(q):
  d=input()
  if '2' in d:
    a,b,c=map(str,d.split())
    if b=='1':
      if h%2==0:
        s=str(c)+s
      else:
        s=s+str(c)
        
    else:
      if h%2==0:
        s=s+str(c)
      else:
        s=str(c)+s
        
  else:
    h+=1
    
if h%2==0:
  print(s)
else:
  f=list(s)
  g=list(reversed(f))
  print(''.join(g))

Submission status: Time Limit Exceeded

Solution code:
s=input()
q=int(input())
h=0
for _  in range(q):
  d=input()
  if '2' in d:
    a,b,c=map(str,d.split())
    if b=='1':
      if h%2==0:
        s=str(c)+s
      else:
        s=s+str(c)
        
    else:
      if h%2==0:
        s=s+str(c)
      else:
        s=str(c)+s
        
  else:
    h+=1
    
if h%2==0:
  print(s)
else:
  f=list(s)
  g=list(reversed(f))
  print(''.join(g))

Submission status: Time Limit Exceeded

Solution code:
s1 = str(input())
q = int(input())
qu = [list(map(str, input().split())) for i in range(q)]
s = []
for i in s1:
    s.append(i)
m = 0
for h in range(q):
    if len(qu[h]) == 1:
        m += 1
    else:
        if (qu[h][1] == '1' and m%2 == 0) or (qu[h][1] == '2' and m%2 == 1):
            s.insert(0,qu[h][2])
        else:
            s.append(qu[h][2])
if m%2 == 1:
    s.reverse() 
w = ""
for j in s:
    w += j
print(w)

Submission status: Time Limit Exceeded

Solution code:
s1 = str(input())
q = int(input())
qu = [list(map(str, input().split())) for i in range(q)]
s = []
for i in s1:
    s.append(i)
for h in range(q):
    if len(qu[h]) == 1:
        s.reverse()
    else:
        if qu[h][1] == '1':
            s.insert(0,qu[h][2])
        else:
            s.append(qu[h][2])
w = ""
for j in s:
    w += j
print(w)

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
query = [list(map(str, input().split())) for _ in range(Q)]
front = True

for i in range(Q):
    q = query[i]
    t = int(q[0])
    if t == 1:
        if front:
            front = False
        else:
            front = True
    else:
        f = int(q[1])
        c = q[2]
        if f == 1:
            if front:
                S = c + S
            else:
                S = S + c
        else:
            if front:
                S = S + c
            else:
                S = c + S

if not front:
    S = S[::-1]

print(S)

Submission status: Time Limit Exceeded

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

for i in range(Q):
    q = list(map(str, input().split()))
    if q[0] == "1":
        if front:
            front = False
        else:
            front = True
    else:
        if q[1] == "1":
            if front:
                S = q[2] + S
            else:
                S = S + q[2]
        else:
            if front:
                S = S + q[2]
            else:
                S = q[2] + S

if not front:
    S = S[::-1]

print(S)

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
l = []
r = []
flg = False
for i in range(q):
    try:
        t,f,c = input().split()
        if flg:
            if f == '1':
                r.append(c)
            else:
                l.insert(0,c)
        else:
            if f == '1':
                l.insert(0,c)
            else:
                r.append(c)
    except ValueError:
        if flg:
            flg = False
        else:
            flg = True
if flg:
    print((''.join(r[::-1]))+(s[::-1])+(''.join(l[::-1])))
else:
    print((''.join(l))+(s)+(''.join(r)))

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
s=deque(input())
q=int(input())
for i in range(q):
  query=input()
  if len(query)==1:
    s.reverse()
  else:
    if query[2]=="1":
      s.appendleft(query[4])
    else:
      s.append(query[4])
print("".join(s))

Submission status: Time Limit Exceeded

Solution code:
s = str(input())
q = int(input())
for i in range(q):
    tmp = tuple(map(str, input().split()))
    if len(tmp) == 3:  # pattern 2
        if int(tmp[1]) == 1:
            s = tmp[2]+s
        else:
            s += tmp[2]
    else:
        s = s[::-1]

print(s)

Submission status: Time Limit Exceeded

Solution code:
s = str(input())
q = int(input())
for i in range(q):
    tmp=tuple(map(str, input().split()))
    if len(tmp)==3: #pattern 2
        if int(tmp[1])==1:
            s=tmp[2]+s
        else:
            s+=tmp[2]
    else:
        s=s[::-1]

print(s)

Submission status: Time Limit Exceeded

Solution code:
ini = lambda : int(input())
inm = lambda : map(int,input().split())
inl = lambda : list(map(int,input().split()))
gcd = lambda x,y : gcd(y,x%y) if x%y else y
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
def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)
    divisors.sort()
    return divisors

#maincode-------------------------------------------------
s = input()
q = ini()
mae = ''
bac = s
for i in range(q):
    a = input()
    if a[0] == '1':
        temp = mae
        mae = bac
        bac = temp
    else:
        if a[2] == '1':
            mae = mae + a[4]
        else:
            bac = bac + a[4]
mae = mae[::-1]
print(mae + bac)