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

N, M = map(int, input().strip().split())

A = {}

for i in range(N):
  a, b = map(int, input().strip().split())
  if a not in A: A[a] = []
  A[a].append(b)

total = 0
days = []
for i in range(1, M + 1):
  if i in A:
    days.extend(A[i])
    days = sorted(days)
  if len(days) > 0:
    a = len(days)
    total += days.pop()

print(total)


### FIXED CODE ###

import heapq

N, M = map(int, input().strip().split())

A = {}

for i in range(N):
  a, b = map(int, input().strip().split())
  if a not in A: A[a] = []
  A[a].append(b)

total = 0
days = []
for i in range(1, M + 1):
  if i in A:
    for a in A[i]:
      heapq.heappush(days, -a)
  if len(days) > 0:
    total += - heapq.heappop(days)

print(total)
### BUGGY CODE ###

import bisect
n,m = map(int,input().split())
l = list(map(int,input().split()))
l.sort()
right = 10**6
left = 0
ans = 0
while right - left >1:
    mid = (right+left)//2
    
    x = 0
    for i in range(n):
        a = l[i]
        index = bisect.bisect_left(l,mid-a)
        x += n-index
   
    if x > m:
        left = mid
    elif m >= x:
        right = mid
ans = []
for i in range(n):
    a = l[i]
    for j in range(n):
        if a + l[j] >= left:
            ans.append(a+l[j])
ans.sort(reverse=True)

print(sum(ans[0:m]))



### FIXED CODE ###

def main():
    import bisect
    import numpy as np
    n,k = map(int,input().split())
    l = list(map(int,input().split()))
    l.sort()
    l1 = np.array(l)
    def bise(x):
        y = np.searchsorted(l1,x-l1)
        return n*n-y.sum()

    left = -1
    right = l[-1]*2+1

    while right > left+1:
        mid = (right+left)//2
        
        if bise(mid) >= k:
            left = mid
        else:
            right = mid
    ans = 0
    count = 0
    acc = [0]
    for i in l:
        acc.append(acc[-1]+i)
    for i in l:
        index = bisect.bisect_left(l,right-i)
        ans += i*(n-index) + acc[n]-acc[index]
        count += n-index

    print(ans-(count-k)*left)
if __name__ == "__main__":
    main()
### BUGGY CODE ###

n = int(input())
listTakahashi = []
listPoint = []
i=0
while i < n:
    a, b = map(int, input().split())
    listTakahashi.append([a+b, i])
    listPoint.append([a, b])
    i+=1
listTakahashi.sort()
listTakahashi.reverse()
index2=0
takaP=0
aokiP=0
while index2 < n:
    if index2%2==0:#takahashi's turn
        index = listTakahashi[0][1]
        takaP+=listPoint[index][0]
        del listTakahashi[0]
    else:
        index = listTakahashi[0][1]
        aokiP+=listPoint[index][1]
        del listTakahashi[0]
    index2+=1

print(takaP-aokiP)
    

### FIXED CODE ###

n = int(input())
listP = []
i=0
while i < n:
    a, b = map(int, input().split())
    listP.append([a+b, a, b])
    i+=1
listP.sort()
listP.reverse()

takaP=0
aokiP=0
index=0
while index < len(listP):
    if index%2==0:
        takaP+=listP[index][1]
    else:
        aokiP+=listP[index][2]
    index+=1
print(takaP-aokiP)
### BUGGY CODE ###

a,b,c,d,e,f = map(int, input().split())
mx=0
swmx=a*100
smx=0
 
for i in range(0,31):
    for j in range(0,31):
        for k in range(0,1000):
            for l in range(0,1000):
                w=0
                s=0
                w+=100*a*i+100*b*j
                s+=c*k+d*l
                targ=w+s
                if targ<=f and s<=e*w/100 and targ!=0 and mx<s/(w+s):
                    mx=s/targ
                    swmx=targ
                    smx=s
print(swmx,smx)

### FIXED CODE ###

