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 (Python):
def main():
    N = int(input())
    X = [list(map(int,input().split())) for _ in range(N)]
    def num(i,j):
        r = int((i*j)**(0.5))
        if r**2 == i*j:
            if i == j:
                print((r-1)*2)
            else:
                print((r-1)*2-1)
        else:
            if r*(r+1) >= i*j:
                print((r-1)*2)
            else:
                print(r*2-1)

    for i in range(N):
        num(X[i][0],X[i][1])

if __name__ == "__main__":
    main()

Solution (Python):
a,*t=map(int,open(0).read().split())
for a,b in zip(*[iter(t)]*2):
  c=a*b
  d=int(c**.5)
  print(2*d-1-(d*(d+1)>=c)-(c==d*d and a!=b))

Solution (Python):
a,*t=map(int,open(0).read().split())
for a,b in zip(*[iter(t)]*2):
  c=a*b
  d=int(c**.5)
  print(2*d-1-(d*(d+1)>=c)-(c==d*d and a!=b))

Solution (Python):
AB=[list(map(int,input().split())) for i in range(int(input()))]
for a,b in AB:
    if a<b:a,b=b,a
    if a==b:
        print(2*a-2)
        continue
    c=int((a*b)**0.5)
    if a*b>c**2:
        if c*(c+1)<a*b:print(c*2-1)
        else:print(c*2-2)
    elif a*b==c**2:print((c-1)*2-1)

Solution (Python):
AB=[list(map(int,input().split())) for i in range(int(input()))]

for a,b in AB:
    if a<b:a,b=b,a
    if a==b:
        print(2*a-2)
        continue
    c=int((a*b)**0.5)
    if a*b>c**2:
        if c*(c+1)<a*b:
            print(c*2-1)
        else:
            print(c*2-2)
    elif a*b==c**2:
        print((c-1)*2-1)

Solution (Python):

import math
Q = int(input())
ans = []
for i in range(Q):
    A,B = list(map(int,input().split()))
    C = math.floor(math.sqrt(A*B)-1e-7)
    if A <= B:
        pass
    else:
        t = A
        A = B
        B = t


    if A == B:
        ans.append(2*A-2)


    elif (A+1) == B:
        ans.append(2*A-2)



    elif C*(C+1) >= A*B:
        ans.append(2*C-2)


    else:
        ans.append(2*C-1)

for a in ans:
    print(a)

Solution (Python):
import math

Q = int(input())

AB = []
for _ in range(Q) :
    a, b = map(int, input().split())
    if a > b :
        a, b = b, a
    AB.append((a, b))

for a, b in AB :
    if a == b :
        print(2 * a - 2)
        continue

    C = int(math.sqrt(a * b))
    if C * C == a * b :
        C -= 1

    if C * (C + 1) >= a * b :
        print(2 * C - 2)
    else :
        print(2 * C - 1)

Solution (Python):
import sys,collections as cl,bisect as bs,math
sys.setrecursionlimit(100000)
Max = sys.maxsize
def l():
    return list(map(int,input().split()))
def m():
    return map(int,input().split())
def onem():
    return int(input())
def s(x):
    a = []
    aa = x[0]
    su = 1
    for i in range(len(x)-1):
        if aa == x[i+1]:
            a.append([aa,su])
            aa = x[i+1]
            su = 1
        else:
            su += 1
    a.append([aa,su])
    return a
def jo(x):
    return " ".join(map(str,x))

q = onem()
AAAA = [sorted(l()) for i in range(q)]

for a,b in AAAA:
    x = math.sqrt(a*b)
    y = int(x)
    if x == y:
        if a == b:
            print(y*2 -2)
        else:
            print(y*2 -3)
    else:
        if y*(y+1)< a*b:
            print(y*2-1)
        else:
            print(y*2-2)

Solution (Python):








Q = int(input())
AB = [[int(x) for x in input().split()] for _ in range(Q)]

for a,b in AB:
  if a > b:
    a,b = b,a
  answer = 0
  x = int((a*b)**0.5)-2
  while (x+1)*(x+1) < a*b:
    x += 1
  y = x
  answer = 2*x-1
  if a <= x:
    answer -= 1
  if x*(x+1) < a*b:
    answer += 1
  print(answer)

