Problem:


Score : 400 points

Problem Statement

Takahashi has a string S consisting of lowercase English letters.

Starting with this string, he will produce a new one in the procedure given as follows.

The procedure consists of Q operations. In Operation i (1 \leq i \leq Q), an integer T_i is provided, which means the following:

If T_i = 1: reverse the string S.

If T_i = 2: An integer F_i and a lowercase English letter C_i are additionally provided.

If F_i = 1 : Add C_i to the beginning of the string S.

If F_i = 2 : Add C_i to the end of the string S.

Help Takahashi by finding the final string that results from the procedure.

Constraints

1 \leq |S| \leq 10^5

S consists of lowercase English letters.

1 \leq Q \leq 2 \times 10^5

T_i = 1 or 2.

F_i = 1 or 2, if provided.

C_i is a lowercase English letter, if provided.

Input

Input is given from Standard Input in the following format:

S
Q
Query_1
:
Query_Q

In the 3-rd through the (Q+2)-th lines, Query_i is one of the following:

1

which means T_i = 1, and:

2 F_i C_i

which means T_i = 2.

Output

Print the resulting string.

Sample Input 1

a
4
2 1 p
1
2 2 c
1

Sample Output 1

cpa

There will be Q = 4 operations. Initially, S is a.

Operation 1: Add p at the beginning of S. S becomes pa.

Operation 2: Reverse S. S becomes ap.

Operation 3: Add c at the end of S. S becomes apc.

Operation 4: Reverse S. S becomes cpa.

Thus, the resulting string is cpa.

Sample Input 2

a
6
2 2 a
2 1 b
1
2 2 c
1
1

Sample Output 2

aabc

There will be Q = 6 operations. Initially, S is a.

Operation 1: S becomes aa.

Operation 2: S becomes baa.

Operation 3: S becomes aab.

Operation 4: S becomes aabc.

Operation 5: S becomes cbaa.

Operation 6: S becomes aabc.

Thus, the resulting string is aabc.

Sample Input 3

y
1
2 1 x

Sample Output 3

xy



Sample input:
a
4
2 1 p
1
2 2 c
1


Sample output:
cpa

Submission status: Time Limit Exceeded

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

print(s)

Submission status: Time Limit Exceeded

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

t = 1
for i in range(Q):
    A = input()
    if A=="1":
        t*=-1
    else:
        AL = list(A.split())
        if AL[1]=="1":
            if t==1:
                B +=AL[2]
            else:
                S +=AL[2]
        elif AL[1]=="2":
            if t==1:
                S +=AL[2]
            else:
                B +=AL[2]

if t==-1:
    print(S[::-1]+B)
else:
    print(B[::-1]+S)

Submission status: Time Limit Exceeded

Solution code:
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
# from fractions import gcd
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from functools import reduce
from bisect import bisect_left, insort_left
from heapq import heapify, heappush, heappop

INPUT = lambda: sys.stdin.readline().rstrip()
INT = lambda: int(INPUT())
MAP = lambda: map(int, INPUT().split())
S_MAP = lambda: map(str, INPUT().split())
LIST = lambda: list(map(int, INPUT().split()))
S_LIST = lambda: list(map(str, INPUT().split()))

sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7


def main():
    S = INPUT()
    Q = INT()

    D_MAP = lambda: map(str, query.split())
    for i in range(Q):
        query = INPUT()
        if len(query.split()) == 1:
            T = 1
        else:
            T = 2
            _, F, C = D_MAP()
            F = int(F)

        if T == 1:
            S = ''.join(list(reversed(S)))
        if T == 2:
            if F == 1:
                S = C + S
            else:
                S = S + C

    print(S)



if __name__ == '__main__':
    main()

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:
# coding: utf-8

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


rev = False
for _ in range(Q):
    line = input()

    if line[0] == "1":
        rev = not rev

    else:
        if line[2] == "1":
            if rev:
                S = "".join([S, line[4]])
            else:
                S = "".join([line[4], S])
        else:
            if rev:
                S = "".join([line[4], S])
            else:
                S = "".join([S, line[4]])

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

