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 check(a, b):
  if b < a:
    a, b = b, a
  if b - a <= 1:
    return 2 * a - 2
  d = a * b
  c, ng = a, b
  while ng - c > 1:
    mid = (ng + c) // 2
    if mid ** 2 < d:
      c = mid
    else:
      ng = mid
  return 2 * c - 1 - (c * (c + 1) >= a * b)

for _ in range(int(input())):
  print(check(*list(map(int, input().split()))))

Solution (Python):
from math import ceil, floor

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


def f(a, b, x):
    return (a + x - 1) * (b - x + 1)


def get_mx(a, b, num):
    tmp = (b - a + 2) / 2
    lx = max(1, floor(tmp))
    rx = min(num, ceil(tmp))
    mx = max(f(a, b, lx), f(a, b, rx))
    return mx


def check(a, b, num):
    mx = 1
    a, b = min(a, b), max(a, b)
    if num < a:
        mx = max(mx, get_mx(1, num, num))
    elif a <= num < b:
        mx = max(mx, get_mx(1, num, a - 1))
        mx = max(mx, get_mx(a + 1, num - a + 1, num - a + 1))
    elif num >= b:
        if num - b + 1 > a - 1:
            mx = max(mx, get_mx(1, num + 1, a - 1))
            mx = max(mx, get_mx(a + 1, num - a - 2, num - a - b + 2))
            mx = max(mx, get_mx(num - b - 2, b - 1, b - 1))
        elif num - b + 1 == a - 1:
            mx = max(mx, get_mx(1, num + 1, a - 1))
            mx = max(mx, get_mx(a + 1, b - 1, b - 1))
        else:
            mx = max(mx, get_mx(1, num + 1, num - b + 1))
            mx = max(mx, get_mx(num - b + 2, b - 1, a + b - num - 2))
            mx = max(mx, get_mx(a + 1, num - a + 1, num - a + 1))

    if mx < a * b:
        return True

    return False


for a, b in ab:
    if a == b:
        print(2 * a - 2)
        continue
    l = 0
    r = 10 ** 10
    while r - l > 1:
        m = (l + r) // 2
        if check(a, b, m):
            l = m
        else:
            r = m

    print(l)

Solution (Python):
q = int(input())
for _ in range(q):
    a, b = map(int, input().split())
    if a == b:
        print(2 * (a - 1))
        continue
    if a > b:
        a, b = b, a
    ans = a - 1

    l, r = -1, b - 1
    while r - l > 1:
        k = (l + r) // 2
        x = min(k, (a + 1 + k) // 2)
        y = (a + 1 + k) - x
        if x * y < a * b:
            l = k
        else:
            r = k
    ans += l
    print(ans)

Solution (Python):
import sys
import math
from collections import defaultdict
from bisect import bisect_left, bisect_right

sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]

mod = 10**9 + 7

def I(): return int(input())
def LI(): return list(map(int, input().split()))
def LIR(row,col):
    if row <= 0:
        return [[] for _ in range(col)]
    elif col == 1:
        return [I() for _ in range(row)]
    else:
        read_all = [LI() for _ in range(row)]
        return map(list, zip(*read_all))








Q = I()
A,B = LIR(Q,2)

