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())
Queries = [[j for j in input().split()] for i in range(Q)]

f = 0
for query in Queries:
    if query[0] == "1":
        if f == 0:
            f = 1
        else:
            f = 0
    else:
        if f == 0:
            if query[1] == "1":
                S = query[2] + S
            else:
                S = S + query[2]
        else:
            if query[1] == "2":
                S = query[2] + S
            else:
                S = S + query[2] 
if f == 1:
    S = S[::-1]
print(S)

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8

a = input()
q = int(input())

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

Submission status: Time Limit Exceeded

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

for _ in range(q):
    a = list(input().split())
    if len(a) == 1:
        s.reverse()
    else:
        if a[1] == "1":
            s.reverse()
            s += a[-1]
            s.reverse()
        else:
            s += a[-1]
print("".join(s))

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
Query = [input().split() for a in range(Q)]
reverseFlag = True

for b in Query:
    if b[0] == '1':
        # S = S[::-1]
        # S = ''.join(list(reversed(S)))
        reverseFlag = not reverseFlag
    else:
        if reverseFlag:
            if b[1] == '1':
                S = b[2] + S
            else:
                S = S + b[2]
        else:
            if b[1] == '1':
                S = S + b[2]
            else:
                S = b[2] + S

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

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

def main():
  s = deque(input())
  q = int(input())
  query = tuple(input() for i in range(q))

  for i in range(q):
    temp = query[i].split()
    if int(temp[0]) == 1:
      s.reverse()
    else:
      if int(temp[1]) == 1:
        s.appendleft(temp[2])
      else:
        s.append(temp[2])

  print(''.join(s))

if __name__ == '__main__':
  main()

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
qs = list(input() for _ in range(Q))
for q in qs:
    q = q.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())
c=1
for i in range(q):
    qu=input().split()
    if qu[0]=="1":
        c*=-1
    else:
        if c==1:
            if qu[1]=="1":
                s=qu[2]+s
            else:
                s=s+qu[2]
        else:
            if qu[1]=="1":
                s=s+qu[2]
            else:
                s=qu[2]+s
if c==1:
    print(s)
else:
    print(s[::-1])

Submission status: Time Limit Exceeded

Solution code:
s = list(input())
q = int(input())
ans = s
cnt = 0
for i in range(q):
    f = [i for i in input().split()]
    if f[0] == '1':
        cnt += 1
    else:
        if f[1] == '1':
            if cnt % 2 == 0:
                ans.insert(0,f[2])
            else:
                ans.append(f[2])
        else:
            if cnt % 2 == 0:
                ans.append(f[2])
            else:
                ans.insert(0,f[2])
if cnt % 2 == 0:
    print(''.join(map(str,ans)))
else:
    ans2 = []
    for i in (reversed(ans)):
        ans2.append(i)
    print(''.join(map(str,ans2)))

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin
S = input()
N = int(input())
Q =  [list(map(str, stdin.readline().split())) for _ in range(N)]
flag = True
for i in range(N):
    if Q[i][0]=="1":
        flag = not(flag)                
    else: 
        if (Q[i][1] == "2") == (flag == True):
            S += Q[i][2]#先頭
        else :
            S1 = Q[i][2]+ S
            S = S1

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

Submission status: Time Limit Exceeded

Solution code:
S = input()
pre, post = [], []
Q = int(input())
reversed = False
for _ in range(Q):
  query = input().split()
  if query[0] == '1':
    reversed = not reversed
    pre, post = post[::-1], pre[::-1]
  else:  # query[0] == 2
    F, C = query[1:]
    if F == '1':
      pre = [C] + pre
    else:  # F == '2'
      post.append(C)
if reversed:
  print(''.join(pre) + S[::-1] + ''.join(post))
else:
  print(''.join(pre) + S + ''.join(post))

Submission status: Time Limit Exceeded

