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 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)))
"""
while len(d):
    tmp = d.popleft()
    if tmp == 'S':
        ans.append(s)
    else:
        ans.append(tmp)
"""
d = [s if i == 'S' else i for i in d]

print(*d,sep='')

Submission status: Time Limit Exceeded

Solution code:
s=input()
q=int(input())
q_list=[input().split() for _ in range(q)]
for i in range(q):
  temp=q_list[i]
  if int(temp[0])==1:
    s=s[::-1]
  elif int(temp[0])==2:
    if int(temp[1])==1:
      s=temp[2]+s
    elif int(temp[1])==2:
      s=s+temp[2]
print(s)

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
turn = 0
for i in range(Q):
  s = input().split()
  if s[0] == "1": # 反転
    turn += 1
  elif s[0] == "2": # 足す
    if (s[1] == "1" and turn % 2 == 0) or (s[1] == "2" and turn % 2 == 1):
      S = s[2] + S
    elif (s[1] == "2" and turn % 2 == 0) or (s[1] == "1" and turn % 2 == 1):
      S = S + s[2]
if turn % 2 == 0:
  print(S)
else:
  print(S[::-1])

Submission status: Time Limit Exceeded

Solution code:
s = input()
sc = s[::-1]
n = int(input())
l = [list(input().split()) for i in range(n)]

for i in range(n):
    if l[i][0] == '1':
        save = s
        s = sc
        sc = save
    elif l[i][1] == '1':
        s = l[i][2] + s
        sc += l[i][2]
    else:
        s += l[i][2]
        sc = l[i][2] + sc

print(s)

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict
d=defaultdict(list)
s=list(input())
queries=int(input())
for _ in range(queries):
	c=input()
	c=c.split()
	
	
	if len(c)==1:
		s=s[::-1]
	else:
		if int(c[1])==1:
			s.insert(0,c[2])
		elif int(c[1])==2:
			s.insert(len(s),c[2])
print(''.join(s))

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(10 ** 7) #再帰用
s = list(input())
n = int(input())
p = []
for i in range(n):
  i =(input().split())
  if i[0] == '1':
    s = s[::-1]
  elif i[0] =='2':
    if i[1] == '1':
      s.insert(0,i[2])
    else:
      s.append(i[2])
#  count +=1      
      
#print(s)      
      
s = ''.join(s)
print(s)

Submission status: Time Limit Exceeded

Solution code:
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))

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

Queries = []
for _ in range(Q):
    Queries.append(list(input().split()))
    
for Query in Queries:
    T = int(Query[0])
    if T == 1:
        S = S[::-1]
    else:
        F = int(Query[1])
        C = Query[2]
        if F == 1:
            S = C + S
        else:
            S = S + C
print(S)

Submission status: Time Limit Exceeded

Solution code:
S=input()
Q=int(input())
import sys
from collections import deque
d=deque()
for _ in S:
  d.append(_)
 
for _ in range(Q):
  s = list(sys.stdin.readline())
  if s[0]=="1":
    d.reverse()
  else:
    if s[2]=="1":
      d.appendleft(s[4])
    else:
      d.append(s[4])
      
print("".join(d))

Submission status: Time Limit Exceeded

Solution code:
s=str(input())
q=int(input())
kt=0
for k in range(q):
    List=list(map(str,input().split()))
    if(List[0]=="1"):
        kt+=1
    else:
        if(kt%2==1):
            s=s[::-1]
        kt=0
        if(List[1]=="1"):
            List[2]+=s
            s=List[2]
        else:
            s+=List[2]
    # print(s)
if(kt%2==1):
    s=s[::-1]
print(s)

Submission status: Time Limit Exceeded

Solution code:
s=str(input())
q=int(input())
a=[input()for i in range(q)]
b=0
for i in range(q):
    if a[i]=="1":
        if b==0:
            b=1
        else:
            b=0
    else:
        if b==0:
            if a[i][2]=="1":
                s=a[i][4]+s
            else:
                s=s+a[i][4]
        else:
            if a[i][2]=="1":
                s=s+a[i][4]
            else:
                s=a[i][4]+s
if b==0:
    print(s)
    exit()
s= ''.join(list(reversed(s)))
print(s)

Submission status: Time Limit Exceeded

Solution code:
import sys
def main():
  S = input()
  Q = int(input())
  r = False
  for l in sys.stdin:
    TFC = l.split()
    if TFC[0]=="1": r = not r
    elif TFC[1]=="1": S = S+TFC[2] if r else TFC[2]+S
    elif TFC[1]=="2": S = TFC[2]+S if r else S+TFC[2]
  print(S[::-1] if r else S)

