You were strolling outside the restaurant at the end of the universe. On a metaspiral path you stumble upon a weird device which takes a three-digit number as input and processes it. The Hitchhiker's guide to the galaxy explains that it processes the input in the following manner: 
- Multiplies it with 13, followed by 11 and then 7          
- Outputs all the distinct three-digit numbers possible from the digits of the new number (each digit can only be used once)          
Your friend Zaphod is in a playful mood, and does the following with the device-           
- Given a three-digit positive number $K$, he feeds it to the device for processing.          
- He then takes the numbers it gives as output, and send each of them through the device and again collect all the numbers sent out.          
- Repeats the above step $N$ times.          
To test your wit, he challenges you to find the number of distinct 3-digit numbers which the device outputs over the $N$ steps. Can you?

-----Input:-----
- First line will contain $T$, number of testcases. Then the testcases follow.
- Each testcase contains of a single line of input, two integers $K, N$.

-----Output:-----
For each testcase, output a single integer denoting the number of distinct 3-digit numbers which the device outputs over the $N$ steps.

-----Constraints-----
- $1 \leq T \leq 1000$ 
- $5 \leq N \leq 10^9$ 
- Each digit of $K$ is non-zero           

-----Sample Input:-----
1

123 5         

-----Sample Output:-----
27

Source: codechef | Difficulty: MEDIUM_HARD


Solutions:
for _ in range(int(input())):
	s = input().split()
	print(len(set(s[0])) ** 3)


T = int(input())
for _ in range(T):
	A = list(map(str, input().split()))
	l = A[0]
	d = dict()
	d[l[0]] = 0
	d[l[1]] = 0
	d[l[2]] = 0
	x = len(d.keys())
	print(x ** 3)


try:
	from sys import stdin as si, stdout as so
	for _ in range(int(si.readline())):
		(n, k) = si.readline().split()
		a = set(n)
		so.write(str(len(a) ** 3) + '\n')
except:
	pass


for i in range(int(input())):
	a = list(map(int, input().split()))
	l = list(map(int, str(a[0])))
	t = len(set(l))
	if t == 3:
		print('27')
	elif t == 2:
		print('8')
	else:
		print('1')


t = int(input())
while t > 0:
	t -= 1
	nk = list(map(int, input().split()))
	n = int(nk[0])
	k = int(nk[1])
	arr = [0] * 10
	while n > 0:
		temp = n % 10
		n = int(n / 10)
		arr[temp] = 1
	count = 0
	for i in range(10):
		if arr[i] == 1:
			count += 1
	print(count ** 3)


for _ in range(int(input())):
	(K, N) = map(int, input().split())
	K = str(K)
	l = [int(d) for d in K]
	while N >= 5:
		print(len(set(K)) ** 3)
		break


for T in range(int(input())):
	(k, n) = map(int, input().split())
	s = str(k)
	if s[0] == s[1] and s[1] == s[2]:
		print('1')
	elif s[0] == s[1] or s[0] == s[2] or s[1] == s[2]:
		print('8')
	else:
		print('27')


t = int(input())
for _ in range(t):
	(k, n) = [int(x) for x in input().split()]
	new_num = set((x for x in str(k) + str(k)))
	c = set()
	for x in new_num:
		for y in new_num:
			for z in new_num:
				c.add(f'{x}{y}{z}')
	print(len(c))


import math
import sys

def fac(num):
	i = 1
	pr = 1
	while i <= num:
		pr *= i
		i += 1
	return pr
for i in range(int(input())):
	(k, n) = map(int, input().split())
	fi = k
	su = 0
	fi = 1001 * fi
	s = str(fi)
	na = []
	for b in range(len(s)):
		if s[b] not in na:
			na.append(s[b])
	su = math.pow(len(na), 3)
	print(int(su))


l = [[1, 1, 1], [1, 7, 8], [1, 18, 27]]
for _ in range(int(input())):
	(k, n) = input().split()
	k = len(set(k)) - 1
	n = min(int(n), 2)
	print(l[k][n])


from itertools import permutations

def ProcessNum(num, processed, count):
	total = 1
	for i in range(count):
		newNum = num * 13 * 11 * 7
		processed.append(num)
		digList = [int(d) for d in str(newNum)]
		perm = set(permutations(digList, 3))
		for s in perm:
			n = 100 * s[0] + 10 * s[1] + s[2]
			if n not in processed:
				processed.append(n)
				total += ProcessNum(n, processed, n - 1)
	return total