for i in range(Q):
    a,b = min(A[i],B[i]), max(A[i],B[i])
    ab = a*b
    if b-a <= 1:
        print(2*(a-1))
    else:
        ok = 0
        ng = b-a-1
        while abs(ok-ng) > 1:
            mid = (ok+ng)//2
            if mid%2 == 0:
                max_val = (a+mid//2)*(a+mid//2+1)
            else:
                max_val = (a+(mid+1)//2)**2
            if max_val < ab:
                ok = mid
            else:
                ng = mid
        print(2*(a-1)+1+ok)

Solution (Python):
def can(a, b):
  a, b = min(b, a),max(a,b)
  if b - a <= 1:
    return 2*a-2
  c = int((a*b)**0.5)
  if c**2 == a*b:
    c -= 1
  if c*(c+1) >= a*b:
    return 2*c-2
  return 2*c-1
q = int(input())
for _ in range(q):
  a, b = map(int, input().split())
  print(can(a, b))

Solution (Python):
from math import sqrt

Q=int(input())
ans=[]
for i in range(0,Q):
    a,b=map(int,input().split())
    if a!=b:
        r=int(sqrt(a*b))
        if r*(r+1)<a*b:
            ans.append(2*r-1)
        else:
            if r**2==a*b:
                ans.append(2*r-3)
            else:
                ans.append(2*r-2)
    else:
        ans.append(2*a-2)


for i in range(0,Q):
    print(ans[i])

Solution (Python):
q=int(input())
def check(t,a,b):
    k=(2*a+t)//2
    return k*(2*a+t-k)<a*b

for i in range(q):
    a,b=sorted(map(int,input().split()))
    if a==b or a==b-1:
        print(2*a-2)
        continue
    l,r=1,b-a
    while l+1<r:
        t=(l+r)//2
        if check(t,a,b):
            l=t
        else:
            r=t
    if check(r,a,b):
        print(2*a-2+r)
    else:
        print(2*a-2+l)

Solution (Python):
q=int(input())
def check(t,a,b):
    k=(2*a+t)//2
    return k*(2*a+t-k)<a*b

for i in range(q):
    a,b=sorted(map(int,input().split()))
    if a==b or a==b-1:
        print(2*a-2)
        continue
    l,r=1,b-a
    while l+1<r:
        t=(l+r)//2
        if check(t,a,b):
            l=t
        else:
            r=t
    
    if check(r,a,b):
        print(2*a-2+r)
    elif check(l,a,b):
        print(2*a-2+l)
    else:
        print(2*a-1)

Solution (Python):
q=int(input())
for _ in range(q):
  a,b=map(int,input().split())
  if a>b:
    a,b=b,a
  if a==b:
    print(2*a-2)
  elif b==a+1:
    print(2*a-2)
  else:
    c=int((a*b)**0.5)-1
    while (c+1)*(c+1)<a*b:
      c+=1
    if c*(c+1)>=a*b:
      print(2*c-2)
    else:
      print(2*c-1)

Solution (Python):
import sys

import bisect








import math



readline = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)


def main():
    q = int(input())

    for i in range(q):
        a, b = list(map(int, readline().split()))

        if a > b:
            a, b = b, a

        if a == b or a + 1 == b:
            print(2 * a - 2)
        else:
            ab = a * b
            ok = 0
            ng = int(ab ** 0.5) + 1000000
            while abs(ng - ok) > 1:
                mid = (ok + ng) // 2
                if mid ** 2 < ab:
                    ok = mid
                else:
                    ng = mid
            c = ok

            if c * (c + 1) < ab:
                print(2 * c - 1)
            else:
                print(2 * c - 2)


if __name__ == '__main__':
    main()

Solution (Python):
def solve(A, B):
    from math import sqrt
    M = A * B
    cnt = 0

    x, y = A, B
    if y - x >= 1:
        z = int(sqrt(M))
        x = z
        y = M // x if M % x else M // x - 1
        cnt += z - A
    cnt += y - 1

    x, y = B, A
    if y - x >= 1:
        z = int(sqrt(M))
        x = z
        y = M // x if M % x else M // x - 1
        cnt += z - B
    cnt += y - 1

    return cnt


Q = int(input())
Query = [tuple(map(int, input().split())) for _ in range(Q)]

for A, B in Query:
    print(solve(A, B))

Solution (Python):
q=int(input())
for _ in range(q):
  a,b=map(int,input().split())
  if a>b:
    a,b=b,a
  if a==b:
    print(2*a-2)
  elif a==b+1:
    print(2*a-1)
  else:
    c=int((a*b)**0.5)
    if c*c==a*b:
      c-=1
    if c*(c+1)>=a*b:
      print(2*c-2)
    else:
      print(2*c-1)

Solution (Python):

"""

(N-1)(N+1) = N^2 - 1
(N-2)(N+2) = N^2 - 4

A,Bが等しいときは、上の式に基づいて2*(A-1)人小さい人を作れる

A,Bが等しくないときは、同じように作るとすると

(A-x)*(B+x) = AB + (A-B)x - x^2
なので、(A-B)x - x^2 < 0 → (A-B) < x
よって、A < B ならば、A-1…1まですべて使用可能。
A>Bならば、 A- (A-B+1) … 1まで使用可能

どちらか片方が高橋君より小さいことは必須→A,B中片方は範囲外の数字で全て無駄なく使ってしまう
残りは使える個数に制限があるけど、できるだけ使ってしまうのでこれで最適かな
→だめだった！！！！

=====けんちょんさんのブログ参照=========

A,Bから小さい方から同じ数だけ使っていくのが最適なのは自明
高橋君のA,Bを除いてA,BからNこずつ取ってくるとする。
最大値を最小化するには、大きい方からx番目と小さい方からx番目をマッチングする
→真ん中らへんに最大値が来るはず
最大値を無理やり求めてあげる→最大値がtA*tBを超えない最大のNを2分探索!!
"""
def kth(m,k):
    if m >= k:
        return m+1
    else:
        return m

def wantmax(m,A,B): 

    ret = 0

    for i in range(-10,10):

        now = m // 2 + i
        
        if 0 < now <= m:

            ret = max( ret , kth(now,A) * kth(m-now+1,B) )

    return ret
        
            

Q = int(input())

for loop in range(Q):

    A,B = map(int,input().split())

    l = 0
    r = A*B

    while r -l != 1:
        m = (r+l) // 2

        ret = wantmax(m,A,B)
        

        if ret >= A*B:
            r = m
        else:
            l = m

    print (l)

Solution (Python):
Q=int(input())
for i in range(Q):
    A,B=map(int,input().split())
    if A>B:
        A,B=B,A
    if A==B:
        print((A-1)*2)
    elif A+1==B:
        print((A-1)*2)
    else:
        SQ=int(pow(A*B,0.5))
        if SQ**2==A*B:
            print((SQ-1)*2-1)
        elif SQ*(SQ+1)<A*B:
            print((SQ-1)*2+1)
        else:
            print((SQ-1)*2)

Solution (Python):
from math import sqrt
Q = int(input())
for _ in range(Q):
    a,b = map(int,input().split())
    rank = a*b
    sqrank = int(sqrt(rank))
    ans = 2*sqrank - 2
    if sqrank**2 == rank:
        ans -= 1
    if sqrank*(sqrank+1) < rank:
        ans += 1
    if a == b:
        ans += 1
    print(ans)

Solution (Python):
import sys
input = sys.stdin.readline
Q = int(input())

def check(x, a, b):
  y = (a <= -(-x // 2)) or (b <= -(-x // 2))
  z = (a <= (x // 2 + 1)) and (b <= (x // 2 + 1))
  return (-(-x // 2) + y) * ((x // 2 + 1) + z) < a * b

for _ in range(Q):
  a, b = map(int, input().split())

  ok = 0
  ng = a * b
  while ng - ok > 1:
    m = (ok + ng) // 2
    if check(m, a, b): ok = m
    else: ng = m
  print(ok)

Solution (Python):



Q=int(input())
for _ in range(Q):
    a,b=map(int,input().split())
    if a>b:
        a,b=b,a
    
    if a==b:
        print(2*a-2)
    else:
        ab=a*b
        ok=1
        ng=a*b
        while (ng-ok)>1:
            med=(ok+ng)//2
            if ((med+1)//2) * ((med+2)//2) >=ab:
                ng=med
            else:
                ok=med
        print(ok-1)

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

def query(a,b):
    if a>b:
        a,b=b,a
    ab=a*b
    c=int(math.sqrt(ab))
    if c*c==ab:
        c-=1
    if b-a<=1:
        print(2*a-2)
    elif c*(c+1)>=ab:
        print(2*c-2)
    else:
        print(2*c-1)
    
for _ in range(Q):
    a,b=map(int,input().split())
    query(a,b)

Solution (Python):
Q = int(input())
def f(a, b, s, i):
    if i == b or i<= 0 or i>s+1:
        return 0
    if s<a:
        if i<b:
            return (s-i+1)*i
        else:
            return (s-i+2)*i
    elif s-a<b-1:
        if i<=s-a:
            return i*(s-i+1)
        elif i<b:
            return i*(s-i)
        else:
            return i*(s-i+1)
    else:
        if i<b:
            return i*(s-i+1)
        elif i<=s-a+1:
            return i*(s-i+2)
        else:
            return i*(s-i+1)  
def C(a, b, x):
    if x >= a:
        x+=1
    
    if max(f(a, b, x, x//2), f(a, b, x, x//2-1), f(a, b, x, x//2+1), f(a, b, x, x//2-2), f(a, b, x, x//2+2), f(a, b, x, x//2-3), f(a, b, x, x//2+3))<a*b:
        return True
    return False
for _ in range(Q):
    a, b = map(int, input().split())
    if a<b:a, b = b, a
    ok = b-1
    ng = 10**20
    while ng-ok>1:
        mid = (ok+ng)//2
        if C(a, b, mid):ok = mid
        else:ng = mid
    print(ok)

Solution (Python):

def f(a, n):
  x = min((a+n)/2, n)
  y = a+n-x
  return x*y

Q = input()
for _ in range(Q):
  A, B = map(int, raw_input().split())
  if A>B:
    A,B = B,A
  n = 0
  b = 2**32
  while b>0:
    if f(A+1, n+b)<A*B:
      n += b
    b /= 2
  print A-1+n

Solution (Python):
from math import sqrt
q=int(input())
for i in range(q):
  a,b=map(int,input().split())
  c=min(a,b)
  b=max(a,b)
  a=c
  s=int(sqrt(a*b-1))
  if s*s>=a*b:
    s=s-1
  if (s+1)*(s+1)<a*b:
    s=s+1
  if a==b:
    print(2*(a-1))
  elif s*(s+1)<a*b:
    print(a-1+s+(s-a))
  else:
    print(a-1+s+(s-a-1))

Solution (Python):
def can(a, b):
  if a > b:
    a, b = b, a
  if b - a <= 1:
    return 2*a-2
  c = int((a*b)**0.5)
  if c**2 == a*b:
    c -= 1
  if c*(c+1) >= a*b:
    return 2*c-2
  return 2*c-1
q = int(input())
for _ in range(q):
  a, b = map(int, input().split())
  print(can(a, b))

Solution (Python):
def floorsqrt(x):
  max_ok = 0
  min_ng = x+1
  while min_ng-max_ok>1:
    tgt = (max_ok+min_ng)//2
    if tgt*tgt<=x:
      max_ok = tgt
    else:
      min_ng = tgt
  return max_ok

def K1(C, r):
  max_ok = 0
  min_ng = C
  while min_ng-max_ok>1:
    tgt = (max_ok+min_ng)//2
    if C-tgt>r+tgt*tgt:
      max_ok = tgt
    else:
      min_ng = tgt
  return max_ok

def K2(C, r):
  max_ok = 0
  min_ng = C
  while min_ng-max_ok>1:
    tgt = (max_ok+min_ng)//2
    if C-tgt>r+tgt*(tgt+1):
      max_ok = tgt
    else:
      min_ng = tgt
  return max_ok

Q = int(input())
for _ in range(Q):
  A, B = map(int, input().split())
  if A*B==1:
    print(0)
    continue
  if B<A: A,B = B,A
  
  C = floorsqrt(A*B-1)
  
  
  
  
  q = (A*B-1)//C
  r = (A*B-1)%C

  if q==C:
    
    
    
    
    
    
    K = K1(C, r)
    ans = 2*C-3 if B<=C+K else 2*C-2
    
  
  elif q==C+1:
    
    
    
    
    
    
    K = K2(C, r)
    ans = 2*C-2 if B<=C+K+1 else 2*C-1
 
  else:
    
    
    
    ans = 2*C if A==B else 2*C-1
    
  print(ans)

Solution (Python):
for _ in range(int(input())):
    A, B = sorted(map(int, input().split()))
    if B - A <= 1:
        print(2*A-2)
    else:
        X = int((A*B)**.5)
        X -= A*B == X*X
        print(2*X-1-(X*(X+1)>=A*B))

Solution (Python):
q = int(input())
for _ in range(q):
    R = list(map(int, input().split()))
    a, b = min(R), max(R)
    if b - a <= 1:
        res = 2 * (a-1)

    else:
        c = int((a*b)**0.5)
        if c ** 2 == a * b:
            c -= 1
        if c * (c+1) < a * b:
            res = 2 * (c-1) + 1
        else:
            res = 2 * (c-1)
    print(res)

Solution (Python):

from math import floor,sqrt
Q = int(input())
for _ in range(Q):
    A,B = map(int,input().split())
    if A > B:
        A,B = B,A
    if A == B:
        print(2*A-2)
        continue
    if A+1 == B:
        print(2*A-2)
        continue
    C = floor(sqrt(A*B))
    if C == sqrt(A*B):
        C -= 1
    if C*(C+1) >= A*B:
        print(2*C-2)
    else:
        print(2*C-1)

Solution (Python):
import sys
input = sys.stdin.readline
Q=int(input())
def solv(A,B):
    if A==B:
        return 2*A-2

    if A>B:
        A,B=B,A

    l=A
    r=B*2
    while r-l>1:
        m=(l+r)//2
        x=(m+1)//2*(m//2+1)
        if x<A*B:
            l=m
        else:
            r=m
    return l-1



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

Solution (Python):
"""
N/x-1 - N/x >= 1のところを狙っていく
解aだとして、aまではAを+1ずつインクリメントでOK
a以降はN/aからbをデクリメント
"""
N = int(input())

def calc(a,b):
    N = a * b 
    n = int(N**0.5)-2
    while (n+1)**2 < N:
        n += 1
    if a == b == 1:
        return 0
    cnta = n
    cntb = n
    if a <= n:
        cnta -= 1
    if b <= n:
        cntb -= 1
    ans = cnta + cntb
    
    
    
    
    
    
    if n*(n+1) >= N:
        ans -= 1
    return ans        

for i in range(N):
    a,b = map(int, input().split())
    ans = calc(a,b)
    print(ans)

Solution (Python):
import sys
from math import floor, sqrt, ceil

q, *ab = map(int, sys.stdin.read().split())
ab = zip(*[iter(ab)] * 2)

def main():
    for a, b in ab:
        if a > b:
            a, b = b, a
        res = 0
        res += (a - 1) * 2
        
        if b - a <= 1:
            yield res 
            continue
        
        p = a * b
        c = floor(sqrt(p))
        res += (c - a) * 2 + 1
        if c * (c + 1) >= p:
            res -= 1
        if c * c == p:
            res -= 1
        yield res



if __name__ == '__main__':
    ans = main()
    print(*ans, sep='\n')

Solution (Python):
Q = int(input())
AB = [tuple(map(int, input().split())) for _ in range(Q)]
for A, B in AB:
    C = (A * B)**0.5

    if A == B:
        print(A * 2 - 2)
    else:
        if C.is_integer():
            C -= 1

        C = int(C)

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

Solution (Python):
def main():
    n = int(input())
    ab = [list(map(int, input().split())) for _ in [0]*n]
    for a, b in ab:
        a, b = min(a, b), max(a, b)
        s = (a*b-1)**0.5
        s_int = int(s)
        if a*b-1 > 1000:
            for i in range(s_int-10, s_int+10):
                if i*i > a*b-1:
                    s_int = i - 1
                    break
        if a == b:
            ans = 2*s_int
        elif (a*b-1)//s_int == s_int:
            ans = 2*s_int-2
        else:
            ans = 2*s_int-1
        print(ans)


main()

Solution (Python):
import math
import decimal

def hantei(f,p):
    if f*f<p :
        return True
    return False


def nibun2(l,r,p):
    if r == l:
        return l
    f = (l+r+1)//2
    if hantei(f,p):
        return nibun2(f,r,p)
    else:
        return nibun2(l,f-1,p)

r=[]
q=int(input())
for _ in range(q):
    a,b=map(int,input().split())
    p=a*b
    if p ==1:res = 0
    else:
        t = nibun2(0,10**20,p)
        res = t*2-1
        if (p-1)//t ==t:res -= 1
        if a==b:res+=1
    r.append(res)

print("\n".join(map(str,r)))

Solution (Python):
Q = int(input())
AB = [tuple(map(int, input().split())) for _ in range(Q)]
for A, B in AB:
    C = (A * B)**0.5

    if A == B:
        print(A * 2 - 2)
    else:
        if C.is_integer():
            C -= 1

        C = int(C)

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

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

for a,b in ab:
    
    if a>b:
        a,b=b,a
    if a==b:
        print(2*(a-1))
        continue
        
    c = math.ceil((a*b)**0.5-1)
    
    if c*(c+1)>=a*b:
        print(2*(c-1))
    else:
        print(2*c-1)

Solution (Python):
import math
Q = int(input())
ans = []

for i in range(Q):
    a,b = map(int,input().split())
    if a>b:
        a,b = b,a
    if a == b:
        ans.append((a-1)*2)
        continue
    c = math.ceil((a*b)**0.5-1)
    
    ans.append(c*2-2)
    if c*(c+1) < a*b:
        ans[-1] +=1
for i in ans:
    print(i)

Solution (Python):
import math
Q = int(input())
ans = []

for i in range(Q):
    a,b = map(int,input().split())
    if a>b:
        a,b = b,a
    if a == b:
        ans.append((a-1)*2)
        continue
    c = math.ceil((a*b)**0.5-1)
    
    ans.append(c*2-2)
    if c*(c+1) < a*b and c+1 != b:
        ans[-1] +=1
for i in ans:
    print(i)

Solution (Python):
import math
Q = int(input())
ans = []

for i in range(Q):
    a,b = map(int,input().split())
    if a>b:
        a,b = b,a
    if a == b:
        ans.append((a-1)*2)
        continue
    c = math.ceil((a*b)**0.5-1)
    
    ans.append(c*2-2)
    if c*(c+1) < a*b and a != c+1 != b:
        ans[-1] +=1
for i in ans:
    print(i)

Solution (Python):
import math
Q = int(input())
ans = []

for i in range(Q):
    a,b = map(int,input().split())
    if a>b:
        a,b = b,a
    if a == b:
        ans.append((a-1)*2)
        continue
    c = math.ceil((a*b)**0.5-1)
    
    ans.append(c*2-1)
    if a <= c:
        ans[-1] -= 1
    if c*(c+1) < a*b and a != c+1 != b:
        ans[-1] +=1
for i in ans:
    print(i)

Solution (Python):
import math
Q = int(input())
ans = []

for i in range(Q):
    a,b = map(int,input().split())
    if a>b:
        a,b = b,a
    if a == b:
        ans.append((a-1)*2)
        continue
    c = math.ceil((a*b)**0.5-1)
    
    ans.append(c*2-1)
    if a <= c:
        ans[-1] -= 1
    if c*(c+1) < a*b and a != c+1 != b:
        ans[-1] +=1
for i in ans:
    print(i)

Solution (Python):
import math


def calc(A, B):
    A, B = max(A, B), min(A, B)
    X = A * B
    rt = int(math.sqrt(X))
    if rt**2 == X:
        rt -= 1
    ans = rt * 2
    if B <= rt:
        ans -= 1
    if rt * (rt+1) >= X:
        ans -= 1
    return ans


def main():
    Q = int(input())
    for _ in range(Q):
        A, B = map(int, input().split())
        ans = calc(A, B)
        print(ans)


if __name__ == "__main__":
    main()

Solution (Python):

import sys
input = sys.stdin.buffer.readline


from collections import defaultdict


def getlist():
	return list(map(int, input().split()))


def main():
	Q = int(input())
	for i in range(Q):
		A, B = sorted(getlist())
		if A == B:
			print(2 * A - 2)
		elif B == A + 1:
			print(2 * A - 2)
		else:
			C = int(((A * B) ** 0.5) // 1)
			if A * B == C ** 2:
				C -= 1
			if C * (C + 1) >= A * B:
				print(2 * C - 2)
			else:
				print(2 * C - 1)



if __name__ == '__main__':
	main()

Solution (Python):
import math
Q = int(input())
lst = []
for i in range(Q):
    lst.append(list(map(int, input().split())))
for i in lst:
    if i[0] == i[1]:
        print(2*i[0] - 2)
    else:
        ans = math.floor((i[0]*i[1])**0.5)
        if ans**2 == i[0]*i[1]:
          print((ans-1)*2 - 1)
        elif ans * (ans+1) >= i[0]*i[1]:
          print(ans*2 - 2)
        else:
          print(ans*2 - 1)

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

for A, B in AB:
  if A == B or A+1 == B:
    print(2*A-2)
    continue
  C = floor((A*B)**(1/2))
  if C == (A*B)**(1/2):
    C -= 1
  if C*(C+1) >= A*B:
    print(2*C-2)
  elif C*C < A*B:
    print(2*C-1)

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

for i in range(Q):
    A, B = map(int, input().split())
    C = int((A*B) ** 0.5)
    if C*(C+1) < A*B:
        print(2*C - 1)
    elif A*B == C*C and A != B:
        print(2*C - 3)
    else: 
        print(2*C - 2)

Solution (Python):
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda :sys.stdin.readline().rstrip()

def trisection(l,r,f,convex=True,discrete=True):
    eps=2 if(discrete) else 10**-12
    while(r-l>eps):
        d=(r-l)//3 if(discrete) else (r-l)/3
        h1=l+d; h2=r-d
        if((f(h1)>=f(h2))^convex): r=h2
        else: l=h1
    if(discrete):
        if(convex): return min([(f(l),l),(f(l+1),l+1),(f(r),r)])[1]
        else: return max([(f(l),l),(f(l+1),l+1),(f(r),r)])[1]
    else:
        if(convex): return min([(f(l),l),(f(r),r)])[1]
        else: return max([(f(l),l),(f(r),r)])[1]

def resolve():
    for _ in range(int(input())):
        a,b=map(int,input().split())
        l=0; r=a+b
        while(r-l>1):
            h=(l+r)//2
            f=lambda x:(x+(x>=a))*(h-x+1+(h-x+1>=b))
            x=trisection(1,h,f,convex=False,discrete=True)
            if(f(x)<a*b): l=h
            else: r=h
        print(l)
resolve()

Solution (Python):
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda :sys.stdin.readline().rstrip()
def resolve():
    for _ in range(int(input())):
        a,b=map(int,input().split())
        s=(a*b)**.5
        c=int(s)-1 if(s==int(s)) else int(s)
        if(a>b): a,b=b,a 
        if(b-a<=1):
            print(2*a-2)
        elif(c*(c+1)>=a*b):
            print(2*c-2)
        else:
            print(2*c-1)
resolve()

Solution (Python):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(pow(10, 6))

def main():
    q = int(input())
    ab = [tuple(map(int, input().split())) for _ in range(q)]
    for a, b in ab:
        if a > b:
            a, b = b, a
            
        if a == b or a + 1 == b:
            print(2 * a - 2)
        else:
            low, high = 1, pow(10, 9) + 5
            while low < high:
                mid = (low + high) // 2
                if mid * mid < a * b:
                    low = mid + 1
                else:
                    high = mid
            if low * (low - 1) >= a * b:
                print(2 * (low - 1) - 2)
            else:
                print(2 * (low - 1) - 1)                    


if __name__ == '__main__':
    main()

Solution (Python):
import math
import sys
input = sys.stdin.readline

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

for A, B in AB:
    P = A*B-1
    b = int(math.sqrt(P))
    if b <= 1:
        if P <= 1:
            print(0)
        else:
            print(1)
        continue
    K = []
    for n in [b-1, b, b+1]:
        if P//n > n:
            border = n
        K.append(min(n, P//n))
    ans = 2*border
    if P//(border+1) == border+1:
        ans += 1
    if min(A, B) <= max(K):
        ans -= 1

    print(ans)

Solution (Python):
def sq(n):
    l = 0
    r = n
    while r-l > 1:
        m = (l+r) // 2
        if m*m < n:
            l = m
        else:
            r = m
    return l

Q = int(input())
for _ in range(Q):
    a, b = map(int, input().split())
    r = sq(a * b)
    if a == b:
        print(2*a-2)
    elif r*(r+1) < a*b:
        print(2*r-1)
    else:
        print(2*r-2)

Solution (Python):
import sys

sys.setrecursionlimit(10 ** 6)
input = sys.stdin.readline

def main():
    def df(c, d):
        res = c // d
        if res * d > c: res -= 1
        return res

    q = int(input())
    L = [list(map(int, input().split())) for _ in range(q)]
    for a, b in L:
        if (a, b) == (1, 1):
            print(0)
            continue
        c = a * b - 1
        rc = int(c ** 0.5)
        rc -= 1
        while (rc + 1) ** 2 <= c:
            rc += 1
        ans = rc * 2
        if rc * (rc + 1) > c:
            ans -= 1
        ab = [a, b]
        for k in ab:
            ans -= 1
            if k > rc:
                y = df(c, k)
                if y == 0:
                    ans += 1
                else:
                    x = df(c, y)
                    if x != k or (df(c, k) == df(c, k - 1)):
                        ans += 1
        print(ans)

main()

Solution (Python):
import math

Q = int(input())
for _ in range(Q):
    A, B = map(int, input().split())
    if A > B:
        A, B = B, A
    C1 = math.floor(math.sqrt(A) * math.sqrt(B))
    if C1 ** 2 >= A*B:
        C1 -= 1
    if C1 == 0:
        print(0)
        continue
    C2 = (A*B-1) // C1
    ans = C1 * 2
    if C1 == C2:
        ans -= 1
    if C1 >= A:
        ans -= 1
    print(ans)

Solution (Python):
t= int(input())
for i in range(t):
    k=input().split()
    a=int(k[0])
    b=int(k[1])
    c=a*b
    d=int(c**.5)
    print(2*d-1-((d*(d+1))>=c)-(c==d*d and a!=b))

Solution (Python):
N = int(input())
Q = [[int(x) for x in input().split()] for _ in range(N)]
for i in range(N):
  a,b = Q[i]
  if a==b:
    print((a-1)*2)
    continue
  c = int((a*b)**0.5)
  if a>c and b>c:
    if c*(c+1)<a*b:
      print(c*2)
    else:
      print(c*2-1)
  else:
    if c*(c+1)<a*b:
      print(c*2-1)
    else:
      if c**2==a*b:
      	print(c*2-3)
      else:
        print(c*2-2)

Solution (Python):
Q, *AB = map(int, open(0).read().split())

for a, b in zip(*[iter(AB)] * 2):
    if a == b or a + 1 == b:
        print(2 * a - 2)
    else:
        c = int((a * b) ** 0.5)
        if c * c == a * b:
            c -= 1

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

Solution (Python):
Q = int(input())
AB = [[int(i) for i in input().split()] for _ in range(Q)]
for a, b in AB :
    ret = 0
    sqrt = int((a * b) ** 0.5)
    ret += sqrt - 1
    ret += (a * b - 1) // sqrt - 1
    if a == b :
        ret += 1
    print(ret)

Solution (Python):
Q = int(input())
L = []
for i in range(Q):
    L.append([int(x) for x in input().split()])
    
for i in range(Q):
    a = min(L[i])
    b = max(L[i])
    if a == b:
        ans = 2*a-2
    elif a+1 == b:
        ans = 2*a-2
    else:
        c = int((a*b)**0.5)
        if c**2 == a*b:
            c -= 1
        if c*(c+1) >= a*b:
            ans = 2*c-2
        else:
            ans = 2*c-1
    print(ans)

Solution (Python):

from collections import defaultdict
from collections import deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
import itertools
sys.setrecursionlimit(10**5)
stdin = sys.stdin
bisect_left = bisect.bisect_left
bisect_right = bisect.bisect_right
def LI(): return list(map(int, stdin.readline().split()))
def LF(): return list(map(float, stdin.readline().split()))
def LI_(): return list(map(lambda x: int(x)-1, stdin.readline().split()))
def II(): return int(stdin.readline())
def IF(): return float(stdin.readline())
def LS(): return list(map(list, stdin.readline().split()))
def S(): return list(stdin.readline().rstrip())
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = float('INF')


def A():
    s = S()
    s.sort()
    if s == ["a", "b", "c"]:
        print("Yes")
    else:
        print("No")
    return


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(b - k + 1, a + k), b + 1):
        ans.append(i)
    for i in ans:
        print(i)
    return


def C():
    abc= LI()
    abc.sort()
    a,b,c = abc
    a1 = (c - a) % 2
    b1 = (c - b) % 2
    if a1 and b1:
        print((c - a) // 2 + (c - b) // 2 + 1)
        return
    if a1 or b1:
        print((c - a) // 2 + (c - b) // 2 + 2)
        return
    print((c - a) // 2 + (c - b) // 2 )

    return

























def D():
    for _ in range(II()):
        a, b = LI()
        c = math.ceil(math.sqrt(a * b)) - 1
        print(2 * a - 2 if a == b else 2 * c - 1 - (c * (c + 1) >= a * b))
    return


if __name__ == '__main__':
    D()

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

for i in range(Q):
    a,b = AB[i]
    C = int((a*b)**0.5)
    if C*C == a*b:
        ans = 2*(C-1)
        if min(a,b) < C:
            ans -= 1
    elif C*(C+1) >= a*b:
        ans = 2*C - 2
    else:
        ans = 2*C - 1
    
    print(ans)

Solution (Python):
def solve(ab,i):
    return (ab - 1) // i
    
q = int(input())
for i in range(q):
    a, b = map(int, input().split())
    if a > b:
        a, b = b, a
    ans = a - 1
    
    start_a = a + 1
    num = int((a * b) ** 0.5)
    while True:
        if solve(a*b, num) == solve(a*b, num + 1):
            break
        else:
            num += 1
    ans = num - 1
    ans += (solve(a*b, num) - 1)
    if a == b:
        ans += 1
    print(ans)

Solution (Python):
q=int(input())
ans=[0]*q
for i in range(q):
    a,b=map(int,input().split())
    n=a*b
    if a==b:
        ans[i]=2*(a-1)
    elif n==int(n**.5)**2:
        ans[i]=(int(n**.5)-1)*2-1
    elif n<=int(n**.5)*(int(n**.5)+1):
        ans[i]=(int(n**.5)-1)*2
    else:
        ans[i]=(int(n**.5)-1)*2+1
print(*ans,sep='\n')