Solution code:
s = list(input())
q = int(input())
con = 0
for i in range(q):
    tmp_q = list(input().split())
    if(int(tmp_q[0])==1):
        #s.reverse()
        con += 1
        #print(s)
    else:
        list_tmp = []
        #print(tmp_q)
        if(int(tmp_q[1])==1 and con%2==0):
            list_tmp = list(tmp_q[2])
            #print(list_tmp)
            list_tmp.extend(s)
            s = list_tmp
            #print(s)
        elif(int(tmp_q[1])==1 and con%2==1):
            s.extend(list(tmp_q[2]))
        elif(int(tmp_q[1])==2 and con%2==1):
            list_tmp = list(tmp_q[2])
            #print(list_tmp)
            list_tmp.extend(s)
            s = list_tmp
            #print(s)
        else:
            #print(tmp_q)
            s.extend(list(tmp_q[2]))
if (con%2==1):
    s.reverse()
print(''.join(s))

Submission status: Time Limit Exceeded

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

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

print(s)

Submission status: Time Limit Exceeded

Solution code:
import sys
import numpy as np

# input = sys.stdin.readline

s = list(input())

n = int(input())

for ii in range(n):
    q = input()
#     print("".join(s))
    if q[0] == "1":
        s = s[::-1]
    else:
        t, f, c = q.split()
        if f == "1":
            s.insert(0, c)
        else:
            s.append(c)
print("".join(s))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
r_i = 0
r = False
for i in range(Q):
    q = input()
    if q[0] == "1":
        #S = S[::-1]
        if r == False:
            r = True
        else:
            r = False
        r_i += 1
    elif q[2] == "1":
        if r == False:
            S = q[4] + S
        else:
            S = S + q[4]
    else:
        if r == False:
            S = S + q[4]
        else:
            S = q[4] + S
if r_i % 2 == 1:
    S = S[::-1]
print(S)

Submission status: Time Limit Exceeded

Solution code:
s = list(input())
n = int(input())
sl = []
fl = []
flag = True
for _ in range(n):
    q = list(map(str, input().split()))
    if len(q) == 1:
        if flag:
            flag = False
        else:
            flag = True
    if len(q) == 3:
        if q[1] == "1":
            if flag:
                sl.insert(0, q[2])
            else:
                fl.append(q[2])
        else:
            if flag:
                fl.append(q[2])
            else:
                sl.insert(0, q[2])
if flag:
    s = sl+s
    s = s+fl
else:
    sl.reverse()
    fl.reverse()
    s.reverse()
    s = fl+s
    s = s+sl
print("".join(s))

Submission status: Time Limit Exceeded

Solution code:


if __name__ == '__main__':
    s = input()
    q = int(input())
    ans = s
    rev_count = 0
    alist =[]
    anslist = []

    for i in range(q):
        a = [i for i in input().split()]
        alist.append(a)
        if len(a) == 1:
            rev_count = rev_count +1

    if rev_count % 2 ==1:
        ans = ans[::-1]
    anslist.append(ans)


    for i in alist:
        if len(i) == 1:
            rev_count = rev_count -1
        else:
            if int(i[1]) == 1:
                if rev_count % 2 == 1:
                    anslist.append(i[2])
                else:
                    anslist.insert(0,i[2])

            else:
                if rev_count % 2 == 1:
                    anslist.insert(0,i[2])
                else:
                    anslist.append(i[2])

    print("".join(anslist))

Submission status: Time Limit Exceeded

Solution code:
def main():
    S=input()
    N=int(input())
    Query=[input() for _ in range(N)]
    ans=S
    flag=0

    for q in Query:
        tmp=ans
        if q=='1':
            tmp=tmp[::-1]
        elif q[0]=='2':
            if q[2]=='1':
                tmp=''.join([q[4],tmp])
            else:
                tmp=''.join([tmp,q[4]])
        ans=tmp

    print(ans)

main()

Submission status: Time Limit Exceeded

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

flag=0
for i in range(Q):
    #query[i]=input()
    b=input()

    if(len(b)==1):
        #S=S[::-1]
        flag+=1
    else:
        a=b.split(" ")
        if(flag==0):
            if(a[1]=="1"):
                S=a[2]+S
            else:
                S+=a[2]
        else:
            if(a[1]=="2"):
                S=a[2]+S
            else:
                S+=a[2]
                
    flag=flag%2

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