t = int(input())
for _ in range(t):
	(k, n) = map(int, input().split())
	numList = set([int(d) for d in str(k)])
	print(len(numList) ** 3)


for _ in range(int(input())):
	(a, n) = map(int, input().split())
	a = set(str(a))
	if len(a) == 3:
		print(27)
	elif len(a) == 2:
		print(8)
	else:
		print(1)


for _ in range(int(input())):
	(k, n) = input().split()
	if k[0] == k[1] and k[1] == k[2]:
		print(1)
	elif k[0] != k[1] and k[1] != k[2] and (k[0] != k[2]):
		print(27)
	else:
		print(8)


for i in range(int(input())):
	(a, b) = map(int, input().split())
	a = set(str(a))
	print(len(a) ** 3)


for _ in range(int(input())):
	arr = input().split()
	(k, n) = (arr[0], arr[1])
	dct = len(set(k))
	if dct == 1:
		print(1)
	elif dct == 2:
		print(8)
	else:
		print(27)


t = int(input().strip())
for _ in range(t):
	(k, n) = map(int, input().strip().split())
	digits = []
	while k:
		digits.append(k % 10)
		k //= 10
	print(len(set(digits)) ** 3)


t = int(input())
for i in range(t):
	(k, n) = input().split()
	if k.count(k[0]) == 1 and k.count(k[1]) == 1 and (k.count(k[2]) == 1):
		print(27)
	elif 2 in [k.count(k[0]), k.count(k[1]), k.count(k[2])]:
		print(8)
	else:
		print(1)


for _ in range(int(input())):
	(num, temp) = map(str, input().split())
	set_num = set([int(i) for i in num])
	print(pow(len(set_num), 3))


for _ in range(int(input())):
	(k, n) = input().split()
	a = list(k)
	x = len(set(a))
	print(x ** 3)


t = int(input())
for i in range(t):
	(n, k) = map(int, input().split())
	s = str(n)
	x = [j for j in s]
	l = len(set(x))
	print(l ** 3)


for t in range(int(input())):
	(a, b) = list(map(int, input().split()))
	a = set(str(a))
	l = len(a) ** 3
	print(l)


t = int(input())
for _ in range(t):
	(n, k) = map(int, input().split())
	n = str(n)
	l = list(n)
	l = set(l)
	if len(l) == 1:
		print(1)
	elif len(l) == 2:
		print(8)
	else:
		print(27)


for _ in range(int(input())):
	(N, K) = list(map(int, input().split()))
	X = str(N)
	if X[0] != X[1] and X[1] != X[2] and (X[2] != X[0]):
		print('27')
		continue
	if X[0] == X[1] == X[2]:
		print('1')
		continue
	if K >= 2:
		print(8)
	else:
		print(7)


from math import *
for _ in range(int(input())):
	(k, n) = map(str, input().split())
	n = int(n)
	p = len(set(list(k)))
	if p == 1:
		print(1)
	else:
		p = p ** 3
		x = 1
		y = 1
		flag = 0
		for i in range(n):
			if x > p:
				flag = 1
				break
			x += y * 2
			y *= 2
		if flag == 0:
			print(x - y)
		else:
			print(p)


input()
import sys
for kn in sys.stdin:
	(k, n) = kn.rstrip().split()
	if k[0] == k[1] and k[1] == k[2]:
		print(1)
	elif k[0] == k[1] or k[1] == k[2] or k[0] == k[2]:
		print(8)
	else:
		print(27)


for _ in range(int(input())):
	(K, _) = input().split()
	diff = len(list(K)) - len(set(K))
	if diff == 2:
		print(1)
	elif diff == 1:
		print(8)
	else:
		print(27)


while True:
	n = int(input())
	if 1 <= n <= 1000:
		break
tc = list()
for i in range(0, n):
	while True:
		(k, p) = input().split()
		if 0 <= int(k) <= 999 and 5 <= int(p) <= 1000000000:
			break
	tc.append([k, p])
for a in tc:
	(k, p) = a
	if k[0] == k[1] or k[1] == k[2] or k[0] == k[2]:
		if k[0] == k[1] == k[2]:
			print(1)
		else:
			print(2 ** 3)
	else:
		print(3 ** 3)


tc = int(input())
for _ in range(tc):
	(x, n) = map(int, input().split())
	X = tuple(str(x))
	X = set(X)
	ans = len(X) ** 3
	print(ans)


def solve(K, n):
	s = str(K)
	d = len(set([x for x in s]))
	return d ** 3
t = int(input())
for _ in range(t):
	(K, n) = map(int, input().split())
	print(solve(K, n))


