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()
roop = int(input())
is_head = True
for i in range(roop):
    q_li = input().split()
    t = int(q_li[0])
    if t == 1:
        is_head = not is_head
    else:
        f = int(q_li[1])
        c = q_li[2]
        if f == 1:
            # 先頭に追加
            s = c + s if is_head else s + c
        else:
            # 末尾に追加
            s = s + c if is_head else c + s

print(s) if is_head else print(s[::-1])

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
S=deque(input())
Q=int(input())
for i in range(Q):
  N=list(input().split())
  if N[0]=="1":
    S.reverse()
  elif N[1]=="1":#先頭にC1
    S.appendleft(N[2])
  else:#末尾にC1
    S.append(N[2])
S="".join(S)
print(S)

Submission status: Time Limit Exceeded

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

for i in range(q):
    x = input()
    if int(len(x)) == 1:
        a = int(x)
        s = s[::-1] 
        # print(s)
    else:
        a, b, c = map(str, x.split()) 
        if int(b) == 1:
            s = c + s
            # print(s)
        else:
            s = s + c
            # print(s)

print(s)

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
F=0
l=""
r=""
for i in range(Q):
  q=list(map(str,input().split()))
  if q[0]=="1":
    F+=1
  else:
    if q[1]=="1":
      if F%2==0:
        l += q[2]
      else:
        r += q[2]
    else:
      if F%2==0:
        r += q[2]
      else:
        l += q[2]  
if F%2==0:
  print(l[::-1]+S+r)
else:
  print(r[::-1]+S[::-1]+l)

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
F=0
for i in range(Q):
  q=list(map(str,input().split()))
  if q[0]=="1":
    F+=1
  else:
    if q[1]=="1":
      if F%2==0:
        S = q[2]+S
      else:
        S = S+q[2]
    else:
      if F%2==0:
        S = S+q[2]
      else:
        S = q[2]+S  
if F%2==0:
  print(S)
else:
  print(S[::-1])

Submission status: Time Limit Exceeded

Solution code:
s= list(input())
r=False
Q=int(input())
for i in range(Q):
	q,*l= input().split()
	#print(l)
	if int(q)==1:
		r=not(r)
	else:
		if(int(l[0])==1):
			if r:
				s.append(l[1])
			else:
				s.insert(0,l[1])
		else:
			if r:
				s.insert(0,l[1])
			else:
				s.append(l[1])
if r:
    s=s[::-1]
    for i in s:
        print(i,end='')
else:
    for i in s:
        print(i,end='')

Submission status: Time Limit Exceeded

Solution code:
s= list(input())
r=False
x=''
Q=int(input())
for i in range(Q):
	q,*l= input().split()
	#print(l)
	if int(q)==1:
		r=not(r)
	else:
		if(int(l[0])==1):
			if r:
				s.append(l[1])
			else:
				s.insert(0,l[1])
		else:
			if r:
				s.insert(0,l[1])
			else:
				s.append(l[1])
x=''.join(s)
if r:
	print(x[::-1])
else:
	print(x)

Submission status: Time Limit Exceeded

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

