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())
snt=1
for i in range(Q):
    inp=input().split()
    if inp[0]=="1":
        snt*=(-1)
    else:
        F=int(inp[1])
        C=inp[2]
        if (2*F-3)*snt>0:
            S=S+C
        else:
            S=C+S
if snt==1:
    print(S)
else:
    for i in range(len(S)):
        print(S[len(S)-1-i],end="")
    print()

Submission status: Time Limit Exceeded

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

reverseFlag = False

for i in range(NQ):
    query = input().split()
    if (int(query[0]) == 1):
        reverseFlag = not reverseFlag
    else:
        if (reverseFlag):
            if (int(query[1]) == 1):
                S = S + query[2]
            else:
                S = query[2] + S
        else:
            if (int(query[1]) == 1):
                S = query[2] + S
            else:
                S = S + query[2]

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import math

S = input().strip()
Q = int(input().strip())

for i in range(Q):
  q = input().strip().split()
  #print(q)
  if q[0] == '1':
    # 反転
    S = S[::-1]    
  else:
    F = q[1]
    if F == '1':
      # 先頭にCを追加
      S = q[2] + S     
    else:
      # 末尾にCを追加
      S = S + q[2]
    
print(S)

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
Q_lst = []
for i in range(Q):
    Q_lst.append(input().split())
for i in range(len(Q_lst)):
    if Q_lst[i] == ["1"]: #T == 1
        S = S[::-1]
    elif Q_lst[i][1] == "1":    #T == 2, F == 1
        S = Q_lst[i][2] + S
    else:   #F == 2
        S = S + Q_lst[i][2]
print(S)

Submission status: Time Limit Exceeded

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

topFlg = True

for _ in range(Q):
    query = list(map(str, input().split()))

    if query[0] == "1":
        topFlg = not (topFlg)
    else:
        if query[1] == "1":
            if topFlg:
                S = query[2] + S
            else:
                S += query[2]
        else:
            if topFlg:
                S += query[2]
            else:
                S = query[2] + S

if not topFlg:
    S = S[::-1]

print(S)

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
S = [s for s in S]

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

Submission status: Time Limit Exceeded

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

for i in range(Q):
    l = str(input())
    if l[0] == '1':
        rev = (rev + 1) % 2
    else:
        two, F, C = l.split()
        if rev == 0:
            if F == '1':
                S = ''.join([C, S])
            else:
                S = ''.join([S, C])
        else:
            if F == '1':
                S = ''.join([S, C])
            else:
                S = ''.join([C, S])



if rev == 0:
    print(S)
else:
    print(''.join(list(reversed(S))))

Submission status: Time Limit Exceeded

Solution code:
def main():
    #from sys import stdin, setrecursionlimit
    #setrecursionlimit(10**9)
    #r = input()
    #n = int(input())
    #a, b = map(int, input().split())
    #s, y = input().split()
    #a = input().split()
    #a = [int(input()) for i in range(n)]
    #a = list(map(int, input().split()))
    #a = [list(map(int, input().split())) for i in range(n)]
    #a = [int(s) - 1 for s in input().split()]
    #l=[int(input()) for _ in range(int(input()))]
    #res = 0
    s = input()
    q = int(input())
    r = [input().split() for i in range(q)]
    cnt = 0

    for i in range(q):
        tmp = r[i]
        if tmp[0] == "1":
            cnt += 1
        else:
            if tmp[1] == "1" and cnt % 2 == 0 or tmp[1] != "1" and cnt % 2 != 0:
                s = tmp[2] + s
            else:
                s = s + tmp[2]
    if cnt % 2 == 0:
        print(s)
    else:
        print(*(list(s)[::-1]), sep="")

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
for i in range(q):
  qn = input()
  if qn[0] == '1':
    s = s[::-1]
    #print(s)
  else:
    qm, f, c = qn.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())
ans = S

for i in range(Q):
    Q_i = list(input().split())
    if Q_i[0] == "1":
        ans = ans[::-1]
    else:
        if Q_i[1] == "1":
            ans = Q_i[2] + ans
        else:
            ans = ans + Q_i[2]

print(ans)

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin
s = str(stdin.readline().rstrip())
q = int(stdin.readline().rstrip())
for i in range(q):
    query = input()
    if len(query)==1:
        #反転
        pass
        s = s[::-1]
    else:
        if int(query[2])==2:
            s += query[4]
        else:
            s = query[4] + s