T = 0
F = 1
C = 2


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

query = [input().split() for l in range(q)]

# print(s,q,query)


for i in range(q):
    if query[i][T] == "2":
        # print(query[i])
        if query[i][F] == "1":
            s = query[i][C]+s
            # print(s)
        else :
            s = s+query[i][C]
    else:
        s = s[::-1]


print(s)

Submission status: Time Limit Exceeded

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

flag = False
for i in range(q):
    if Q[i][0] == '1':
        flag = not flag
    elif (Q[i][1] == '1' and flag) or (Q[i][1] == '2' and not flag):
        S += Q[i][2]
    else:
        S = Q[i][2] + S

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

Submission status: Time Limit Exceeded

Solution code:
s = list(input())
q = int(input())
flag = True
ans = s

for i in range(q):
  query = list(input().split())
  if int(query[0]) == 1:
    flag = not flag
  else:
    if int(query[1]) == 1:
      if flag:
        ans.insert(0, query[2])
      else:
        ans.append(query[2])
    else:
      if flag:
        ans.append(query[2])
      else:
        ans.insert(0, query[2])

if flag:
  print("".join(ans))
else:
  t = ans[::-1]
  print("".join(t))

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

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


for _ in range(Q):
    i = input()
    if len(i) >= 2:
        i, F, C = i.split()
    if int(i) == 1:
        d.reverse()
    else:
        if int(F) == 1:
            d.appendleft(C)
        else:
            d.append(C)

for i in d:
    print(i, end='')
print()

Submission status: Time Limit Exceeded

Solution code:
*s, = input()
_q = int(input())
rev = 0
l, r = [], []
for _ in range(_q):
    *q, = map(str, input().split())

    if q[0] == "1":
        rev += 1
        continue

    t = q[0]
    f = q[1]
    c = q[2]

    if rev%2 == 0:
        if f == "1":
            l.insert(0, c)
        else:
            r.append(c)
    else:
        if f == "1":
            r.append(c)
        else:
            l.insert(0, c)

s = l + s + r

if rev%2 == 1:
    *s, = reversed(s)

print("".join(s))

Submission status: Time Limit Exceeded

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

def aaa(s,q):
    for i in range(q):
    #    a = list(map(str,input().split()))
        a = str(input())
        if a[0] == "2":
            if a[2] == "1":
                s = str(a[4]) + s
            else:
                s = s + str(a[4])
        else:
            s = s[::-1]
    return s

s = aaa(s,q)

print(s)

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin
import math

s = stdin.readline().rstrip()
n = int(stdin.readline().rstrip())

flag = True
for i in range(n):
    q = stdin.readline().rstrip().split()
    if int(q[0]) == 1:
        #s = s[::-1]
        if flag:
            flag = False
        else:
            flag = True
    if int(q[0]) == 2:
        if int(q[1]) == 1:
            if flag:
                s = q[2] + s
            else:
                s = s + q[2]
        if int(q[1]) == 2:
            if flag:
                s = s + q[2]
            else:
                s = q[2] + s

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

Submission status: Time Limit Exceeded

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


for i in range(Q):
    Qq=input()
    
    if(Qq.count(' ')>0):
        ti,fi,ci=Qq.split()
        if(fi=='1'):
            S=str(ci)+S
        else:
            S=S+str(ci)
    else:
        S=S[::-1]
print(S)

Submission status: Time Limit Exceeded

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



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


print(s)

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
queries = []
for _ in range(Q):
  q = list(input().split()) # 文字列のリスト
  queries.append(q)

r_count = 0
for query in queries:
  t = query[0]
  if (t == '1'):
    r_count += 1
  elif (t == '2'):
    f = query[1]
    new_S = ""
    if(f == '1' and (r_count % 2 == 0)):
      new_S = query[2] + S
    elif(f == '1' and (r_count % 2 == 1)):
      new_S = S + query[2]
    elif(f == '2' and (r_count % 2 == 0)):
      new_S = S + query[2]
    elif(f == '2' and (r_count % 2 == 1)):
      new_S = query[2] + S
    S = new_S

