## https://yukicoder.me/problems/no/2882 import math from collections import deque def solve(A, B): sqrt_b = int(math.sqrt(B)) answer1 = 0 for m in range(1, sqrt_b + 1): mod_a = A % m mod_b = B % m if mod_a >= mod_b: answer1 += 1 a_array = deque() # 半開区間の列を構築 for q in range(sqrt_b + 1): if q == 0: a_array.append((B, 0, A)) else: s = A // q if s > sqrt_b: a_array.append((s, q, A % q)) else: a_array.append((sqrt_b, -1, -1)) b_array = deque() for q in range(1, sqrt_b + 1): s = B // q if s > sqrt_b: b_array.append((s, q, B % q)) else: b_array.append((sqrt_b, -1, -1)) if b_array[-1][0] > sqrt_b: b_array.append((sqrt_b, -1, -1)) a = a_array.popleft() b = b_array.popleft() max_x = a[0] answer2 = 0 while len(a_array) > 0 and len(b_array) > 0: x = max(a_array[0][0], b_array[0][0]) ans = 0 a0, coef_a, intercept_a = a b0, coef_b, intercept_b = b if coef_a == coef_b: if intercept_b + coef_b * b0 <= intercept_a + coef_a * a0: ans += max_x - x else: # coef_a < coef_b: coef = coef_b - coef_a z = ((intercept_b + coef_b * b0 )- (intercept_a + coef_a * a0)) m_lower = z // coef + (1 if z % coef > 0 else 0) if m_lower <= x: ans += max_x - x elif x < m_lower <= max_x: ans += max_x - m_lower + 1 answer2 += ans if a_array[0][0] == b_array[0][0]: max_x = x a = a_array.popleft() b = b_array.popleft() elif a_array[0][0] > b_array[0][0]: max_x = x a = a_array.popleft() elif a_array[0][0] < b_array[0][0]: max_x = x b = b_array.popleft() return answer1 + answer2 def solve2(A, B): answer1 = 0 for m in range(1, B + 1): mod_a = A % m mod_b = B % m if mod_a >= mod_b: answer1 += 1 print(f"m = {m}") return answer1 def main(): T = int(input()) answers = [] for _ in range(T): A, B = map(int, input().split()) ans = solve(A, B) # ans2 = solve2(A, B) # print(ans2) answers.append(ans) for ans in answers: print(ans) if __name__ == "__main__": main()