fun sqrtFloor(x: Long): Long {
  var l = 0L
  var u = 2000000000L
  while (Math.abs(u - l) >= 2) {
    val m = (l + u) / 2
    if (m * m <= x) {
      l = m
    } else {
      u = m
    }
  }
  return l
}

fun main(args: Array<String>) {
  val q = readLine()!!.toInt()
  repeat(q) {
    // a <= b
    val (a, b) = readLine()!!.split(" ").map(String::toLong).sorted()
    if (a == 1L && b == 1L) {
      println(0)
      return@repeat
    }
    val product = a * b - 1
    val r = sqrtFloor(product)
    var ans = 2 * r
    // Don't double count (r, r)
    if (r == product / r) {
      ans -= 1
    }
    val aSet = mutableSetOf<Long>()
    if (a <= r) {
      aSet.add(a)
    }

    val a1 = product / b
    if (a1 > 0) {
      val b1 = product / a1
      if (b1 == b) {
        val a2 = a1 + 1
        val b2 = product / a2
        if (b1 - 1 == b2) {
          aSet.add(a1)
        }
      }
    }

    // println(aSet.size)
    ans -= aSet.size

    println(ans)
  }
}