Problem:


Score : 400 points

Problem Statement

Takahashi has a string S consisting of lowercase English letters.

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

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

If T_i = 1: reverse the string S.

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

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

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

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

Constraints

1 \leq |S| \leq 10^5

S consists of lowercase English letters.

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

T_i = 1 or 2.

F_i = 1 or 2, if provided.

C_i is a lowercase English letter, if provided.

Input

Input is given from Standard Input in the following format:

S
Q
Query_1
:
Query_Q

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

1

which means T_i = 1, and:

2 F_i C_i

which means T_i = 2.

Output

Print the resulting string.

Sample Input 1

a
4
2 1 p
1
2 2 c
1

Sample Output 1

cpa

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

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

Operation 2: Reverse S. S becomes ap.

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

Operation 4: Reverse S. S becomes cpa.

Thus, the resulting string is cpa.

Sample Input 2

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

Sample Output 2

aabc

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

Operation 1: S becomes aa.

Operation 2: S becomes baa.

Operation 3: S becomes aab.

Operation 4: S becomes aabc.

Operation 5: S becomes cbaa.

Operation 6: S becomes aabc.

Thus, the resulting string is aabc.

Sample Input 3

y
1
2 1 x

Sample Output 3

xy



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


Sample output:
cpa

Submission status: Time Limit Exceeded

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

for _ in range(q):
    ar = list(map(str,input().split()))
    if len(ar) >1:
        if ar[1] == '1':
            s.insert(0,ar[2])
        else:
            s.append(ar[2])
    else:
        s=s[::-1]

print(''.join(s))

Submission status: Time Limit Exceeded

Solution code:
s=input()
q=int(input())
hanten=0
mae=""
ato=""

for i in range(q):
    query=input()
    if query=="1":
        hanten=(hanten+1)%2
    else:
        if query[2]=="1":
            if hanten==0:
                mae+=query[4]
            else:
                ato+=query[4]
        else:
            if hanten==0:
                ato+=query[4]
            else:
                mae+=query[4]

answer=""
if hanten==0:
    lem=len(mae)
    for i in range(lem):
        answer+=mae[-i-1]
    answer+=s
    answer+=ato
else:
    lem=len(ato)
    les=len(s)
    for i in range(lem):
        answer+=ato[-i-1]
    for i in range(les):
        answer+=s[les-i-1]
    answer+=mae
    
print(answer)

Submission status: Time Limit Exceeded

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

for i in range(n):
    q = list(map(str,input().split()))
    if len(q) == 1 and q[0] == "1":
        s = s[::-1]
    elif q[1] == "1":
        s = q[2] + s
    elif q[1] == "2":
        s = s + q[2]


print(s)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

S = input().strip()
Q = int(input())

reverse = False

for _ in range(Q):
    s = input().strip()
    if s.startswith("1"):
        reverse = not reverse
    else:
        _, f, c = s.split()
        if f == "1":
            if reverse:
                S = S + c
            else:
                S = c + S
        else:
            if reverse:
                S = c + S
            else:
                S = S + c

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

Submission status: Time Limit Exceeded

Solution code:
s=str(input())
q=int(input())
for i in range(q):
    que=list(input())
    if len(que)==1:
        s=s[::-1]
    else:
        if int(que[2])==1:
            s=que[4]+s
        elif int(que[2])==2:
            s=s+que[4]
print(s)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

for i in range(q):
    s1=input()
    if len(s1)==1:
        hanten+=1
    else:
        a,f,c=s1.split()
        f=int(f)
        if f==1:
            if hanten%2==0:
                s = c + s
            else:
                s = s + c
        else:
            if hanten%2==0:
                s = s + c
            else:
                s = c + s
        

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

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

S = deque(input())
Q = int(input())
for _ in range(Q):
    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 = list(input())
Q = int(input())
for i in range(Q):
    query = input().split()
    if int(query[0]) == 1:
        S = S[::-1]
    elif int(query[0]) == 2:
        if int(query[1]) == 1:
            S.insert(0,query[2])
        elif int(query[1]) == 2:
            S.append(query[2])
print(*S ,sep='')

Submission status: Time Limit Exceeded

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

rev=0

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

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

Submission status: Time Limit Exceeded

Solution code:
s=str(input())
q=int(input())
now = 0
mae,usiro="",""
for _ in range(q):
  lis = list(map(str,input().split()))
  if int(lis[0]) == 1:
    now = (now+1)%2
  else:
    if (int(lis[1])+now)%2 == 1:
      mae = lis[2]+mae
    else:
      usiro= usiro +lis[2]
