import math MOD = 10 ** 9 + 7 t = int(input()) for _ in range(t): a, n = map(int, input().split()) ans = 0 for x in range(1, int(math.sqrt(n * a)) + 1): lb = (x * x + a - 1) // a ub = min(n + 1, ((x + 1) * (x + 1) + a - 1) // a) if ub - lb <= 0: continue ans = (ans + (ub - lb) * x) % MOD print(ans)