Hooray! Polycarp turned $n$ years old! The Technocup Team sincerely congratulates Polycarp!

Polycarp celebrated all of his $n$ birthdays: from the $1$-th to the $n$-th. At the moment, he is wondering: how many times he turned beautiful number of years?

According to Polycarp, a positive integer is beautiful if it consists of only one digit repeated one or more times. For example, the following numbers are beautiful: $1$, $77$, $777$, $44$ and $999999$. The following numbers are not beautiful: $12$, $11110$, $6969$ and $987654321$.

Of course, Polycarpus uses the decimal numeral system (i.e. radix is 10).

Help Polycarpus to find the number of numbers from $1$ to $n$ (inclusive) that are beautiful.


-----Input-----

The first line contains an integer $t$ ($1 \le t \le 10^4$) — the number of test cases in the input. Then $t$ test cases follow.

Each test case consists of one line, which contains a positive integer $n$ ($1 \le n \le 10^9$) — how many years Polycarp has turned.


-----Output-----

Print $t$ integers — the answers to the given test cases in the order they are written in the test. Each answer is an integer: the number of beautiful years between $1$ and $n$, inclusive.


-----Example-----
Input
6
18
1
9
100500
33
1000000000

Output
10
1
9
45
12
81



-----Note-----

In the first test case of the example beautiful years are $1$, $2$, $3$, $4$, $5$, $6$, $7$, $8$, $9$ and $11$.

Difficulty: EASY
Tags: Implementation

Starter code:

Solutions:
s = []
for i in range(1, 10):
	k = 0
	for l in range(1, 10):
		k *= 10
		k += i
		s.append(k)
s.sort()
q = int(input())
while q:
	n = int(input())
	l = 0
	r = len(s)
	while l + 1 < r:
		m = (l + r) // 2
		if s[m] <= n:
			l = m
		else:
			r = m
	print(r)
	q -= 1


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


for t in range(int(input())):
	n = int(input())
	tt = 1
	razr = 1
	c = 0
	while int(str(tt) * razr) <= n:
		c += 1
		tt += 1
		if tt == 10:
			tt = 1
			razr += 1
	print(c)


for i in range(int(input())):
	n = int(input())
	ans = 0
	for k in range(1, 11):
		for j in range(1, 10):
			s = str(j) * k
			if int(s) <= n:
				ans += 1
	print(ans)


for t in range(int(input())):
	a = input()
	out = 9 * (len(a) - 1)
	for i in range(1, 10):
		if int(a) >= int(str(i) * len(a)):
			out += 1
		else:
			break
	print(out)


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


N = int(input())

def check(n, cifra):
	f = cifra
	count = 0
	while cifra <= n:
		cifra = cifra * 10 + f
		count += 1
	return count
for i in range(N):
	a = int(input())
	print(check(a, 1) + check(a, 2) + check(a, 3) + check(a, 4) + check(a, 5) + check(a, 6) + check(a, 7) + check(a, 8) + check(a, 9))


q = int(input())
while q:
	a = int(input())
	g = len(str(a))
	ans = (g - 1) * 9
	h = (pow(10, g) - 1) / 9
	x = h
	while a - (x + h) >= 0:
		x += h
	ans += int(str(x)[0])
	if x > a:
		ans -= 1
	print(ans)
	q -= 1


tests = int(input())
for test in range(tests):
	n = int(input())
	l = len(str(n))
	c = 0
	for i in range(1, l + 1):
		for j in range(1, 10):
			a = int(str(j) * i)
			if a <= n:
				c += 1
	print(c)


for _ in range(int(input())):
	limit = int(input())
	count = 0
	for numb in range(1, 10):
		i = 1
		while int(str(numb) * i) <= limit:
			count += 1
			i += 1
	print(count)


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


t = int(input())
for kkk in range(t):
	n = int(input())
	ans = 0
	nn = len(str(n))
	for i in range(1, 10):
		ans += nn - (1 if int(str(i) * nn) > n else 0)
	print(ans)


t = int(input())
all = []
for i in range(1, 10):
	for j in range(1, 10):
		all.append(int(str(i) * j))
for i in range(t):
	n = int(input())
	ans = 0
	for i in range(len(all)):
		if all[i] <= n:
			ans += 1
	print(ans)


n = int(input())
for i in range(n):
	t = int(input())
	c = 0
	for r in range(1, 10):
		for k in range(1, 11):
			if int(str(r) * k) <= t:
				c += 1
	print(c)


