Problem:
Score : 700 points

Problem Statement

10^{10^{10}} participants, including Takahashi, competed in two programming contests.
In each contest, all participants had distinct ranks from first through 10^{10^{10}}-th.

The score of a participant is the product of his/her ranks in the two contests.

Process the following Q queries:

In the i-th query, you are given two positive integers A_i and B_i. Assuming that Takahashi was ranked A_i-th in the first contest and B_i-th in the second contest, find the maximum possible number of participants whose scores are smaller than Takahashi's.

Constraints

1 \leq Q \leq 100

1\leq A_i,B_i\leq 10^9(1\leq i\leq Q)

All values in input are integers.

Input

Input is given from Standard Input in the following format:

Q
A_1 B_1
:
A_Q B_Q

Output

For each query, print the maximum possible number of participants whose scores are smaller than Takahashi's.

Sample Input 1

8
1 4
10 5
3 3
4 11
8 9
22 40
8 36
314159265 358979323

Sample Output 1

1
12
4
11
14
57
31
671644785

Let us denote a participant who was ranked x-th in the first contest and y-th in the second contest as (x,y).

In the first query, (2,1) is a possible candidate of a participant whose score is smaller than Takahashi's. There are never two or more participants whose scores are smaller than Takahashi's, so we should print 1.

Solution in C++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

#include <bits/stdc++.h>
using namespace std;
#define REP(i,a,n) for(int i=(a); i<(int)(n); i++)
#define rep(i,n) REP(i,0,n)
#define FOR(it,c) for(__typeof((c).begin()) it=(c).begin(); it!=(c).end(); ++it)
#define ALLOF(c) (c).begin(), (c).end()
typedef long long ll;
typedef unsigned long long ull;

bool check(pair<ll,ll> x, pair<ll,ll> y, ll ab){
  ll n = x.second+1 - x.first;
  ll a0 = x.first;
  ll b0 = y.first;

  ll lb = 0, ub = n;
  while(ub-lb>1){
    ll m = lb + (ub-lb)/2;
    double aa = (a0 + m) * (double)(b0 - m);
    double bb = (a0 + m+1) * (double)(b0 - (m+1));
    //cout << "    " << m << " " << aa << " " << bb << endl;
    if(bb-aa>0) lb = m;
    else ub = m;
  }
  //cout << " check: " << lb << " " << (a0 + lb) * (double)(b0 - lb) << " " << (double)ab << endl;
  if(log(a0 + lb) + log(b0 - lb) < log(ab)){
    return (a0 + lb) * (b0 - lb) < ab;
  }
  return false;
}


bool f(ll A, ll B, ll X){
  //cout << "F: " << A << " " << B << " " << X << endl;
  vector<pair<ll,ll>> v, w;
  if(A<=X){
    if(1<=A-1) v.push_back(make_pair(1,A-1));
    if(A+1<=X+1) v.push_back(make_pair(A+1,X+1));
  }else{
    v.push_back(make_pair(1,X));
  }
  if(B<=X){
    if(X+1>=B+1) w.push_back(make_pair(X+1,B+1));
    if(B-1>=1) w.push_back(make_pair(B-1,1));
  }else{
    w.push_back(make_pair(X,1));
  }
  if(v.size() == 0 || w.size() == 0) return false;
/*
  rep(i,v.size()){
    cout << " " << v[i].first << " " << v[i].second << endl;
  }
  rep(i,w.size()){
    cout << " " << w[i].first << " " << w[i].second << endl;
  }
*/
  vector<pair<ll,ll>> vv, ww;
  int ii = 0, jj = 0;
  pair<ll,ll> x(0,0), y(0,0);
  while(true){

    if(x.first == 0){
      if(ii>=v.size()) break;
      x = v[ii];
      ii++;
    }
    if(y.first == 0){
      if(jj>=w.size()) break;
      y = w[jj];
      jj++;
    }
    ll n = x.second+1-x.first;
    ll m = y.first+1-y.second;

    if(n < m){
      vv.push_back(x);
      x.first = 0;
      ww.push_back(make_pair(y.first,y.first-(n-1)));
      y.first = y.first-n;
      if(y.first < y.second) y.first = 0;
    }
    else if(n == m){
      vv.push_back(x);
      x.first = 0;
      ww.push_back(y);
      y.first = 0;
    }
    else{
      vv.push_back(make_pair(x.first,x.first+(m-1)));
      x.first = x.first+m;
      if(x.first > x.second) x.first = 0;
      ww.push_back(y);
      y.first = 0;
    }
  }

  //rep(i,vv.size()){
  //  cout << i << " " << vv[i].first << " " << vv[i].second << " " << ww[i].first << " " << ww[i].second << endl;
  //}

  bool flg = true;
  rep(i,vv.size()){
    if(!check(vv[i],ww[i],A*B)) flg = false;
  }
  //if(flg) cout << "YES" << endl;
  //else cout << "NO" << endl;
  return flg;
}


ll solve(ll A, ll B){
  if(A>B) swap(A,B);

  ll lb = 0, ub = A*B;
  while(ub-lb>1){
    ll m = (lb+ub)/2;
    if(f(A, B, m)) lb = m;
    else ub = m;
  }

  return lb;
}


int main(){
  int Q;
  cin >> Q;

  rep(i,Q){
    ll A, B;
    cin >> A >> B;
    cout << solve(A, B) << endl;
  }
  return 0;
}