Harryxun/sft_time huggingface dataset:
### BUGGY CODE ###

import numpy as np
import math

n, d, a = map(int,input().split())
xh = np.zeros((n,2), dtype = int)
for i in range(n):
    x,h = map(int,input().split())
    xh[i] = [x,h]

num = 0
xh = xh[np.argsort(xh[:, 0])]

while xh.size > 0.5:
    nat = math.ceil((xh[0,1])/a)
    num += nat
    dam = nat*a
    side = xh[0,0]
    for i in range(xh.shape[0]):
        if xh[i,0] <= side+2*d:
            xh[i,1] -= dam
        else:
            break
    xh = xh[~np.any(xh<0.5,axis=1)]

print(num)


### FIXED CODE ###

import math

n, d, a = map(int,input().split())
xh = [[0 for i in range(2)] for j in range(n)]
left = [i for i in range(n)]
for i in range(n):
    x,h = map(int,input().split())
    xh[i] = [x,h]

num = 0
xh.sort(key=lambda xh:xh[0])

for i in range(n):
    for j in range(left[i-1],i):
        if xh[j][0]+d >= xh[i][0]-d:
            left[i] = j
            break

dam = [0]*n

def dsum(k):
    if k == 0:
        return 0
    elif left[k] == 0:
        return dam[k-1]
    else:
        return dam[k-1]-dam[left[k]-1]

for i in range(n):
    res = xh[i][1]-dsum(i)
    nat = max([math.ceil(res/a),0])
    dam[i] = dam[i-1]+a*nat
    if res > 0:
        num += nat

print(num)
### BUGGY CODE ###

l, r = map(int, input().split())
x = 2018

for i in range(l,r):
  i1 = i % 2019
  for j in range(i+1, r+1):
    j1 = j% 2019
    if (i1 * j1) % 2019 <x:
      x = (i1 * j1) % 2019
print(x)

### FIXED CODE ###

l, r = map(int, input().split())
x = 2018

if r - l >=2019:
  x = 0
  
else:
  for i in range(l,r):
    for j in range(i+1, r+1):
      if (i * j) % 2019 < x:
        x = (i * j) % 2019
        
print(x)
### BUGGY CODE ###

# -*- coding: utf-8 -*-
"""
Created on Sun Mar 24 20:45:14 2019

@author: screamLab
"""

n,m = input().split()
s = input()
ans = 0
for i in range(int(m)):
    a,b = input().split()
    a = int(a)
    b = int(b)
    t = s[a-1:b]
    t += 'R'
    for y in range(len(t)):
        if y == len(t):
            break
        if t[y]== 'A' and t[y+1] == 'C':
            ans += 1
    print(ans)
    ans = 0

### FIXED CODE ###

# -*- coding: utf-8 -*-
"""
Created on Sun Mar 24 20:45:14 2019

@author: screamLab
"""

n,m = input().split()
s = input()
ans = 0
table = [0]
for i in range(len(s)-1):
    if s[i:i+2] == 'AC':
        ans += 1
    table.append(ans)
        
for i in range(int(m)):
    a,b = input().split()
    a = int(a)
    b = int(b)
    print(table[b-1] - table[a-1])
### BUGGY CODE ###

import math
tmp = input().split()
X = int(tmp[0])
Y = int(tmp[1])

mod = 10 ** 9 + 7

if (X + Y) % 3 != 0:
    print(0)
else:
    n = (2 * Y - X) / 3
    m = Y - 2 * n
    if n < 0 or m < 0:
        print(0)
        exit()
    n %= mod
    m %= mod
    total=1
    for i in range(int(n + m), int(n), -1):
        total *= i

    m = math.factorial(m)

    total //= m
    print(total%mod)

### FIXED CODE ###

import math
tmp = input().split()
X = int(tmp[0])
Y = int(tmp[1])

mod = 10 ** 9 + 7

if (X + Y) % 3 != 0:
    print(0)
else:
    n = int((2 * Y - X) / 3)
    m = int(Y - 2 * n)
    if n < 0 or m < 0:
        print(0)
        exit()
    n %= mod
    m %= mod

    r = n
    n = n+m
    # if n - r < r:
    #     r = n - r
    # if r == 0:
    #     return 1
    # if r == 1:
    #     return n

    numerator = [n - r + k + 1 for k in range(r)]
    denominator = [k + 1 for k in range(r)]

    for p in range(2, r+1):
        pivot = denominator[p - 1]
        if pivot > 1:
            offset = (n - r) % p
            for k in range(p-1, r, p):
                numerator[k - offset] /= pivot
                denominator[k] /= pivot

    result = 1
    for k in range(r):
        if numerator[k] > 1:
            result *= int(numerator[k])

    # return result
    print(result % mod)