if (r_count % 2 == 1):
  for i, s in enumerate(reversed(S)):
    if (i == len(S) - 1):
      print(s)
    else:
      print(s, end="")
else:
  print(S)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
#D - String Formation
S = list(str(input()))
Q = int(input())
q = [input().split() for i in range(Q)]

#print(S)
#print(q)

for i in range(Q):
    tmp1 = ""
    if q[i][0] == '1':
        S = S[::-1]
       # tmp1 = S[0]
       # S[0] = S[-1]
       # S[-1] = tmp1
    else:
        if q[i][1] == "1":
            S.insert(0,q[i][2])
        else:
            S.append(q[i][2])
ans = ''.join(S)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin
from collections import deque
s=list(str(input()))
s=deque(s)
q=int(stdin.readline().rstrip())
cnt=0
for i in range(q):
    que=list(input().split())
    if len(que)==1:
        cnt+=1
    else:
        if cnt%2!=0:
            s.reverse()
        if int(que[1])==1:
            s=deque(s)
            s.appendleft(que[2])
            cnt=0
        elif int(que[1])==2:
            s=deque(s)
            s.append(que[2])
            cnt=0
if cnt%2!=0:
    s.reverse()
print(''.join(s))

Submission status: Time Limit Exceeded

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

rev = True

for _ in range(q):
    t = [x for x in input().split()]
    if t[0] == "1":
        if rev:
            rev = False
        else:
            rev = True
    else:
        if (t[1] == "1" and rev) or (t[1] == "2" and not rev):
            s = "".join([t[2],s])
        else:
            s = "".join([s,t[2]])

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

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
import copy

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

d = deque(['S'])

#d = ['S']

rev = 0

for i in range(q):
    if query[i][0] == '2':
        if query[i][1] == '1':
            d.appendleft(query[i][2])
        else:
            d.append(query[i][2])
    else:
        d.reverse()
        rev += 1

#ans = deque()

if rev % 2 == 1:
    s = ''.join(list(reversed(s)))

idx = 0

copy_d = copy.deepcopy(d)

while len(copy_d):
    if copy_d.popleft() == 'S':
        break
    idx += 1

d[idx] = s

print(*d,sep='')

Submission status: Time Limit Exceeded

Solution code:
def input_int_list():
    return [int(thing) for thing in input().split(" ")]


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


print("".join(S))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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


def main():
    s = input().strip()
    q = int(input())
    queries = [input().split() for _ in range(q)]
    for query in queries:
        if query[0] == "1":
            s = s[::-1]
            continue
        if query[1] == "1":
            s = "".join(query[2] + s)
        else:
            s = "".join(s + query[2])
    print(s)


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
s = list(input())
n = int(input())
p = []
for i in range(n):
  p.append(input().split())
  
#print(p)
count = 0  
#while count <= n:
for i in p:
 # print(i)
  if i[0] == '1':
    s = s[::-1]
  elif i[0] =='2':
    if i[1] == '1':
      s.insert(0,i[2])
    else:
      s.append(i[2])
#  count +=1      
      
#print(s)      
      
s = ''.join(s)
print(s)

Submission status: Time Limit Exceeded

Solution code:
S = input()

Q = int(input())
temp = S
for i in range(Q):
    try:
        n, f,c = input().split()
        if int(f) == 1:
            temp = c+temp
        else:
            temp = temp+c
    except ValueError:
        temp = temp[::-1]
    #print(temp)

print(temp)

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
# Your code here!

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

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

Submission status: Time Limit Exceeded

Solution code:
S = list(input())
Q = int(input())
rev = 0
for i in range(Q):
  query = input()
  if query == "1": rev = 1 - rev 
  elif int(query[2]) + rev == 2: S.append(query[4])
  else:  S.insert(0,query[4])
if rev == 1: S.reverse()  
print("".join(S))

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
ls = []
count = 0
for i in range(Q):
    ls.append(input())