if __name__ == "__main__":
  main()

Submission status: Time Limit Exceeded

Solution code:
s = input()
n = int(input())
qs = [list(input().split()) for i in range(n)]
for q in qs:
    if len(q) == 1:
        s = s[::-1]
    elif len(q) == 3:
        if int(q[1]) == 1:
            s = q[2]+s
        elif int(q[1]) == 2:
            s = s+q[2]
print(s)

Submission status: Time Limit Exceeded

Solution code:
S = str(input())
Q = int(input())
S=list(S)
rev=0
for i in range(Q):

    #T= map(str,input().split())
    X=list(map(str,input().split()))

    if X[0] == '1':
        rev+=1
    else:
        if X[1] =='1':
            if rev%2==0:
                S.insert(0,X[2])
            else:
                S.append(X[2])
        else:
            if rev%2==0:
                S.append(X[2])
            else:
                S.insert(0,X[2])

if rev%2!=0:
    S.reverse()
S = ''.join(S)
print(S)

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin
s = str(stdin.readline().rstrip())
q = int(stdin.readline().rstrip())
for i in range(q):
    query = list(map(str, stdin.readline().rstrip().split()))
    if int(query[0])==1:
        s = s[::-1]
    else:
        if int(query[1])==2:
            s += query[2]
        else:
            s = query[2] + s

print(s)

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
pre = []
suf = []
rev = False
for i in range(Q):
    q = input().split()
    if len(q) == 1:
        pre, suf = suf[::-1], pre[::-1]
        rev = not rev
    else:
        if q[1] == '1':
            pre = pre[::-1]
            pre.append(q[2])
            pre = pre[::-1]
        else:
            suf.append(q[2])
if rev:
    S = S[::-1]
print(''.join(pre) + S + ''.join(suf))

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
s = input().rstrip('\n')
q = int(input())
for _ in range(q):
  w = list(map(str, input().split()))
  if w[0]=='1':
    s = s[::-1]
  else:
    if w[1]=='1':
      s = w[2]+s
    else:
      s = s+w[2]
print(s)

Submission status: Time Limit Exceeded

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

idx = 1
while idx <= count:
  idx += 1
  process = input().split()
  if process[0] == '1':
    S = S[::-1]
  elif process[0] == '2':
    if process[1] == '1':
      S = process[2] + S
    elif process[1] == '2':
      S = S + process[2]

print(S)

Submission status: Time Limit Exceeded

Solution code:
S=input()
Q=int(input())
import sys
from collections import deque
d=deque()
for _ in S:
  d.append(_)
 
for _ in range(Q):
  s = list(sys.stdin.readline())
  if s[0]=="1":
    lis=list(d)
    lis=lis[::-1]
    d=deque(lis)
  else:
    if s[2]=="1":
      d.appendleft(s[4])
    else:
      d.append(s[4])
      
print("".join(d))

Submission status: Time Limit Exceeded

Solution code:
def main():
  s,q,*r = [list(k.split()) for k in open(0)]
  s=s[0]
  p = False
  for x in r:
    if len(x)==1:
      p = not p
      continue
    if (p==False and x[1]=='1') or (p==True and x[1]=='2'):
        s = x[2] + s
    else:
        s = s + x[2]
  print("".join(s) if p==False else "".join(s)[::-1])
  
if __name__=='__main__':
  main()

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
S=input()
Q=int(input())
counter=0
for i in range(Q):
    query=input().split()

    if "1"==query[0]:

        counter+=1
    else:
        if counter>0:
            if counter%2==1:
                S=S[::-1]
            counter=0
        if query[1]=="1":
            S=S[::-1]
            S+=query[2]
            S=S[::-1]

        else:
            S+=query[2]


if counter>0:
    if counter%2==1:
        S=S[::-1]
print(S)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python
 
def main():
    s = list(input())
    q = int(input())
    list_ = []
    for _ in range(q):
        list_.append(list(map(str, input().split())))
 
    for i in range(q):
        if list_[i][0] == '1':
            s.reverse()
        else:
            if list_[i][1] == '1':
                s[0:0] = list_[i][2]
            else:
                s.append(list_[i][2])
 
    print(''.join(s))
 
if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys

