Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Time Limit Exceeded):
import math
q = int(input())
list_range = [list(map(int, input().split())) for _ in range(q)]

max_range = math.sqrt(10**5)
list_search = [i for i in range(2, 10**5 + 1)]
list_prime = []
while list_search[0] <= max_range:
  list_prime.append(list_search[0])
  temp = list_search[0]
  list_search = [i for i in list_search if i % temp != 0]
list_prime.extend(list_search)

list_satisfy = []
for one_prime in list_prime:
  if one_prime * 2 > 10**5:
    break
  if one_prime * 2 - 1 in list_prime:
    list_satisfy.append(one_prime * 2 - 1)

result = [0 for _ in range(q)]
for one_num in list_satisfy:
  for i in range(q):
    if list_range[i][0] <= one_num <= list_range[i][1]:
      result[i] += 1
for one_result in result:
  print(one_result)

Fixed submission:
q = int(input())
list_range = [list(map(int, input().split())) for _ in range(q)]

list_flag_prime = [1 for i in range(10**5+1)]
list_flag_prime[0:2] = [0, 0]
for i in range(2, 10**5+1):
  if list_flag_prime[i] == 1:
    for j in range(2*i, 10**5+1, i):
      list_flag_prime[j] = 0

list_subj = [0 for i in range(10**5+1)]
for i in range(3, 10**5+1, 2):
  if list_flag_prime[i] == 1 & list_flag_prime[int((i+1)/2)] == 1:
    list_subj[i] += 1

list_satisfy = [0 for i in range(10**5+1)]
for i in range(1, 10**5+1):
  list_satisfy[i] += list_satisfy[i-1] + list_subj[i]

for l, r in list_range:
  print(list_satisfy[r] - list_satisfy[l-1])

Buggy python submission (verdict: Time Limit Exceeded):
import math
q = int(input())
list_range = [list(map(int, input().split())) for _ in range(q)]

max_range = math.sqrt(10**5)
list_search = [i for i in range(2, 10**5 + 1)]
list_prime = []
while list_search[0] <= max_range:
  list_prime.append(list_search[0])
  temp = list_search[0]
  list_search = [i for i in list_search if i % temp != 0]
list_prime.extend(list_search)

list_satisfy = []
for one_prime in list_prime:
  if one_prime * 2 > 10**5:
    break
  if one_prime * 2 - 1 in list_prime:
    list_satisfy.append(one_prime * 2 - 1)

for l, r in list_range:
  result = len([i for i in list_satisfy if l <= i <= r])
  print(result)

Fixed submission:
q = int(input())
list_range = [list(map(int, input().split())) for _ in range(q)]

list_flag_prime = [1 for i in range(10**5+1)]
list_flag_prime[0:2] = [0, 0]
for i in range(2, 10**5+1):
  if list_flag_prime[i] == 1:
    for j in range(2*i, 10**5+1, i):
      list_flag_prime[j] = 0

list_subj = [0 for i in range(10**5+1)]
for i in range(3, 10**5+1, 2):
  if list_flag_prime[i] == 1 & list_flag_prime[int((i+1)/2)] == 1:
    list_subj[i] += 1

list_satisfy = [0 for i in range(10**5+1)]
for i in range(1, 10**5+1):
  list_satisfy[i] += list_satisfy[i-1] + list_subj[i]

for l, r in list_range:
  print(list_satisfy[r] - list_satisfy[l-1])
Buggy python submission (verdict: Runtime Error):
n = int(input())
list_p = list(map(int, input().split()))
same_p = [1 if i+1 == p else 0 for i, p in enumerate(list_p)]

count = 0
i = 0
while i < n:
  if sum(same_p) == 0:
    break
  if same_p[i] == 1:
    temp_p = list_p[i]
    list_p[i] = list_p[i+1]
    list_p[i+1] = temp_p
    same_p[i] = 0
    same_p[i+1] = 0
    if list_p[i+1] == i+2:
      same_p[i+1] = 1
    count += 1
  i += 1
print(count)

Fixed submission:
n = int(input())
list_p = list(map(int, input().split()))
count = 0
i = 0
while i < n:
  if list_p[i] == i + 1:
    count += 1
    i += 2
  else:
    i += 1
    continue
