main = do
  n <- readLn
  inps <- mapM (const (fmap (map read . words) getLine)) [1..n]
  mapM_ (print . solve) inps


solve :: [Int] -> Int
solve [a,b] =
  (max 0 (2*(m-2))) - sa - sb + count m a b 0 + count m b a 1
  where
    m  = floor (sqrt (fromIntegral(a*b-1)))
    sa = if a < m-1 then 1 else 0
    sb = if b < m-1 then 1 else 0
    count x s t f
      | x <= 0    = 0
      | x < m-1   = 0
      | x == s    = count (x-1) s t 0
      | length[() |i<-[f..5], x*(m+i) < a*b, m+i/=t] >= 1 = 1 + count (x-1) s t 0
      | otherwise = count (x-1) s t 0