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:
string=str(input())
Q=int(input())
def func1(s):
    s=s[::-1]
    return s
def func2(s,F,C):
    if F=="1":
        s=C+s
        return s
    if F=="2":
        s=s+C
        return s

            
for i in range(Q):
    Input=list(map(str,input().split()))
    if Input[0]=='1':
        string=func1(string)
        
    elif Input[0]=='2':
        string=func2(string,Input[1],Input[2])

print(string)

Submission status: Time Limit Exceeded

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

f = 1
tail1 = []
head1 = []
for _ in range(q):
    i = list(map(str, input().split()))
    if i[0] == "1":
        f *= -1
    else:
        j,x,s1 = i
        if x == "1":
            if f == 1:
                head1 = [s1] + head1
            else:
                tail1 = tail1 + [s1]
        else:
            if f == -1:
                head1 = [s1] + head1
            else:
                tail1 = tail1 + [s1]

s2 = (head1 + s + tail1)
if f == -1:
    s2.reverse()

print("".join(s2))

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
Que = [input().split(" ") for i in range(Q)]
flip = False

start = ""
end = ""

for i in Que:
    T = i[0]
    if T == "1":
        flip = not(flip)
    else:
        F = i[1]
        C = i[2]
        if (F == "1" and not flip) or (F == "2" and flip):
            S = "".join([C, S])
        else:
            S = "".join([S, C])
print(S if not flip else S[::-1])

Submission status: Time Limit Exceeded

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

f = 1
tail1 = []
head1 = []
for _ in range(q):
    i = list(map(str, input().split()))
    if i[0] == "1":
        f *= -1
    else:
        j,x,s1 = i
        if x == "1":
            if f == 1:
                head1 = [s1] + head1
            else:
                tail1 = tail1 + [s1]
        else:
            if f == -1:
                head1 = [s1] + head1
            else:
                tail1 = tail1 + [s1]

s2 = (head1 + s + tail1)[::f]

print("".join(s2))

Submission status: Time Limit Exceeded

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

f = 1
tail1 = []
head1 = []
for i in q1:
    if i[0] == "1":
        f *= -1
    else:
        j,x,s1 = i
        if (x == "1" and f == 1) or (x == "2" and f == -1):
            head1 = [s1] + head1
        else:
            tail1 = tail1 + [s1]

s2 = (head1 + s + tail1)[::f]

print("".join(s2))

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

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

reverse = 1 #reverse == 1 -> 正常, reverse == -1 -> 逆から

d = deque(S)

for i in range(Q):
    Query_tmp = list(map(str, input().split()))
    
    if Query_tmp[0] == "1":
        reverse *= -1
        # S = S[::-1]
    else:
        Ci = Query_tmp[2]

        if Query_tmp[1] == "1":
            if reverse == 1:
                d.appendleft(Ci)
            else:
                rev_Ci = Ci[::-1]
                d.append(rev_Ci)
        else:
            if reverse == 1:
                d.append(Ci)
            else:
                rev_Ci = Ci[::-1]
                d.appendleft(rev_Ci)
    
    # if reverse == 1:
    #     print(S)
    # else:
    #     print(S[::-1])

length = len(d)
for i in range(length):
    if reverse == -1:
        i = length - i - 1
    print(d[i], end="")
print()

Submission status: Time Limit Exceeded

Solution code:
S=list(input())
Q=int(input())
rev=False
tmp=0
for i in range(Q):
  query=list(input().split())
  if len(query)!=1:
    if rev:
      if query[1]=="1":
        S.append(query[2])
      elif query[1]=="2":
        S.insert(0,query[2])
    else:
      if query[1]=="1":
        S.insert(0,query[2])
      elif query[1]=="2":
        S.append(query[2])
  else:
    tmp+=1
    if rev:
      rev=False
    else:
      rev=True
if tmp%2==0:
  print(''.join(S))
else:
  print(''.join(S[::-1]))

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
Que = [input().split(" ") for i in range(Q)]
flip = False

for i in Que:
    T = int(i[0])
    if T == 1:
        flip = not(flip)
    else:
        F = int(i[1])
        C = i[2]
        if (F == 1 and not flip) or (F == 2 and flip):
            S = C + S
        else:
            S = S + C