readline = sys.stdin.readline
s = list(readline().strip())
n = int(readline())
q = sys.stdin.readlines()
reverse = False
for qi in q:
    if qi[0] == '1':
        reverse = not reverse
    else:
        c = qi[4]
        if qi[2] == '2':
            if reverse:
                s = [c] + s
            else:
                s.append(c)
        else:
            if reverse:
                s.append(c)
            else:
                s = [c] + s
if reverse:
    s = reversed(s)
print(''.join(s))

Submission status: Time Limit Exceeded

Solution code:
s=input()
q=int(input())
q_list=[input().split() for _ in range(q)]
for i in range(q):
  temp=q_list[i]
  if int(temp[0])==1:
    temps=s[::-1]
    s=temps
  elif int(temp[0])==2:
    if int(temp[1])==1:
      s=temp[2]+s
    elif int(temp[1])==2:
      s=s+temp[2]
print(s)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
import sys
import math
from bisect import bisect_left
from bisect import bisect_right
import collections
import copy
import heapq
from collections import defaultdict
from heapq import heappop, heappush
import itertools
input = sys.stdin.readline

##### リストの 二分木検索 #####
# bisect_left(lists, 3)
# bisect_right(lists, 3)

##### プライオリティキュー #####
# heapq.heapify(a) #リストaのheap化
# heapq.heappush(a,x) #heap化されたリストaに要素xを追加
# heapq.heappop(a) #heap化されたリストaから最小値を削除＆その最小値を出力

# heapq.heappush(a, -x) #最大値を取り出す時は、pushする時にマイナスにして入れよう
# heapq.heappop(a) * (-1) #取り出す時は、-1を掛けて取り出すこと

##### タプルリストのソート #####
# sorted(ans) #(a, b) -> 1st : aの昇順, 2nd : bの昇順
# sorted(SP, key=lambda x:(x[0],-x[1])) #(a, b) -> 1st : aの昇順, 2nd : bの降順
# sorted(SP, key=lambda x:(-x[0],x[1])) #(a, b) -> 1st : aの降順, 2nd : bの昇順
# sorted(SP, key=lambda x:(-x[0],-x[1])) #(a, b) -> 1st : aの降順, 2nd : bの降順

# sorted(SP, key=lambda x:(x[1])) #(a, b) -> 1st : bの昇順
# sorted(SP, key=lambda x:(-x[1])) #(a, b) -> 1st : bの降順

##### 累乗 #####
# pow(x, y, z) -> x**y % z

def inputInt(): return int(input())
def inputMap(): return map(int, input().split())
def inputList(): return list(map(int, input().split()))

inf = float('inf')
mod = 1000000007

#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-

def main():
	SS = input()
	SS = SS[:-1]
	Q = inputInt()

	S = "ooogggiii"

	flg = True
	for _ in range(Q):
		inp = input()
		inp = inp[:-1]
		inpt = inp.split()

		if len(inpt) == 1:
			if flg == True:
				flg = False
			else:
				flg = True
		else:
			oder = inpt[1]
			if oder == "1":
				if flg == True:
					S = inpt[2] + S
				else:
					S = S + inpt[2]
			else:
				if flg == True:
					S = S + inpt[2]
				else:
					S = inpt[2] + S

	#print(S)
	#S = "".join(S)
	S = S.replace("ooogggiii",SS)
	if flg == True:
		print(S)
	else:
		print(S[::-1])


#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
# special thanks :
#  https://nagiss.hateblo.jp/entry/2019/07/01/185421
#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
# nCr mod m
# rがn/2に近いと非常に重くなる
def combination(n, r, mod=10**9+7):
    r = min(r, n-r)
    res = 1
    for i in range(r):
        res = res * (n - i) * modinv(i+1, mod) % mod
    return res

# mを法とするaの乗法的逆元
def modinv(a, mod=10**9+7):
    return pow(a, mod-2, mod)