### BUGGY CODE ###

import itertools
import math
from collections import deque
from collections import defaultdict
from itertools import permutations
import heapq
import bisect
from scipy.sparse.csgraph import floyd_warshall as wf
INF = float("inf")

A,B,Q = map(int,input().split())
s = [-INF]+[int(input()) for _ in range(A)]+[INF]
t = [-INF]+[int(input()) for _ in range(B)]+[INF]

for i in range(Q):
    q = int(input())
    b,d = bisect.bisect_right(s,q),bisect.bisect_right(t,q)
    ans = INF
    for S in [s[b-1],s[b]]:
        for T in [t[d-1],t[d]]:
            d1,d2 = abs(S-q)+abs(T-S),abs(T-q)+abs(S-T)
            ans = min(ans,d1,d2)
    print(ans)

### FIXED CODE ###

import bisect
from scipy.sparse.csgraph import floyd_warshall as wf
INF = 10**20

A,B,Q = map(int,input().split())
s = [-INF]+[int(input()) for _ in range(A)]+[INF]
t = [-INF]+[int(input()) for _ in range(B)]+[INF]

for i in range(Q):
    q = int(input())
    b,d = bisect.bisect_right(s,q),bisect.bisect_right(t,q)
    ans = INF
    for S in [s[b-1],s[b]]:
        for T in [t[d-1],t[d]]:
            d1,d2 = abs(S-q)+abs(T-S),abs(T-q)+abs(S-T)
            ans = min(ans,d1,d2)
    print(ans)
### BUGGY CODE ###

import bisect
n = int(input())
A = [int(input()) for i in range(n)]
m = 10**9
c = 0
X = []
for a in A:
    i = bisect.bisect_left(X, a)
    if i == 0:
        X.reverse()
        X.append(a)
        X.reverse()
    else:
        X[i-1] = a
print(len(X))


### FIXED CODE ###

import bisect
n = int(input())
A = [int(input()) for i in range(n)]
m = 10**9
X = []
for a in A:
    i = bisect.bisect_right(X, -a)
    if i == len(X):
        X.append(-a)
    else:
        X[i] = -a
print(len(X))
### BUGGY CODE ###

#D
H=int(input())
cnt=1
ans=1
while True:
    H=int((H-H%2)/2)
    ans+=2**cnt
    cnt+=1
    if H==1:
        break
print(ans)

### FIXED CODE ###

#D
H=int(input())
cnt=0
ans=0
while True:
    ans+=pow(2,cnt)
    cnt+=1
    if H==1:
        break
    H=int((H-H%2)/2)
print(ans)
### BUGGY CODE ###

n,k=map(int,input().split())
i=1
while True:
    if n<k**i:
        print(i)
        break

### FIXED CODE ###

n,k=map(int,input().split())
i=1
while True:
    if n<k**i:
        print(i)
        break
    i+=1
### BUGGY CODE ###

import sys
HK,WK,M = map(int, input().split())
hw_s = [list(map(int, input().split())) for _ in range(M)]
W = {}
H = {}
HW = [[0 for i in range(WK+1)] for _ in range(HK+1)]
for i, hw in enumerate(hw_s):
    h=hw[0]
    w=hw[1]

    if not h in H:
        H[h] = 1
    else:
        H[h] += 1
    
    if not w in W:
        W[w] = 1
    else:
        W[w] += 1

    HW[h][w] = 1
answer = 0
HMAXES = []
WMAXES = []
HMAX = max(H.values())
WMAX = max(W.values())

for ki, vi in H.items():
    if vi == max(H.values()):
        HMAXES.append(ki)
for ki, vi in W.items():
    if vi == max(W.values()):
        WMAXES.append(ki)

answer = HMAX + WMAX
for h in HMAXES:
    for w in WMAXES:
        if HW[h][w] == 0:
            print(answer)
            sys.exit(0)
print(answer-1)

### FIXED CODE ###