s = mae+s+usiro
if now ==0:
  print(s)
else:
  print(s[::-1])

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
ans = S
turncnt = 0
for i in range(Q):
    query = input().split()
    if (query[0] == "1"):
        turncnt = turncnt + 1
    if (query[0] == "2"):
        if (query[1] == "1"):
            if (turncnt % 2 == 0):
                ans = query[2] + ans
            else:
                ans = ans + query[2]
        if (query[1] == "2"):
            if (turncnt % 2 == 0):
                ans = ans + query[2]
            else:
                ans = query[2] + ans

if (turncnt % 2 == 1):
    ans = ans[::-1]    
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
s=list(input())
s=deque(s)
q=int(input())
a=0
for i in range(q):
  A=list(input().split())
  if A[0]=="1":
    a=(a+1)%2
  else:
    if A[1]=="1":
      if a==0:
        s.appendleft(A[2])
      else:
        s.append(A[2])
    else:
      if a==0:
        s.append(A[2])
      else:
        s.appendleft(A[2])
if a==1:
  S=[]
  c=len(s)
  for i in range(c):
    S.append(s[c-i-1])
else:
  S=s
for i in range(len(S)):
  print(S[i],end="")
print()

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

S = input()
Q = int(input())
W = 0
ans = S

for _ in range(Q):
#    print(W,ans)
    qry = list(input().split())
    if qry[0] == "1":
        W = 1-W
        continue
    T,F,C = qry
    T = int(T)
    F = int(F)
    if (W == 0 and F==2) or (W==1 and F==1):
        ans = ans+C
    else:
        ans = C+ans
if W == 1:
    ans = ans[::-1]
print(ans)

Submission status: Time Limit Exceeded

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


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

from collections import deque

S = list(S)
S = deque(S)

for i in range(Q):
    if Que[i][0] == '1':
        S.reverse()
    elif Que[i][0] == '2':
        if Que[i][1] == '1':
            S.appendleft(Que[i][2])
        elif Que[i][1] == '2':
            S.append(Que[i][2])
            
print("".join(S))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
rev = 1
for _ in range(Q):
    lst = input().split() 
    if (lst[0] == '1'):
        rev *= -1
    else:
        if (lst[1] == '1'):
            pos = rev
        else:
            pos = -1 * rev
        if (pos == 1):
            S = lst[2] + S
        else:
            S = S + lst[2]
print(S[::rev])

Submission status: Time Limit Exceeded

Solution code:
reverseFlag = False

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

for i in range(Q):
    TFC = input().split(' ')
    if TFC[0] == '1':
        if reverseFlag == False:
            reverseFlag = True
        else:
            reverseFlag = False
    else:
        if TFC[1] == '1':
            if reverseFlag == False:
                S = TFC[2] + S
            else:
                S = S + TFC[2]
        else:
            if reverseFlag == True:
                S = TFC[2] + S
            else:
                S = S + TFC[2]
if reverseFlag == True:
    print(S[::-1])
else:
    print(S)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:
import math
import fractions
import bisect
import collections
import itertools
import heapq
import string
import sys
import copy
from collections import deque
sys.setrecursionlimit(10**7)
MOD = 10**9+7
def gcd(a,b):return fractions.gcd(a,b) #最大公約数
def lcm(a,b):return (a*b) // fractions.gcd(a,b) #最小公倍数
def iin(): return int(input()) #整数読み込み
def ifn(): return float(input()) #浮動小数点読み込み
def isn(): return input().split() #文字列読み込み
def imn(): return map(int, input().split()) #整数map取得
def fmn(): return map(float, input().split()) #浮動小数点map取得
def iln(): return list(map(int, input().split())) #整数リスト取得
def iln_s(): return sorted(iln()) # 昇順の整数リスト取得
def iln_r(): return sorted(iln(), reverse=True) # 降順の整数リスト取得
def fln(): return list(map(float, input().split())) # 浮動小数点リスト取得
def join(l, s=''): return s.join(l) #リストを文字列に変換
def perm(l, n): return itertools.permutations(l, n) # 順列取得
def perm_count(n, r): return math.factorial(n) // math.factorial(n-r) # 順列の総数
def comb(l, n): return itertools.combinations(l, n) # 組み合わせ取得
def comb_count(n, r): return math.factorial(n) // (math.factorial(n-r) * math.factorial(r)) #組み合わせの総数
def two_distance(a, b, c, d): return ((c-a)**2 + (d-b)**2)**.5 # 2点間の距離
def m_add(a,b): return (a+b) % MOD

