Chandler wants to give Joey money as Joey is going through some financial struggles. He believes 1500 will be enough to pay off the bills. Chandler makes up a game on the spot called "Cups", which has no rules. Every move Joey makes in the "game", Chandler says that he wins a lot of money. Joey, being "smart", finds this out and thus wants to set some rules.

Joey and Chandler have a set of cards, where the value of a card is defined as the product of all the digits of the number printed on that card.

For example, value of the card with the number 7 equals 7, value of the card with the number 203 equals 2 \cdot 0 \cdot 3 = 0.

The initial number on Joey's card is N (without any leading zeroes). Chandler can make at most K changes on the digits of the number before it gets noticed. In one change, he can choose any digit x (x ≠ 9) of the number printed on card and replace it with x+1. 
Find the maximum possible value of Joey's card after making the changes.

------ Input Format ------ 

- First line will contain T, number of testcases. Then the testcases follow.
- For each test case, given two integer N and K, denoting the initial number on Joey's card and the number of changes Chandler can make.

------ Output Format ------ 

For each testcase, output in a single line the maximum possible value of Joey's card after making the changes.

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

$1 ≤ T ≤ 10^{3}$
$1 ≤ N ≤ 10^{6}$
$0 ≤ K ≤ 10^{6}$

----- Sample Input 1 ------ 
3
1 5
2221 3
123456 0
----- Sample Output 1 ------ 
6
36
720
----- explanation 1 ------ 
Test case 1: Chandler can increase the digit $1$ five times to make the final number on card $6$.

Test case 2: Chandler can increase the first digit of the given number once and the last digit twice. After making these changes, the final number on the card would be $3223$, whose value is equal to $36$. There are other ways to make changes on the printed number which may lead to one of the following numbers being the resulting number on card : $2233$, $2323$, $2332$, $3232$, $3322$; note that all these numbers have their values equal to $36$ as well.

Source: codechef | Difficulty: MEDIUM
Tags: Mathematics

Implementation


Solutions:
kk = 100
a = int(input())
for i in range(a):
	(n, t) = map(int, input().split())
	len = [int(x) for x in str(n)]
	i = 0
	pop = 1
	while i < t:
		b = min(len)
		if b == 9:
			break
		else:
			a = len.index(b)
			len[a] = b + 1
			i += 1
	for j in len:
		pop *= j
	print(pop)


for _ in range(int(input())):
	(a, b) = map(int, input().split())
	aa = list(str(a))
	aa = ' '.join(aa)
	x = list(map(int, aa.split()))
	x.sort()
	for i in range(b):
		k = min(x)
		if k != 9:
			x.remove(k)
			k += 1
			x.append(k)
	p = 1
	for i in x:
		p *= i
	print(p)


t = int(input())
while t:
	t -= 1
	(n, k) = map(int, input().split())
	a = []
	for i in str(n):
		a.append(int(i))
	a.sort()
	ans = 1
	while k and a[0] != 9:
		a[0] += 1
		k -= 1
		a.sort()
	for i in a:
		ans *= i
	print(ans)


t = int(input())
for _ in range(t):
	(n, k) = map(int, input().split())
	x = [int(a) for a in str(n)]
	x.sort()
	while k > 0:
		if x[0] == 9:
			break
		x[0] += 1
		x.sort()
		k -= 1
	ans = 1
	for i in x:
		ans *= i
	print(ans)


for tc in range(int(input())):
	(n, k) = map(int, input().split(' '))
	l = list(str(n))
	l = list(map(int, l))
	while k > 0:
		m = l.index(min(l))
		if l[m] >= 9:
			break
		l[m] += 1
		k -= 1
	s = 1
	for i in l:
		s *= i
	print(s)


for i in range(int(input())):
	(n, k) = map(int, input().split())
	N = str(n)
	num = []
	for i in N:
		num.append(int(i))
	num.sort()
	while k > 0:
		if num[0] == 9:
			break
		num[0] += 1
		num.sort()
		k -= 1
	ans = 1
	for i in num:
		ans *= i
	print(ans)