def egcd(a, b):
    if a == 0:
        return b, 0, 1
    else:
        g, y, x = egcd(b % a, a)
        return g, x - (b // a) * y, y

# nHr mod m
# 問題によって、combination()を切り替えること
def H(n, r, mod=10**9+7):
	# comb = Combination(n+r-1, mod)
	# return comb(n+r-1, r)
    return combination(n+r-1, r, mod)

class Combination:
    """
    O(n)の前計算を1回行うことで，O(1)でnCr mod mを求められる
    n_max = 10**6のとき前処理は約950ms (PyPyなら約340ms, 10**7で約1800ms)
    使用例：
    comb = Combination(1000000)
    print(comb(5, 3))  # 10
    """
    def __init__(self, n_max, mod=10**9+7):
        self.mod = mod
        self.modinv = self.make_modinv_list(n_max)
        self.fac, self.facinv = self.make_factorial_list(n_max)

    def __call__(self, n, r):
        return self.fac[n] * self.facinv[r] % self.mod * self.facinv[n-r] % self.mod

    def make_factorial_list(self, n):
        # 階乗のリストと階乗のmod逆元のリストを返す O(n)
        # self.make_modinv_list()が先に実行されている必要がある
        fac = [1]
        facinv = [1]
        for i in range(1, n+1):
            fac.append(fac[i-1] * i % self.mod)
            facinv.append(facinv[i-1] * self.modinv[i] % self.mod)
        return fac, facinv

    def make_modinv_list(self, n):
        # 0からnまでのmod逆元のリストを返す O(n)
        modinv = [0] * (n+1)
        modinv[1] = 1
        for i in range(2, n+1):
            modinv[i] = self.mod - self.mod//i * modinv[self.mod%i] % self.mod
        return modinv

if __name__ == "__main__":
	main()

Submission status: Time Limit Exceeded

Solution code:

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

Ac = []
for i in range(Q):
    Ac.append(list(input().split()))

for i in range(Q):
    if Ac[i][0]=='1':
        S = ''.join(list(reversed(S)))

    else:
        if Ac[i][1]=='1':
            S= Ac[i][2]+S
        else:
            S= S+Ac[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 range(Q):
  if len(q[i]) == 1:
    S = S[::-1]
    continue
  else:
    if int(q[i][1]) == 1:
      S = q[i][2] + S
      continue
    else: 
      S += q[i][2]
      continue

print(S)

Submission status: Time Limit Exceeded

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

rev_s = 0
for i in range(q):
   tmp = list(input().split())
   if len(tmp) == 1:
      rev_s += 1
   else:
      f, c = int(tmp[1]), tmp[2]
      x = rev_s % 2
      if (x == 0 and f == 1) or (x == 1 and f == 2):
         l = [c, s]
      else:
         l = [s, c]
      s = ''.join(l)

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

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8


def reverse():
    pass

def main():
    a = input()
    n = int(input())

    order = True
    reverse_count = 0
    for _ in range(n):
        input_ = input().split(' ')
        if input_[0] == '2':
            if order:
                if input_[1] == '1':
                    a = input_[2] + a
                if input_[1] == '2':
                    a = a + input_[2]
            else:
                if input_[1] == '1':
                    a = a + input_[2]
                if input_[1] == '2':
                    a = input_[2] + a
        elif input_[0] == '1':
            order = not order
            reverse_count += 1

    if reverse_count % 2 == 1:
        a = a[::-1]

    print(a)

    return 0


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

#solve
ans = S
reverse = 0
for i in range(Q):
  Query = input().split()
  if int(Query[0]) == 1:
    reverse += 1
    #ans = ans[::-1]
  if int(Query[0]) == 2:
    if reverse > 0:
      if reverse % 2 == 1:
        ans = ans[::-1]
      reverse = 0
    if int(Query[1]) == 1:
      ans = Query[2] + ans
    if int(Query[1]) == 2:
      ans += Query[2]
if reverse % 2 == 1: ans = ans[::-1]
print(ans)

Submission status: Time Limit Exceeded

Solution code:
S=str(input())
Q=int(input())
which=0
front=[]
back=[]
for i in range(Q):
    a=list(map(str,input().split()))
    if a==["1"]:
        which+=1
        which%=2
        continue
    else:
        if which==0:
            if a[1]=="1":
                front.insert(0,a[2])
            if a[1]=="2":
                back.append(a[2])
        if which==1:
            if a[1]=="1":
                back.append(a[2])
            if a[1]=="2":
                front.insert(0,a[2])
F="".join(front)
B="".join(back)
S=F+S+B
if which==1:
    print("".join(S[len(S)-i-1] for i in range(len(S))))
if which==0:
    print(S)

Submission status: Time Limit Exceeded

Solution code:
letter = input()
num = int(input())

reve = 0

for kk in range(num):
    lis = list(input().split())
    if int(lis[0]) == 1:
        if reve == 0:
            reve = 1
        else:
            reve = 0
    else:
        if reve == 1:
            reve = 0
            letter = letter[::-1]

        if int(lis[1]) == 1:
            letter = lis[2] + letter
        else:
            letter = letter + lis[2]
            
if reve == 1:
            reve = 0
            letter = letter[::-1]

print(letter)

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)]

