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())
querys = [list(input().split()) for i in range(q)]

rev = 0
moji = []
for query in querys:
    if query[0] == '1':
        rev += 1
    else:
        query[1] = int(query[1])
        p = True if query[1] == 1 else False
        moji.append([query[2], rev, p])

if rev%2 != 0:
    s = s[::-1]

for m in moji:
    if (rev - m[1]) % 2 != 0:
        m[2] = not(m[2])
    
    if m[2]:
        s = m[0] + s
    else:
        s = s + m[0]

print(s)

Submission status: Time Limit Exceeded

Solution code:
import sys
import itertools
# import numpy as np
import time
import math

sys.setrecursionlimit(10 ** 7)

from collections import defaultdict

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

s = readline().decode().strip()
q = int(readline())
is_reversed = False
qs = read().decode().split("\n")
left = ""
right = ""
for i in range(q):
    query = qs[i].split()
    t = int(query[0])
    if t == 1:
        is_reversed = not is_reversed
    elif t == 2:
        f = int(query[1])
        c = query[2]
        if f == 1:
            if is_reversed:
                right += c
            else:
                left += c
        elif f == 2:
            if is_reversed:
                left += c
            else:
                right += c
s = left[::-1] + s + right
if is_reversed:
    print(s[::-1])
else:
    print(s)

Submission status: Time Limit Exceeded

Solution code:
S = str(input())
Q = int(input())
reverse = 0
ans = [S]
for _ in range(Q):
    q = str(input()).split()
    if len(q) == 1:
        reverse += 1
    if len(q) == 3:
        f = q[1]
        c = q[2]
        if reverse % 2 == 0:
            if f == '1':
                ans = [c] + ans
            else:
                ans.append(c)
        else:
            if f == '1':
                ans.append(c)
            else:
                ans = [c] + ans

if reverse % 2 == 0:
    print(''.join(ans))
else:
    print(''.join(ans)[::-1])

Submission status: Time Limit Exceeded

Solution code:
import sys
import itertools
# import numpy as np
import time
import math

sys.setrecursionlimit(10 ** 7)

from collections import defaultdict

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

s = readline().decode().strip()
q = int(readline())
is_reversed = False
qs = read().decode().split("\n")
for i in range(q):
    query = qs[i].split()
    t = int(query[0])
    if t == 1:
        is_reversed = not is_reversed
    elif t == 2:
        f = int(query[1])
        c = query[2]
        if f == 1:
            if is_reversed:
                s += c
            else:
                s = c + s
        elif f == 2:
            if is_reversed:
                s = c + s
            else:
                s += c
if is_reversed:
    print(s[::-1])
else:
    print(s)

Submission status: Time Limit Exceeded

Solution code:
S = str(input())
Q = int(input())
reverse = 0
ans = S
for _ in range(Q):
    q = str(input()).split()
    if len(q) == 1:
        reverse += 1
    if len(q) == 3:
        f = q[1]
        c = q[2]
        if reverse % 2 == 0:
            if f == '1':
                ans = c + ans
            else:
                ans += c
        else:
            if f == '1':
                ans += c
            else:
                ans = c + ans

if reverse % 2 == 0:
    print(ans)
else:
    print(ans[::-1])

Submission status: Time Limit Exceeded

Solution code:
import sys
import itertools
# import numpy as np
import time
import math

sys.setrecursionlimit(10 ** 7)

from collections import defaultdict

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

s = readline().decode().strip()
q = int(readline())
is_reversed = False
for i in range(q):
    query = readline().decode().strip().split()
    t = int(query[0])
    if t == 1:
        is_reversed = not is_reversed
    elif t == 2:
        f = int(query[1])
        c = query[2]
        if f == 1:
            if is_reversed:
                s += c
            else:
                s = c + s
        elif f == 2:
            if is_reversed:
                s = c + s
            else:
                s += c
if is_reversed:
    print(s[::-1])
else:
    print(s)

Submission status: Time Limit Exceeded

Solution code:
import sys
import itertools
# import numpy as np
import time
import math

sys.setrecursionlimit(10 ** 7)

from collections import defaultdict

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

s = input()
q = int(input())
is_reversed = False
for i in range(q):
    query = input().split()
    t = int(query[0])
    if t == 1:
        is_reversed = not is_reversed
    elif t == 2:
        f = int(query[1])
        c = query[2]
        if f == 1:
            if is_reversed:
                s += c
            else:
                s = c + s
        elif f == 2:
            if is_reversed:
                s = c + s
            else:
                s += c
if is_reversed:
    print(s[::-1])
else:
    print(s)

Submission status: Time Limit Exceeded