for i in range(int(input())):
	(a, b) = map(int, input().split())
	l = []
	while a > 0:
		i = a % 10
		l.append(i)
		a //= 10
	l.sort()
	c = 1
	d = len(l)
	for i in range(b):
		if l[0] == 9:
			break
		if d >= b:
			l[0] += 1
		else:
			l[0] += 1
		l.sort()
	for i in range(d):
		c *= l[i]
	print(c)


import numpy as np
t = int(input())
for _ in range(t):
	(n, k) = map(int, input().split())
	ns = list(map(int, list(str(n))))
	ns.sort()
	s = 0
	for _ in range(k):
		if ns[s] < 9:
			ns[s] += 1
		if s == len(ns) - 1:
			s = 0
		elif ns[s + 1] >= ns[s]:
			s = 0
		else:
			s += 1
	print(np.prod(ns))


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	l = []
	while n != 0:
		r = n % 10
		n = n // 10
		l.append(r)
	for i in range(k):
		m = min(l)
		if m == 9:
			break
		l[l.index(m)] += 1
	s = 1
	for j in l:
		s *= j
	print(s)


t = int(input())
c = 1
E = []
for a in range(t):
	(A, B) = map(int, input().split())
	C = list(map(int, str(A)))
	for i in range(B):
		minimum = min(C)
		if minimum == 9:
			break
		L = C.index(minimum)
		J = C[L]
		J = int(J) + 1
		C[L] = J
	for k in C:
		c *= int(k)
	print(c)
	c = 1


numTest = int(input())
for j in range(numTest):
	(n, k) = map(int, input().split())
	arr = list(map(int, str(n)))
	prod = 1
	for j in range(k):
		mini = min(arr)
		if mini == 9:
			break
		arr[arr.index(mini)] += 1
	for j in arr:
		prod *= j
	print(prod)


numTest = int(input())
for _ in range(numTest):
	(n, k) = map(int, input().split())
	arr = list(map(int, str(n)))
	prod = 1
	curr = None
	idx = 0
	for i in range(k):
		mini = min(arr)
		if mini == 9:
			break
		arr[arr.index(mini)] += 1
	for i in arr:
		prod *= i
	print(prod)


for i in range(int(input())):
	(N, K) = input().split()
	n = list(map(int, list(N)))
	k = int(K)
	for _ in range(k):
		mi = min(n)
		if mi == 9:
			break
		else:
			n[n.index(mi)] += 1
	product = 1
	for m in n:
		product *= m
	print(product)


testCases = int(input())
for i in range(testCases, 0, -1):
	(num, k) = map(int, input().split())
	digits = [int(a) for a in str(num)]
	for i in range(k):
		if min(digits) == 9:
			break
		else:
			digits[digits.index(min(digits))] = min(digits) + 1
	prod = 1
	for i in digits:
		prod *= i
	print(prod)


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	li = [0 for i in range(10)]
	sn = str(n)
	for i in sn:
		li[int(i)] += 1
	for i in range(k):
		for j in range(len(li)):
			if li[j] > 0 and j != 9:
				li[j] -= 1
				li[j + 1] += 1
				break
	val = 1
	for i in range(len(li)):
		if li[i] != 0:
			val *= i ** li[i]
	print(val)


from heapq import *

def solve():
	(s, k) = list(map(int, input().split()))
	s = str(s)
	pq = []
	for i in s:
		heappush(pq, int(i))
	for i in range(k):
		x = heappop(pq)
		if x == 9:
			x -= 1
		x += 1
		heappush(pq, x)
	prod = 1
	for i in pq:
		prod *= i
	return prod
tc = int(input())
for t in range(tc):
	print(solve())


T = int(input(''))
for i in range(T):
	l1 = [int(c) for c in input('').split()]
	a = l1[1]
	num = [int(x) for x in str(l1[0])]
	mydict = {0: 0, 1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 0, 7: 0, 8: 0, 9: 0}
	for digit in num:
		mydict[digit] += 1
	while a > 0:
		for digit in mydict:
			if mydict[digit] > 0 and digit != 9:
				mydict[digit] -= 1
				mydict[digit + 1] += 1
				a -= 1
				break
			elif digit == 9:
				a = 0
				break
	prod = 1
	for digit in mydict:
		if mydict[digit] != 0:
			k = mydict[digit]
			while k > 0:
				prod *= digit
				k -= 1
	print(prod)