def sieves_of_e(n):
    is_prime = [True] * (n+1)
    is_prime[0] = False
    is_prime[1] = False
    for i in range(2, int(n**0.5)+1):
        if not is_prime[i]: continue
        for j in range(i * 2, n+1, i): is_prime[j] = False
    return is_prime

S = list(input())
d = deque(S)
Q = iin()
Query = [input().split() for _ in range(Q)]

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

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
# Your code here!
# coding: utf-8
from fractions import gcd 
from functools import reduce
import sys
sys.setrecursionlimit(200000000)
from inspect import currentframe


# my functions here!
#標準エラー出力
def printargs2err(*args):
    names = {id(v):k for k,v in currentframe().f_back.f_locals.items()}
    print(', '.join(names.get(id(arg),'???')+' : '+repr(arg) for arg in args),file=sys.stderr)
def debug(*args):
    print(*args,file=sys.stderr)

def printglobals():
    for (symbol, value) in globals().items():
        print('symbol="%s"、value=%s' % (symbol, value),file=sys.stderr)
def printlocals():
    for (symbol, value) in locals().items():
        print('symbol="%s"、value=%s' % (symbol, value),file=sys.stderr)
#入力（後でいじる）
def pin(type=int):
    return map(type,input().split())
     
#input
from collections import deque
def resolve():
    S=deque(input())
    Q,=pin()
    ans=""
    
    for i in range(Q):
        q=input().split()
        if q[0]=="1":S.reverse()
        else:
            if q[1]=="1":
                S.appendleft(q[2])
            else:
                S.append(q[2])
    for s in S:
        ans+=s
    print(ans)
    #print([["NA","YYMM"],["MMYY","AMBIGUOUS"]][cMMYY][cYYMM])
#if __name__=="__main__":resolve()

"""
#printデバッグ消した？
#前の問題の結果見てないのに次の問題に行くの？
"""
"""
お前カッコ閉じるの忘れてるだろ
"""
import sys
from io import StringIO
import unittest

class TestClass(unittest.TestCase):
    def assertIO(self, input, output):
        stdout, stdin = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(input)
        resolve()
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, stdin
        self.assertEqual(out, output)
    def test_入力例_1(self):
        input = """a
4
2 1 p
1
2 2 c
1"""
        output = """cpa"""
        self.assertIO(input, output)
    def test_入力例_2(self):
        input = """a
6
2 2 a
2 1 b
1
2 2 c
1
1"""
        output = """aabc"""
        self.assertIO(input, output)
    def test_入力例_3(self):
        input = """y
1
2 1 x"""
        output = """xy"""
        self.assertIO(input, output)

if __name__=="__main__":resolve()

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/python3
# -*- coding:utf-8 -*-

def main():
  s = input().strip()
  q = int(input())
  state = 1
  swap = [0, 2, 1]
  for _ in range(q):
    x = input().strip()
    if x == '1':
      state *= -1
    else:
      _, f, c = x.split(' ')
      f = int(f)
      if state == -1:
        f = swap[int(f)]
      if f == 1:
        s = c + s
      else:
        s += c
  if state == -1:
    s = s[::-1]
  print(s)


  pass

if __name__=='__main__':
  main()

Submission status: Time Limit Exceeded

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

inv = False
for _ in range(q):
    com, *query = input().split()
    if com == "1":
        if not inv:
            inv = True
        else:
            inv = False
    else:
        f, c = query
        if not inv:
            if f == "1":
                s = c + s
            else:
                s = s + c
        else:
            if f == "1":
                s = s + c
            else:
                s = c + s


print(s if not inv else s[::-1])

Submission status: Time Limit Exceeded

Solution code:
ans = input()
Q = int(input())
is_reverse = False
for _ in range(Q):
    q = list(map(str,input().split()))
    if q[0] == '1':
        is_reverse = True ^ is_reverse # 反転
        #ans = ans[::-1]
    else:
        if is_reverse: # 反転してた場合
            if q[1] == '1':
                ans = ans + q[2]
            else:
                ans = q[2] + ans
        else:
            if q[1] == '1':
                ans = q[2] + ans
            else:
                ans = ans + q[2]
if is_reverse:
    print(ans[::-1])
else:
    print(ans)
#print(ans)

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
count = 0
for i in range(Q):
    TFC = input().split()
    if TFC[0] == '1':
        count += 1
    else:
        T, F, C = TFC
        if F == '1':
            if count % 2 == 0:
                S = ''.join([C, S])
            else:
                S = ''.join([S, C])
        else:
            if count % 2 == 0:
                S = ''.join([S, C])
            else:
                S = ''.join([C, S])
if count % 2 == 0:
    print(S)