for i in range(len(ls)):
    if ls[i] == '1':
        try:
            if ls[i+1] == '1':
                count += 1
            else:
                if count == 0:
                    S = S[::-1]
                else:
                    if count %2 == 1:
                        count = 0
                    else:
                        S = S[::-1]
                        count = 0
        except IndexError:
            if count == 0:
                S = S[::-1]
            else:
                if count % 2 == 1:
                    count = 0
                else:
                    S = S[::-1]
                    count = 0

    else:
        ls[i] = [x for x in ls[i]]
        if ls[i][2] == '1':
            S = ls[i][4] + S
        else:
            S = S + ls[i][4]

print(S)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

def add_s(s, f, c):
  if f == '1':
    # 先頭
    s = ''.join((c, s))
  else:
    # 末尾
    s = ''.join((s, c))
  return s

for _ in range(Q):
  q = input()
  if q == '1':
    reverse_flag = not reverse_flag
    continue
  _, f, c = q.split()
  #print(reverse_flag, f, c)
  if reverse_flag:
    S = add_s(S, '1' if f == '2' else '2', c)
  else:
    S = add_s(S, f, c)
  #print('---', S)

if reverse_flag:
  S = S[::-1]
print(S)

Submission status: Time Limit Exceeded

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

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

    if X[0] == '1':
        S.reverse()
    else:
        if X[1] =='1':
            S.insert(0,X[2])
        else:
            S.append(X[2])
S = ''.join(S)
print(S)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

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

fc = []
for i in range(q):
    t, *f = map(str, input().split())
    if t == "1":
        s.reverse()
    else:
        if f[0] == "1":
            s.appendleft(f[1])
        else:
            s.append(f[1])

print("".join(s))

Submission status: Time Limit Exceeded

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

#solve
ans = S
skip = False
for i in range(Q):
  if i != Q-1:
    if skip == True:
      skip == false
      continue
    elif int(Query[i][0]) == 0 and int(Query[i+1][0]) == 0:
      skip = True
      continue
  
  if int(Query[i][0]) == 1:
    ans = ans[::-1]
  if int(Query[i][0]) == 2:
    if int(Query[i][1]) == 1:
      ans = Query[i][2] + ans
    if int(Query[i][1]) == 2:
      ans += Query[i][2]
print(ans)

Submission status: Time Limit Exceeded

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

process_list = []

for _ in range(0, count):
  process_list.append(input())
  
for elm in process_list:
  process = elm.split()
  if process[0] == '1':
    S = S[::-1]
  elif process[0] == '2':
    if process[1] == '1':
      S = process[2] + S
    elif process[1] == '2':
      S = S + process[2]

print(S)

Submission status: Time Limit Exceeded

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

for i in range(q):

    if a[i]=="1":
        s = ''.join(list(reversed(s)))
    
    else:

        if a[i][2]=="1":
            s=a[i][4]+s

        else:
            s=s+a[i][4]


print(s)

Submission status: Time Limit Exceeded

Solution code:
s=input()
q=int(input())
co=0
for i in range(q):
	a=input()
	if a[0]=="1":
#		s=s[::-1]
		co+=1
	elif a[2]=="1":
		if co%2 == 1:
			s=s[::-1]
			co=0
		s=a[4]+s
	else:
		if co%2==1:
			s=s[::-1]
			co=0
		s=s+a[4]
if co%2==1:
	s=s[::-1]
print(s)

Submission status: Time Limit Exceeded

Solution code:
import sys
a = sys.stdin.read().split('\n')[:-1]
s = list(a[0])
q = int(a[1])
f = 1
for i in range(2, q + 2):
    if a[i][0] == '1':
        f = 1 - f
    else:
        if a[i][2] == '21'[f]:
            s.insert(0, a[i][4])
        else:
            s.append(a[i][4])
if f == 1:
    print(''.join(s))
else:
    print(''.join(reversed(s)))

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
Q_list = list()
for i in range(Q):
    Q_list.append(input().split())