print(S if not flip else S[::-1])

Submission status: Time Limit Exceeded

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

for i in Que:
    T = int(i[0])
    if T == 1:
        S = S[::-1]
    else:
        F = int(i[1])
        C = i[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())
rev=False
tmp=0
for i in range(Q):
  query=list(input().split())
  if len(query)!=1:
    if rev:
      if query[1]=="1":
        S=S+query[2]
      elif query[1]=="2":
        S=query[2]+S
    else:
      if query[1]=="1":
        S=query[2]+S
      elif query[1]=="2":
        S=S+query[2]
  else:
    tmp+=1
    if rev:
      rev=False
    else:
      rev=True
if tmp%2==0:
  print(S)
else:
  print(S[::-1])

Submission status: Time Limit Exceeded

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

rev=1

for _ in range(0,q):
  n=input()
  if n[0:1]=="1":
    rev*=-1
  else:
    m=n.split()
    if (m[1]=="1" and rev==1) or (m[1]=="2" and rev==-1):
      s=m[2]+s
    else:
      s=s+m[2]
if rev==1:
  print(s)
else:
  print(s[::-1])

Submission status: Time Limit Exceeded

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

rev=1

for _ in range(0,q):
  n=input()
#  print(n[0:1])
  if n[0:1]=="1":
#    print("!")
    rev*=-1
  else:
    m=n.split()
    x=[]
    if (m[1]=="1" and rev==1) or (m[1]=="2" and rev==-1):
      x.append(m[2])
      x.append(s)
      s=''.join(x)
#      s=m[2]+s
    else:
      x.append(s)
      x.append(m[2])
      s=''.join(x)
#      s=s+m[2]
if rev==1:
  print(s)
else:
  print(s[::-1])

Submission status: Time Limit Exceeded

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

f = ''
b = ''
num = 0
for i in range(q):
    a = input().split()
    if len(a) == 1:
        s = s[::-1]
        f = f[::-1]
        b = b[::-1]
        f,b = b,f
    else :
        if a[1] == '1' :
            f = a[2] + f
        elif a[1] == '2' :
            b = b + a[2]

print(f + s + b)

Submission status: Time Limit Exceeded

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

for i in range(Q):
  query=list(input().split())
  if len(query)!=1:
    if query[1]=="1":
      S=query[2]+S
    elif query[1]=="2":
      S=S+query[2]
  else:
    S=S[::-1]
print(S)

Submission status: Time Limit Exceeded

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

t = False
for _ in range(q_num):
  q = input().split()
  if q[0] == "1":
    t = not t
  else:
    if not t:
      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 t:
  s = s[::-1]
print(s)

Submission status: Time Limit Exceeded

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

for _ in range(0,q):
  n=input()
#  print(n[0:1])
  if n[0:1]=="1":
#    print("!")
    s = s[::-1]
  else:
    m=n.split()
    x=[]
    if m[1]=="1":
      x.append(m[2])
      x.append(s)
      s=''.join(x)
#      s=m[2]+s
    else:
      x.append(s)
      x.append(m[2])
      s=''.join(x)
#      s=s+m[2]
print(s)

Submission status: Time Limit Exceeded

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

for _ in range(0,q):
  n=input()
#  print(n[0:1])
  if n[0:1]=="1":
#    print("!")
    s = s[::-1]
  else:
    m=n.split()
    if m[1]=="1":
      s=m[2]+s
    else:
      s=s+m[2]
print(s)

Submission status: Time Limit Exceeded

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


for i in range(q):
    if quer[i][0] == '1':
        s = ''.join(list(reversed(s)))
    else:
        if quer[i][1] == '1':
            s = quer[i][2] + s
        else:
            s = s + quer[i][2]

print(s)

Submission status: Time Limit Exceeded

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

for i in range(n):
    op = Q[i]
    if len(op) == 1:
        s = s[::-1]
    elif op[1] == "1":
        s = op[2] + s
    else:
        s = s + op[2]
print(s)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

