let rec upper_bound l r p =
  if r <= 1 + l
  then l
  else let m = (l + r) / 2 in
       if p m 
       then upper_bound m r p
       else upper_bound l m p

let sqrt n = upper_bound 0 2147483648 (fun i -> i * i <= n)

let () = Scanf.scanf "%d\n" @@ fun n ->
  for i = 0 to n - 1 do
    Scanf.scanf "%d %d\n" @@ fun a b ->
      let n = sqrt (a * b - 1) in
      Printf.printf "%d\n" @@
        if n = 0 then 0
        else 
          2 * n
          - (if a <= n then 1 else 0)
          - (if b <= n then 1 else 0)
          - (if (a * b - 1) / n = n then 1 else 0)
  done