import sys
HK,WK,M = map(int, input().split())
W = [0] * (WK + 1)
H = [0] * (HK + 1)
HW = {}
# 3*10^5

for i in range(M):
    h, w = map(int, input().split())

    H[h] += 1
    W[w] += 1

    HW[(h,w)] = 1

answer = 0
HMAX = max(H)
WMAX = max(W)

HEMAX = H.count(HMAX)
WEMAX = W.count(WMAX)
if HEMAX * WEMAX > M:
    print(HMAX + WMAX)
    sys.exit(0)

HMAXES = [i for i, _ in enumerate(H) if _ == HMAX]
WMAXES = [i for i, _ in enumerate(W) if _ == WMAX]

# 3*10^5
answer = HMAX + WMAX
for h in HMAXES:
    for w in WMAXES:
        if not (h,w) in HW:
            print(answer)
            sys.exit(0)
print(answer-1)
### BUGGY CODE ###

S = list(map(int, list(input())))
N = len(S)
CNT = [0]*2019
CNT[0] = 1
num = 0
for i in range(N):
    base = pow(10, N-i, 2019)
    num = num * 10 + S[i]
    CNT[(num*base)%2019] += 1
ans = 0 
for cnt in CNT:
    if cnt==0: continue
    ans += (cnt*(cnt - 1)) // 2
print(ans)

### FIXED CODE ###

S = list(map(int, list(input())))[::-1]
N = len(S)
CNT = [0]*2019
CNT[0] = 1
num = 0
d = 0
for i in range(N):
    num += S[i]*pow(10, d, 2019)
    num %= 2019
    CNT[num] += 1
    d += 1
ans = 0 
for cnt in CNT:
    ans += (cnt*(cnt - 1)) // 2
print(ans)
### BUGGY CODE ###

#!/usr/bin/env python3
import collections as cl
import sys


def II():
    return int(sys.stdin.readline())


def MI():
    return map(int, sys.stdin.readline().split())


def LI():
    return list(map(int, sys.stdin.readline().split()))


MOD = 10 ** 9 + 7


def main():
    N = II()
    targets = LI()

    ttl_bins = [[0]*60 for i in range(N)]
    for i in range(1, N):
        target = targets[N-i]
        target_str = format(target, f"0{60}b")
        for keta in range(60):
            ttl_bins[N-i-1][keta] = ttl_bins[N -
                                             i][keta] + int(target_str[keta])

    ans = 0

    for i in range(N-1):
        target = targets[i]
        target_str = format(target, f"0{60}b")
        for keta in range(60):
            keta_int = int(target_str[keta])
            if keta_int == 0:
                ttl_others_1 = ttl_bins[i][keta]
                ans += (ttl_others_1) * (2 ** (59-keta)
                                         % MOD)
            if keta_int == 1:
                ttl_others_0 = (N - i - 1) - ttl_bins[i][keta]
                ans += (ttl_others_0) * (2 ** (59-keta)
                                         % MOD)
            ans = ans % MOD

    print(ans)


main()


### FIXED CODE ###

#!/usr/bin/env python3
import collections as cl
import sys


def II():
    return int(sys.stdin.readline())


def MI():
    return map(int, sys.stdin.readline().split())


def LI():
    return list(map(int, sys.stdin.readline().split()))


MOD = 10 ** 9 + 7


def main():
    N = II()
    targets = LI()

    ttl_bins = [0]*60
    for i in range(N):
        target = targets[i]
        target_str = format(target, f"0{60}b")
        for keta in range(60):
            ttl_bins[keta] += int(target_str[keta])

    ans = 0
    for i in range(60):
        ttl = ttl_bins[i] * (N - ttl_bins[i])
        ans += pow(2, 59-i, MOD) * ttl
        ans %= MOD

    print(ans)


main()
### BUGGY CODE ###

import math
def is_prime(num):
    for i in range(2, num):  # 第二引数は sqrt(num)+1 でも良い
        if num % i == 0:
            return False

    return True
X = int(input())
if X==1:
    print(0,-1) 
else:
    if is_prime(X):
        d = X
    else:
        d = 2
        while(X%d!=0):
            d+=1
            
    b = -(10**3)
    a = b+d
    
    while(X!=((a**5)-(b**5))):
        b+=1
        a+=1
    print(a,b)

### FIXED CODE ###

X = int(input())

