Let's call a positive integer $n$ ordinary if in the decimal notation all its digits are the same. For example, $1$, $2$ and $99$ are ordinary numbers, but $719$ and $2021$ are not ordinary numbers.

For a given number $n$, find the number of ordinary numbers among the numbers from $1$ to $n$.


-----Input-----

The first line contains one integer $t$ ($1 \le t \le 10^4$). Then $t$ test cases follow.

Each test case is characterized by one integer $n$ ($1 \le n \le 10^9$).


-----Output-----

For each test case output the number of ordinary numbers among numbers from $1$ to $n$.


-----Examples-----

Input
6
1
2
3
4
5
100
Output
1
2
3
4
5
18


-----Note-----

None

Difficulty: EASY
Tags: Number theory

Mathematics

Complete search
Skill types: Complete search

Starter code:

Solutions:
t = int(input())
for _ in range(t):
	n = input()
	print(9 * (len(n) - 1) + int(n) // int('1' * len(n)))


import math
t = int(input())
for _ in range(t):
	n = int(input())
	curr = 0
	c = 0
	for i in range(0, int(math.log(n, 10)) + 1):
		curr = 10 * curr + 1
		for j in range(1, 10):
			if 0 <= curr * j <= n:
				c += 1
	print(c)


def check(s):
	if n < 10:
		return n
	else:
		l = len(str(n))
		f = str(n)[0]
		x = int(f * l)
		ctr = 9 * (l - 1)
		if n < x:
			ctr += int(f) - 1
		else:
			ctr += int(f)
		return ctr
for _ in range(int(input())):
	n = int(input())
	print(check(n))


l = []
for i in range(1, 10):
	x = i
	temp = ''
	for j in range(9):
		temp += str(x)
		l.append(int(temp))
l.sort()
t = int(input())
for _ in range(t):
	n = int(input())
	ans = 0
	for i in range(len(l)):
		if l[i] <= n:
			ans += 1
		else:
			break
	print(ans)


def solve(num):
	if len(num) < 2:
		return num
	sz = len(num)
	ans = 9 * (sz - 1)
	temp = num[0] * sz
	if temp <= num:
		return ans + int(num[0])
	else:
		return ans + int(num[0]) - 1
for _ in range(int(input())):
	num = input()
	print(solve(num))


for _ in range(int(input())):
	n = input()
	l = len(n)
	print(9 * l - 10 + int(n[0]) + (int(n[0] * l) <= int(n)))


import math
for _ in range(int(input())):
	n = int(input())
	c = 0
	i = 1
	z = 1
	dig = 1
	flag = 0
	ch = 1
	while i <= n:
		c += 1
		if flag == 0:
			ch = pow(10, dig - 1) + z
		if i // 9 == ch - 1:
			dig += 1
			flag = 0
			z = ch
			i += ch
		else:
			flag = 1
			i += ch - 1
	print(c)


t = int(input())
for i in range(t):
	n = int(input())
	s = str(n)
	l = len(s)
	c = (l - 1) * 9
	if int(s[0] * l) <= n:
		c += int(s[0])
	else:
		c += int(s[0]) - 1
	print(c)


import math

def main():
	t = int(input())
	for _ in range(t):
		n = input()
		l_n = len(n)
		if l_n == 1:
			print(n)
		else:
			print(9 * (l_n - 1) + int(n[0]) - 1 + (n >= str(n[0]) * l_n))
main()


for _ in range(int(input())):
	n = int(input())
	l = len(str(n))
	s = (l - 1) * 9
	x = str(n)
	if l > 1:
		s1 = x[0] * l
		if x >= s1:
			s += int(x[0])
		else:
			s += int(x[0]) - 1
	else:
		s += n
	print(s)


t = int(input())
for i in range(t):
	n = int(input())
	if n < 10:
		print(n)
	elif n < 100:
		print(9 + n // 11)
	elif n < 1000:
		print(9 + 9 + n // 111)
	elif n < 10000:
		print(9 + 9 + 9 + n // 1111)
	elif n < 100000:
		print(9 + 9 + 9 + 9 + n // 11111)
	elif n < 1000000:
		print(9 + 9 + 9 + 9 + 9 + n // 111111)
	elif n < 10000000:
		print(9 + 9 + 9 + 9 + 9 + 9 + n // 1111111)
	elif n < 100000000:
		print(9 + 9 + 9 + 9 + 9 + 9 + 9 + n // 11111111)
	elif n < 1000000000:
		print(9 + 9 + 9 + 9 + 9 + 9 + 9 + 9 + n // 111111111)
	else:
		print(9 + 9 + 9 + 9 + 9 + 9 + 9 + 9 + 9 + n // 1111111111)


for _ in range(int(input())):
	n = int(input())
	if int(str(n)[0] * len(str(n))) <= n:
		print((len(str(n)) - 2) * 9 + int(str(n)[0]) + 9)
	else:
		print((len(str(n)) - 2) * 9 + int(str(n)[0]) + 8)


inp1 = int(input())
for i in range(inp1):
	inp2 = input()
	a = len(inp2) - 1
	b = a * 9
	c = len(inp2) * '1'
	d = int(inp2) // int(c)
	e = d + b
	print(e)


def noofdig(n: int) -> int:
	t = n
	res = 0
	while t > 0:
		res = res + 1
		t = t // 10
	return res

def main():
	stri = []
	t = int(input())
	for i in range(t):
		stri.clear()
		n = int(input())
		c = noofdig(n)
		result = (c - 1) * 9
		for i in range(9):
			stri.append('')
		for i in range(9):
			sq = ''
			for j in range(c):
				sq = sq + str(i + 1)
			stri[i] = sq
		for q in range(len(stri)):
			if n >= int(stri[q]):
				result = result + 1
			else:
				break
		print(result)
main()


from bisect import bisect_left
import sys

def get_ints():
	return list(map(int, sys.stdin.readline().strip().split()))
vis = []
for i in range(1, 12, 1):
	for j in range(1, 10, 1):
		curr = int(str(j) * i)
		vis.append(curr)
vis.sort()
T = int(input())
for _ in range(T):
	N = int(input())
	idx = bisect_left(vis, N)
	if N >= vis[idx]:
		print(idx + 1)
	else:
		print(idx)


from sys import stdin
input = stdin.readline
T = int(input())
for _ in range(T):
	N = int(input())
	cnt = 0
	for i in range(1, 10):
		val = ''
		while True:
			val += str(i)
			if int(val) > N:
				break
			cnt += 1
	print(cnt)


import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
	n = int(input())
	ans = 0
	for d in range(1, 10):
		t = d
		while t <= n:
			ans += 1
			t = t * 10 + d
	print(ans)


for s in [*open(0)][1:]:
	print(9 * len(s) - 18 + int(s[0]) - ('.' < s.strip(s[0]) < s[0]))


import sys, math
input = sys.stdin.readline
for _ in range(int(input())):
	n = int(input())
	x = int(math.log(n, 10))
	v = int('1' * (x + 1))
	h = n // v
	print(9 * x + h)


test = int(input())
for g in range(test):
	n = input()
	m = int(n)
	tot = 0
	for i in n[1:]:
		tot += 9
	s = ''
	for i in n:
		s1 = s + '1'
		s = s1
	s = int(s)
	for i in range(1, 10):
		if i * s <= m:
			tot += 1
			continue
		else:
			break
	print(tot)
	continue


def printf(*x):
	print('----', *x)

def count_digits(n):
	t = 0
	while n:
		t += 1
		n //= 10
	return t

def solve():
	pass
	n = int(input())
	digits = count_digits(n)
	ans = (digits - 1) * 9
	r = 10 ** (digits - 1)
	dl = [1, 2, 3, 4, 5, 6, 7, 8, 9]
	for i in dl:
		if i * int('1' * digits) <= n:
			ans += 1
	print(ans)
T = int(input())
while T:
	T -= 1
	solve()


arr = []
for i in range(1, 10):
	for k in range(1, 10):
		val = int(str(i) * k)
		arr.append(val)
arr.sort()
for _ in range(int(input())):
	num = int(input())
	count = 0
	for ele in arr:
		if ele <= num:
			count += 1
	print(count)


from math import log10, ceil
t = int(input())
res = []
for i in range(t):
	n = int(input())
	if n < 10:
		res.append(n)
	elif n == 10:
		res.append(9)
	else:
		p = ceil(log10(n))
		c = 9 * p
		d = n // 10 ** (p - 1)
		e = 9 - d
		f = 0
		for i in range(p):
			f += d * 10 ** i
		if n < f:
			e += 1
		c -= e
		res.append(c)
for i in res:
	print(i)


tc = int(input())
l = 0
for i in range(tc):
	cnt = 0
	mul = '1'
	num = int(input())
	length = len(str(num))
	for i in range(0, length):
		for j in range(1, 10):
			if int(mul) * j < num or int(mul) * j == num:
				cnt += 1
		mul += '1'
	print(cnt)


for _ in range(int(input())):
	n = input()
	if len(n) == 1:
		print(n)
	else:
		c = (len(n) - 1) * 9
		h = int(n[0])
		re = h - 1
		if int(str(h) * len(n)) <= int(n):
			re = re + 1
		print(re + c)


def main():
	for _ in range(int(input())):
		n = int(input())
		r = len(str(n))
		sm = 9 * (r - 1) + n // int('1' * r)
		print(sm)
main()


def call(n):
	if n < 10:
		return n
	if n <= 100:
		return 9 + n // 11
	if n <= 1000:
		return 18 + n // 111
	if n <= 10000:
		return 27 + n // 1111
	if n <= 100000:
		return 36 + n // 11111
	if n <= 1000000:
		return 45 + n // 111111
	if n <= 10000000:
		return 54 + n // 1111111
	if n <= 100000000:
		return 63 + n // 11111111
	if n <= 1000000000:
		return 72 + n // 111111111
for _ in range(int(input())):
	n = int(input())
	print(call(n))


t = int(input())
while t > 0:
	n = int(input())
	s = str(n)
	k = len(s)
	ch = ''
	for i in range(k):
		ch = ch + s[0]
	if n < int(ch):
		res = 9 * (k - 1) + int(s[0]) - 1
	else:
		res = 9 * (k - 1) + int(s[0])
	print(res)
	t = t - 1


def is_ord(n):
	l = len(n)
	return int(n) % int('1' * l) == 0
for _ in range(int(input())):
	n = input()
	l = len(n)
	t = 9 * l - 9
	print(t + int(n[0]) - 1 + (int(n[0] * l) <= int(n)))


import sys
LI = lambda : list(map(int, sys.stdin.readline().split()))
MI = lambda : map(int, sys.stdin.readline().split())
SI = lambda : sys.stdin.readline().strip('\n')
II = lambda : int(sys.stdin.readline())
for _ in range(II()):
	n = SI()
	s = 9 * (len(n) - 1)
	d = '1' * len(n)
	while int(d) <= int(n):
		s += 1
		d = str(int(d[0]) + 1) * len(n)
	print(s)


import math
for _ in range(int(input())):
	n = int(input())
	len_num = int(math.log10(n)) + 1
	t = a = 0
	for j in range(len_num):
		t = t * 10 + 1
		for k in range(1, 10):
			if t * k <= n:
				a += 1
	print(a)


def orid():
	for _ in range(int(input())):
		n = int(input())
		r = len(str(n))
		sum = 9 * (r - 1) + n // int('1' * r)
		print(sum)
orid()


for t in range(int(input())):
	n = int(input())
	arr = ['1', '2', '3', '4', '5', '6', '7', '8', '9']
	ans = 0
	for i in arr:
		var = i
		adder = i
		while int(var) <= n:
			ans += 1
			var += adder
	print(ans)


for _ in range(int(input())):
	n = str(input())
	ans = (len(n) - 1) * 9
	ans += int(n[0]) - 1
	if n >= str(n[0] * len(n)):
		ans += 1
	print(ans)


for _ in range(int(input())):
	n = int(input())
	if n >= 1 and n <= 9:
		print(n)
	elif n == 10:
		print(9)
	elif n < 109:
		c = 9 + int(n / 11)
		print(c)
	elif n == 110:
		print(18)
	else:
		c = len(str(n)) * 9 - 9
		c = c + int(n / int(str(1) * len(str(n))))
		print(c)


import math
t = int(input())

def count(L, R):
	temp = 0
	result = 0
	n = int(math.log10(R) + 1)
	for i in range(0, n):
		temp = temp * 10 + 1
		for j in range(1, 10):
			if L <= temp * j and temp * j <= R:
				result += 1
	return result

def solve():
	n = int(input())
	print(count(1, n))
while t != 0:
	solve()
	t -= 1


for s in [*open(0)][1:]:
	print((len(s) - 2) * 9 + int(s[0]) - 1 + (int(s) >= int(s[0] * len(s[:-1]))))


n = int(input())
old = 0
new = 0
k = 0
total = 0
for i in range(n):
	num = int(input())
	if len(str(num)) == 1:
		print(num)
	else:
		total = 9 * (len(str(num)) - 1) + int(str(num)[0]) - 1
		loucura = int(''.join([str(num)[0] for i in range(len(str(num)))]))
		if num >= loucura:
			total += 1
		print(total)
		total = 0


for _ in range(int(input())):
	n = input()
	k = int(n)
	if k < 10:
		print(n)
	else:
		b = len(n) - 1
		c = k // 10 ** b
		ans = b * 9 + c - 1
		if k < int(str(c) * (b + 1)):
			print(ans)
		else:
			print(ans + 1)


t = input()
for i in range(int(t)):
	n = input()
	k = len(n)
	n = int(n)
	p = int(k * '1')
	a = n // p + 9 * (k - 1)
	print(a)


def check(n):
	gen = {0: 0, 1: 9, 2: 18, 3: 27, 4: 36, 5: 45, 6: 54, 7: 63, 8: 72, 9: 81}
	x = n[0]
	y = n[0] * len(n)
	if int(n) < 10:
		return n
	ans = gen[len(n) - 1]
	if int(y) <= int(n):
		ans += int(x)
	else:
		ans += int(x) - 1
	return ans
for _ in range(int(input())):
	n = input()
	print(check(n))


t = int(input())
for i in range(t):
	n = int(input())
	if n > 9:
		ans = 9
	else:
		ans = n
	k = 11
	while len(str(n)) > len(str(k)):
		ans += 9
		k = int(str(k) + '1')
	print(ans + n // k)


for t in range(int(input())):
	n = int(input())
	c = 0
	s = str(n)
	if n < 10:
		print(n)
	else:
		if len(s) == 2:
			ans = n // 11 + 9
		if len(s) == 3:
			ans = n // 111 + 9 * 2
		if len(s) == 4:
			ans = n // 1111 + 9 * 3
		if len(s) == 5:
			ans = n // 11111 + 9 * 4
		if len(s) == 6:
			ans = n // 111111 + 9 * 5
		if len(s) == 7:
			ans = n // 1111111 + 9 * 6
		if len(s) == 8:
			ans = n // 11111111 + 9 * 7
		if len(s) == 9:
			ans = n // 111111111 + 9 * 8
		if len(s) == 10:
			ans = n // 1111111111 + 9 * 9
		print(ans)


for i in range(int(input())):
	n = input()
	print(9 * (len(n) - 1) + int(n) // int('1' * len(n)))


import sys

def rl():
	return sys.stdin.readline().strip()

def rl_types(types):
	str_list = [x for x in sys.stdin.readline().strip().split(' ')]
	return [types[i](str_list[i]) for i in range(len(str_list))]

def pr(something):
	sys.stdout.write(str(something) + '\n')

def pra(array):
	sys.stdout.write(' '.join([str(x) for x in array]) + '\n')

def solve(array):
	return array
NT = int(rl())
vals = []
for _ in range(NT):
	vals.append(int(rl()))
mx = max(vals)
seq = []
for nd in range(1, 10):
	for d in range(1, 10):
		val = int(''.join([str(d)] * nd))
		if val > mx:
			break
		seq.append(val)
for val in vals:
	cnt = 0
	for v in seq:
		if v > val:
			break
		cnt += 1
	pr(cnt)


for i in range(int(input())):
	a = input()
	p = len(a)
	r = 9 * (p - 1)
	if int(a) <= 9:
		print(int(a))
	else:
		j = 1
		z = '1' * p
		while int(z) <= int(a):
			r += 1
			j += 1
			z = str(j) * p
		print(r)


z = int(input())
l = [1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 22, 33, 44, 55, 66, 77, 88, 99, 111, 222, 333, 444, 555, 666, 777, 888, 999, 1111, 2222, 3333, 4444, 5555, 6666, 7777, 8888, 9999, 11111, 22222, 33333, 44444, 55555, 66666, 77777, 88888, 99999, 111111, 222222, 333333, 444444, 555555, 666666, 777777, 888888, 999999, 1111111, 2222222, 3333333, 4444444, 5555555, 6666666, 7777777, 8888888, 9999999, 11111111, 22222222, 33333333, 44444444, 55555555, 66666666, 77777777, 88888888, 99999999, 111111111, 222222222, 333333333, 444444444, 555555555, 666666666, 777777777, 888888888, 999999999]
for t in range(z):
	n = int(input())
	i = 0
	c = 0
	while i < len(l) and l[i] <= n:
		c += 1
		i += 1
	print(c)


import sys
input = sys.stdin.readline

def solve(n):
	res = 0
	for d in range(1, 10):
		for l in range(1, 11):
			if n >= int(str(d) * l):
				res += 1
	print(res)
for _ in range(int(input())):
	n = int(input())
	solve(n)


tc = int(input())
for _ in range(tc):
	x = input()
	n = len(x)
	f = int(x[0])
	ans = 9 * (n - 1) + (f - 1)
	if int(x[0] * n) <= int(x):
		ans += 1
	print(ans)


import sys
input = sys.stdin.readline
for _ in range(int(input())):
	n = int(input())
	ans = 0
	for i in range(9):
		cur = 1
		for j in range(i):
			cur = cur * 10 + 1
		start = cur
		for j in range(9):
			if start <= n:
				ans += 1
			start += cur
	print(ans)


for _ in range(int(input())):
	n = int(input())
	s = str(n)
	l = len(s)
	print((l - 1) * 9 + n // int('1' * l))


t = int(input())
i = 0
A = []
while 0 <= i < t:
	i = i + 1
	n = int(input())
	m = str(n)
	q = len(m)
	if n >= int(m[0] * q):
		a = (q - 1) * 9 + int(m[0])
	else:
		a = (q - 1) * 9 + int(m[0]) - 1
	A.append(a)
k = -1
while -1 <= k < t - 1:
	k = k + 1
	print(A[k])


t = int(input())
for _ in range(t):
	n = input()
	comp = [n[0]] * len(n)
	if int(n) >= int(n[0] * len(n)):
		print((len(n) - 1) * 9 + int(n[0]))
	else:
		print((len(n) - 1) * 9 + int(n[0]) - 1)


for _ in range(int(input())):
	a = input()
	print(9 * (len(a) - 1) + int(a) // int('1' * len(a)))


for t in range(int(input())):
	s = input()
	n = int(s)
	l = len(s)
	x = ['1', '2', '3', '4', '5', '6', '7', '8', '9']
	ans = 0
	for i in range(1, l + 1):
		if l > i:
			ans += 9
		else:
			for j in x:
				if int(j * i) <= n:
					ans += 1
	print(ans)


import sys
import math

def readInts():
	return [int(x) for x in sys.stdin.readline().split()]

def readInt():
	return int(sys.stdin.readline())

def count_same_digit(L, R):
	tmp = 0
	ans = 0
	n = int(math.log10(R) + 1)
	for i in range(n):
		tmp = tmp * 10 + 1
		for j in range(1, 10):
			if L <= tmp * j and tmp * j <= R:
				ans += 1
	return ans

def main():
	t = readInt()
	for _ in range(t):
		n = readInt()
		print(count_same_digit(1, n))
main()


t = int(input())
if t >= 1 and t <= 10 ** 4:
	lan = []
	for _ in range(t):
		n = input()
		b = int(n)
		if b >= 1 and b <= 9:
			lan.append(n)
		else:
			indx = 0
			ln = []
			lgth = len(str(n))
			flag = True
			y = int(n[0])
			while indx != lgth:
				if int(n[indx]) > y:
					flag = True
					break
				if int(n[indx]) < y:
					flag = False
					break
				indx += 1
			if flag == True:
				j = 9 - y
				z = 9 * lgth - j
				lan.append(z)
			if flag == False:
				j = 9 - y + 1
				z = 9 * lgth - j
				lan.append(z)
	print(*lan, sep='\n')


def test(n):
	number = [i for i in range(10)]
	count = 0
	while True:
		for i in range(1, 10):
			if number[i] <= n:
				count += 1
				number[i] = number[i] * 10 + i
			else:
				print(count)
				return
T = int(input())
for testcase in range(1, T + 1):
	n = int(input())
	test(n)


n = int(input())
for x in range(n):
	t = int(input())
	if t <= 9:
		print(t)
	else:
		s = str(t)
		l = len(s)
		c = 9 * (l - 1)
		for y in range(9):
			q = str(y + 1)
			p = int(q * l)
			if t < p:
				break
			else:
				c += 1
		print(c)


for _ in range(int(input())):
	n = int(input())
	d = '123456789'
	k = 0
	for c in range(1, 10):
		for dig in d:
			if int(c * dig) <= n:
				k += 1
	print(k)


for i in range(int(input())):
	n = int(input())
	tot = 0
	tot += 9 * (len(str(n)) - 1)
	tot += n // int('1' * len(str(n)))
	print(tot)


t = int(input())
for i in range(t):
	n = int(input())
	l = len(str(n))
	s = 9 * (l - 1)
	x = n // ((10 ** l - 1) // 9)
	s += x
	print(s)


for j in range(int(input())):
	n = int(input())
	k = 0
	for i in range(9, 0, -1):
		if int(len(str(n)) * str(i)) <= n:
			k += 1
	k += (len(str(n)) - 1) * 9
	print(k)


for _ in range(int(input())):
	n = int(input())
	c = 0
	i = 1
	while i < n + 1:
		for j in range(1, 10):
			if i * j <= n:
				c += 1
		i = i * 10 + 1
	print(c)


import sys
import time
import os
from math import sqrt, log, log2, ceil, log10, gcd, floor, pow, sin, cos, tan, pi, inf, factorial
from copy import copy, deepcopy
from sys import exit, stdin, stdout
from collections import Counter, defaultdict, deque
from itertools import permutations
import heapq
from bisect import bisect_left as bl
from bisect import bisect_right as br
from bisect import bisect
mod = 1000000007
iinp = lambda : int(sys.stdin.readline())
inp = lambda : sys.stdin.readline().strip()
strl = lambda : list(inp().strip().split(' '))
intl = lambda : list(map(int, inp().split(' ')))
mint = lambda : map(int, inp().split())
flol = lambda : list(map(float, inp().split(' ')))
flush = lambda : stdout.flush()

def permute(nums):

	def fun(arr, nums, cur, v):
		if len(cur) == len(nums):
			arr.append(cur.copy())
		i = 0
		while i < len(nums):
			if v[i]:
				i += 1
				continue
			else:
				cur.append(nums[i])
				v[i] = 1
				fun(arr, nums, cur, v)
				cur.pop()
				v[i] = 0
				i += 1
		return arr
	res = []
	nums.sort()
	v = [0] * len(nums)
	return fun(res, nums, [], v)

def subsets(res, index, arr, cur):
	res.append(cur.copy())
	for i in range(index, len(arr)):
		cur.append(arr[i])
		subsets(res, i + 1, arr, cur)
		cur.pop()
	return res

def sieve(N):
	root = int(sqrt(N))
	primes = [1] * (N + 1)
	(primes[0], primes[1]) = (0, 0)
	for i in range(2, root + 1):
		if primes[i]:
			for j in range(i * i, N + 1, i):
				primes[j] = 0
	return primes

def bs(arr, l, r, x):
	if x < arr[0] or x > arr[len(arr) - 1]:
		return -1
	while l <= r:
		mid = l + (r - l) // 2
		if arr[mid] == x:
			return mid
		elif arr[mid] < x:
			l = mid + 1
		else:
			r = mid - 1
	return -1

def isPrime(n):
	if n <= 1:
		return False
	if n <= 3:
		return True
	if n % 2 == 0 or n % 3 == 0:
		return False
	p = int(sqrt(n))
	for i in range(5, p + 1, 6):
		if n % i == 0 or n % (i + 2) == 0:
			return False
	return True

def solve():
	n = iinp()
	if 1 <= n <= 9:
		print(n)
		return
	else:
		ans = 0
		for i in range(1, 10):
			j = str(i)
			while int(j) <= n:
				ans += 1
				j += str(i)
		print(ans)
start_time = time.time()
for _ in range(iinp()):
	solve()


digit_arr = []
for num_digits in range(1, 10):
	for digit in range(1, 10):
		digit_arr.append(int(str(digit) * num_digits))
digit_arr.sort()
tc = int(input())
for i in range(tc):
	n = int(input())
	cnt = 0
	for j in range(len(digit_arr)):
		if digit_arr[j] > n:
			break
		cnt += 1
	print(cnt)


input_num = int(input())
lst = []

def checker(target):
	setter = set()
	before = ''
	for a in target:
		if a == before:
			continue
		elif a not in setter:
			setter.add(a)
			before = a
		else:
			return 'NO'
	return 'YES'

def solve(target):
	len_target = len(target)
	first_digit = target[0]
	if int(target) >= int(first_digit * len_target):
		return (len_target - 1) * 9 + int(first_digit)
	else:
		return (len_target - 1) * 9 + int(first_digit) - 1
for i in range(input_num):
	lst.append(solve(input()))
for i in lst:
	print(i)


t = int(input())
for i in range(t):
	n = int(input())
	kol = 9 * (len(str(n)) - 1) + int(str(n)[0]) - 1
	a = int(str(n)[0] * len(str(n)))
	if a <= n:
		kol += 1
	print(kol)


x = int(input())
for _ in range(x):
	n = input()
	ans = (len(n) - 1) * 9
	ans += int(n[0]) - 1
	ss = n[0] * len(n)
	if int(n) >= int(ss):
		ans += 1
	print(ans)


from sys import stdin, stdout
import math
from collections import deque, Counter, OrderedDict
import heapq

def ii():
	return int(stdin.readline())

def mi():
	return map(int, stdin.readline().split())

def li():
	return list(mi())

def si():
	return stdin.readline()
import bisect
ans = []
for i in range(1, 10):
	i = int('1' * i)
	for j in range(1, 10):
		ans.append(j * i)
t = ii()
for _ in range(t):
	n = ii()
	print(bisect.bisect(ans, n))


a = []
b = []
for i in range(int(input())):
	k = 0
	n = int(input())
	a = str(n)
	v = int('1' * len(a))
	k = int(9 * (len(a) - 1) + int(n / v))
	b.append(k)
print(*b, sep='\n')


t = int(input())
l = []
for i in range(t):
	n = int(input())
	x = len(str(n // 10)) * 9
	x1 = (len(str(n // 10)) + 1) * '1'
	if n < 10:
		l.append(n)
	elif n < int(x1):
		l.append(x)
	else:
		l.append(n // int(x1) + x)
print('\n'.join(map(str, l)))


t = int(input())
for _ in range(t):
	n = input()
	if int(n) <= 9:
		print(n)
		continue
	digit = len(n)
	ans = (digit - 1) * 9
	maxi = n[0]
	ma = maxi * digit
	if int(ma) <= int(n):
		ans += 1
	ans += int(maxi) - 1
	print(ans)


for _ in range(int(input())):
	n = int(input())
	if n <= 9:
		print(n)
	else:
		ans = 0
		l = len(str(n))
		ans += (l - 1) * 9
		for i in range(1, 10):
			if int(str(i) * l) <= n:
				ans += 1
			else:
				break
		print(ans)


t = int(input())
for i in range(t):
	n = int(input())
	a = str(n)
	b = len(a)
	ans = (b - 1) * 9
	p = a[0] * b
	if p <= a:
		ans += int(a[0])
	else:
		ans += int(a[0]) - 1
	print(ans)


for _ in range(int(input())):
	n = int(input())
	if n <= 9:
		print(n)
	else:
		x = len(str(n))
		print(9 * (x - 1) + n // int('1' * x))


t = int(input())
for i in range(t):
	n = int(input())
	s = len(str(n))
	c = (s - 1) * 9
	c += n // int('1' * s)
	print(c)


for iter in range(int(input())):
	a = int(input())
	cnt = 0
	for i in range(1, 10):
		for j in range(1, 11):
			if int(str(i) * j) <= a:
				cnt += 1
			else:
				break
	print(cnt)


for _ in range(int(input())):
	n = input()
	l = len(n)
	if n >= n[0] * l:
		print((l - 1) * 9 + int(n[0]))
	else:
		print((l - 1) * 9 + int(n[0]) - 1)


import math

def solve(n):
	if n < 10:
		return n
	digit = int(math.log10(n))
	(cnt, num1, num2) = (0, 0, 0)
	k = ''
	for i in range(0, digit + 1):
		k += '1'
	num1 = int(k)
	num2 = num1
	while num2 <= n:
		cnt += 1
		num2 += num1
	return digit * 9 + cnt
for _ in range(int(input())):
	n = int(input())
	ans = solve(n)
	print(ans)


t = int(input())

def solve(s):
	ans = 9 * (len(s) - 1)
	if int(s[0] * len(s)) <= int(s):
		ans += int(s[0])
	else:
		ans += int(s[0]) - 1
	print(ans)
for i in range(t):
	n = input()
	solve(n)


from collections import defaultdict
d = defaultdict(int)

def problem():
	num_test_cases = int(input())
	nums = [int(input()) for _ in range(num_test_cases)]
	results = [str(ordinaries_to_n2(n)) for n in nums]
	for r in results:
		print(r)

def ordinaries_to_n2(n):
	step = 1
	i = 1
	reset_step = False
	l = []
	while i <= n:
		if '9' in str(i):
			step += 1
			reset_step = True
		elif reset_step:
			step = i
			reset_step = False
		l.append(l)
		i += step
	return len(l)
problem()


from math import log10

def Count(i, j):
	(Num, Val) = (0, 0)
	N = int(log10(j) + 1)
	for x in range(N):
		Num = Num * 10 + 1
		for y in range(1, 10):
			if i <= Num * y and Num * y <= j:
				Val = Val + 1
	return Val
for _ in range(int(input())):
	N = int(input())
	if N < 10:
		print(N)
	else:
		print(Count(1, N))


def ordinary_number():
	for i in range(int(input())):
		inp = input()
		k = len(inp)
		inp = int(inp)
		p = int('1' * k)
		a = inp // p + 9 * (k - 1)
		print(a)
ordinary_number()


list1 = []
for x in range(1, 10):
	for y in range(1, 10):
		list1.append(str(x) * y)
t = int(input())
for i in range(t):
	k = int(input())
	count = 0
	for i in list1:
		if int(i) <= k:
			count += 1
	print(count)


t = int(input())
for i in range(t):
	n = int(input())
	count = 0
	for i in range(1, 10):
		temp = i
		while temp <= n:
			temp = temp * 10 + i
			count += 1
	print(count)


from __future__ import division, print_function
import math
import os
import sys
from fractions import *
from sys import *
from decimal import *
from io import BytesIO, IOBase
from itertools import *
from collections import *
M = 10 ** 9 + 7
if sys.version_info[0] < 3:
	from __builtin__ import xrange as range
	from future_builtins import ascii, filter, hex, map, oct, zip
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def inp():
	return sys.stdin.readline().rstrip('\r\n')

def out(var):
	sys.stdout.write(str(var))

def lis():
	return list(map(int, inp().split()))

def stringlis():
	return list(map(str, inp().split()))

def sep():
	return map(int, inp().split())

def strsep():
	return map(str, inp().split())

def fsep():
	return map(float, inp().split())

def inpu():
	return int(inp())

def regularbracket(t):
	p = 0
	for i in t:
		if i == '(':
			p += 1
		else:
			p -= 1
		if p < 0:
			return False
	else:
		if p > 0:
			return False
		else:
			return True

def binarySearchCount(arr, n, key):
	left = 0
	right = n - 1
	count = 0
	while left <= right:
		mid = int((right + left) / 2)
		if arr[mid] <= key:
			count = mid + 1
			left = mid + 1
		else:
			right = mid - 1
	return count

def reverse1(string):
	pp = ''
	for i in string[::-1]:
		pp += i
	if pp == string:
		return True
	return False

def reverse2(list1):
	l = []
	for i in list1[::-1]:
		l.append(i)
	if l == list1:
		return True
	return False

def mex(list1):
	p = max(list1) + 1
	for i in range(len(list1)):
		if list1[i] != i:
			p = i
			break
	return p

def sumofdigits(n):
	n = str(n)
	s1 = 0
	for i in n:
		s1 += int(i)
	return s1

def perfect_square(n):
	s = math.sqrt(n)
	if s == int(s):
		return True
	return False

def roman_number(x):
	if x > 15999:
		return
	value = [5000, 4000, 1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1]
	symbol = ['F', 'MF', 'M', 'CM', 'D', 'CD', 'C', 'XC', 'L', 'XL', 'X', 'IX', 'V', 'IV', 'I']
	roman = ''
	i = 0
	while x > 0:
		div = x // value[i]
		x = x % value[i]
		while div:
			roman += symbol[i]
			div -= 1
		i += 1
	return roman

def soretd(s):
	for i in range(1, len(s)):
		if s[i - 1] > s[i]:
			return False
	return True

def countRhombi(h, w):
	ct = 0
	for i in range(2, h + 1, 2):
		for j in range(2, w + 1, 2):
			ct += (h - i + 1) * (w - j + 1)
	return ct

def countrhombi2(h, w):
	return h * h // 4 * (w * w // 4)

def binpow(a, b):
	if b == 0:
		return 1
	else:
		res = binpow(a, b // 2)
	if b % 2 != 0:
		return res * res * a
	else:
		return res * res

def binpowmodulus(a, b, m):
	a %= m
	res = 1
	while b > 0:
		if b & 1:
			res = res * a % m
		a = a * a % m
		b >>= 1
	return res

def coprime_to_n(n):
	result = n
	i = 2
	while i * i <= n:
		if n % i == 0:
			while n % i == 0:
				n //= i
			result -= result // i
		i += 1
	if n > 1:
		result -= result // n
	return result

def prime(x):
	if x == 1:
		return False
	else:
		for i in range(2, int(math.sqrt(x)) + 1):
			if x % i == 0:
				return False
		else:
			return True

def luckynumwithequalnumberoffourandseven(x, n, a):
	if x >= n and str(x).count('4') == str(x).count('7'):
		a.append(x)
	elif x < 1000000000000.0:
		luckynumwithequalnumberoffourandseven(x * 10 + 4, n, a)
		luckynumwithequalnumberoffourandseven(x * 10 + 7, n, a)
	return a

def luckynum(x, l, r, a):
	if x >= l and x <= r:
		a.append(x)
	if x > r:
		a.append(x)
		return a
	if x < 10000000000.0:
		luckynum(x * 10 + 4, l, r, a)
		luckynum(x * 10 + 7, l, r, a)
	return a

def luckynuber(x, n, a):
	p = set(str(x))
	if len(p) <= 2:
		a.append(x)
	if x < n:
		luckynuber(x + 1, n, a)
	return a

def interact(type, x):
	if type == 'r':
		inp = input()
		return inp.strip()
	else:
		print(x, flush=True)

def findTrailingZeros(n):
	count = 0
	while n >= 5:
		n //= 5
		count += n
	return count

def mergeSort(arr):
	if len(arr) > 1:
		mid = len(arr) // 2
		L = arr[:mid]
		R = arr[mid:]
		mergeSort(L)
		mergeSort(R)
		i = j = k = 0
		while i < len(L) and j < len(R):
			if L[i] < R[j]:
				arr[k] = L[i]
				i += 1
			else:
				arr[k] = R[j]
				j += 1
			k += 1
		while i < len(L):
			arr[k] = L[i]
			i += 1
			k += 1
		while j < len(R):
			arr[k] = R[j]
			j += 1
			k += 1
res = set()

def solven(p, l, a, b, n):
	if p > n or l > 10:
		return
	if p > 0:
		res.add(p)
	solven(p * 10 + a, l + 1, a, b, n)
	solven(p * 10 + b, l + 1, a, b, n)

def subsetsUtil(A, subset, index, d):
	print(*subset)
	s = sum(subset)
	d.append(s)
	for i in range(index, len(A)):
		subset.append(A[i])
		subsetsUtil(A, subset, i + 1, d)
		subset.pop(-1)
	return d

def subsetSums(arr, l, r, d, sum=0):
	if l > r:
		d.append(sum)
		return
	subsetSums(arr, l + 1, r, d, sum + arr[l])
	subsetSums(arr, l + 1, r, d, sum)
	return d

def print_factors(x):
	factors = []
	for i in range(1, x + 1):
		if x % i == 0:
			factors.append(i)
	return factors

def calc(X, d, ans, D):
	if len(X) == 0:
		return
	i = X.index(max(X))
	ans[D[max(X)]] = d
	Y = X[:i]
	Z = X[i + 1:]
	calc(Y, d + 1, ans, D)
	calc(Z, d + 1, ans, D)

def factorization(n, l):
	c = n
	if prime(n) == True:
		l.append(n)
		return l
	for i in range(2, c):
		if n == 1:
			break
		while n % i == 0:
			l.append(i)
			n = n // i
	return l

def good(b):
	l = []
	i = 0
	while len(b) != 0:
		if b[i] < b[len(b) - 1 - i]:
			l.append(b[i])
			b.remove(b[i])
		else:
			l.append(b[len(b) - 1 - i])
			b.remove(b[len(b) - 1 - i])
	if l == sorted(l):
		return True
	return False

def generate(st, s):
	if len(s) == 0:
		return
	if s not in st:
		st.add(s)
		for i in range(len(s)):
			t = list(s).copy()
			t.remove(s[i])
			t = ''.join(t)
			generate(st, t)
	return

def largestincreasingsubsequence(A):
	l = [1] * len(A)
	sub = []
	for i in range(1, len(l)):
		for k in range(i):
			if A[k] < A[i]:
				sub.append(l[k])
		l[i] = 1 + max(sub, default=0)
	return max(l, default=0)

def findOR(nums, N):
	prefix_sum = 0
	result = 0
	for i in range(N):
		result |= nums[i]
		prefix_sum += nums[i]
		result |= prefix_sum
	return result

def OR(a, n):
	ans = a[0]
	for i in range(1, n):
		ans |= a[i]
	return ans

def main():
	for _ in range(int(input())):
		n = int(input())
		pp = str(n)
		qqq = pp[0] * len(pp)
		ans1 = ''
		if int(qqq) <= n:
			ans1 = qqq
		elif pp[0] == '1':
			for i in range(len(pp) - 1):
				ans1 += '9'
		else:
			ans1 += str(int(pp[0]) - 1) * len(pp)
		q = len(ans1)
		p = ans1
		p = int(p[0])
		ans = q * 9 - (9 - p)
		print(ans)
main()


import sys
input = sys.stdin.readline
output = sys.stdout.write

def main():
	tests = int(input().rstrip())
	for i in range(tests):
		num = input().rstrip()
		first_ = num[0]
		length_ = len(num)
		state = True
		term = int(first_)
		for j in range(1, length_):
			next_ = int(num[j])
			if term < next_:
				break
			elif term == next_:
				continue
			else:
				state = False
				break
		initial = int(first_) - 1
		added = 9 * (length_ - 1)
		initial += added
		if state:
			initial += 1
		output(str(initial))
		output('\n')
main()


for _ in range(int(input())):
	n = int(input())
	if n < 10 and n != 0:
		print(n)
	else:
		s = str(n)
		print(9 * (len(s) - 1) + int(s[0]) - 1 + [0, 1][n >= int(len(s) * s[0])])


from __future__ import division, print_function
import os
from collections import defaultdict
import sys
from io import BytesIO, IOBase
from sys import setrecursionlimit
setrecursionlimit(1500)
if sys.version_info[0] < 3:
	from __builtin__ import xrange as range
	from future_builtins import ascii, filter, hex, map, oct, zip

def sol(n, s):
	(c, digits) = (0, len(s))
	d = s[0]
	start = int(d * digits)
	d = int(d)
	while d >= 1 and start > n:
		d -= 1
		start = int(d * digits)
	c += d
	c += (digits - 1) * 9
	print(c)

def main():
	for _ in range(int(input())):
		n = int(input())
		sol(n, str(n))
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


for _ in range(int(input())):
	n = int(input())
	cou = 0
	for j in range(1, 10):
		st = str(j)
		for i in range(10):
			if int(st) <= n:
				cou += 1
				st += str(j)
	print(cou)


import math
for i in range(int(input())):
	res = int(input())
	base = 9 * (len(str(res)) - 1)
	a = math.floor(res / int(len(str(res)) * '1'))
	print(base + a)


for _ in range(int(input())):
	n = int(input())
	if n < 10:
		print(n)
	else:
		tmp = 1
		ans = 9
		while tmp <= n:
			tmp = tmp * 10 + 1
			for j in range(1, 10):
				if 1 <= tmp * j and tmp * j <= n:
					ans += 1
		print(ans)


alp = int(input())
num = []
for i in range(alp):
	s = input()
	num.append(s)
tes = [11, 111, 1111, 11111, 111111, 1111111, 11111111, 111111111]

def main(arr):
	return 9 * (len(arr) - 1) + int(arr) // int('1' * len(arr))
for i in num:
	print(main(i))


import random
import math
from collections import defaultdict
import itertools
from sys import stdin, stdout
import operator

def main():
	z = ''
	for _ in range(int(stdin.readline())):
		n = int(input())
		a = [1, 11, 111, 1111, 11111, 111111, 1111111, 11111111, 111111111, 1111111111]
		p = n
		d = 0
		while n != 0:
			d += 1
			n //= 10
		ans = 0
		ld = p // 10 ** (d - 1)
		for i in range(d - 1):
			ans += 9
		if p >= ld * a[d - 1]:
			ans += ld
		else:
			ans += ld - 1
		print(ans)
main()


def zauryad_sonlar_soni(s):
	len_son = len(s)
	Sum = (len_son - 1) * 9
	s = list(s)
	for i in range(len(s)):
		s[i] = int(s[i])
	if len_son == 1:
		return s[0]
	Sum += s[0] - 1
	for i in range(1, len_son):
		if s[i] > s[0]:
			Sum += 1
			return Sum
		elif s[i] < s[0]:
			return Sum
	return Sum + 1
t = int(input())
natija = []
for i in range(t):
	n = input()
	natija.append(zauryad_sonlar_soni(n))
for i in range(t):
	print(natija[i])


a = []
for i in range(1, 10):
	s = ''
	for j in range(9):
		s += str(i)
		a.append(int(s))
a.sort()
for j in range(int(input())):
	n = int(input())
	c = 0
	for i in a:
		if i > n:
			break
		c += 1
	print(c)


t = int(input())
for _ in range(t):
	n = int(input())
	ans = 0
	for i in range(1, 10):
		s = str(i)
		while int(s) <= n:
			ans += 1
			s += str(i)
	print(ans)


T = int(input())
for t in range(T):
	n = int(input())
	s = 1
	end = 10
	result = 0
	if n <= 10:
		print(min(n, 9))
	else:
		while n > end:
			if n > end:
				result += 9
			end *= 10
			s = s * 10 + 1
		result += n // s
		print(result)


t = int(input())
numbers = [input() for i in range(t)]
results = []
for i in range(t):
	counter = 0
	n = numbers[i]
	if n.__len__() == 1:
		counter += int(n)
	else:
		counter += 9 * (n.__len__() - 1)
		if int(''.join([n[0] for i in range(n.__len__())])) <= int(n):
			counter += int(n[0]) - 1 + 1
		else:
			counter += int(n[0]) - 1
	results.append(counter)
[print(i) for i in results]


for _ in range(int(input())):
	s = input()
	n = int(s)
	print(9 * (len(s) - 1) + int(s[0]) - 1 + [0, 1][n >= int(len(s) * s[0])])


for _ in range(int(input())):
	num = int(input())
	if num < 10:
		print(num)
	else:
		L = list(str(num))
		length = len(L)
		if L[0] == '1' and L.count('0') == length - 1:
			print(9 * (length - 1))
		else:
			string = L[0] * length
			if num >= int(string):
				ans = 9 * (length - 1) + int(L[0])
			else:
				ans = 9 * (length - 1) + int(L[0]) - 1
			print(ans)


def check(n):
	v = 0
	list1 = str(n)
	le = len(list1)
	v += 9 * (le - 1)
	v += int(list1[0]) - 1
	m = ''
	for y in range(le):
		m += str(list1[0])
	if int(m) <= n:
		v += 1
	return v
t = int(input())
listf = []
for x in range(t):
	n = int(input())
	listf.append(check(n))
for g in listf:
	print(g)


for _ in range(int(input())):
	n = input()
	k = len(n)
	n = int(n)
	p = int('1' * k)
	a = n // p + 9 * (k - 1)
	print(a)


def func():
	n = int(input())
	l = len(str(n))
	t = 9 * (l - 1) + n // int('1' * l)
	print(t)

def main():
	for _ in range(int(input())):
		func()
main()


t = int(input())
for i in range(t):
	n = int(input())
	z = 0
	if n > 10:
		lenght = len(str(n))
		s = int('1' * lenght)
		k = n // s
		print((lenght - 1) * 9 + k)
	elif n == 10:
		print(9)
	else:
		print(n)


for _ in range(int(input())):
	n = int(input())
	ans = 0
	s = str(n)
	k = len(s)
	last = '1' + '0' * (k - 1)
	ans = ans + (k - 1) * 9
	for i in range(1, 10):
		if int(str(i) * k) <= n:
			ans = ans + 1
	print(ans)


n = int(input())
ans = []
for i in range(n):
	current_num = input()
	current_ans = (len(current_num) - 1) * 9 + (int(current_num[0]) if int(current_num) >= int('1' * len(current_num)) * int(current_num[0]) else int(current_num[0]) - 1)
	ans.append(current_ans)
for j in ans:
	print(j)


t = int(input())
for i in range(t):
	n = input()
	base = (len(n) - 1) * 9
	extra = int(n[0]) - 1
	bingo = int(n[0] * len(n))
	if int(n) >= bingo:
		extra += 1
	print(base + extra)


for i in range(int(input())):
	n = int(input())
	if n > 9:
		ans = 9
		tmp = 2
		flag = True
		while True:
			for c in range(1, 10):
				if int(str(c) * tmp) > n:
					flag = False
					break
				ans += 1
			if not flag:
				break
			tmp += 1
		print(ans)
	else:
		print(n)


import math
t = int(input())
for _ in range(t):
	n = int(input())
	l = int(math.log10(n)) + 1
	print((l - 1) * 9 + n // int(l * '1'))


import sys
input = sys.stdin.readline
for _ in range(int(input())):
	n = int(input())
	res = 0
	for x in range(1, 10):
		for d in range(1, 10):
			if x * (10 ** d - 1) // 9 <= n:
				res += 1
	print(res)


from __future__ import division, print_function
import os
import sys
from io import BytesIO, IOBase
import math
if sys.version_info[0] < 3:
	from __builtin__ import xrange as range
	from future_builtins import ascii, filter, hex, map, oct, zip

def inp():
	return int(input())

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

def insr():
	s = input()
	return list(s[:len(s) - 1])

def invr():
	return map(int, input().split())
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
mod = 10 ** 9 + 7
for _ in range(inp()):
	n = inp()
	c = 0
	s = str(n)
	c = c + 9 * (len(s) - 1)
	y = s[0] * len(s)
	if int(y) <= n:
		c += int(s[0])
	else:
		c = c + int(s[0]) - 1
	print(c)


t = int(input())
for i in range(t):
	a = input()
	a_first = a[0]
	a_len = len(a)
	min_on = 9 * (a_len - 1)
	relevant_bound = a_first * a_len
	a_first_num = int(a_first)
	if int(a) < int(relevant_bound):
		a_first_num -= 1
	print(min_on + a_first_num)


import math

def printAns(a):
	if a < 10:
		print(a)
	elif a >= 10 and a < 11:
		print(9)
	elif a >= 11 and a < 110:
		print(9 + math.floor(a / 11))
	elif a >= 110 and a < 111:
		print(18)
	elif a >= 111 and a < 1110:
		print(18 + math.floor(a / 111))
	elif a >= 1110 and a < 1111:
		print(27)
	elif a >= 1111 and a < 11110:
		print(27 + math.floor(a // 1111))
	elif a >= 11110 and a < 11111:
		print(36)
	elif a >= 11111 and a < 111110:
		print(36 + math.floor(a / 11111))
	elif a >= 111110 and a < 111111:
		print(36)
	elif a >= 111111 and a < 1111110:
		print(45 + math.floor(a / 111111))
	elif a >= 1111110 and a < 1111111:
		print(54)
	elif a >= 1111111 and a < 11111110:
		print(54 + math.floor(a / 1111111))
	elif a >= 11111110 and a < 11111111:
		print(63)
	elif a >= 11111111 and a < 111111110:
		print(63 + math.floor(a / 11111111))
	elif a >= 111111110 and a < 111111111:
		print(72)
	elif a >= 111111111 and a < 1111111110:
		print(72 + math.floor(a / 111111111))
	elif a >= 1111111110 and a < 1111111111:
		print(81)
t = int(input())
for _ in range(t):
	a = int(input())
	printAns(a)


t = int(input())
while t != 0:
	t = t - 1
	n = int(input())
	s = str(n)
	k = len(s)
	s1 = str(1)
	c = 9 * (k - 1)
	for i in range(k - 1):
		s1 += '1'
	n1 = int(s1)
	j = 1
	while n1 * j <= n:
		j = j + 1
		c = c + 1
	print(c)


import io, os
import sys
input = lambda : sys.stdin.readline().rstrip()

def solve_tc():
	n = int(input())
	c = 0
	k = 0
	rom = [1, 11, 111, 1111, 11111, 111111, 1111111, 11111111, 111111111, 1111111111, 11111111111, 2, 22, 222, 2222, 22222, 222222, 2222222, 22222222, 222222222, 2222222222, 22222222222, 3, 33, 333, 3333, 33333, 333333, 3333333, 33333333, 333333333, 3333333333, 33333333333, 4, 44, 444, 4444, 44444, 444444, 4444444, 44444444, 444444444, 4444444444, 44444444444, 5, 55, 555, 5555, 55555, 555555, 5555555, 55555555, 555555555, 5555555555, 55555555555, 6, 66, 666, 6666, 66666, 666666, 6666666, 66666666, 666666666, 6666666666, 66666666666, 7, 77, 777, 7777, 77777, 777777, 7777777, 77777777, 777777777, 7777777777, 77777777777, 8, 88, 888, 8888, 88888, 888888, 8888888, 88888888, 888888888, 8888888888, 88888888888, 9, 99, 999, 9999, 99999, 999999, 9999999, 99999999, 999999999, 9999999999, 99999999999]
	rom.sort()
	for i in rom:
		c += 1
		if i > n:
			return str(c - 1)
t = int(input())
while t > 0:
	t -= 1
	sys.stdout.write(solve_tc())
	sys.stdout.write('\n')


import math
t = int(input())
l = []
for i in range(0, t):
	n = int(input())
	l.append(n)
for j in range(0, t):
	a = l[j]
	b = int(math.log(a, 10))
	s = '1' * (b + 1)
	k = int(s)
	print(int(9 * b + a // k))


import math
from sys import stdin, stdout

def solve():
	dp = []
	for x in range(1, 10):
		for y in range(1, 10):
			p = x * str(y)
			dp.append(int(p))
	dp.sort()
	t = int(input())
	while t:
		t -= 1
		n = int(input())
		ans = sum((1 for x in dp if x <= n))
		print(ans)
solve()


import collections as cc
import bisect as bi
I = lambda : list(map(int, input().split()))
for tc in range(int(input())):
	(n,) = I()
	dic = []
	for i in range(1, 10):
		for j in range(1, 10):
			dic.append(int(str(i) * j))
	dic.sort()
	res = bi.bisect(dic, n)
	print(res)


for _ in range(int(input())):
	n = int(input())
	length = len(list(str(n)))
	answer = 9 * (length - 1)
	num = int(str(1) * length)
	answer += n // num
	print(answer)


import sys
input_str = sys.stdin.read()
input_arr = input_str.split('\n')
t = int(input_arr[0])
input_arr = input_arr[1:]
results = []
for i in range(t):
	n = int(input_arr[i])
	div = int('1' * len(input_arr[i]))
	results.append(n // div + 9 * (len(input_arr[i]) - 1))
print('\n'.join([str(val) for val in results]))


def Sort(sub_li):
	return sorted(sub_li, key=lambda x: x[1])
import math
n = int(input())
mod = 10 ** 9 + 7
for i in range(0, n):
	o = int(input())
	A = list(str(o))
	if len(A) == 1:
		print(o)
	else:
		ans = (len(A) - 1) * 9
		W = A[0]
		ans += int(W)
		ans -= 1
		G = -1
		for j in range(0, len(A)):
			if int(A[j]) > int(W):
				G = 1
				break
			elif int(A[j]) < int(W):
				G = 0
				break
		if G == 1 or G == -1:
			ans += 1
		print(ans)


t = int(input())
for tc in range(t):
	s = input()
	n = int(s)
	l = len(s)
	if l == 1:
		print(n)
	else:
		ch = ''
		for i in range(l):
			ch = ch + s[0]
		if n >= int(ch):
			print((l - 1) * 9 + int(s[0]))
		else:
			print((l - 1) * 9 + int(s[0]) - 1)


def calculateNumOrdinary(str_n):
	n = len(str_n)
	ones = n * '1'
	return (n - 1) * 9 + int(str_n) // int(ones)
output = []
for i in range(int(input())):
	n = input()
	output.append(calculateNumOrdinary(n))
print(*output, sep='\n')


n = int(input())
count = 0
for i in range(0, n):
	x = input()
	k = len(x)
	x = int(x)
	p = int('1' * k)
	a = x // p + 9 * (k - 1)
	print(a)


for _ in range(int(input())):
	n = int(input())
	p = len(str(n))
	count = (p - 1) * 9
	for i in range(1, 10):
		if int(str(i) * p) > n:
			break
		else:
			count += 1
	print(count)


for i in range(int(input())):
	a = int(input())
	if a < 10:
		print(a)
	else:
		for i in range(2, 10):
			d = 9 * (i - 1)
			if a <= 10 ** i:
				b = '1' * i
				n = int(b)
				d = d + a // n
				print(d)
				break


n = int(input())
for i in range(n):
	t = input()
	s = (len(t) - 1) * 9
	b = ''
	for j in t:
		b += '1'
	f = int(t) // int(b)
	if len(t) == 1:
		print(t)
	else:
		print(f + s)


for t in range(int(input())):
	n = int(input())
	n2 = n
	s = 0
	while n >= 10:
		s += min(n, 9)
		n //= 10
	for i in range(1, 10):
		if int(str(i) * len(str(n2))) <= n2:
			s += 1
	print(s)


nn = int(input())
res = []
for _ in range(nn):
	n = int(input())
	r = 0
	if n <= 9:
		res.append(n)
		continue
	x = len(str(n))
	while n > 9:
		if n // int('1' * x) == 0:
			n -= 1
		else:
			r += n // int('1' * x)
			n = 10 ** (x - 1) - 1
		x -= 1
	res.append(r + 9)
for i in res:
	print(i)


import math

def count_same_digit(N):
	temp = 0
	answer = 0
	n = int(math.log10(N) + 1)
	for i in range(0, n):
		temp = temp * 10 + 1
		for j in range(1, 10):
			if 1 <= temp * j and temp * j <= N:
				answer += 1
	return answer
for _ in range(int(input())):
	n = int(input())
	print(count_same_digit(n))


t = int(input())
for i in range(t):
	count = 0
	n = int(input())
	l = len(str(n))
	if n <= 9:
		count = n
	else:
		div = ['1'] * l
		s = ''.join(div)
		count = count + (l - 1) * 9
		count = count + n // int(s)
	print(count)


def get(x):
	ten = 10
	tenCount = 0
	temp = x
	singnificant = 0
	ans = 0
	while temp:
		significant = temp % 10
		temp //= 10
		tenCount += 1
		if temp:
			ans += 9
	num = int(str(significant) * tenCount)
	if x < num:
		return ans + (significant - 1)
	return ans + significant
t = int(input())
while t:
	x = int(input())
	print(get(x))
	t -= 1


import bisect
nums = []
for i in range(1, 10):
	for j in range(1, 10):
		nums.append(i * str(j))
nums = nums
nums = list(map(int, nums))
n = int(input())
nums.sort()
nums.append(float('inf'))
for i in range(n):
	x = int(input())
	ans = 0
	while nums[ans] <= x:
		ans += 1
	print(ans)


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

def mapIn():
	return map(int, input().split())
for ii in range(int(input())):
	n = int(input())
	i = 1
	b = 9
	x = 1
	ans = 0
	while i <= n:
		i += x
		ans += 1
		if i > n:
			break
		if i == b:
			x = '1' * (len(str(x)) + 1)
			x = int(x)
			b = '9' * (len(str(b)) + 1)
			b = int(b)
			i = x
			ans += 1
	print(ans)


T = int(input())
for _ in range(T):
	N = int(input())
	dd = N - 1
	num1 = int(len(str(N)) - 1) * 9
	num2 = N // 10 ** int(len(str(N)) - 1) - 1
	num3 = N >= int(str(N)[0] * len(str(N)))
	if num3:
		res = 1
	else:
		res = 0
	print(res + num1 + num2)


import math
for _ in range(int(input())):
	n = int(input())
	s = len(str(n))
	a = str(n)
	b = int(a[0])
	c = 0
	j = 10 ** (s - 1)
	while j > 0:
		c = c + b * j
		j = j // 10
	k = 9 * (s - 1)
	k = k + b - 1
	if n >= int(c):
		k = k + 1
	print(k)


def returnCount(n):
	if n >= 1 and n <= 9:
		return n
	temp = str(n)
	leng = len(temp)
	ubound = ''
	for _ in range(0, leng):
		ubound += '0'
	ubound = '1' + ubound
	ubound = int(ubound)
	t = ubound // 10
	count = 9 * leng
	diff = ubound - n
	count -= diff // t
	temp2 = temp[0]
	for _ in range(leng - 1):
		temp2 += temp[0]
	if int(temp2) > n:
		count -= 1
	if n % t == 0:
		count += 1
	return count
testcases = int(input())
while testcases:
	n = int(input())
	result = returnCount(n)
	print(result)
	testcases -= 1


import math

def check(L, R):
	count = 0
	tmp = 0
	n = int(math.log10(R) + 1)
	for i in range(0, n):
		tmp = tmp * 10 + 1
		for j in range(1, 10):
			if L <= tmp * j and tmp * j <= R:
				count += 1
	return count
n = int(input())
for i in range(n):
	k = int(input())
	print(check(1, k))


arr = [11, 22, 33, 44, 55, 66, 77, 88, 99, 111, 222, 333, 444, 555, 666, 777, 888, 999, 1111, 2222, 3333, 4444, 5555, 6666, 7777, 8888, 9999, 11111, 22222, 33333, 44444, 55555, 66666, 77777, 88888, 99999, 111111, 222222, 333333, 444444, 555555, 666666, 777777, 888888, 999999, 1111111, 2222222, 3333333, 4444444, 5555555, 6666666, 7777777, 8888888, 9999999, 11111111, 22222222, 33333333, 44444444, 55555555, 66666666, 77777777, 88888888, 99999999, 111111111, 222222222, 333333333, 444444444, 555555555, 666666666, 777777777, 888888888, 999999999]
for _ in range(int(input())):
	n = int(input())
	count = 0
	if n < 10:
		print(n)
		continue
	count += 9
	for i in range(len(arr)):
		if n == arr[i]:
			count += i + 1
			break
		elif n < arr[i]:
			count += i
			break
	else:
		count += len(arr)
	print(count)


test = int(input())
for t in range(test):
	n = int(input())
	nn = n
	num = []
	ln = 0
	while nn:
		ln += 1
		num.append(nn % 10)
		nn //= 10
	ans = (ln - 1) * 9
	num.reverse()
	f = num[0]
	new = 0
	for i in range(0, ln):
		new *= 10
		new += 1
	new *= f
	if n >= new:
		print(ans + f)
	else:
		print(ans + f - 1)


from sys import stdin
for _ in range(int(stdin.readline())):
	n = int(stdin.readline())
	c = 9
	if n <= 9:
		print(n)
		continue
	elif n >= 10 and n <= 100:
		c += n // 11
	elif n >= 100 and n <= 1000:
		c += 9
		c += n // 111
	elif n >= 1000 and n <= 10000:
		c += 18
		c += n // 1111
	elif n >= 10000 and n <= 100000:
		c += 27
		c += n // 11111
	elif n >= 100000 and n <= 1000000:
		c += 36
		c += n // 111111
	elif n >= 1000000 and n <= 10000000:
		c += 45
		c += n // 1111111
	elif n >= 10000000 and n <= 100000000:
		c += 54
		c += n // 11111111
	elif n >= 100000000 and n <= 1000000000:
		c += 63
		c += n // 111111111
	print(c)


for i in range(int(input())):
	n = int(input())
	if n < 10:
		print(n)
	else:
		s = len(str(n))
		x = 9 * (s - 1)
		x += n // int('1' * s)
		print(x)


import bisect, sys
input = sys.stdin.readline
MAXN = 10 ** 9
t = int(input())
nums = []
p = 1
while p <= MAXN:
	for d in range(1, 10):
		nums.append(p * d)
	p = 10 * p + 1
for _ in range(t):
	n = int(input())
	print(bisect.bisect(nums, n))


t = int(input())
while t != 0:
	num = input()
	fa = 0
	for i in range(len(num) - 1):
		fa += 9
	tt = num[0]
	if int(tt * len(num)) <= int(num):
		fa += int(tt)
	else:
		fa += int(tt) - 1
	print(fa)
	t -= 1


import math
MOD = 10 ** 9 + 7
sv = math.sqrt(5)
fip = (1 + sv) / 2
fin = (1 - sv) / 2
F = lambda i: (fip ** i - fin ** i) // sv
L = lambda i: (fip ** i + fin ** i) // 1
ln = len

def pfs(n):
	fs = []
	i = 2
	while i * i <= n:
		while n % i == 0:
			n = n // i
			factors.append(i)
		i += 1
	return (factors + [n] * (n != 1))[::-1]

def in_var(to: type):
	return to(input())

def in_arr(to: type):
	return list(map(to, input().split()))

def in_ivar():
	return in_var(int)

def in_iarr():
	return in_arr(int)

def solve():
	n = in_ivar()
	l = len(str(n))
	count = 9 * (len(str(n)) - 1)
	for i in range(1, 10):
		if str(i) * l <= str(n):
			count += 1
	print(count)

def main():
	for _ in range(in_ivar()):
		solve()
	return
main()


import math
for _ in range(int(input())):
	n = int(input())
	(t, x) = (0, 0)
	a = int(math.log10(n) + 1)
	for i in range(0, a):
		t = t * 10 + 1
		for j in range(1, 10):
			if 1 <= t * j and t * j <= n:
				x += 1
	print(x)


t = int(input())
result = []
for i in range(t):
	n = int(input())
	if n < 10:
		result.append(n)
	else:
		length = len(str(n))
		compareTo = int('1' * length)
		result.append(9 * (length - 1) + n // compareTo)
for i in result:
	print(i)


from collections import deque, defaultdict
from math import sqrt, ceil, factorial, floor, inf, gcd
import bisect
import sys
import copy

def get_array():
	return list(map(int, sys.stdin.readline().strip().split()))

def get_ints():
	return map(int, sys.stdin.readline().strip().split())

def input():
	return sys.stdin.readline().strip()
for _ in range(int(input())):
	n = int(input())
	l = len(str(n))
	f_digit = int(str(n)[0])
	if n >= int(str(f_digit) * l):
		print((l - 1) * 9 + f_digit)
	else:
		print((l - 1) * 9 + f_digit - 1)


from math import floor, log10
for _ in range(int(input())):
	n = int(input())
	nd = floor(log10(n)) + 1
	c = (nd - 1) * 9
	c += n // int('1' * nd)
	print(c)


times = int(input().rstrip())
for i in range(times):
	num = input().rstrip()
	ans = 0
	sigdig = len(num)
	num = int(num)
	ans = 9 * (sigdig - 1) + num // int(''.join(['1' for i in range(sigdig)]))
	print(ans)


def solve(n):
	if n < 10:
		return n
	l = [str(i) for i in range(1, 10)]
	ans = 9
	j = 0
	i = 0
	while 1:
		i = int(l[j] + l[j][0])
		if i <= n:
			ans += 1
			l[j] += l[j][0]
			if j >= 8:
				j = 0
			else:
				j += 1
		else:
			return ans
t = int(input())
for _ in range(t):
	n = int(input())
	print(solve(n))


for _ in range(0, int(input())):
	i = input()
	print(9 * (len(i) - 1) + (int(i[0]) - 1) + (int(i) >= int(i[0] * len(i))))