s = deque(list(input()))
q = int(input())
rev = False
for _ in range(q):
    qr = list(map(str, input().split()))
    if qr[0] == '1':
        s.reverse()
    else:
        if qr[1] == '1':
            s.appendleft(qr[2])
        else:
            s.append(qr[2])
print(''.join(s))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

#output
T = "AB"
for i in range(Q):
    if len(P[i]) == 1:
        T = T[-1::-1]
    else:
        v = P[i][2]
        if int(P[i][1]) == 1:
            T = v + T
        else:
            T = T + v

if "AB" in T:
    S = T.replace("AB", S)
else:
    S = T.replace("BA", S[-1::-1])
                        
print(S)

Submission status: Time Limit Exceeded

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

#output
T = "AB"
for i in range(Q):
    if len(P[i]) == 1:
        T = T[-1::-1]
    else:
        v = P[i][2]
        if int(P[i][1]) == 1:
            T = v + T
        else:
            T = T + v

if "AB" in T:
    S = T.replace("AB", S)
else:
    S = T.replace("BA", S[-1::-1])
                        
print(S)

Submission status: Time Limit Exceeded

Solution code:
def func1(S):
  tmp = ''
  for i in range(len(S)):
    tmp = tmp + S[len(S)-1-i]
  return tmp
S = input()
Q = int(input())
rev = False
for i in range(Q):
  query = input().split()
  if query[0] == '1':
    rev = not rev
  elif query[0] == '2':
    if query[1] == '1':
      if rev == False:
        S = query[2] + S
      else:
        S = S + query[2]
    elif query[1] == '2':
      if rev == False:
        S = S + query[2]
      else:
        S = query[2] + S
  #print('S='+S)
if rev == True:
  S = func1(S)
print(S)

Submission status: Time Limit Exceeded

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

#output
for i in range(Q):
    if len(P[i]) == 1:
        S = S[-1::-1]
    else:
        v = P[i][2]
        if int(P[i][1]) == 1:
            S = v + S
        else:
            S = S + v
                        
print(S)

Submission status: Time Limit Exceeded

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

#output
for i in range(Q):
    if len(P[i]) == 1:
        S = S[-1::-1]
    else:
        v = P[i][2]
        if int(P[i][1]) == 1:
            T = v + S
            S = T
        else:
            T = S + v
            S = T
                        
print(S)

Submission status: Time Limit Exceeded

Solution code:
def rev(S):
  tmp = ''
  for i in range(len(S)):
    tmp = tmp + S[len(S)-1-i]
  return tmp

S = input()
Q = int(input())
tmp = S
for i in range(Q):
  query = input().split()
  if query[0] == '1':
    tmp = rev(tmp)
  elif query[0] == '2':
    if query[1] == '1':
      tmp = query[2] + tmp
    elif query[1] == '2':
      tmp = tmp + query[2]
print(tmp)

Submission status: Time Limit Exceeded

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

ans = s
ret = "-1"

tcount = 0
rlist = ""
llist = ""

for i in range(q):
    qret = list(input().split())
    if qret[0] == "1":
        tcount += 1
        # ans = ans[::-1]
    else:
        f = qret[1]
        c = qret[2]
        if f == "1":
            if tcount % 2 == 0:
                llist += c
            else:
                rlist += c
        else:
            if tcount % 2 == 0:
                rlist += c
            else:
                llist += c

if tcount % 2 == 0:
    print(llist[::-1]+ans+rlist)
else:
    print(rlist[::-1]+ans[::-1]+llist)

Submission status: Time Limit Exceeded

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

for i in range(Q):
    if query[i][0]=='1':
        S = S[::-1]
    elif query[i][0]=='2':
        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:
s = input()
q = int(input())

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

Submission status: Time Limit Exceeded

Solution code:
def main():
  s = input()
  for i in range(int(input())):
    values = input().split()
    if values[0] == '1':
      s = s[::-1]
    else:
      s = values[2] + s if values[1] == '1' else s + values[2]
    
  return s 

print(main())

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

S = input()
Q = int(input())
ARR = []

for i in range(Q):
    arr = input().split(" ")
    ARR.append(arr)


