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

for i in range(Q):
  TFC = input().split()
  T = int(TFC[0])
  if T == 1:
    # turn over
    S = S[-1::-1]
  elif T == 2:
    F = int(TFC[1])
    C = TFC[2]
    if F == 1:
      S = C + S
      # put C before S
    elif F == 2:
      S = S + C
      # put C after S

print(S)

Submission status: Time Limit Exceeded

Solution code:
# 158-D ver2
s = input()
q = int(input())
is_not_reverse = True
for _ in range(q):
    command = [e for e in input().split()]
    if command[0] == '1':
        is_not_reverse = not is_not_reverse      
    elif command[1] == '1':
        if is_not_reverse:
            s = command[2] + s
        else:
            s = s + command[2]
    elif command[1] == '2':
        if is_not_reverse:
            s = s + command[2]
        else:
            s = command[2] + s
if is_not_reverse:
    print(s)
else:
    print(s[::-1])

Submission status: Time Limit Exceeded

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

flag = False
for i in range(1, q+1):
    a = input().split()
    if len(a) == 1:
        flag = not flag
    else:
        _, f, c = a
        if (f == "1" and flag == False) or (f == "2" and flag == True):
            ans = c + ans
        elif (f == "1" and flag == True) or (f == "2" and flag == False):
            ans = ans + c
        else:
            print("fac")
            exit(1)

if flag:
    print(ans[::-1])
else:
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
# 158-D
s = input()
q = int(input())
for _ in range(q):
    command = input().split()
    if command[0] == '1':
        s = s[::-1]
    elif command[0] == '2':
        if command[1] == '1':
            s = command[2]+s
        elif command[1] == '2':
            s = s + command[2]
print(s)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

sList = deque(list(input()))
numOp = int(input())
instList = []
finEnd = []
beg = []
end = []

instList = [input().split(' ') for i in range(numOp)]

for m in range(len(instList)):
  if instList[m - 1] != ['1'] and instList[m] == ['1']:
    beg.append(m)
  if (m + 1) < len(instList):
    if instList[m] == ['1'] and instList[m + 1] != ['1']:
      end.append(m)
  else:
    end.append(m)

finEnd = [e for b, e in zip(beg, end) if (e - b + 1) % 2 != 0 or (e - b + 1) == 0]

for i in range(len(instList)):
  for ii in range(len(instList[i])):
    if instList[i][ii] == '2':
      if instList[i][ii + 1] == '1':
          sList.appendleft(instList[i][ii + 2])
      elif instList[i][ii + 1] == '2':
        sList.append(instList[i][ii + 2])
      break
  if i in finEnd:
    sList.reverse()

print(''.join(sList))

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
mylist =[]
check = 1

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

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

Submission status: Time Limit Exceeded

Solution code:
sList = list(input())
numOp = int(input())
instList = []
finEnd = []
beg = []
end = []

instList = [input().split(' ') for i in range(numOp)]

for m in range(len(instList)):
  if instList[m - 1] != ['1'] and instList[m] == ['1']:
    beg.append(m)
  if (m + 1) < len(instList):
    if instList[m] == ['1'] and instList[m + 1] != ['1']:
      end.append(m)
  else:
    end.append(m)

finEnd = [e for b, e in zip(beg, end) if (e - b + 1) % 2 != 0 or (e - b + 1) == 0]

for i in range(len(instList)):
  for ii in range(len(instList[i])):
    if instList[i][ii] == '2':
      if instList[i][ii + 1] == '1':
          tempList = []
          tempList.append(instList[i][ii + 2])
          for t in sList:
            tempList.append(t)
            sList = tempList
      elif instList[i][ii + 1] == '2':
        sList.append(instList[i][ii + 2])
      break
  if i in finEnd:
    reverseList = []
    for s in range(len(sList)):
      reverseList.append(sList[len(sList) - (s + 1)])
    sList = reverseList

print(''.join(sList))

Submission status: Time Limit Exceeded

Solution code:
sList = list(input())
numOp = int(input())
instList = []
count = {}
finEnd = []
beg = []
end = []

for i in range(numOp):
  instList.append(input().split(' '))

