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: Wrong Answer

Solution code:
import collections
dq = collections.deque()
s = input()
dq.append(s)
is_reveresed = False
q = int(input())
for i in range(q):
    query = input()
    if query[0] == '1':
        is_reveresed = not is_reveresed
    else:
        f = query[2]
        c = query[4]
        if (f == '1') ^ is_reveresed:
            dq.appendleft(c)
        else:
            dq.append(c)
if is_reveresed:
    dq.reverse()
print(''.join(dq))

Submission status: Wrong Answer

Solution code:
from collections import deque
S = deque(input())
Q = int(input())
for i in range(Q):
  m = input().split()
  if len(m)==1:
    a = S.popleft()
    b = S.pop()
    S.appendleft(b)
    S.append(a)
  else:
    f = m[1]
    if f=='1':
      S.appendleft(m[2])
    else:
      S.append(m[2])
print(''.join(S))

Submission status: Wrong Answer

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

Submission status: Time Limit Exceeded

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

for i in range(Q):
    Query = input().split()
    if len(Query) == 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 = str(input())
Q = int(input())
Qvery = [0]*Q
for i in range(Q):
    Qvery[i] = list(map(str,input().split()))
frontback = 1
for i in range(Q):
    if Qvery[i][0]=='1':
        frontback *= -1
        
    else:
        if Qvery[i][1]=='2':
            if frontback == 1:
                S = S + str(Qvery[i][2])
            else:
                S = str(Qvery[i][2]) + S
        else:
            if frontback == 1:
                S = str(Qvery[i][2]) + S
            else:
                S = S + str(Qvery[i][2])
if frontback == -1:
    S = S[::-1]
print(S)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

poe=deque([])
S=input()
for i in range(len(S)):
    poe.append(S[i])
Q=int(input())
P=[]
for i in range(Q):
    a=list(input().split())
    P.append(a)

for i in range(Q):
    if len(P[i])==1:
        poe=list(poe)
        poe=poe[::-1]
        poe=deque(poe)
    else:
        if P[i][1]=='1':
            poe.appendleft(P[i][2])
        else:
            poe.append(P[i][2])

poe=list(poe)
print(''.join(poe))

Submission status: Time Limit Exceeded

Solution code:
# author:  Taichicchi
# created: 21.09.2020 20:00:43

import sys

S = input()

Q = int(input())

rev = 0

for q in range(Q):
    query = input().split()
    if query[0] == "1":
        rev = 1 - rev
    else:
        if ((rev == 0) & (query[1] == "1")) | ((rev == 1) & (query[1] == "2")):
            S = query[2] + S
        else:
            S = S + query[2]

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

Submission status: Time Limit Exceeded

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

flag = True
lS = ''
for _ in range(Q):
    query = input().split()
    if query[0] == '1':
        lS, S = S, lS
    else:
        if query[1] == '1':
            lS += query[2]
        else:
            S += query[2]

print(lS[::-1] + S)

Submission status: Time Limit Exceeded

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

flag = True
lS = ''
for _ in range(Q):
    query = input()
    if query[0] == '1':
        lS, S = S, lS
    else:
        _, f, c = query.split()
        if f == '1':
            lS += c
        else:
            S += c

print(lS[::-1] + S)

Submission status: Time Limit Exceeded

Solution code:
def resolve():
    s = input()
    query = int(input())
    r = 0
    for i in range(query):
        q = input()
        if q[0]=='2':
            _,f,c = q.split()
            if (int(f)+r)%2 == 0:
                s = s + c
            else:
                s = c + s
        else:
            r = (r+1)%2
    print(s if r==0 else s[::-1])
resolve()

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

flag = True
for _ in range(Q):
    query = input()
    if query[0] == '1':
        flag = not flag
    else:
        _, f, c = query.split()
        if (f == '1' and flag) or (f == '2' and not flag):
            S = c + S
        else:
            S = S + c
if not flag:
    S = S[::-1]

print(S)

Submission status: Time Limit Exceeded

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


for _ in range(Q):
    query = input()
    if query[0] == '1':
        S = S[::-1]
    else:
        _, f, c = query.split()
        if f == '1':
            S = c + S
        else:
            S = S + c

print(S)

Submission status: Time Limit Exceeded