Solution code:
import sys
import itertools
# import numpy as np
import time
import math

sys.setrecursionlimit(10 ** 7)

from collections import defaultdict

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

s = input()
q = int(input())
for i in range(q):
    query = input().split()
    t = int(query[0])
    if t == 1:
        s = s[::-1]
    elif t == 2:
        f = int(query[1])
        c = query[2]
        if f == 1:
            s = c + s
        elif f == 2:
            s += c
print(s)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
s=deque(input())
q=int(input())
for i in range(q):
    t=list(map(str,input().split()))
    if int(t[0])==1:
        s.reverse()
        #print(s)
    else:
        if int(t[1])==1:
            s.appendleft(t[2])
        else:
            s.append(t[2])
for k in s:
    print(k,end='')

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

S=input()
N=int(input())
inilen=len(S)
d=deque()
for i in range(inilen):
    d.append(S[i])

for i in range(N):
    L=list(map(str,input().split()))
    if L[0]=="1":
        d.reverse()
    elif L[1]=="1":
        d.appendleft(L[2])
        
    elif L[1]=="2":
        d.append(L[2])

Z=list(d)
Z=[str(a) for a in Z]
Z="".join(Z)
print(Z)

Submission status: Time Limit Exceeded

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

def main():
    ans =input().rstrip()
    n = int(input())
    li = [input().split() for i in range(n)]
    a = 0 #先頭がどっちかという情報

    for x in li:
        if x[0] == "1": #反転クエリ
            a = (a+1)%2
        else: #追加クエリ
            if (x[1] == "1" and a == 0) or (x[1] == "2" and a == 1):
                ans = x[2] + ans
            else:
                ans += x[2]

    if a == 0: #先頭が前なら
        print(ans)
    else: #先頭が後なら
        print(ans[::-1])

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

def main():
    s = input().rstrip()
    n = int(input())
    li = [input().split() for i in range(n)]
    a = 0 #先頭がどっちかという情報

    for x in li:
        if x[0] == "1":
            if a == 0:
                a = 1
            else:
                a = 0
        else:
            if (x[1] == "1" and a == 0) or (x[1] == "2" and a == 1):
                s = x[2] + s
            else:
                s = s + x[2]

    if a == 0: #先頭が前なら
        print(s)
    else: #先頭が後なら
        print(s[::-1])

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

for i in range(times):
    Q =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 = str(input())
Q = int(input())
List = [list(map(str, input().split()))for i in range(Q)]
#print(List)

#S='a'
#Q=4
#List=[['2', '1', 'p'], ['1'], ['2', '2', 'c'], ['1']]
han=0
for a in List:
    if a[0]=='1':
        han+=1
    if a[0]=='2':
        if a[1]=='1' and han%2==0 :
            S=a[2]+S
        if a[1]=='2' and han%2==0:
            S=S+a[2]
        if a[1]=='1' and han%2==1:
            S=S+a[2]
        if a[1]=='2' and han%2==1:
            S=a[2]+S
if han%2==0:
    print(S)
else:
    print(S[::-1])

Submission status: Time Limit Exceeded

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

#S='a'
#Q=4
#List=[['2', '1', 'p'], ['1'], ['2', '2', 'c'], ['1']]

for a in List:
    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 = input()
q = int(input())
rev = 0
for i in range(q):
    query = list(map(str, input().split()))
    if len(query) == 1:
        rev = (rev + 1) % 2
    else:
        if rev == 0:
            if int(query[1]) == 1:
                s = query[2] + s
            else:
                s = s + query[2]
        else:
            if int(query[1]) == 2:
                s = query[2] + s
            else:
                s = s + query[2]
if rev == 1:
    s = s[::-1]
print(s)

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
query = []
for i in range(q):
    query.append(list(map(str, input().split())))
i = 0
rev = 0
while i < q:
    if len(query[i]) == 1:
        if i < q - 1:
            if len(query[i + 1]) == 1:
                i += 2
                continue
        rev = (rev + 1) % 2
        i += 1
    else:
        if rev == 0:
            if int(query[i][1]) == 1:
                s = query[i][2] + s
                i += 1
            else:
                s = s + query[i][2]
                i += 1
        else:
            if int(query[i][1]) == 2:
                s = query[i][2] + s
                i += 1
            else:
                s = s + query[i][2]
                i += 1
if rev == 1:
    s = s[::-1]
print(s)

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
query = []
for i in range(q):
    query.append(list(map(str, input().split())))
i = 0
while i < q:
    if len(query[i]) == 1:
        if i < q - 1:
            if len(query[i + 1]) == 1:
                i += 2
                continue
        s = s[::-1]
        i += 1
    else:
        if int(query[i][1]) == 1:
            s = query[i][2] + s
            i += 1
        else:
            s = s + query[i][2]
            i += 1