for m in range(len(instList)):
  if instList[m - 1] != ['1'] and instList[m] == ['1']:
    beg.append(m)
  if (m + 1) < len(instList):
    if instList[m] == ['1'] and instList[m + 1] != ['1']:
      end.append(m)
  else:
    end.append(m)

for b, e in zip(beg, end):
  if (e - b + 1) not in count:
    count[(e - b + 1)] = e
  elif (e - b + 1) % 2 != 0 or (e - b + 1) == 0:
    finEnd.append(e)

for c in count:
  if c % 2 == 0 and c != 0:
    count[c] = 'mark'

for c in count.values():
  if c != 'mark':
    finEnd.append(c)

for i in range(len(instList)):
  for ii in range(len(instList[i])):
    if instList[i][ii] == '2':
      if instList[i][ii + 1] == '1':
          tempList = []
          tempList.append(instList[i][ii + 2])
          for t in sList:
            tempList.append(t)
            sList = tempList
      elif instList[i][ii + 1] == '2':
        sList.append(instList[i][ii + 2])
      break
  if i in finEnd:
    reverseList = []
    for s in range(len(sList)):
      reverseList.append(sList[len(sList) - (s + 1)])
    sList = reverseList

print(''.join(sList))

Submission status: Time Limit Exceeded

Solution code:
sList = list(input())
numOp = int(input())
instList = []

for i in range(numOp):
  instList.append(input().split(' '))

for i in range(len(instList)):
  for ii in range(len(instList[i])):
    if instList[i][ii] == '2':
      if instList[i][ii + 1] == '1':
          tempList = []
          tempList.append(instList[i][ii + 2])
          for i in sList:
            tempList.append(i)
            sList = tempList
      elif instList[i][ii + 1] == '2':
        sList.append(instList[i][ii + 2])
      break
    elif instList[i][ii] == '1':
      reverseList = []
      for s in range(len(sList)):
        reverseList.append(sList[len(sList) - (s + 1)])
      sList = reverseList
      break

print(''.join(sList))

Submission status: Time Limit Exceeded

Solution code:
S = list(input())
Q = int(input())
Q_l = []
for i in range(Q):
    Q_l.append(input().split())
for i in range(Q):
    #print(S)
    if int(Q_l[i][0]) == 1:
        S.reverse()
    else:
        if int(Q_l[i][1]) == 1:
            S = list(Q_l[i][2]) + S
        else:
            S.append(Q_l[i][2])
print("".join(S))

Submission status: Time Limit Exceeded

Solution code:
S = input()

flag = 1
N = int(input())
for i in range(N):
  line = input().split()
  if line[0] == "1":
    if flag == 1:
      flag = 2
    else:
      flag = 1
  else:
    if flag == 1:
      if line[1] == "1":
        S = line[2] + S
      else:
        S = S + line[2]
    else:
      if line[1] == "1":
        S = S + line[2]
      else:
        S = line[2] + S

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

Submission status: Time Limit Exceeded

Solution code:
L = []
S = str(input())
Q = int(input())
Ql = [[] for _ in range(Q)] 

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

for i in range(Q):
    A = input().split()
    Ql[i] = A

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

print(''.join(L))

Submission status: Time Limit Exceeded

Solution code:
S=input()
Q=int(input())
words=[]
for a in range(Q):
    A=input().split()
    words.append(A)
left=[]
right=[]
number=0
for a in words:
    if a[0]=='1':
        number+=1
    else:
        if number%2==0:
            if a[1]=='1':
                left.insert(0,a[2])
            else:
                right.append(a[2])
        else:
            if a[1]=='1':
                right.append(a[2])
            else:
                left.insert(0,a[2])
T=[a for a in S]
if number%2==0:
    kotae=left+T+right
    moji=''
    for a in kotae:
        moji+=a
    print(moji)
else:
    left.reverse()
    T.reverse()
    right.reverse()
    kotae = right+T+left
    moji = ''
    for a in kotae:
        moji += a
    print(moji)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