Solution (Python):
import math
Q = int(input())

for _ in range(Q):
    A, B = map(int, input().split())
    score = A*B 
    if A > B:
        A, B = B, A 
    if A == B:
        print  (2 * A -2)
    elif A+1 == B:
        print (2 * A - 2)
    else:
        c = int(math.sqrt(A*B))
        if c ** 2 == A * B:
            c -= 1
        if c * (c+1) >= A * B:
            print (2 * c -2)
        else:
            print (2 * c -1)

Solution (Python):


from sys import stdout
from bisect import bisect_left as binl
from copy import copy, deepcopy
from collections import defaultdict


mod = 1


def intin():
    input_tuple = input().split()
    if len(input_tuple) <= 1:
        return int(input_tuple[0])
    return tuple(map(int, input_tuple))


def intina():
    return [int(i) for i in input().split()]


def intinl(count):
    return [intin() for _ in range(count)]


def modadd(x, y):
    global mod
    return (x + y) % mod


def modmlt(x, y):
    global mod
    return (x * y) % mod


def lcm(x, y):
    while y != 0:
        z = x % y
        x = y
        y = z
    return x


def combination(x, y):
    assert(x >= y)

    if y > x // 2:
        y = x - y

    ret = 1

    for i in range(0, y):
        j = x - i
        i = i + 1

        ret = ret * j
        ret = ret // i

    return ret