ans = []
ans.append(S)
T = [0,0,""]
for i in range(Q):
  TS = input()
  if TS == "1":
    revcount += 1
  else:
    T = TS.rstrip().split(" ")
    if (int(T[1]) == 1 and revcount % 2 == 0) or (int(T[1]) == 2 and revcount % 2 == 1):
      ans.append(T[2] + ans[0])
      ans.pop(0)
    else:
      ans.append(ans[0] + T[2])
      ans.pop(0)
if revcount % 2 == 1:
  print(ans[0][::-1])
else:
  print(ans[0])

Submission status: Time Limit Exceeded

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

for _ in range(q_num):
    query = input().split()
    if query[0] == '1':
        s.reverse()
    elif query[0] == '2':
        if query[1] == '1':
            s.insert(0, query[2])
        elif query[1] == '2':
            s.append(query[2])
print(''.join(s))

Submission status: Time Limit Exceeded

Solution code:
reverseFlag = False

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

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

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
import sys
input = sys.stdin.readline


a = list(input())
a.pop(-1)

Q = int(input())
for _ in range(Q):
    q = list(input().split())
    if q[0] == "1":
        a.reverse()
    else:
        if q[1] == "1":
            # a.insert(0, q[2])
            b = deque(a)
            b.appendleft(q[2])
            a = list(b)
        else:
            a.append(q[2])
print("".join(a))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
def main():
    S = input()
    Q = int(input())
    revFlag = False
    for i in range(Q):
        tmp = list(input().split())
        if tmp[0] == "1":
            if revFlag:
                revFlag = False
            else:
                revFlag = True
        else:
            if tmp[1] == "1":
                if revFlag:
                    tl = [S,tmp[2]]
                    S = "".join(tl)
                else:
                    tl = [tmp[2],S]
                    S = "".join(tl)
            else:
                if revFlag:
                    tl = [tmp[2],S]
                    S = "".join(tl)
                else:
                    tl = [S,tmp[2]]
                    S = "".join(tl)
    if revFlag:
        print(S[::-1])
    else:
        print(S)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

for _ in range(q):
    query = input().split()

    if query[0] == '1':
        s = s[::-1]

    elif query[0] == '2' and query[1] == '1':
        s = query[2] + s

    elif query[0] == '2' and query[1] == '2':
        s += query[2]

print(s)

Submission status: Time Limit Exceeded

Solution code:
s=input()
q=int(input())
cnt=0
qq=[list(input().split()) for _ in range(q)]
for j in qq:
    if  len(j)!=1 :
        if (cnt+int(j[1]))%2==1:
            s=j[2]+s
        else:
            s=s+j[2]
    else:
        cnt+=1
if cnt%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):
	a = 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:
import sys
input = sys.stdin.readline
from collections import deque

S = input().rstrip()
Q = int(input())
Query = []

flag = 0

for _ in range(Q):
    Query.append(input().rstrip().split())

for i in Query:
    if i[0] == "1":
        flag += 1
    elif i[1] == "1":
        if flag % 2 == 0:
            S = i[2] + S
        else:
            S += i[2]
    else:
        if flag % 2 == 0:
            S += i[2]
        else:
            S = i[2] + S
if flag % 2 == 0:
    print(S)
else:
    print(S[::-1])

Submission status: Time Limit Exceeded

Solution code:
s = input().rstrip()
qn = int(input())
mode = 0
for n in range(qn):
    qs = input().rstrip()
    if qs[0] == '1':
        mode = 1 - mode
    if qs[0] == '2':
        if qs[2] == '1':
            if mode == 0:
                s = qs[4] + s
            else:
                s = s + qs[4]
        else:
            if mode == 0:
                s = s + qs[4]
            else:
                s = qs[4] + s
if mode == 0:
    print(s)
