#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<b;i++)
#define rrep(i,a,b) for(int i=a;i>=b;i--)
#define fore(i,a) for(auto &i:a)
#define all(x) (x).begin(),(x).end()
#pragma GCC optimize ("-O3")
using namespace std; void _main(); int main() { cin.tie(0); ios::sync_with_stdio(false); _main(); }
typedef long long ll; const int inf = INT_MAX / 2; const ll infl = 1LL << 60;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a = b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a = b; return 1; } return 0; }
//---------------------------------------------------------------------------------------------------
template<typename Func>
ll findMax(ll L, ll R, Func f) { //[L, R)
	ll lo = L, hi = R;
	while (lo + 1 != hi) {
		ll mi = (lo + hi) / 2;
		if (f(mi) - f(mi - 1) > 0) lo = mi;
		else hi = mi;
	}
	return lo;
}
/*---------------------------------------------------------------------------------------------------
　　　　　　　　　　　 ∧＿∧  
　　　　　 ∧＿∧ 　（´<_｀ ）　 Welcome to My Coding Space!
　　　　 （ ´_ゝ`）　/　 ⌒i     
　　　　／　　　＼　 　  |　|     
　　　 /　　 /￣￣￣￣/　　|  
　 ＿_(__ﾆつ/　    ＿/ .| .|＿＿＿＿  
　 　　　＼/＿＿＿＿/　（u　⊃  
---------------------------------------------------------------------------------------------------*/








ll A, B;
//---------------------------------------------------------------------------------------------------
ll aa, bb;
ll getmax(ll a, ll b, ll n) {
    if (n == 0) return 0;

    aa = a; bb = b;
    // y=(a+x)(b-x)  0≦x<n
    ll xx = findMax(0, n, [](ll x) { return (aa + x)*(bb - x); });
    return (aa + xx) * (bb - xx);
}
//---------------------------------------------------------------------------------------------------
int check(ll x) {
    if (x < A and x < B) return 1;
    else if (A <= x and x < B) {
        // A以上B未満
        // 123 A56789...
        // 876 54321...B

        if (getmax(1, x, A - 1) < A*B and getmax(A + 1, x - (A - 1), x - A + 1) < A*B) return 1;

        return 0;
    } else {
        // A,B以上

        ll lft = A - 1;
        ll rht = x + 1 - B;

        if (lft < rht) {
            // 12 A4567 89
            // 12 345B7 89
            ll a = getmax(1, x + 1, lft);
            ll b = getmax(A + 1, x + 1 - lft, rht-lft);
            ll c = getmax(rht+2, B - 1, x - rht);
            if (a < A*B and b < A*B and c < A*B) return 1;
        }
        else if (lft == rht) {
            // 12 A4567 89
            // 12 3456B 89

            ll a = getmax(1, x + 1, lft);
            ll b = getmax(A + 1, B - 1, x - lft);
            if (a < A*B and b < A*B) return 1;
        }
        else {
            // 12 3A567 89
            // 12 3456B 89
            ll a = getmax(1, x + 1, rht);
            ll b = getmax(rht+1, B-1, lft-rht);
            ll c = getmax(A + 1, x+1-lft-1 , x - lft);
            if (a < A*B and b < A*B and c < A*B) return 1;
        }
        return 0;
    }
}
//---------------------------------------------------------------------------------------------------
void solve() {
    cin >> A >> B;
    if (A > B) swap(A, B);

    ll ok = A-1, ng = B*2+100;
    while (ok + 1 != ng) {
        ll md = (ok + ng) / 2;
        if (check(md)) ok = md;
        else ng = md;
    }
    printf("%lld\n", ok);
}
//---------------------------------------------------------------------------------------------------
void _main() {
    int T; cin >> T;
    rep(t, 0, T) solve();
}

Measured memory_bytes: 256