t = int(input())
ans = []
for j in range(t):
	n = int(input())
	s = ''
	k = 0
	for i in range(1, 10):
		s = str(i)
		while int(s) <= n:
			k += 1
			s += str(i)
	ans.append(k)
for i in ans:
	print(i)


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


k = int(input())
a = []
for i in range(1, 10):
	for i1 in range(1, 10):
		a.append(str(i) * i1)
for _ in range(k):
	n = int(input())
	ans = 0
	for i1 in a:
		if int(i1) <= n:
			ans += 1
	print(ans)


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


for i in range(int(input())):
	x = int(input())
	ans = 0
	ans += 9 * (len(str(x)) - 1)
	ans += int(str(x)[0]) - 1
	if int(str(x)[0] * len(str(x))) <= x:
		ans += 1
	print(ans)


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


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


t = int(input())
for _ in range(t):
	n = input()
	l = len(n)
	n = int(n)
	c = 0
	for i in range(1, 10):
		x = i
		while x <= n:
			c += 1
			x = int(str(x) + str(i))
	print(c)


for __ in range(int(input())):
	n = int(input())
	x = n
	num = 0
	while x >= 10:
		x //= 10
		num += 1
	ans = num * 9
	ans += n // int('1' * (num + 1))
	print(ans)


g = int(input())
for i in range(g):
	a = int(input())
	ans = 0
	if a >= 10:
		ans += 9
	else:
		ans += a
		print(ans)
		continue
	for i in range(2, 11):
		if 10 ** i <= a:
			ans += 9
		else:
			for j in range(1, 10):
				if int(str(j) * i) <= a:
					ans += 1
				else:
					break
			break
	print(ans)


t = int(input())
for i in range(t):
	r = int(input())
	a = [1, 2, 3, 4, 5, 6, 7, 8, 9]
	ans = 0
	i = 0
	while True:
		if i == 9:
			i = 0
		if a[i] <= r:
			ans += 1
		if a[i] > r:
			break
		a[i] = a[i] * 10 + a[i] % 10
		i += 1
	print(ans)


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


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


for _ in range(int(input())):
	N = input()
	kolvo = (len(N) - 1) * 9
	for i in range(1, 10):
		kolvo += int(str(i) * len(N)) <= int(N)
	print(kolvo)


t = int(input())
arr = []
while t > 0:
	t -= 1
	n = int(input())
	ans = 9 * (len(str(n)) - 1)
	for i in range(1, 10):
		if int(str(i) * len(str(n))) <= n:
			ans += 1
	arr.append(ans)
print(*arr, sep='\n')


a = ['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', '1111111111', '2222222222', '3333333333', '4444444444', '5555555555', '6666666666', '7777777777', '8888888888', '9999999999']
t = int(input())
for i in range(t):
	n = int(input())
	j = 0
	ans = 0
	while j < len(a) and n >= int(a[j]):
		ans += 1
		j += 1
	print(ans)


t = int(input())
for j in range(t):
	n = int(input())
	n1 = str(n)
	a = 0
	while n // 10:
		a += 9
		n = n // 10
	a += n - 1
	if str(n) * len(n1) <= n1:
		a += 1
	print(a)


t = int(input())
while t > 0:
	n = int(input())
	ans = 0
	for i in range(1, 10):
		for j in range(1, 10):
			if int(str(i) * j) <= n:
				ans += 1
	print(ans)
	t -= 1


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


t = int(input())
for i in range(t):
	n = int(input())
	a = 0
	p = '1'
	c = 1
	u = 1
	while True:
		if int(p) <= n:
			a += 1
		else:
			break
		c += 1
		if c == 10:
			u += 1
			p = '1' * u
			c = 1
		else:
			p = str(int('1' * u) * c)
	print(a)


t = int(input())
a = set()
for i in range(1, 10):
	s = str(i)
	for j in range(1, 11):
		a.add(int(j * s))
b = list(a)
b.sort()
for i in range(t):
	n = int(input())
	j = 0
	while b[j] <= n:
		j += 1
	print(j)


t = int(input())
for i in range(t):
	n = int(input())
	j = 1
	while int('9' * j) <= n:
		j += 1
	ans = 9 * (j - 1)
	for k in range(1, 9):
		if int(str(k) * j) <= n:
			ans += 1
		else:
			break
	print(ans)