Solution code:
S = input()
N = int(input())
flg = 0
L = []
for i in range(N):
    e = input().split()
    if e[0] == '1':
        flg += 1
        flg = flg%2
    else:
        L.append(((flg+int(e[1]))%2, e[2]))

S = ''.join(list(reversed(S))) if flg else S
for f, v in L:
    if (flg+f)%2 == 1:
        S = v + S
    else:
        S = S + v

print(S)

Submission status: Time Limit Exceeded

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

for i in range(Q):
  line = input()
  if line == '1':
    rev *= -1
  else:
    line = line.split()
    F = int(line[1])
    C = line[2]
    if rev * F in [1, -2]:
      S = C + S
    else:
      S = S + C

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

Submission status: Time Limit Exceeded

Solution code:
import sys
S = input()
Q = int(input())
key = 1

for i in sys.stdin:
    if i[0] == '1':
        key = -key
    
    elif (i[2] == '1' and key == 1) or (i[2] == '2' and key == -1):
        S = i[4] + S
    else:
        S = S + i[4]


print(S[::key])

Submission status: Time Limit Exceeded

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

for i in sys.stdin:
    if i[0] == '1':
        S = S[::-1]
    
    elif i[2] == '1':
        S = i[4] + S
    else:
        S = S + i[4]

print(S)

Submission status: Time Limit Exceeded

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

for i in range(Q):
    queryi = input()
    if queryi == '1':
        S = S[::-1]
    
    else:
        T, F, C = queryi.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())
f = 1
for i in range(q):
  query = list(map(str, input().split()))
  if query[0] == '1':
    f *= -1
  else:
    if (f == 1 and query[1] == '1') or (f == -1 and query[1] == '2'):
      s = query[2] + s
    else:
      s += query[2]
if f == -1:
  s = s[::-1]
print(s)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
f = 1
for i in range(q):
  query = list(map(str, input().split()))
  if query[0] == '1':
    f *= -1
  else:
    if f == 1:
      if query[1] == '1':
        s = query[2] + s
      else:
        s += query[2]
    else:
      if query[1] == '1':
        s += query[2]
      else:
        s = query[2] + s