from bisect import bisect_right as br
from bisect import bisect_left as bl
from collections import *
from itertools import *
import functools
import sys
import math
import random
MAX = sys.maxsize
MAXN = 10 ** 5 + 10
MOD = 10 ** 9 + 7

def isprime(n):
	n = abs(int(n))
	if n < 2:
		return False
	if n == 2:
		return True
	if not n & 1:
		return False
	for x in range(3, int(n ** 0.5) + 1, 2):
		if n % x == 0:
			return False
	return True

def mhd(a, b, x, y):
	return abs(a - x) + abs(b - y)

def numIN(x=' '):
	return map(int, sys.stdin.readline().strip().split(x))

def charIN():
	return sys.stdin.readline().strip().split()

def dis(x, y):
	a = y[0] - x[0]
	b = x[1] - y[1]
	return (a * a + b * b) ** 0.5

def isper(n):
	return int(n ** 0.5) == n ** 0.5
for _ in range(int(input())):
	(n, k) = numIN()
	le = len(set([int(i) for i in str(n)]))
	if le == 3:
		print(27)
	elif le == 2:
		print(8)
	else:
		print(1)


t = int(input())
for i in range(t):
	(k, n) = map(int, input().split())
	if n == 1:
		lis = [int(x) for x in str(k)]
		mylist = list(dict.fromkeys(lis))
		if len(mylist) == 3:
			print('24')
		elif len(mylist) == 2:
			print('7')
		else:
			print('1')
	else:
		lis = [int(x) for x in str(k)]
		mylist = list(dict.fromkeys(lis))
		if len(mylist) == 3:
			print('27')
		elif len(mylist) == 2:
			print('8')
		else:
			print('1')


T = int(input())
for i in range(T):
	(K, N) = map(str, input().split())
	K = len(set(K))
	print(K ** 3)


for _ in range(int(input())):
	(k, n) = input().split()
	unique = set(k)
	len_set = len(unique)
	print(len_set ** 3)


for i in range(int(input())):
	(k, n) = map(int, input().split())
	if len(set(str(k))) == 1:
		print(1)
	if len(set(str(k))) == 2:
		print(8)
	if len(set(str(k))) == 3:
		print(27)


def icl(a, n):
	m = a * 1001
	a = str(m)
	a = set(a)
	n = len(a)
	print(n ** 3)
for _ in range(int(input())):
	(a, n) = map(int, input().split())
	icl(a, n)


try:
	t = int(input())
except:
	t = 0
for i in range(t):
	X = list(map(int, input().split()))
	r = X[0] % 10
	q = (X[0] % 100 - r) / 10
	p = (X[0] - q * 10 - r) / 100
	N = [0] * 10
	N[int(p)] += 1
	N[int(q)] += 1
	N[int(r)] += 1
	ans = 4 - max(N)
	print(ans ** 3)


t = int(input())
while t:
	t = t - 1
	(k, n) = map(str, input().split())
	n = int(n)
	if k[0] == k[1] or k[1] == k[2] or k[0] == k[2]:
		if k[0] == k[1] == k[2]:
			print(1)
		else:
			print(2 ** 3)
	else:
		print(3 ** 3)


for _ in range(int(input())):
	inp = [int(x) for x in input().split()]
	K = inp[0]
	N = inp[1]
	k1 = K % 10
	k2 = K // 10 % 10
	k3 = K // 100
	if k1 == k2 == k3:
		print(1)
	elif k1 == k2 or k2 == k3 or k1 == k3:
		print(8)
	else:
		print(27)


for _ in range(int(input())):
	(k, n) = map(int, input().split())
	s = str(k)
	s = set(s)
	if len(s) == 3:
		print(27)
	elif len(s) == 2:
		print(8)
	elif len(s) == 1:
		print(1)


t = int(input())
while t:
	(n, k) = [i for i in input().split()]
	k = len(set(n))
	if k == 3:
		print(27)
	elif k == 2:
		print(8)
	elif k == 1:
		print(1)
	t = t - 1


t = int(input())
while t:
	t = t - 1
	(k, n) = map(int, input().split())
	a = []
	while k != 0:
		x = k % 10
		if x not in a:
			a.append(x)
		k = k // 10
	print(len(a) ** 3)


t = int(input())
for tt in range(t):
	(k, n) = map(int, input().split())
	k = str(k)
	if k[0] == k[1] or k[1] == k[2] or k[2] == k[0]:
		if k[1] == k[2] and k[2] == k[0]:
			print(1)
		else:
			print(8)
	else:
		print(27)