def rev(s):
  n=len(s)
  lis = [x for x in s]
  for i in range(n//2):
    lis[i],lis[n-1-i]=lis[n-1-i],lis[i]
  rtn=''
  for st in lis:
    rtn+=st
  return rtn

def stra(s,f,c):
  rtn=''
  if f=='2':
    rtn=s+c
  else:
    rtn=c+s
  return rtn

for i in range(q):
  a=list(input().split())
  if a[0]=="1":
    s=rev(s)
  else:
    s=stra(s,a[1],a[2])
print(s)

Submission status: Time Limit Exceeded

Solution code:
c = input()
n = int(input())
head = 1

for _ in range(n):
  now = list(input().split())
  if now[0] == "1":
    head *= -1
  else:
    if now[1] == "1":
      if head == 1:
        c = now[2] + c
      else:
        c = c + now[2]
    else:
      if head == 1:
        c = c + now[2]
      else:
        c = now[2] + c
      
print(c if head == 1 else c[::-1])

Submission status: Time Limit Exceeded

Solution code:
S = input()
queryNum = input()
flip = False
for i in range(int(queryNum)):
	order = input()
	if order == '1':
		if flip:
			flip = False
		else:
			flip = True
	else:
		if order[2] == '1':
			if not flip:
				S = order[4] + S
			elif flip:
				S = S + order[4]
		else:
			if not flip:
				S = S + order[4]
			elif flip:
				S = order[4] + S
if flip:
	print(S[::-1])
else: 
	print(S)

Submission status: Time Limit Exceeded

Solution code:
c = input()
n = int(input())

for _ in range(n):
  now = list(input().split())
  if now[0] == "1":
    c = c[::-1]
  else:
    if now[1] == "1":
      c = now[2] + c
    else:
      c = c + now[2]
      
print(c)

Submission status: Time Limit Exceeded

Solution code:
S = input()
queryNum = input()
for i in range(int(queryNum)):
	order = input()
	if order == '1':
		S = S[::-1]
	else:
		if order[2] == '1':
			S = order[4] + S
		else:
			S = S + order[4]
print(S)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

ini = input()
N = int(input())
for i in range(N):
#    data = np.array(input().split(), dtype=np.int64)
    data = np.array(input().split())
    num = np.shape(data)
    if (num[0] == 1):
        ini = ini[::-1]
    else:
        add = data[2]
        point = data[1]
        if (point == "1"):
            ini = str(add) + ini
        else:
            ini = ini + str(add)
print(ini)

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

mae = ""
usiro = ""
cnt = 0
for i in range(N):
    x = input()
    
    if x[0] == "1":
        cnt += 1
    
    elif x[2] == "1":
        if cnt % 2 == 0:
            mae = x[4] + mae
        else:
            usiro = usiro + x[4]
    
    else:
        if cnt % 2 == 0:
            usiro = usiro + x[4]
        else:
            mae = x[4] + mae
    

if cnt %2 == 0:
    S = mae + S + usiro
else:
    S = usiro[::-1] + S[::-1] + mae[::-1]
    
print(S)

Submission status: Time Limit Exceeded

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

for i in range(N):
    x = input()
    
    if x[0] == "1":
        S = S[::-1]
    
    elif x[2] == "1":
        S = x[4] + S
    
    else:
        S = S + x[4]
        
print(S)

Submission status: Time Limit Exceeded

Solution code:
s=input()
n=int(input())
for i in range(n):
    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:
from collections import deque  
d = deque()

S = input()
Q = int(input())
Query = [input() for i in range(Q)]

#最初の文字列をdに追加
for i in range(len(S)):
    d.append(S[i])

# 先頭から読むか，末尾から読むかを判定する
# 偶数 -> 先頭から，奇数 -> 末尾から
isReadOrientation = 0

for i in range(Q):
    if len(Query[i]) == 1:
        isReadOrientation += 1
    else:
        #先頭に追加
        if Query[i][2] == "1":
            #先頭に追加
            if isReadOrientation % 2 == 0:
                d.appendleft(Query[i][4])
            #末尾に追加
            else:
                d.append(Query[i][4])
        #末尾に追加
        else:
            #末尾に追加
            if isReadOrientation % 2 == 0:
                d.append(Query[i][4])
            #先頭に追加
            else:
                d.appendleft(Query[i][4])

ans = ""
#先頭から文字列に記録
if isReadOrientation % 2 == 0:
    for i in range(len(d)):
        ans += d[i]
#末尾から文字列に記録
else:
    for i in range(len(d)-1,-1,-1):
        ans += d[i]
print(ans)

Submission status: Time Limit Exceeded

Solution code:
s = str(input())
q = int(input())
b = 1
for i in range(q):
  la = input()
  if la == "1":
    b ^= 1
  else:
    a,f,c = la.split()
    if (int(f)-1)^b:s=c+s
    else:s=s+c
      
print(s if b==1 else s[::-1])

Submission status: Time Limit Exceeded

Solution code:
s = str(input())
q = int(input())
b = 1
for i in range(q):
  la = input()
  if la == "1":
    b ^= 1
  else:
    a,f,c = la.split()
    if f == "1":
      s = c+s if b==1 else s+c
    else:
      s = s+c if b==1 else c+s
      
print(s if b==1 else s[::-1])

Submission status: Time Limit Exceeded

Solution code:
s = input()
Q = int(input())
count = 0
right = []
left = []
for i in range(Q):
    q = input().split()
    if int(q[0]) == 1:
        count += 1
    else:
        if (count%2 == 0 and int(q[1]) == 1) or (count%2 == 1 and int(q[1]) == 2):
            left.insert(0, q[2])
        else:
            right.append(q[2])

left = "".join(left)
right = "".join(right)
if count%2 == 1:
    left = left[::-1]
    s = s[::-1]
    right = right[::-1]
    print(right+s+left)
else:
    print(left+s+right)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8

str = list(input())
num = int(input())

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

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8

str = list(input())
num = int(input())

for i in range(num):
    list = input().split()
    if list[0]=="1":
        str.reverse()
    elif list[0]=="2":
        if list[1]=="1":
            str.insert(0, list[2])
        elif list[1]=="2":
            str.append(list[2])
    
print("".join(str))

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
Qr = [list(input().split()) for i in range(Q)]
isForward = True
for i in range(Q):
    if Qr[i][0] == "1":
        isForward = not(isForward)
    elif (isForward and  Qr[i][1] == "1") or (not(isForward) and Qr[i][1] == "2"):
        S = Qr[i][2] + S
    else:
        S = S + Qr[i][2]
if not(isForward):
    S = S[::-1]
print(S)

Submission status: Time Limit Exceeded

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

quary = [list(map(str, input().split())) for i in range(c)]

for q in quary:
    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 = str(input())
n = int(input())

for _ in range(n):
    q = input()

    # 反転の場合
    if q[0] == "1":
        s = s[::-1]
        continue

    # q = q.split()

    if q[2] == "1":
        s = q[4] + s

    if q[2] == "2":
        s = s + q[4]

print(s)

Submission status: Time Limit Exceeded

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

for _ in range(n):
    q = input()

    # 反転の場合
    if q[0] == "1":
        s = s[::-1]
        continue

    q = q.split()

    if q[1] == "1":
        s = q[2] + s

    if q[1] == "2":
        s = s + q[2]

print(s)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3 ## -*- python -*-

def main():
    s = []
    s.append(input())
    q = int(input())
    head = "l"
    for i in range(q):
        d = input()
        if d[0] == "1":
            if head == "l":
                head = "r"
            else:
                head = "l"
        else: # 2
            dd = d.split()
            f = int(dd[1])
            c = dd[2]
            if f == 1:
                if head == "l":
                    s.insert(0,c)
                else:
                    s.append(c)
            else: # f == 2
                if head == "l":
                    s.append(c)
                else:
                    s.insert(0,c)

    ans = ''.join(s)
    if head == "r":
        ans = ans[::-1]
    print(ans)
main()

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3 ## -*- python -*-

def main():
    s = input()
    q = int(input())
    head = "l"
    for i in range(q):
        d = input()
        if d[0] == "1":
            if head == "l":
                head = "r"
            else:
                head = "l"
        else: # 2
            dd = d.split()
            f = int(dd[1])
            c = dd[2]
            if f == 1:
                if head == "l":
                    s = c + s
                else:
                    s = s + c
            else: # f == 2
                if head == "l":
                    s = s + c
                else:
                    s = c + s

    if head == "r":
        s = s[::-1]
    print(s)
main()

Submission status: Time Limit Exceeded

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

for _ in range(Q):
    q = input().split()
    if q[0]=="1":
        S=S[::-1]
    elif 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())