A = range(-118,120)
B = range(-118,120)
flag = 0
for a in A:
  for b in B:
    ans = a**5 - b**5
    if ans == X:
      print("{} {}".format(a,b))
      flag=1
      break
  if flag:
    break
### BUGGY CODE ###

n = int(input())
s = []
c = []
s_dash = []
ans = 0

for i in range(n):
	s.append(input())
	c.clear()
	for j in s[i]:
		c.append(j)

	c.sort()
	c_str = ''
	for j in range(10):
		c_str += c[j]

	s_dash.append(c_str)

for i in range(n):
	for j in range(i+1,n):
		if s_dash[i] == s_dash[j]:
			ans += 1

print(ans)


### FIXED CODE ###

def cmb(n, r):
	if n < r: return 0
	if r == 0: return 1
	if r == 1: return n
	if n - r < r: r = n - r

	numerator = [n - r + k + 1 for k in range(r)]
	denominator = [k + 1 for k in range(r)]

	for p in range(2,r+1):
		pivot = denominator[p - 1]
		if pivot > 1:
			offset = (n - r) % p
			for k in range(p-1,r,p):
				numerator[k - offset] /= pivot
				denominator[k] /= pivot

	result = 1
	for k in range(r):
		if numerator[k] > 1:
			result *= int(numerator[k])

	return result

n = int(input())
s = []
for _ in range(n):
	tmp = list(input())
	tmp.sort()
	s.append(tmp)

s.sort()

count = 1
ans = 0
for i in range(n-1):
	if s[i] == s[i+1]:
		count += 1
	else:
		ans += cmb(count,2)
		count = 1

if count != 1:
	ans += cmb(count,2)

print(ans)
### BUGGY CODE ###

###template###
import sys
def input(): return sys.stdin.readline().rstrip()
def mi(): return map(int, input().split())
###template###

N, A, B, C, D = mi()
#S[0~N-1]でアクセス
cells = input()

if C > D:
  c = C
  C = D
  D = c
  a = A
  A = B
  B = a

#全部0~N-1ベースにする
A -= 1
B -= 1
C -= 1
D -= 1

#既にスタック済みの座標の組(visited[0~N-1][0~N-1]でアクセス)
visited = [[0]*N for _ in range(N)]

from collections import deque
st = deque(maxlen=10000)
#初期座標
st.append((A,B))

#結果が出るまでループする
#スタックから座標の組を取り出し、F>Sの優先順位で動かしてスタックに入れる
num = 0
while num < 10000:
  num += 1
  #選択肢がなくなったら、駄目だったと分かる
  try:
    s, f = st.popleft()
  except:
    print('No')
    exit()

  #ここがゴールかどうか
  if s==C and f==D:
    print('Yes')
    exit()
  #Fが動けるかどうか
  fok = 0
  try:
    if visited[s][f+2]==0 and cells[f+2]=='.' and s != f+2:
      st.append((s,f+2))
      visited[s][f+2] = 1
      fok = 1
  except:
    pass
  try:
    if visited[s][f+1]==0 and cells[f+1]=='.' and s != f+1:
      st.append((s,f+1))
      visited[s][f+1] = 1
      fok = 1
  except:
    pass
  #Fが動けなかった場合のみSが動く
  if fok == 0:
    try:
      if visited[s+2][f]==0 and cells[s+2]=='.' and s+2 != f:
        st.append((s+2,f))
        visited[s+2][f] = 1
        fok = 1
    except:
      pass
    try:
      if visited[s+1][f]==0 and cells[s+1]=='.' and s+1 != f:
        st.append((s+1,f))
        visited[s+1][f] = 1
        fok = 1
    except:
      pass






### FIXED CODE ###

###template###
import sys
def input(): return sys.stdin.readline().rstrip()
def mi(): return map(int, input().split())
###template###

N, A, B, C, D = mi()
#S[0~N-1]でアクセス
cells = input()

#全部0~N-1ベースにする
A -= 1
B -= 1
C -= 1
D -= 1

ans = '?'

goal = max(C,D)


rockcheck = 0 #直前が岩なら1
for i, cell in enumerate(cells):
  if i < A or goal < i: continue
  if rockcheck == 1 and cell == '#':
    ans = 'No'
    break
  elif cell == '#': rockcheck = 1
  else: rockcheck = 0

#C<Dならflag=0, D<Cならflag=1
if C < D: flag = 0
else: flag = 1