def calculate(s,q,arr):

    s = deque(list(s))

    for ar in arr:
        if int(ar[0]) == 1:
            s.reverse()
        if int(ar[0]) == 2:
            if int(ar[1]) == 1:
                s.appendleft(ar[2])
            else:
                s.append(ar[2])
    print("".join(list(s)))

calculate(S,Q,ARR)

Submission status: Time Limit Exceeded

Solution code:
s = input()
n = int(input())
f = 1
for i in range(n):
  l = list(map(str, input().split()))
  if l[0] == "2":
    tmp = int(l[1])*f
    if tmp == 1 or tmp == -2:
      s = l[2] + s
    else:
      s = s + l[2]
  else:
    f *= -1


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

Submission status: Time Limit Exceeded

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

count = 0
for i in range(q):
    t = int(Q[i][0])
    if t==1:
        count += 1
    else:
        f = int(Q[i][1])
        if count%2==0 and f==1:
            s = Q[i][2] + s
        elif count%2==0 and f==2:
            s = s + Q[i][2]
        elif count%2==1 and f==1:
            s = s + Q[i][2]
        elif count%2==1 and f==2:
            s = Q[i][2] + s
        else:
            pass

# print(count)

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

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
Q = [ list(input().split()) for _ in range(q) ]
count = 0
for i in range(q):
    t = int(Q[i][0])
    if t==1:
        count += 1
    else:
        f = int(Q[i][1])
        if count%2==0 and f==1:
            s = Q[i][2] + s
        elif count%2==0 and f==2:
            s = s + Q[i][2]
        elif count%2==1 and f==1:
            s = s + Q[i][2]
        elif count%2==1 and f==2:
            s = Q[i][2] + s
        else:
            pass

# print(count)

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

Submission status: Time Limit Exceeded

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

# print(Q)

for i in range(q):
    t = int(Q[i][0])
    if t==1:
        s = s[::-1]
    else:
        f = int(Q[i][1])
        if f == 1:
            s = Q[i][2] + s
        else:
            s = s + Q[i][2]

print(s)

Submission status: Time Limit Exceeded

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

for _ in range(Q):
    query = input()
    if query[0] == "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:
S=input()
Q=input()
#rev -1:False 0:True
rev=-1
for i in range(int(Q)):
  tmp=input().split()
  if tmp[0]=='1':
    rev = abs(rev)-1
  else:
    if rev+int(tmp[1])==1:
      S = S + tmp[2]
    else:
      S = tmp[2] + S
if rev==-1:
  print(S)
else:
  print(S[::-1])

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
S=input()
q=int(input())
rev=False
pre=''
for i in range(q):
    inp=input()
    if inp[0]=="1":
        rev=not rev
    else:
        t,f,c=inp.split()
        if (f=='1' and rev) or (f=='2' and not rev):
            S+=c
        else:
            pre+=c
if not rev:
    print(''.join(list(reversed(pre)))+S)
else:
    print(''.join(list(reversed(S)))+pre)

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
r = False
for _ in range(q):
  cmd = input()
  if cmd[0] == '1':
    r = not r
  else:
    t,f,c = cmd.split()
    if (f == '1' and not r) or (f == '2' and r):
      s = c + s
    else:
      s = s + c
if r:
	print(s[::-1])
else:
	print(s)

Submission status: Time Limit Exceeded

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

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

print(s)

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
for _ in range(q):
  cmd = input()
  if cmd[0] == '1':
    s = s[::-1]
  else:
    t,f,c = cmd.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())
rev=False
pre=''
for i in range(q):
    inp=input()
    if inp[0]=="1":
        rev=not rev
    else:
        t,f,c=inp.split()
        if (f=='1' and rev) or (f=='2' and not rev):
            S+=c
        else:
            pre+=c
if not rev:
    print(pre[::-1]+S)
else:
    print(S[::-1]+pre)

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
def main():
    S = deque([s for s in input()])
    Q = int(input())
    for i in range(Q):
        query = input().split()
        t = int(query[0])
        if t == 1:
            S.reverse()
        else:
            f = int(query[1])
            c = query[2]
            if f == 1:
                S.appendleft(c)
            else:
                S.append(c)
    print(''.join(S))

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
S = deque([s for s in input()])
Q = int(input())
for i in range(Q):
    query = input().split()
    t = int(query[0])
    if t == 1:
        S.reverse()
    else:
        f = int(query[1])
        c = query[2]
        if f == 1:
            S.appendleft(c)
        else:
            S.append(c)