if f == -1:
  s = s[::-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 query[0] == '1':
    s = s[::-1]
  else:
    if query[1] == '1':
      s = query[2] + s
    else:
      s += query[2]
print(s)

Submission status: Time Limit Exceeded

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

if forward == -1:
    ans = ''
    for i in range(len(S)):
        ans += S[-i-1]
else:
    ans = S
print(ans)

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
query = []
for _ in range(Q):
    q = list(input().split())
    if len(q) != 1:
        q = [int(x) for x in q[:2]] + [q[2]]
    else:
        q = list(map(int, q)) + [0, '']
    query.append(q)

forward = 1
for t, f, c in query:
    if t == 1:
        forward *= -1
    else:
        if f == 1:
            if forward == 1:
                S = c + S
            else:
                S = S + c
        else:
            if forward == 1:
                S = S + c
            else:
                S = c + S

if forward == -1:
    ans = ''
    for i in range(len(S)):
        ans += S[-i-1]
else:
    ans = S
print(ans)

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
query = []
for _ in range(Q):
    q = list(input().split())
    if len(q) != 1:
        q = [int(x) for x in q[:2]] + [q[2]]
    else:
        q = list(map(int, q)) + [0, '']
    query.append(q)

S = list(S)
forward = 1
for t, f, c in query:
    if t == 1:
        forward *= -1
    else:
        if f == 1:
            if forward == 1:
                S = [c] + S
            else:
                S = S + [c]
        else:
            if forward == 1:
                S = S + [c]
            else:
                S = [c] + S
if forward == -1:
    S = list(reversed(S))
print(''.join(S))

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
query = []
for _ in range(Q):
    q = list(input().split())
    if len(q) != 1:
        q = [int(x) for x in q[:2]] + [q[2]]
    else:
        q = list(map(int, q)) + [0, '']
    query.append(q)

S = list(S)
forward = 1
for t, f, c in query:
    if t == 1:
        forward *= -1
    else:
        if f == 1:
            if forward == 1:
                S.insert(0, c)
            else:
                S.append(c)
        else:
            if forward == 1:
                S.append(c)
            else:
                S.insert(0, c)
if forward == -1:
    S = list(reversed(S))
print(''.join(S))

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
query = []
for _ in range(Q):
    q = list(input().split())
    if len(q) != 1:
        q = [int(x) for x in q[:2]] + [q[2]]
    else:
        q = list(map(int, q)) + [0, '']
    query.append(q)

S = list(S)
for t, f, c in query:
    if t == 1:
        S = list(reversed(S))
    else:
        if f == 1:
            S.insert(0, c)
        else:
            S.append(c)
print(''.join(S))

Submission status: Time Limit Exceeded

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

for q in range(Q):
  R = input()
  
  if int(R[0])==1:
    n = (n+1)%2
  else:
    T,F,C = R.split()
    F = int(F)
    
    if n==0 and F==1:
      S = C+S
    elif n==0 and F==2:
      S = S+C
    elif n==1 and F==1:
      S = S+C
    else:
      S = C+S

if n==0:
  print(S)
else:
  print(S[::-1])

Submission status: Time Limit Exceeded

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

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


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
def abc158d_string_formation():
    s = input()
    q = int(input())
    flip = 0
    for _ in range(q):
        query = list(input().split())
        if query[0] == '1':
            flip = 1-flip
        elif query[1] == '1':
            if flip == 0:
                s = query[2]+s
            else:
                s = s+query[2]
        else:
            if flip == 0:
                s = s+query[2]
            else:
                s = query[2]+s
    if flip == 1:
        s = ''.join(reversed(s))
    print(s)
abc158d_string_formation()

Submission status: Time Limit Exceeded

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

query = []
for q in range(Q):
    query.append(list(input().split()))

is_inv = False

for q in query:
    if q[0] == '1':
        is_inv = ~is_inv
    else:
        if q[1] == '1':
            if is_inv:
                S = S + q[2]
            else:
                S = q[2] + S
        else:
            if is_inv:
                S = q[2] + S
            else:
                S = S + q[2]

if is_inv:
    print(''.join(reversed(list(S))))
else:
    print(S)

Submission status: Time Limit Exceeded

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

is_inv = False

for q in range(Q):
    t = list(input().split())
    if t[0] == '1':
        is_inv = ~is_inv
    else:
        if t[1] == '1':
            if is_inv:
                S = S + t[2]
            else:
                S = t[2] + S
        else:
            if is_inv:
                S = t[2] + S
            else:
                S = S + t[2]

if is_inv:
    print(''.join(reversed(list(S))))
else:
    print(S)

Submission status: Time Limit Exceeded

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

is_inv = False

for q in range(Q):
    t = input()
    if t == '1':
        t = int(t)
        is_inv = ~is_inv
    else:
        t, f, c = t.split()
        if f == '1':
            if is_inv:
                S = S + c
            else:
                S = c + S
        else:
            if is_inv:
                S = c + S
            else:
                S = S + c

if is_inv:
    print(''.join(reversed(list(S))))
else:
    print(S)

Submission status: Time Limit Exceeded

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

for q in range(Q):
    t = input()
    if t == '1':
        t = int(t)
        S = ''.join(list(reversed(S)))
    else:
        t, f, c = t.split()
        if f == '1':
            S = c + S
        else:
            S = S + c

print(S)

Submission status: Time Limit Exceeded

Solution code:
s=[i for i in input()]
q=int(input())
t=0
for i in range(q):
  qu=list(input().split())
  if qu[0]=='2':
    if qu[1]=='1' and t==0:
      s=[qu[2]]+s
    elif qu[1]=='1' and t==1:
      s=s+[qu[2]]
    elif qu[1]=='2' and t==0:
      s=s+[qu[2]]
    elif qu[1]=='2' and t==1:
      s=[qu[2]]+s
  else:
    if t==0:
      t=1
    else:
      t=0
if t==0:
  print(*s,sep='')
else:
  s.reverse()
  print(*s,sep='')

Submission status: Time Limit Exceeded

Solution code:
s=[i for i in input()]
q=int(input())
for i in range(q):
  qu=list(input().split())
  if qu[0]=='2':
    if qu[1]=='1':
      s=[qu[2]]+s
    else:
      s=s+[qu[2]]
  else:
    s.reverse()
print(*s,sep='')

Submission status: Time Limit Exceeded

Solution code:
def main():
    s = str(input())
    q = int(input())
    lst = [list(map(str, input().split())) for _ in range(q)]
    switch = 0  # 0が通常　1が前

    str_lst = [s]
    for i in range(q):

        if lst[i][0] == '1':
            switch = 1 - switch

        else:
            f = lst[i][1]
            c = lst[i][2]

            if f == '1':  # 先頭に追加

                if switch == 0:
                    str_lst.insert(0, c)

                else:
                    str_lst.append(c)

            else:

                if switch == 0:
                    str_lst.append(c)

                else:
                    str_lst.insert(0, c)

    answer = ''.join(str_lst)
    if switch == 1:
        answer = answer[::-1]

    print(answer)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
def main():
    s = str(input())
    q = int(input())
    lst = [list(map(str, input().split())) for _ in range(q)]
    switch = 0  # 0が通常　1が前

    for i in range(q):

        if lst[i][0] == '1':
            switch = 1 - switch

        else:
            f = lst[i][1]
            c = lst[i][2]

            if f == '1':  # 先頭に追加

                if switch == 0:
                    tmp = c + s
                    s = tmp

                else:
                    s += c

            else:

                if switch == 0:
                    s += c

                else:
                    tmp = c + s
                    s = tmp

    if switch == 1:
        s = s[::-1]

    print(s)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
def main():
    s = str(input())
    q = int(input())
    lst = [list(map(str, input().split())) for _ in range(q)]
    switch = 0  # 0が通常　1が前

    for i in range(q):

        if lst[i][0] == '1':
            switch = 1 - switch

        else:
            f = lst[i][1]
            c = lst[i][2]

            if f == '1':  # 先頭に追加

                if switch == 0:
                    s = c + s

                else:
                    s = s + c

            else:

                if switch == 0:
                    s = s + c

                else:
                    s = c + s

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


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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


def main():
    S = input().rstrip()
    Q = int(input())
    reverse_flag = False
    for _ in range(Q):
        q = input().rstrip()
        if q == '1':
            reverse_flag = not reverse_flag
        else:
            _, F, C = q.split()
            if (F == '1' and reverse_flag is False) or (F == '2' and reverse_flag is True):
                S = C + S
            else:
                S = S + C
    if reverse_flag is True:
        print(S[::-1])
    else:
        print(S)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
S=input()
n=int(input())
a=0
for i in range(n):
  q=input()
  if q=="1":
    a+=1
  else:
    p,b,c=q.split()
    if b=="1":
      if a%2==0:
        S=c+S
      else:
        S=S+c
    else:
      if a%2==1:
        S=c+S
      else:
        S=S+c
if a%2==1:
  S=S[::-1]
print(str(S))

Submission status: Time Limit Exceeded

Solution code:
s = list(input())
q = int(input())
t = [list(map(str,input().split())) for _ in range(q)]

for i in range(q):
  if len(t[i]) == 1:
    s = s[::-1]
  else:
    if t[i][1] == '1':
      s = list(t[i][2]) + s
    else:
      s = s + list(t[i][2])
      
print(''.join(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]
    else:
        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())
hanten = 0
for i in range(Q):
    query = list(input().split())
    if query[0] == '1':
        hanten +=1
    else:
        if query[1] == '1':
            if hanten % 2 == 0:
                S = query[2] + S
            else:
                S = S + query[2]
        else:
            if hanten % 2 == 1:
                S = query[2] + S
            else:
                S = S + query[2]
if hanten % 2 == 0:
    print(S)
else:
    print(S[::-1])

Submission status: Time Limit Exceeded

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

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]
    else:
        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())