else:
    print(s[::-1])

Submission status: Time Limit Exceeded

Solution code:
s = [i for i in input()]
q = int(input())
for _ in range(q):
    s_ = input()
    if len(s_) == 1:
        s = s[::-1]
    else:
        _, f, c = map(str, s_.split())
        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())

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:
s = input("")
Q = int(input())
for i in range(Q):
    t = input()
    if len(t) == 1:
        s = s[::-1]
    elif t[2] == "1":
        s = t[4] + s
    else:
        s = s + t[4]

print(s)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

s = input()
Q = int(input())
query = [list(map(str, input().split())) for _ in range(Q)]

d = deque(s)

for q in query:
    if q[0] == '1':
        s = s[::-1]
    else:
        d = deque(s)
        if q[1] == '1':
            d.appendleft(q[2])
        else:
            d.append(q[2])
        d_list = list(d)
        s = ''.join(d_list)

print(s)

Submission status: Time Limit Exceeded

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

for i in range(q):
    if que[i][0] == "1":
        s = s[::-1]
    elif que[i][0] == "2":
        if que[i][1] == "1":
            s = que[i][2] + s
        elif que[i][1] == "2":
            s = s + que[i][2]

print(s)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
# 先頭と末尾を保ちつつ
s = input()
q = int(input())
x, y = [], []
r = 0
for _ in range(q):
    query = input().split()
    if query[0] == "1":
        r += 1
        # これ遅いか？
        x, y = y[::-1], x[::-1]
    elif query[0] == "2":
        f, c = query[1], query[2]
        if f == "1":
            # x.append(c)
            x = x[::-1]
            x.append(c)
            x = x[::-1]
        elif f == "2":
            y.append(c)
if r % 2 == 1:
    s = s[::-1]
print("".join(x) + s + "".join(y))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sun Jan 19 20:52:57 2020