print(s)

Submission status: Time Limit Exceeded

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

vec = 0
for _ in range(q):
    X = input().split()
    if X[0] == '1':
        vec = (vec+1) % 2
    else:
        if X[1] == '1' and vec == 0:
            s.insert(0, X[2])
        elif X[1] == '2' and vec == 0:
            s.append(X[2])
        elif X[1] == '1' and vec == 1:
            s.append(X[2])
        elif X[1] == '2' and vec == 1:
            s.insert(0, X[2])

if vec == 1:
    s.reverse()

print(''.join(map(str, s)))

Submission status: Time Limit Exceeded

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

# 毎回反転させていたら時間切れになる
is_reverse = False
for _ in range(q):
    query = input().split()
    if len(query) == 1:
        if is_reverse:
            is_reverse = False
        else:
            is_reverse = True
    else:
        _, f, c = query
        if f == '1':
            if is_reverse:
                s = s + c
            else:
                s = c + s
        else:
            if is_reverse:
                s = c + s
            else:
                s = s + c

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

Submission status: Time Limit Exceeded

Solution code:
s=input()
ans=""
q=int(input())
switch=0

for i in range(q):
  ques=list(input().split())
  if ques[0]=="1":
    switch+=1
    #print("1",s)
  else:
    if ques[1]=="1":
      if switch%2==0:
        s=ques[2]+s
        #print("2",s)
      else:
        s=s+ques[2]
    else:
      if switch%2==0:
        s=s+ques[2]
      else:
        s=ques[2]+s
        #print("22",s)
if switch%2==1:
  ans=s[::-1]
else:
  ans=s
print(ans)

Submission status: Time Limit Exceeded

Solution code:
s = input()
n = int(input())
qs = [input().split(' ') for i in range(n)]

div = True
for q in qs:
    if q[0] == '1':
        div = not div
    if q[0] == '2':
        if q[1] == '1':
            if div:
                s = q[2] + s
            else:
                s = s + q[2]
        if q[1] == '2':
            if div:
                s = s + q[2]
            else:
                s = q[2] + s

if div:
    print(s)
else:
    s = ''.join(list(reversed(s)))
    print(s)

Submission status: Time Limit Exceeded

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

for i in range(Q):
    Query = input()
    if Query[0] == "1":
        Flag = not Flag
    else:
        QueryList = Query.split()
        if Flag:
            if QueryList[1] =="1":
                S = QueryList[2]+S
            else:
                S = S+QueryList[2]
        else:
            inv_add = QueryList[2][::-1]
            if QueryList[1] =="2":
                S = QueryList[2]+S
            else:
                S = S+QueryList[2]

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

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
query = ""
list_1 = []

for i in range(Q):
    query = input()

    if query == "1":
        S = S[::-1]
    else:
        list_1 = query.split()
        if list_1[1] == "1":
            S = list_1[2] + S
        else:
            S = S + list_1[2]

print(S)

Submission status: Time Limit Exceeded

Solution code:
s=input()
a=int(input())
for i in range(a):
  n=input().split()
  if(n[0]=="1"):
    s = ''.join(list(reversed(s)))
  elif(n[0]=="2"):
    mylist=list(s)
    if(n[1]=="1"):
      mylist.insert(0,n[2])
    else:
      mylist.append(n[2])
    s=''.join(mylist)
    
print(s)

Submission status: Time Limit Exceeded

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

for _ 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:
S = input()
Q = int(input())
count = 0
right = ''
left = ''
for i in range(Q):
    arr = list(input().split())
    if len(arr) == 1:
        count += 1
    else:
        if arr[1] == '1':
            if count%2==0:
                S = arr[2] + S
            else:
                S = S + arr[2]
        else:
            if count%2!=0:
                S = arr[2] + S
            else:
                S = S + arr[2]
if count%2 == 0:
    print(S)
else:
    ans = ''
    for i in S:
        ans = i + ans
    print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