t = int(input())
for i in range(t):
	ns = input()
	n = int(ns)
	ans = 0
	s = ''
	for j in range(1, len(ns) + 1):
		s += '1'
		si = int(s)
		if si > n:
			break
		for k in range(1, 10):
			if si * k <= n:
				ans += 1
			else:
				break
	print(ans)


for _ in range(int(input())):
	n = int(input())
	i = 1
	j = 1
	cnt = 0
	while True:
		if n - int(str(i) * j) >= 0:
			cnt += 1
		else:
			break
		i += 1
		if i == 10:
			i = 1
			j += 1
	print(cnt)


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


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


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


n = int(input())
for i in range(n):
	k = int(input())
	l = len(str(k))
	s = 9 * (l - 1)
	for i in range(9):
		if int(str(i + 1) * l) > k:
			break
		s += 1
	print(s)


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


t = int(input())
for i in range(t):
	n = int(input())
	l = len(str(n))
	c = 0
	for j in range(1, 10):
		g = 10
		num = j
		for d in range(1, 11):
			if num <= n:
				c += 1
				num += g * j
				g *= 10
	print(c)


t = int(input())
answer = []
for i in range(t):
	n = input()
	k = (len(n) - 1) * 9 + int(n[0])
	if int(n[0] * len(n)) > int(n):
		k -= 1
	answer.append(k)
for i in answer:
	print(i)


t = int(input())
for i in range(t):
	n = int(input())
	if n == 1:
		print(1)
	else:
		i = 0
		while 10 ** i <= n:
			i += 1
		i -= 1
		k = 1
		ans = 0
		while int(str(k) * (i + 1)) <= n:
			k += 1
			ans += 1
		print(9 * i + ans)


t = int(input())
for _ in range(t):
	n = int(input())
	answer = 0
	k = 1
	while n >= 10 ** k:
		answer += 9
		k += 1
	n %= 10 ** k
	i = 1
	while n >= int(str(i) * k):
		answer += 1
		i += 1
	print(answer)


n = int(input())
for i in range(n):
	q = int(input())
	qq = str(q)
	L = len(qq)
	w = (L - 1) * 9
	for i in range(1, 10):
		if q >= int(str(i) * L):
			w += 1
		else:
			break
	print(w)


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


import sys
import math

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

def iinput():
	return int(input())

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

def rinput():
	return list(map(int, tinput()))

def rlinput():
	return list(rinput())

def main():
	n = iinput()
	res = 10
	q = 1
	while res <= n:
		res *= 10
		q += 1
	res //= 10
	q -= 1
	for i in range(q):
		res += 10 ** i
	ans = 0
	i = 1
	while res <= n and i <= 9:
		ans += 1
		res = res // i * (i + 1)
		i += 1
	print(9 * q + ans)
for i in range(iinput()):
	main()


from sys import stdin as s
for i in range(int(s.readline())):
	a = s.readline().rstrip()
	l = len(a)
	a = int(a)
	c = (l - 1) * 9
	list = [int(str(j) * l) for j in range(1, 10)]
	for j in list:
		if j <= a:
			c += 1
	print(c)


n = int(input())
for i in range(n):
	a = int(input())
	t = ['1', '2', '3', '4', '5', '6', '7', '8', '9']
	keks = 0
	c = 0
	while True:
		if int(t[keks]) > a:
			break
		else:
			c += 1
			t[keks] += t[keks][0]
			keks += 1
		if keks == 9:
			keks = 0
	print(c)


t = int(input())
for _ in range(t):
	s = input()
	g = len(s)
	cnt = 9 * (g - 1)
	let = 1
	j = int(str(let) * g)
	while j <= int(s):
		cnt += 1
		let += 1
		j = int(str(let) * g)
	print(cnt)


Q = int(input())
for _ in range(Q):
	n = int(input())
	ans = 9 * (len(str(n)) - 1)
	first = n // 10 ** (len(str(n)) - 1)
	ans += first - 1
	if n >= int(str(first) * len(str(n))):
		ans += 1
	print(ans)


q = int(input())
for _ in range(q):
	n = int(input())
	t = (len(str(n)) - 1) * 9
	for i in range(1, 10):
		if int(str(i) * len(str(n))) <= n:
			t += 1
	print(t)


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


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


def rec(l, n):
	c = 0
	for i in (1, 2, 3, 4, 5, 6, 7, 8, 9):
		if int(str(i) * l) <= n:
			c += 1
		else:
			return (c, 0)
	return (c, 1)
