Chef wants to make a feast. In order to do that, he needs a lot of different ingredients. Each ingredient has a certain tastiness; the tastiness of each ingredient may be any positive integer. Initially, for each tastiness between $K$ and $K+N-1$ (inclusive), Chef has an infinite supply of ingredients with this tastiness.
The ingredients have a special property: any two of them can be mixed to create a new ingredient. If the original ingredients had tastiness $x$ and $y$ (possibly $x = y$), the new ingredient has tastiness $x+y$. The ingredients created this way may be used to mix other ingredients as well. Chef is free to mix ingredients in any way he chooses any number of times.
Let's call a tastiness $v$ ($v > 0$) unreachable if there is no way to obtain an ingredient with tastiness $v$; otherwise, tastiness $v$ is reachable. Chef wants to make ingredients with all reachable values of tastiness and he would like to know the number of unreachable values. Help him solve this problem. Since the answer may be large, compute it modulo $1,000,000,007$ ($10^9+7$).
Note that there is an infinite number of reachable values of tastiness, but it can be proven that the number of unreachable values is always finite for $N \ge 2$.

-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first and only line of each test case contains two space-separated integers $N$ and $K$.

-----Output-----
For each test case, print a single line containing one integer — the number of unreachable values of tastiness, modulo $1,000,000,007$.

-----Constraints-----
- $1 \le T \le 10^5$
- $2 \le N \le 10^{18}$
- $1 \le K \le 10^{18}$

-----Subtasks-----
Subtask #1 (20 points): $N = 2$
Subtask #2 (80 points): original constraints

-----Example Input-----
2
2 1
3 3

-----Example Output-----
0
2

-----Explanation-----
Example case 1: It is possible to obtain ingredients with all values of tastiness.
Example case 2: Ingredients with tastiness $1$ and $2$ cannot be made.

Difficulty: MEDIUM_HARD
Tags: Mathematics

Starter code:

Solutions:
t = int(input())
mod = 10 ** 9 + 7
for _ in range(t):
	(n, k) = map(int, input().split())
	n = n - 1
	if k % n == 0:
		z = k // n - 1
	else:
		z = k // n
	if z == 0:
		print((k - 1) % mod)
	else:
		l = (k * z - n * z * (z + 1) // 2) % mod
		print(((l - z) % mod + (k - 1) % mod) % mod)


__author__ = 'Prateek'
MOD = int(10 ** 9 + 7)

def test():
	(n, k) = list(map(int, input().split()))
	l = k
	d = n - 1
	ans = l - 1
	ans = ans % MOD
	a = k - n
	term = (d + a) // d
	ll = (a % MOD - (term - 1) % MOD * (d % MOD) % MOD) % MOD
	if ll < 0:
		ll = (ll + MOD) % MOD
	m = term % MOD * ((a % MOD + ll % MOD) % MOD) % MOD
	m = m * pow(2, MOD - 2, MOD) % MOD
	ans += m
	ans = ans % MOD
	print(ans)
if __author__ == 'Prateek':
	t = int(input())
	for _ in range(t):
		test()


m = 1000000007
import math
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	(s, ans, N, d, a) = (0, 0, 0, 0, 0)
	if n - 1 >= k - 1:
		print((k - 1) % m)
	else:
		N = (k - 1) // (n - 1)
		d = n - 1
		a = k - 1
		if (k - 1) % (n - 1):
			N += 1
		s = N * (2 * a - (N - 1) * d)
		ans = s // 2
		print(ans % m)


def main():
	for i in range(int(input())):
		(n, k) = map(int, input().split())
		ans = k - 1
		if n == 1 | k == 1:
			print(0)
		elif n == 2:
			print(k * (k - 1) // 2 % 1000000007)
		elif n >= k:
			print((k - 1) % 1000000007)
		elif k > n:
			k = k - n
			n -= 1
			time = k // n
			ans += k + k * time - n * (time * (time + 1) // 2)
			print(ans % 1000000007)
main()


MOD = 1000000007
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	last_term = k - 1
	if n == 2:
		ans = last_term * (last_term + 1) // 2
		print(ans % MOD)
	else:
		diff = n - 1
		first_term = last_term % diff
		if first_term == 0:
			first_term = diff
		no_of_terms = (last_term - first_term) // diff + 1
		ans = (2 * first_term + (no_of_terms - 1) * diff) * no_of_terms // 2
		print(ans % MOD)


for _ in range(int(input())):
	(n, k) = input().split()
	m = 10 ** 9 + 7
	n = int(n) - 1
	k = int(k) - 1
	if n >= k:
		print(k % m)
	else:
		t = k // n
		if k % n:
			t += 1
		ans = t * (2 * k - (t - 1) * n)
		ans = ans // 2
		print(ans % m)


for i in range(int(input())):
	(n, k) = [int(i) for i in input().split()]
	mod = 1000000007
	c = (k - 1) % mod
	dif = 2 * k - (n + k - 1) - 1
	if dif > 0:
		no = dif // (n - 1)
		if dif % (n - 1) != 0:
			no += 1
		c = (c + no * (2 * dif - (no - 1) * (n - 1)) // 2 % mod) % mod
	print(c)


t = int(input())
while t > 0:
	(n, k) = map(int, input().split())
	ans = 0
	last = k - 1
	com_dif = n - 1
	first = last % com_dif
	if first == 0:
		first = com_dif
	terms = (last - first) // com_dif + 1
	ans = terms * (last + first) // 2 % 1000000007
	print(ans)
	t -= 1


t = int(input())
m = 1000000007
for i in range(0, t):
	(n, k) = list(map(int, input().split()))
	s = 0
	l = k - n
	if l < 0:
		print((k - 1) % m)
	else:
		n1 = l // (n - 1)
		if l % (n - 1) != 0:
			n1 += 1
		f = k - n - (n1 - 1) * (n - 1)
		s = n1 * (f + l) // 2
		print((k - 1 + s) % m)


def main():
	from sys import stdin, stdout
	Mod = 10 ** 9 + 7
	t = int(stdin.readline())
	for i in range(t):
		(n, k) = list(map(int, stdin.readline().split()))
		a = (k - 1) % (n - 1)
		d = n - 1
		num = (k - 1) // (n - 1) + 1
		res = num * (2 * a + (num - 1) * d) // 2
		stdout.write(str(res % Mod) + '\n')

def __starting_point():
	main()
__starting_point()


for _ in range(int(input())):
	(N, K) = map(int, input().split())
	l = K - 1
	d = N - 1
	a = l % d
	if a == 0:
		a = d
	n = (l - a) // d + 1
	S = n * (a + l) // 2
	print(S % 1000000007)


import math
for i in range(int(input().strip())):
	ans = 0
	data = input().split()
	n = int(data[0])
	k = int(data[1])
	high = (k - 2) // (n - 1)
	ans = (k * high - (n - 1) * high * (high + 1) // 2 - high) % 1000000007
	print((ans + (k - 1)) % 1000000007)


MOD = 10 ** 9 + 7
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	a = k
	b = n + k - 1
	c = (a - 1) % MOD
	req = a * 2 - b - 1
	d = n - 1
	if req > 0:
		if req % d == 0:
			nt = req // d
		else:
			nt = req // d + 1
		c += nt * (2 * req + (nt - 1) * -d) // 2 % MOD % MOD
	print(c % MOD)


t = int(input())
for i in range(t):
	(n, k) = map(int, input().split())
	v = (k - n - 1) // (n - 1) + 1
	mod = 1000000007
	if n >= k:
		ans = k - 1
	else:
		ans = k - 1 + (k - n) * v - (n - 1) * v * (v - 1) // 2
	print(ans % mod)


MOD = 1000000007
testcases = int(input())
while testcases > 0:
	(n, k) = input().split(' ')
	n = int(n)
	k = int(k)
	if n >= k:
		print((k - 1) % MOD)
	else:
		terms = int((k - 1) // (n - 1))
		s1 = 2 * (k - n)
		s2 = terms - 1
		s3 = 1 - n
		sum = k - 1 + terms * 500000004 * (s1 + s2 * s3)
		sum %= MOD
		print(sum)
	testcases = testcases - 1


t = int(input())
for i in range(t):
	ni = input().split()
	n = int(ni[0])
	k = int(ni[1])
	d = 1 - n
	a = k - 1
	r = int(a % (-1 * d))
	ter = int((r - a) // d + 1)
	sum1 = ter * (a + r) // 2
	print(int(sum1 % 1000000007))


def main():
	Mod = 10 ** 9 + 7
	t = int(input())
	for i in range(t):
		(n, k) = list(map(int, input().split()))
		a = (k - 1) % (n - 1)
		d = n - 1
		num = (k - 1) // (n - 1) + 1
		res = num * (2 * a + (num - 1) * d) // 2
		print(res % Mod)
main()


t = int(input())
for i in range(t):
	m = pow(10, 9) + 7
	(N, K) = map(int, input().split())
	if N == 2:
		r = K - 1
		t = r * (r + 1) // 2 % m
		print(t)
	elif N - 1 >= K - 1:
		print((K - 1) % m)
	else:
		n = (K - 1) // (N - 1)
		w = N - 1
		a = K - 1
		if (K - 1) % (N - 1):
			n += 1
		p = n * (2 * a - (n - 1) * w)
		ans = p // 2
		print(ans % m)


import sys
import math

def modInverse(a):
	m = 1000000007
	g = gcd(a, m)
	return power(a, m - 2, m)

def power(x, y, m):
	if y == 0:
		return 1
	p = power(x, y // 2, m) % m
	p = p * p % m
	if y % 2 == 0:
		return p
	else:
		return x * p % m

def gcd(a, b):
	if a == 0:
		return b
	return gcd(b % a, a)
n = sys.stdin.readline()
for i in range(int(n)):
	arr = [int(x) for x in sys.stdin.readline().rstrip().split()]
	n = arr[0]
	k = arr[1]
	high = (k - 1) // (n - 1)
	ans = (k * high - (n - 1) * high * (high + 1) * modInverse(2) - high + 1000000007) % 1000000007
	print((ans + (k - 1)) % 1000000007)


from math import floor as flr, ceil
modv = 1000000007
mmi2 = pow(2, modv - 2, modv)
for tnum in range(int(input())):
	ipstr = [int(elem) for elem in input().strip().split(' ')]
	(nvar, kvar) = ipstr
	cdiff = nvar - 1
	a_n = kvar - 1
	a_first = a_n % cdiff
	if a_first == 0:
		a_first = cdiff
	nval = (a_n - a_first) // cdiff + 1
	res = nval * (a_first + a_n) // 2
	if res < 0:
		res = 0
	print(int(res) % modv)


import math
MOD = 1000000007
t = int(input())
for _ in range(t):
	(n, k) = map(int, input().split())
	ans = 0
	if k == 1:
		ans = 0
	elif n == 2:
		ans = k * (k - 1)
		ans = ans // 2
		ans = ans % MOD
	elif n >= k:
		ans = k - 1
		ans = ans % MOD
	else:
		t = (k - 1) // (n - 1) + 1
		last_term = k - 1 + (t - 1) * (1 - n)
		ans = (k - 1 + last_term) * t // 2
		ans = ans % MOD
	print(ans)


mod = 10 ** 9 + 7
for T in range(int(input())):
	(N, K) = list(map(int, input().split()))
	if K == 1:
		print(0)
	else:
		A = K - N
		R = N - 1
		X = A // R + 1
		S = X % mod * ((2 * A - (X - 1) * R) % mod) % mod
		print((S * pow(2, mod - 2, mod) % mod + (K - 1)) % mod)


import math
MOD = 1000000007
t = int(input())
while t > 0:
	(d, k) = input().split()
	d = int(d)
	k = int(k)
	k = k - 1
	d = d - 1
	if d >= k:
		print(k % MOD)
		t = t - 1
		continue
	x = k // d
	if k % d:
		x += 1
	ans = x * (2 * k - (x - 1) * d)
	ans = ans // 2
	ans = int(ans % MOD)
	print(ans)
	t = t - 1


mod = 10 ** 9 + 7
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	if k <= n:
		print((k - 1) % mod)
		continue
	x = (k - 1) // (n - 1) + 1
	if x % 2:
		d = (x - 1) // 2 % mod * (x % mod)
	else:
		d = x // 2 % mod * ((mod + x - 1) % mod)
	d = d % mod
	x = x % mod
	k = (k - 1) % mod
	n = (n - 1) % mod
	ans = k * x % mod - d * n % mod
	if ans < 0:
		ans = mod + ans
	print(ans % mod)


t = int(input())
mood = 1000000007
for i in range(t):
	(a, b) = map(int, input().split())
	h = (b + a - 2) // (a - 1)
	l = h * (h + 1) // 2 * b - h - h * (h - 1) // 2 * (b + a - 1)
	l %= mood
	print(l)


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	if k == 1:
		print(0)
	else:
		m = 1000000007
		a = k - 1
		d = n - 1
		N = a // d + 1
		sum = N * (2 * a - N * d + d) // 2
		print(sum % m)


for _ in range(int(input())):
	(N, K) = map(int, input().split())
	m = 10 ** 9 + 7
	(ans, s, n, p, a, d) = (0, 0, 0, 0, 0, 0)
	if N - 1 >= K - 1:
		print((K - 1) % m)
	else:
		n = (K - 1) // (N - 1)
		d = N - 1
		a = K - 1
		if (K - 1) % (N - 1):
			n += 1
		s = n * (2 * a - (n - 1) * d)
		ans = s // 2
		print(ans % m)


for i in range(int(input())):
	(n, k) = list(map(int, input().split()))
	remainder = (k - 2) % (n - 1)
	quotient = (k - 2) // (n - 1)
	k += remainder
	if quotient % 2 == 1:
		feast = (1 + quotient) // 2
		print(k % 1000000007 * (feast % 1000000007) % 1000000007)
	else:
		feast = 1 + quotient
		k = k // 2
		print(k % 1000000007 * (feast % 1000000007) % 1000000007)


T = int(input())
for _ in range(T):
	(n, k) = map(int, input().split())
	val = (k - n - 1) // (n - 1) + 1
	mod = 1000000007
	if n >= k:
		ans = k - 1
	else:
		ans = k - 1 + (k - n) * val - (n - 1) * val * (val - 1) // 2
	print(ans % mod)


t = int(input())
mod = 10 ** 9 + 7
for i in range(t):
	(n, k) = input().split(' ')
	ki = int(k)
	ni = int(n)
	km = ki - 1
	nm = ni - 1
	q = km // nm
	r = km % nm
	a = km + q * r + nm * q * (q - 1) // 2
	ans = a % mod
	print(ans)


t = int(input())
for i in range(t):
	[n, k] = input().split()
	n = int(n)
	k = int(k)
	a = k - 1
	d = 1 - n
	x = a // (n - 1) + 1
	ans = x * (2 * a + (x - 1) * d) // 2 % 1000000007
	print(ans)


import math
t = int(input())
for _ in range(t):
	m = 10 ** 9 + 7
	(n, k) = list(map(int, input().split()))
	if n == 2:
		print(k * (k - 1) // 2 % m)


t = int(input())
for itt in range(t):
	(a, b) = map(int, input().split())
	n = int(a)
	k = int(b)
	end = k - 1
	diff = n - 1
	start = end % diff
	num_of_terms = (end - start) // diff + 1
	ans = num_of_terms * (end + start)
	ans = ans // 2
	ans = int(ans % 1000000007)
	print(ans)


import math
t = int(input())
for _ in range(t):
	m = 10 ** 9 + 7
	p = 0
	s = 0
	(n, k) = list(map(int, input().split()))
	if n == 2:
		print(k * (k - 1) // 2 % m)
	else:
		while k - 1 - p * (n - 1) > 0:
			s += k - 1 - p * (n - 1)
			p += 1
		print(s)


e = 1000000007

def foo(a, d):
	l = a % d
	n = (a - l) // d + 1
	return (a + l) * n // 2
for t in range(int(input())):
	(n, k) = map(int, input().split())
	print(foo(k - 1, n - 1) % e)


import math
t = int(input())
for x in range(t):
	(n, k) = map(int, input().split())
	tot = k - 1
	if n < k:
		left = k - n
		extra = n - 1
		if left <= extra:
			tot += left
		else:
			y = math.ceil(left / extra)
			tot = tot + (y * left - y * (y - 1) * extra // 2)
	print(tot % 1000000007)


for t in range(int(input())):
	(n, k) = map(int, input().split())
	a = k - 1
	b = k * 2
	c = k + n - 1
	d = b - c - 1
	print(a + d * (d + 1) // 2 % 1000000007)


M = 10 ** 9 + 7
for T in range(int(input())):
	(N, K) = list(map(int, input().split()))
	S = K - 1
	if N == 1 or K == 1:
		print(0)
	elif N == 2:
		print(K * S // 2 % M)
	elif N >= K:
		print(S % M)
	else:
		K -= N
		N -= 1
		T = K // N + 1
		S += K * T - N * (T * (T - 1) // 2)
		print(S % M)


MOD = 1000000007
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	if n == 2:
		last_term = k - 1
		first_term = 1
		no_terms = last_term
		ans = last_term * (last_term + 1) // 2
		print(ans % MOD)
	else:
		print(1)


import math
t = int(input().strip())
for i in range(0, t):
	(n, k) = map(int, input().strip().split())
	d = n - 1
	l = k - 1
	p = 10 ** 9 + 7
	if n == 2:
		s = k * (k - 1) // 2
	else:
		a = l // d + 1
		s = a * (2 * l + (a - 1) * (d * -1)) // 2
		s = int(s)
	print(s % p)


t = int(input())
while t:
	(N, K) = map(int, input().split())
	m = 10 ** 9 + 7
	(ans, s, n, p, a, d) = (0, 0, 0, 0, 0, 0)
	if N - 1 >= K - 1:
		print((K - 1) % m)
	else:
		n = (K - 1) // (N - 1)
		d = N - 1
		a = K - 1
		if (K - 1) % (N - 1):
			n += 1
		s = n * (2 * a - (n - 1) * d)
		ans = s // 2
		print(ans % m)
	t -= 1


import sys
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	m = 1000000007
	if n >= k:
		print((k - 1) % m)
	else:
		x = (n + k - 2) // (n - 1)
		s = 2 * (k - 1) - (x - 1) * (n - 1)
		s = x * s
		s = s // 2
		print(s % m)


t = int(input())
for _ in range(t):
	(n, k) = map(int, input().split())
	summation = 0
	last_term = k - 1
	common_difference = n - 1
	first_term = last_term % common_difference
	if first_term == 0:
		first_term = common_difference
	no_of_terms = (last_term - first_term) // common_difference + 1
	summation = no_of_terms * (first_term + last_term) // 2 % 1000000007
	print(summation)


import math
t = int(input())
mod = 1000000007
while t > 0:
	t = t - 1
	(n, k) = map(int, input().split())
	d1 = k - 1
	d2 = max(0, 2 * k - (k + n - 1) - 1)
	ans = 0
	if d2 > 1:
		d3 = 3 * k - 2 * (k + n - 1) - 1
		diff = d2 - d3
		nt = d2 // diff + 1
		ans = nt * (2 * d2 - (nt - 1) * diff) // 2
		fin = ans + d1
		fin = fin % mod
		print(fin)
	else:
		fin = d1 + d2
		print(fin % mod)


import math
MOD = 1000000007
t = int(input())
for _ in range(t):
	(n, k) = map(int, input().split())
	first = k - 1
	diff = n - 1
	t = (first + diff) // diff
	y = 2 * first - (t - 1) * diff
	ans = y * t // 2
	ans = ans % MOD
	print(ans)


test = int(input())
x = int(1000000000.0 + 7)
for _ in range(test):
	(n, k) = map(int, input().split())
	if k == 1:
		print(0)
	else:
		d = 1 - n
		a = k - n
		m = (d - a) // d
		sumval = (m * (2 * a + (m - 1) * d) // 2 + k - 1) % x
		print(sumval)


import math
t = int(input())
while t:
	(n, k) = input().split()
	n = int(n) - 1
	k = int(k) - 1
	x = k // n
	a = (x + 1) * k - n * x * (x + 1) // 2
	print(a % 1000000007)
	t -= 1


from math import ceil
T = int(input())
m = 1000000007
while T > 0:
	(n, k) = map(int, input().split())
	if k == 1:
		print(0)
	elif n == 2:
		ans = (k - 1) % m * (k % m) // 2
		print(ans % m)
	else:
		u = (k - 1) // (n - 1) + 1
		a = k - 1
		l = (k - 1) % (n - 1)
		ans = u * (a + l) // 2 % m
		print(int(ans))
	T -= 1


T = int(input())
for qwqwwwq in range(T):
	(n, k) = map(int, input().split(' '))
	r = (k - 1) // (n - 1)
	a = (r + 1) * (k - 1)
	b = (n - 1) * r * (r + 1)
	b = b // 2
	a = a - b
	a = a % 1000000007
	print(a)


def modInverse(a, m):
	g = gcd(a, m)
	if g != 1:
		print("Inverse doesn't exist")
	else:
		return power(a, m - 2, m)

def power(x, y, m):
	if y == 0:
		return 1
	p = power(x, y // 2, m) % m
	p = p * p % m
	if y % 2 == 0:
		return p
	else:
		return x * p % m

def gcd(a, b):
	if a == 0:
		return b
	return gcd(b % a, a)
t = int(input())
while t:
	mod = 10 ** 9 + 7
	(n, k) = map(int, input().split())
	if n >= k:
		print((k % mod - 1 % mod) % mod)
	else:
		d = n - 1
		u = k - n
		if u % d == 0:
			no = u // d
		else:
			no = u // d + 1
		m = no % mod * ((2 % mod * u % mod % mod - (no % mod - 1 % mod) % mod * d % mod % mod) % mod) % mod
		o = modInverse(2, 10 ** 9 + 7)
		print(((k % mod - 1 % mod) % mod + m % mod * o % mod % mod) % mod)
	t -= 1


mods = 1000000007
nb_Test = int(input())
for _ in range(nb_Test):
	(N, K) = [int(x) for x in input().split()]
	d = N - 1
	a = K - 1
	if d > a:
		print(a % mods)
	else:
		n = a // d
		n += a % d > 0
		ans = n * (2 * a - (n - 1) * d) // 2
		print(ans % mods)


M = 10 ** 9 + 7
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	if n - 1 >= k - 1:
		print((k - 1) % M)
		continue
	val = (k - 1) // (n - 1)
	a = k - 1
	d = n - 1
	if (k - 1) % (n - 1):
		val += 1
	sm = val * (2 * a - (val - 1) * d)
	ans = sm // 2
	print(ans % M)


import math
t = input()
MOD = 1000000007
for T in range(int(t)):
	inp = input()
	N = int(inp.split()[0])
	K = int(inp.split()[1])
	result = 0
	result = result + max(K - 1, 0)
	d = N - 1
	firstDifference = K - N
	num = firstDifference // d + 1
	if K > N:
		lastDifference = firstDifference - (num - 1) * d
		result = result + num * (firstDifference + lastDifference) // 2
	print(result % MOD)


t = int(input())
mod = 10 ** 9 + 7
s = pow(2, mod - 2, mod)
for i in range(t):
	(n, k) = map(int, input().split())
	if k == 1:
		print(0)
	elif k == 2:
		print(1)
	elif n == 2:
		num = (k - 1) % mod * (k % mod) * s
		num = num % mod
		print(num)
	elif k == 3:
		print(2)
	elif n >= k:
		print((k - 1) % mod)
	else:
		inv = pow(n - 1, mod - 2, mod)
		pos = (k - 1) % (n - 1)
		if pos == 0:
			pos = n - 1
		add = (k - 1) // (n - 1) % mod
		if pos != n - 1:
			add += 1
		add = add % mod * (pos % mod) % mod
		groups = (k % mod - pos % mod - 1) % mod * inv % mod
		sm = groups * ((groups + 1) % mod) * s
		sm = sm % mod
		sm = sm * ((n - 1) % mod)
		sm = sm % mod
		final = sm + add
		final = final % mod
		print(final)


import math
import bisect

def power(x, y, p):
	res = 1
	x = x % p
	while y > 0:
		if y & 1 == 1:
			res = res * x % p
		y = y >> 1
		x = x * x % p
	return res

def inn():
	return int(input())

def inl():
	return list(map(int, input().split()))
MOD = 10 ** 9 + 7
INF = inf = 10 ** 18 + 5
for t in range(int(input())):
	(n, k) = inl()
	a = k - 1
	d = n - 1
	m = a // d + 1
	ans = m * (2 * a - (m - 1) * d) // 2 % MOD
	print(ans)


q = int(input())
for i in range(0, q):
	(a, b) = map(int, input().split())
	l = b + a - 2
	pl = l // (a - 1)
	ans = pl * (2 * b - 2 + (pl - 1) * (1 - a)) // 2
	ans = ans % 1000000007
	print(ans)


t = int(input())
while t:
	(na, k) = input().split()
	na = int(na)
	k = int(k)
	m = 10 ** 9 + 7
	a = k - 1
	d = na - 1
	n = a // d + 1
	sum = n * (2 * a - (n - 1) * d)
	sum = sum // 2
	print(sum % m)
	t -= 1


mod = 10 ** 9 + 7
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	if n >= k:
		ans = (k - 1) % mod
	else:
		start = k - 1
		diff = n - 1
		terms = start // diff
		if start % diff != 0:
			terms = terms + 1
		p2 = (2 * start - (terms - 1) * diff) * terms // 2
		ans = p2 % mod
	print(ans)


from sys import stdin, stdout
import math
MOD = 1000000007
t = input()
t = int(t)
while t > 0:
	t = t - 1
	(n, k) = map(int, input().strip().split())
	if (k - 1) % (n - 1) == 0:
		final = n - 1
	else:
		final = (k - 1) % (n - 1)
	d = (k - 1 - final) // (n - 1) + 1
	ans = d * (k - 1 + final) % MOD % MOD * 500000004 % MOD
	print(int(ans))


for _ in range(int(input())):
	(n, k) = [int(i) for i in input().split()]
	(l, r) = (k, k + n - 1)
	m = (l - 1) // (r - l)
	ans = ((m + 1) * (m + 2) * l - 2 - 2 * m - m * (m + 1) * r) // 2
	print(ans % (10 ** 9 + 7))


from math import *
mod = 10 ** 9 + 7

def find_x(n, k):
	x = (k - 1) // (n - 1)
	return x
tc = int(input())
while tc:
	(n, k) = input().split()
	n = int(n)
	k = int(k)
	if n > k:
		ans = (k - 1) % mod
	else:
		x = int(find_x(n, k))
		ans = k - 1 + x * k - x * (x + 1) // 2 * n + x * (x - 1) // 2
		ans %= mod
	print(ans)
	tc -= 1


t = int(input())
while t:
	t = t - 1
	m = 1000000007
	(n, k) = list(map(int, input().split()))
	l = k - 1
	d = n - 1
	if l % d == 0:
		nt = l // d
		f = d
	else:
		f = l % d
		nt = l // d + 1
	if nt % 2 == 0:
		n1 = nt // 2
		ans = (2 % m * (f % m) % m + (nt - 1) % m * (d % m) % m) % m * (n1 % m) % m
	else:
		n1 = (nt - 1) // 2
		ans = (f % m + n1 % m * (d % m) % m) % m * (nt % m) % m
	print(ans)


from math import *
mod = 1000000007
t = int(input())
while t:
	t = t - 1
	(n, k) = input().split()
	n = int(n) - 1
	k = int(k) - 1
	y = k // n
	tot = (1 + y) * k - n * y * (y + 1) // 2
	print(tot % mod)


import math
MOD = 1000000000 + 7

def myCeiling(a, b):
	d = a // b
	if d * b < a:
		return d + 1
	else:
		return d

def getMissing(n, k):
	d = myCeiling(k, n - 1)
	ans = d * (k + n - 2) - (n - 1) * d * (d + 1) // 2
	return ans % MOD
tc = int(input())
for t in range(tc):
	inp = input().split()
	n = int(inp[0])
	k = int(inp[1])
	ans = getMissing(n, k)
	print(ans)


t = int(input())
for i in range(t):
	(n, k) = map(int, input().split())
	if k <= n:
		print((k - 1) % 1000000007)
	else:
		x = (k - 1) // (n - 1)
		print((k - 1 + int(x * k - n * x * (x + 1) // 2 + x * (x - 1) // 2)) % 1000000007)


def ti():
	return tuple(map(int, input().split()))

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

def si():
	return input().split()

def ii():
	return int(input())

def ip():
	return input()
for tastcas in range(int(input())):
	(n, k) = li()
	nn = n - 1
	kk = k - 1
	x = (kk - n) // nn + 1
	xx = x - 1
	inf = 1000000007
	if n >= k:
		print((k - 1) % inf)
	else:
		print((kk + (k - n) * x - nn * x * xx // 2) % inf)


for i in range(int(input())):
	(n, k) = input().split()
	n = int(n)
	k = int(k)
	a = k - 1
	d = n - 1
	num = a // d + 1
	sum = num * (2 * a - (num - 1) * d) // 2
	print(sum % 1000000007)


t = int(input())
mod = 10 ** 9 + 7
for _ in range(t):
	(n, k) = map(int, input().split())
	if n - 1 >= k - 1:
		print((k - 1) % mod)
	else:
		a = k - 1
		d = n - 1
		n = a // d
		if a % d != 0:
			n = n + 1
		sum = n * (2 * a - (n - 1) * d)
		result = sum // 2
		print(result % mod)


t = int(input())
for _ in range(t):
	(n, k) = map(int, input().split())
	M = 1000000007
	first_term = k - 1
	differnce = 1 - n
	last_term = first_term % -differnce
	if last_term == 0:
		last_term = -differnce
	nth_digit = (last_term - first_term) // differnce + 1
	summ = nth_digit * (last_term + first_term) // 2 % M
	print(summ)


T = int(input())
for t in range(T):
	A = list(map(int, input().split()))
	(N, K) = (A[0], A[1])
	if K == 1:
		print(0)
	elif N == 2:
		a = K % 1000000007
		b = (K - 1) % 1000000007
		if a == 0 or b == 0:
			print(0)
		else:
			print(a * b // 2 % 1000000007)
	else:
		d = N - 1
		l = K - 1
		a = l % d
		n = (l - a) // d + 1
		s = (a + l) * n // 2
		print(s % 1000000007)