print(''.join(S))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
reverse = False
for i in range(Q):
    Query = input()
    if Query[0] == '1':
        reverse = not reverse 
    else:
        if (Query[2] == '1' and reverse == False) or (Query[2] == '2' and reverse == True):
            S = Query[4] + S
        else:
            S = S + Query[4]

if reverse == True:
    S = S[::-1]


print(S)
print('\n')

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
reverse = False
for i in range(Q):
    Query = input()
    T = int(Query[0])
    if T == 1:
        reverse = not reverse 
    else:
        F = int(Query[2])
        C = Query[4]
        if (F == 1 and reverse == False) or (F == 2 and reverse == True):
            S = C + S
        else:
            S = S + C

if reverse == True:
    S = S[::-1]


print(S)
print('\n')

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
reverse = False
for i in range(Q):
    Query = input()
    T = int(Query[0])
    if T == 1:
        reverse = not reverse 
    elif T == 2:
        F = int(Query[2])
        C = Query[4]
        if (F == 1 and reverse == False) or (F == 2 and reverse == True):
            S = C + S
        elif (F == 2 and reverse == False) or (F == 1 and reverse == True):
            S = S + C

if reverse == True:
    S = S[::-1]


print(S)
print('\n')

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
reverse = False
for i in range(Q):
    Query = input()
    T = int(Query[0])
    if T == 1:
        reverse = not reverse 
    elif T == 2:
        F = int(Query[2])
        C = Query[4]
        if (F == 1 and reverse == False) or (F == 2 and reverse == True):
            S = C + S
        elif (F == 2 and reverse == False) or (F == 1 and reverse == True):
            S = S + C

if reverse%2 == 1:
    S = S[::-1]


print(S)
print('\n')

Submission status: Time Limit Exceeded

Solution code:
S = list(input())
Q = int(input())
reverse = False
for i in range(Q):
    Query = input()
    T = int(Query[0])
    if T == 1:
        reverse = not reverse 
    elif T == 2:
        F = int(Query[2])
        C = Query[4]
        if (F == 1 and reverse == False) or (F == 2 and reverse == True):
            S.insert(0, C)
        elif (F == 2 and reverse == False) or (F == 1 and reverse == True):
            S.append(C)

if reverse%2 == 1:
    S = S[::-1]


print(''.join(S))
print('\n')

Submission status: Time Limit Exceeded

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

for query in range( q ):
  qi = input()
  if len( qi ) == 1:
    s = ''.join(list(reversed(s)))
  else:
    n, f, c = qi.split()
    if f == "1":
      s = c + s
    else:
      s = s + c

print( s )

Submission status: Time Limit Exceeded

Solution code:
S = list(input())
Q = int(input())
reverse = 0
for i in range(Q):
    Query = input()
    T = int(Query[0])
    if T == 1:
        reverse += 1
    elif T == 2:
        F = int(Query[2])
        C = Query[4]
        if (F+reverse)%2 == 1:
            S.insert(0, C)
        elif (F+reverse)%2 == 0:
            S.append(C)

if reverse%2 == 1:
    S = S[::-1]


print(''.join(S))
print('\n')

Submission status: Time Limit Exceeded

Solution code:
S = list(input())
Q = int(input())
reverse = 0
for i in range(Q):
    Query = input()
    T = int(Query[0])
    if T == 1:
        reverse += 1
    elif T == 2:
        F = int(Query[2])
        C = Query[4]
        if (F+reverse)%2 == 1:
            S.insert(0, C)
        elif (F+reverse)%2 == 0:
            S.append(C)

if reverse%2 == 1:
    temp = list(S)
    for j in range(len(S)):
        S[j] = temp[-1-j]


print(''.join(S))
print('\n')

Submission status: Time Limit Exceeded

