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)

use std::io;
//use std::collections::HashMap;
//use std::collections::BinaryHeap;
//use std::collections::HashSet;
//use std::mem; // mem::swap(&mut x,&mut y);
use std::cmp; // cmp::max,cmp::min
// Rust memo: https://hackmd.io/i0lQY1OKTDW1t66TECOmiw?both

#[allow(dead_code)]
fn read_line() -> String{
    let mut s = String::new();
    io::stdin().read_line(&mut s).unwrap();
    s.trim().to_string()
}

#[allow(dead_code)]
fn read_charvec() -> Vec<char>{
    read_line().chars().collect()
}

#[allow(dead_code)]
fn read_ints() -> Vec<i64>{
    let s = read_line();
    let split:Vec<&str> = s.split(" ").collect();
    split.iter().map(|&x| x.to_string().parse().unwrap()).collect()
}

#[allow(dead_code)]
fn read_int() -> i64{
    read_ints()[0]
}

#[allow(dead_code)]
fn reverse(s: &String) -> String{
    s.chars().rev().collect::<String>()
}

#[allow(dead_code)]
fn read_lines(n:i64) -> Vec<String>{
    let mut xs = Vec::new();

    for _i in 0..n{
        xs.push(read_line());
    }

    xs
}

#[allow(dead_code)]
fn read_ints2(n:i64) -> Vec<i64>{
    read_lines(n).iter().map(|x| x.parse().unwrap()).collect()
}

#[allow(dead_code)]
fn arange(start:i64,end:i64,step:i64) -> Vec<i64>{
    let mut ret = Vec::new();
    let mut i = start;

    while i < end{
        ret.push(i);
        i += step;
    }

    ret
}

////////////////////////////////////////////////

fn check_all(a:&i64,b:&i64,x:&i64) -> bool{
    let c = a * b;

    // println!("----------{}",x);
    for i in 0..*x{
        let aa = if i+1 < *a { i+1 } else { i + 2 };
        let bb = if x-*b > i {*x-i} else {*x-i-1};

        // println!("check ({},{}):{}",aa,bb,aa*bb);
        if aa * bb >= c {
            return false;
        }
    }

    true
}


fn check_middle(a:&i64,b:&i64,x:&i64) -> bool{
    
    let c = a * b;
    let x0 = ((a*b) as f64).sqrt() as i64 - 10;
    // println!("----------{}",x);
    for i in x0..x0+20{
        let aa = if i+1 < *a { i+1 } else { i + 2 };
        let bb = if x-*b > i {*x-i} else {*x-i-1};

        // println!("check ({},{}):{}",aa,bb,aa*bb);
        if aa * bb >= c {
            return false;
        }
    }

    true
}

fn solve(a:i64,b:i64) -> i64{
    if a == 1 && b == 1 {
        return 0;
    }

    let mut xmax = cmp::max(2,(((a*b) as f64).sqrt() * 2.0 + 20.0) as i64);

    if xmax < 50 {
        while ! check_all(&a,&b,&xmax){
            xmax = xmax - 1;
        }
    }else {
        while ! check_middle(&a,&b,&xmax){
            xmax = xmax - 1;
        }
    }
    xmax-1
}

fn main(){
    let q = read_int();
    for _ in 0..q{
        let input =read_ints();
        let (a,b) = (input[0],input[1]);
        println!("{}",solve(a,b));
    }
}