mylist =[]

for i in range(q):
    temp = list(map(str,input().split()))
    mylist.append(temp)
    
for i in range(q):
    if mylist[i][0] == "1":
        s = s[::-1]
    else:
        f = int(mylist[i][1])
        c = mylist[i][2]
        if f == 1:
            s = c + s
        else:
            s = s + c
            
print(s)

Submission status: Time Limit Exceeded

Solution code:
s = list(str(input()))
q = int(input())
for i in range (q):
    t = list(str(input()))
    if t[0] == '2':
        if t[2] == '1':
            S = [t[4]]
            S.extend(s)
            s = S
        else:
            s.append(t[4])
    else:
        s.reverse()
print(''.join(s))

Submission status: Time Limit Exceeded

Solution code:
s = list(str(input()))
q = int(input())
for i in range (q):
    t = list(str(input()))
    if t[0] == '2':
        if t[2] == '1':
            s.reverse()
            s.append(t[4])
            s.reverse()      
        else:
            s.append(t[4])
    else:
        s.reverse()
print(''.join(s))

Submission status: Time Limit Exceeded

Solution code:
s=list(input())
q=int(input())
a=[]
state=0
for i in range(q):
  a.append(input().split())
for i in range(q):
  if a[i][0]=='1':
    state=state^1
  else:
    if (a[i][1]=='1' and state==0) or (a[i][1]=='2' and state==1):
      s.insert(0,a[i][2])
    else:
      s.append(a[i][2])
