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())
reverse_index = -1

for i in range(Q):
    query = input()
    query = query.replace(' ', '')
    if query[0] == '1':
        reverse_index *= -1
    elif query[0] == '2':
        if query[1] == '1':
            if reverse_index == -1:
                S = query[2] + S
            else:
                S = S + query[2]
        else:
            if reverse_index == -1:
                S = S + query[2]
            else:
                S = query[2] + S

if reverse_index == 1:
    S = S[::-1]

print(S)

Submission status: Time Limit Exceeded

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

for i in range(Q):
    query = input()
    query = query.replace(' ', '')
    if query[0] == '1':
        S = S[::-1]
    elif query[0] == '2':
        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())
query = [list(input().replace(' ','')) for _ in range(q)]

r = False
for i in range(q):
    if r:
        if len(query[i]) == 1:
            r = False
        elif query[i][1] == '1':
            s = s + query[i][2]
        else:
            s = query[i][2] + s
    else:
        if len(query[i]) == 1:
            r = True
        elif query[i][1] == '1':
            s = query[i][2] + s
        else:
            s = s + query[i][2]

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

Submission status: Time Limit Exceeded

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

for q in range(1, Q+1):
    query = input()
    if query[0] == "1":
        S = "".join(reversed(S))
    else:
        T, F, C = query.split()
        if F == "1":
            S = C + S
        else:
            S += C

print(S)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
S=input()
Q=int(input())
reverse=False
for _ in range(Q):
    inp=input()
    if inp[0]=="1":
        reverse= not reverse
    elif inp[0]=="2":
        if (inp[2]=="1" and not reverse) or (inp[2]=="2" and reverse):
            S=inp[4]+S
        else:
            S=S+inp[4]
if reverse:
    S=S[::-1]
print(S)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

S = input()

result = deque(S)


def input_str(line):
    r = line.split()
    if len(r) == 1:
        return int(r[0]), None, None
    else:
        return int(r[0]), int(r[1]), r[2]


for _ in range(int(input())):
    Q, F, C = input_str(input())
    if Q == 1:
        result.reverse()
    if Q == 2:
        if F == 1:
            result.appendleft(C)
        else:
            result.append(C)

print(''.join(result))

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:
S=input()
q=int(input())
s=[]
for i in S:
    s.append(i)
t=1
for i in range(q):
    qv=list(input().split())
    if qv[0]=='1':
        t^=1
    else:
        if int(qv[1])^t==0 or int(qv[1])^t==2:
            s.insert(0,qv[2])
        else:
            s.append(qv[2])
            
if t==0:
    ss=s.copy()
    s=[]
    for j in range(len(ss)):
        s.append(ss[-1-j])
ans=''.join(s)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
S=input()
q=int(input())
s=[]
for i in S:
    s.append(i)
for i in range(q):
    qv=list(input().split())
    if qv[0]=='1':
        ss=[]
        for j in range(len(s)):
            ss.append(s[-1-j])
        s=ss
    else:
        if qv[1]=='1':
            s.insert(0,qv[2])
        else:
            s.append(qv[2])
    
ans=''.join(s)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

def deqreverse(deq):
    newdeq = deque()
    for _ in range(len(deq)):
        c = deq.pop()
        newdeq.append(c)
    
    return newdeq

S = deque(list(input()))
Q = int(input())
Query = []
for _ in range(Q):
    Query.append([x for x in input().split()])

i = 0
while i < Q:
    if i != Q-1:
        temp = Query[i+1][0]
        if Query[i][0] == '1':
            if temp != '1':
                S = deqreverse(S)
            else:
                i += 1
        else:
            if Query[i][1] == '1':
                S.appendleft(Query[i][2])
            else:
                S.append(Query[i][2])
    else:
        if Query[i][0] == '1':
            S = deqreverse(S)
        else:
            if Query[i][1] == '1':
                S.appendleft(Query[i][2])
            else:
                S.append(Query[i][2])
    i += 1
        
print("".join(S))

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

def deqreverse(deq):
    newdeq = deque()
    for _ in range(len(deq)):
        c = deq.pop()
        newdeq.append(c)
    
    return newdeq

S = deque(list(input()))
Q = int(input())