rev = 0
for i in range(q):
    if len(Q[i]) == 1:
        rev += 1
        rev %= 2
    elif rev == 0:
        if Q[i][1] == "1":
            s.appendleft(Q[i][2])
        else:
            s.append(Q[i][2])
    else:
        if Q[i][1] == "2":
            s.appendleft(Q[i][2])
        else:
            s.append(Q[i][2])

if rev == 0:
    for i in range(len(s)):
        print(s[i],end="")
else:
    for i in reversed(range(len(s))):
        print(s[i],end="")
print()

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])-1
    if t:
        f = int(l[1])
        c = str(l[2])
        if f + flag == 2:
            s.append(c)
        else:
            s.insert(0,c)
    else:
        flag = not flag
        cnt += 1

if cnt % 2 != 0:
    print(''.join(s[::-1]))
else:
    print(''.join(s))

Submission status: Time Limit Exceeded

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

for _ in range(Q):
    q = input()
    if q[0] == "1":
        S = S[::-1]
    else:
        if q[2] == "1":
            temp = q[4] + S
            S = temp
        else:
            S += q[4]


print(S)

Submission status: Time Limit Exceeded

Solution code:
s=input()
n=int(input())
l=[]
r=[]
cnt=0
for i in range(n):
    e=list(map(str,input().split()))
    q=int(e[0])
    if len(e)!=1:
        f=int(e[1])
        c=e[2]
    if q==2:
        if (f==1 and cnt%2==0) or (f==2 and cnt%2!=0):
            s=c+s
        else:
            s=s+c
    else:
        cnt+=1
if cnt%2==0:
    print(s)
else:
    print(s[::-1])

Submission status: Time Limit Exceeded

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




def main_wrong():
    # Failed to predict input format
    S = input()
    Q = int(input())
    Queries = []

    cumsum_Queries = [0]
    current = 0

    temae = ''
    oku = ''
    for i in range(Q):
        q = input().split()
        if len(q) == 1:
            Queries.append(int(q[0]))
            current += 1
            cumsum_Queries.append(current)
        else:
            Queries.append([int(q[0]), int(q[1]), q[2]])
            cumsum_Queries.append(current)
            if Queries[i][1] == 1:
                if cumsum_Queries[i] % 2 == 0:
                    temae = Queries[i][2] + temae
                else:
                    oku += Queries[i][2][::-1]
            else:
                if cumsum_Queries[i] % 2 == 0:
                    oku += Queries[i][2]
                else:
                    temae = Queries[i][2][::-1] + temae
    if current % 2 == 0:
        S = S
    else:
        S = S[::-1]
    print(temae + S + oku)

    pass

import itertools
def main():
    S = input()
    Q = int(input())
    Queries = []

    Queries = [input().split() for i in range(Q)]
    Queries = [int(q[0]) if len(q) == 1 else [int(q[0]), int(q[1]), q[2]] for q in Queries]

    cumsum_Queries_preprocessing = [1 if q == 1 else 0 for q in Queries]
    current = sum(cumsum_Queries_preprocessing)
    cumsum_Queries = list(itertools.accumulate(cumsum_Queries_preprocessing))
    cumsum_Queries = [current - cumsum_Queries[i] for i in range(Q)]



    if current % 2 == 0:
        S = S
    else:
        S = S[::-1]

    for i in range(Q):
        if Queries[i] == 1:
            continue
        else:
            if Queries[i][1] == 1:
                if cumsum_Queries[i] % 2 == 0:
                    S = Queries[i][2] + S
                else:
                    S += Queries[i][2][::-1]
            else:
                if cumsum_Queries[i] % 2 == 0:
                    S += Queries[i][2]
                else:
                    S = Queries[i][2][::-1] + S
    print(S)

    pass



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

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))

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

for _ in range(Q):
    Query = list(input().split())
    T = int(Query[0])
    if T == 1:
        S = S[::-1]
    else:
        F = int(Query[1])
        C = Query[2]
        if F == 1:
            S = C + S
        else:
            S = S + C
print(S)

Submission status: Time Limit Exceeded

Solution code:
def main():
    s = input()
    q = int(input())
#queries = []
    for _ in range(0, q):
        query = input()
        if len(query) == 1:
     #       queries.append([int(query)])
            s = s[::-1]
        else:
            query = query.split(' ')
            query = [int(query[0]), int(query[1]), query[2]]
#        queries.append(query)
            if query[1] == 1:
                s = query[2] + s
            else:
                s = s + query[2]



