Buggy python submission (verdict: Time Limit Exceeded):
S=list(input())
Q=int(input())
R_count=0
for i in range(Q):
  Query=list(input().split())
  if Query[0]=="1":
    R_count+=1
  else:
    if Query[1]=="1":
      if R_count%2==0:
        S.insert(0,Query[2])
      else:
        S.append(Query[2])
    else:
      if R_count%2==0:
        S.append(Query[2])
      else:
        S.insert(0,Query[2])
print("".join(S) if R_count%2==0 else "".join(S[::-1]))


Fixed submission:
from collections import deque

S=input()
Q=int(input())
r_flag=False
ans=deque((S))
for i in range(Q):
  Qu=input().split()
  if Qu[0]=="1":
    r_flag=not r_flag
  else:
    if Qu[1]=="1":
      if r_flag==True:
        ans.append(Qu[2])
      else:
        ans.appendleft(Qu[2])
    else:
      if r_flag==True:
        ans.appendleft(Qu[2])
      else:
        ans.append(Qu[2])
ans=list(ans)
print("".join(ans) if r_flag==False else "".join(ans[::-1]))

Buggy python submission (verdict: Time Limit Exceeded):
S=list(input())
Q=int(input())
R_count=0
for i in range(Q):
  Query=list(input().split())
  if Query[0]=="1":
    R_count+=1
  else:
    if Query[1]=="1":
      if R_count%2==0:
        S.insert(0,Query[2])
      else:
        S.append(Query[2])
    else:
      if R_count%2==0:
        S.append(Query[2])
      else:
        S.insert(0,Query[2])
print("".join(S) if R_count%2==0 else "".join(S[::-1]))


Fixed submission:
from collections import deque

S=input()
Q=int(input())
r_flag=False
ans=deque((S))
for i in range(Q):
  Qu=input().split()
  if Qu[0]=="1":
    r_flag=not r_flag
  else:
    if Qu[1]=="1":
      if r_flag==True:
        ans.append(Qu[2])
      else:
        ans.appendleft(Qu[2])
    else:
      if r_flag==True:
        ans.appendleft(Qu[2])
      else:
        ans.append(Qu[2])
ans=list(ans)
print("".join(ans) if r_flag==False else "".join(ans[::-1]))

Buggy python submission (verdict: Time Limit Exceeded):
S=input()
Q=int(input())
R_count=0
for i in range(Q):
  Query=list(filter(lambda a:a!=" ",list(input())))
  if Query[0]=="1":
    R_count+=1
  elif Query[0]=="2":
    if Query[1]=="1":
      if R_count%2==0:
        S=str(Query[2])+S
      else:
        S=S+str(Query[2])
    elif Query[1]=="2":
      if R_count%2==0:
        S=S+str(Query[2])
      else:
        S=str(Query[2])+S
print(S if R_count%2==0 else S[::-1])


Fixed submission:
from collections import deque

S=input()
Q=int(input())
r_flag=False
ans=deque((S))
for i in range(Q):
  Qu=input().split()
  if Qu[0]=="1":
    r_flag=not r_flag
  else:
    if Qu[1]=="1":
      if r_flag==True:
        ans.append(Qu[2])
      else:
        ans.appendleft(Qu[2])
    else:
      if r_flag==True:
        ans.appendleft(Qu[2])
      else:
        ans.append(Qu[2])
ans=list(ans)
print("".join(ans) if r_flag==False else "".join(ans[::-1]))

Buggy python submission (verdict: Time Limit Exceeded):
S=input()
Q=int(input())
for i in range(Q):
  Query=list(filter(lambda a:a!=" ",list(input())))
  if Query[0]=="1":
    S=S[::-1]
  elif Query[0]=="2":
    if Query[1]=="1":
      S=str(Query[2])+S
    elif Query[1]=="2":
      S=S+str(Query[2])
print(S)


Fixed submission:
from collections import deque

S=input()
Q=int(input())
r_flag=False
ans=deque((S))
for i in range(Q):
  Qu=input().split()
  if Qu[0]=="1":
    r_flag=not r_flag
  else:
    if Qu[1]=="1":
      if r_flag==True:
        ans.append(Qu[2])
      else:
        ans.appendleft(Qu[2])
    else:
      if r_flag==True:
        ans.appendleft(Qu[2])
      else:
        ans.append(Qu[2])
ans=list(ans)
print("".join(ans) if r_flag==False else "".join(ans[::-1]))

Buggy python submission (verdict: Time Limit Exceeded):
S=input()
Q=int(input())
for i in range(Q):
  Query=list(filter(lambda a:a!=" ",list(input())))
  if Query[0]=="1":
    S=S[::-1]
  elif Query[0]=="2":
    if Query[1]=="1":
      S=str(Query[2])+S
    elif Query[1]=="2":
      S=S+str(Query[2])
print(S)


Fixed submission:
from collections import deque

S=input()
Q=int(input())
r_flag=False
ans=deque((S))
for i in range(Q):
  Qu=input().split()
  if Qu[0]=="1":
    r_flag=not r_flag
  else:
    if Qu[1]=="1":
      if r_flag==True:
        ans.append(Qu[2])
      else:
        ans.appendleft(Qu[2])
    else:
      if r_flag==True:
        ans.appendleft(Qu[2])
      else:
        ans.append(Qu[2])
ans=list(ans)
print("".join(ans) if r_flag==False else "".join(ans[::-1]))