MIDEARTH - Middle Earth

no tags 

Middle Earth 
Barbarians in Middle-Earth consider pillaging villages a sport and they are quite fond of it. Hussain the greatest barbarian in the world arrived to Gondor, and wants to prove himself by pillaging as many villages as he can. 
There are N villages in Gondor that Hussain can pillage in any order he likes, but to pillage village i he has to spend Ai golden coins from his personal bank account on weapons and armor to successfully pillage that village otherwise he will die in the attack, but if his attack is successful he will loot Bi golden coins and add it to his personal bank account. 
Hussain starts with C golden coins in his bank account and due to his barbarian code of honor he will keep attacking villages until he either pillages all villages or dies. 
Barbarians define a strong village if and only if Ai>Bi, Hussain also knows based on the BEAA (Barbarian Enemy Analysis Algorithm) that in Gondor there will never be more than 15 strong villages. 
And because Hussain is a proud barbarian and wants to bring honor to his tribe he asked you to determine the maximum number of villages that he can successfully pillage.

Barbarians in Middle-Earth consider pillaging villages a sport and they are quite fond of it. Hussain the greatest barbarian in the world arrived to Gondor, and wants to prove himself by pillaging as many villages as he can. 

There are N villages in Gondor that Hussain can pillage in any order he likes, but to pillage village i he has to spend Ai golden coins from his personal bank account on weapons and armor to successfully pillage that village otherwise he will die in the attack, but if his attack is successful he will loot Bi golden coins and add it to his personal bank account. 

Hussain starts with C golden coins in his bank account and due to his barbarian code of honor he will keep attacking villages until he either pillages all villages or dies. 

Barbarians define a strong village if and only if Ai>Bi, Hussain also knows based on the BEAA (Barbarian Enemy Analysis Algorithm) that in Gondor there will never be more than 15 strong villages that could be attacked at any time (That means if there is a village that could never be attacked the algorithm will simply ignore it). 

And because Hussain is a proud barbarian and wants to bring honor to his tribe he asked you to determine the maximum number of villages that he can successfully pillage.

Input

The first line of input contains T (the number of test cases). 

The first line of each test case contains 2 integers separated by spaces (1 ≤ n ≤ 10^5) and (1≤ C ≤ 10^9). 

The following N lines contain 2 integers separated by spaces (1 ≤ Ai, Bi ≤ 10^9). 

Output

The answer to each test case separated by a new line. 

Example

Input:
1 
4 2 
2 10 
1 0 
10 23 
54 44

Output:
3 

hide comments
Simes: 2020-10-28 21:46:58

The statement "...that could be attacked at any time" really had me fooled.

nadstratosfer: 2020-03-29 22:19:06

Enjoyed this one, especially since I got stuck here 2 years ago after realizing it needs an algo I suck at. Well, I seem to still suck at it but found a good enough workaround =) Easily solvable with slower languages as the sum of n in a testfile is less than 300000, good problem.

hodobox: 2017-07-14 14:20:35

To clarify the confusion in the comments, the meaning of the whole '15 villages ' is as follows.

Take all N! orders in which the cities can be raided, and try to raid them in this order (skipping those you can't raid). Take the maximum number of gold coins Gondor has at any given time during all of these raids, lets call it M. So M is the highest possible amount of coins Gondor could get, no matter his strategy.
There are at most 15 villages in any testcase for which B_i < A_i <= M.

dmh's testcase is not valid according to this, as Gondor has 16 coins at one point, and there are 16 villages whose B_i A_i are 0 < 1 <= 16 (although its not a 'wrong' test case, the answer is 16, but such a 'hard' testcase is never in the input)

I hope that is clear enough (and I asserted the above logic in my code, and passed).

Last edit: 2017-07-14 14:26:18
Rishav Goyal: 2015-09-10 08:09:47

i guess , setters attention should be to give a new concept rather than making problem statement more difficult and unclear that most of our time gets consumed reading it. seariously! statement sucks. Or atleast prvide a test case which clarify the statement

Last edit: 2015-09-10 08:10:18
Bhavik: 2014-05-30 10:02:59

@dmh: 16 according to my understanding..i am trying:)
also your testcase has more than 15 strong villages so i dont know if this is valid testcase or not..

@ALEX: can you kindly tell if "dmh" testcase is valid? if yes then is ans=16?

Last edit: 2014-05-30 12:15:11
dmh: 2014-05-25 01:43:34

Can anyone provide their answer to the case:
1
16 16
1 0
1 0
1 0
1 0
1 0
1 0
1 0
1 0
1 0
1 0
1 0
1 0
1 0
1 0
1 0
1 0

Prahlad Yeri: 2014-05-24 20:07:20

Strong villages CAN be looted if you have enough money

Last edit: 2014-05-24 23:59:27
Evan Putra Limanto: 2014-05-24 15:53:23

I think there is something wrong with the testcases. There are more than 15 strong villages?
ٍEDIT: Sorry for the confusion I've updated the statement.

Last edit: 2014-05-24 15:03:45

Added by:Aleksandar Abas
Date:2014-05-24
Time limit:2s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ASM64
Resource:Own Problem