if ans == 'No':
  print('No')
  exit()
elif flag == 0: #C<Dならこの時点で勝ち確
  print('Yes')
  exit()
#そうでなければ戦いは続く

ans = 'No'
#C<Dの場合決着がついているので、ここからは必ずD<C
#かつ、##は無いことが保証されている
blankcheck = 0 #直前が.なら1, 直前が..なら2
for i, cell in enumerate(cells):
  if i < (B-1) or (D+1) < i: continue
  if cell == '.': blankcheck += 1
  else: blankcheck = 0
  if blankcheck == 3:
    ans = 'Yes'
    break

print(ans)
### BUGGY CODE ###

n,k = map(int, input().split())
H = list(map(int, input().split()))
dp = [10**9 for i in range(n)]

dp[0] = 0
for i in range(n-1):
    for j in range(1,min(k+1,n-i)):
        dp[i+j] = min(dp[i+j],dp[i]+abs(H[i+j]-H[i]))
print(dp[n-1])

### FIXED CODE ###

n,k = map(int, input().split())
H = list(map(int, input().split()))
dp = [10**9 for i in range(n)]

dp[0] = 0
for i in range(n-1):
    for j in range(1,min(k+1,n-i)):
        dp[i+j] = min(dp[i+j],dp[i]+abs(H[i+j]-H[i]))
print(dp[n-1])
### BUGGY CODE ###

x, y, z, k = map(int, input().split())
a = [int(x) for x in input().split()]
b = [int(x) for x in input().split()]
c = [int(x) for x in input().split()]
a.sort(reverse=True)
b.sort(reverse=True)
c.sort(reverse=True)
ans_list = []
for i in a:
    for j in b:
        ans_list.append(i+j)
ans_list.sort(reverse=True)
ans_list = ans_list[:min(3001, x*y*z+1)]
ans_list_2 = []
for i in ans_list:
    for j in c:
        ans_list_2.append(i+j)
ans_list_2.sort(reverse=True)
for i in range(k):
    print(ans_list_2[i])

### FIXED CODE ###

x, y, z, k = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = list(map(int, input().split()))
a.sort(reverse=True)
b.sort(reverse=True)
c.sort(reverse=True)
p = []
for i in a:
    for j in b:
        p.append(i+j)
p.sort(reverse=True)
p = p[:min(3001, x*y*z+1)]
q = []
for j in c:
    for i in p:
        q.append(i+j)
q.sort(reverse=True)
for i in range(k):
    print(q[i])
### BUGGY CODE ###

import queue
import copy

def bfs(s1,s2):
    global ans
    cs = copy.deepcopy(s)
    queueX = queue.Queue()
    queueY = queue.Queue()
    queueX.put(s1)
    queueY.put(s2)
    cs[s1][s2] = 0
    while queueX.empty() == False:
        x = queueX.get()
        y = queueY.get()

        for i in range(4):
            nextX = x + moveR[i]
            nextY = y + moveC[i]
            if 0 <= nextX and nextX < H\
                and 0 <= nextY and nextY < W\
                and cs[nextX][nextY] != '#'\
                and cs[nextX][nextY] == '.':
                cs[nextX][nextY] = cs[x][y] + 1
                ans = max(ans, cs[nextX][nextY])
                queueX.put(nextX)
                queueY.put(nextY)

H, W = map(int, input().split())
s = [list(input()) for _ in range(H)]

moveR = [0, 1, -1, 0]
moveC = [1, 0, 0, -1]
ans = 0
for i in range(H):
    for j in range(W):
        if s[i][j] == '.':
            bfs(i, j)

print(ans)

### FIXED CODE ###

import queue
import copy

def bfs(s1,s2):
    global ans
    cs = copy.deepcopy(s)
    queueX = queue.Queue()
    queueY = queue.Queue()
    queueX.put(s1)
    queueY.put(s2)
    cs[s1][s2] = 0
    while queueX.empty() == False:
        x = queueX.get()
        y = queueY.get()

        for i in range(4):
            nextX = x + moveR[i]
            nextY = y + moveC[i]
            if 0 <= nextX and nextX < H\
                and 0 <= nextY and nextY < W\
                and cs[nextX][nextY] != '#'\
                and cs[nextX][nextY] == '.':
                cs[nextX][nextY] = cs[x][y] + 1
                ans = max(ans, cs[nextX][nextY])
                queueX.put(nextX)
                queueY.put(nextY)