def ans(s, k):
	k = int(k)
	arr = list(map(int, s))
	while k != 0:
		pos = arr.index(min(arr))
		if arr[pos] == 9:
			break
		else:
			arr[pos] += 1
		k -= 1
	a = 1
	for y in arr:
		a *= y
	return a
test_cases = int(input())
while test_cases != 0:
	d = list(input().split())
	print(ans(d[0], d[len(d) - 1]))
	test_cases -= 1


from math import ceil
t = int(input())
while t != 0:
	t = t - 1
	(n, k) = input().split()
	k = int(k)
	a = list()
	for i in n:
		a.append(i)
	a.sort()
	if a[0] == '9' or k == 0:
		m1 = 1
		for i in a:
			m1 = m1 * int(i)
		print(m1)
	elif len(a) == 1:
		if int(a[0]) + k > 9:
			print('9')
		else:
			print(int(a[0]) + k)
	else:
		ss = 0
		ind = len(a) - 1
		for i in range(len(a) - 1):
			if ss + (int(a[i + 1]) - int(a[i])) * (i + 1) >= k or a[i + 1] == '9':
				ind = i
				break
			else:
				ss = ss + (int(a[i + 1]) - int(a[i])) * (i + 1)
		for i in range(ind):
			k = k + int(a[i]) - int(a[ind])
			a[i] = int(a[ind])
		for i in range(ind + 1):
			if k <= 0:
				break
			if int(a[i]) + ceil(k / (ind + 1 - i)) <= 9:
				a[i] = int(a[i]) + ceil(k / (ind + 1 - i))
				k = k - ceil(k / (ind + 1 - i))
			else:
				k = k - (9 - int(a[i]))
				a[i] = 9
		ans = 1
		for i in a:
			ans = ans * int(i)
		print(ans)


for _ in range(int(input())):
	(a, n) = map(int, input().split())
	num = a
	l = []
	p = 1
	while num != 0:
		l.append(num % 10)
		num //= 10
	for i in range(n):
		l.sort()
		if l[0] == 9:
			continue
		else:
			ne = l[0] + 1
			l.pop(0)
			l.append(ne)
	for i in l:
		p *= i
	print(p)


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	arr = list(str(n))
	arr = [int(arr[i]) for i in range(len(arr))]
	sm = 0
	for i in range(len(arr)):
		sm += 9 - arr[i]
	if k >= sm:
		print(9 ** len(arr))
		continue
	arr.sort()
	for i in range(len(arr)):
		if i == len(arr) - 1:
			m = i
			while k > 0:
				if k == 0:
					break
				arr[m] += 1
				k -= 1
				m -= 1
				if m == -1:
					m = i
			break
		if arr[i] != arr[i + 1]:
			m = i
			while m >= 0 and k > 0 and (arr[m] < arr[i + 1]):
				if arr[m] + 1 > arr[i + 1]:
					break
				if k == 0:
					break
				arr[m] += 1
				k -= 1
				m -= 1
				if m == -1:
					m = i
	prod = 1
	for i in range(len(arr)):
		prod = prod * arr[i]
	print(prod)


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	a = str(n)
	b = list(a)
	c = []
	for i in b:
		c.append(int(i))
	for i in range(k):
		c.sort()
		if c[0] == 9:
			pass
		else:
			l = c[0] + 1
			c.pop(0)
			c.append(l)
	p = 1
	for i in range(len(c)):
		p = p * c[i]
	print(p)


t = int(input())
for i in range(t):
	(n, k) = [int(x) for x in input().split()]
	d = [int(x) for x in list(str(n))]
	d.sort()
	l = {}
	for j in range(10):
		l[j] = 0
	for j in d:
		l[j] += 1
	while k > 0:
		for j in l:
			if j < 9 and k > 0 and l[j]:
				if l[j] >= k:
					l[j] -= k
					l[j + 1] += k
					k = 0
				else:
					k -= l[j]
					l[j + 1] += l[j]
					l[j] = 0
		k -= 1
	ans = 1
	for i in l:
		ans = ans * i ** l[i]
	print(ans)