t = int(input())
ans = []
for i in range(t):
	(n, k) = input().split()
	k = int(k)
	counter = 0
	counter_0 = 0
	for j in n:
		if j == '0':
			count = n.count(j)
			counter_0 = max(count, counter_0)
		else:
			count = n.count(j)
			counter = max(count, counter)
	if k >= 2:
		if counter == 1 and counter_0 == 0:
			ans.append(27)
		elif counter == 2 and counter_0 == 0:
			ans.append(8)
		elif counter == 3:
			ans.append(1)
		elif counter == 1 and counter_0 == 1:
			ans.append(12)
		elif counter == 2 and counter_0 == 1:
			ans.append(4)
		elif counter == 1 and counter_0 == 2:
			ans.append(4)
	if k == 1:
		if counter == 1 and counter_0 == 0:
			ans.append(24)
		elif counter == 2 and counter_0 == 0:
			ans.append(6)
		elif counter == 3:
			ans.append(1)
		elif counter == 1 and counter_0 == 1:
			ans.append(10)
		elif counter == 2 and counter_0 == 1:
			ans.append(4)
		elif counter == 1 and counter_0 == 2:
			ans.append(3)
for a in ans:
	print(a)


a = int(input())
for i in range(a):
	(K, N) = input().split()
	if K.count(K[0]) == 3:
		print(1)
	elif K.count(K[0]) == 2 or K.count(K[1]) == 2 or K.count(K[2]) == 2:
		print(8)
	else:
		print(27)


def dist_num(n, cnt):
	l = len(set(n))
	if l == 1:
		return 1
	elif l == 2 and cnt == 1:
		return 7
	elif l == 2 and cnt > 1:
		return 8
	elif l == 3 and cnt == 1:
		return 24
	else:
		return 27
T = int(input())
for i in range(T):
	(n, cnt) = input().split()
	cnt = int(cnt)
	print(dist_num(n, cnt))


t = int(input())
while t > 0:
	(k, n) = list(map(int, input().split()))
	k = str(k)
	k = set(k)
	if len(k) == 3:
		print(27)
	elif len(k) == 2:
		print(8)
	else:
		print(1)
	t -= 1


import sys
sys.setrecursionlimit(10 ** 5 + 1)
inf = int(10 ** 20)
max_val = inf
min_val = -inf
RW = lambda : sys.stdin.readline().strip()
RI = lambda : int(RW())
RMI = lambda : [int(x) for x in sys.stdin.readline().strip().split()]
RWI = lambda : [x for x in sys.stdin.readline().strip().split()]
nb_test = RI()
for _ in range(nb_test):
	(curr, _) = RMI()
	ans = 1
	lens = len(set(str(curr)))
	if lens == 3:
		ans = 27
	if lens == 2:
		ans = 8
	print(ans)


n = int(input())
a = []
b = []
for i in range(n):
	(q, w) = input().split()
	a.append(int(q))
	b.append(int(w))
for i in range(n):
	x = a[i] % 10
	y = int((a[i] % 100 - x) / 10)
	z = int((a[i] - a[i] % 100) / 100)
	count = 0
	if x == y:
		count += 1
	if y == z:
		count += 1
	if z == x:
		count += 1
	if count == 0:
		print(27)
	if count == 1:
		print(8)
	if count > 1:
		print(1)


t = int(input())
for z in range(t):
	(k, n) = map(int, input().split())
	s = set(str(k))
	l = len(s)
	if l == 3:
		ans = 27
	elif l == 2:
		ans = 8
	else:
		ans = 1
	print(ans)


for _ in range(int(input())):
	(k, n) = map(int, input().split())
	digits = list(str(k))
	if digits[0] == digits[1] == digits[2]:
		print(1)
	elif digits[0] != digits[1] and digits[1] != digits[2] and (digits[0] != digits[2]):
		print(27)
	else:
		print(8)


from itertools import permutations

def apply(K):
	digits = []
	val = 100
	while val > 0:
		x = K // val
		digits.append(x)
		digits.append(x)
		K = K - x * val
		val //= 10
	perms = {a * 100 + b * 10 + c for (a, b, c) in set(permutations(digits, 3))}
	return perms

def solve(K, N):
	perms = {K}
	last_len = 1
	for i in range(N):
		app = map(apply, perms)
		for perm in app:
			perms = perms.union(perm)
		if len(perms) == last_len:
			break
		last_len = len(perms)
	return last_len