def operate(i):
    global S_list, count
    if Q_list[i][0] == '1':
        count += 1
    else:
        if count % 2 == 0:
            if Q_list[i][1] == '1':
                S_list = [Q_list[i][2]] + S_list
            else:
                S_list = S_list + [Q_list[i][2]]
        else:
            if Q_list[i][1] == '1':
                S_list = S_list + [Q_list[i][2]]
            else:
                S_list = [Q_list[i][2]] + S_list
 
S_list = [i for i in S]
check = 0
for i in range(Q):
    if Q_list[i][0] == '1':
        check += 1
if check % 2 == 1:
    S_list = list(reversed(S_list))

count = check % 2
for i in range(Q):
    operate(i)
print(''.join(S_list))

Submission status: Time Limit Exceeded

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

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

print(s)

Submission status: Time Limit Exceeded

Solution code:
N= input()
M= int(input())
for _ in range(M):
    a = list(map(str, input().split()))
    if (a[0]=='1'):
        N = N[::-1]
    else:
        if (a[1]=='1'):
            N = a[2]+N
        else:
            N = N+a[2]
print(N)

Submission status: Time Limit Exceeded

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

flag = True

for i in range(Q):
    q = list(map(str, input().split()))
    if len(q) == 1:
        flag = not flag
    else:
        if (q[1] == '1' and flag == True) or (q[1] == '2' and flag == False):
            S.insert(0, q[2])
        else:
            S.extend([q[2]])

if flag == True:
    print(*S, sep="")
else:
    S.reverse()
    print(*S, sep="")

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
bool = True
for i in range(0, Q):
	s = input()
	if len(s) == 1:
		bool = not bool
	else:
		if s[2] == "1":
			if bool:
				S = s[-1] + S
			else:
				S += s[-1]
		else:
			if bool:
				S += s[-1]
			else:
				S = s[-1] + S
if bool:
	print(S)
else:
	print(S[::-1])

Submission status: Time Limit Exceeded

Solution code:
s=input()
q=int(input())
Q=[]
for i in range(0,q):
    L=input()
    if len(L) != 1: 
        Q.append(list(L.split(' ')))
    else:
        Q.append(1)

re=False
for i in range(0,q):
    if Q[i]==1:
        re=not re
        continue
    if int(Q[i][0])==2:
        if (int(Q[i][1])==1 and (not re)) or (int(Q[i][1])==2 and re):
            s=Q[i][2]+s
        if (int(Q[i][1])==1 and re) or (int(Q[i][1])==2 and (not re)):
            s=s+Q[i][2]
if re==True:
    s=s[::-1]
print(s)

Submission status: Time Limit Exceeded

Solution code:
s = input()
n = int(input())
q = [list(input().split()) for _ in range(n)]
for qi in q:
    if qi[0] == "1":
        s = s[::-1]
       # print(s)
    if len(qi) != 1:
        if qi[1] == '1':
            s = s[::-1] + qi[2]
            s = s[::-1]
            #print(s)
        elif qi[1] == "2":
            s += qi[2]
            #print(s)
print(s)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

def main():
  s = deque(input())
  q = int(input())
  query = [input() for i in range(q)]

  for i in range(q):
    temp = query[i].split()
    if int(temp[0]) == 1:
      s.reverse()
    else:
      if int(temp[1]) == 1:
        s.appendleft(temp[2])
      else:
        s.append(temp[2])

  print(''.join(s))

if __name__ == '__main__':
  main()

Submission status: Time Limit Exceeded

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

counter = 0
for i in range(q):
    x = input().split()

    if str(x[0]) == '1':
        counter += 1

    else:
        if counter % 2 != 0:
            s = s[::-1]
            counter = 0
        if str(x[1]) == '1':
            s = str(x[2]) + s

        else:
            s = s + str(x[2])

if counter % 2 != 0:
    s = s[::-1]
print(s)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
def main():
  s=input()
  q=int(input())
  for _ in [0]*q:
      query = input()
      try:
          t = int(query)
          s = s[::-1]
      except ValueError:
          t,f,c = map(str,query.split())
          if f == "1":
              s = c + s
          if f == "2":
              s = s + c
  print(s)
main()