t = int(input())
for i in range(t):
	n = int(input())
	c = 0
	x = 1
	ans = (0, 1)
	while ans[1]:
		ans = rec(x, n)
		c += ans[0]
		x += 1
	print(c)


t = int(input())
ans_l = []
for _ in range(t):
	n = int(input())
	ans = 0
	ans += 9 * (len(str(n)) - 1)
	for i in range(1, 10):
		x = int(str(i) * len(str(n)))
		if x <= n:
			ans += 1
		else:
			break
	ans_l.append(ans)
print(*ans_l, sep='\n')


for _ in ' ' * int(input()):
	n = int(input())
	ans = len(str(n)) * 9 - 9
	k = 0
	a = int(str(k) * len(str(n)))
	while a <= n:
		a = int(str(k) * len(str(n)))
		if a <= n:
			k += 1
	print(ans + k - 1)


n = int(input())
for i in range(n):
	x = input()
	col = (len(x) - 1) * 9
	la = int(x)
	s = ''
	for i in range(len(x)):
		s += '1'
	for i in range(9):
		if int(s) * (i + 1) <= la:
			col += 1
	print(col)


t = int(input())
for q in range(t):
	n = input()
	k = len(n)
	ans = 9 * (k - 1)
	a1 = n[0]
	v = ''
	for i in range(k):
		v += a1
	n = int(n)
	v = int(v)
	if v <= n:
		ans += int(a1)
	else:
		ans += int(a1) - 1
	print(ans)


for i in range(int(input())):
	n = input()
	m = len(n)
	ans = 9 * (m - 1)
	n = int(n)
	ans += int('1' * m) <= n
	ans += int('2' * m) <= n
	ans += int('3' * m) <= n
	ans += int('4' * m) <= n
	ans += int('5' * m) <= n
	ans += int('6' * m) <= n
	ans += int('7' * m) <= n
	ans += int('8' * m) <= n
	ans += int('9' * m) <= n
	print(ans)


n = int(input())
for i in range(n):
	num = int(input())
	temp = 1
	res = 0
	temp1 = 1
	slog = 1
	slog_temp = 11
	while temp <= num:
		if temp < 10 ** temp1:
			temp += slog
			res += 1
		else:
			temp = slog_temp
			temp1 += 1
			slog = slog_temp
			slog_temp = slog_temp + 10 ** temp1
	print(res)


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


t = int(input())
for i in range(t):
	n = list(input())
	if len(n) == 1:
		print(int(n[0]))
	else:
		count = (len(n) - 1) * 9
		flag = True
		for i in range(len(n) - 1):
			if n[i] < n[i + 1]:
				break
			if n[i] > n[i + 1]:
				flag = False
				break
		if flag:
			count += int(n[0])
		else:
			count += int(n[0]) - 1
		print(count)


n = int(input())
for i in range(n):
	a = int(input())
	b = max(len(str(a)) * 9 - 9, 0)
	j = '1' * len(str(a))
	j = int(j)
	t = j
	for q in range(9):
		if t <= a:
			t += j
			b += 1
	print(b)


q = int(input())
for _ in range(q):
	n = int(input())
	z = 10
	k = 1
	ans = 0
	x = 1
	while z <= n:
		x = x * 10 + 1
		z *= 10
		k += 1
		ans += 9
	k = 1
	while n >= x * k:
		k += 1
	print(ans + k - 1)


a = int(input())
for iter in range(a):
	b = input()
	if len(b) == 1:
		print(b)
		continue
	ans = (len(b) - 1) * 9
	counter = 0
	for iter in range(1, 10):
		if int(str(iter) * len(b)) <= int(b):
			counter += 1
	print(ans + counter)


a = int(input())
for i in range(a):
	n = int(input())
	b = 1
	k = 1
	d = 0
	while b * k <= n:
		if k == 9:
			k = 0
			b *= 10
			b += 1
		d += 1
		k += 1
	print(d)


k = int(input())
for i in range(k):
	n = input()
	l = len(n)
	a = (l - 1) * 9
	x1 = int('1' * l)
	x = int('1' * l)
	n = int(n)
	while n >= x:
		a += 1
		x += x1
	print(a)


def main():
	best = []
	for i in range(1, 20):
		for j in ['1', '2', '3', '4', '5', '6', '7', '8', '9']:
			best.append(int(j * i))
	m = int(input())
	for i in range(m):
		n = int(input())
		t = 0
		for j in best:
			if n >= j:
				t += 1
		print(t)