t = int(input())
for test in range(t):
	(n, k) = [int(x) for x in input().split()]
	digits = [int(x) for x in list(str(n))]
	digits.sort()
	arr = {}
	for i in range(10):
		arr[i] = 0
	for i in digits:
		arr[i] += 1
	while k > 0:
		for i in arr:
			if i < 9 and k > 0 and arr[i]:
				if arr[i] >= k:
					arr[i] -= k
					arr[i + 1] += k
					k = 0
				else:
					k = k - arr[i]
					arr[i + 1] += arr[i]
					arr[i] = 0
		k -= 1
	ans = 1
	for i in arr:
		ans = ans * i ** arr[i]
	print(ans)


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	num = n
	arr = []
	mul = 1
	while num != 0:
		arr.append(num % 10)
		num = num // 10
	for i in range(k):
		arr.sort()
		if arr[0] == 9:
			pass
		else:
			new = arr[0] + 1
			arr.pop(0)
			arr.append(new)
	for i in range(len(arr)):
		mul = mul * arr[i]
	print(mul)


for i in range(int(input())):
	(n, k) = input().split(' ')
	n = [int(i) for i in n]
	k = int(k)
	for i in range(k):
		a = min(n)
		if a == 9:
			break
		else:
			b = n.index(a)
			n[b] += 1
	ans = 1
	for i in n:
		ans *= i
	print(ans)


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	l = []
	s = str(n)
	for i in range(len(s)):
		p = int(s[i])
		l.append(p)
	while k > 0:
		l.sort()
		if l[0] != 9:
			l[0] += 1
		k = k - 1
	ans = 1
	for i in range(len(l)):
		ans = ans * l[i]
	print(ans)


def ans(n, k):
	import heapq
	heap = []
	while n:
		heapq.heappush(heap, n % 10)
		n = n // 10
	while k and heap[0] != 9:
		x = heapq.heappop(heap)
		x += 1
		k -= 1
		heapq.heappush(heap, x)
	ans = 1
	for i in heap:
		ans *= i
	print(ans)
n = int(input())
for i in range(n):
	z = [int(i) for i in input().split()]
	ans(z[0], z[1])


for i in range(int(input())):
	(n, k) = map(int, input().split())
	pos = 0
	digits = list((int(j) for j in str(n)))
	count = 0
	digits.sort()
	while count != k:
		if min(digits) == 9:
			break
		m = digits.index(min(digits))
		digits[m] += 1
		count += 1
	product = 1
	for k in digits:
		product *= k
	print(product)


for i in range(int(input())):
	(n, k) = map(int, input().split())
	x = []
	while n > 0:
		x.append(n % 10)
		n //= 10
	while k:
		m = x.index(min(x))
		if min(x) == 9:
			break
		x[m] += 1
		k -= 1
	c = 1
	for i in range(len(x)):
		c *= x[i]
	print(c)


import numpy
import heapq
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	num = [int(i) for i in str(n)]
	for t in range(k):
		heapq.heapify(num)
		min_num = heapq.heappop(num)
		if min_num is 9:
			heapq.heappush(num, 9)
			break
		else:
			heapq.heappush(num, min_num + 1)
			k -= 1
	print(numpy.prod(list(num)))


for i in range(int(input())):
	(n, k) = input().split()
	k = int(k)
	n = list((int(i) for i in n))
	n.sort()
	c = 1
	for i in range(k):
		if n[0] != 9:
			n[0] += 1
			n.sort()
	for i in n:
		c *= i
	print(c)


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	s = list(map(int, str(n)))
	for i in range(k):
		s.sort()
		if s[0] == 9:
			break
		else:
			s[0] += 1
	ans = 1
	for i in s:
		ans *= i
	print(ans)


for i in range(int(input())):
	N = []
	K = 0
	prod = 1
	(N, K) = map(int, input().strip().split())
	N = str(N)
	N = list(map(int, N))
	if len(N) == 1 and N[0] != 9:
		if N[0] + K >= 9:
			prod = 9
		else:
			prod = N[0] + K
		print(prod)
	else:
		for x in range(K):
			N.sort()
			if N[0] != 9:
				N[0] = N[0] + 1
		for j in range(len(N)):
			prod = prod * N[j]
		print(prod)


t = int(input())
for i in range(t):
	f = 1
	(n, k) = map(str, input().split())
	k = int(k)
	l = list(n)
	l = list(map(int, l))
	for i in range(0, k):
		l.sort()
		if l[0] < 9:
			l[0] = l[0] + 1
		else:
			pass
	for j in l:
		f = f * j
	print(f)