ans=deque()
ans.append(str(input()))
q=int(input())
l=[list(map(str,input().split())) for i in range(q)]
#print(l)
c=0
for i in range(q):
	if l[i][0]=='1':
		c+=1
	else:
		if c%2==0:
			if l[i][1]=='1':
				ans.appendleft(l[i][2])
			else:
				ans.append(l[i][2])
		else:
			if l[i][1]=='2':
				ans.appendleft(l[i][2])
			else:
				ans.append(l[i][2])
#print(list(ans))
A=''
for i in range(len(ans)):
	A+=ans[i]
if c%2==1:
	print(A[::-1])
else:
	print(A)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

reflag = 0
s = deque(input())
q = int(input())

for _ in range(q):
    x = input().split()
    
    if(x[0]=='1'):
        reflag = reflag + 1
        #s.reverse()
    else:
        if(reflag % 2 == 0):
            reflag = 0
        else:
            s.reverse()
            reflag = 0
        
        s.appendleft(x[2]) if(x[1]=='1') else s.append(x[2])

if(reflag % 2 == 0):
    pass
else:
    s.reverse()

print(''.join(s))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

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

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

print(''.join(s))

Submission status: Time Limit Exceeded

Solution code:
reply = []
reply.append(input())
num = int(input())
hanten = False
text = ""
for i in range(num):
    text = input().split()
    if text[0] == "1":
        hanten = not hanten
    else:
        if text[1] == "1":
            if hanten:
                reply.append(text[2])
            else:
                reply.insert(0,text[2])
        else:
            if hanten:
                reply.insert(0,text[2])
            else:
                reply.append(text[2])

reply_text=''.join(reply)
if hanten:
    reply_text = reply_text[::-1]

print(reply_text)

Submission status: Time Limit Exceeded

Solution code:
reply = input()
num = int(input())
hanten = False
text = ""
for i in range(num):
    text = input().split()
    if text[0] == "1":
        hanten = not hanten
    else:
        if text[1] == "1":
            if hanten:
                reply = reply + text[2]
            else:
                reply = text[2] + reply
        else:
            if hanten:
                reply = text[2] + reply
            else:
                reply = reply + text[2]

if hanten:
    reply = reply[::-1]


print(reply)

Submission status: Time Limit Exceeded

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

for i in range(num):
    text = input().split()
    if text[0] == "1":
        reply = ''.join(list(reversed(reply)))
    else:
        if text[1] == "1":
            reply = text[2] + reply
        else:
            reply = reply + text[2]

print(reply)

Submission status: Time Limit Exceeded

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

for i in range(num):
    text = input().split()
    if text[0] == "1":
        reply = reply[::-1]
    else:
        if text[1] == "1":
            reply = text[2] + reply
        else:
            reply = reply + text[2]

print(reply)

Submission status: Time Limit Exceeded

Solution code:
rflag = 0
s = [i for i in input()]
q = int(input())

for i in range(q):
    x = input().split()
    
    if(x[0] == '1'):
        rflag = rflag + 1
        # s = s[::-1]
    else:
        if((rflag % 2) == 0):
            rflag = 0
        else:
            s = s[::-1]
            rflag = 0
            
        s.insert(0,x[2]) if(x[1]=='1') else s.append(x[2])

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

print(''.join(s))

Submission status: Time Limit Exceeded

Solution code:
S = input()

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

print(S)

Submission status: Time Limit Exceeded

Solution code:
s = [i for i in input()]
q = int(input())

for i in range(q):
    x = input().split()
    
    if(x[0] == '1'):
        s = s[::-1]
    else:
        s.insert(0,x[2]) if(x[1]=='1') else s.append(x[2])


print(''.join(s))

Submission status: Time Limit Exceeded

Solution code:
def sr(x):
    y = ''
    for i in range(len(x)-1,-1,-1):
        y = '%s%s' %(y,x[i])
    return y

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

for i in range(q):
    x = input().split()
    
    if (x[0] == '1'):
        #s = s[::-1]
        s = sr(s)
    else:
        s = '%s%s'%(x[2],s) if (x[1] == '1') else '%s%s'%(s,x[2])

print(s)

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

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