H, W = map(int, input().split())
s = [list(input()) for _ in range(H)]

moveR = [0, 1, -1, 0]
moveC = [1, 0, 0, -1]
ans = 0
for i in range(H):
    for j in range(W):
        if s[i][j] == '.':
            bfs(i, j)

print(ans)
### BUGGY CODE ###

import unittest
import functools


class TestE(unittest.TestCase):
    def test_1(self):
        self.assertEqual(think([2, 3, 4]), 13)

    def test_2(self):
        self.assertEqual(think([12, 12, 12, 12, 12]), 5)

    def test_3(self):
        self.assertEqual(think([1000000, 999999, 999998]), 996989508)

    def test_4(self):
        self.assertEqual(gcd(3, 2), 1)


def solve():
    a = read()
    result = think(a)
    write(result)


def read():
    n = read_int(1)[0]
    return read_int(n)


def read_int(n):
    return read_type(int, n, sep=' ')


def read_float(n):
    return read_type(float, n, sep=' ')


def read_type(t, n, sep):
    return list(map(lambda x: t(x), read_line().split(sep)))[:n]


def read_line(n=0):
    if n == 0:
        return input().rstrip()
    else:
        return input().rstrip()[:n]


def think(a):
    mode = 10 ** 9 + 7

    lcm_of_all_a = 1
    for e in a:
        lcm_of_all_a = lcm(lcm_of_all_a, e)

    result = 0
    for e in a:
        result += lcm_of_all_a * inverse(e, mode) % mode
        result %= mode
    return result


def write(result):
    print(result)


def lcm(a, b):
    return a // gcd(a, b) * b 


def gcd(a, b):
    if a < b:
        return gcd(b, a)

    while True:
        if a % b == 0:
            return b
        a, b = b, a % b


def inverse(x, mode):
    return pow(x, mode - 2, mode)


if __name__ == '__main__':
    # unittest.main()
    solve()

### FIXED CODE ###

import unittest
import functools


class TestE(unittest.TestCase):
    def test_1(self):
        self.assertEqual(think([2, 3, 4]), 13)

    def test_2(self):
        self.assertEqual(think([12, 12, 12, 12, 12]), 5)

    def test_3(self):
        self.assertEqual(think([1000000, 999999, 999998]), 996989508)

    def test_4(self):
        self.assertEqual(gcd(3, 2), 1)


def solve():
    a = read()
    result = think(a)
    write(result)


def read():
    n = read_int(1)[0]
    return read_int(n)


def read_int(n):
    return read_type(int, n, sep=' ')


def read_float(n):
    return read_type(float, n, sep=' ')


def read_type(t, n, sep):
    return list(map(lambda x: t(x), read_line().split(sep)))[:n]


def read_line(n=0):
    if n == 0:
        return input().rstrip()
    else:
        return input().rstrip()[:n]


def think(a):
    mode = 10 ** 9 + 7

    lcm_of_all_a = 1
    for e in a:
        lcm_of_all_a = lcm(lcm_of_all_a, e)

    lcm_of_all_a %= mode
    result = 0
    for e in a:
        result += lcm_of_all_a * inverse(e, mode) % mode
        result %= mode
    return result


def write(result):
    print(result)


def lcm(a, b):
    return a // gcd(a, b) * b 


def gcd(a, b):
    if a < b:
        return gcd(b, a)

    while True:
        if a % b == 0:
            return b
        a, b = b, a % b


def inverse(x, mode):
    return pow(x, mode - 2, mode)


if __name__ == '__main__':
    # unittest.main()
    solve()
### BUGGY CODE ###

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

for i in range(Q):
    query = input()
    if str(query)[0]=="1":
        S = S[::-1]
    else:
        F = int(query.split()[1])
        C = str(query.split()[2])
        if F==1:
            S = C+S
        else:
            S = S+C
print(S)

### FIXED CODE ###

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

isreverse = False
head=[]
tail=[]

for i in range(Q):
    query = input()
    if str(query)[0]=="1":
        isreverse = isreverse^True
    else:
        F = int(query.split()[1])
        C = str(query.split()[2])
        if (F==1 and isreverse==False) or (F==2 and isreverse==True):
            head.append(C)
        else:
            tail.append(C)