for _ in range(Q):
    Query = [x for x in input().split()]
    if Query[0] == '1':
        S = deqreverse(S)
    else:
        if Query[1] == '1':
            S.appendleft(Query[2])
        else:
            S.append(Query[2])
        
print("".join(S))

Submission status: Time Limit Exceeded

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

cnt = 0
for i in range(Q):
    q = input().split()
    if q[0] == '1':
        cnt += 1
    else:
        reverse = cnt % 2
        if q[1] == '1':
            if reverse == 0:
                s = q[2] + s
            else:
                s = s + q[2]
        else:
            if reverse == 0:
                s = s + q[2]
            else:
                s = q[2] + s

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

Submission status: Time Limit Exceeded

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

cnt = 0
for q in query:
    if q[0] == '1':
        cnt += 1
    else:
        reverse = cnt % 2
        if q[1] == '1':
            if reverse == 0:
                s = q[2] + s
            else:
                s = s + q[2]
        else:
            if reverse == 0:
                s = s + q[2]
            else:
                s = q[2] + s

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

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
t = []
f = []
c = []
for i in range(Q):
    in_ = list(input().split())
    if in_[0] == "1":
        t.append(1)
        f.append(0)
        c.append(0)
    else:
        t.append(2)
        f.append(int(in_[1]))
        c.append(in_[2])
        
acc = [0 for _ in range(Q)]
if t[-1] == 1:  acc[-1] = 1
for i in reversed(range(Q - 1)):
    if t[i] == 1:
        acc[i] = acc[i + 1] + 1
    else:
        acc[i] = acc[i + 1]
        
if acc[0] % 2 == 0:
    ans = S
else:
    ans = S[::-1]
    
for i in range(Q):
    if t[i] == 2:
        if (f[i] + acc[i]) % 2 == 1:
            ans = c[i] + ans
        else:
            ans += c[i]
print(ans)

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
t = []
f = []
c = []
for i in range(Q):
    in_ = list(input().split())
    if in_[0] == "1":
        t.append(1)
        f.append(0)
        c.append(0)
    else:
        t.append(2)
        f.append(int(in_[1]))
        c.append(in_[2])
        
acc = [0 for _ in range(Q)]
if t[-1] == 1:  acc[-1] = 1
for i in reversed(range(Q - 1)):
    if t[i] == 1:
        acc[i] = acc[i + 1] + 1
    else:
        acc[i] = acc[i + 1]
        
if acc[0] % 2 == 0:
    ans = S
else:
    ans = S[::-1]
    
for i in range(Q):
    if t[i] == 2:
        if (f[i] + acc[i]) % 2 == 1:
            ans = c[i] + ans
        else:
            ans = ans + c[i]
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

s = input()
Q = int(input())

l = deque()
l.append(s)

rev = True
for i in range(Q):
    q = list(input().split())
    if q[0] == "1":
        rev = not rev
    else:
        if rev:
            if q[1] == "1":
                l.appendleft(q[2])
            else:
                l.append(q[2])
        else:
            if q[1] == "1":
                l.append(q[2])
            else:
                l.appendleft(q[2])

ans = ""    
for i in l:
    ans += i

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

Submission status: Time Limit Exceeded

Solution code:
s=input()
q=int(input())
n=0
for i in range(q):
    l=input()
    if l[0]=='1':
        n+=1
    else:
        if (l[2]=='1' and n%2==0) or (l[2]=='2' and n%2==1):
            s=l[4]+s
        else:
            s=s+l[4]
if n%2==0:
    print(s)
else:
    print(s[::-1])

Submission status: Time Limit Exceeded

Solution code:
import itertools
import functools
import math
from collections import Counter
from itertools import combinations

def CHK():
    S=list(input())
    Q=int(input())

    rflg = 0
    for i in range(Q):
        Que = input()
        if Que == '1':
            if rflg == 0:
                rflg = 1
            else:
                rflg = 0
            #S= list(reversed(S))
        else:
            T,F,C=map(str,Que.split())
            if F == '1' and rflg == 0 or (F != '1' and rflg == 1):
                S=list(C)+S
            else:
                S.append(C)

    if rflg == 1:
        S = list(reversed(S))
    print("".join(S))

CHK()

Submission status: Time Limit Exceeded

Solution code:
import itertools
import functools
import math
from collections import Counter
from itertools import combinations


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

