/***********Template Starts Here***********/
#include <bits/stdc++.h>
#define pb push_back
#define nl puts ("")
#define sp printf ( " " )
#define phl printf ( "hello\n" )
#define ff first
#define ss second
#define POPCOUNT __builtin_popcountll
#define RIGHTMOST __builtin_ctzll
#define LEFTMOST(x) (63-__builtin_clzll((x)))
#define MP make_pair
#define FOR(i,x,y) for(vlong i = (x) ; i <= (y) ; ++i)
#define ROF(i,x,y) for(vlong i = (y) ; i >= (x) ; --i)
#define CLR(x,y) memset(x,y,sizeof(x))
#define UNIQUE(V) (V).erase(unique((V).begin(),(V).end()),(V).end())
#define MIN(a,b) ((a)<(b)?(a):(b))
#define MAX(a,b) ((a)>(b)?(a):(b))
#define NUMDIGIT(x,y) (((vlong)(log10((x))/log10((y))))+1)
#define SQ(x) ((x)*(x))
#define ABS(x) ((x)<0?-(x):(x))
#define FABS(x) ((x)+eps<0?-(x):(x))
#define ALL(x) (x).begin(),(x).end()
#define LCM(x,y) (((x)/gcd((x),(y)))*(y))
#define SZ(x) ((vlong)(x).size())
#define NORM(x) if(x>=mod)x-=mod;
using namespace std;
typedef long long vlong;
/***********Template Ends Here***********/
int main () {
int kase;
scanf ( "%d", &kase );
while ( kase-- ) {
vlong n, m;
scanf ( "%lld %lld", &n, &m );
int critical = n - 1;
m -= n - 1;
int k = 1;
while ( m > 0 ) {
///Remove edges
m -= k;
if ( k == 1 ) { ///Special case
critical -= 2;
}
else {
critical -= 1;
}
k++;
}
printf ( "%d\n", critical );
}
return 0;
}
LyoqKioqKioqKioqVGVtcGxhdGUgU3RhcnRzIEhlcmUqKioqKioqKioqKi8KI2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CgojZGVmaW5lIHBiIHB1c2hfYmFjawojZGVmaW5lIG5sIHB1dHMgKCIiKQojZGVmaW5lIHNwIHByaW50ZiAoICIgIiApCiNkZWZpbmUgcGhsIHByaW50ZiAoICJoZWxsb1xuIiApCiNkZWZpbmUgZmYgZmlyc3QKI2RlZmluZSBzcyBzZWNvbmQKI2Rl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qKioqKioqLwoKaW50IG1haW4gKCkgewoKICAgIGludCBrYXNlOwogICAgc2NhbmYgKCAiJWQiLCAma2FzZSApOwoKICAgIHdoaWxlICgga2FzZS0tICkgewogICAgICAgIHZsb25nIG4sIG07CiAgICAgICAgc2NhbmYgKCAiJWxsZCAlbGxkIiwgJm4sICZtICk7CgogICAgICAgIGludCBjcml0aWNhbCA9IG4gLSAxOwogICAgICAgIG0gLT0gbiAtIDE7CgogICAgICAgIGludCBrID0gMTsKICAgICAgICB3aGlsZSAoIG0gPiAwICkgewoKICAgICAgICAgICAgLy8vUmVtb3ZlIGVkZ2VzCiAgICAgICAgICAgIG0gLT0gazsKICAgICAgICAgICAgaWYgKCBrID09IDEgKSB7IC8vL1NwZWNpYWwgY2FzZQogICAgICAgICAgICAgICAgY3JpdGljYWwgLT0gMjsKICAgICAgICAgICAgfQogICAgICAgICAgICBlbHNlIHsKICAgICAgICAgICAgICAgIGNyaXRpY2FsIC09IDE7CiAgICAgICAgICAgIH0KCiAgICAgICAgICAgIGsrKzsKICAgICAgICB9CgogICAgICAgIHByaW50ZiAoICIlZFxuIiwgY3JpdGljYWwgKTsKICAgIH0KCiAgICByZXR1cm4gMDsKfQo=