def get_divisors(x):
    retlist = []
    for i in range(1, int(x**0.5) + 3):
        if x % i == 0:
            retlist.append(i)
            retlist.append(x // i)
    return retlist


def get_factors(x):
    retlist = []
    for i in range(2, int(x**0.5) + 3):
        while x % i == 0:
            retlist.append(i)
            x = x // i
    retlist.append(x)
    return retlist


def make_linklist(xylist):
    linklist = {}
    for a, b in xylist:
        linklist.setdefault(a, [])
        linklist.setdefault(b, [])
        linklist[a].append(b)
        linklist[b].append(a)
    return linklist


def calc_longest_distance(linklist, v=1):
    distance_list = {}
    distance_count = 0
    distance = 0
    vlist_previous = []
    vlist = [v]
    nodecount = len(linklist)

    while distance_count < nodecount:
        vlist_next = []
        for v in vlist:
            distance_list[v] = distance
            distance_count += 1
            vlist_next.extend(linklist[v])
        distance += 1
        vlist_to_del = vlist_previous
        vlist_previous = vlist
        vlist = list(set(vlist_next) - set(vlist_to_del))

    max_distance = -1
    max_v = None
    for v, distance in distance_list.items():
        if distance > max_distance:
            max_distance = distance
            max_v = v

    return (max_distance, max_v)


def calc_tree_diameter(linklist, v=1):
    _, u = calc_longest_distance(linklist, v)
    distance, _ = calc_longest_distance(linklist, u)
    return distance


class UnionFind:

    def __init__(self, n):
        self.parent = [i for i in range(n)]

    def root(self, i):
        if self.parent[i] == i:
            return i
        self.parent[i] = self.root(self.parent[i])
        return self.parent[i]

    def unite(self, i, j):
        rooti = self.root(i)
        rootj = self.root(j)
        if rooti == rootj:
            return
        if rooti < rootj:
            self.parent[rootj] = rooti
        else:
            self.parent[rooti] = rootj

    def same(self, i, j):
        return self.root(i) == self.root(j)





def f(selected_p, selected_ceil, p, ceil, score):
    if p in selected_p:
        return (None, None)
    q = ceil
    while True:
        q += 1
        if q in selected_ceil:
            continue
        if p * q >= score:
            return (p, None)
        return (p, q)


def main():
    q = intin()
    ablist = intinl(q)

    for a, b in ablist:
        score = a * b
        ceil = int(score ** 0.5)
        if ceil ** 2 == score:
            ceil -= 1

        selected_x = [a]
        selected_y = [b]
        keep_x = []
        keep_y = []
        ans = 0

        for i in range(ceil, max(0, ceil - 100), -1):
            x, y = f(selected_x, selected_y, i, ceil, score)
            if x is not None:
                if y is None:
                    keep_x.append(x)
                else:
                    selected_x.append(x)
                    selected_y.append(y)
                    ans += 1

            y, x = f(selected_y, selected_x, i, ceil, score)
            if y is not None:
                if x is None:
                    keep_y.append(y)
                else:
                    selected_x.append(x)
                    selected_y.append(y)
                    ans += 1

        ans += min(len(keep_x), len(keep_y))

        ans += 2 * max(0, ceil - 100)
        if a <= ceil - 100:
            ans -= 1
        if b <= ceil - 100:
            ans -= 1

        print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
Q = int(input())
AB = [sorted([int(_) for _ in input().split()]) for _ in range(Q)]

for a, b in AB:
    s = a * b
    r = int(s**0.5)

    def calc(x):
        if x == 0:
            return
        elif s % x:
            return s // x
        else:
            return s // x - 1

    ans = 2 * r - 1
    if a == b or calc(r) == r:
        ans -= 1
    elif calc(r) < r:
        ans -= 2
    print(ans)

Solution (Python):
import math
for A,B in[map(int,input().split())for _ in range(int(input()))]:
 R=int(math.sqrt(A*B))
 N=not R*(R+1)<A*B
 print(2*(R-N)-(not ((N and R*R<A*B) or A==B)))

Solution (Python):
import math
for A,B in [map(int,input().split())for _ in range(int(input()))]:
 rt=int(math.sqrt(A*B))
 nc=not rt*(rt+1) < A*B
 print(2*(rt-nc) - (not ((nc and rt*rt < A*B) or A==B)))

Solution (Python):
import math
for A,B in [map(int,input().split())for _ in range(int(input()))]:
    rt=int(math.sqrt(A*B))
    nc = not rt*(rt+1) < A*B
    print(2*(rt-nc) - (not ((nc and rt*rt < A*B) or A==B)))

Solution (Python):
from math import sqrt
N=int(input())
for A,B in [map(int,input().split())for _ in range(N)]:
    rt=int(sqrt(A*B))
    crossable = rt*(rt+1) < A*B
    print(2*(rt-1+crossable)-1 + ((not crossable and rt*rt < A*B) or A==B))

Solution (Python):
import math

import sys

sys.setrecursionlimit(10000)
INF = float('inf')

Q = int(input())
A, B = zip(*[list(map(int, input().split())) for _ in range(Q)])


def solve(a, b):
    a, b = min(a, b), max(a, b)
    assert a <= b

    if a == b:
        
        
        
        return (a - 1) * 2
    if a == b - 1:
        
        
        
        
        return (a - 1) * 2

    
    
    
    c = int(math.sqrt(a * b))
    if c ** 2 == a * b:
        c -= 1

    
    ret = a - 1
    if c * (c + 1) < a * b:
        
        
        ret += c - a
        
        ret += c
    else:
        
        
        ret += c - a
        
        ret += c - 1
    return ret


for a, b in zip(A, B):
    print(solve(a, b))

Solution (Python):
import math

import sys

sys.setrecursionlimit(10000)
INF = float('inf')

Q = int(input())
A, B = zip(*[list(map(int, input().split())) for _ in range(Q)])


def solve2(a, b):
    a, b = min(a, b), max(a, b)
    assert a <= b

    if a == b:
        
        
        
        return (a - 1) * 2
    if a == b - 1:
        
        
        
        
        return (a - 1) * 2

    
    ret = a - 1
    c = int(math.sqrt(a * b))
    if c ** 2 == a * b:
        c -= 1
    if c * (c + 1) < a * b:
        
        
        ret += c - a
        
        ret += c
    else:
        
        
        ret += c - a
        
        ret += c - 1
    return ret


for a, b in zip(A, B):
    print(solve2(a, b))

Solution (Python):
import math
def main():
	Q = int(input())
	ans = [0]*Q
	for i in range(Q):
		A, B = sorted(map(int, input().split()))
		if A == B or A+1 == B:
			ans[i] = 2*A-2
			continue

		C = int(math.sqrt(A*B))
		if C*C == A*B:
			C -= 1
		if C*(C+1) >= A*B:
			ans[i] = 2*C-2
		else:
			ans[i] = 2*C-1

	for a in ans:
		print(a)


if __name__ == '__main__':
    main()

Solution (Python):
import math

q = int(input())
for _ in range(q):
    a, b = sorted(map(int, input().split()))
    if a == b:
        print(2 * a - 2)
    elif a + 1 == b:
        print(2 * a - 2)
    else:
        c = math.ceil(math.sqrt(a * b)) - 1

        if c * (c + 1) >= a * b:
            print(2 * c - 2)
        else:
            print(2 * c - 1)

Solution (Python):
Q = int(input())
Ans = ""
for i in range(Q):
    A, B = map(int, input().split())
    A, B = min(A, B), max(A, B)
    if A == B or A + 1 == B: Ans += str(2 * (A - 1)) + "\n"
    else:
        limit = int(pow(A * B, 0.5))
        if limit ** 2 == A * B: limit -= 1
        if limit * (limit + 1) >= A * B: Ans += str(2 * (limit - 1)) + "\n"
        else:Ans += str(2 * limit - 1) + "\n"

print(Ans)

Solution (Python):


import sys

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

def calc(x):
    lim=a*b
    mx=ceil(x, 2)*(x//2+1)
    return mx<lim

Q=INT()
for _ in range(Q):
    a,b=MAP()

    
    if a==b:
        print(a*2-2)
        continue

    
    low=1
    hi=10**18
    while low+1<hi:
        mid=(hi+low)//2
        if calc(mid):
            low=mid
        else:
            hi=mid
    print(low-1)

Solution (Python):


import sys

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

def calc(x):
    lim=a*b
    mx=ceil(x, 2)*(x//2+1)
    return mx>=lim

Q=INT()
for _ in range(Q):
    a,b=MAP()

    
    if a==b:
        print(a*2-2)
        continue

    
    low=1
    hi=10**18
    while low+1<hi:
        mid=(hi+low)//2
        if calc(mid):
            hi=mid
        else:
            low=mid
    print(low-1)

Solution (Python):


import sys

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

def calc(x):
    lim=a*b-1
    mx=(x+1)//2*(x+1-(x+1)//2)
    return mx>lim

Q=INT()
for _ in range(Q):
    a,b=MAP()

    
    if a==b:
        print((a-1)*2)
        continue

    
    low=1
    hi=10**18
    while low+1<hi:
        mid=(hi+low)//2
        if calc(mid):
            hi=mid
        else:
            low=mid
    print(low-1)

Solution (Python):

import sys
import math
Q = int(sys.stdin.readline())
A = [0]*Q
B = [0]*Q
for i in range(Q):
    A[i], B[i] = list(map(int, sys.stdin.readline().split()))


def f(a, b, d):
    """
    a, a+1, ..., b と d, d-1, ..., d-(b-a)
    積から、最大のものを返す
    """
    kosu = b-a+1
    wa = d+a
    if wa//2 < a:
        return a*d

    elif wa//2 < b:
        return b*(wa//2-b)

    else:
        return b*d-(b-a)


for i in range(Q):
    if A[i] == B[i]:
        print(2*(A[i]-1))
        continue
    a = min(A[i], B[i])
    b = max(A[i], B[i])
    
    c = math.ceil(math.sqrt(a*b))-1
    
    if c*(c+1) < a*b:
        print(2*c-1)
    else:
        print(2*c-2)

Solution (Python):
Q = int(input())
AB = [list(map(int, input().split())) for _ in range(Q)]

for a,b in AB:
  if a==b:
    print(2*(a-1))
  else:
    a, b = min(a,b), max(a,b)
    sq = int((a*b)**0.5)
    print(a-1 + sq - a + (a*b-1)//sq -1)

Solution (Python):
import math

n = int(input())
lst = [list(map(int, input().split())) for _ in range(n)]

def max_multi(a,b,d,c):
        length = b-a
        n = math.ceil((d-a)/2)
        if n < 0:
            n = 0
        elif n > length:
            n = length
        x = (a+n)*(d-n)
        n = math.floor((d-a)/2)
        if n < 0:
            n = 0
        elif n > length:
            n = length
        y = (a+n)*(d-n)
        return max(x,y)

def check(a, b, n):
    multi = a*b
    if n<a:
        return True
    else:
        if a<= n < b:
            second_1 = (n,1)
            second_2 = (0,0)
        else:
            second_1 = (b-1,1)
            second_2 = (n+1, b+1)
        m = (second_2[0] - second_2[1])+1
        if second_2 == (0,0):
            a1,b1,c1,d1 = 1, a-1, second_1[0], second_1[0]-(a-2)
            a2,b2,c2,d2 = a+1, n+1, second_1[0]-(a-1), second_1[1]
            s = max(max_multi(a1,b1,c1,d1), max_multi(a2,b2,c2,d2))
        elif m == a-1:
            a1,b1,c1,d1 = 1, a-1, second_2[0], second_2[1]
            a2,b2,c2,d2 = a+1, n+1, second_1[0], second_1[1]
            s = max(max_multi(a1,b1,c1,d1), max_multi(a2,b2,c2,d2))
        elif m > a-1:
            a1,b1,c1,d1 = 1, a-1, second_2[0], second_2[0]-(a-2)
            a2,b2,c2,d2 = a+1, m+1, second_2[0]-(a-1), second_2[1]
            a3,b3,c3,d3 = m+2, n+1, second_1[0], second_1[1]
            s = max(max_multi(a1,b1,c1,d1), max_multi(a2,b2,c2,d2), max_multi(a3,b3,c3,d3))
        else:
            a1,b1,c1,d1 = 1,m,second_2[0], second_2[1]
            a2,b2,c2,d2 = m+1, a-1, second_1[0], second_1[0]-(a-m-2)
            a3,b3,c3,d3 = a+1, n+1, second_1[0]-(a-m-1), second_1[1]
            s = max(max_multi(a1,b1,c1,d1), max_multi(a2,b2,c2,d2), max_multi(a3,b3,c3,d3))
        return True if s < multi else False

def search(a,b):
    a,b = min(a,b), max(a,b)
    low = 0
    high = 10**30
    while high - low > 1:
        if check(a,b,(low+high)//2):
            low = (low+high)//2
        else:
            high = (low+high)//2
    return high if check(a,b,high) else low

for i in lst:
    print(search(i[0], i[1]))

Solution (Python):
import math

n = int(input())
lst = [list(map(int, input().split())) for _ in range(n)]

def max_multi(a,b,d,c):
        length = b-a
        n = math.ceil((d-a)/2)
        if n < 0:
            n = 0
        elif n > length:
            n = length
        x = (a+n)*(d-n)
        n = math.floor((d-a)/2)
        if n < 0:
            n = 0
        elif n > length:
            n = length
        y = (a+n)*(d-n)
        return max(x,y)

def check(a, b, n):
    multi = a*b
    if n<a:
        return True
    else:
        if a<= n < b:
            
            second_1 = (n,1)
            second_2 = (0,0)
        else:
            
            second_1 = (b-1,1)
            
            second_2 = (n+1, b+1)
        m = (second_2[0] - second_2[1])+1
        
        if second_2 == (0,0):
            a1,b1,c1,d1 = 1, a-1, second_1[0], second_1[0]-(a-2)
            a2,b2,c2,d2 = a+1, n+1, second_1[0]-(a-1), second_1[1]
            
            
            s = max(max_multi(a1,b1,c1,d1), max_multi(a2,b2,c2,d2))
        elif m == a-1:
            a1,b1,c1,d1 = 1, a-1, second_2[0], second_2[1]
            a2,b2,c2,d2 = a+1, n+1, second_1[0], second_1[1]
            
            
            
            s = max(max_multi(a1,b1,c1,d1), max_multi(a2,b2,c2,d2))
        elif m > a-1:
            a1,b1,c1,d1 = 1, a-1, second_2[0], second_2[0]-(a-2)
            a2,b2,c2,d2 = a+1, m+1, second_2[0]-(a-1), second_2[1]
            a3,b3,c3,d3 = m+2, n+1, second_1[0], second_1[1]
            
            
            
            s = max(max_multi(a1,b1,c1,d1), max_multi(a2,b2,c2,d2), max_multi(a3,b3,c3,d3))
        else:
            a1,b1,c1,d1 = 1,m,second_2[0], second_2[1]
            a2,b2,c2,d2 = m+1, a-1, second_1[0], second_1[0]-(a-m-2)
            a3,b3,c3,d3 = a+1, n+1, second_1[0]-(a-m-1), second_1[1]
            
            
            
            s = max(max_multi(a1,b1,c1,d1), max_multi(a2,b2,c2,d2), max_multi(a3,b3,c3,d3))
        return True if s < multi else False

def search(a,b):
    a,b = min(a,b), max(a,b)
    low = 0
    high = 10**30
    while high - low > 1:
        if check(a,b,(low+high)//2):
            low = (low+high)//2
            
        else:
            high = (low+high)//2
            
    return high if check(a,b,high) else low

for i in lst:
    print(search(i[0], i[1]))

Solution (Python):
q = int(input())
a = [list(map(int,input().split())) for _ in range(q)]
import math
def f(a,b):
    k = int(math.sqrt(a*b))
    if k>=math.sqrt(a*b):
        k-=1
    if a==b:
        k = 2*min(a,b)-2
    elif min(a,b)+1==max(a,b):
        k = 2*min(a,b)-2
    else:
        if k*(k+1)>=a*b:
            k = 2*k-2
        else:
            k = 2*k-1
    return k
for a_ in a:
    print(f(a_[0],a_[1]))

Solution (Python):
import math

q = int(input())
a = [0] * q
b = [0] * q

for i in range(0, q):
    a[i], b[i] = [int(i) for i in input().split(" ")]
    
def sqrt_int(n):
    s = math.floor(n**(1/2)) - 1
    while s * s <= n:
        s += 1
    return s - 1

for i in range(0, q):
    mi = min(a[i], b[i])
    mx = max(a[i], b[i])
    p = mi * mx - 1
    s = math.floor(p**(1/2))
    c = 2 * (s - 1)
    if s == 0:
        print(0)
        continue
    if mi <= s - 1:
        c -= 1
    r = p // s
    if r > s:
        if s == mi:
            c += 1
        else:
            c += 2
    elif r == s and s != mi:
        c += 1
    print(c)

Solution (Python):
import math
Q = int(input())
AB = [sorted(list(map(int,input().split()))) for i in range(Q)]

for A,B in AB:
    x = math.sqrt(A*B)
    y = int(x)
    if x!=y:
        if y*(y+1)<A*B:
            print(y*2-1)
        else:
            print(y*2-2)
    else:
        if A==B:
            print(y*2-2)
        else:
            print(y*2-3)

Solution (Python):
import math

n = int(input())
for _ in range(n):
  a,b = input().split(" ")
  a,b = sorted([int(a), int(b)])
  c = math.ceil((a*b)**0.5)-1
  print(2*c-(a!=b)-(c*(c+1)>=a*b))

Solution (Python):
from math import sqrt


def solve(string):
    q, *ab = map(int, string.split())
    ans = []
    for a, b in zip(ab[::2], ab[1::2]):
        c = a * b
        d = int(sqrt(c))
        answer = 2 * d - 2
        if d * d == c:
            answer -= 1
        if d * (d + 1) < c:
            answer += 1
        if a == b:
            answer += 1
        ans.append(str(answer))
    return "\n".join(ans)


if __name__ == '__main__':
    n = int(input())
    print(solve('{}\n'.format(n) + '\n'.join([input() for _ in range(n)])))

Solution (Python):
q = int(input())

def f(A,B,x):
    return x*B//(x+A) + 1
def F(A,B):
    st = 1
    ed = B-A-1
    gap = B-A
    maxst = 1
    while st<ed:
        bar = (st+ed)//2
        if f(A,B,bar) + bar <=gap:
            maxst = max(maxst,bar)
            st = bar + 1
        else:
            ed = bar - 1
    if f(A,B,st) + st<=gap:
        maxst = max(maxst,st)
    if f(A,B,maxst)+maxst > gap:
        return -1
    return maxst  
def solveAB(A,B):
    res = 0
    res += (A-1)*2
    st = F(A,B)
    
    if st == -1:
        if B-A <2:
            return res
        else:
            return res+1
    if f(A,B,st)+st == B-A and (st-1)*2+1 == B-A-1:
        
        res += (st-1)*2+1
    elif f(A,B,st)+st < B-A and st*2 == B-A-1:
        
        res += st*2
    elif f(A,B,st)+st < B-A and st*2 < B-A-1: 
        
        res += st*2 + 1
    elif f(A,B,st)+st == B-A and (st-1)*2+1 <B-A-1:
        
        res += (st-1)*2+1+1
    return res 
     

for _ in range(q):
    a,b = map(int,input().split())
    A = min(a,b)
    B = max(a,b)
    print(solveAB(A,B))

Solution (Python):

from collections import defaultdict
from collections import deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LS():return list(map(list, sys.stdin.readline().split()))
def S(): return list(sys.stdin.readline())[:-1]
def IR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = I()
    return l
def LIR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = LI()
    return l
def SR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = S()
    return l
def LSR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = SR()
    return l
mod = 1000000007
sys.setrecursionlimit(1000000)


def A():
    s = S()
    a = s.count("a")
    b = s.count("b")
    c = 3-a-b
    if a&b&c:
        print("Yes")
    else:
        print("No")

def B():
    a,b,k = LI()
    ans = []
    for i in range(a,min(a+k,b+1)):
        ans.append(i)
    for i in range(max(a,b-k+1),b+1):
        ans.append(i)
    ans = list(set(ans))
    ans.sort()
    for i in ans:
        print(i)


def C():
    a,b,c = LI()
    ans = 0
    if a%2:
        if b%2:
            if not c%2:
                ans += 1
                a += 1
                b += 1
        else:
            ans += 1
            if not c%2:
                b += 1
                c += 1
            else:
                a += 1
                c += 1
    else:
        if b%2:
            ans += 1
            if not c%2:
                a += 1
                c += 1
            else:
                b += 1
                c += 1
        else:
            if c%2:
                ans += 1
                a += 1
                b += 1
    ans += (3*max(a,b,c)-a-b-c)//2
    print(ans)

def D():
    q = I()
    for i in range(q):
        a,b = LI()
        x = int((a*b)**0.5)
        y = math.ceil((a*b)/x)-1
        ans = x+y-2
        if a==b:
            ans += 1
        print(ans)


def E():
    s = S()
    f = {"N":0,"W":0,"S":0,"E":0}
    for i in s:
        f[i] = 1
    if f["N"]^f["S"]:
        print("No")
    elif f["W"]^f["E"]:
        print("No")
    else:
        print("Yes")

def F():
    return


def G():
    return


def H():
    return


if __name__ == "__main__":
    D()

Solution (Python):
Q = int(input())
Ri = [list(map(int, input().split())) for _ in range(Q)]

for r in Ri:
    a = r[0]
    b = r[1]
    if a > b:
        t = b
        b = a
        a = t
    max_score = a * b

    if a == b:
        print(2 * a - 2)
    elif a + 1 == b:
        print(2 * a - 2)
    else:
        r = max_score ** 0.5
        rn = int(r)
        if rn == r:
            rn -= 1
        if rn * (rn + 1) >= max_score:
            print(2 * rn - 2)
        else:
            print(2 * rn - 1)

Solution (Python):
Q = int(input())

for i in range(Q):
    A, B = map(int, input().split())
    A, B = min(A, B), max(A, B)

    if A == B:
        print((A - 1) + (B - 1))
        continue

    ok, ng = 0, A + B - 1
    while abs(ok - ng) > 1:
        K = (ok + ng) // 2
        m = (K + 2) // 2

        if m * (K + 2 - m) < A * B:
            ok = K
        else:
            ng = K

    print(ok)

Solution (Python):
def upper_square(A: int, B: int) -> int:
    """C**2 < A*B を満たす最大の C を求めます。
    """
    l, r = min(A, B), max(A, B)
    while r - l > 1:
        m = (l + r) // 2
        if m * m < A * B:
            l = m
        else:
            r = m
    return l


def query(A: int, B: int) -> int:
    A, B = min(A, B), max(A, B)
    if A == B:
        
        
        return 2 * A - 2
    if B == A + 1:
        
        
        return 2 * A - 2

    
    
    
    C = upper_square(A, B)

    if A * B <= C * (C + 1):
        
        
        return 2 * C - 2

    
    
    return 2*C - 1


def worst_case(Q: int, queries: list) -> list:
    return [query(A, B) for A, B in queries]


if __name__ == "__main__":
    Q = int(input())
    queries = [tuple(map(int, input().split())) for _ in range(Q)]
    ans = worst_case(Q, queries)
    for a in ans:
        print(a)

Solution (Python):
q = int(input())
quest = [tuple(map(int, input().split())) for _ in range(q)]
for a, b in quest:
    m = a*b
    l = int((m - 1) ** (1/2))
    
    if l * l >= m:
        l -= 1
    elif (l+1) * (l+1) < m:
        l += 1
    if a == b:
        print((a-1) * 2)
    elif (l+1) * l < m:
        print(l*2 - 1)
    else:
        print(l*2 - 2)

Solution (Python):


q=int(input())
A=[]
for i in range(q):
    A.append(list(map(int,input().split())))
    A[i].sort()








for i in range(q):
    a=A[i][0]
    b=A[i][1]
    if a==b :
        ans1=2*(a-1)
    elif a+1==b :
        ans1=2*a-2
    else :
        c=int((a*b-1)**0.5)+2
        while c*c>=a*b :
            c-=1
        
        if c*(c+1)>=a*b :
            ans1=2*c-2
        else :
            ans1=2*c-1

    print(ans1)

Solution (Python):
import math

q = int(input())
for i in range(q):
    a,b = map(int,input().split())
    x = min(a,b)
    y = max(a,b)
    
    if x <= y <= x+1:
        print((x-1)*2)
    else:
        xy = math.ceil((x*y)**0.5)
        l = xy
        r = xy
        if l*r < x*y:
            print((r-1)*2)
            continue
        r -= 1
        if l*r < x*y:
            print(r*2-1)
            continue
            
        print((r-1)*2)

Solution (Python):

q = int(input())

a = [0] * q
b = [0] * q
for i in range(q):
    a[i], b[i] = map(int, input().split())

for i in range(q):
    k=min(a[i],b[i])
    score=a[i]*b[i]
    sqrt=int(score**0.5)
    if abs(a[i]-b[i])<=1:
        ans=(k-1)*2
    elif sqrt**2==score:
        ans=sqrt*2-3
    elif sqrt*(sqrt+1)>=score:
        ans=(sqrt-1)*2
    else:
        ans=sqrt*2-1
    print(ans)

Solution (Python):

Q = int(input())
qs = [ tuple(map(int, input().split())) for _ in range(Q)]
for i in range(Q):
    
    A,B = qs[i]
    A,B = min(A,B), max(A,B)
    if A == B:
        C = A
        print(2*(C-1))
    elif B == A+1:
        C = A
        print(C-1 + C-1)

    else:
        C = int( (A*B)**(1/2))
        if C**2 == A*B:
            C=C-1

        if C*(C+1) >= A*B:
            print(2*C-2)
        else:
            print(2*C-1)



        continue
        C = A
        n = B - C
        ans = C-1
        

        MIN = 1
        MAX = B-1 +1 
        middle = (MIN+MAX)//2

        
        i = middle
        

        
        while MAX - MIN > 1: 
            if (A*B-1) // i != (A*B-1)//(i+1) and (A*B-1)//i >= A+1:
                MIN = middle
                i = middle = (MIN+MAX)//2
            else:
                MAX = middle
                i = middle = (MIN+MAX)//2
                

        print("min: {}, max: {}".format(MIN, MAX))
        print(ans + i)