for i in range(Q):
  if L[i][0] == "1":
    S = ''.join(list(reversed(S)))
    #print(S)
  if L[i][0] == "2":
    if L[i][1] == "1":
      S_list = list(S)
      S_list.insert(0, L[i][2])
      S = ''.join(S_list)
    if L[i][1] == "2":
      S_list = list(S)
      S_list.append(L[i][2])
      S = ''.join(S_list)
print(S)

Submission status: Time Limit Exceeded

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

for i in range(q):
  t = input().split()
  if int(t[0]) == 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:
import math
import string
import itertools
import fractions
import heapq
import collections
import re
import array
import bisect
import sys
import random
import time
inf = 10**9


def main():
    o = 1
    s = input()
    q = int(input())
    for i in range(q):
        qu = input().split()
        if qu[0] == '1':
            o = int(not o)
        else:
            addo = 1 if qu[1] == '1' else 0
            if o == addo:
                s = qu[2] + s
            else:
                s += qu[2]
        # print(s, o)
    
    if o:
        print(s)
    else:
        print(''.join(reversed(s)))




if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
def func_1(line):
    line = line[::-1]
    return line

def func_2_1(line,letter):
    letter += line
    return letter
    
def func_2_2(line,letter):
    line += letter
    return line
    
line = input()
N = int(input())

for _ in range(N):
    tmp = input()
    if tmp == "1":
        line = func_1(line)
    else:
        num_1, num_2, letter= tmp.split()
        if num_2 == "1":
            line = func_2_1(line,letter)
        if num_2 == "2":
            line = func_2_2(line,letter)
            

print(line)

Submission status: Time Limit Exceeded

Solution code:
s = input()

roops = int(input())

grid = []
for i in range(roops):
    a = list(map(str,input().strip().split()))
    if len(a)==1:
        s = ''.join(list(reversed(s)))
    else:
        if a[1]=="1":
            s = a[2]+s
        else:
            s = s+a[2]
print(s)

Submission status: Time Limit Exceeded

Solution code:
def sf(s,queries):

    is_reversed = 0

    for query in queries:
        addl = []
        addr = []
        if query[0] == '1':
            is_reversed ^= 1
        elif query[0] == '2':
            f = query[1]
            c = query[2]
            if is_reversed == 0 and f == '1' or is_reversed == 1 and f == '2':
                addl = [c]
            elif is_reversed == 0 and f == '2' or is_reversed == 1 and f == '1':
                addr = [c]

            s = addl + s + addr

    return ''.join(s) if not is_reversed else ''.join(reversed(s))


if __name__ == '__main__':
    s = list(input())
    q = int(input())
    ql = [input().split() for i in range(q)]
    print(sf(s,ql))

Submission status: Time Limit Exceeded

Solution code:
def sf(s,queries):

    is_reversed = 0

    for query in queries:
        addl = []
        addr = []
        if query[0] == '1':
            is_reversed ^= 1
        elif query[0] == '2':
            f = query[1]
            c = query[2]
            if is_reversed == 0 and f == '1' or is_reversed == 1 and f == '2':
                addl.append(c)
            elif is_reversed == 0 and f == '2' or is_reversed == 1 and f == '1':
                addr.append(c)

            s = addl + s + addr

    return ''.join(s) if not is_reversed else ''.join(reversed(s))


if __name__ == '__main__':
    s = list(input())
    q = int(input())
    ql = [input().split() for i in range(q)]
    print(sf(s,ql))

Submission status: Time Limit Exceeded

Solution code:
def add(s,f,c,is_reversed):
    if f == '1':
        ht = 0
    elif f == '2':
        ht = 1
        
    if is_reversed != ht:
            ns = s + c 
    if is_reversed == ht:
            ns = c + s

    return ns

def msf(s,q,ql):
    len_ql = len(ql)
    is_reversed = 0
    for i in range(0,len_ql):
        t1 = ql[i][0]
        if t1 == "1":
            is_reversed ^= 1
        elif t1 == "2":
            f = ql[i][1]
            c = list(ql[i][2])
            s = add(s,f,c,is_reversed)

    return ''.join(s) if not is_reversed else ''.join(reversed(s))
    