print(s)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

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

for i in range(q):
  a = list(input().split())
  if a[0] == '1':
    s.reverse()
  elif a[0] == '2':
    if a[1] == '1':
      s.appendleft(a[2])
    elif a[1] == '2':
      s.append(a[2])
      
print(''.join(s))

Submission status: Time Limit Exceeded

Solution code:

s=input()
q=int(input())
p=0
for i in range(q):
    l=list(map(str,input().split()))
    if l[0]=="1":
        p=(p+1)%2
    else:
        if p==0:
            if l[1]=="1":
                s=l[2]+s
            else:
                s=s+l[2]
        else:
            if l[1]=="1":
                s+=l[2]
            else:
                s=l[2]+s

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

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
s=input()
ss=[]
for i in range(len(s)):
    ss.append(s[i])

    
arr=np.array(ss)

q=int(input())
for i in range(q):
    l=list(map(str,input().split()))
    if l[0]=="1":
        arr=arr[::-1]
        
    else:
        if l[1]=="1":
            arr=arr[::-1]
            arr=np.append(arr,l[2])
            arr=arr[::-1]
        if l[1]=="2":
            arr=np.append(arr,[l[2]])
        

a=arr.tolist()
for i in a:
    print(i,end="")

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
s=input()

ss=[]
for i in range(len(s)):
    ss.append(s[i])
arr=np.array(ss)

q=int(input())
for i in range(q):
    l=list(map(str,input().split()))
    if l[0]=="1":
        arr=arr[::-1]
        
    else:
        if l[1]=="1":
            arr=arr[::-1]
            arr=np.hstack((arr,l[2]))
            arr=arr[::-1]
        if l[1]=="2":
            arr=np.hstack((arr,[l[2]]))
        

a=arr.tolist()
for i in a:
    print(i,end="")

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
s=input()

ss=[]
for i in range(len(s)):
    ss.append(s[i])
arr=np.array(ss)

q=int(input())
for i in range(q):
    l=list(map(str,input().split()))
    if l[0]=="1":
        arr=arr[::-1]
        
    else:
        if l[1]=="1":
            arr=np.hstack((l[2],arr))
        if l[1]=="2":
            arr=np.hstack((arr,[l[2]]))
        

a=arr.tolist()
for i in a:
    print(i,end="")

Submission status: Time Limit Exceeded

Solution code:
# from decorator import stop_watch
# 
# 
# @stop_watch
def solve(S, Q, Qs):
    rev_flg = False
    ans = S
    for q in Qs:
        if q[0] == 1:
            rev_flg = not rev_flg
        else:
            bf = q[1]
            if (bf == 1 and not rev_flg) or (bf == 2 and rev_flg):
                ans = q[2] + ans
            else:
                ans = ans + q[2]
    if rev_flg:
        ans = ans[::-1]
    print(ans)


if __name__ == '__main__':
    S = input()
    Q = int(input())
    Qs = []
    for _ in range(Q):
        s = input()
        if s[0] == '1':
            q = [1]
        else:
            q = s.split()
            q[0] = int(q[0])
            q[1] = int(q[1])

        Qs.append(q)
    solve(S, Q, Qs)

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
f = ""
b = ""
forward = True
for _ in range(q):
    que = list(input().split())
    if int(que[0]) == 1:
        if forward:
            forward = False
        else:
            forward = True
    else:
        if (int(que[1]) == 1 and forward) or (int(que[1]) == 2 and not forward):
            f+=que[2]
        else:
            b+=que[2]

if forward:
    ff = ""
    for i in reversed(range(len(f))):
        ff += f[i]
    
    print(ff+s+b)
else:
    bb = ""
    for i in reversed(range(len(b))):
        bb += b[i]
    ss = ""
    for i in reversed(range(len(s))):
        ss += s[i]
    print(bb+ss+f)

Submission status: Time Limit Exceeded

Solution code:
s = [input()]
q = int(input())
forward = True
for _ in range(q):
    que = list(input().split())
    if int(que[0]) == 1:
        if forward:
            forward = False
        else:
            forward = True
    else:
        if (int(que[1]) == 1 and forward) or (int(que[1]) == 2 and not forward):
            s = [que[2]] + s
        else:
            s.append(que[2])
if forward:
    print("".join(s))