#for i in range(0, q):
#    if queries[i][0] == 1:
#        s = s[::-1]
#    else:
#        if queries[i][1] == 1:
#            s = queries[i][2] + s
#        else:
#            s = s + queries[i][2]

    print(s)

main()

Submission status: Time Limit Exceeded

Solution code:
#coding:utf-8
import sys,os
sys.setrecursionlimit(10**6)
write = sys.stdout.write
dbg = (lambda *something: print(*something)) if 'TERM_PROGRAM' in os.environ else lambda *x: 0
def main(given=sys.stdin.readline):
    input = lambda: given().rstrip()
    LMIIS = lambda: list(map(int,input().split()))
    II = lambda: int(input())
    XLMIIS = lambda x: [LMIIS() for _ in range(x)]
    YN = lambda c : print('Yes') if c else print('No')
    MOD = 10**9+7
    S = input()
    # Slist = [input()]
    Q = II()
    # Slist = ['']*Q+Slist+['']*Q
    r = False
    for _ in range(Q):
        query = input().split()
        if query[0] == '1':
            r ^= True
        else:
            if query[1] == '1':
                    if r:
                        S = ''.join([S,query[2]])
                    else:
                        S = ''.join([query[2],S])
            else:
                if r:
                    S = ''.join([query[2],S])
                else:
                    S = ''.join([S,query[2]])
    print(S[::-1] if r else S)
                



    

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

c=0

for i in range(Q):
    if Query[i][0]=='1':
        c=c+1
    else:
        if ((Query[i][1]=='1')and(c%2==0)) or ((Query[i][1]=='2')and(c%2==1)):
            S='{}{}'.format(Query[i][2],S)
        else:
            S = '{}{}'.format(S,Query[i][2])

if c%2==0:
    print(S)
else:
    print(''.join(list(reversed(S))))

Submission status: Time Limit Exceeded

Solution code:
s = input()
for i in range(int(input())):
    query = input()
    if query[0] == "1":
        s = s[::-1]
        continue

    s = query[-1] + s if query[2] == "1" else s + query[-1]

print(s)

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
hanten = False
list = []
#list = [input().split() for l in range(q)]



for i in range(q):
    list = input().strip().split()


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

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


print(s)

Submission status: Time Limit Exceeded

Solution code:
typeIn = input()
S = []
for i in typeIn:
    S.append(i)
Q = int(input())
Query = []
for i in range(Q):
    a = list(input().split())
    if a[0] == '2':
        a[0] = int(a[0])
        a[1] = int(a[1])
    else:
        a[0] = int(a[0])
    Query.append(a)
for i in Query:
    if i[0]==1:
        S.reverse()
    if i[0]==2:
        if i[1]==1:
            S.insert(0,i[2])
        if i[1]==2:
            S.append(i[2])
out = listToStr = ''.join([str(elem) for elem in S])
print(listToStr)

Submission status: Time Limit Exceeded

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

def hanten(c):
    new_c = c[::-1]
    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:
import sys

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

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

Submission status: Time Limit Exceeded

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

reverse = 0

for i in range(q):
    inp = input()
    if len(inp) <= 1:
        reverse += 1
    else:
        dmy, f, c = inp.split()
        f = int(f)
        if reverse & 1:
            if f == 1:
                s = s + c
            else:
                s = c + s
        else:
            if f == 1:
                s = c + s
            else:
                s = s + c

if reverse & 1:
    print(s[::-1])
else:
    print(s)

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

S = input()
Q = int(input())
query = ""
list_1 = deque()
flag = 0
counter = 0
for i in range(Q):
    query = input()
    if query == "1":
        counter += 1
        flag = (flag+1)%2
    elif flag == 0:
        list_1 = query.split()
        if list_1[1] == "1":
            S = list_1[2] + S
        else:
            S = S + list_1[2]
    else:
        list_1 = query.split()
        if list_1[1] == "1":
            S = S+list_1[2]
        else:
            S = list_1[2]+S

if counter % 2 == 0:
    print(S)
else:
    print(S[::-1])

Submission status: Time Limit Exceeded

Solution code:
s = str(input())
loop = int(input())

rev = 0
for i in range(loop):
    queries = input().split()
    if len(queries) == 1:
        rev = (rev+1) % 2
    else:
        type = int(queries[1])
        c = str(queries[2])
        if (type + rev) % 2 == 1:
            s = c + s
        else:
            s = s + c
if rev == 1:
    s = s[::-1]
print(s)