def __starting_point():
	main()
__starting_point()


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


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


def ans(s):
	size = len(s)
	flag = True
	for i in range(1, size):
		if s[i - 1] < s[i]:
			break
		if s[i - 1] == s[i]:
			continue
		if s[i - 1] > s[i]:
			flag = False
			break
	x = 0
	if flag:
		x = int(s[0])
	else:
		x = int(s[0]) - 1
	return x + (size - 1) * 9
for i in range(int(input())):
	print(ans(input()))


lst = []
for i in range(1, 11):
	for j in range(1, 10):
		lst.append(int(str(j) * i))
lst.sort()
t = int(input())
for i in range(t):
	n = int(input())
	c = 0
	for j in lst:
		if n >= j:
			c += 1
		else:
			break
	print(c)


def f(x):
	x1 = len(x)
	x1 = '1' * x1
	return x1
n = int(input())
for i in range(n):
	a = input()
	ans = (len(a) - 1) * 9
	a1 = f(a)
	(a1, a) = (int(a1), int(a))
	ans += a // a1
	print(ans)


from math import log10
Ans = []
for i in range(int(input())):
	x = int(input())
	if x >= int(str(x)[0] * len(str(x))):
		Ans.append(int(log10(x)) * 9 + int(str(x)[0]) - 0)
	else:
		Ans.append(int(log10(x)) * 9 + int(str(x)[0]) - 1)
for a in Ans:
	print(a)


t = int(input())
for i in range(t):
	ans = 0
	t1 = int(input())
	n = len(str(t1))
	ans = 9 * (n - 1)
	l = ['1', '2', '3', '4', '5', '6', '7', '8', '9']
	for el in l:
		if int(el * n) <= t1:
			ans += 1
		else:
			break
	print(ans)


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


t = int(input())
for i in range(t):
	n = input()
	res = (len(n) - 1) * 9
	res += int(n) // int(n[0] * len(n))
	res += int(n[0]) - 1
	print(res)


t = int(input())
a = []
for i in range(t):
	n = int(input())
	a.append(n)
for i in range(t):
	ans = 0
	for j in range(1, 10):
		for k in range(1, 10):
			if int(str(j) * k) <= a[i]:
				ans += 1
	print(ans)


def main():
	t = int(input())
	for i in range(t):
		n = int(input())
		s = str(n)
		l = len(s)
		if l == 1:
			print(n)
			continue
		c = 9 * (l - 1)
		f = int(s[0] * l)
		if n >= f:
			print(c + f % 10)
		else:
			print(c + f % 10 - 1)
main()


n = int(input())
for i in range(n):
	k = int(input())
	s = 9 * (len(str(k)) - 1)
	if len(str(k)) == 1:
		print(k)
	else:
		f = int(str(k)[0])
		if int(str(f) * len(str(k))) > k:
			print(s + f - 1)
		else:
			print(s + f)


arr = [11, 111, 1111, 11111, 111111, 1111111, 11111111, 111111111]
for t in range(int(input())):
	n = int(int(input()))
	if n < 10:
		res = n
	else:
		res = 9
		for item in arr:
			if n > item * 9:
				res += 9
			else:
				res += n // item
				break
	print(res)


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


t = int(input())
for i in range(t):
	n = int(input())
	cnt = 0
	i = 1
	tmp = 1
	while int(str(tmp) * i) <= n:
		cnt += 1
		if tmp == 9:
			i += 1
			tmp = 1
		else:
			tmp += 1
	print(cnt)


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


t = int(input())
for i in range(t):
	n = input()
	pos = (len(n) - 1) * 9
	x = 1
	while int(str(x) * len(n)) <= int(n):
		pos += 1
		x += 1
	print(pos)


n = int(input())
for i in range(n):
	a = int(input())
	(k, amount) = (1, 0)
	while k * 10 + 1 <= a:
		k = k * 10 + 1
	for j in range(len(str(k))):
		for h in [1, 2, 3, 4, 5, 6, 7, 8, 9]:
			if h * int(str(k)[:j + 1]) <= a:
				amount += 1
	print(amount)


t = int(input())
for r in range(t):
	q = input()
	a = '1' * len(q)
	s = int(a)
	while s <= int(q):
		s += int(a)
	e = s // int(a) - 1
	e += -9 + 9 * len(q)
	print(e)