out=''
if state==1:
  state=-1
else:
  state=1
for x in s[::state]:
  out+=x
print(out)

Submission status: Time Limit Exceeded

Solution code:
def main():
    S=input()
    Q=int(input())
    turn=1
    S_str=[S]
    for i in range(Q):
        a=input()
        if(a[0]=='1'):
            turn*=-1
        elif(a[0]=='2'):
            b=a.split()
            F,C=b[1],b[2]
            if(F=='1'):
                if(turn==1):
                    S_str.insert(0,C)
                elif(turn==-1):
                    S_str.append(C)
            elif(F=='2'):
                if(turn==1):
                    S_str.append(C)
                if(turn==-1):
                    S_str.insert(0,C)
    ans=''.join(S_str)
    if(turn==-1):
        ans=ans[::-1]
    print(ans)
main()

Submission status: Time Limit Exceeded

Solution code:
S=input()
Q=int(input())
turn=1
S_str=[S]
for i in range(Q):
    a=input()
    if(a[0]=='1'):
        turn*=-1
    elif(a[0]=='2'):
        b=a.split()
        F,C=b[1],b[2]
        if(F=='1'):
            if(turn==1):
                S_str.insert(0,C)
            elif(turn==-1):
                S_str.append(C)
        elif(F=='2'):
            if(turn==1):
                S_str.append(C)
            if(turn==-1):
                S_str.insert(0,C)
ans=''.join(S_str)
if(turn==-1):
    ans=ans[::-1]
print(ans)

Submission status: Time Limit Exceeded

Solution code:
S=input()
Q=int(input())
turn=1
for i in range(Q):
    a=input()
    if(a[0]=='1'):
        # S=S[::-1]
        turn*=-1
    elif(a[0]=='2'):
        b=a.split()
        F,C=b[1],b[2]
        if(F=='1'):
            if(turn==1):
                S=C+S
            elif(turn==-1):
                S=S+C
        elif(F=='2'):
            if(turn==1):
                S=S+C
            if(turn==-1):
                S=C+S
if(turn==-1):
    S=S[::-1]
print(S)

Submission status: Time Limit Exceeded