rflg = 0
for i in range(Q):
    Que = input()
    if Que == '1':
        if rflg == 0:
            rflg = 1
        else:
            rflg = 0
        #S= list(reversed(S))
    else:
        T,F,C=map(str,Que.split())
        if F == '1' and rflg == 0 or (F != '1' and rflg == 1):
            S=list(C)+S
        else:
            S.append(C)

if rflg == 1:
    S = list(reversed(S))
print("".join(S))

Submission status: Time Limit Exceeded

Solution code:
import itertools
import functools
import math
from collections import Counter
from itertools import combinations


S=list(input())
Q=int(input())
for i in range(Q):
    Que = input()
    if Que == '1':
        S= list(reversed(S))
    else:
        T,F,C=map(str,Que.split())
        if F == '1':
            S=list(C)+S
        else:
            S.append(C)

print("".join(S))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
S=input()
Q=int(input())
R_count=0
for i in range(Q):
  Query=list(filter(lambda a:a!=" ",list(input())))
  if Query[0]=="1":
    R_count+=1
  elif Query[0]=="2":
    if Query[1]=="1":
      if R_count%2==0:
        S=str(Query[2])+S
      else:
        S=S+str(Query[2])
    elif Query[1]=="2":
      if R_count%2==0:
        S=S+str(Query[2])
      else:
        S=str(Query[2])+S
print(S if R_count%2==0 else S[::-1])

Submission status: Time Limit Exceeded

Solution code:
S=input()
Q=int(input())
for i in range(Q):
  Query=list(filter(lambda a:a!=" ",list(input())))
  if Query[0]=="1":
    S=S[::-1]
  elif Query[0]=="2":
    if Query[1]=="1":
      S=str(Query[2])+S
    elif Query[1]=="2":
      S=S+str(Query[2])
print(S)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
from itertools import islice
alfabet=deque(input())
n=int(input())
for i in range(n):
  operation_list=list(input().split())
  if int(operation_list[0])==1:
    alfabet2=list(alfabet)
    alfabet=deque(alfabet2[::-1])
  else:
    if int(operation_list[1])==2:
      alfabet.append(operation_list[2])
    else:
      alfabet.appendleft(operation_list[2])
print(''.join(alfabet))

Submission status: Time Limit Exceeded

Solution code:
# D - String Formation
S = input()
Q = int(input())
now = 1
for _ in range(Q):
    t = list(input().split())
    if t[0]=='1':
        now = 1+(now==1)
    else:
        if int(t[1])==now:
            S = t[2] + S
        else:
            S = S + t[2]
if now==2:
    S = S[::-1]
print(S)

Submission status: Time Limit Exceeded

Solution code:
# D - String Formation
S = input()
Q = int(input())
for _ in range(Q):
    t = list(input().split())
    if t[0]=='1':
        S = S[::-1]
    else:
        if t[1]=='1':
            S = t[2] + S
        else:
            S = S + t[2]
print(S)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
S = list(input())
Q = int(input())
for i in range(Q) :
    a = list(input().split())
    if a[0] == "1" :
        S = S[::-1]
    else :
        if a[1] == "1" :
            S.insert(0,a[2])
        else :
            S.append(a[2])
print("".join(S))

Submission status: Time Limit Exceeded

Solution code:
s=input()
q=int(input())
r=1
for i in range(q):
  q2=input().split()
  if int(q2[0])==1:
    r*=-1
  else:
    if (r==1 and int(q2[1])==1) or (r==-1 and int(q2[1])==2):
        s=q2[2]+s
    else:
        s=s+q2[2]
print(s if r==1 else s[::-1])

Submission status: Time Limit Exceeded

Solution code:

s=input()
q=int(input())
n=0
for i in range(q):
    l=input().split()
    if l[0]=='1':
        n+=1
    else:
        if (l[1]=='1' and n%2==0) or (l[1]=='2' and n%2==1):
            s=l[2]+s
        else:
            s=s+l[2]
if n%2==0:
    print(s)
else:
    print(s[::-1])

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:

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

print(s)

Submission status: Time Limit Exceeded

Solution code:
s=str(input())
s=list(s)
st=["A"]
q=int(input())
ta=0
for i in range(q):
  temp=str(input())
  temp=list(temp)
  if temp[0]=="1":
    ta=ta+1
  else:
    if (temp[2]=="1" and ta%2==0) or (temp[2]=="2" and ta%2!=0):
      st.insert(0,temp[4])
    else:
      st.append(temp[4])