print(count)
Buggy python submission (verdict: Runtime Error):
n = int(input())
list_a = list(map(int, input().split()))
num_rank = [0 for _ in range(9)]
for one_a in list_a:
  temp_rank = one_a // 400
  num_rank[temp_rank] += 1
if len([i for i in num_rank[:-1] if i > 0]) > 0:
  min_color = len([i for i in num_rank[:-1] if i > 0])
else:
  min_color = 1
max_color = len([i for i in num_rank[:-1] if i > 0]) + num_rank[-1]
print("{} {}".format(min_color, max_color))

Fixed submission:
n = int(input())
list_a = list(map(int, input().split()))
num_rank = [0 for _ in range(9)]
for one_a in list_a:
  temp_rank = one_a // 400
  if temp_rank > 8:
    temp_rank = 8
  num_rank[temp_rank] += 1
if len([i for i in num_rank[:-1] if i > 0]) > 0:
  min_color = len([i for i in num_rank[:-1] if i > 0])
else:
  min_color = 1
max_color = len([i for i in num_rank[:-1] if i > 0]) + num_rank[-1]
print("{} {}".format(min_color, max_color))

Buggy python submission (verdict: Runtime Error):
n = int(input())
list_a = list(map(int, input().split()))
num_rank = [0 for _ in range(9)]
for one_a in list_a:
  temp_rank = one_a // 400
  num_rank[temp_rank] += 1

min_color = len([i for i in num_rank[:-1] if i > 0])
max_color = len([i for i in num_rank[:-1] if i > 0]) + num_rank[-1]
print("{} {}".format(min_color, max_color))

Fixed submission:
n = int(input())
list_a = list(map(int, input().split()))
num_rank = [0 for _ in range(9)]
for one_a in list_a:
  temp_rank = one_a // 400
  if temp_rank > 8:
    temp_rank = 8
  num_rank[temp_rank] += 1
if len([i for i in num_rank[:-1] if i > 0]) > 0:
  min_color = len([i for i in num_rank[:-1] if i > 0])
else:
  min_color = 1
max_color = len([i for i in num_rank[:-1] if i > 0]) + num_rank[-1]
print("{} {}".format(min_color, max_color))
Buggy python submission (verdict: Time Limit Exceeded):
n, a, b = map(int, input().split())
list_enemy = [int(input()) for _ in range(n)]
list_enemy.sort(reverse=True)
sub_list = list_enemy
count = 0
while len(sub_list) > 0:
  temp_list = [one_e - b for one_e in sub_list[1:] if one_e - b > 0]
  if sub_list[0] > a:
    temp_list.insert(0, sub_list[0] - a)
  sub_list = temp_list
  sub_list.sort(reverse=True)
  count += 1
print(count)

Fixed submission:
import math
n, a, b = map(int, input().split())
list_enemy = [int(input()) for _ in range(n)]
list_enemy.sort(reverse=True)

diff_dam = a - b
min_a = math.ceil(list_enemy[-1] / a)
max_a = math.ceil(list_enemy[0] / b)
while max_a - min_a > 1:
  mid_a = min_a + (max_a - min_a) // 2
  count = 0
  for one_enemy in list_enemy:
    rest_h = one_enemy - (b * mid_a)
    if rest_h > 0:
      count += math.ceil(rest_h / diff_dam)
    else:
      break
  if count <= mid_a:
    max_a = mid_a
  else:
    min_a = mid_a
print(max_a)
Buggy python submission (verdict: Runtime Error):
import math
n = int(input())
sqrt_n = math.ceil(math.sqrt(n))