a,b,c,d,e,f = map(int, input().split())
mx=0
swmx=100*a
smx=0
 
for i in range(0,31):
    for j in range(0,31):
        for k in range(0,51):
            for l in range(0,51):
                w=0
                s=0
                w+=100*a*i+100*b*j
                s+=c*k+d*l
                if w+s<=f and s<=e*w/100 and w!=0 and mx<100*s/(w+s):
                    mx=100*s/(w+s)
                    swmx=w+s
                    smx=s
print(swmx,smx)
### BUGGY CODE ###

n=int(input())
c=1
for i in range(n):
    c*=(i+1)
print(c%(10**9+7))


### FIXED CODE ###

n=int(input())
c=1
for i in range(n):
    c*=(i+1)
    c%=(10**9+7)
print(c)
### BUGGY CODE ###

n,k = map(int,input().split())
H= list(map(int,input().split()))
if(k<n):
  H.sort()
  for i in range(k):
    H.pop()
  cont=0
  for i in range(len(H)):
    while H[i]>0:
      H[i]-=1
      cont+=1
  print(cont)
else:
  print(0)


### FIXED CODE ###

n,k = map(int,input().split())
H= list(map(int,input().split()))
if(k<n):
  H.sort()
  for i in range(k):
    H.pop()
  cont=0
  for i in range(len(H)):
    cont+= H[i]
  print(cont)
else:
  print(0)
### BUGGY CODE ###

def lcs(x,y):
    xs = len(x)
    ys = len(y)
    x = " " + x
    y = " " + y
    c = [[0 for i in xrange(len(x))] for j in xrange(len(y))]
    for i in xrange (1,len(y)):
        for j in xrange (1,len(x)):
            if x[j] == y[i]:
                a = 1
            else:
                a = 0
            c[i][j] = max(c[i][j-1],c[i-1][j],c[i-1][j-1] + a)
    return max(max(c))
n = input() 
for i in xrange (n): 
    x = raw_input()
    y = raw_input() 
    print lcs(x,y)

### FIXED CODE ###

def lcs():
    x, y = raw_input(), raw_input()
    b1 = [0 for k in range(len(y)+1)]
    for i in xrange(len(x)):
        a = x[i]
        b2 = b1 + []
        for j in xrange(len(y)):
            if a == y[j]:
                b1[j+1] = b2[j] + 1
            elif b1[j+1] < b1[j]:
                b1[j+1] = b1[j]
    print max(b1)
 
n = input()
for i in xrange(n):
    lcs()
### BUGGY CODE ###

mod = 10**9+7 #出力の制限
N = 2*10**5
g1 = [1, 1] # 元テーブル
g2 = [1, 1] #逆元テーブル
inverse = [0, 1] #逆元テーブル計算用テーブル