S = list(str(input()))
Q = int(input())
reverse = False
for i in range(Q):
    query = list(map(str,input().split()))
    if query[0] == '1':
        if reverse == True:
            reverse = False
        else:
            reverse = True
    if query[0] == '2':
        if query[1] == '1':
            if reverse == False:
                S.insert(0, query[2])
            else:
                S.append(query[2])
        else:
            if reverse == True:
                S.insert(0, query[2])
            else:
                S.append(query[2])
if reverse == True:
    S.reverse()
print(*S, sep = '')

Submission status: Time Limit Exceeded

Solution code:
s=input()
n=int(input())
rev=0
for i in range(n):
  l=input().split()
  q=int(l[0])
 
  
  if q==1:
    rev+=1
  elif q==2:
    f=int(l[1])
    c=l[2]
    if (rev%2==0 and f==1)or(rev%2==1 and f==2):
      s=c+s
    elif (rev%2==0 and f==2) or(rev%2==1 and f==1):
      s=s+c
if rev%2==1:
  ll=list(reversed(s))
  s="".join(ll)
print(s)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
from collections import deque

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

for i in range(q):
    query = input()
    if query == "1":
        s = s[::-1]
    else:
        x, q2_1, q2_2 = query.split()
        if q2_1 == "1":
            a = deque(s)
            a.appendleft(q2_2)
            s = list(a)
        else:
            s.append(q2_2)
print("".join(s))

Submission status: Time Limit Exceeded

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

rev = 0

for _ in range(q):
    t = [x for x in input().split()]
    if t[0] == "1":
        rev += 1
    else:
        if (t[1] == "1" and rev % 2 == 0) or (t[1] == "2" and rev % 2 != 0):
            s = t[2] + s
        else:
            s += t[2]

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

Submission status: Time Limit Exceeded

Solution code:
def resolve():
    s = input()
    q = int(input())
    qall = []
    for _ in range(q):
        inp = input()
        if inp == "1":
            s = s[::-1]
        else:
            z = inp.split()
            a = int(z[0])
            b = int(z[1])
            c = z[2]
            if b == 1:
                s = s[::-1]
                s = s + c
                s = s[::-1]
            else:
                s = s + c
    print(s)
resolve()

Submission status: Time Limit Exceeded

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

current = a

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

Submission status: Time Limit Exceeded

Solution code:
S =input()
Q = int(input())
T = [0]*Q
for i in range(0,Q):
  T[i] = list(map(str, input().split()))

nS = ""
for i in range(0,Q):
  if(T[i][0] == "1"):
    S = S[::-1]
  else:
    if(T[i][1] == "1"):
      S = T[i][2]+S
    elif(T[i][1] =="2"):
      S = S+T[i][2]

print(S)

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 = "".join(list(reversed(s)))
        continue

    _,f,c = query
    if f=="1":
        s=c+s
    else:
        s+=c

print(s)

Submission status: Time Limit Exceeded

Solution code:
item = [i for i in input()]
n = int(input())
for i in range(n):
    tmp = input()
    if tmp[0] == '1':
        item.reverse()
    else:
        _, q, s = tmp.split()
        if q == '1':
            item = [s] + item
        else:
            item += [s]
print(''.join(item))

Submission status: Time Limit Exceeded

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


for i in range(q):
    List=list(input().split())
    if len(List) > 1 :
      if List[1] == '1' :
        s.insert(0,List[2])
      else:
        s.append(List[2])
    else:
      s.reverse()

ans = ''
for i in range(len(s)):
  ans += s[i]

print(ans)

Submission status: Time Limit Exceeded

Solution code:
s=input()
n=int(input())
q=[]
for i in range(n):
    q.append(input().split())
    if len(q[i])==3:
        if q[i][1]=='1':
            s = q[i][2]+s
        else:
            s= s+q[i][2]
    else:
        s = s[::-1]
print(s)

Submission status: Time Limit Exceeded

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

s = input().split()[0]
q = int(input())

for _ in range(q):
    q_i = input().split()
    if len(q_i) == 1:
        s = s[::-1]
    else:
        _, f, c = q_i
        f = int(f)
        if f == 1:
            s = c + s
        else:
            s = s + c

print(s)

Submission status: Time Limit Exceeded

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

count = 0

for i in range(Q):
  if(int(T[i][0]) == 1):
    count += 1
  else:
    if(int(T[i][2]) == 1):
      if(count % 2 == 0):
        S = T[i][4] + S
      else:
        S = S + T[i][4]
    else:
      if(count % 2 == 0):
        S = S + T[i][4]
      else:
        S = T[i][4] + S

