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 Rust (1.15.1) (judge verdict: Accepted)

#![allow(unused_imports)]
#![allow(non_snake_case)]
use std::cmp::*;
use std::collections::*;
use std::io::Write;

#[allow(unused_macros)]
macro_rules! debug {
    ($($e:expr),*) => {
        #[cfg(debug_assertions)]
        $({
            let (e, mut err) = (stringify!($e), std::io::stderr());
            writeln!(err, "{} = {:?}", e, $e).unwrap()
        })*
    };
}

fn criterion(i: i64, a: i64, b: i64) -> bool {
    if i < a {
        return true;
    }
    let mut result1;
    let mut result2;
    let mut result3 = true;

    if i > b {
        if a < i - b {
            result1 = ternary_search(0, a - 1, 1, i + 1) < a * b;
            result2 = ternary_search(a + 1, i - b, 1, i + 1) < a * b;
            result3 = ternary_search(b + 1, i, 1, i + 1) < a * b;
        } else if a == i - b {
            result1 = ternary_search(0, a - 1, 1, i + 1) < a * b;
            result2 = ternary_search(a + 1, i, 1, i + 1) < a * b;
        } else {
            result1 = ternary_search(0, i - b - 1, 1, i + 1) < a * b;
            result2 = ternary_search(i - b + 1, a - 1, 1, i + 1) < a * b;
            result3 = ternary_search(a + 1, i, 1, i + 1) < a * b;
        }
    } else {
        result1 = ternary_search(0, a - 1, 1, i) < a * b;
        result2 = ternary_search(0, i - a + 1, a + 1, i - a + 1) < a * b;
    }
    // debug!(i, a, b, ternary_search(0, i - 1, 1, i), result);
    result1 && result2 && result3
}

fn main() {
    let n = read::<usize>();
    let mut pairs = vec![];
    for _ in 0..n {
        let v = read_vec::<i64>();
        pairs.push(v);
    }
    for v in pairs {
        let a = min(v[0], v[1]);
        let b = max(v[0], v[1]);
        if a == b {
            println!("{}", (a - 1) * 2);
            continue;
        }
        let ans = binary_search(0, 2 * max(a, b), a, b, criterion);
        // debug!(ans);
        println!("{}", ans);
    }
}

fn read<T: std::str::FromStr>() -> T {
    let mut s = String::new();
    std::io::stdin().read_line(&mut s).ok();
    s.trim().parse().ok().unwrap()
}

fn read_vec<T: std::str::FromStr>() -> Vec<T> {
    read::<String>()
        .split_whitespace()
        .map(|e| e.parse().ok().unwrap())
        .collect()
}

fn eval(a: i64, b: i64, i: i64) -> i64 {
    (a + i) * (b - i)
}

fn binary_search<F>(lb: i64, ub: i64, a: i64, b: i64, criterion: F) -> i64
where
    F: Fn(i64, i64, i64) -> bool,
{
    let mut lb = lb;
    let mut ub = ub;
    while ub - lb > 1 {
        let mid = (ub + lb) / 2;
        if criterion(mid, a, b) {
            lb = mid;
        } else {
            ub = mid;
        }
    }
    lb
}

fn ternary_search(lb: i64, ub: i64, a: i64, b: i64) -> i64 {
    let mut lb = lb;
    let mut ub = ub;
    while ub - lb > 2 {
        let mid1 = (ub + lb * 2) / 3;
        let mid2 = (ub * 2 + lb) / 3;
        if eval(a, b, mid1) < eval(a, b, mid2) {
            lb = mid1;
        } else {
            ub = mid2;
        }
    }
    max(
        max(eval(a, b, lb), eval(a, b, (lb + ub) / 2)),
        eval(a, b, ub),
    )
}