l=st.index("A")
ans=st[0:l]+s+st[l+1:len(st)]
if ta%2!=0:
  ans.reverse()
print("".join(ans))

Submission status: Time Limit Exceeded

Solution code:
s=str(input())
s=list(s)
q=int(input())
t=0
for i in range(q):
  temp=str(input())
  temp=list(temp)
  if temp[0]=="1":
    t=t+1
  else:
    if t%2!=0:
      s.reverse()
    t=0
    if temp[2]=="1":
      s.insert(0,temp[4])
    else:
      s.append(temp[4])
if t%2!=0:
  s.reverse()
print("".join(s))

Submission status: Time Limit Exceeded

Solution code:
s=str(input())
s=list(s)
q=int(input())
temp=0
for i in range(q):
  temp=str(input())
  temp=list(temp)
  if temp[0]=="1":
    s=s[::-1]
  else:
    if temp[2]=="1":
      s.insert(0,temp[4])
    else:
      s.append(temp[4])
print("".join(s))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
s = [i for i in input()]
q = int(input())
for i in range(q):
    recebe = input().split()
    if recebe[0] == '1':
        s.reverse()
    else:
        if recebe[1] == '1':
            s.reverse()
            s.append(recebe[2])
            s.reverse()
        else:
            s.append(recebe[2])
print("".join(map(str,s)))

Submission status: Time Limit Exceeded

Solution code:
s = [i for i in input()]
q = int(input())
for i in range(q):
    recebe = input().split()
    if recebe[0] == '1':
        s.reverse()
    else:
        if recebe[1] == '1':
            s.insert(0,recebe[2])
        else:
            s.append(recebe[2])
print("".join(map(str,s)))

Submission status: Time Limit Exceeded

Solution code:
s = [i for i in input()]
q = int(input())
for i in range(q):
    recebe = input().split()
    if recebe[0] == '1':
        s.reverse()
    else:
        if recebe[1] == '1':
            s = [recebe[2]]+s
        else:
            s.append(recebe[2])
print("".join(map(str,s)))

Submission status: Time Limit Exceeded

Solution code:
S = str(input())
Q = int(input())
t = 0

for _ in range(Q):
    A = list(input().split())
    if A[0] == "1":
        if t == 0:
            t = 1
        else:
            t = 0

    elif (A[1] == "1" and t == 0) or (A[1] == "2" and t == 1):
        S = A[2] + S
    else:
        S = S + A[2]

if t == 1:
    S = S[::-1]

print(S)

Submission status: Time Limit Exceeded

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

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

print(S)

Submission status: Time Limit Exceeded

Solution code:
S = list(input())
Q = int(input())
a = 0
for i in range(Q):
  q = list(input().split())
  q[0] = int(q[0])
  if q[0] == 1:
    if a == 0:
      a = 1
    else:
      a = 0
  else:
    q[1] = int(q[1])
    if (q[1] == 1 and a == 0) or (q[1] == 2 and a == 1):
      S = [q[2]] + S
    else:
      S = S + [q[2]]
if a == 1:
  S = S[::-1]
S = ''.join(S)
print(S)

Submission status: Time Limit Exceeded

Solution code:
S = list(input())
Q = int(input())
a = 0
for i in range(Q):
  q = list(input().split())
  if q[0] == '1':
    if a == 0:
      a = 1
    else:
      a = 0
  else:
    if (q[1] == '1' and a == 0) or (q[1] == '2' and a == 1):
      S = [q[2]] + S
    else:
      S = S + [q[2]]
if a == 1:
  S = S[::-1]
S = ''.join(S)
print(S)

Submission status: Time Limit Exceeded

Solution code:
S = list(input())
Q = int(input())
a = 0
for i in range(Q):
  q = list(input().split())
  if q[0] == '1':
    a += 1
  else:
    if (q[1] == '1' and a % 2 == 0) or (q[1] == '2' and a % 2 == 1):
      S = [q[2]] + S
    else:
      S = S + [q[2]]
if a % 2 == 1:
  S = S[::-1]
S = ''.join(S)
print(S)