t = int(input())
while t > 0:
	(n, k) = input().split()
	k = int(k)
	ans = 1
	list = [int(i) for i in n]
	while k > 0:
		list.sort()
		if list[0] < 9:
			list[0] += 1
		else:
			k = -1
		k -= 1
	for i in list:
		ans *= i
	print(ans)
	t -= 1


from heapq import heapify, heappop, heappush
for _ in range(int(input())):
	(n, k) = map(str, input().split())
	n = list(n)
	k = int(k)
	heapify(n)
	ans = 1
	for i in range(k):
		e = heappop(n)
		if e == '9':
			heappush(n, e)
			break
		p = str(int(e) + 1)
		heappush(n, p)
	for i in range(len(n)):
		ans *= int(n[i])
	print(ans)


from itertools import permutations as per
from math import factorial as fact
from difflib import SequenceMatcher
t = int(input())
for _ in range(t):
	(a, k) = input().split()
	d = {}
	for i in a:
		if i in d.keys():
			d[i] += 1
		else:
			d[i] = 1
	for i in range(10):
		if str(i) not in d.keys():
			d[str(i)] = 0
	i = 0
	k = int(k)
	while i < 9 and k != 0:
		if d[str(i)] <= k:
			d[str(i + 1)] += d[str(i)]
			k -= d[str(i)]
			d[str(i)] = 0
		else:
			d[str(i + 1)] += k
			d[str(i)] -= k
			k = 0
		i += 1
	ans = 1
	for (k, v) in d.items():
		if v > 0:
			ans *= int(k) ** v
	print(ans)


import heapq
for _ in range(int(input())):
	(n, k) = map(str, input().split())
	n_str = list(n)
	k = int(k)
	heapq.heapify(n_str)
	for i in range(k):
		num = heapq.heappop(n_str)
		if num == '9':
			heapq.heappush(n_str, num)
			break
		incr = str(int(num) + 1)
		heapq.heappush(n_str, incr)
	ans = 1
	for i in range(len(n_str)):
		ans *= int(n_str[i])
	print(ans)


import math, os, io, heapq
from sys import stdin, stdout
from collections import Counter, deque, defaultdict
from itertools import product
import bisect

def getList(method=int):
	return list(map(method, input().split()))

def getInt():
	return int(input())

def solve():
	(n, k) = getList()
	mp = defaultdict(lambda : 0)
	while n:
		mp[n % 10] += 1
		n //= 10
	if mp[0] > k:
		return 0
	k -= mp[0]
	mp[1] += mp[0]
	for i in range(1, 9):
		v = min(mp[i], k)
		mp[i] -= v
		mp[i + 1] += v
		k -= v
		if k == 0:
			break
	ans = 1
	for i in range(1, 10):
		if mp[i] > 0:
			ans *= pow(i, mp[i])
	return ans
for _ in range(getInt()):
	print(solve())


from functools import reduce
t = int(input())
for test_case in range(t):
	(n, k) = map(int, input().split())
	digits = sorted([int(i) for i in str(n)])
	for i in range(k):
		if digits[0] == 9:
			pass
		else:
			digits[0] += 1
			digits.sort()
	mul = lambda x, y: x * y
	product = reduce(mul, digits)
	print(product)


for _ in range(int(input())):
	(s, k) = input().split()
	k = int(k)
	arr = []
	maxx = 1
	for i in s:
		arr.append(int(i))
	arr.sort()
	n = len(arr)
	while k > 0:
		if arr[0] != 9:
			arr[0] += 1
			k -= 1
			arr.sort()
		else:
			break
	ans = 1
	for i in arr:
		ans *= i
	print(ans)


t = int(input())
out_l = []
for _ in range(t):
	(num, k) = tuple(map(str, input().split(' ')))
	num = list(map(int, list(num)))
	k = int(k)
	num.sort()
	counter = 0
	while counter < k:
		index = 0
		to_break = True
		if num[index] != 9:
			num[index] = num[index] + 1
			counter = counter + 1
			num.sort()
			to_break = False
		if to_break == True:
			counter = counter + 1
	mul = 1
	for ele in num:
		mul *= ele
	out_l.append(mul)
for ele in out_l:
	print(ele)