if __name__ == '__main__':
    s = list(input())
    q = int(input())
    ql = [list(input().split()) for _ in range(q)]
    
    print(msf(s,q,ql))

Submission status: Time Limit Exceeded

Solution code:
# import bisect
# import heapq
# from copy import deepcopy
# from collections import deque
# from collections import Counter
# from itertools import accumulate
# from itertools import permutations
# import numpy as np
# import math

# n, k = map(int, input().split())
# a = list(map(int, input().split()))
s = input()
q = int(input())
rev = False
for i in range(q):
    que = input()
    if que[0] == '1':
        rev = not rev
    else:
        que = list(que.split())
        if (int(que[1])==1 and not rev) or (int(que[1])==2 and rev):
            s = que[2] + s
        else:
            s = s + que[2]

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

Submission status: Time Limit Exceeded

Solution code:
def msf(s,q,ql):
    len_ql = len(ql)
    for i in range(1,len_ql-1):
        t1 = ql[i][0]
        if t1 == "1":
            s = list(reversed(s))
        elif t1 == "2":
            f = ql[i][1]
            c = list(ql[i][2])
            if f == "1":
                s = c + s
            elif f == "2":
                s = s + c
    return s
    
if __name__ == '__main__':
    s = list(input())
    q = int(input())
    ql = [list(input().split()) for _ in range(q)]

    ql = [['0']] + ql + [['0']]
    len_ql = len(ql)

    for i in range(1,len_ql-1):
        if len_ql <= i:
            break
        elif ql[i][0] == "1" and ql[i-1][0] == "1" :
            ql = ql[0:i-1] + ql[i+1:]
            len_ql -= 2
        else:
            pass

    print(''.join(msf(s,q,ql)))

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:
# import bisect
# import heapq
# from copy import deepcopy
# from collections import deque
# from collections import Counter
# from itertools import accumulate
# from itertools import permutations
# import numpy as np
# import math

# n, k = map(int, input().split())
# a = list(map(int, input().split()))
s = input()
q = int(input())
rev = False
for i in range(q):
    que = input()
    if que[0] == '1':
        rev = not rev
    else:
        que = list(que.split())
        if (int(que[1])==1 and not rev) or (int(que[1])==2 and rev):
            s = que[2] + s
        else:
            s = s + que[2]

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

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

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

for i in range(q):
    x = input().split()
    
    if (x[0] == '1'):
        rflag += 1
        # s = s[::-1]
    elif(x[0] == '2'):
        if((rflag % 2) == 0):
            rflag = 0
        else:
            s = s[::-1]
            rflag = 0
        
        if(x[1] == '1'):
            s = x[2] + s
        elif(x[1] == '2'):
            s += x[2]
if(rflag % 2 == 0):
    pass
else:
    s = s[::-1]

print(s)

Submission status: Time Limit Exceeded

Solution code:
if __name__ == '__main__':
    s = list(input())
    q = int(input())
    #ql = [list(input().split()) for _ in range(q)]

    for _ in range(q):
        ql = list(input().split())
        t = ql[0]
        #print(br)
        if t == "1":
            s = list(reversed(s))
            #print(s)
        elif t == "2":
            f = ql[1]
            c = list(ql[2])
            if f == "1":
                s = c + s
            elif f == "2":
                s = s + c
            
    print(''.join(s))

Submission status: Time Limit Exceeded

Solution code:
def string_formation(s,q,ql):
    len_ql = len(ql)
    for i in range(len_ql):
        t = ql[i][0]
        #print(br)
        if t == "1":
            s = list(reversed(s))
            #print(s)
        elif t == "2":
            f = ql[i][1]
            c = list(ql[i][2])
            if f == "1":
                s = c + s
            elif f == "2":
                s = s + c
    return s

if __name__ == '__main__':
    s = input()
    q = int(input())
    ql = [list(input().split()) for _ in range(q)]

    ans = string_formation(list(s),q,ql)
    print(''.join(ans))

Submission status: Time Limit Exceeded

Solution code:
def flip(s):
    return list(reversed(s))