if(count % 2 == 0):
  print(S)
else:
  print(S[::-1])

Submission status: Time Limit Exceeded

Solution code:
S = list(input())
N = int(input())
for i in range(N):
    A = list(input().split())
    if int(A[0]) == 1:
        S.reverse()
    elif int(A[1]) == 1:
        S.insert(0,A[2])
    else:
        S.append(A[2])
print(''.join(S))

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
for _ in range(q):
    query = 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()
n = int(input())

top = True
while n > 0:
    a = input().split()
    if a[0] == '1':
        top = not top
        n -= 1
        continue
    if (a[1] == '1' and top) or (a[1] == '2' and not top):
        s = a[2] + s
    else:
        s += a[2]
    n -= 1

if not top:
    print(s[::-1])
else:
    print(s)

Submission status: Time Limit Exceeded

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


for i in range(Q):
    if Query[i][0]=='1':
        S=''.join(list(reversed(S)))
    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:
#n, m = [int(x) for x in input().split()]

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

for _ in range(q):
  Q = [x for x in input().split()]
  if Q[0] == '1':
    s.reverse()
  else:
    if Q[1] == '1':
      s.insert(0, Q[2])
    else:
      s.append(Q[2])

print('' . join(s))

Submission status: Time Limit Exceeded

Solution code:
S = input()
reverse = 0
front = []
end = []
n = int(input())
for _ in range(n):
  List = list(input().split())
  #print(List)
  if List[0] == '1':
    reverse += 1
    front,end = end[::-1],front[::-1]
  else:
    if List[1] == '1':
      front.insert(0,List[2])
    else:
      end.append(List[2])
if reverse %2 == 1:
  S = S[::-1]
Answer = ''.join(front) + S + ''.join(end)
print(Answer)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
 
for i in range(Q):
    x = input().split()
    if len(x)==1:
        S = S[::-1]
    else:
        x2,string = x[1],x[2]
        if int(x2)==2:
            S = S + string
        else:
            S = string + S
print(S)

Submission status: Time Limit Exceeded

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

for query_list in query:
    if query_list[0] == '1':
        s = s[::-1]
    else:
        if query_list[1] == '1':
            s = query_list[2] + s
        else:
            s = s + query_list[2]

print(s)

Submission status: Time Limit Exceeded

Solution code:
s = list(input())
n = int(input())
for _ in range(n):
    q = input().split()
    if q[0] == "1":
        s.reverse()
    elif q[0] == "2":
        if q[1] == "1":
            s.insert(0,q[2])
        elif q[1] == "2":
            s.append(q[2])
s = "".join(s)
print(s)

Submission status: Time Limit Exceeded

Solution code:
def main():
    S = input()
    Q = int(input())
    S_reverse = S[::-1]

    for i in range(Q):
        q = input()
        if len(q.split(' ')) > 1:
            T, F, C = q.split(' ')
            if F == '1':
                S = C + S
                S_reverse = S_reverse + C 
            elif F == '2':
                S = S + C
                S_reverse = C + S_reverse
        elif q == '1':
            temp = S_reverse
            S_reverse = S
            S = temp

    print(S)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

for i in range(q):
  temp = query[i].split()
  if int(temp[0]) == 1:
    s = ''.join(list(reversed(s)))
#    s = s[::-1]
  else:
    if int(temp[1]) == 1:
      s = '{}{}'.format(temp[2], s)
    else:
      s = '{}{}'.format(s, temp[2])

print(s)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
ans = S
for i in range(Q):
    Query = input()
    if Query[0] == "1":
        ans = ans[::-1]
    elif Query[0:3] == "2 1":
        ans = Query[4]+ans
    elif Query[0:3] == "2 2":
        ans += Query[4]
#     print(ans)
print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
s=input()
q=int(input())
for c in range(q):
    l=input()
    if l=="1":
        s=s[::-1]
    else:
        j=l.split()
        if j[1]=="1":
            s=j[2]+s
        else:
            s=s+j[2]
print(s)

Submission status: Time Limit Exceeded

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

for i in range(Q):
    A = list(map(str,input().split()))
    if A[0] == '1':
        S = S[::-1]
    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())

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