for t in range(int(input())):
	(n, k) = input().split()
	n = [int(x) for x in n]
	k = int(k)
	while k > 0:
		n.sort()
		if n[0] == 9:
			break
		n[0] += 1
		k -= 1
	ans = 1
	for x in n:
		ans *= x
	print(ans)


from sys import stdin
import heapq
input = stdin.readline
t = int(input())
for _ in range(t):
	(n, k) = [int(x) for x in input().split()]
	a = []
	for s in str(n):
		a.append(int(s))
	heapq.heapify(a)
	for i in range(k):
		x = heapq.heappop(a)
		if x == 9:
			heapq.heappush(a, x)
			break
		heapq.heappush(a, x + 1)
	ans = 1
	for v in a:
		ans *= v
	print(ans)


def value(n):
	res = 1
	for i in n:
		res *= int(i)
	return res
for _ in range(int(input())):
	(a, b) = map(int, input().split())
	if b == 0:
		a = list(str(a))
		print(value(a))
	else:
		a = list(str(a))
		for i in range(b):
			d = a.index(min(a))
			if int(a[d]) != 9:
				a[d] = int(a[d]) + 1
				a[d] = str(a[d])
			else:
				break
		print(value(a))


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	lis = []
	while n > 0:
		lis.append(n % 10)
		n = n // 10
	for i in range(k):
		lis.sort()
		if lis[0] == 9:
			pass
		else:
			val = lis[0] + 1
			lis.pop(0)
			lis.append(val)
	mul = 1
	for i in lis:
		mul *= i
	print(mul)


import heapq
t = int(input())
for _ in range(t):
	(n, k) = map(int, input().split())
	num = n
	arr = []
	while num != 0:
		dig = num % 10
		num = num // 10
		arr.append(dig)
	leni = len(arr)
	heapq.heapify(arr)
	while k:
		num = arr[0]
		if num == 9:
			break
		else:
			heapq.heappop(arr)
			num += 1
			k -= 1
			heapq.heappush(arr, num)
	ans = 1
	for i in range(leni):
		ans = ans * arr[i]
	print(ans)


T = int(input())
for t in range(T):
	(n, k) = [int(i) for i in input().split()]
	s = list(str(n))
	n = len(s)
	for thres in range(1, 10):
		for j in range(n):
			if k == 0:
				break
			if int(s[j]) < thres:
				s[j] = str(int(s[j]) + 1)
				k -= 1
	ans = 1
	for i in s:
		ans *= int(i)
	print(ans)


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	n = [int(i) for i in str(n)]
	n.sort()
	ans = 1
	for i in range(k):
		if n[0] != 9:
			n[0] = n[0] + 1
			n.sort()
	for i in n:
		ans *= i
	print(ans)


import numpy
for _ in range(int(input())):
	(n, k) = input().split()
	n = list(map(int, list(n)))
	k = int(k)
	while k != 0:
		temp = n.index(min(n))
		if n[temp] != 9:
			n[temp] += 1
		k -= 1
	print(numpy.prod(n))


def SieveOfEratosthenes(n):
	prime = [True for i in range(n + 1)]
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p ** 2, n + 1, p):
				prime[i] = False
		p += 1
	prime[0] = False
	prime[1] = False
	for p in range(n + 1):
		if prime[p]:
			print(p)
for _ in range(int(input())):
	(n, k) = list(map(int, input().split()))
	d = []
	while n:
		d.append(n % 10)
		n //= 10
	d.sort()
	while k > 0 and d[0] != 9:
		d[0] += 1
		k -= 1
		d.sort()
	ans = 1
	for i in range(len(d)):
		ans *= d[i]
	print(ans)


t = int(input())
for _ in range(t):
	(n, k) = map(int, input().split())
	l = []
	le = 0
	while n != 0:
		r = n % 10
		n //= 10
		l.append(r)
		le += 1
	for i in range(9):
		for j in range(le):
			if k > 0:
				if l[j] == i:
					l[j] += 1
					k -= 1
			else:
				break
	ans = 1
	for x in l:
		ans *= x
	print(ans)