else:
    print(S[::-1])

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
top = ""
end = ""
count = 0
for i in range(q):
    a = list(input().split())
    if len(a) == 1:
        count += 1
    else:
        if (count + int(a[1])) % 2 == 0:
            end = end + a[2]
        else:
            top = a[2] + top
ans = top + s + end
if count % 2 == 1:
    ans = ans[::-1]
print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

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

q1_cnt = 0
for qi in qs:
    if qi[0]=="1":
        q1_cnt+=1
    else:
        if q1_cnt%2==0:
            if qi[1]=="1":
                s = qi[2]+s
            else:
                s = s+qi[2]
        else:
            if qi[1]=="1":
                s = s+qi[2]
            else:
                s = qi[2]+s

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

Submission status: Time Limit Exceeded

Solution code:
s = [_ for _ in input()]
qn = int(input())
rev = False

for _ in range(qn):
  q = [_ for _ in input().split()]
  if q[0] == '1':
    rev = not rev
  else:
    if (rev and q[1] == '2') or (not rev and q[1] == '1'):
      s.insert(0, q[2])
    else:
      s.append(q[2])

if rev:
  s.reverse()
print("".join(s))

Submission status: Time Limit Exceeded

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

sei_or_gyaku = 1
ans = s[:]

for i in range(q):
    sirei = input()
    if len(sirei) == 1:
        ti = int(sirei)
        sei_or_gyaku *= -1
    else:
        ti, fi, ci = map(str, sirei.split())
        ti, fi = int(ti), int(fi)
        if fi == 1:
            if sei_or_gyaku == 1:
                ans = ci + ans
            else:
                ans = ans + ci
        else:
            if sei_or_gyaku == 1:
                ans = ans + ci
            else:
                ans = ci + ans


if sei_or_gyaku == 1:
    print(ans)
else:
    print(ans[::-1])

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
S=input()                                               # 1行1列
Q=int(input())                                          # 1行1列
Q_LIST=[input().split() for i in range(Q)]                # N行C列・・・Cは任意

for query in Q_LIST:
	if query[0] == '1':
		S = S[::-1]
	if query[0] == '2':
		if query[1] == '1':
			S = query[2] + S 
		if query[1] == '2':
			S =  S + query[2]

print(S)

Submission status: Time Limit Exceeded

Solution code:
def main():
    S = input()
    Q = int(input())
    revFlag = False
    for i in range(Q):
        tmp = list(input().split())
        if tmp[0] == "1":
            if revFlag:
                revFlag = False
            else:
                revFlag = True
        else:
            if tmp[1] == "1":
                if revFlag:
                    S += tmp[2]
                else:
                    S = tmp[2] + S
            else:
                if revFlag:
                    S = tmp[2] + S
                else:
                    S += tmp[2]
    if revFlag:
        print(S[::-1])
    else:
        print(S)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
S = input()

reverse = False

Q = int(input())

frontstack = ''
backstack = ''

for _ in range(Q):
    qstr = input()
    qlist = qstr.split()
    T = int(qlist[0])
    if T == 1:
        reverse = False if reverse == True else True
    elif T == 2:
        F = int(qlist[1])
        C = qlist[2]
        if F == 1:
            if reverse is False:
                frontstack += C
            else:
                backstack += C
        elif F == 2:
            if reverse is False:
                backstack += C
            else:
                frontstack += C

if reverse is False:
    ans = frontstack[::-1] + S + backstack
else:
    ans = backstack[::-1] + S[::-1] + frontstack

print(ans)

Submission status: Time Limit Exceeded

Solution code:
def main():
    S = input()
    Q = int(input())
    reverse = False

    for _ in range(Q):
        TFC = list(input().split())
        if len(TFC) == 1:
            reverse = not(reverse)
        else:
            t,f,c = TFC
            if reverse:
                if f == "2":
                    S = c + S
                else:
                    S = S + c

            else:
                if f == "1":
                    S = c + S
                else:
                    S = S + c
    if reverse:
        print(S[::-1])
    else:
        print(S)
    return

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

flip = -1
for query in Q:
    if query[0] == '1':
        flip = flip * -1
    elif query[0] == '2':
        if query[1] == '1':
            if flip == -1:
                s = query[2] + s
            else:
                s = s + query[2]
        else:
            if flip == -1:
                s = s + query[2]
            else:
                s = query[2] + s
if flip == 1:
    s = s[::-1]
print(s)

Submission status: Time Limit Exceeded