@author: over-
"""


S=input()

R=''
L=len(S)
for i in range(L):
    R+=S[L-i-1]


Q=int(input())

for i in range(Q):
    q=input().split()
    if int(q[0])==1:
        t=S
        S=R
        R=t
    if int(q[0])==2:
        if int(q[1])==1:
            S=q[2]+S
            R=R+q[2]
        if int(q[1])==2:
            R=q[2]+R
            S=S+q[2]


print(S)

Submission status: Time Limit Exceeded

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

for i in range(NQ):
    query = input().split()
    if (int(query[0]) == 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:
s = input()
q = int(input())
x = 0
for i in range(q):
    query = input().split()
    if query[0] == '1':
        x += 1
    else :
        y = x + int(query[1])
        if y % 2 == 0:
            s = s + query[2]
        else :
            s = query[2] + s
if x % 2 == 0:
    print(s)
else :
    print(s[::-1])

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

for i in range(Q):
    q = list(map(str, input().split()))
    if len(q) == 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:
def main():
    s=input()
    q=int(input())
    for _ in range(q):
        t=input()
        if t=='1':
            s=s[::-1]
        else:
            t = t.split()
            if int(t[1])==1:
                s = t[2] + s
            else:
                s  += t[2]
    print(s)
main()

Submission status: Time Limit Exceeded

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

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

    r_flg = True

    for i in range(q):
        query = input().split()

        # query 1
        if query[0] == '1':
            r_flg = not r_flg
        
        # query 2
        else:
            # Fi = 1
            if query[1] == '1':
                if r_flg:
                    s = query[2] + s
                else:
                    s = s + query[2]
            # Fi = 2
            else:
                if r_flg:
                    s = s + query[2]
                else:
                    s = query[2] + s

        # print('cont:{}, s:{}, r_flg:{}'.format(i, s, r_flg))

    if not r_flg:
        s = s[::-1]

    print(s)

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

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

for i in range(Q):
  A = input().split()
  if len(A) == 1:
    S = S[::-1]
  else:
    if int(A[1]) == 1:
      S = A[2] + S
    else:
      S += A[2]

print(S)

Submission status: Time Limit Exceeded

Solution code:
s = input()
n = int(input())
query = [0]*n
for i in range(n):
    query[i] = list(map(str, input().split()))
#    print(query[i])

for i in range(n):
    if int(query[i][0]) == 1:
        s = s[::-1]
    else:
        if int(query[i][1]) == 1:
            s = query[i][2] + s
        else:
            s = s + query[i][2]
#    print(s)
print(s)

Submission status: Time Limit Exceeded

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

for i in range(Q):
    a = list(map(str, input().split()))
    if a[0] == '1':
        S = S[::-1]
    elif a[0] == '2':
        if a[1] == '1':
            S.insert(0, a[2])
        else:
            S.append(a[2])

print(''.join(S))

Submission status: Time Limit Exceeded

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

rev_count = 0
for _ in range(q):
  l = input().split()
  if len(l) == 1:
    rev_count += 1
  else:
    if rev_count & 1:
      s.reverse()
      rev_count = 0

    if int(l[1]) == 1:
      s = [l[2]] + s
    else:
      s = s + [l[2]]

if rev_count & 1:
  s.reverse()

print(''.join(s))

Submission status: Time Limit Exceeded

Solution code:
s  = str(input())

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
tmp = s
flag = 0
for _ in range(q):
    _s = input()
    t = 0
    f = 0
    c = ""
    if _s == "1":
        flag += 1
    else:
        t, f, c = _s.split(" ")
        if f == "1" and flag % 2 == 0:
            tmp = c + tmp
        elif f == "1" and flag % 2 == 1:
            tmp = tmp + c
        elif f == "2" and flag % 2 == 0:
            tmp = tmp + c
        elif f == "2" and flag % 2 == 1:
            tmp = c + tmp
if flag % 2 == 0:
    print(tmp)
else:
    print(tmp[::-1])

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
for _ in range(Q):
    q = input()
    if len(q)>1:
        _, f, c = q.split()
        if f=='1':
            S = c+S
        else:
            S = S+c
    else:
        S = S[::-1]
print(S)

Submission status: Time Limit Exceeded

Solution code:
first_step_string = input()
query_num = int(input())
query_li = list()
for _ in range(query_num):
    query_li.append(input().split())

ans = first_step_string
for query in query_li:
    if query[0] == '1':
        ans = ans[::-1]
    else:
        if query[1] == '1':
            ans = query[2] + ans
        else:
            ans += query[2]

print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import sys
from collections import deque

S = input()
Q = int(input())
QUERY = sys.stdin.readlines()

state = 0
q1 = deque()
q2 = deque()
for i in range(Q):
    query = QUERY[i]
    if query[0] == '1':
        state ^= 1
    else:
        _, F, C = query.split()
        F = int(F)
        if (state + F) % 2 == 1:
            q1.appendleft(C)
        else:
            q2.append(C)

ans = ''
if state == 0:
    while q1:
        ans += q1.popleft()
    ans += S
    while q2:
        ans += q2.popleft()
else:
    while q2:
        ans += q2.pop()
    ans += ''.join(list(reversed(S)))
    while q1:
        ans += q1.pop()
print(ans)

Submission status: Time Limit Exceeded

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

for i in range(q):

    data = input().split(" ")

    if int(data[0]) == 1:
        s = s[::-1]

    else:
        c = data[2]
        if int(data[1]) == 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):
    q = input()
    if q.startswith('1'):
        S = S[::-1]
    else:
        t, f, c = q.split()
        if f == '1':
            S = c + S
        else:
            S = S + c

print(S)

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
for i in range(q):
    xxx = input()
    if len(xxx) == 1:
        s = s[::-1]
    else:
        T,F,C=xxx.split()
        F = int(F)
        T = int(T)
        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:
s = input()
q = int(input())

reverse_flag = False
for i in range(q):
  q_list = list(input().split())
  
  if len(q_list) == 1:
    reverse_flag = not reverse_flag
    
  elif q_list[1] == '1' and (not reverse_flag):
    s = q_list[2] + s
    
  elif q_list[1] == '2' and (not reverse_flag):
    s += q_list[2]
    
  elif q_list[1] == '1' and reverse_flag:
    s += q_list[2]
    
  elif q_list[1] == '2' and reverse_flag:
    s = q_list[2] + s
    
if reverse_flag:
  print(''.join(list(reversed(s))))
else:
  print(s)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

s=input()

d=deque()
for i in s:
  d.append(i)
  
v=1

q=int(input())

for i in range(q):
  a=input()
  if len(a)==1:
    if v:
      v=0
    else:
      v=1
  else:
    b,f,c=a.split()
    if f=="1":
      if v:
        d.appendleft(c)
      else:
        d.append(c)
    else:
      if v:
        d.append(c)
      else:
        d.appendleft(c)
        
ans=""
if v:
  for i in range(len(d)):
    ans+=d[i]
else:
  for i in range(len(d)):
    ans+=d.pop()
    
print(ans)

Submission status: Time Limit Exceeded

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

reversing = False
for i in range(Q):
    query = list(input().split())
    if query[0] == '1':
        reversing = not reversing
    else:
        if query[1] == '1':
            if not reversing:
                S = query[2] + S
            else:
                S += query[2]
        else:
            if reversing:
                S = query[2] + S
            else:
                S += query[2]

print(S) if not reversing else print(S[::-1])

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
from functools import reduce
from operator import add

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

rev_cnt = 0
for i in range(q):
    query = input().split()
    if query[0] == '2':
        if (query[1] == '1' and rev_cnt % 2 == 0) or ((query[1] == '2' and rev_cnt % 2 == 1)):
            s.appendleft(query[2])
        else:
            s.append(query[2])
    else:
        rev_cnt += 1

res = reduce(add, s)
if rev_cnt % 2 == 1:
    res = res[::-1]

print(res)

Submission status: Time Limit Exceeded

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

rev_flag = False

for i in range(Q):
  in_ = input()
  if len(in_) > 1:
    _, f, c = in_.split()
    f = int(f)
    
    if rev_flag:
      if f==1:
        S = S + c
      elif f==2:
        S = c+S
    else:
      if f==2:
        S = S + c
      elif f==1:
        S = c + S
    
  else:
    # reverse    
    rev_flag = not rev_flag

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

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
Qs = []
for _ in range(Q):
    Qs.append(input().split())  # T, F, C

rev = 0
for i in range(len(Qs)):
    if Qs[i][0]=='1':
        rev += 1
    else:
        if rev%2 == 1:
            S = S[::-1]
            rev = 0
        
        if Qs[i][1] == '1':
            S = Qs[i][2] + S
        else:
            S = S + Qs[i][2]

if rev%2 == 1:
    S = S[::-1]
    rev = 0            

print(S)

Submission status: Time Limit Exceeded

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

for _ in range(0, num_query):
    query = input()
    if query[0] == "1":
        s = s[::-1]
    else:
        _, pos, character = query.split()
        if pos == "1":
            s = character + s
        else:
            s = s + character
print(s)

Submission status: Time Limit Exceeded

Solution code:
s = input()
rev = False
for _ in range(int(input())):
  t = input()
  if t == "1":
    rev = not rev
  else:
    t = t.split()
    if (t[1] == "1" and not rev) or (t[1] == "2" and rev):
      s = t[2] + s
    else:
      s += t[2]
if rev:
  print(s[::-1])
else:
  print(s)

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
count = 0
left = ''
right = ''
for i in range(Q):
    TFC = input().split()
    if TFC[0] == '1':
        count += 1
    else:
        T, F, C = TFC
        if F == '1':
            if count % 2 == 0:
                left += C
            else:
                right += C
        else:
            if count % 2 == 0:
                right += C
            else:
                left += C
if count % 2 == 0:
    print(left[::-1] + S + right)
else:
    print(right[::-1] + S[::-1] + left)

Submission status: Time Limit Exceeded

Solution code:
S=input()                                               # 1行1列
Q=int(input())                                          # 1行1列
Q_LIST=[input().split() for i in range(Q)]                # N行C列・・・Cは任意

dir = 0 # 0が正常

for query in Q_LIST:
	if query[0] == '1':
		if dir == 0:
			dir = 1
		else:
			dir = 0

	if dir == 0:
		HEAD = '1'
		BACK = '2'
	else:
		HEAD = '2'
		BACK = '1'

	if query[0] == '2':
		if query[1] == HEAD:
			S = query[2] + S 
		if query[1] == BACK:
			S =  S + query[2]

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

Submission status: Time Limit Exceeded

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

que = []

for i in range(q):
    que = li[i]
    if que[0] == "1":
        s = s[::-1]
    else:
        f = que[1]
        c = que[2]
        if f == "1":
            s = c + s
        else:
            s = s + c

print(s)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

S = input().strip()
Q = int(input())
for i in range(Q):
    q = input().split()
    if q[0] == '1':
        S = S[::-1]
    elif q[0] == '2':
        if q[1] == '1':
            S = q[2] + S
        elif q[1] == '2':
            S = S + q[2]

print(S)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

s = list(s)
for query in Q:
    if len(query) == 1:
        s.reverse()
    else:
        if query[1] == '1':
            s.insert(0, query[2])
        elif query[1] == '2':
            s.append(query[2])

print("".join(s))

Submission status: Time Limit Exceeded

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

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

print(s)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
S = input().strip()
Q = int(input())
cnt = 0
for _ in range(Q):
    q = list(input().split())
    if int(q[0])==1:
        cnt += 1
    else:
        if cnt%2==0:
            if int(q[1])==1:
                S = q[2]+S
            else:
                S = S+q[2]
        else:
            if int(q[1])==1:
                S = S+q[2]
            else:
                S = q[2]+S
if cnt%2==0:
    print(S)
else:
    S = S[::-1]
    print(S)

Submission status: Time Limit Exceeded

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

def do(S, method):
    if method == "1":
        return S[::-1]
    elif method[:3] == "2 1":
        return method[4] + S
    elif method[:3] == "2 2":
        return S + method[4]

for _ in range(N):
    S = do(S, input())

print(S)

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
reverse = 0
for i in range(Q):
    q = input()
    if q[0] == "1":
        reverse += 1 
        reverse %= 2
    else:
        _, F, C = q.split()
        if F == "1":
            if reverse:
                S += C
            else:
                S = C + S
        else:
            if reverse:
                S = C + S
            else:
                S += C

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

Submission status: Time Limit Exceeded

Solution code:
s=list(input())
q=int(input())
n=0
for i in range(q):
    li=input().split()
    if int(li[0])==1:
        n+=1
    elif int(li[0])==2:
        if (n%2==0 and int(li[1])==1) or (n%2==1 and int(li[1])==2):
            s.insert(0,li[2])
        else:
            s.append(li[2])
if n%2==0:
    print(''.join(s))
else:
    s.reverse()
    print(''.join(s))

Submission status: Time Limit Exceeded

Solution code:
# abc 158
import sys
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
read = sys.stdin.buffer.read
sys.setrecursionlimit(10 ** 7)

S = readline().strip().decode('utf-8')
Q = int(readline())

is_reversed = False

answer = S
for _ in range(Q):
    l = readline().decode('utf-8').split()
    if len(l) == 1:
        is_reversed = not is_reversed
    else:
        _, F, C = map(str, l)
        if F == "1":
            if is_reversed:
                answer = answer + C
            else:
                answer = C + answer
        else:
            if is_reversed:
                answer = C + answer
            else:
                answer = answer + C

if is_reversed:
    answer = "".join(list(reversed(answer)))
print(answer)