import heapq
t = int(input())
for i in range(t):
	(n, k) = map(int, input().split())
	st = str(n)
	lis = list(map(int, st))
	num = 1
	lis1 = []
	for j in lis:
		if j == 9:
			num *= j
		else:
			lis1.append(j)
	heapq.heapify(lis1)
	j = 0
	while j < k:
		if len(lis1) == 0:
			break
		temp = heapq.heappop(lis1)
		j += 1
		if temp + 1 == 9:
			num *= 9
		else:
			heapq.heappush(lis1, temp + 1)
	while len(lis1) > 0:
		num = num * heapq.heappop(lis1)
	print(num)


t = int(input())

def multiplyList(myList):
	result = 1
	for x in myList:
		result = result * x
	return result
for i in range(0, t):
	(n, k) = [int(x) for x in input().split()]
	arr = [int(x) for x in str(n)]
	for i in range(0, k):
		temp = arr
		m = min(temp)
		if m == 9:
			arr = temp
			break
		minpos = temp.index(min(temp))
		temp[minpos] += 1
		arr = temp
	print(multiplyList(arr))


def check(n, k):
	l = []
	val = str(n)
	for i in val:
		l.append(int(i))
	j = 0
	while j != 1:
		if k == 0:
			j = 1
			break
		l.sort()
		mn = l[0]
		if mn == 9:
			j = 1
			break
		l.remove(l[0])
		l.append(mn + 1)
		k -= 1
	data = 1
	for i in range(len(l)):
		data = data * l[i]
	return data
t = int(input())
for _ in range(t):
	(n, k) = map(int, input().split())
	print(check(n, k))


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	l = [int(x) for x in str(n)]
	m = 0
	c = 1
	for j in range(k):
		m = min(l)
		if m == 9:
			pass
		else:
			i = l.index(min(l))
			l[i] += 1
			m = l[i]
	for x in l:
		c *= x
	print(c)


t = int(input())
for i in range(t):
	r = 1
	(n, k) = map(int, input().split())
	n = str(n)
	l = []
	for i in n:
		l.append(int(i))
	while k >= 1:
		m = min(l)
		if m != 9:
			l.remove(m)
			m += 1
			l.append(m)
		k -= 1
	for i in l:
		r *= i
	print(r)


import sys
for _ in range(int(sys.stdin.readline())):
	(n, k) = map(int, sys.stdin.readline().split())
	a = str(n)
	l = []
	for i in range(0, len(a)):
		l.append(int(a[i]))
	while k > 0:
		x = min(l)
		if x != 9:
			l.remove(x)
			x += 1
			l.append(x)
		k -= 1
	prod = 1
	for j in l:
		prod = j * prod
	print(prod)


for i in range(int(input())):
	(n, k) = map(int, input().split())
	l = [int(i) for i in str(n)]
	c = 0
	s = k
	for i in range(k):
		if min(l) == 9:
			break
		else:
			k = min(l)
			l.remove(k)
			l.append(k + 1)
	ans = 1
	for i in l:
		ans *= i
	print(ans)


for _ in range(int(input())):
	(n, k) = map(str, input().split())
	a = list(map(int, n))
	for i in range(int(k)):
		j = a.index(min(a))
		if a[j] != 9:
			a[j] += 1
	p = 1
	for i in a:
		p *= i
	print(p)


T = int(input())
for i in range(T):
	(n, k) = map(int, input().split(' '))
	lst = list(map(int, str(n)))
	for j in range(k):
		minimum = min(lst)
		ind = lst.index(minimum)
		if minimum != 9:
			lst[ind] = minimum + 1
	product = 1
	for x in lst:
		product = product * x
	print(product)


for i in range(int(input())):
	(n, k) = map(int, input().split())
	n = str(n)
	N = []
	for l in n:
		N.append(int(l))
	for j in range(k):
		m = min(N)
		N.remove(m)
		if m != 9:
			m += 1
			N.append(m)
		else:
			N.append(m)
	res = 1
	for w in N:
		res = res * w
	print(res)


import functools
for q in range(int(input())):
	(n, k) = input().split()
	li = [int(x) for x in n]
	k = int(k)
	for i in range(k):
		if li[li.index(min(li))] < 9:
			li[li.index(min(li))] += 1
	sol = functools.reduce(lambda a, b: a * b, li)
	print(sol)