list_cand = []
for i in range(1, sqrt_n):
  if n % i == 0:
    list_cand.append(max(len(str(i)), len(str(n // i))))

print(min(list_cand))

Fixed submission:
import math
n = int(input())
sqrt_n = math.ceil(math.sqrt(n))

result = 100
for i in range(1, sqrt_n + 1):
  if n % i == 0:
    digit = max(len(str(i)), len(str(n // i)))
    if digit < result:
      result = digit

print(result)
Buggy python submission (verdict: Time Limit Exceeded):
import itertools
n, ma, mb = map(int, input().split(" "))
info_med = []
for _ in range(n):
  info_med.append(list(map(int, input().split(" "))))

min_cost = 10000
for num_med in range(1, n + 1):
  list_med = [i for i in range(n)]
  list_select = itertools.combinations(list_med, num_med)
  for one_select in list_select:
    sum_ma = sum([info_med[one_med][0] for one_med in one_select])
    sum_mb = sum([info_med[one_med][1] for one_med in one_select])
    if sum_ma / sum_mb == ma / mb:
      sum_c = sum([info_med[one_med][2] for one_med in one_select])
      if sum_c < min_cost:
        min_cost = sum_c

if min_cost == 10000:
  print(-1)
else:
  print(min_cost)

Fixed submission:
n, ma, mb = map(int, input().split(" "))
info_med = []
for _ in range(n):
  info_med.append(list(map(int, input().split(" "))))
info_med.sort(key=lambda x: x[2])
dp = []
dp.append([(0, 0, [-1])])

for min_c in range(1, n * 100 + 1):
  temp_list = []
  for i, (a, b, c) in enumerate(info_med):
    if min_c - c >= 0:
      for pre_a, pre_b, pre_i in dp[min_c - c]:
        if pre_i[-1] < i:
          if (a + pre_a) * mb == (b + pre_b) * ma:
            print(min_c)
            exit()
          temp_list.append((a + pre_a, b + pre_b, pre_i + [i]))
  dp.append(temp_list)
print("-1")
Buggy python submission (verdict: Runtime Error):
import heapq

def dijkstra(graph, node, start):
  INF = float("inf")
  dist = [INF] * node
  dist[start] = 0
  que = [(0, start)]
  while que:
    cost, one_node = heapq.heappop(heap)
    for next_c, next_n in graph[one_node]:
      dist_candi = dist[one_node] + next_c
      if dist_candi < dist[next_n]:
        dist[next_node] = dist_candi
        heapq.heappush(heap, (dist[next_n], next_n))
  return dist

class SetGraph:
  def __init__(self, node_num):
    self.graph = [[] for _ in range(node_num)]

  def add_one_edge(self, start, end, cost):
    self.graph[start].append((cost, end))
    self.graph[end].append((cost, start))

  def get_graph(self):
    return self.graph 
  

n, m = map(int, input().split(" "))
graph = SetGraph(n)
add_process = SetGraph.add_one_edge
for i in range(m):
  a, b, c = map(int, input().split(" "))
  add_process(a - 1, b - 1, c)
graph = Graph.get_graph()

cost = []
for i in range(n):
  temp_cost = dijkstra(graph, n, i)
  cost.append(temp_cost)

result = 0
for i in range(n):
  for c, node in graph[i]:
    if cost[i][node] != c:
      result += 1

print(result // 2)

Fixed submission:
import heapq

def dijkstra(graph, node, target):
  INF = float("inf")
  dist = [INF] * node
  dist[target] = 0
  que = [(0, target)]
  while que:
    co, one_node = heapq.heappop(que)
    for next_c, next_n in graph[one_node]:
      dist_candi = dist[one_node] + next_c
      if dist_candi < dist[next_n]:
        dist[next_n] = dist_candi
        heapq.heappush(que, (dist[next_n], next_n))
  return dist


class SetGraph:
  def __init__(self, node_num):
    self.n = node_num
    self.graph = [[] for _ in range(self.n)]

  def add_one_edge(self, start, end, cost):
    self.graph[start].append((cost, end))
    self.graph[end].append((cost, start))

  def get_graph(self):
    return self.graph 
  

n, m = map(int, input().split(" "))
graph_set = SetGraph(n)
for i in range(m):
  a, b, c = map(int, input().split(" "))
  graph_set.add_one_edge(a - 1, b - 1, c)
graph = graph_set.get_graph()

cost = []
for i in range(n):
  temp_cost = dijkstra(graph, n, i)
  cost.append(temp_cost)

result = 0
for i in range(n):
  for c, node in graph[i]:
    if cost[i][node] != c:
      result += 1

print(result // 2)
Buggy python submission (verdict: Runtime Error):
a, b, c = map(int, input().split(" "))
if a % 2 = 0 and b % 2 = 0 and c % 2 = 0:
  print("Yes")
else:
  print("No")

Fixed submission:
a, b, c = map(int, input().split(" "))
if a + b == c or b + c == a or c + a == b:
  print("Yes")
else:
  print("No")
Buggy python submission (verdict: Time Limit Exceeded):
n, t = map(int, input().split(" "))
list_a = list(map(int, input().split(" ")))
list_profit = []
for i, a in enumerate(list_a[:-1]):
  max_price = max(list_a[(i+1):])
  if a < max_price:
    list_profit.append(max_price - a)
  else:
    list_profit.append(0)
max_profit = max(list_profit)
print(list_profit.count(max_profit))

Fixed submission:
n, t = map(int, input().split(" "))
list_a = list(map(int, input().split(" ")))
list_profit = []
min_price = list_a[0]
for a in list_a:
  if min_price < a:
    list_profit.append(a - min_price)
  else:
    list_profit.append(0)
    min_price = a
max_profit = max(list_profit)
print(list_profit.count(max_profit))
Buggy python submission (verdict: Time Limit Exceeded):
s = input()
n = len(s)
list_count = []
for i in range(1, n + 1):
  sub_str = s[:i]
  count_g = sub_str.count("g")
  count_p = sub_str.count("p")
  list_count.append(count_g - count_p)

point = 0
while list_count[-1] >= 2:
  for back_index in range(n)[::-1]:
    if list_count[back_index] < 2:
      list_count = [value if index <= back_index else value - 2 for index, value in enumerate(list_count)]
      point += 1
      break
print(point)

Fixed submission:
s = input()
point = 0
count_g = 0
count_p = 0
for enemy_move in s:
  if enemy_move == "g":
    if count_g > count_p:
      point += 1
      count_p += 1
    else:
      count_g += 1
  else:
    if count_g > count_p:
      count_p += 1
    else:
      count_g += 1
      point -= 1
print(point)
Buggy python submission (verdict: Runtime Error):
import numpy as np
h, w, n = map(int, input().split(" "))
board = np.zeros((h, w))
for i in range(n):
  a, b = map(int, input().split(" "))
  board[a - 1][b - 1] = 1
result = [0 for _ in range(10)]
for start_h in range(h - 2):
  for start_w in range(w - 2):
    sum_black = np.count_nonzero(board[start_h:(start_h+3), start_w:(start_w + 3)] > 0)
    result[sum_black] += 1

for one_result in result:
  print(one_result)

Fixed submission:
import collections
h, w, n = map(int, input().split(" "))
list_include = []
for i in range(n):
  a, b = map(int, input().split(" "))
  for y in range(3):
    for x in range(3):
      if a - y > 0 and a - y + 2 <= h:
        if b - x > 0 and b - x + 2 <= w:
          list_include.append((a - y, b - x))
count = collections.Counter(list_include)
result = [0 for _ in range(10)]
for item in count.items():
  result[item[1]] += 1
result[0] = (h - 2) * (w - 2) - sum(result)
for one_result in result:
  print(one_result)
Buggy python submission (verdict: Time Limit Exceeded):
import math
n = int(input())
s = int(input())

result = -1
if n == s:
  result = n + 1
elif s > n:
  pass
else:
  sqrt_n = math.floor(math.sqrt(n))
  for b in range(2, sqrt_n + 1):
    temp_n = n
    temp_b = 0
    while temp_n >= b:
      temp_b += temp_n % b
      temp_n = temp_n // b
    if temp_b + temp_n == s:
      result = b
      break
  if result == -1:
    for quot in range(sqrt_n, 0, -1):
      if n - s % quot != 0:
        b = (n - s) // quot + 1
        temp_n = n
        temp_b = 0
        while temp_n >= b:
          temp_b += temp_n % b
          temp_n = temp_n // b
        if temp_b + temp_n == s:
          result = b
          break
print(result)

Fixed submission:
import math
n = int(input())
s = int(input())

result = -1
if n == s:
  result = n + 1
elif s > n:
  pass
else:
  temp_sqrt_n = math.floor(math.sqrt(n))
  for b in range(2, temp_sqrt_n + 1):
    temp_n = n
    temp_b = 0
    while temp_n >= b:
      temp_b += temp_n % b
      temp_n = temp_n // b
    if temp_b + temp_n == s:
      result = b
      break
  if result == -1:
    temp_sqrt_n = math.floor(math.sqrt(n - 1))
    for quot in range(temp_sqrt_n, 0, -1):
      if n - s % quot != 0:
        b = (n - s) // quot + 1
        if quot + n % b == s:
          result = b
          break
print(result)
Buggy python submission (verdict: Time Limit Exceeded):
import math
h, w, a, b = map(int, input().split(" "))
count = 0
for inter_h in range(h - a):
    molec = math.factorial(b - 1 + inter_h)
    denom = math.factorial(b - 1) * math.factorial(inter_h)
    comb_1 = molec // denom
    molec = math.factorial(w - b - 1 + h - inter_h - 1)
    denom = math.factorial(w - b - 1) * math.factorial(h - inter_h - 1)
    comb_2 = molec // denom
    count += (comb_1 * comb_2) % (10 ** 9 + 7)
rem = count % (10 ** 9 + 7)
print(rem)

Fixed submission:
h, w, a, b = map(int, input().split(" "))
div = 10 ** 9 + 7
size = h + w - 1

fact = [0] * size
inverse = [0] * size
inv_calced = [0] * size
fact[:2] = [1, 1]
inverse[:2] = [1, 1]
inv_calced[:2] = [0, 1]

for i in range(2, size):
  fact[i] = fact[i - 1] * i % div
  inv_calced[i] = -inv_calced[div % i] * (div // i) % div
  inverse[i] = inverse[i - 1] * inv_calced[i] % div
  
result = 0
for x in range(b, w):
  y = h - a - 1
  temp_1 = (fact[x+y] * inverse[x] % div) * inverse[y] % div
  temp_2 = (fact[h+w-x-y-3] * inverse[w-x-1] % div) * inverse[h-y-2] % div
  result += ((temp_1 % div) * (temp_2 % div)) % div

print(result % div)
Buggy python submission (verdict: Runtime Error):
n=int(input())
a=n[0]
if a in list('24579'):
  ans='hon'
elif a in list('0168'):
  ans='pon'
else:
  ans='bon'

print(ans)

Fixed submission:
# 1
# 2 
# 3
n=input()
a=n[-1]
if a in list('24579'):
  ans='hon'
elif a in list('0168'):
  ans='pon'
else:
  ans='bon'

print(ans)
Buggy python submission (verdict: Runtime Error):
N,M,Q=map(int,input().split())
que=[tuple(map(int,input().split())) for _ in range(Q)]
task=[]
sta=[str(i) for i in range(1,M+1)]
while sta:
    s=sta.pop()
    if len(s)==N: task.append(s)
    else:
        for d in range(int(s[-1]),M+1):
            sta.append(s+str(d))
ans=0
for s in task:
    A=[int(i) for i in s]
    tmp=0
    for a,b,c,d in que:
        if A[b-1]-A[a-1]==c: tmp+=d
    ans=max(ans,tmp)
print(ans)

Fixed submission:
N,M,Q=map(int,input().split())
que=[tuple(map(int,input().split())) for _ in range(Q)]
task=[]
sta=[[i] for i in range(1,M+1)]
while sta:
    s=sta.pop()
    if len(s)==N: task.append(s)
    else:
        for d in range(int(s[-1]),M+1):
            sta.append(s+[d])
ans=0
for s in task:
    tmp=0
    for a,b,c,d in que:
        if s[b-1]-s[a-1]==c: tmp+=d
    ans=max(ans,tmp)
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
MOD=2019

S=list(int(s) for s in input()[::-1])
T=[S[0]]
for i in range(1,len(S)):
    T.append(T[-1]+S[i]*(10**i))
cnt={}
for i in range(2019):
    cnt[i]=0
for t in T:
    cnt[t%MOD]+=1
ans=0
for i in range(2019):
    c=cnt[i]
    if c>=2:
        ans+=c*(c-1)//2
ans+=cnt[0]
print(ans)

Fixed submission:
MOD=2019

S=list(int(s) for s in input()[::-1])
T=[S[0]]
for i in range(1,len(S)):
    T.append(T[-1]+S[i]*pow(10,i,MOD))
cnt={}
for i in range(2019):
    cnt[i]=0
for t in T:
    cnt[t%MOD]+=1
ans=0
for i in range(2019):
    c=cnt[i]
    if c>=2:
        ans+=c*(c-1)//2
ans+=cnt[0]
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
MOD=2019

S=list(int(s) for s in input()[::-1])
T=[S[0]]
for i in range(1,len(S)):
    T.append(T[-1]+S[i]*(10**i))
cnt={}
for i in range(2019):
    cnt[i]=0
for t in T:
    cnt[t%MOD]+=1
ans=0
for i in range(2019):
    c=cnt[i]
    if c>=2:
        ans+=c*(c-1)//2
ans+=cnt[0]
print(ans)

Fixed submission:
MOD=2019

S=list(int(s) for s in input()[::-1])
T=[S[0]]
for i in range(1,len(S)):
    T.append(T[-1]+S[i]*pow(10,i,MOD))
cnt={}
for i in range(2019):
    cnt[i]=0
for t in T:
    cnt[t%MOD]+=1
ans=0
for i in range(2019):
    c=cnt[i]
    if c>=2:
        ans+=c*(c-1)//2
ans+=cnt[0]
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
N=int(input())
S=input()
order=list('RGB')
cnt=[[0,0,0] for _ in range(N)]
for i in range(N):
    cnt[i][order.index(S[i])]+=1
    if i!=0:
        for j in range(3):
            cnt[i][j]+=cnt[i-1][j]
ans=0
for j in range(1,N):
    for k in range(j+1,N):
        # print(j,k)
        if S[j]==S[k]:
            continue
        else:
            s=list(set(order)-set([S[j],S[k]]))[0]
            ans+=cnt[j-1][order.index(s)]
            # print('db',ans)
            i=2*j-k
            if i>=0 and S[i]==s:
                # print('!')
                ans-=1
print(ans)


Fixed submission:
# D
N=int(input())
S=input()
order=list('RGB')
cnt=[[0,0,0] for _ in range(N)]
for i in range(N):
    cnt[i][order.index(S[i])]+=1
    if i!=0:
        for j in range(3):
            cnt[i][j]+=cnt[i-1][j]
ans=0
for j in range(1,N):
    for k in range(j+1,N):
        # print(j,k)
        if S[j]==S[k]:
            continue
        else:
            s=list(set(order)-set([S[j],S[k]]))[0]
            ans+=cnt[j-1][order.index(s)]
            # print('db',ans)
            i=2*j-k
            if i>=0 and S[i]==s:
                # print('!')
                ans-=1
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
sel='D'

if sel=='A':
    S=input()
    if S[2]==S[3] and S[4]==S[5]:
        print('Yes')
    else:
        print('No')

if sel=='B':
    X=int(input())
    ans=0
    ans+=(X//500)*1000
    ans+=((X%500)//5)*5
    print(ans)

if sel=='C':
    K,N=map(int,input().split())
    A=[int(i) for i in input().split()]
    d=[A[0]+K-A[-1]]
    for i in range(N-1):
        d.append(A[i+1]-A[i])
    d.sort()
    print(K-d[-1])

if sel=='D':
    N,X,Y=map(int,input().split())
    cnt={}
    for k in range(1,N):
        cnt[k]=0
    for i in range(1,N):
        for j in range(i+1,N+1):
            k=j-i # まっすぐ
            k=min(k,abs(i-X)+1+abs(j-Y)) # う回路１
            k=min(k,abs(i-Y)+1+abs(X-j)) # う回路２
            cnt[k]+=1

    for k in range(1,N):
        print(cnt[k])





Fixed submission:
sel='D'

if sel=='A':
    S=input()
    if S[2]==S[3] and S[4]==S[5]:
        print('Yes')
    else:
        print('No')

if sel=='B':
    X=int(input())
    ans=0
    ans+=(X//500)*1000
    ans+=((X%500)//5)*5
    print(ans)

if sel=='C':
    K,N=map(int,input().split())
    A=[int(i) for i in input().split()]
    d=[A[0]+K-A[-1]]
    for i in range(N-1):
        d.append(A[i+1]-A[i])
    d.sort()
    print(K-d[-1])

if sel=='D':
    N,X,Y=map(int,input().split())
    cnt={}
    for k in range(1,N):
        cnt[k]=0
    for i in range(1,N):
        for j in range(i+1,N+1):
            k=j-i # まっすぐ
            k=min(k,abs(i-X)+1+abs(j-Y)) # う回路１
            k=min(k,abs(i-Y)+1+abs(X-j)) # う回路２
            cnt[k]+=1

    for k in range(1,N):
        print(cnt[k])
Buggy python submission (verdict: Runtime Error):
sel='B'
#A
if sel=='A':
    N,M=map(int,input().split())
    ans=0
    ans+=M*(M-1)//2
    ans+=N*(N-1)//2
    print(ans)

#B
if sel=='B':
    def ispal(s):
        for i in range(len(s)//2+1):
            if s[i]!=s[-(i+1)]:
                return False
        return True

    S=input()
    if ispal(S) and ispal(S[:(N-1)//2]) and ispal(S[(N+3)//2-1:]):
        print('Yes')
    else:
        print('No')

#C
if sel=='C':
    L=int(input())
    print((L**3)/27)

#D
if sel=='D':    
    N=int(input())
    A=[int(i) for i in input().split()]
    kin=list(set(A))
    cnt={}
    for k in kin:
        cnt[k]=0
    for a in A:
        cnt[a]+=1
    SUM=0
    for k in kin:
        SUM+=cnt[k]*(cnt[k]-1)//2
    for a in A:
        if cnt[a]>=2:
            print(SUM-cnt[a]+1)
        else:
            print(SUM)
        
#E
if sel=='E':
    a=0

#F
if sel=='F':
    N,S=map(int,input().split())
    A=[int(i) for i in input().split()]

    for l in range(N):
        for r in range(l,N):
            print(tab[l][r])



Fixed submission:
sel='B'
#A
if sel=='A':
    N,M=map(int,input().split())
    ans=0
    ans+=M*(M-1)//2
    ans+=N*(N-1)//2
    print(ans)

#B
if sel=='B':
    def ispal(s):
        for i in range(len(s)//2+1):
            if s[i]!=s[-(i+1)]:
                return False
        return True

    S=input()
    N=len(S)
    if ispal(S) and ispal(S[:(N-1)//2]) and ispal(S[(N+3)//2-1:]):
        print('Yes')
    else:
        print('No')

#C
if sel=='C':
    L=int(input())
    print((L**3)/27)

#D
if sel=='D':    
    N=int(input())
    A=[int(i) for i in input().split()]
    kin=list(set(A))
    cnt={}
    for k in kin:
        cnt[k]=0
    for a in A:
        cnt[a]+=1
    SUM=0
    for k in kin:
        SUM+=cnt[k]*(cnt[k]-1)//2
    for a in A:
        if cnt[a]>=2:
            print(SUM-cnt[a]+1)
        else:
            print(SUM)
        
#E
if sel=='E':
    a=0

#F
if sel=='F':
    N,S=map(int,input().split())
    A=[int(i) for i in input().split()]

    for l in range(N):
        for r in range(l,N):
            print(tab[l][r])
Buggy python submission (verdict: Time Limit Exceeded):
#D
S=input()
Q=int(input())
t=1 # 1:正順,0:逆順
for _ in range(Q):
    q=input()
    if q[0]=='1':
        t=(t+1)%2
    else:
        __,f,c=q.split()
        if f=='1':
            if t==1:
                S=c+S
            else:
                S=S+c
        else:
            if t==1:
                S=S+c
            else:
                S=c+S
    # print(S)
print(S if t else S[::-1])

Fixed submission:
#D
S=input()
Q=int(input())
t=1 # 1:正順,0:逆順
fro=''
bac=''
for _ in range(Q):
    q=input()
    if q[0]=='1':
        t=(t+1)%2
    else:
        __,f,c=q.split()
        if f=='1':
            if t==1:
                fro=c+fro
            else:
                bac=bac+c
        else:
            if t==1:
                bac=bac+c
            else:
                fro=c+fro
    # print(S)
ans=fro+S+bac
print(ans if t else ans[::-1])