print(S)

Submission status: Time Limit Exceeded

Solution code:
import sys
 
S = input()
 
Q = int(input())
flag=0
 
for i in range(Q):
    r = sys.stdin.readline().strip('\n')
    if r == '1':
        S = S[::-1]
    else:
        ni, f, c = r.split()
        if f=='1':
            S = c + S
        else:
            S+=c
 
print(S)

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
Query = []*Q
for _ in range(Q):
    Query.append(list(map(str,input().split())))

for q in Query:
    if q[0]=='1':
        S = S[::-1]
    else:#q[0]=='2'
        if q[1]=='1':
            S = q[2] + S
        else:#q[1]=='2'
            S = S + q[2]
print(S)

Submission status: Time Limit Exceeded

Solution code:
s = input()
Q = int(input())
flag = True  # flagがTrueのとき、偶数回数の反転が行われている
cnt = 0
for _ in range(Q):
    query = list(map(str, input().split()))
    # tmp.append(query)
    if len(query) == 1:  # 1のqueryのとき
        cnt += 1
        if flag:
            flag = False
        else:
            flag = True
    else:
        f, c = query[1], query[2]
        if f == "1":  # 先頭に追加するもの
            if flag:
                s = c + s  # 偶数回の反転なら素直に先頭に追加する
            else:
                s += c  # 奇数会の反転なら後ろに追加する
        else:  # 末尾に追加する
            if flag:
                s += c
            else:
                s = c + s

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

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
flag = 1
for i in range(q):
    line = input().split();
    if line[0] == '1':
        flag = -flag
    else:
        if line[1] == '1':
            if flag == 1:
                s = line[2] + s
            else:
                s = s + line[2]
        elif line[1] == '2':
            if flag == 1:
                s = s + line[2]
            else:
                s = line[2] + s

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
S = input()
Q = int(input())

count = 0
ans = S

for i in range(Q):
    T = input().split()
    if len(T)==1:
        count+=1
        continue

    if count%2==0:
        if T[1] == '1': ans = T[2] + ans
        else: ans += T[2]
    else:
        if T[1] == '1': ans += T[2]
        else: ans = T[2] + ans

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
s=input()
q=int(input())
hanten=0
mae=""
ato=""
 
for i in range(q):
    query=input()
    if query=="1":
        hanten=(hanten+1)%2
    else:
        if query[2]=="1":
            if hanten==0:
                mae+=query[4]
            else:
                ato+=query[4]
        else:
            if hanten==0:
                ato+=query[4]
            else:
                mae+=query[4]
 
answer=""
if hanten==0:
    lem=len(mae)
    for i in range(lem):
        print(mae[-i-1], end="")
    print(s, end="")
    print(ato)
else:
    lem=len(ato)
    les=len(s)
    for i in range(lem):
        print(ato[-i-1], end="")
    for i in range(les):
        print(s[les-i-1], end="")
    print(mae)

Submission status: Time Limit Exceeded

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

isRev = False

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

Submission status: Time Limit Exceeded

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

reverse = False
head = list()
tail = list()

for i in range(Q):
    operation = input().split()
    if operation[0] == '1':
        reverse = not reverse
        continue

    if operation[1] == '1':
        if reverse:
            tail.append(operation[2])
        else:
            head.insert(0, operation[2])
    else:
        if reverse:
            head.insert(0, operation[2])
        else:
            tail.append(operation[2])

ans = "".join(head) + S + "".join(tail)
if reverse:
    ans = ans[::-1]

print(ans)

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:
    _,a,b = (x for x in query.split())
    if a == '1':
      s = b + s
    elif a == '2':
      s = s + b

print(s)

Submission status: Time Limit Exceeded

Solution code:
s=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+int(li[1]))%2==1:
            s=li[2]+s
        else:
            s=s+li[2]
if n%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):
  _list = input().split(" ")
  if int(_list[0]) == 1:
    S = S[::-1]
  else:
    if int(_list[1]) == 1:
      S = _list[2] + S
    else:
      S = S + _list[2]
print(S)

Submission status: Time Limit Exceeded

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

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

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:
        query = query.split(" ")
        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_num = int(input())

reversed = False

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

Submission status: Time Limit Exceeded

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

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