import heapq
for _ in range(int(input())):
	(n, k) = input().split()
	k = int(k)
	digits = []
	for i in range(len(n)):
		digits.append(int(n[i]))
	heapq.heapify(digits)
	while k != 0:
		p = heapq.heappop(digits)
		if p != 9:
			heapq.heappush(digits, p + 1)
			k = k - 1
		else:
			heapq.heappush(digits, 9)
			break
	s = 1
	for i in digits:
		s = s * i
	print(s)


for _ in range(int(input())):
	(N, K) = input().split()
	K = int(K)
	N = [int(i) for i in N]
	for i in range(1, 10):
		for j in range(len(N)):
			if N[j] < i:
				if K == 0:
					break
				N[j] = i
				K -= 1
	ans = 1
	for i in N:
		ans *= i
	print(ans)


for _ in range(int(input())):
	(n, k) = input().split()
	k = int(k)
	c = [0] * 10
	for i in n:
		c[ord(i) - ord('0')] += 1
	for i in range(0, 9):
		s = min(c[i], k)
		c[i + 1] += s
		c[i] -= s
		k -= s
	ans = 1
	for i in range(0, 10):
		ans *= pow(i, c[i])
	print(ans)


def ma(n):
	l = sorted([i for i in str(n)])
	for i in range(len(l)):
		if l[i] != '9':
			l[i] = str(int(l[i]) + 1)
			return l
	return l
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	n = str(n)
	while k > 0:
		l = ma(n)
		n = ''
		for i in range(len(l)):
			n += l[i]
		k -= 1
	ans = 1
	for i in range(len(n)):
		ans *= int(n[i])
	print(ans)


t = int(input())
for _ in range(t):
	(n, k) = input().split()
	k = int(k)
	p = int('9' * len(n))
	s = str(p - int(n))
	N = len(n)
	su = 0
	for i in s:
		su += int(i)
	d = []
	for i in n:
		d.append(int(i))
	i = 0
	while i < k and i < su:
		d[d.index(min(d))] += 1
		i += 1
	p = 1
	for i in d:
		p *= i
	print(p)


test = int(input())
for i in range(test):
	(n, k) = map(int, input().split())
	temp = n
	lst = []
	while temp != 0:
		dig = temp % 10
		temp = temp // 10
		lst.append(dig)
	while k != 0:
		mini = min(lst)
		if mini >= 9:
			break
		ind = lst.index(mini)
		lst.remove(mini)
		lst.insert(ind, mini + 1)
		k -= 1
	pro = 1
	for i in lst:
		pro *= i
	print(pro)


t = int(input())
l1 = []
sum = 1
for i in range(t):
	(n, k) = map(int, input().split())
	x = n
	while x > 0:
		d = x % 10
		l1.append(d)
		x = x // 10
	for i in range(k):
		y = min(l1)
		if y == 9:
			continue
		l1.remove(y)
		l1.append(y + 1)
	for i in l1:
		sum = sum * i
	print(sum)
	l1.clear()
	sum = 1


for j in range(int(input())):
	(n, k) = map(str, input().split())
	l = []
	for i in range(len(n)):
		l.append(int(n[i]))
	for h in range(int(k)):
		l.sort()
		if l[0] != 9:
			l[0] += 1
	c = 1
	for j in range(len(l)):
		c *= l[j]
	print(c)


T = int(input())
for _ in range(T):
	(N, K) = input().split()
	K = int(K)
	seq = []
	ans = 1
	for i in N:
		seq.append(int(i))
	for i in range(K):
		if min(seq) != 9:
			seq[seq.index(min(seq))] += 1
		else:
			break
	for i in seq:
		ans *= i
	print(ans)


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	l = sorted(list(str(n)))
	for i in range(k):
		if l[0] == '9':
			break
		else:
			l[0] = chr(ord(l[0]) + 1)
			l.sort()
	ans = 1
	for i in l:
		ans *= int(i)
	print(ans)


t = int(input())
while t > 0:
	(n, k) = map(int, input().split())
	arr = []
	num = n
	mul = 1
	while num > 0:
		rem = num % 10
		arr.append(rem)
		num //= 10
	for i in range(k):
		arr.sort()
		if arr[0] != 9:
			arr[0] += 1
	for i in range(0, len(arr)):
		mul = mul * arr[i]
	print(mul)
	t = t - 1