CodeforcesAug 24, 2025

Zmei Gorynich

Hazrat Ali

Codeforces

You are fighting with Zmei Gorynich — a ferocious monster from Slavic myths, a huge dragon-like reptile with multiple heads!

Initially Zmei Gorynich has x heads. You can deal n types of blows. If you deal a blow of the i-th type, you decrease the number of Gorynich's heads by min(di,curX), there curX is the current number of heads. But if after this blow Zmei Gorynich has at least one head, he grows hi new heads. If curX=0 then Gorynich is defeated.

You can deal each blow any number of times, in any order.

For example, if curX=10d=7h=10 then the number of heads changes to 13 (you cut 7 heads off, but then Zmei grows 10 new ones), but if curX=10d=11h=100 then number of heads changes to 0 and Zmei Gorynich is considered defeated.

Calculate the minimum number of blows to defeat Zmei Gorynich!

You have to answer t independent queries.

Input

The first line contains one integer t (1t100) – the number of queries.

The first line of each query contains two integers n and x (1n1001x109) — the number of possible types of blows and the number of heads Zmei initially has, respectively.

The following n lines of each query contain the descriptions of types of blows you can deal. The i-th line contains two integers di and hi (1di,hi109) — the description of the i-th blow.

Output

For each query print the minimum number of blows you have to deal to defeat Zmei Gorynich.

If Zmei Gorynuch cannot be defeated print 1.

Example
Input
3
3 10
6 3
8 2
1 4
4 10
4 1
3 2
2 6
1 100
2 15
10 11
14 100
Output
2
3
-1

Solution

#include <bits/stdc++.h>
using namespace std;

int main()
{

    int t;
    cin >> t;
    while (t--)
    {
        int n, x;
        cin >> n >> x;
        vector<int> d(n), h(n);
        int mxd = 0, mxdh = 0;
        for (int i = 0; i < n; i++)
        {
            cin >> d[i] >> h[i];
            mxd = max(mxd, d[i]);
            mxdh = max(mxdh, d[i] - h[i]);
        }
        int ans = x <= mxd ? 1 : -1;
        if (x > mxd and mxdh > 0)
        {
            ans = ceil(1.0 * (x - mxd) / mxdh) + 1;
        }
        cout << ans << endl;
    }
    return 0;
}





Comments