else:
    ss = "".join(s)
    ans = ""
    for i in reversed(range(len(ss))):
        ans += ss[i]
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
s = list(input())
q = int(input())
forward = True
for _ in range(q):
    que = list(input().split())
    if int(que[0]) == 1:
        if forward:
            forward = False
        else:
            forward = True
    else:
        if (int(que[1]) == 1 and forward) or (int(que[1]) == 2 and not forward):
            s = [que[2]] + s
        else:
            s .append(que[2])
if forward:
    print("".join(s))
else:
    print("".join(reversed(s)))

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
def main():
  S=deque(input())
  n=int(input())
  for i in range(n):
    q=input()
    if q=="1":
      S.reverse()
    else:
      a,b,c=q.split()
      if b=="1":
        S.appendleft(c)
      else:
        S.append(c)
  print("".join(list(S)))
if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
def main():
  S=deque(input())
  S_append = S.append
  S_appendleft = S.appendleft
  S_reverse = S.reverse
  n=int(input())
  for i in range(n):
    q=input()
    if q=="1":
      S_reverse()
    else:
      a,b,c=q.split()
      if b=="1":
        S_appendleft(c)
      else:
        S_append(c)
  print("".join(list(S)))
if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
deque_array=deque(input())
n=int(input())
for i in range(n):
  q=input()
  if q=="1":
    deque_array.reverse()
  else:
    a,b,c=q.split()
    if b=="1":
      deque_array.appendleft(c)
    else:
      deque_array.append(c)
print("".join(list(deque_array)))

Submission status: Time Limit Exceeded

Solution code:
S=list(input())
n=int(input())
for i in range(n):
  q=input()
  if q=="1":
    S.reverse()
  else:
    a,b,c=q.split()
    if b=="1":
      S.insert(0, c)
    else:
      S.append(c)
print("".join(S))

Submission status: Time Limit Exceeded

Solution code:
S=input()
N=int(input())
for i in range(N):
  A=list(map(str,input().split()))
  if A[0]=='1':
     S=S[::-1]
  else:
     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:
from collections import deque
def main():
    S = input()
    Q = int(input())
    ans = deque(S)

    for q in range(Q):
        T, *arg = input().split()
        T = int(T)

        if T == 1:
            ans.reverse()
        else:
            if int(arg[0]) == 1:
                ans.appendleft(arg[1])
            else:
                ans.append(arg[1])

    print("".join(ans))
if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
S = input()
N = int(input())
ls = [[i for i in input().split()] for j in range(N)]

for l in ls:
    if l[0]=="1": # 反転
        S = S[::-1]
    elif l[0]=="2":
        if l[1] == "1":
            S = l[2] + S
        elif l[1] == "2":
            S = S + l[2]

print(S)

Submission status: Time Limit Exceeded

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

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

print(S)

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
rev = 1
for _ in range(Q):
    op = input().split(" ")
    if int(op[0]) == 1:
        rev *= -1
    else:
        f, ch = op[1:]
        if rev == 1:
            if f == "1":
                S = ch + S
            else:
                S = S + ch
        else:
            if f == "1":
                S = S + ch
            else:
                S = ch + S
if rev == -1:
    S = S[::-1]
print(S)

Submission status: Time Limit Exceeded

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

for _ in range(Q):
    op = input().split(" ")
    if int(op[0]) == 1:
        S = S[::-1]
    else:
        f, ch = op[1:]
        if f == "1":
            S = ch + S
        else:
            S = S + ch

print(S)

Submission status: Time Limit Exceeded

Solution code:
import sys


def input(): return sys.stdin.readline().strip()
def I(): return int(input())
def LI(): return list(map(int, input().split()))
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def S(): return input()
def LS(): return input().split()


s = S()
q = I()
queries = SR(q)
for query in queries:
    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 _ in range(q):
    a=[x for x in input().split()]
    if len(a)==1:
        s=s[::-1]
    else:
        if a[1]=='1':
            s=a[2]+s
        else:
            s+=a[2]

print(s)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
t=list(input())
q=int(input())
query=deque([])
for i in range(q):
  s=list(map(str,input().split()))
  query.append(s)
#print(query)
while query:
  p=query.popleft()
  if len(p)==1:
    t.reverse()
    
  else:
    if p[1]==str(1):
      t.insert(0,p[2])
      
    else:
      t.append(p[2])
      
print(''.join(t))

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
t=list(input())
q=int(input())
query=deque([])
for i in range(q):
  s=list(map(str,input().split()))
  query.append(s)
#print(query)
while query:
  p=query.popleft()
  if len(p)==1:
    t.reverse()
    
  else:
    if p[1]==str(1):
      t.insert(0,p[2])
      
    else:
      t.append(p[2])
      
print(''.join(t))

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
S = deque(input())
Q = int(input())
#W, H, N = map(int, input().split())
lst = []
for i in range(Q):
    lst.append(list(input().split()))