Solution code:
S=input()
Q=int(input())
for i in range(Q):
    a=input()
    if(a[0]=='1'):
        S=S[::-1]
    elif(a[0]=='2'):
        b=a.split()
        F,C=int(b[1]),b[2]
        if(F==1):
            S=C+S
        elif(F==2):
            S=S+C
print(S)

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
rev = False
for i in range(Q):
    query = input()
    if query == "1":
        T = 1
        rev = not(rev)
    else:
        T,F,C = query.split()
        if rev:
            if F == "1":
                S = S + C
            else:
                S = C + S
        else:
            if F == "1":
                S = C + S
            else:
                S = S + C
if rev:
    print("".join(list(reversed(S))))
else:
    print(S)

Submission status: Time Limit Exceeded

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

for i in range(Q):
    TFC = input()

    if TFC[0] == "1":
        Left = not Left

    elif TFC[0] == "2" and TFC[2] == "1":
        if Left:
            S = TFC[4] + S
        else:
            S = S + TFC[4]

    else:
        if Left:
            S = S + TFC[4]
        else:
            S = TFC[4] + S

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

print(S)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
string = input()
times = int(input())

for _ in range(times):
    input_value = [i for i in input().split()]
    command = input_value[0]
    if command == '1':
        string = string[::-1]
    elif command == '2':
        second_command = input_value[1]
        if second_command == '1':
            string = input_value[2] + string
        elif second_command == '2':
            string = string + input_value[2]

print(string)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

s = deque(input())
q = int(input())
rev = 0 # revが0のとき先頭、1のとき後尾

for _ in range(q):
    t = input().split()

    if len(t) == 1:
        rev ^= 1
    else:
        if rev == 1:
            if t[1] == '1':
                s.append(t[2])
            else:
                s.appendleft(t[2])
        else:
            if t[1] == '1':
                s.appendleft(t[2])
            else:
                s.append(t[2])

if rev == 1:
    s.reverse()

for i in range(len(s)):
    print(s[i],end='')

print()

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

r = 1
for i in range(1,Q+1):
    T = input()
    if len(T) == 1:
        r = -r
    else:
        if (T[2] == '1' and r == 1) or (T[2] == '2' and r == -1) :
            S = T[4] + S
        elif (T[2] == '2' and r == 1) or (T[2] == '1' and r == -1):
            S = S + T[4]
if r == -1:
    S = S[::-1]
print(S)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

d = deque(list(input()))
for _ in range(int(input())):
    a = input()
    if a[0] == '1':
        d.reverse()
    elif a[2] == '1':
        d.appendleft(a[4])
    else:
        d.append(a[4])
print(*d, sep='')

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
T1 = 0
for _ in range(Q):
  a = list(map(str,input().split()))
  if a[0] == '1':
    T1 += 1
  elif a[0] == '2':
    if ((a[1]=='1')and(T1%2==0)) or ((a[1]=='2')and(T1%2!=0)):
      S = a[2]+S
    else:
      S = S+a[2]

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

Submission status: Time Limit Exceeded

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

T = 0
for i in range(Q):
    Query = list(input().split())
    if Query[0] == "1" :
        T = T + 1
    elif Query[0] == "2" :
        if T % 2 == 0 :
            if Query[1] == "1" :
                S = Query[2] + S
            elif Query[1] == "2" :
                S = S + Query[2]
        elif T % 2 == 1 :
            if Query[1] == "1" :
                S = S + Query[2]
            elif Query[1] == "2" :
                S = Query[2] + S

if T % 2 == 1 :
    S = S[::-1]

print(S)

Submission status: Time Limit Exceeded

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

flag = 0
for i in range(Q):
	q = list(map(str, input().split()))
	if q[0] == '1':
		flag = 1 - flag
	else:
		if (q[1] == '1' and flag == 1) or (q[1] == '2' and flag == 0):
			S.append(q[2])
		else:
			S.insert(0, q[2])
if flag == 1:
	S.reverse()
ans = ''.join(S)
print(ans)

