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

#define NDEBUG
#ifdef DEBUG
#include "cout11.h"
#undef NDEBUG
#endif
#include <cassert>

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> ii;
typedef pair<ll,ll> llll;
typedef pair<double,double> dd;

typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<ii> vii;
typedef vector<vector<ii>> vvii;
typedef vector<ll> vll;

#define sz(a)  int((a).size())
#define pb  push_back
#define FOR(var,from,to) for(int var=(from);var<=(to);++var)
#define rep(var,n)  for(int var=0;var<(n);++var)
#define rep1(var,n)  for(int var=1;var<=(n);++var)
#define repC2(vari,varj,n)  for(int vari=0;vari<(n)-1;++vari)for(int varj=vari+1;varj<(n);++varj)
#define ALL(c)  (c).begin(),(c).end()
#define RALL(c)  (c).rbegin(),(c).rend()
#define tr(i,c)  for(auto i=(c).begin(); i!=(c).end(); ++i)
#define found(s,e)  ((s).find(e)!=(s).end())
#define mset(arr,val)  memset(arr,val,sizeof(arr))
#define mid(x,y) ((x)+((y)-(x))/2)
#define IN(x,a,b) ((a)<=(x)&&(x)<=(b))

bool p(ll a, ll b, ll m){
    // m人いける
#ifdef DEBUG
//    fprintf(stderr, "p(a=%lld, b=%lld, m=%lld)\n", a,b,m);
#endif
    ll ab = a * b;
    int h = m/2;
    if (a > m) {
        if (b > m) {
            // x: 1..m
            // y: m..1
            // x(m+1 -x) < ab
            // x2 - (m+1)x + ab > 0
            auto f0 = [&](ll x){ return x*(m+1 - x) < ab; };
            if (!f0(1) || !f0(m)) return false;

            int h = (m+1)/2;
            if (IN(h, 1, m) && !f0(h)) return false;
            if (IN(h+1, 1, m) && !f0(h+1)) return false;
            return true;
        } else {
            // x: 1.._b | _b+1..m
            // y: m+1..b+1 || b-1..1  excluding b
            auto f1 = [&](ll x){ return x*(m+2 - x) < ab; };
            auto f2 = [&](ll x){ return x*(m+1 - x) < ab; };
            if (!f1(1) || !f2(m)) return false;
            int _b = (m+2) - (b+1);

            int h = (m+1)/2;
            if (IN(h, 1, _b) && !f1(h)) return false;
            else if (IN(h, _b+1, m) && !f2(h)) return false;
            if (IN(h+1, 1, _b) && !f1(h+1)) return false;
            else if (IN(h+1, _b+1, m) && !f2(h+1)) return false;
        }
    } else {
        if (b > m) {
            // x: 1..a-1 ||a+1..m+1  excluding a
            // y: m..z || z+1..1
            auto f1 = [&](ll x){ return x*(m+1 - x) < ab; };
            auto f2 = [&](ll x){ return (x+1)*(m+1 - x) < ab; };
            if (!f1(1) || !f2(m)) return false;
            int _a = a-1;

            int h = (m+1)/2;
            if (IN(h, 1, _a) && !f1(h)) return false;
            else if (IN(h, _a+1, m) && !f2(h)) return false;
            if (IN(h+1, 1, _a) && !f1(h+1)) return false;
            else if (IN(h+1, _a+1, m) && !f2(h+1)) return false;
        } else {
            // x: 1..a-1 || a+1..m+1  excluding a
            // y: m+1..b+1 || b-1..1  excluding b
            int _a = a-1;
            int _b = (m+2) - (b+1);
            auto f1 = [&](ll x){ return x*(m+2 - x) < ab; };
            auto f2 = [&](ll x){ return (x+1)*(m+1 - x) < ab; };
            if (!f1(1) || !f2(m)) return false;
            if (_a < _b) {
                auto fm = [&](ll x){ return (x+1)*(m+2 - x) < ab; };

                if (IN(h, 1, _a) && !f1(h)) return false;
                else if (IN(h, _a+1, _b) && !fm(h)) return false;
                else if (IN(h, _b+1, m) && !f2(h)) return false;

                if (IN(h+1, 1, _a) && !f1(h+1)) return false;
                else if (IN(h+1, _a+1, _b) && !fm(h+1)) return false;
                else if (IN(h+1, _b+1, m) && !f2(h+1)) return false;
            } else if (_a == _b) {
                if (IN(h, 1, _a) && !f1(h)) return false;
                else if (IN(h, _a+1, m) && !f2(h)) return false;

                if (IN(h+1, 1, _a) && !f1(h+1)) return false;
                else if (IN(h+1, _a+1, m) && !f2(h+1)) return false;
            } else if (_b < _a) {
                auto fm = [&](ll x){ return x*(m+1 - x) < ab; };

                if (IN(h, 1, _b) && !f1(h)) return false;
                else if (IN(h, _b+1, _a) && !fm(h)) return false;
                else if (IN(h, _a+1, m) && !f2(h)) return false;
                if (IN(h+1, 1, _b) && !f1(h+1)) return false;
                else if (IN(h+1, _b+1, _a) && !fm(h+1)) return false;
                else if (IN(h+1, _a+1, m) && !f2(h+1)) return false;
            }
        }
    }
    return true;
}

ll solve(ll a, ll b) {
    ll lo = 0, hi = sqrt(a*b)*2+1+(a+b+1); // lo: possible, hi: impossible
    while (lo+1 < hi) {
        ll m = mid(lo, hi);
#ifdef DEBUG
//        fprintf(stderr, "[%lld, %lld, %lld)\n", lo,m,hi);
#endif
        if (p(a,b,m)) lo = m;
        else hi = m;
    }
    return lo;
}

int main() {
    int Q; cin >> Q; //1-100
    rep(i,Q){
        int a, b; cin >> a >> b;
        cout << solve(a, b) << endl;
    }
    return 0;
}


Measured memory_bytes: 256