Solution code:
S = list(input())
Q = int(input())
for i in range(Q):
    Query = input()
    T = int(Query[0])
    if T == 1:
        temp = list(S)
        for j in range(len(S)):
            S[j] = temp[-1-j]
    elif T == 2:
        F = int(Query[2])
        C = Query[4]
        if F == 1:
            S.insert(0, C)
        elif F == 2:
            S.append(C)



print(''.join(S))
print('\n')

Submission status: Time Limit Exceeded

Solution code:
import sys
s = sys.stdin.readline().rstrip()
q = int(sys.stdin.readline())
for _ in range(q):
    qi = sys.stdin.readline()
    if len(qi) <= 2:
        s = s[::-1]
    else:
        _,F,C = qi.split()
        if F == "1":
            s = C + s
        else:
            s = s + C           
print(s)

Submission status: Time Limit Exceeded

Solution code:
import math
import numpy as np

s=input()
q=int(input())
for i in range(1,q+1):
    v=input()  
    if int(v[0])==1:
        s=s[::-1]
    else:
        if int(v[2])==1:
            s=v[4]+s
        else:
            s+=v[4]
print(s)

Submission status: Time Limit Exceeded

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

s = list(input())
q = int(input())
reverse_counter = 0
for q0 in range(q):
    A = [i for i in input().split()]
    if len(A) == 1:
        reverse_counter += 1
    else:
        if A[1] == '1':
            if reverse_counter % 2 == 0:
                s.insert(0, A[2])
            else:
                s.append(A[2])
        else:
            if reverse_counter % 2 == 0:
                s.append(A[2])
            else:
                s.insert(0, A[2])

if reverse_counter % 2 == 1:
    s.reverse()

print(''.join(s))

Submission status: Time Limit Exceeded

Solution code:
import math
import numpy as np

s=input()
q=int(input())
for i in range(1,q+1):
    v=input()  
    if int(v[0])==1:
        s=s[::-1]
    else:
        if int(v[2])==1:
            s=s[::-1]
            s+=v[4:]
            s=s[::-1]
        else:
            s+=v[4:]
print(s)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = lambda : sys.stdin.readline().rstrip()
s = input()
q = int(input())
for _ in range(q):
    qi = input()
    if len(qi) <= 2:
        s = s[::-1]
    else:
        _,F,C = qi.split()
        if F == "1":
            s = C + s
        else:
            s = s + C           
print(s)

Submission status: Time Limit Exceeded

Solution code:
import sys
s = input()
q = int(input())
for _ in range(q):
    qi = input()
    if len(qi) <= 2:
        s = s[::-1]
    else:
        _,F,C = qi.split()
        if F == "1":
            s = C + s
        else:
            s = s + C           
print(s)

Submission status: Time Limit Exceeded

Solution code:
S = input()

Q = int(input())

for _ in range(Q):
    op = input().split()
    if len(op) == 1:
        S = S[::-1]
    else:
        if op[1] == '1':
            S = op[2] + S
        else:
            S = S + op[2]
print(S)

Submission status: Time Limit Exceeded

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

reverse = False

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

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

Submission status: Time Limit Exceeded

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

reverse = False

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

if not reverse:
    print(S)
else:
    for i in range(1, len(S)+1):
        print(S[-i], end='')

Submission status: Time Limit Exceeded

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

for ss in range(q):
    qi = input()
    if len(qi) <= 2:
        s = s[::-1]
    else:
        _,F,C = qi.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())

# 逆フラグ
r = 1  # 1==正
for q in range(Q):
  s = input()
  if len(s) == 1:
    r *= -1
  else:
    s, f, c = s.split()
    if f == '1':
      if r == 1:
        S = c + S
      else:
        S = S + c
    else:
      if r == 1:
        S = S + c
      else:
        S = c + S
if r == -1:
  S = S[::-1]
print(S)

Submission status: Time Limit Exceeded

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

r = False

for i in range(q):
    ts = input().split()
    if len(ts) == 1:
        r = not r
        # print(f"reverse: {r}")
    else:
        f, c = int(ts[1]), ts[2]
        if (r and f == 1) or (not r and f == 2):
            # 最後尾
            s += c
        else:
            # 先頭
            s = c + s
if not r:
    print(s)
else:
    print(s[::-1])