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

void fastIO() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
}

int main() {
    fastIO();
    ll t; 
    cin >> t;
    while (t--) {
        ll n, m;
        cin >> n >> m;
        // compute maximum g such that g(g-1) <= n:
        // g <= (1 + sqrt(1+4n)) / 2
        long double D = 1.0L + 4.0L * n;
        ll root = (ll)((1.0L + sqrtl(D)) / 2.0L);
        // clamp to [0..m]
        ll G = min(root, m);
        // we need g >= 2 so that a = g*(g-1) >= 1
        // count is number of g in [2..G] => max(0, G-1)
        ll ans = G >= 2 ? (G - 1) : 0;
        cout << ans << "\n";
    }
    return 0;
}