for i in range( 2, N + 1 ):
    g1.append( ( g1[-1] * i ) % mod )
    inverse.append( ( -inverse[mod % i] * (mod//i) ) % mod )
    g2.append( (g2[-1] * inverse[-1]) % mod )

def func(n):
    if n%2==0:
        return (-g1[n]*g2[n//2]*pow(inverse[2],n//2,mod))%mod
    else:
        return 0

import sys

sys.setrecursionlimit(10**5)

N=int(input())
edge=[[] for i in range(N)]
for i in range(N-1):
    x,y=map(int,input().split())
    edge[x-1].append(y-1)
    edge[y-1].append(x-1)

parent=[0]*N
def prv(v,pv):
    for nv in edge[v]:
        if nv!=pv:
            parent[nv]=v
            prv(nv,v)

prv(0,-1)

for i in range(N):
    new=[]
    for v in edge[i]:
        if v!=parent[i]:
            new.append(v)
    edge[i]=new


dp=[[] for i in range(N)]
sz=[0 for i in range(N)]

def dfs(v,pv):
    sz[v]=1
    dp[v]=[0]*(sz[v]+1)
    dp[v][1]=1
    for nv in edge[v]:
        dfs(nv,v)
        merged=[0]*(sz[v]+sz[nv]+1)
        for i in range(sz[v]+1):
            for j in range(sz[nv]+1):
                merged[i+j] +=dp[v][i]*dp[nv][j]
                merged[i+j]%=mod
        sz[v]+=sz[nv]
        dp[v] =merged
    dp[v][0]=(sum(func(k)*dp[v][k] for k in range(1,sz[v]+1)))%mod


dfs(0,-1)

print((-dp[0][0])%mod)

### FIXED CODE ###

mod = 10**9+7 #出力の制限
N = 2*10**5
g1 = [1, 1] # 元テーブル
g2 = [1, 1] #逆元テーブル
inverse = [0, 1] #逆元テーブル計算用テーブル

for i in range( 2, N + 1 ):
    g1.append( ( g1[-1] * i ) % mod )
    inverse.append( ( -inverse[mod % i] * (mod//i) ) % mod )
    g2.append( (g2[-1] * inverse[-1]) % mod )

import sys

sys.setrecursionlimit(10**4)

N=int(input())
edge=[[] for i in range(N)]
parent=[0]*N
deg=[1]*N
deg[0]=0
for i in range(N-1):
    x,y=map(int,input().split())
    edge[x-1].append(y-1)
    edge[y-1].append(x-1)


def prv(v,pv):
    for nv in edge[v]:
        if nv!=pv:
            parent[nv]=v
            prv(nv,v)

prv(0,-1)

for i in range(N):
    new=[]
    for v in edge[i]:
        if v!=parent[i]:
            new.append(v)
    edge[i]=new


from collections import deque
ans = list(v for v in range(N) if deg[v]==0)
deq = deque(ans)
used = [0]*N

while deq:
    v = deq.popleft()
    for t in edge[v]:
        deg[t] -= 1
        if deg[t]==0:
            deq.append(t)
            ans.append(t)


dp=[[] for i in range(N)]
sz=[0 for i in range(N)]

for v in ans[::-1]:
    sz[v]=1
    dp[v]=[0]*(sz[v]+1)
    dp[v][1]=1
    for nv in edge[v]:
        merged=[0]*(sz[v]+sz[nv]+1)
        for i in range(sz[v]+1):
            for j in range(sz[nv]+1):
                merged[i+j]=(merged[i+j]+dp[v][i]*dp[nv][j])%mod
        sz[v]+=sz[nv]
        dp[v] =merged
    for k in range(1,sz[v]+1):
        dp[v][0]=(dp[v][0]-(g1[k]*g2[k//2])%mod*((k+1)%2)*dp[v][k])%mod

print((-dp[0][0]*pow(inverse[2],N//2,mod))%mod)
### BUGGY CODE ###

n = int(input())

a = list(map(int, input().split()))

sigma = 0
for i in range(len(a)-1):
  start = a[i]
  after = sum(a[i+1:])
  if after >= (10**9 + 7):
    after = after % (10**9+7)
  sigma = sigma + start * after
  if sigma >= (10**9 + 7):
    sigma = sigma % (10**9+7)

print(sigma)


### FIXED CODE ###

n = int(input())

a = list(map(int, input().split()))

sigma = 0
after = sum(a[0:])
for i in range(len(a)-1):
  start = a[i]
  after = after - a[i]
  sigma = sigma + start * after
  if sigma >= (10**9 + 7):
    sigma = sigma % (10**9+7)

print(sigma)
### BUGGY CODE ###


N = int(input())
ary = list(map(int, input().split(' ')))
count = 0
for i in range(1,N+1):
    for j in range(N):
        if j + i > N: break
        if sum(ary[j:j+i]) == 0:
            count += 1
print(count)

### FIXED CODE ###

import numpy
N = int(input())
ary = list(map(int, input().split(' ')))
ary_int = numpy.cumsum(ary).tolist()
ary_int.sort()
ary_int.append(float('inf'))

count = 0
same_count = 0
for i in range(N):
    if ary_int[i] == ary_int[i+1]:
        same_count += 1
    else:
        count += same_count * (same_count + 1) // 2
        same_count = 0
count += ary_int.count(0)
print(int(count))
### BUGGY CODE ###

from collections import Counter

def solve(i,j,a):
    k=False
    sc=0
    for v in [2,3,5,7,11,13,17,19]:
        if j%v==0:
            break
    v=j//v
    for c in a:
        if c>v:
            break
        else:
            if j%c==0:
                k=True
                break
    return k

n=int(input())
a=sorted(list(map(int,input().split())))
ac=Counter(a)
ans=0
for i,j in enumerate(a):
    if ac[j]>=2:
        continue
    if solve(i,j,a):
        continue
    else:
        ans+=1
print(ans)

### FIXED CODE ###

n=int(input())
a=list(map(int,input().split()))
m=max(a)
cnt=[0 for _ in range(m+1)]
ans=0
for i in a:
    if cnt[i]>=1:
        cnt[i]+=1
        continue
    for j in range(2*i,m+1,i):
        cnt[j]+=1
    cnt[i]=1
for i in range(n):
    if cnt[a[i]]==1:
        ans+=1
print(ans)
### BUGGY CODE ###

n,m=map(int,input().split())
mod=10**9+7
a = [int(input()) for i in range(m)]
block=[0,1]
for i in range(1,n+1):
  if i in a:
    block=block[1:]+[0]
  else:
    block=block[1:]+[sum(block)%mod]
print(block[-1])


### FIXED CODE ###

n,m=map(int,input().split())
mod=10**9+7
a = [int(input()) for i in range(m)]
block=[0,1]
x=[True for i in range(n)]
for j in a:
  x[j-1]=False

for i in range(1,n+1):
  if x[i-1]==False:
    block=block[1:]+[0]
  else:
    block=block[1:]+[sum(block)%mod]
print(block[-1])
### BUGGY CODE ###

# -*- coding: utf-8 -*-

N = int(input().strip())

print(sum(int(i) for i in range(N)))

### FIXED CODE ###

# -*- coding: utf-8 -*-

N = int(input().strip())
print((N-1)*N//2)
### BUGGY CODE ###

n = int(input())
a = list(map(int, input().split()))

ans = 0
#even = []
for i in range(60):
	cnt = 0
	for x in a:
		if (x>>i) & 1:
			cnt += 1
	if cnt%2 == 1:
		ans += (1<<i)
		for j in range(n):
			if (a[j]>>i) & 1:
				a[j] -= (1<<i)
#print(ans, even)

yet = 0
for r in range(59, -1, -1):
	stood = False
	for c in range(yet, n):
		if (a[c]>>r) & 1:
			if not stood:
				stood = True
				a[yet], a[c] = a[c], a[yet]
				yet += 1
	if stood:
		for c in range(n):
			if c == yet-1 or (a[c]>>r) & 1 == 0:
				continue
			#print(c)
			a[c] ^= a[yet-1]

tmp = 0
res = 0
for c in range(n):
	tmp ^= a[c]
	res = max(res, tmp) 

print(ans + (res<<1))

### FIXED CODE ###

n = int(input())
a = list(map(int, input().split()))

ans = 0
#even = []
for i in range(60):
	cnt = 0
	for x in a:
		if (x>>i) & 1:
			cnt += 1
	if cnt%2 == 1:
		ans += (1<<i)
		for j in range(n):
			if (a[j]>>i) & 1:
				a[j] -= (1<<i)
#print(ans, even)

yet = 0
for r in range(59, -1, -1):
	stood = False
	for c in range(yet, n):
		if (a[c]>>r) & 1:
			if not stood:
				stood = True
				a[yet], a[c] = a[c], a[yet]
				yet += 1
	if stood:
		for c in range(n):
			if c == yet-1 or (a[c]>>r) & 1 == 0:
				continue
			#print(c)
			a[c] ^= a[yet-1]

tmp = 0
res = 0
for c in range(n):
	tmp ^= a[c]
	res = max(res, tmp) 

print(ans + (res<<1))
### BUGGY CODE ###

import itertools

N, M = map(int, input().strip().split())
cakes = []

for _ in range(N):
  cakes.append(tuple(map(int, input().strip().split())))

max_sum = None
for cakes_combi in itertools.combinations(cakes, M):
  x, y, z = 0, 0, 0
  for cake in cakes_combi:
    x += cake[0]
    y += cake[1]
    z += cake[2]
  if max_sum is None or abs(x) + abs(y) + abs(z) > max_sum:
    max_sum = abs(x) + abs(y) + abs(z)
print(max_sum)
  

### FIXED CODE ###

n, m = map(int, input().split())
xyz = [tuple(map(int, input().split())) for _ in range(n)]

s1 = abs(sum(sorted([x+y+z for x, y, z in xyz])[:m]))
s2 = abs(sum(sorted([x+y-z for x, y, z in xyz])[:m]))
s3 = abs(sum(sorted([x-y+z for x, y, z in xyz])[:m]))
s4 = abs(sum(sorted([x-y-z for x, y, z in xyz])[:m]))
s5 = abs(sum(sorted([-x+y+z for x, y, z in xyz])[:m]))
s6 = abs(sum(sorted([-x+y-z for x, y, z in xyz])[:m]))
s7 = abs(sum(sorted([-x-y+z for x, y, z in xyz])[:m]))
s8 = abs(sum(sorted([-x-y-z for x, y, z in xyz])[:m]))

print(max(s1, s2, s3, s4, s5, s6, s7, s8))
### BUGGY CODE ###

# -*- coding: utf-8 -*-
"""
Created on Sat Mar 16 15:50:39 2019

@author: Yamazaki Kenichi
"""

N,A,B,K = map(int,input().split())
mod = 998244353

T = []
R = K//A
for i in range(R+1):
    if i == 0:
        if K%B == 0 and K//B <= N:
            T.append([0,0,K//B,N-K%B])
        continue
    if (K - (A*i)) % B == 0:
        r = i
        b = (K - (A*i)) // B
        g = min(r,b)
        r,b = r-g, b-g
        while r+g+b <=N and g>=0:
            T.append([r,g,b,N-(r+g+b)])
            r,g,b = r+1,g-1,b+1
g1 = [1, 1] # 元テーブル
g2 = [1, 1] # 逆元テーブル
inv = [0, 1] # 逆元テーブル計算用テーブル
for i in range(2, N+1):
    g1.append((g1[-1] * i) % mod)
    inv.append((-inv[mod % i] * (mod//i)) % mod)
    g2.append((g2[-1] * inv[-1]) % mod)

ans = 0
for c in T:
    ans += g1[N]*g2[c[0]]*g2[c[1]]*g2[c[2]]*g2[c[3]]%mod
print(ans%mod)


### FIXED CODE ###

# -*- coding: utf-8 -*-
"""
Created on Sat Mar 16 18:40:19 2019

@author: Yamazaki Kenichi
"""

N,A,B,K = map(int,input().split())
mod = 998244353
def cmb(n, r, mod):
    if (r < 0 or r > n):
        return 0
    r = min(r, n-r)
    return g1[n] * g2[r] * g2[n-r] % mod
g1 = [1, 1] # 元テーブル
g2 = [1, 1] # 逆元テーブル
inv = [0, 1] # 逆元テーブル計算用テーブル
for i in range(2, N+1):
    g1.append((g1[-1] * i) % mod)
    inv.append((-inv[mod % i] * (mod//i)) % mod)
    g2.append((g2[-1] * inv[-1]) % mod)
def a(r,b):
    return cmb(N,r,mod)*cmb(N,b,mod)
ans = 0
R = K//A
for i in range(R+1):
    if (K-A*i)%B == 0:
        ans += a(i,(K-A*i)//B) % mod
print(ans%mod)
### BUGGY CODE ###

N, S = open(0).read().split()
N = int(N)
ans = set()
for i in range(N):
    for j in range(i+1, N):
        for k in range(j+1, N):
            ans.add(S[i] + S[j] + S[k])
print(len(ans))


### FIXED CODE ###

N, S = open(0).read().split()
S = list(map(int, list(S)))
N = int(N)

cnt = 0
for i in range(0, 10):
        for j in range(0, 10):
            for k in range(0, 10):
                try:
                    n = S.index(i)
                    m = S[n+1:].index(j)
                    o = S[n+m+2:].index(k)
                    cnt += 1
                except ValueError:
                    pass
print(cnt)
### BUGGY CODE ###

s = input()
N = int(input())

S = []
for i in range(len(s)):
    S.append(s[i])
    
back_count = 0
for i in range(N):
    Q = list(map(str, input().split()))
    Q[0] = int(Q[0])
    if Q[0] == 1:
        if back_count == 0:
            back_count = 1
        else:
            back_count = 0
    else:
        Q[1] = int(Q[1])
        if back_count == 0:
            if Q[1] == 1:
                S.insert(0, Q[2])
            else:
                S.append(Q[2])
        else:
            if Q[1] == 1:
                S.append(Q[2])
            else:
                S.insert(0, Q[2])

if back_count == 0:
    print("".join(S))
else:
    S = S[::-1]
    print("".join(S))

### FIXED CODE ###

from collections import deque

s = input()
N = int(input())

S = []
for i in range(len(s)):
    S.append(s[i])

d = deque(S)
    
back_count = 0
for i in range(N):
    Q = list(map(str, input().split()))
    Q[0] = int(Q[0])
    if Q[0] == 1:
        if back_count == 0:
            back_count = 1
        else:
            back_count = 0
    else:
        Q[1] = int(Q[1])
        if back_count == 0:
            if Q[1] == 1:
                d.appendleft(Q[2])
            else:
                d.append(Q[2])
        else:
            if Q[1] == 1:
                d.append(Q[2])
            else:
                d.appendleft(Q[2])

S = list(d)
if back_count == 0:
    print("".join(S))
else:
    S = S[::-1]
    print("".join(S))
### BUGGY CODE ###

s = input()
ans = 0
c = 1
while c > 0:
    c = s.count('ABC')
    ans += c
    s = s.replace('ABC', 'BCA')
# print(s)
print(ans)


### FIXED CODE ###

s = list(input())
ans = 0
ls = len(s)
i = 0
ca = 0
while i < ls-2:
    if s[i:i+3] == ['A', 'B', 'C']:
        ans += 1 + ca
        # print(i, ans, ca)
        s[i+2] = 'A'
        if ca >= 1:
            ca -= 2
            s[i+1] = 'A'
        else:
            ca = 0
        continue
    elif s[i] == 'A':
        ca += 1
    else:
        ca = 0
    i += 1

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

N=list(map(int,input().split()))
A=list(map(int,input().split()))
A.sort()

cnt=0
ans=[]
ans_=[]
for a in A:
    for ans_idx in range(len(ans)+1):
        if ans_idx == len(ans):
            ans.append(a)
            ans_.append(a)
            break
            
        ans_temp=ans[ans_idx]
        if a % ans_temp == 0:
            if a in ans_:
                ans_.remove(a)
            break
        
print(len(ans_))

### FIXED CODE ###

# %%time
# from collections import Counter

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

# A=list(range(2,10**6))

# counter = Counter(A)

A.sort()
max=A[-1]
dp=[0]*(max+1)

for i in A:
    if dp[i]!=0:
        dp[i]+=1
        continue
    for j in range(1*i,max+1,i):
        dp[j]+=1
        
ans=[]
for a_idx,a in enumerate(A):
    if dp[a] == 1:
        ans.append(a)
        
print(len(ans))
### BUGGY CODE ###

import sys
sys.setrecursionlimit(10000000)
n,k= map(int,input().split())
mxScore = set()
items= []
for i in range(n):
    w,v = map(int,input().split())
    items.append([w,v])
def factory(item,weight,value):
    if item==n:
        mxScore.add(value)
    else:
        w, v = items[item]
        factory(item + 1,weight, value)
        if weight+w<=k:
            factory(item+1,weight+w,value+v)
factory(0,0,0)
print(max(mxScore))

### FIXED CODE ###

n,k = map(int,input().split())
mx = 100000
dp = [[k+1 for _ in range(mx+1)]for _ in range(n+1)]
dp[0][0]=0
for i in range(1,n+1):
    dp[i][0]= 0
    w,v = map(int,input().split())
    for j in range(v):
        dp[i][j]= dp[i-1][j]
    for j in range(v,mx+1):
        next = w+dp[i-1][j-v]
        if next<=k:
            dp[i][j]= min(next,dp[i-1][j])
        else:
            dp[i][j]=dp[i-1][j]
for i in range(mx,-1,-1):
    if dp[n][i]<=k:
        print(i)
        break
### BUGGY CODE ###

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

B = sorted([x for x in set(A) if A.count(x) == 1])
C = [x for x in set(A) if A.count(x) > 1]
ans = 0
#自身よりも小さい数で試し割　あとに重複した要素で試し割
for i in range(len(B)):
    for b in B[:i]:
        if B[i] % b == 0:
            break
    else:
        for c in C:
            if B[i] % c == 0:
                break
        else:
            ans += 1
print(ans)

### FIXED CODE ###

N = int(input())
A = list(map(int, input().split()))
MAX = max(A)
dp = [True for _ in range(MAX+1)]

for a in set(A):
    for k in range(2*a,MAX+1,a):
        dp[k] = False
B = sorted(A)
for i in range(len(B)-1):
    if B[i] == B[i+1]:
        dp[B[i]] = False

print(sum(dp[a] for a in set(A)))
### BUGGY CODE ###

N = int(input())

A = [int(x) for x in input().split()]

for j in range(N):
    b = A.count(j+1)
    print(b)

### FIXED CODE ###

N = int(input())
A = [int(x) for x in input().split()]
B = [0] * N
for i in range(N-1):
    B[(A[i]-1)] += 1

for j in range(N):
    print(B[j])
### BUGGY CODE ###

def warshall_floyd(w):
    inf = 1E+100
    n = len(w)
    d = w
    for k in range(n):
        d_new = [[inf for _ in range(n)] for _ in range(n)]
        for i in range(n):
            for j in range(n):
                d_new[i][j] = min(d[i][j], d[i][k] + d[k][j])
        d = d_new
    return d


inf = 1E+100
s = [int(x) for x in input().split()]
n = s[0]
m = s[1]
w = [[inf for _ in range(n)] for _ in range(n)]
for i in range(n):
    w[i][i] = 0
for _ in range(m):
    s = [int(x) for x in input().split()]
    w[s[0]][s[1]] = 1
    w[s[1]][s[0]] = 1

d = warshall_floyd(w)

q = int(input())
for _ in range(q):
    s = [int(x) for x in input().split()]
    u = s[0]
    v = s[1]
    if d[u][v] < inf:
        print("yes")
    else:
        print("no")


### FIXED CODE ###

n, m = map(int, input().split())
adj = [[] for _ in range(n)]
for _ in range(m):
    s = [int(x) for x in input().split()]
    adj[s[0]].append(s[1])
    adj[s[1]].append(s[0])

cc = [-1 for _ in range(n)]
cnt = 0
for i in range(n):
    if cc[i] < 0:
        cc[i] = cnt
        stack = [i]
        while stack:
            v = stack.pop()
            for u in adj[v]:
                if cc[u] < 0:
                    cc[u] = cnt
                    stack.append(u)
        cnt += 1

q = int(input())
for _ in range(q):
    s, t = map(int, input().split())
    if cc[s] == cc[t]:
        print("yes")
    else:
        print("no")