Submission status: Time Limit Exceeded

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

rev = list(reversed(S))
S_rev = ''.join(rev)

for i in range(Q):
	q = list(map(str, input().split()))
	if q[0] == '1':
		S, S_rev = S_rev, S
	else:
		if q[1] == '1':
			s = [q[2], S]
			s_rev = [S_rev, q[2]]
			S = ''.join(s)
			S_rev = ''.join(s_rev)
		else:
			s = [S, q[2]]
			s_rev = [q[2], S_rev]
			S = ''.join(s)
			S_rev = ''.join(s_rev)

print(S)

Submission status: Time Limit Exceeded

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

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

print(S)

Submission status: Time Limit Exceeded

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

rev = []
for i in range(N):
	rev.append(S[N-i-1])
S_rev = ''.join(rev)

for i in range(Q):
	q = list(map(str, input().split()))
	if q[0] == '1':
		S, S_rev = S_rev, S
	else:
		if q[1] == '1':
			s = [q[2], S]
			s_rev = [S_rev, q[2]]
			S = ''.join(s)
			S_rev = ''.join(s_rev)
		else:
			s = [S, q[2]]
			s_rev = [q[2], S_rev]
			S = ''.join(s)
			S_rev = ''.join(s_rev)

print(S)

Submission status: Time Limit Exceeded

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

rev = 1

for i in range(n):
  cs = input().split()
  #print( cs )
  if( cs[0] == '1' ) :
    rev *= -1
  else:
    cf = cs[1]
    cfs = cs[2]
    if rev == 1 :
      if cf == '1' :
        s =  cfs + s 
      else:
        s =  s + cfs
    else: #rev == -1 :
      if cf == '1' :
        s =  s + cfs
      else:
        s =  cfs + s

if rev == 1 :
 print( s )
else:
 res = ''.join(list(reversed( s )))
 print (res)

Submission status: Time Limit Exceeded

Solution code:
a = input()
c = input()
d=[]
alist = list(a)
count = 0
for i in range(int(c)):
  e = input().split()
  d.append(e)
for i in range(int(c)):
  if d[i][0]=="1":
    count = count + 1
  else:
    if d[i][1] == "1" and count % 2 == 0:
      alist = [d[i][2]] + alist
    elif d[i][1] == "2" and count % 2 == 1:
      alist = [d[i][2]] + alist
    else:
      alist = alist + [d[i][2]]
