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 (Clang 3.8.0) (judge verdict: Accepted)

#include <stdio.h>
#include <utility>

constexpr int UB = 1e9+1;
long long int getSQRT(long long int A){
    long long int ok = 0, ng = UB;
    while(ng - ok > 1){
        long long int mid = (ok+ng) >> 1;
        if(mid * mid < A) ok = mid;
        else ng = mid;
    }
    return ok;
}


int main(void){
    int Q; scanf("%d", &Q);
    while(Q--){
        int a, b; scanf("%d%d", &a, &b);
        if(a > b) std::swap(a, b);
        // a ≤ bとしても一般性を失わない
        // 明らかに, a-1ペアを作ることはできる, そして明らかに解はb-1以下
        // 1 ~~~> ab-1と組めば良い
        // 2 ~~~> floor(ab/2)と組めば良い
        // 3 ~~~> floor(ab/3)と組めば良い
        // ....
        // t^2 ≤ ab なる整数とする
        // 1 -> ab-1
        // 2 -> small(ab/2)
        // 3 -> small(ab/3)
        // ....
        // t -> small(ab/t)
        // それ以降は被ってしまう : 逆向きもそう ; t ≤ ab/tであるから被りもしない ? 
        // (1, ab-1), ..., (T, T or T+1?)
        // (ab-1, 1), ..., (T, T or T+1?)   <= ここに (a, b)が入るのか ? Tの部分で被りあるかも？
        // (a, b)は近くなければ, 余裕がそれなりにあるのでワンチャンセーフでは？
        //     e.g. T-1 -> T, T+1 ok
        // でも, T = aみたいなケースだと厳しい
        // どんなとき? a^2 < a*(a+1) < a*(a+2) < (a+1)^2
        // 差が2以下の時
        // a == b ----> (1, a^2-1), ..., (a-1, a+1)で組めば良い : 2a-2
        // a == b + 1 ---> (1,x)...., (a-1, a+1), (x, 1), ...., (a+1, a-1); 2a-2
        // a == b + 2 ---> (1,x)...., (a-1, a+1), (x,1)...(a+2,a) : 2a-1
        // それ以外なら, a, bの混入を気にせず基本的に上記の方法でやれば良い
        // (T-1, ...., T+1 or T) : T - T+1がい毛ないなら-1組
        long long int K = a; K *= b;
        long long int T = getSQRT(K);
        if(b - a <= 2){
            if(a == b or a + 1 == b) printf("%d\n", 2 * a - 2);
            else printf("%d\n", 2 * a - 1);
        }
        else if(T * (T+1) < K){
            printf("%lld\n", 2 * T - 1);
        }
        else printf("%lld\n", 2 * T - 2);
    }
    return 0;
}