rev = 1
for i in range(q):
  t = input()
  
  if t == '1':
    rev *= -1
  else:
    x,y,sr = t.split()
    if (rev == 1 and y == '2') or (rev == -1 and y == '1'):
      s = ''.join([s,sr])
    else:
      s = ''.join([sr,s])

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

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
rev = 1
for i in range(q):
  t = input()
  
  if t == '1':
    rev *= -1
  else:
    x,y,sr = t.split()
    if (rev == 1 and y == '2') or (rev == -1 and y == '1'):
      s = s + sr
    else:
      s = sr + s

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

Submission status: Time Limit Exceeded

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time, copy

sys.setrecursionlimit(10**7)
inf = 10**20
mod = 10**9 + 7

stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()  # ignore trailing spaces

ans = ns()
q = ni()
now = 0
for i in range(q):
    query = ns().split(' ')
    if query[0] == '1':
        now = 1 - now
    else:
        f, c = query[1], query[2]
        if f == '1':
            if now == 1:
                ans += c
            else:
                ans = c + ans
        else:
            if now != 1:
                ans += c
            else:
                ans = c + ans

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
# ABC 158 D
S=list(input())
Q=int(input())
inv=0
for _ in [0]*Q:
    s=input()
    if s=='1':
        inv+=1
    else:
        t,f,c=s.split()
        if ((f=='1') ^ (inv%2==1)):
            S=[c]+S
        else:
            S=S+[c]