if isreverse==False:
    print("".join(reversed(head))+S+"".join(tail))
else:
    print("".join(reversed(tail))+S[::-1]+"".join(head))
### BUGGY CODE ###

N,M = map(int,input().split())
mod=10**9+7
A=[]
for i in range(M):
  A +=[int(input())]

B=[1]
if 1 in A:
  B +=[0]
else:
  B +=[1]
for i in range(2,N+1):
  if i in A:
    B +=[0]
  else:
    B +=[B[i-1]+B[i-2]]
print(B[-1]%mod)

### FIXED CODE ###

N,M = map(int,input().split())
mod=10**9+7
A = {int(input()) for i in range(M)}

if 1 in A:
  x=0
else:
  x=1
B=[1,x]
for i in range(2,N+1):
  if i in A:
    B +=[0]
  else:
    B +=[(B[i-1]+B[i-2])%mod]
print(B[-1]%mod)
### BUGGY CODE ###

mod = 998244353
N,M,K = map(int,input().split())
ans = 0
def color(n,m):
    return m*(pow_k(m-1, n-1))

def pow_k(x, n):
    if n == 0:
        return 1
    K = 1
    while n > 1:
        if n % 2 != 0:
            K = K * x
            x = x ** 2
            x %= mod
            n = (n - 1) // 2
        else:
            x = x ** 2
            x %= mod
            n = n // 2

    return (K * x)%mod
invs = [0] * (N + 1)
invs[1] = 1
for i in range(2, N + 1):
    invs[i] = mod - invs[mod % i] * (mod//i) % mod

def mod_inv_factorial2(n1, n2, m):
    n = max(n1, n2)
    r1 = r2 = 1
    for i in range(1, n+1):
        r1 *= invs[i]
        r1 %= m
        if i == min(n1, n2):
            r2 = r1
    return r1, r2

def mod_factorial(n, m):
    r = 1
    for i in range(1, n+1):
        r *= i
        r %= m
    return r

upper = mod_factorial(N-1,mod)

for i in range(K+1):
    tmp = color(N-i,M)
    lower1,lower2 = mod_inv_factorial2(i,N-1-i,mod)
    ansk = (upper*lower1*lower2*tmp)%mod
    ans += ansk
    ans %= mod
print(ans)

    


### FIXED CODE ###

mod = 998244353
N,M,K = map(int,input().split())
ans = 0
def color(n,m):
    return m*(pow_k(m-1, n-1))

def pow_k(x, n):
    if n == 0:
        return 1
    K = 1
    while n > 1:
        if n % 2 != 0:
            K = K * x
            x = x ** 2
            x %= mod
            n = (n - 1) // 2
        else:
            x = x ** 2
            x %= mod
            n = n // 2

    return (K * x)%mod
invs = [0] * (N + 1)
invs[1] = 1
for i in range(2, N + 1):
    invs[i] = mod - invs[mod % i] * (mod//i) % mod
mod_inv_factorials=[1]*(N+1)

for i in range(2,N+1):
    mod_inv_factorials[i] = mod_inv_factorials[i-1]*invs[i]
    mod_inv_factorials[i] %= mod

def mod_factorial(n, m):
    r = 1
    for i in range(1, n+1):
        r *= i
        r %= m
    return r

upper = mod_factorial(N-1,mod)

for i in range(K+1):
    tmp = color(N-i,M)
    ansk = (upper*mod_inv_factorials[i]*mod_inv_factorials[N-1-i]*tmp)%mod
    ans += ansk
    ans %= mod
print(ans)
### BUGGY CODE ###

from bisect import bisect_right

N = int(input())
A = sorted(list(map(int, input().split())))
B = sorted(list(map(int, input().split())))
C = sorted(list(map(int, input().split())))

ans = 0
y_low = 0
z_low = 0
for x in A:
    y_low = bisect_right(B, x)
    for y in B[y_low:]:
        z_low = bisect_right(C, y)
        ans += N - z_low
print(ans)


### FIXED CODE ###

from bisect import bisect_left
from bisect import bisect_right


N = int(input())
A = sorted(map(int, input().split()))
B = sorted(map(int, input().split()))
C = sorted(map(int, input().split()))

ans = 0
y_low = {}
i = 0
for y in B:
    x = bisect_left(A, y)
    z = bisect_right(C, y)
    ans += x * (N-z)
print(ans)