/*Bismillahir Rahmanir Rahim*///{
#include <bits/stdc++.h>
using namespace std;
using ll = long long; using ull = unsigned long long; using ld = long double;
const ll llinf = (1ll<<61)-1;
const double eps = 1e-6, ldeps = 1e-9;
struct Dbg {
static constexpr auto &os = cout;   // replace by cerr in interactive.
template<class C> static auto dud(C *x)->decltype(os << *x, 0);
template<class C> static char dud(...);
template<class C> typename enable_if<sizeof dud<C>(0)!=1, Dbg&>::type operator<<(const C &x) { os << x; return *this; }
template<class C> Dbg &dump(C b, C e) {
    *this << "\n[";
    int cur = 0;
    for (C i = b; i!=e and cur<26; i++, cur++) *this << ", "+2*(i==b) << *i;
    return *this << ']';
}
template<class C> typename enable_if<sizeof dud<C>(0)==1, Dbg&>::type operator<<(const C &x) {
    return dump(begin(x), end(x));
}
template<class C, size_t X> typename enable_if<sizeof(C)!=1, Dbg&>::type operator <<(C (&x)[X]) {
    return dump(begin(x), end(x));
}
template<class B, class C> Dbg &operator <<(const pair<B, C> &x) {
    return *this << '{' << x.first << ", " << x.second << '}';
}
template<class C, size_t Y> struct TP {
    void operator()(Dbg& os, const C &t) { TP<C, Y-1>()(os, t); os << ", " << get<Y-1>(t); }
};
template<class C> struct TP<C, 1> { void operator()(Dbg& os, const C &t) { os << get<0>(t); } };
template<class... C> Dbg& operator<<(const tuple<C...> &t) {
    os << '{', TP<decltype(t), sizeof...(C)>()(*this, t); return *this << '}';
}
Dbg &operator<<(ostream&(*x)(std::ostream&)) { os << x; return *this; }
} dbg;
void err(istringstream *iss) { delete iss; }
template<class C, class... Args> void err(istringstream *iss, const C &val, const Args &... args) {
    string name; *iss >> name; if (name.back()==',') name.pop_back();
    dbg << name << " = " << val << "; ", err(iss, args ...);
}
#define bug(args...) dbg << __LINE__ << ": ", err(new istringstream(string(#args)), args), dbg << endl
#define ASSERT(a, o, b) { if (!((a) o (b))) bug(a, b), assert((a) o (b)); }
#define sl(a) int(strlen(a))
#define sz(a) int(a.size())
int T, Q, cn;//}
const int inf = 1000000007, mxn = -1;

ll f(ll x, ll c, ll d) {
    ll res;
    if (__builtin_mul_overflow(c+x, d-x, &res)) return llinf;
    return res;
}

ll maxInPartition(ll x, ll c, ll d) {
    if (x<0) return -llinf;
    ll lo = 0, hi = x;
    while(lo<hi) {
        auto mid = (lo+hi) >> 1;
        if (f(mid, c, d) > f(mid+1, c, d)) hi = mid; else lo = mid+1;
    }
    return f(lo, c, d);
}

bool check(ll x, ll a, ll b) {
    ll mx = -llinf;
    if (a>x) mx = maxInPartition(x-1, 1, x);
    else {
        if (b>x) {
            ll p1 = a-1;
            mx = max(maxInPartition(p1-1, 1, x), maxInPartition(x-p1-1, a+1, x-p1));
        }
        else {
            ll sb = x+1, p1 = min(a-1, sb-b), p2 = max(a-1, sb-b);
            if (a-1<=sb-b) {
                mx = max({maxInPartition(p1-1, 1, sb), maxInPartition(p2-p1-1, p1+2, sb-p1), maxInPartition(x-p2-1, p2+2, sb-p2-1)});
            } else {
                mx = max({maxInPartition(p1-1, 1, sb), maxInPartition(p2-p1-1, p1+1, sb-p1-1), maxInPartition(x-p2-1, p2+2, sb-p2-1)});
            }
        }
    }
    return mx<a*b;
}

signed main() { ios::sync_with_stdio(0); cin.tie(NULL); cout.precision(11);
/*
In the check(x) of binary search over answer:
Let, f[] = first x numbers avoiding a in ascending order, s[] = first x numbers avoiding b in descending order.
Now, each of the x participants' score will be f[i]*s[i]. So it's sufficient to find max of f[i]*s[i].
It's easy to notice that there are at most 3 such partitions where for all i: f[i] = f[i-1]+1 and s[i] = s[i-1]-1.
3 partition happens when max(a,b)<=x. BTW, order of a,b doesn't matter, so we can make a<b.
Let, starting rank of such a partition is {c,d}: then all other ranks can be represented by c+x,d-x.
Let, f(x) = (c+x)*(d-x) = cd-cx+dx-x**2. Derivative of this function is: -c+d-2x, which is decreasing.
==> We can find max of this function by ternary search.
*/

    cin >> Q;
    while (Q--) {
        int a, b;
        cin >> a >> b;
        if (a>b) swap(a, b);
        ll lo = 0, hi = llinf, ans = -1;
        while (lo<=hi) {
            auto mid = (lo+hi)>>1;
            if (check(mid, a, b)) lo = mid+1, ans = mid;
            else hi = mid-1;
        }
        cout << ans << '\n';
    }
}

Measured memory_bytes: 256