Submission status: Time Limit Exceeded

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


for i in range(Q):
    if q[i][0] == "1":
        r += 1
    else:
        if (q[i][1] == "1" and r%2==0)or (q[i][1] == "2" and r%2==1 ):
            S = q[i][2] + S
        else:
            S = S + q[i][2]

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

Submission status: Time Limit Exceeded

Solution code:
S=input()
Q=int(input())
reverse=0
for q in range(Q):
    Query=list(input().split())
    T=Query[0]
    if T=="1":
        reverse+=1
    if T=="2":
        F,C=Query[1],Query[2]
        if F=="1" and reverse%2==0 or F=="2" and reverse%2==1:
            S=C+S
        else:
            S=S+C
print(S if reverse%2==0 else S[::-1])

Submission status: Time Limit Exceeded

Solution code:
S=input()
Q=int(input())
for q in range(Q):
    Query=list(input().split())
    T=Query[0]
    if T=="1":
        S=S[::-1]
    if T=="2":
        F,C=Query[1],Query[2]
        if F=="1":
            S=C+S
        if F=="2":
            S=S+C
print(S)

Submission status: Time Limit Exceeded

Solution code:
s = list(input())

q = int(input())

cnt = 0

for i in range(q):
    t = list(input().split())
    
    if t[0] == "1":
        cnt += 1
    else:
        f = t[1]
        c = t[2]
        if f == "1":
            if cnt%2 == 0:
                s.insert(0,c)
            else:
                s.append(c)
        else:
            if cnt%2 == 0:
                s.append(c)
            else:
                s.insert(0,c)
                
if cnt%2 == 1:
    s.reverse()
            
for i in s:
    print(i,end = "")

Submission status: Time Limit Exceeded

Solution code:
s = list(input())

q = int(input())

for i in range(q):
    t = list(input().split())
    
    if t[0] == "1":
        s = s[::-1]
    else:
        f = t[1]
        c = t[2]
        if f == "1":
            s.insert(0,c)
        else:
            s.append(c)
            
for i in s:
    print(i,end = "")

Submission status: Time Limit Exceeded

Solution code:
s = list(input())

q = int(input())

for i in range(q):
    t = list(input().split())
    
    if t[0] == "1":
        s.reverse()
    else:
        f = t[1]
        c = t[2]
        if f == "1":
            s.insert(0,c)
        else:
            s.append(c)
            
for i in s:
    print(i,end = "")

Submission status: Time Limit Exceeded

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

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

print(S)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

for _ in range(q):
    tmp = input()
    if len(tmp) == 1:
        s = s[::-1]
    else:
        if tmp[2] == str(2):
            s = s + tmp[-1]
        else:
            s = tmp[-1] + s

print(s)

Submission status: Time Limit Exceeded

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

QQ = []
cnt =0
flag = False
for i in range(Q):
    Q1 = list(map(str,input().split()))
    QQ.append(Q1)
    if QQ[i][0] == '1' and flag == False:
        flag = True
        cnt+=1
    elif QQ[i][0] == '1' and flag == True:
        flag = False
        cnt+=1
    elif QQ[i][1] == '1'and flag == True:
        S=S+QQ[i][2]
    elif QQ[i][1] == '1'and flag == False:
        S=QQ[i][2]+S
    elif QQ[i][1] == '2' and flag == True:
        S=QQ[i][2]+S
    elif QQ[i][1] == '2' and flag == False:
        S=S+QQ[i][2]

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

Submission status: Time Limit Exceeded

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

QQ = []
for i in range(Q):
    Q1 = list(map(str,input().split()))
    QQ.append(Q1)
    if QQ[i][0] == '1':
        S= S[::-1] 
    elif QQ[i][1] == '1':
        S=QQ[i][2] + S
    elif QQ[i][1] == '2':
        S=S+QQ[i][2] 
    
print(S)

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:

INT = lambda: int(input())
INTM = lambda: map(int,input().split())
STRM = lambda: map(str,input().split())
STR = lambda: str(input())
LIST = lambda: list(map(int,input().split()))
LISTS = lambda: list(map(str,input().split()))

def query2(s,f,c,ct):
    if ct%2==1:
        s=s[::-1]
    
    if f=='1':
        s=c+s
    else:
        s=s+c
    
    return s
    