def add(s,ql):
    if ql[1] == "1":
        return list(ql[2])+s
    elif ql[1] == "2":
        return s+list(ql[2])
        
def string_formation(s,q,ql):
    for i in range(len(ql)):
        br = ql[i][0]
        #print(br)
        if br == "1":
            s = flip(s)
            #print(s)
        elif br == "2":
            s = add(s,ql[i])
            #print(s)
    return s

if __name__ == '__main__':
    s = input()
    q = int(input())
    ql = [list(input().split()) for _ in range(q)]

    #print(s)
    #print(q)
    #print(ql)
    
    ans = string_formation(list(s),q,ql)
    print(''.join(ans))

Submission status: Time Limit Exceeded

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

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

print(s)

Submission status: Time Limit Exceeded

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

def main():
    S = input().strip()
    Q = int(input())

    s = S
    for _ in range(Q):
        query = input().split()

        if query[0] == "1":
            s = s[::-1]
        elif query[0] == "2":
            F, C = query[1], query[2]
            if F == "1":
                s = C + s
            elif F == "2":
                s = s + C

    print(s)


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
S = list(input())
Q = int(input())
Query = []

for _ in range(Q):
    Query.append(input().split())
    
for query in Query:
    #print(S)
    if int(query[0]) == 1:
        S.reverse()
    elif int(query[1]) == 2:
        S.append(query[2])
    else:
        S.insert(0, query[2])
print(''.join(S))

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]
    else:
        S = q[2] + S if q[1]=='1' else S + q[2]
print(S)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
s = deque(input())
n = int(input())

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


print(''.join(list(s)))

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
flg = True
for _ in range(Q):
    query = input()
    if query[0] == "1":
        flg = not flg
    else:
        _,F,C = query.split()
        if (F == "1" and flg) or (F == "2" and not flg):
            S = C + S
        else:
            S = S + C
if not flg:
    S = S[::-1]
print(S)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

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

d = deque(list(s))
for _ in range(q):
    qi = input()
    if len(qi) == 1:
        # query 1
        d.reverse()
    else:
        # query 2
        _, f, c = qi.split()
        if f == '1':
            d.appendleft(c)
        else:
            d.append(c)

ans = ''.join(list(d))
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

s = input()
q = int(input())
is_reverse = False

d = deque(list(s))
for _ in range(q):
    qi = input()
    if len(qi) == 1:
        # query 1
        d.reverse()
    else:
        # query 2
        _, f, c = qi.split()
        if f == '1':
            d.appendleft(c)
        else:
            d.append(c)

ans = ''.join(d)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
rev = False
ans = list(S)
for i in range(Q):
    tfc = input()
    if tfc == '1':
        rev = not rev
    else:
        t,f,c = tfc.split()
        if f == '1':
            if rev: ans = ans + [c]
            else: ans = [c] + ans
        elif f == '2':
            if rev: ans = [c] + ans
            else: ans = ans + [c]

if rev: ans.reverse()

print(''.join(ans))

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
for _ in range(Q):
    query = input()
    if query[0] == "1":
        S = S[::-1]
    else:
        _,F,C = query.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())
for i in range(Q):
    N = input()
    if N == '1':
        S = S[::-1]
    else:
        if N[2] == '1':
            S = N[4] + S
        elif N[2] == '2':
            S = S + N[4]
print(S)

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin


def get_result(data):
    S, Q = data[0][0], int(data[1][0])
    for i in range(Q):
        if data[i+2][0] == '1':
            S = S[::-1]
        else:
            F, C = data[i+2][1], data[i+2][2]
            if F == '1':
                S = C + S
            else:
                S = S + C

    return S


if __name__ == '__main__':
    raw_data = [val.rstrip('\n') for val in stdin.readlines()]
    data = [list(map(str, val.split(' '))) for val in raw_data]
    result = get_result(data)
    print(result)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

s = deque(input())
q = int(input())
for _ in range(q):
    query = input()
    if query[0] == "1":
        s.reverse()
        continue
    _, f, c = query.split()
    if f == "1":
        s.appendleft(c)
    else:
        s.append(c)
print("".join(list(s)))