if count % 2 == 1:
  l = len(alist)
  for x in range(int(l//2)):
    alist[x],alist[l-x-1] = alist[l-x-1],alist[x]
answer = "".join(alist)
print(answer)

Submission status: Time Limit Exceeded

Solution code:
left = input()
q = int(input())
right = ""
flag = 0
for i in range(q) :
    t = input()
    if t == "1" :
        flag ^= 1
        continue
    t, f, c = t.split(" ")
    f = int(f)
    left = c*((flag+f)%2) + left
    right += c*((flag+f-1)%2)
if flag == 0 :
    print(left+right)
else :
    print(right[::-1]+left[::-1])

Submission status: Time Limit Exceeded

Solution code:
a = input()
c = input()
d=[]
alist = list(a)
for i in range(int(c)):
  e = input().split()
  d.append(e)
for i in range(int(c)):
  if d[i][0]=="1":
    l = len(alist)
    for x in range(int(l//2)):
      alist[x],alist[l-x-1] = alist[l-x-1],alist[x]
  else:
    if d[i][1] == "1":
      alist = [d[i][2]] + alist
    else:
      alist = alist + [d[i][2]]
le = len(alist)
answer =""
answer = "".join(alist)
print(answer)

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
flag = 0b0
for i in range(q) :
    t = input()
    if t == "1" :
        flag ^= 1
        continue
    t, f, c = t.split(" ")
    f = int(f)
    s = c*((flag+f)%2) + s + c*((flag+f+1)%2)
if flag == 0 :
    print(s)
else :
    print(s[::-1])

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
flag = 0b0
for i in range(q) :
    t = input()
    if t == "1" :
        flag ^= 1
        continue
    if (int(t[2]) - flag) == 1 :
        s = t[4] + s
    else :
        s = s + t[4]
if flag == 0 :
    print(s)
else :
    print(s[::-1])

Submission status: Time Limit Exceeded

Solution code:
a = input()
c = input()
d=[]
alist = list(a)
for i in range(int(c)):
  e = input().split()
  d.append(e)
for i in range(int(c)):
  if d[i][0]=="1":
    l = len(alist)
    for x in range(int(l//2)):
      alist[x],alist[l-x-1] = alist[l-x-1],alist[x]
  else:
    if d[i][1] == "1":
      alist = [d[i][2]] + alist
    else:
      alist = alist + [d[i][2]]
le = len(alist)
answer =""
for i in range(le):
  answer = answer + alist[i]
print(answer)

Submission status: Time Limit Exceeded

Solution code:
a = input()
c = input()
d=[]
alist = list(a)
for i in range(int(c)):
  e = input().split()
  d.append(e)
for i in range(int(c)):
  if d[i][0]=="1":
    l = len(alist)
    for x in range(int(l//2)):
      alist[x],alist[l-x-1] = alist[l-x-1],alist[x]
  else:
    if d[i][1] == "1":
      alist = [d[i][2]] + alist
    else:
      alist = alist + [d[i][2]]
le = len(alist)
answer =""
for i in range(le):
  answer = answer + alist[i]
print(answer)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
s = input()
n = int(input())
inv = False
for i in range(n):
  l = input()
  if l[0]=="1":
    inv = not inv
  else:
    ll,mm,nn = map(str,l.split())
    if (mm=="1" and (not inv)) or (mm=="2" and inv):
      s=nn+s
    else:
      s+=nn
ans=""
if(inv):
  for i in range(len(s)-1,-1,-1):
    ans+=s[i]
else:
  ans = s
print(ans)

Submission status: Time Limit Exceeded

Solution code:
s = input()
change_flag = 1
for i in range(int(input())):
    a = input().split(" ")
    if(a[0] == "1"):
        change_flag *= -1
    else:
        if((a[1] == "1" and change_flag == 1) or (a[1] == "2" and change_flag == -1)):
            s = a[2] + s
        else:
            s = s + a[2]

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

Submission status: Time Limit Exceeded

Solution code:
import collections

x = collections.deque(input())
n = int(input())

for i in range(n):
    y = input().split()
    if len(y) == 1:
      x.reverse()
    else:
      if int(y[1]) == 2:
          x.append(y[2])
      else:
          x.appendleft(y[2])

print("".join(x))

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3 ## -*- python -*-

def main():
    s = list(input())
    q = int(input())
    for i in range(q):
        d = input()
        if d[0] == "1":
            s.reverse()
        else: # 2
            dd = d.split()
            f = int(dd[1])
            c = dd[2]
            if f == 1:
                s.insert(0, c)
            else: # f == 2
                s.append(c)

    print(''.join(s))

main()

Submission status: Time Limit Exceeded

Solution code:
import collections

x = collections.deque(input())
n = int(input())
y = 0

for i in range(n):
    a = input()
    if a[0] == "1":
      x.reverse()
    else:
      a, b, c = a.split()
      a = int(a)
      b = int(b)

      if b == 2:
          x.append(c)
      elif b == 1:
          x.appendleft(c)

print("".join(x))

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3 ## -*- python -*-

def main():
    s = input()
    q = int(input())

    for i in range(q):
        d = input()
        if d[0] == "1":
            s = s[::-1]
        else: # 2
            dd = d.split()
            f = int(dd[1])
            c = dd[2]
            if f == 1:
                s = c + s
            else: # f == 2
                s = s + c

    print(s)

main()