if inv%2:
    S=S[::-1]
print(*S,sep='')

Submission status: Time Limit Exceeded

Solution code:
# ABC 158 D
S=list(input())
Q=int(input())
for _ in [0]*Q:
    s=input()
    if s=='1':
        s=int(s)
        S=S[::-1]
    else:
        t,f,c=s.split()
        if f=='1':
            S=[c]+S
        else:
            S=S+[c]
print(*S,sep='')

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin
input=stdin.readline
s=input().rstrip()
q=int(input())
cnt=0
for i in range(q):
  qu=list(input().split())
  if qu[0]=='1':
    cnt+=1
  elif qu[0]=='2':
    if (int(qu[1])+cnt)%2==1:
      s=qu[2]+s
    else:
      s=s+qu[2]
if cnt%2==0:
  print(s)
else:
  print(s[::-1])

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
s=input()
data=[i for i in s]
q=int(input())
qu=[input().split() for i in range(q)]
cnt=0
for i in qu:
  if i[0]=='1':
    cnt+=1
  elif i[0]=='2':
    if (int(i[1])+cnt)%2==1:
      data=[i[2]]+data
    else:
      data=data+[i[2]]
if cnt%2==1:
  data.reverse()
print(*data,sep='')

Submission status: Time Limit Exceeded

Solution code:
s=input()
data=[i for i in s]
q=int(input())
qu=[input().split() for i in range(q)]
for i in qu:
  if i[0]=='1':
    data.reverse()
  elif i[0]=='2':
    if i[1]=='1':
      data=[i[2]]+data
    else:
      data=data+[i[2]]
print(*data,sep='')

Submission status: Time Limit Exceeded

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

    for _ in range(q):
        query = list(input())
        if query[0] == "1":
            s = s[::-1]
        elif query[0] == "2":
            if query[2] == "1":
                s = query[4] + s
            elif query[2] == "2":
                s = s + query[4]

    print(s)


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
def main():

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

    flag = 0
    query = []
    for _ in range(q):
        temp = input().split()
        if len(temp) == 1:
            flag += 1
        else:
            query.append(temp + [flag%2])

    for q in query:
        if q[-1] == 0:
            if q[1] == "1":
                s = q[2] + s
            else:
                s += q[2]
        else:
            if q[1] == "1":
                s += q[2]
            else:
                s = q[2] + s

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

main()

Submission status: Time Limit Exceeded

Solution code:
def main():

    s = input()
    q = int(input())
    query = [input().split() for _ in range(q)]
    ctn = query.count(["1"]) % 2 == 1

    new_query = []
    flag = 0
    for q in query:
        if len(q) == 1:
            flag += 1
        else:
            q += [flag%2]
            new_query.append(q)

    for q in new_query:
        if q[-1] == 0:
            if q[1] == "1":
                s = q[2] + s
            else:
                s += q[2]
        else:
            if q[1] == "1":
                s += q[2]
            else:
                s = q[2] + s

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

main()

Submission status: Time Limit Exceeded

Solution code:
def main():

    s = input()
    q = int(input())
    query = [input().split() for _ in range(q)]
    ctn = query.count(["1"]) % 2 == 1

    new_query = []
    flag = 0
    for q in query:
        if len(q) == 1:
            if flag == 0:
                flag = 1
            else:
                flag = 0
        else:
            q += [flag]
            new_query.append(q)

    for q in new_query:
        if q[-1] == 0:
            if q[1] == "1":
                s = q[2] + s
            else:
                s += q[2]
        else:
            if q[1] == "1":
                s += q[2]
            else:
                s = q[2] + s

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

main()