Solution code:
s=list(str(input()))
q=int(input())
now = 0
for _ in range(q):
  lis = list(map(str,input().split()))
  if int(lis[0]) == 1:
    now = (now+1)%2
  else:
    if (int(lis[1])+now)%2 == 1:
      s.insert(0,lis[2])
    else:
      s.append(lis[2])
if now ==0:
  for i in range(len(s)):
    print(s[i],end="")
else:
  for i in range(len(s)):
    print(s[-i-1],end="")

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
cnt = 0
mae = ""
usiro = ""
for _ in range(Q):
    q = input().split()
    if len(q) == 1:
        cnt += 1
    else:
        if cnt % 2 == 0:
            if int(q[1]) == 1:
                mae += q[2]
            else:
                usiro += q[2]
        else:
            if int(q[1]) == 1:
                usiro += q[2]
            else:
                mae += q[2]

mae = mae[::-1]
S = mae + S + usiro

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

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
ans = []
ans.append(S)
T = [0,0,""]
for i in range(Q):
  TS = input()
  if TS == "1":
    ans.append(ans[0][::-1])
    ans.pop(0)
  else:
    T = TS.rstrip().split(" ")
    if int(T[1]) == 1:
      ans.append(T[2] + ans[0])
      ans.pop(0)
    else:
      ans.append(ans[0] + T[2])
      ans.pop(0)
print(ans[0])

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin
sin = stdin.readline().rstrip()
q = int(stdin.readline().rstrip())
li = [list(stdin.readline().rstrip().split()) for _ in range(q)]
point = 0
for i in li:
    if i[0] == "1":
        point += 1
    elif i[0] == "2":
        if i[1] == "1":
            if point%2 == 0:
                sin = i[2] + sin
            else:
                sin += i[2]
        else:
            if point%2 == 0:
                sin += i[2]
            else:
                sin = i[2] + sin
if point%2 == 1:
    sin = sin[::-1]
print(sin)

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

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

for i in range(q):
    s1=input()
    if len(s1)==1:
        hanten+=1
    else:
        a,f,c=s1.split()
        f=int(f)
        if f==1:
            if hanten%2==0:
                s = c + s
            else:
                s = s + c
        else:
            if hanten%2==0:
                s = s + c
            else:
                s = c + s
        

if hanten%2==0:
    print(s)
else:
    s2=""
    for i in range(len(s)):
        s2 += s[-(i+1)]
    print(s2)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

#qs = []
for i in range(q):
    x = input()
    if x[0] == '1':
        #qs.append(x)
        s = s[::-1]
    else:
        #qs.append(x.split(' '))
        xx = x.split(' ')
        if xx[1] == '1':
            s = xx[2] + s
        else:
            s = s + xx[2]
print(s)

Submission status: Time Limit Exceeded

Solution code:
import sys
import math
from functools import reduce
from operator import mul
sys.setrecursionlimit(10**6)

def InMap(): return list(map(int, sys.stdin.readline().split(' ')))
def In(): return int(sys.stdin.readline())
def InS(): return sys.stdin.readline()

S = InS()[:-1:]
Q = In()

for _ in range(Q):
    Query = InS()[:-1:].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())
Qs = []
for _ in range(Q):
    Qs.append(input().split())  # T, F, C
    
for i in range(len(Qs)):
    if Qs[i][0]=='1':
        S = S[::-1]
    else:
        if Qs[i][1] == '1':
            S = Qs[i][2] + S
        else:
            S = S + Qs[i][2]

print(S)

Submission status: Time Limit Exceeded

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

revs = 0

for i in range(q):
    ins = input().split()
    if(ins[0] == "1"):
        revs += 1
    else:
        if (ins[1] == "1" and revs%2 == 0 or ins[1] =="2" and revs%2 == 1):
            s.insert(0, ins[2])
        else:
            s.append(ins[2])

if(revs %2 == 0):
    for j in s:
        print(j, end="")
else:
    for j in reversed(s):
        print(j, end="")
print()

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
S=input()
q=int(input())
Q=[]
state=True
ans=deque(S)
for _ in range(q):
    t = input().split()
    if t[0] == "1":
        Q.append(int(t[0]))
    elif t[0] == "2":
        Q.append((int(t[0]),int(t[1]),t[2]))
for que in Q:
    if que == 1:
        state = not state
    else:
        if state:
            if que[1] == 1:
                ans.appendleft(que[2])
            else:
                ans.append(que[2])
        else:
            if que[1] == 2:
                ans.appendleft(que[2])
            else:
                ans.append(que[2])
anss=""
if not state:
    while len(ans) >0:
        anss += ans.pop()
else:
    while len(ans) >0:
        anss += ans.popleft()
print(anss)