for _ in range(int(input())):
	(K, N) = map(int, input().split())
	print(solve(K, N))


for _ in range(int(input())):
	(K, N) = input().split()
	print(len(set(K)) ** 3)


T = int(input())
while T > 0:
	(a, b) = map(int, input().split())
	a = set(str(a))
	l = len(a) ** 3
	print(l)
	T -= 1


t = int(input())
for _ in range(t):
	x = input()
	a = x.split()
	m = a[0]
	n = int(a[1])
	b = []
	c = 0
	for __ in range(0, 3):
		if m[__] not in b:
			c = c + 1
			b.append(m[__])
	if c == 3:
		if n > 1:
			print('27')
		else:
			print('24')
	elif c == 2:
		if n > 1:
			print('8')
		else:
			print('7')
	else:
		print('1')


t = int(input())
while t > 0:
	(k, n) = map(int, input().split())
	k = set(str(k))
	l = len(k) ** 3
	print(l)
	t -= 1


for _ in range(int(input())):
	(k, n) = map(str, input().split())
	if k[0] == k[1] or k[1] == k[2] or k[2] == k[0]:
		if k[0] == k[1] == k[2]:
			print(1)
		else:
			print(2 ** 3)
	else:
		print(3 ** 3)


def main():
	t = int(input())
	for z in range(t):
		(n, k) = input().split()
		n = int(n)
		k = int(n)
		n1 = n % 10
		n2 = int((n % 100 - n1) / 10)
		n3 = int((n - n2 * 10 - n1) / 100)
		if n1 == n2 == n3:
			print(1)
		elif n1 == n2 or n1 == n3 or n2 == n3:
			if k == 1:
				print(7)
			else:
				print(8)
		elif k == 1:
			print(24)
		else:
			print(27)
main()


t = int(input())
for i in range(t):
	(n, k) = input().split()
	n = set(n)
	print(len(n) ** 3)


t = int(input())
for _ in range(t):
	(k, n) = map(int, input().split())
	g = len(set(str(k)))
	print(g ** 3)


t = int(input())
for i in range(t):
	(k, n) = map(int, input().split())
	l = set(str(k))
	m = len(l)
	print(m ** 3)


for _ in range(int(input())):
	(k, n) = input().split()
	while int(n) >= 5:
		print(len(set(k)) ** 3)
		break


T = int(input())
for each_case in range(T):
	(k, n) = input().split(' ')
	n = int(n)
	res = 1
	(ch1, ch2, ch3) = (k.count(k[0]), k.count(k[1]), k.count(k[2]))
	if ch1 == 1 and ch2 == 1 and (ch3 == 1):
		res = 3 ** 3
	elif 2 in [ch1, ch2, ch3]:
		res = 2 ** 3
	print(res)


t = int(input())
for _ in range(t):
	(k, n) = map(int, input().split())
	ks = str(k)
	if ks[0] == ks[1] == ks[2]:
		print(1)
	elif ks[0] == ks[1] or ks[1] == ks[2] or ks[2] == ks[0]:
		print(8)
	else:
		print(27)


t = int(input())
for i in range(t):
	g = 0
	l = input().split()
	(k, n) = (l[0], l[1])
	g = len(set(k))
	print(g ** 3)


for t in range(int(input())):
	(n, k) = input().split()
	_ = len(set(list(n)))
	if _ == 3:
		print(27)
	elif _ == 2:
		print(8)
	else:
		print(1)


testcase = int(input())
for i in range(testcase):
	testcase = testcase - 1
	(k, n) = map(int, input().split())
	k = set(str(k))
	l = len(k) ** 3
	print(l)


for _ in range(int(input())):
	N = input()
	L = []
	for i in N:
		L.append(i)
	if L[0] == L[1] and L[1] == L[2]:
		print(1)
	elif L[0] == L[1] and L[1] != L[2] or (L[0] == L[2] and L[1] != L[2]) or (L[0] != L[1] and L[1] == L[2]):
		print(8)
	else:
		print(27)


t = int(input())
for i in range(t):
	(num, k) = input().split()
	num = set(num)
	l = len(num)
	if l == 3:
		print('27')
	if l == 2:
		print('8')
	if l == 1:
		print('1')


for i in range(int(input())):
	(k, n) = map(int, input().split())
	li = []
	s = str(k)
	li = []
	for j in s:
		li.append(j)
	l = len(set(li))
	if l == 3:
		if n == 1:
			print('24')
		else:
			print(27)
	elif l == 2:
		if n == 1:
			print('7')
		else:
			print('8')
	else:
		print('1')