def do():
    S=STR()
    q=INT()
    ct=0
    for i in range(q):
        qt=LISTS()
        if qt[0]=='1':
            ct+=1
        else:
            S=query2(S,qt[1],qt[2],ct)
            ct=0
    
    if ct%2==1:
        S=S[::-1]
    
    print(S)
    
if __name__ == '__main__':
    do()

Submission status: Time Limit Exceeded

Solution code:

INT = lambda: int(input())
INTM = lambda: map(int,input().split())
STRM = lambda: map(str,input().split())
STR = lambda: str(input())
LIST = lambda: list(map(int,input().split()))
LISTS = lambda: list(map(str,input().split()))

def query1(s):
    s=s[::-1]
    #print(s)
    return s
def query2(s,f,c):
    if f=='1':
        s=c+s
    else:
        s=s+c
    
    #print(s)
    return s
    
def do():
    S=STR()
    q=INT()
    for i in range(q):
        qt=LISTS()
        if qt[0]=='1':
            S=query1(S)
        else:
            S=query2(S,qt[1],qt[2])
            
    print(S)
    
if __name__ == '__main__':
    do()

Submission status: Time Limit Exceeded

Solution code:
import sys
s = str(input())
Q = int(input())
cnt = 0

for i in range(Q):
  q = list(input().split())
  if q[0] == '1':
    cnt = (cnt+1) % 2
  elif q[1] == '1':
    if cnt == 0:
      s = q[2] + s
    else:
      s += q[2]
  else:
    if cnt == 0:
      s += q[2]
    else:
      s = q[2] + s

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

Submission status: Time Limit Exceeded

Solution code:
import sys
s = str(input())
Q = int(input())
q = sys.stdin.readlines()
cnt = 0

for i in q:
  if i[0] == '1':
    cnt = (cnt+1) % 2
  elif i[2] == '1':
    if cnt == 0:
      s = i[4] + s
    else:
      s += i[4]
  else:
    if cnt == 0:
      s += i[4]
    else:
      s = i[4] + s

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

Submission status: Time Limit Exceeded

Solution code:
import sys
s = str(input())
Q = int(input())
q = sys.stdin.readlines()
for i in q:
  i = i.strip()
  if i[0] == '1':
    s = s[::-1]
  elif i[2] == '1':
    s = i[4] + s
  else:
    s += i[4]
print(s)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

rev = 0

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

Submission status: Time Limit Exceeded

Solution code:
S = input()
p = 0
head = ''
end = ''
Q = int(input())
for _ in range(Q):
  Qr = list(input().split())
  if int(Qr[0])==1:
    p = not p
  else:
    if int(Qr[1])==1:
      if p:
        end += Qr[2]
      else:
        head = Qr[2]+head
    else:
      if p:
        head = Qr[2]+head
      else:
        end += Qr[2]
S = head + S + end
if p:
  print(S[::-1])
else:
  print(S)

Submission status: Time Limit Exceeded

Solution code:
a = input()
num = int(input())
b = [input().split() for i in range(num)]
c = 1
for f in b:
  if f[0] == "1":
    c *= -1
  else:
    if f[1] == "1":
      if c < 0:
        a = a + f[2]
      else:
        a = f[2] + a
    else:
      if c < 0:
        a = f[2] + a
      else:
        a = a + f[2]
if c < 0:
  print(a[::-1])
else:
  print(a)

Submission status: Time Limit Exceeded

Solution code:
a = input()
num = int(input())
b = [input().split() for i in range(num)]
for f in b:
  if f[0] == "1":
    a = a[::-1]
  else:
    if f[1] == "1":
      a = f[2] + a
    else:
      a = a + f[2]
print(a)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
A =input()
S = deque(A)
H = 1
Q = int(input())
Qlist = []
for i in range(Q):
    op = list((map(str,input().split())))
    if len(op) ==1:
        H *= -1
    else:
        if op[1] =='1':
            if H == 1:
                S.appendleft(op[2])
            else:
                S.append(op[2]) 
        else:
            if H == 1:
                S.append(op[2])
            else:
                S.appendleft(op[2])
if H == -1:
    A = S[-1]
    for i in range(1,len(S)):
        A += S[-i-1]
else:
    A = S[0]
    for i in range(1,len(S)):
        A += S[i]
print(A)