#print(lst)

for i in range(Q):
    if lst[i][0] == '1':
        S.reverse()
    else:
        if lst[i][1] == '1':
            S.appendleft(lst[i][2])
        else:
            S.append(lst[i][2])

print(''.join(S))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

for i in range(Q):
    if query[i][0] == "1":
        S.reverse()
    else:
        if query[i][1] == "1":
            S.insert(0, query[i][2])
        else:
            S.append(query[i][2])

print("".join(S))

Submission status: Time Limit Exceeded

Solution code:
S = list(input())
Q = int(input())
Flag = True
for _ in range(Q):
    q = input().split()
    if len(q) == 1:
        Flag = False if Flag else True
    else:
        if Flag:
            if q[1] == '1':
                S = [q[2]] + S
            else:
                S = S + [q[2]]
        else:
            if q[1] == '1':
                S = S + [q[2]]
            else:
                S = [q[2]] + S
if Flag:
    print(''.join(S))
else:
    print(''.join(reversed(S)))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
x = 0
for i in range(Q):
    A = list(input().split())
    if A[0] == '1':
        x += 1
    elif x%2 ==0:
        if A[1] == '1':
            S = A[2] + S
        else:
            S = S + A[2]
    else:
        if A[1] == '1':
            S = S + A[2] 
        else:
            S = A[2] + S
if x%2 == 1:
    S = ''.join(reversed(list(S)))
print(S)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
TFC = [input().split() for _ in range(Q)]
is_reversed = False
for tfc in TFC:
    if tfc[0] == '1':
        is_reversed = not is_reversed
    else:
        if tfc[1] == '1':
            if is_reversed:
                S += tfc[2]
            else:
                S = tfc[2] + S 
        else:
            if is_reversed:
                S = tfc[2] + S
            else:
                S += tfc[2]
if is_reversed:
    print(S[::-1])
else:
    print(S)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

for i in range(Q):
    if Query[i][0] == '1':
        S = S[::-1]
    else:
        F = Query[i][1]
        C = Query[i][2]
        if F == '1':
            S = C + S
        else:
            S = S + C

print(S)

Submission status: Time Limit Exceeded

Solution code:
s = input()
now = 1
q = int(input())
#Query = [ list(map(str,input().split())) for _ in range(q)]
#print(*Query)
adhere = []
back = []
for i in range(q):
    query = input()
    if len(query) == 5:
        #t = int(Query[i][0])
        f = int(query[2])
        c = query[4]
        if f == 1:
            adhere = [c] + adhere
        else:
            back.append(c)
    else:
        now *= -1
        ##tmp_adhere = back
        #back = adhere[::-1]
        #adhere = tmp_adhere[::-1]
        back,adhere = adhere[::-1],back[::-1]
if now == -1:
    ans = "".join(adhere) + s[::-1] + "".join(back)
    #print(-1).join
else:
    ans = "".join(adhere) + s + "".join(back)
    #print(1)
print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

S = input()
num_of_query = int(input())
querys = []
for _ in range(num_of_query):
    # querys.append(list(input().split()))
    query = list(input().split())
    if query[0] == '1':
        S = S[::-1]
    else:
        if query[1] == '1':
            S = deque(S)
            S.appendleft(query[2])
            S = list(S)
        else:
            S = deque(S)
            S.append(query[2])
            S = list(S)


# for query in querys:
#     if query[0] == '1':
#         S = S[::-1]
#     else:
#         if query[1] == '1':
#             S = deque(S)
#             S.appendleft(query[2])
#             S = list(S)
#         else:
#             S = deque(S)
#             S.append(query[2])
#             S = list(S)

print(''.join(S))

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

S = input()
num_of_query = int(input())
querys = []
for _ in range(num_of_query):
    querys.append(list(input().split()))

for query in querys:
    if query[0] == '1':
        S = S[::-1]
    else:
        if query[1] == '1':
            S = deque(S)
            S.appendleft(query[2])
            S = list(S)
        else:
            S = deque(S)
            S.append(query[2])
            S = list(S)

print(''.join(S))

Submission status: Time Limit Exceeded

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

print(S)

Submission status: Time Limit Exceeded

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

for q in range(Q):
    inpu_ = input()
    if len(inpu_)==1:
        counter += 1
    else:
        a,f,c = map(str,inpu_.split())
        if f=="1":
            if counter%2 == 1:
                S.append(c)
            else:
                S.insert(0,c)

        else:
            if counter%2==1:
                S.insert(0,c)
            else: 
                S.append(c)

if counter%2==1:
    S.reverse()
print("".join(S))