Read problems statements in Mandarin Chinese, Russian and Vietnamese as well. 

While studying representation of numbers in various bases, Chef discovered an interesting properties of some positive numbers, which Chef believed are somewhat easy to memorize. Chef called such positive numbers easy numbers.

These numbers had an interesting property: Let the base Chef is currently studying be b. Represent the number in base b (without leading zeros). For each prefix of length k (from 1 to number of digits in number in the base), the number formed by considering the digits of the prefix in base b is divisible by k.

For example, let base Chef is studying be 2. 

1 is an easy number as its prefix of length 1 (i.e. 1) is divisible by 1.
2 is also an easy number, as its prefix of length 1 (i.e. 1) is divisible by 1 and the prefix of length 2 (i.e. 2, represented as 10 in base 2) is also divisible by 2.
3 is not an easy number, as its prefix of length 2 (i.e. 3, represented as 11 in base 2) is not divisible by 2.

Now Chef finds these numbers easy and thinks that following question related to easy numbers will be easy for you too. He asks you to find the number of easy numbers represented in base b and consisting of exactly d digits. As the answer could be quite large, output it modulo 10^{9} + 7.

------ Input ------ 

First line of the input contains a single integer T denoting the number of test cases.
For each test case, there will be a single line containing two space separated integers b, d, denoting the base and number of digits respectively.

------ Output ------ 

For each test case, output a single integer denoting the number of easy numbers in base b consisting of d digits, modulo 10^{9} + 7.

------ Constraints ------ 

$1 ≤ T ≤ 100$
$2 ≤ b ≤ 16$
$1 ≤ d ≤ 10^{9}$

----- Sample Input 1 ------ 
2

2 2

10 3
----- Sample Output 1 ------ 
1

150
----- explanation 1 ------ 
Example case 1. There are 2 numbers in base 2 consisting of 2 digits. Those are 2 (represented as 10 in base 2) and 3 (represented as 11 in base 2). 2 is an easy number, whereas 3 is not (see the explanation about it in the problem statement). Hence the number of easy numbers consisting of 2 digits in base 2 are 1.

Source: codechef | Difficulty: VERY_HARD
Tags: String algorithms

Preprocessing

Complete search

Number theory

Mathematics

Data structures


Solutions:
def find(b):
	a = [0]
	if b < 2:
		return []
	v = [i for i in range(1, b)]
	i = 2
	while True:
		a.append(len(v))
		if i != b:
			v2 = []
			for x in v:
				t = i - x * b % i
				if t == i:
					t = 0
				while t < b:
					v2.append(x * b + t)
					t += i
			if len(v2) == 0:
				return a
			v = []
			for j in v2:
				v.append(j)
		else:
			for j in range(len(v)):
				v[j] *= b
		i += 1
t = 1
t = int(input())
ans = [find(i) for i in range(15)]
ans.append([0, 14, 105, 525, 1995, 5985, 15960, 34199, 64409, 107154, 171563, 233954, 296371, 342124, 368134, 368134, 368134, 324876, 281553, 222037, 171005, 122130, 84431, 54937, 34827, 20907, 12333, 7023, 3880, 2057, 1167, 564, 260, 129, 62, 20, 7, 1, 1])
ans.append([0, 15, 120, 640, 2560, 8192, 21888, 50030, 100060, 178236, 285074, 414662, 554018, 681778, 779164, 831886, 831886, 782347, 701717, 591178, 473607, 361604, 263114, 183069, 121870, 78013, 48065, 28497, 16204, 8852, 4742, 2457, 1216, 565, 259, 124, 46, 21, 10, 3])
for _test_ in range(t):
	(b, d) = map(int, input().split())
	if len(ans[b]) <= d:
		print(0)
	else:
		print(ans[b][d])


ans = [[], [], [0, 1, 1], [0, 2, 3, 3, 3, 2, 2], [0, 3, 6, 8, 8, 7, 4, 1], [0, 4, 10, 17, 21, 21, 21, 13, 10, 6, 4], [0, 5, 15, 30, 45, 54, 54, 49, 46, 21, 3, 1], [0, 6, 21, 49, 87, 121, 145, 145, 145, 121, 92, 56, 33, 20, 14, 7, 3, 1, 1], [0, 7, 28, 74, 148, 238, 324, 367, 367, 320, 258, 188, 122, 69, 37, 12, 6, 3], [0, 8, 36, 108, 252, 454, 706, 898, 1039, 1039, 1039, 869, 674, 473, 309, 216, 126, 66, 34, 18, 8, 5, 3], [0, 9, 45, 150, 375, 750, 1200, 1713, 2227, 2492, 2492, 2225, 2041, 1575, 1132, 770, 571, 335, 180, 90, 44, 18, 12, 6, 3, 1], [0, 10, 55, 202, 560, 1232, 2278, 3574, 4959, 6074, 6709, 6709, 6709, 5703, 4493, 3294, 2217, 1468, 872, 514, 282, 143, 65, 28, 14, 7, 2], [0, 11, 66, 264, 792, 1900, 3800, 6514, 9772, 13030, 15471, 16799, 16799, 15652, 13418, 10748, 8079, 5688, 3774, 2381, 1435, 817, 445, 226, 108, 45, 20, 3, 1], [0, 12, 78, 338, 1105, 2874, 6278, 11660, 19068, 27607, 36136, 42702, 46610, 46610, 46610, 41294, 33796, 25982, 18892, 13103, 8577, 5356, 3162, 1743, 944, 470, 250, 126, 63, 25, 10, 4, 2, 2, 2, 1], [0, 13, 91, 424, 1484, 4154, 9707, 19414, 29121, 42063, 59159, 75615, 88747, 95597, 95597, 89036, 80204, 65867, 51176, 37661, 26428, 17759, 11353, 6821, 4096, 2220, 1165, 605, 310, 139, 69, 27, 10, 6, 2, 1, 1, 1, 1, 1], [0, 14, 105, 525, 1995, 5985, 15960, 34199, 64409, 107154, 171563, 233954, 296371, 342124, 368134, 368134, 368134, 324876, 281553, 222037, 171005, 122130, 84431, 54937, 34827, 20907, 12333, 7023, 3880, 2057, 1167, 564, 260, 129, 62, 20, 7, 1, 1], [0, 15, 120, 640, 2560, 8192, 21888, 50030, 100060, 178236, 285074, 414662, 554018, 681778, 779164, 831886, 831886, 782347, 701717, 591178, 473607, 361604, 263114, 183069, 121870, 78013, 48065, 28497, 16204, 8852, 4742, 2457, 1216, 565, 259, 124, 46, 21, 10, 3]]
t = int(input())
for i in range(t):
	(r, n) = map(int, input().split())
	if n >= len(ans[r]):
		print(0)
	else:
		print(ans[r][n])