let sqrt_int x = int_of_float (sqrt (float_of_int x))

let () =
  Scanf.scanf "%d" @@ fun q ->
  Array.init q begin fun _ -> Scanf.scanf " %d %d" @@ fun a b ->
    let a, b = min a b, max a b in
    let mul, hmul = a * b - 1, sqrt_int (a * b - 1) in
    let rec search l r =
      if l+1 >= r then l else
      let m = (l + r) / 2 in
      if mul / m > hmul then search m r else search l m
    in
    let c = search a (a*b+1) - a |> max 0 in
    (a-1) + min (b-1) (if hmul <> 0 && mul/hmul = a then hmul-1 else hmul) + c
  end |> Array.iter (Printf.printf "%d\n")
