#include <bits/stdc++.h>

using namespace std;

int main()
{
   long long t,n,x=0,a,b,y;
   cin>>t;

   for (int i=0;i<t;i++){
    x=0;
    cin>>n;
    y=n;
    while (y>9){

        y/=10;
        x++;

    }
    x++;
   // cout<<x<<endl;
    y=n;

    if (x!=1){
            for (int i=0;i<x-2;i++){
        y/=10;

    }
    a=y/10;
    b=y%10;
    //cout<<a<<' '<<b<<endl;
            if (b>=a){cout<<(x-1)*9+a<<endl;;}
            else {cout<<(x-1)*9+a-1<<endl;}
    }

    else {cout<<n;}

   }

}


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <vector>
#include <cstdio>
#include <string>
#include <algorithm>
#include <set>

using namespace std;

long long n, m, k, t, sum = 0, sum_t, a, b, c, ans, q, tmp;
int dp[1001][1001];
int num[1001];
char str[500001];
char lett[30];
void show();
void read(char *ptr, int len);
bool check(char ch);
int main() {
	cin.tie(0);
#ifdef _DEBUG
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	cin >> q;
	while (q--) {
		cin >> n;
		if (n < 10)
			cout << n << '\n';
		else {
			tmp = 1;
			t = log10(n);
			sum = 9*t;
			for (int i = 0; i < t; ++i) {
				tmp *= 10;
				++tmp;
			}
//			cout << tmp<<' ';
//			if(n < tmp)
				cout << sum + n/tmp<< '\n';
		}

	}

	return 0;
}
bool check(char ch) {
	for (int j = 0; j < k; ++j)
		if (lett[j] == ch)
			return true;
	return false;
}
void show() {
	for (int i = 0; i < n; ++i)
		cout << str[i] << ' ';
	cout << '\n';
}
void read(char *ptr, int len) {
	for (int i = 0; i < len; ++i) {
		cin >> *(ptr + i);
	}
}


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <vector>
#include <cstdio>
#include <string>
#include <algorithm>
#include <cmath>
#include <set>

using namespace std;

long long n, m, k, t, sum = 0, sum_t, a, b, c, ans, q, tmp;
int dp[1001][1001];
int num[1001];
char str[500001];
char lett[30];
void show();
void read(char *ptr, int len);
bool check(char ch);
int main() {
	cin.tie(0);
#ifdef _DEBUG
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	cin >> q;
	while (q--) {
		cin >> n;
		if (n < 10)
			cout << n << '\n';
		else {
			tmp = 1;
			t = log10(n);
			sum = 9*t;
			for (int i = 0; i < t; ++i) {
				tmp *= 10;
				++tmp;
			}
//			cout << tmp<<' ';
//			if(n < tmp)
				cout << sum + n/tmp<< '\n';
		}

	}

	return 0;
}
bool check(char ch) {
	for (int j = 0; j < k; ++j)
		if (lett[j] == ch)
			return true;
	return false;
}
void show() {
	for (int i = 0; i < n; ++i)
		cout << str[i] << ' ';
	cout << '\n';
}
void read(char *ptr, int len) {
	for (int i = 0; i < len; ++i) {
		cin >> *(ptr + i);
	}
}


        #include "bits/stdc++.h"

        using namespace std;

        int main() {
        	int t;
        	cin >> t;
        	for (int j = 0; j < t; j++) {
        		int n;
        		cin >> n;
        		int m = n;
        		vector<int> d;
        		while (n > 0) {
        			d.push_back(n % 10);
        			n /= 10;
        		}
        		int ans = (d.size() - 1) * 9;
        		ans += d[d.size() - 1] - 1;
        		int x = 0;
        		for (int i = 0; i < d.size(); ++i) {
        			x *= 10;
        			x += d[d.size() - 1];
        		}
        		if (x <= m) {
        			ans++;
        		}
        		cout << ans << endl;
        	}
            return 0;
        }


#include "bits/stdc++.h"

using namespace std;

int main() {
	int t;
	cin >> t;
	for (int i = 0; i < t; i++) {
		int n;
		cin >> n;
		vector<int> d;
		while (n > 0) {
			d.push_back(n % 10);
			n /= 10;
		}
		int ans = (d.size() - 1) * 9;
		int mn = 10;
		for (int i = 0; i < d.size(); i++) {
			mn = min(mn, d[i]);
		}
		ans += d[d.size() - 1] - 1;
		if (d[d.size() - 1] <= mn) {
			ans++;
		}
		cout << ans << endl;
	}
    return 0;
}


#include "bits/stdc++.h"

using namespace std;

int main() {
	int t;
	cin >> t;
	for (int j = 0; j < t; j++) {
		int n;
		cin >> n;
		vector<int> d;
		while (n > 0) {
			d.push_back(n % 10);
			n /= 10;
		}
		int ans = (d.size() - 1) * 9;
		int mn = 10;
		for (int i = 0; i < d.size(); i++) {
			mn = min(mn, d[i]);
		}
		ans += d[d.size() - 1] - 1;
		if (d[d.size() - 1] <= mn) {
			ans++;
		}
		cout << ans << endl;
	}
    return 0;
}


    #include "bits/stdc++.h"

    using namespace std;

    int main() {
    	int t;
    	cin >> t;
    	for (int j = 0; j < t; j++) {
    		int n;
    		cin >> n;
    		vector<int> d;
    		while (n > 0) {
    			d.push_back(n % 10);
    			n /= 10;
    		}
    		int ans = (d.size() - 1) * 9;
    		ans += d[d.size() - 1] - 1;
    		int x = 0;
    		for (int i = 0; i < d.size(); ++i) {
    			x *= 10;
    			x += d[d.size() - 1];
    		}
    		if (x <= n) {
    			ans++;
    		}
    		cout << ans << endl;
    	}
        return 0;
    }


#include<iostream>
#include<vector>
#include<map>
#include<set>
#include<algorithm>
#include<queue>
#include<deque>
#include <string>
#include <sstream>
#include <iomanip>
#include <list>
#include <cmath>
using namespace std;
#define ll long long
#define pii pair<int, int>
#define pll pair<long long, long long>
#define ld long double
int main() {
	ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	ll q;
	cin >> q;
	while (q--) {
		ll n, m = 0;
		string a;
		cin >> a;
		if (a.size() > 1)m = (a.size() - 1) * 9;
		m += (a[0] - '0') - 1;
		int i = 1;
		while (a.size() > i && a[1] == a[0])i++;
		if (i == a.size() || a[i - 1] > a[i])m++;
		cout << m << endl;
	}
	return 0;
}


#include<iostream>
#include<vector>
#include<map>
#include<set>
#include<algorithm>
#include<queue>
#include<deque>
#include <string>
#include <sstream>
#include <iomanip>
#include <list>
#include <cmath>
using namespace std;
#define ll long long
#define pii pair<int, int>
#define pll pair<long long, long long>
#define ld long double
int main() {
	ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	ll q;
	cin >> q;
	while (q--) {
		ll n, m = 0;
		string a;
		cin >> a;
		if (a.size() > 1)m = (a.size() - 1) * 9;
		m += (a[0] - '0') - 1;
		int i = 1;
		while (a.size() > i && a[i] == a[i-1])i++;
		if (i == a.size() || a[i - 1] < a[i])m++;
		cout << m << endl;
	}
	return 0;
}


#include<iostream>
#include<vector>
#include<map>
#include<set>
#include<algorithm>
#include<queue>
#include<deque>
#include <string>
#include <sstream>
#include <iomanip>
#include <list>
#include <cmath>
using namespace std;
#define ll long long
#define pii pair<int, int>
#define pll pair<long long, long long>
#define ld long double
int main() {
	ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	ll q;
	cin >> q;
	while (q--) {
		ll n, m = 0;
		string a;
		cin >> a;
		if (a.size() > 1)m = (a.size() - 1) * 9;
		m += (a[0] - '0') -1;
		int i = 0;
		while (i < a.size() && a[i] >= a[i - 1])i++;
		if (i == a.size())m++;
		cout << m << endl;
	}
	return 0;
}


#include <iostream>
#include <iomanip>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <cassert>
#include <string>
#include <set>
#include <map>
#include <random>
#include <bitset>
#include <string>
#include <unordered_set>
#include <unordered_map>
#include <deque>
#include <queue>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sp system("pause")

using namespace std;
using ll = long long;
using ul = unsigned long long;
using ld = long double;

ll t, n, r;
vector<ll> ans(1e5);

int main() {
	cin.sync_with_stdio(0); cin.tie(0);
	cin >> t;
	rep(i, t) {
		cin >> n;
		rep(j, 11) {
			if (n < pow(10, j)) {
				r = j;
				break;
			}
		}
		ans[i] = 9 * (r - 1) + n / ((pow(10, r) - 1) / 9);
	}
	rep(i, t) {
		cout << ans[i] << "\n";
	}
	return 0;
}


#include <iostream>
#include <iomanip>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <cassert>
#include <string>
#include <set>
#include <map>
#include <random>
#include <bitset>
#include <string>
#include <unordered_set>
#include <unordered_map>
#include <deque>
#include <queue>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sp system("pause")

using namespace std;
using ll = long long;
using ul = unsigned long long;
using ld = long double;

int t;
vector<ll> age(1e5);
vector<ll> ans(1e5);

int main() {
	cin.sync_with_stdio(0); cin.tie(0);
	cin >> t;
	ll r = 0;
	rep(i, t) {
		cin >> r;
		age[i] = r;
		int deg = 0;
		rep(d, 10) {
			if (r < pow(10, d)) {
				deg = d;
				break;
			}
		}
		if (deg == 1) {
			ans[i] = r;
		}
		else {
			ans[i] = (9 * (deg - 1)) + (r / pow(10, deg - 1)) / ((pow(10, deg - 1) - 1) / 9);
			if (deg == 0) {
				ans[i] = 81;
			}
		}
	}
	rep(i, t) {
		cout << ans[i] << "\n";
	}
	return 0;
}


#include <iostream>
#include <iomanip>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <cassert>
#include <string>
#include <set>
#include <map>
#include <random>
#include <bitset>
#include <string>
#include <unordered_set>
#include <unordered_map>
#include <deque>
#include <queue>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sp system("pause")

using namespace std;
using ll = long long;
using ul = unsigned long long;
using ld = long double;

int t;
vector<ll> age(1e5);
vector<ll> ans(1e5);

int main() {
	cin.sync_with_stdio(0); cin.tie(0);
	cin >> t;
	ll r = 0;
	rep(i, t) {
		cin >> r;
		age[i] = r;
		int deg = 0;
		rep(d, 10) {
			if (r < pow(10, d)) {
				deg = d;
				break;
			}
		}
		if (deg == 1) {
			ans[i] = r;
		}
		else {
			ans[i] = (9 * (deg - 1)) + (r / pow(10, deg - 1)) / ((pow(10, deg - 1) - 1) / 9);
			if (deg == 0) {
				ans[i] = 81;
			}
		}
	}
	rep(i, t) {
		cout << ans[i];
	}
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main() {
    int t;
    cin >> t;
    vector<long long > a;
    for (int len = 1; len <= 10; len++) {
    for (int digit = 1; digit <= 9; digit++) {
    long long z = 0;
    for (int t = 0; t < len; t++) {
    z = 1ll*z*10 + digit;
    }
    a.push_back(z);
    }
    }
    for (int i = 0; i < t; i++){
        int n;
        cin >> n;
        int k = 0;
        while (n >= a[k]) {
            k++;
        }
        cout << k << "\n";
    }
}


#include <bits/stdc++.h>

using namespace std;

int main(){
  long long n, i, t, k, c = 8;
   cin >> t;
   for(i = 0; i < t; i++){
    cin >> n;
    k = 1111111111;
    c = 9;
    while(k > n){
        k = k / 10;
        c--;
    }
    //cout << c << ' ' << k;
    cout << c * 9 + (n / k) << endl;
       }
  }


#include <bits/stdc++.h>

using namespace std;

int main(){
  long long n, i, t, k, c = 8;
   cin >> t;
   for(i = 0; i < t; i++){
    cin >> n;
    if(n == 10){
        cout << 9 << endl;
        continue;
    }
    k = 111111111;
    c = 8;
    while(k > n){
        k = k / 10;
        c--;
    }
    //cout << c << ' ' << k;
    cout << 9 * c + (n / k) << endl;
    }
  }


#include <bits/stdc++.h>

using namespace std;
long long pow(long long x,long long y){
long long    z = x;
  for(int i = 0;  i < y; i++){
    x = x * z;
  }
}

int main(){
  long long n, i, t, k, c = 8;
   cin >> t;
   for(i = 0; i < t; i++){
    cin >> n;
    if(n == 10){
        cout << 9 << endl;
        continue;
    }
    k = 111111111;
    c = 8;
    while(pow(10, c) > n){
        k = k / 10;
        c--;
    }
    //cout << c << ' ' << k;
    cout << 9 * c + (n / k) << endl;
    }
  }


#include <bits/stdc++.h>

using namespace std;

int main(){
  long long n, i, t, k, c = 8;
   cin >> t;
   for(i = 0; i < t; i++){
    cin >> n;
    k = 111111111;
    c =  8;
    while(k > n){
        k = k / 10;
        c--;
    }
    //cout << c << ' ' << k;
    cout << c * 9 + (n / k) << endl;
       }
  }


#include <bits/stdc++.h>
#define ll long long int
#define MAX 1000000
using namespace std;

void slove(){
    int n;
    cin >> n;
    string s = to_string(n);
    int k = s.size();
    ll ans = 1LL * (s.size() - 1) * 9;
    int t = 10;
    for (auto i : s)
        t = min(t, (int)i - 48);
    ans += t;
    cout << ans << endl;
}
int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    //ifstream cin ("input.txt");
    //ofstream cout ("output.txt");
    int t;
    cin >> t;
    while(t -->0)
        slove();

    return 0;
}


#include <bits/stdc++.h>
#define ll long long int
#define MAX 1000000
using namespace std;
ll gen(ll n, ll pow){
    ll r = 0;
    while (pow -->0){
        r = r * 10 + n;
    }
    return r;
}
void slove(){
    ll n;
    cin >> n;
    string s = to_string(n);
    ll k = s.size();
    ll ans = 1LL * (s.size() - 1) * 9;
    for (ll i = 1; i < 10; i++){
        if (gen(i, s.size()) <= n)
            ans++;
    }
    cout << ans << endl;
}
int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    //ifstream cin ("input.txt");
    //ofstream cout ("output.txt");
    ll t;
    cin >> t;
    while(t -->0)
        slove();

    return 0;
}


#ifndef ONLINE_JUDGE
#define _GLIBCXX_DEBUG 1
#endif

#include <bits/stdc++.h>

using namespace std;

using ll = long long;

int main() {
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    srand(time(NULL));
    int t;
    cin >> t;
    while (t-- > 0) {
        int a;
        cin >> a;
        string n = to_string(a);
        int ans = n.size();
        int mn = 10;
        for (auto c : n){
            mn = min(mn, c - '0');
        }
        cout << (ans - 1) * 9 + mn << endl;
    }
    return 0;
}


#ifndef ONLINE_JUDGE
#define _GLIBCXX_DEBUG 1
#endif

#include <bits/stdc++.h>

using namespace std;

using ll = long long;

int main() {
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    srand(time(NULL));
    int t;
    cin >> t;
    while (t-- > 0) {
        int n;
        cin >> n;
        int ans = 0;
        int mn = 10;
        while (n > 0) {
            ans++;
            mn = min(mn, n % 10);
            n /= 10;
        }
        cout << (ans - 1) * 9 + mn << endl;
    }
    return 0;
}


#ifndef ONLINE_JUDGE
#define _GLIBCXX_DEBUG 1
#endif

#include <bits/stdc++.h>

using namespace std;

using ll = long long;

int main() {
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    srand(time(NULL));
    int t;
    cin >> t;
    while (t-- > 0) {
        string n;
        cin >> n;
        int ans = n.size();
        int mn = 10;
        for (auto c : n){
            mn = min(mn, c - '0');
        }
        cout << (ans - 1) * 9 + mn << endl;
    }
    return 0;
}


#ifndef ONLINE_JUDGE
#define _GLIBCXX_DEBUG 1
#endif

#include <bits/stdc++.h>

using namespace std;

using ll = long long;

int main() {
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    srand(time(NULL));
    int t;
    cin >> t;
    while (t-- > 0) {
        int a;
        cin >> a;
        string n = to_string(a);
        int ans = n.size();
        int mn = n[0] - '0';
        for (auto c : n){
            if (c > n[0]) break;
            if (c < n[0]){
                mn--;
                break;
            }
        }
        cout << (ans - 1) * 9 + mn << endl;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
    long long f(long long c) {
    bool u = (c % 10 == 9);
    long long ans = 0, d =1 + c % 10;
    if (d == 10) d = 1;
    while (c)
        {
        ans *= 10 + d;
        c /= 10;}
    if (u) ans = ans * 10 + d;
    return ans;
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    int t, n;
    cin >> t;
    while (t--) {
        cin >> n;
        long long otv = 1, x = 1;
        while (f(x) <= n){
                otv ++;
                x = f(x);}
        cout << otv << endl;}
    return 0;
}


#include <bits/stdc++.h>

#pragma GCC optimize("Ofast")
#define ll long long
using namespace std;

ll f(ll c) {
    bool u=(c%10==9);
    ll ans = 0;
    int d = c % 10 + 1;
    if (d == 10) d = 1;
    while (c) {
        ans = ans * 10 + d;
        c /= 10;
    }
    if (u) ans=ans * 10 + d;
    return ans;
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    int t, n;
    cin >> t;
    while (t--) {
        cin >> n;
        ll ans = 1;
        ll x = 1;
        while (f(x) <= n){
                ans++;
                x = f(x);
        }
        cout << ans << endl;
    }
    return 0;
}


#include<bits/stdc++.h>

#pragma GCC optimize("Ofast")

using namespace std;
    long long f(long long c) {
    bool u = (c % 10 == 9);
    long long ans = 0;
    int d =1 + c % 10;
    if (d == 10) d = 1;
    while (c)
        {
        ans *= 10 + d;
        c /= 10;}
    if (u) ans = ans * 10 + d;
    return ans;
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    int t, n;
    cin >> t;
    while (t--) {
        cin >> n;
        long long otv = 1, x = 1;
        while (f(x) <= n){
                otv ++;
                x = f(x);}
        cout << otv << endl;}
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int f(int a){
    int i = 0;
    while(a){
        i ++;
        a /= 10;
    }
    return i;
}
int main(){
    int t;
    cin >> t;
    int m[10][10];
    for (int i = 0; i <= 9; i ++){
        for (int j=0; j <= 9; j++){
            m[i][j] = j;
            for (int k = 1; k < i; k++){
                m[i][j] = m[i][j] * 10 + j;
            }
        }
    }
    for (int i=0;i<t;i++){
        int n;
        cin >> n;
        if (n == 1000000000){
            cout << 81 << endl;
        }
        else{
        int ans = 0;
        int k = f(n) - 1;
        int b = n;
        for (int j = 0; j < k; j ++)
            n /= 10;
        ans = k * 9 + n;
        if (b < m[k + 1][n])
            ans--;
        cout << ans << endl;
        }
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int f(int a){
    int i = 0;
    while(a){
        i ++;
        a /= 10;
    }
    return i;
}
int main(){
    int t;
    cin >> t;
    int m[10][10];
    for (int i = 0; i <= 9; i ++){
        for (int j=0; j <= 9; j++){
            m[i][j] = j;
            for (int k = 1; k < i; k++){
                m[i][j] = m[i][j] * 10 + j;
            }
        }
    }
    for (int i=0;i<t;i++){
        int n;
        cin >> n;
        int ans = 0;
        int k = f(n) - 1;
        int b = n;
        for (int j = 0; j < k; j ++)
            n /= 10;
        ans = k * 9 + n;
        if (b < m[k + 1][n])
            ans--;
        cout << ans << endl;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main(){
    int t;
    cin >> t;
    for (int i=0;i<t;i++){
        string n;
        cin >> n;
        int ans = 0;
        ans += 9 * (n.size() - 1) + n[0] - '0';
        for (int i=0;i<n.size();i++){
            if (n[i] < n[0]){
                ans --;
                break;
            }
        }
        cout << ans << endl;
    }
    return 0;
}


// Blueberry13
#include <bits/stdc++.h>

using namespace std;

int main(){
	ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
	int q;
	cin >> q;
	for (int i = 0; i < q; i++){
		int n;
		cin >> n;
		stringstream ss;
        ss << n;
        int answ = 0;
		string s = ss.str();
		int h = s.size();
		/*int f = 1;
		for (int j = 1; j < s.size(); j++){
			if (s[j] == s[j-1]){
				f++;
			}
		}

		if (h == f){
			answ++;
		}*/
		if (n < 10){
			answ = n;
		}
		else{
			for (int j = 0; j < h - 1; j++){
				answ += 9;
			}
			int g = n / pow(10, h-1);
			answ += g;
			for (int j = 0; j < h - 1; j++){
				if (s[j] > s[j + 1]){
					answ--;
					break;
				}
			}
		}
		cout << answ << '\n';
	}
}


#include <bits/stdc++.h>
using namespace std;

int main(){
    int t;
    cin >> t;
    while(t--){
        long long n;
        cin >> n;
        if(n == 10){
            cout << 9 << "\n";
        }
        else{
            if(n < 10){
                cout << n << "\n";
            }
            else{
                int x = 11;
                int ans = 0;
                int cnt = 0;
                int sum = 0;
                while(1){
                    sum += x;
                    ans++;
                    cnt++;
                    if(cnt == 9){
                        cnt = 1;
                        x = x * 10 + 1;
                        ans ++;
                        sum = x;
                    }
                    if(sum == n){
                        break;
                    }
                    if(sum > n){
                        --ans;
                        break;
                    }
                }
                cout << ans + 9 << "\n";
            }
        }
    }
}


#include <bits/stdc++.h>
using namespace std;

int main(){
    int t;
    cin >> t;
    while(t--){
        long long n;
        cin >> n;
        if(n == 10){
            cout << 9 << "\n";
        }
        else{
            if(n < 10){
                cout << n << "\n";
            }
            else{
                int x = 11;
                int ans = 0;
                int cnt = 0;
                int sum = 0;
                while(1){
                    sum += x;
                    ans++;
                    if(cnt == 9){
                        cnt = 0;
                        x = x * 10 + 1;
                        sum = x;
                    }
                    cnt++;
                    if(sum == n){
                        break;
                    }
                    if(sum > n){
                        ans--;
                        break;
                    }
                }
                cout << ans + 9 << "\n";
            }
        }
    }
}


#include <bits/stdc++.h>
using namespace std;

int main(){
    int t;
    cin >> t;
    while(t--){
        long long n;
        cin >> n;
        if(n == 10){
            cout << 9 << "\n";
        }
        else{
            if(n < 10){
                cout << n << "\n";
            }
            else{
                int x = 11;
                int ans = 0;
                int cnt = 1;
                int sum = 0;
                while(1){
                    if(cnt == 9){
                        x = x * 10 + 1;
                        sum = x;
                        cnt = 0;
                    }
                    sum += x;
                    ++cnt;
                    ans++;
                    if(sum == n){
                        break;
                    }
                    if(sum + x > n){
                        break;
                    }
                }
                if(n >= 222) cout << ans + 9 + 2 << "\n";
                else{
                    if(n >= 111)cout << ans + 10 << "\n";
                    else cout << ans + 9 << "\n";
                }
            }
        }
    }
}


#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace	std;

typedef unsigned long long		ull;
typedef long long				ll;
typedef long double				ld;

template<typename T> using vv	= vector<vector<T>>;
template<typename T> using pp   = pair<T, T>;
template<typename T> inline istream& operator >>(istream& in, vector<T>& v) { for (auto& u : v) in >> u; return in; }
template<typename T> inline ostream& operator <<(ostream& os, const vector<T>& v) { for (auto& u : v) os << u << ' '; return os; }

#define fr(i, n)                for(int i = 0; i < n; ++i)
#define rep(i, a, b)            for(int i = a; i <= b; ++i)
#define all(a)                  a.begin(), a.end()
#define rall(a)                 a.rbegin(), a.rend()
#define nl                      '\n'

vector<char> v{ '1', '2','3', '4', '5', '6', '7', '8', '9' };

void obr()
{
	int n;
	cin >> n;

	int ans = 0;
	if (n <= 9)
	{
		cout << n << nl;
		return;
	}

	string s = to_string(n);
	int sz = s.size();

	int idx = 0;
	while (idx < v.size() && n >= stoi(string(sz, v[idx])))
	{
		++idx;
	}

	while (n > 9)
	{
		ans += 9;
		n /= 10;
	}

//	cout << "!!";
	cout << ans + idx << nl;
}

int main()
{
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);

	int t;
	cin >> t;

	fr(i, t)
		obr();

	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace	std;

typedef unsigned long long		ull;
typedef long long				ll;
typedef long double				ld;

template<typename T> using vv	= vector<vector<T>>;
template<typename T> using pp   = pair<T, T>;
template<typename T> inline istream& operator >>(istream& in, vector<T>& v) { for (auto& u : v) in >> u; return in; }
template<typename T> inline ostream& operator <<(ostream& os, const vector<T>& v) { for (auto& u : v) os << u << ' '; return os; }

#define fr(i, n)                for(int i = 0; i < n; ++i)
#define rep(i, a, b)            for(int i = a; i <= b; ++i)
#define all(a)                  a.begin(), a.end()
#define rall(a)                 a.rbegin(), a.rend()
#define nl                      '\n'

vector<char> v{ '1', '2','3', '4', '5', '6', '7', '8', '9' };

void obr()
{
	int n;
	cin >> n;

	int ans = 0;
	if (n <= 9)
	{
		cout << n << nl;
		return;
	}
	string s = to_string(n);
	int sz = s.size();

	int idx = 0;
	while (n >= stoi(string(sz, v[idx])))
	{
		++idx;
	}

	while (n > 9)
	{
		ans += 9;
		n /= 10;
	}

//	cout << "!!";
	cout << ans + idx << nl;
}

int main()
{
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);

	int t;
	cin >> t;

	fr(i, t)
		obr();

	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace	std;

typedef unsigned long long		ull;
typedef long long				ll;
typedef long double				ld;

template<typename T> using vv	= vector<vector<T>>;
template<typename T> using pp   = pair<T, T>;
template<typename T> inline istream& operator >>(istream& in, vector<T>& v) { for (auto& u : v) in >> u; return in; }
template<typename T> inline ostream& operator <<(ostream& os, const vector<T>& v) { for (auto& u : v) os << u << ' '; return os; }

#define fr(i, n)                for(int i = 0; i < n; ++i)
#define rep(i, a, b)            for(int i = a; i <= b; ++i)
#define all(a)                  a.begin(), a.end()
#define rall(a)                 a.rbegin(), a.rend()
#define nl                      '\n'

vector<char> v{ '1', '2','3', '4', '5', '6', '7', '8', '9' };

void obr()
{
	int n;
	cin >> n;

	int ans = 0;
	if (n <= 9)
	{
		cout << n;
		return;
	}
	string s = to_string(n);
	int sz = s.size();

	int idx = 0;
	while (n >= stoi(string(sz, v[idx])))
	{
		++idx;
	}

	while (n > 9)
	{
		ans += 9;
		n /= 10;
	}

//	cout << "!!";
	cout << ans + idx << nl;
}

int main()
{
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);

	int t;
	cin >> t;

	fr(i, t)
		obr();

	return 0;
}


#include <bits/stdc++.h>
#define ll long long
using namespace std;
ll rec(ll n){
ll ans = 0;
	while(n > 0){
		ans++;
		n /= 10;
	}
	return ans;
}

ll a , n , ans = 1;

int main (){
cin >> n;
for(int i = 1; i <= n; ++i){
	cin >> a;
	for(int j = 1; j <= rec(a) - 1; ++j){
		ans *= 10;
		ans ++;
	}
	if(a < 10){
		cout << a << "\n";
		ans = 1;
	}
	else{
		cout << (rec(a) - 1) * 9 + (a / ans) << "\n";
	}
	ans = 1;
}
return 0;
}


#include <bits/stdc++.h>
#define ll long long

using namespace std;

set < char > st;
ll n , l , d , f , ans;

int main(){
	cin >> n;
	while(n--){
		string s;
		ans = 0;
		cin >> s;
		for(int i = 0; i < s.size(); i++){
			if(i == s.size() - 1){
				bool ok = 1;
				for(int j = 0; j < s.size(); j++){
					if(int(s[j] - '0') < int(s[0] - '0')){
						ok = 0;
						break;
					}
				}
				if(ok) ans += int(s[0] - '0');
				else ans += (int(s[0] - '0') - 1);
			}else{
				ans += 9;
			}
		}
		cout << ans << "\n";
	}
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <set>
#include <iomanip>
#include <vector>
#include <cmath>
#include <queue>
#include <map>
#include <algorithm>
#include <stack>
#include <string>

using namespace std;

typedef long long ll;
typedef long double dl;

const int INF = (int)1e9 + 7;
const dl EPS = 1e-9;
const ll MOD = (ll)pow(2, 32);

void input() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
#ifdef _DEBUG
    freopen("C:\\Users\\kiril\\source\\repos\\Generator\\Generator\\input.txt", "r", stdin);
    freopen("C:\\Users\\kiril\\source\\repos\\Generator\\Generator\\your.txt", "w", stdout);
#endif
}

void end() {
#ifdef _DEBUG
    fclose(stdin);
    fclose(stdout);
#endif
}

int main() {
    input();
	int t;
	cin >> t;
	for (int h = 0; h < t; h++) {
		int n, k = 0, c;
		cin >> n;
		vector<int> num;
		int u = n;
		while (u) {
			num.push_back(u % 10);
			u /= 10;
			k++;
		}
		reverse(num.begin(), num.end());
		c = n / pow(10, k - 1);
		bool flag = true;
		for (int i = 0; i < k; i++) {
			if (num[i] > c) {
				break;
			}
			else if (num[i] < c) {
				flag = false;
			}
		}
		if (flag) {
			cout << 9 * (k - 1) + c << endl;
		}
		else {
			cout << 9 * (k - 1) + c - 1 << endl;
		}
	}
    end();
    return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <set>
#include <iomanip>
#include <vector>
#include <cmath>
#include <queue>
#include <map>
#include <algorithm>
#include <stack>
#include <string>

using namespace std;

typedef long long ll;
typedef long double dl;

const int INF = (int)1e9 + 7;
const dl EPS = 1e-9;
const ll MOD = (ll)pow(2, 32);

void input() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
#ifdef _DEBUG
    freopen("C:\\Users\\kiril\\source\\repos\\Generator\\Generator\\input.txt", "r", stdin);
    freopen("C:\\Users\\kiril\\source\\repos\\Generator\\Generator\\your.txt", "w", stdout);
#endif
}

void end() {
#ifdef _DEBUG
    fclose(stdin);
    fclose(stdout);
#endif
}

int main() {
    input();
	int t;
	cin >> t;
	for (int h = 0; h < t; h++) {
		int n, k = 0, c;
		cin >> n;
		vector<int> num;
		int u = n;
		while (u) {
			num.push_back(u % 10);
			u /= 10;
			k++;
		}
		c = n / pow(10, k - 1);
		bool flag = true;
		for (int i = 0; i < k; i++) {
			if (num[i] < c) {
				flag = false;
			}
		}
		if (flag) {
			cout << 9 * (k - 1) + c << endl;
		}
		else {
			cout << 9 * (k - 1) + c - 1 << endl;
		}
	}
    end();
    return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <set>
#include <iomanip>
#include <vector>
#include <cmath>
#include <queue>
#include <map>
#include <algorithm>
#include <stack>
#include <string>

using namespace std;

typedef long long ll;
typedef long double dl;

const int INF = (int)1e9 + 7;
const dl EPS = 1e-9;
const ll MOD = (ll)pow(2, 32);

void input() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
#ifdef _DEBUG
    freopen("C:\\Users\\kiril\\source\\repos\\Generator\\Generator\\input.txt", "r", stdin);
    freopen("C:\\Users\\kiril\\source\\repos\\Generator\\Generator\\your.txt", "w", stdout);
#endif
}

void end() {
#ifdef _DEBUG
    fclose(stdin);
    fclose(stdout);
#endif
}

int main() {
    input();
	int t;
	cin >> t;
	for (int h = 0; h < t; h++) {
		int n, k = 0, c;
		cin >> n;
		vector<int> num;
		int u = n;
		while (u) {
			num.push_back(u % 10);
			u /= 10;
			k++;
		}
		reverse(num.begin(), num.end());
		c = n / pow(10, k - 1);
		bool flag = true;
		for (int i = 0; i < k; i++) {
			if (num[i] < c) {
				flag = false;
			}
		}
		if (flag) {
			cout << 9 * (k - 1) + c << endl;
		}
		else {
			cout << 9 * (k - 1) + c - 1 << endl;
		}
	}
    end();
    return 0;
}


#include <iostream>
#include <vector>
#include <list>
#include <algorithm>
#include <string>
#include <math.h>
#include <unordered_set>
#include <climits>

//#include <chrono>
//#include <fstream>
//#include <bits/stdc++.h>

#define mp make_pair
#define pb push_back
#define eb emplace_back
#define x first
#define y second
#define sz(x) (int)x.size()
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)
#define FOR(i,a,b) for (int i = (a); i < (b); i++)
#define RFOR(i,b,a) for (int i = (b) - 1; i >= (a); i--)

using namespace std;

typedef unsigned long long ull;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef vector<int> veci;
typedef vector<bool> vecb;
typedef vector<vector<int>> vvi;
typedef vector<vector<bool>> vvb;

const int INF_I = 1e9;
const ll INF_LL = 1e18;

int main() {
	/*ifstream cin("in.txt");
	ofstream cout("out.txt");
	auto start_time = chrono::high_resolution_clock::now();*/

	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);

	int t;
	cin >> t;
	unordered_set<ll> s;
	for (int i = 1; i < 10; ++i) {
		for (int j = 1; j < 10; j++) {
			string str = string(i, char('0' + j));
			ll a = atoll(str.c_str());
			s.emplace(a);
		}
	}
	while (t--) {
		ll str;
		cin >> str;
		int res = 0;
		for (ll i : s)
			if (i <= str)
				res++;
		cout << res << "\n";

	}

	/*auto end_time = chrono::high_resolution_clock::now();
	chrono::duration<double> duration = end_time - start_time;
	cout << duration.count() << "\n";
	cin.close();
	cout.close();*/
	return 0;
}


#include <iostream>
#include <vector>
#include <list>
#include <algorithm>
#include <string>
#include <math.h>
#include <climits>

//#include <chrono>
//#include <fstream>
//#include <bits/stdc++.h>

#define mp make_pair
#define pb push_back
#define eb emplace_back
#define x first
#define y second
#define sz(x) (int)x.size()
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)
#define FOR(i,a,b) for (int i = (a); i < (b); i++)
#define RFOR(i,b,a) for (int i = (b) - 1; i >= (a); i--)

using namespace std;

typedef unsigned long long ull;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef vector<int> veci;
typedef vector<bool> vecb;
typedef vector<vector<int>> vvi;
typedef vector<vector<bool>> vvb;

const int INF_I = 1e9;
const ll INF_LL = 1e18;

int minn(string s) {
	int res = INT_MAX;
	for (char c : s)
		if (c - '0' < res)
			res = c - '0';
	return res;
}

int main() {
	/*ifstream cin("in.txt");
	ofstream cout("out.txt");
	auto start_time = chrono::high_resolution_clock::now();*/

	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);

	int t;
	cin >> t;
	while (t--) {
		string str;
		cin >> str;
		cout << (str.size() - 1) * 9 + minn(str) << "\n";

	}

	/*auto end_time = chrono::high_resolution_clock::now();
	chrono::duration<double> duration = end_time - start_time;
	cout << duration.count() << "\n";
	cin.close();
	cout.close();*/
	return 0;
}


#include <iostream>
#include <vector>
#include <list>
#include <algorithm>
#include <string>
#include <math.h>
#include <climits>

//#include <chrono>
//#include <fstream>
//#include <bits/stdc++.h>

#define mp make_pair
#define pb push_back
#define eb emplace_back
#define x first
#define y second
#define sz(x) (int)x.size()
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)
#define FOR(i,a,b) for (int i = (a); i < (b); i++)
#define RFOR(i,b,a) for (int i = (b) - 1; i >= (a); i--)

using namespace std;

typedef unsigned long long ull;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef vector<int> veci;
typedef vector<bool> vecb;
typedef vector<vector<int>> vvi;
typedef vector<vector<bool>> vvb;

const int INF_I = 1e9;
const ll INF_LL = 1e18;

int minn(string s) {
	int res = INT_MAX;
	for (int i = 1; i < s.size(); i++)
		if (s[i] - '0' < res) {
			res = s[i] - '0';
		}

	return res;
}

int main() {
	/*ifstream cin("in.txt");
	ofstream cout("out.txt");
	auto start_time = chrono::high_resolution_clock::now();*/

	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);

	int t;
	cin >> t;
	while (t--) {
		string str;
		cin >> str;
		int a = max(0, str[0] - '0' - 1 + (str[0] - '0' <= minn(str) ? 1 : 0));
		cout << (str.size() - 1) * 9 + a << "\n";

	}

	/*auto end_time = chrono::high_resolution_clock::now();
	chrono::duration<double> duration = end_time - start_time;
	cout << duration.count() << "\n";
	cin.close();
	cout.close();*/
	return 0;
}


#include <iostream>
#include <vector>
#include <list>
#include <algorithm>
#include <string>
#include <math.h>
#include <climits>

//#include <chrono>
//#include <fstream>
//#include <bits/stdc++.h>

#define mp make_pair
#define pb push_back
#define eb emplace_back
#define x first
#define y second
#define sz(x) (int)x.size()
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)
#define FOR(i,a,b) for (int i = (a); i < (b); i++)
#define RFOR(i,b,a) for (int i = (b) - 1; i >= (a); i--)

using namespace std;

typedef unsigned long long ull;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef vector<int> veci;
typedef vector<bool> vecb;
typedef vector<vector<int>> vvi;
typedef vector<vector<bool>> vvb;

const int INF_I = 1e9;
const ll INF_LL = 1e18;

int minn(string s) {
	int i = 1;
	for (; i < 10; i++) {
		for (char ch : s)
			if (ch - '0' < i)
				return i-1;
	}
	return max(0, i-1);
}

int main() {
	/*ifstream cin("in.txt");
	ofstream cout("out.txt");
	auto start_time = chrono::high_resolution_clock::now();*/

	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);

	int t;
	cin >> t;
	while (t--) {
		string str;
		cin >> str;
		cout << (str.size() - 1) * 9 + minn(str) << "\n";

	}

	/*auto end_time = chrono::high_resolution_clock::now();
	chrono::duration<double> duration = end_time - start_time;
	cout << duration.count() << "\n";
	cin.close();
	cout.close();*/
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
int f(long long o){
	int s=o%10;
	o/=10;
	while(o>0){
		int i=o%10;
		if(i!=s)return 0;
		o/=10;
	}
	return 1;
}
int main(){
	int t;
	cin >> t;
	for(int i=0;i<t;i++){
		long long n,o=1,ans=0;
		cin >> n;
		while(n>o){
			if(f(o)==1) ans++;
			o++;
		}
		cout << ans << endl;
	}
}


#include <bits/stdc++.h>
using namespace std;
int main(){
	int t;
	cin >> t;
	for(int j=0;j<t;j++){
		long long n,o=1,i=1,ans=0;
		cin >> n;
		while(n>=o){
			ans++;
			o+=i;
			if(o%10==0){
				o++;
				i=i*10+1;
			}
		}
		cout << ans << endl;
	}
}


#include <iostream>
#include <string>

#define ll long long

using namespace std;

/*bool checkYear(int a)
{
    int num;
    bool r = true;
    num = a % 10;
    a /= 10;

    while (a > 0 && r) {
        if (a % 10 != num) {
            r = false;
        }
        a /= 10;
    }

    return r;
} */

int main()
{
    ios::sync_with_stdio(false);

    int t;

    cin >> t;

    ll int n, ans;
    ll int startNum, del, curNum, j;

    for (int i = 0; i < t; i++) {
        cin >> n;

        startNum = 1;
        del = 1;
        curNum = 1;
        j = 1;
        ans = 0;

        while (curNum <= n) {
            if (j != 1 && (j - 1) % 9 == 0) {
                startNum = startNum * 10 + 1;
                del = startNum;
                curNum = startNum;
            }

            cout << curNum << endl;
            curNum += del;

            j++;
            ans++;
        }

        cout << ans << ' ';
    }

    return 0;
}


#include <iostream>
#include <string>

#define ll long long

using namespace std;

/*bool checkYear(int a)
{
    int num;
    bool r = true;
    num = a % 10;
    a /= 10;

    while (a > 0 && r) {
        if (a % 10 != num) {
            r = false;
        }
        a /= 10;
    }

    return r;
} */

int main()
{
    ios::sync_with_stdio(false);

    int t;

    cin >> t;

    ll int n, ans;
    int startNum, del, curNum, j;

    for (int i = 0; i < t; i++) {
        cin >> n;

        startNum = 1;
        del = 1;
        curNum = 1;
        j = 1;
        ans = 0;

        while (curNum <= n) {
            if (j != 1 && (j - 1) % 9 == 0) {
                startNum = startNum * 10 + 1;
                del = startNum;
                curNum = startNum;
            }

            //cout << curNum << endl;
            curNum += del;

            j++;
            ans++;
        }

        cout << ans << ' ';
    }

    return 0;
}


#include <iostream>
#include <string>

#define ll long long

using namespace std;

int main()
{
    ios::sync_with_stdio(false);

    int t;

    cin >> t;

    ll int n, ans;
    ll int startNum, del, curNum, j;

    for (int i = 0; i < t; i++) {
        cin >> n;

        startNum = 1;
        del = 1;
        curNum = 1;
        j = 1;
        ans = 0;

        while (curNum <= n) {
            if (j != 1 && (j - 1) % 9 == 0) {
                startNum = startNum * 10 + 1;
                del = startNum;
                curNum = startNum;
            }

            //cout << curNum << endl;
            curNum += del;

            j++;
            ans++;
        }

        cout << ans << ' ';
    }

    return 0;
}


#include <iostream>
#include <string>

#define ll long long

using namespace std;

/*bool checkYear(int a)
{
    int num;
    bool r = true;
    num = a % 10;
    a /= 10;

    while (a > 0 && r) {
        if (a % 10 != num) {
            r = false;
        }
        a /= 10;
    }

    return r;
} */

int main()
{
    ios::sync_with_stdio(false);

    int t;

    cin >> t;

    ll int n, ans;
    int startNum, del, curNum, j;

    for (int i = 0; i < t; i++) {
        cin >> n;

        startNum = 1;
        del = 1;
        curNum = 1;
        j = 1;
        ans = 0;

        while (curNum <= n) {
            if (j != 1 && (j - 1) % 9 == 0) {
                startNum = startNum * 10 + 1;
                del = startNum;
                curNum = startNum;
            }

            //cout << curNum << endl;
            curNum += del;

            j++;
            ans++;
        }

        cout << ans;
    }

    return 0;
}


#include <iostream>
#include <string>

#define ll long long

using namespace std;

int main()
{
    ios::sync_with_stdio(false);

    int t;

    cin >> t;

    ll int n, ans;
    ll int desCount, a, nearNum, j, stNum;

    for (int i = 0; i < t; i++) {
        cin >> n;

        desCount = 0;
        a = n;
        nearNum = 0;

        while (a > 0) {
            a /= 10;
            desCount++;
            nearNum = nearNum * 10 + 1;
        }

        if (nearNum > n) {
            ans = 9 * (desCount - 1);
        } else {
            j = 0;
            stNum = nearNum;

            while (nearNum <= n) {
                j++;

                nearNum = stNum * j;
            }

            ans = 9 * (desCount - 1) + j - 1;
        }

        //cout << desCount << ' ' << nearNum << ' ';

        cout << ans << ' ';
    }

    return 0;
}


#include <iostream>
#include <string>

#define ll long long

using namespace std;

int main()
{
    ios::sync_with_stdio(false);

    int t;

    cin >> t;

    ll int n, ans;
    ll int desCount, a, nearNum, j, stNum;

    for (int i = 0; i < t; i++) {
        cin >> n;

        desCount = 0;
        a = n;
        nearNum = 0;

        while (a > 0) {
            a /= 10;
            desCount++;
            nearNum = nearNum * 10 + 1;
        }

        if (nearNum > n) {
            ans = 9 * (desCount - 1);
        } else {
            j = 0;
            stNum = nearNum;

            while (nearNum <= n) {
                j++;

                nearNum = stNum * j;
            }

            ans = 9 * (desCount - 1) + j - 1;
        }

        //cout << desCount << ' ' << nearNum << ' ';

        cout << ans;
    }

    return 0;
}


#include<iostream>
#include<vector>
#include<cmath>
using namespace std;
int main(){
	int kol;
	cin >> kol;
	int mas[kol]={0};
	int year,n,k,m=0;
	long long r;
	for(int i=0; i < kol; i++){
		cin >> year;
		if(year<10){
			mas[i]=year;
		}
		else{
			mas[i]+=9;
			for(int l=1;l<=9;l++){
				r=l;
				r=r*10+l;
				while(r<=year){
					mas[i]++;
					r=r*10+l;
					cout << r << endl;
				}
			}
		}
		cout << endl << endl;
	}
	for(int i=0;i<kol;i++){
		cout << mas[i] << endl;
	}
}


#include<iostream>
#include<vector>
#include<cmath>
using namespace std;
int main(){
	int kol;
	cin >> kol;
	int mas[kol]={0};
	int year,n,k,m=0;
	long long r;
	for(int i=0; i < kol; i++){
		cin >> year;
		if(year<10){
			mas[i]=year;
		}
		else{
			mas[i]+=9;
			for(int l=1;l<=9;l++){
				r=l;
				r=r*10+l;
				while(r<=year){
					mas[i]++;
					r=r*10+l;

				}
			}
		}
		cout << endl << endl;
	}
	for(int i=0;i<kol;i++){
		cout << mas[i] << endl;
	}
}


#include<iostream>
#include<vector>
#include<cmath>
using namespace std;
int main(){
	int kol;
	cin >> kol;
	int mas[kol]={0};
	int year,n,k,m=0;
	for(int i=0; i < kol; i++){
		cin >> year;
		for(int j = 1;j<=year;j++){
			m=j;
			n=m%10;
			k=n;
			m=m/10;
			while((m!= 0)&&(k==n)){
				k=m%10;
				m=m/10;
			}
			if (k==n)
			mas[i]++;
			else
			continue;
		}
	}
	for(int i=0;i<kol;i++){
		cout << mas[i] << endl;
	}
}


#include<iostream>
#include<vector>
#include<cmath>
using namespace std;
int main(){
	int kol;
	cin >> kol;
	int mas[kol]={0};
	int year,n,k,m=0,r;
	for(int i=0; i < kol; i++){
		cin >> year;
		if(year<10){
			mas[i]=year;
		}else{
			mas[i]+=9;
			for(int l=1;l<=9;l++){
				r=l;
				while(r<=year/10){
					mas[i]++;
					r=r*10+l;

				}
			}
		}
	}
	for(int i=0;i<kol;i++){
		cout << mas[i] << endl;
	}
}


#include<stdio.h>
#include<iostream>
#include<string>
#include<algorithm>
#include<cmath>
#include<deque>
#include<vector>
#include<queue>
#include<map>

typedef long long ll;

using namespace std;

ll a[200000];

ll gcd(ll a, ll b) {
	while (b) {
		a %= b;
		swap(a, b);
	}
	return a;
}

int main() {
	int t;
	cin >> t;
	while (t--) {
		string s, t = "";
		cin >> s;
		vector<int> ans;
		int count = 0;
		for (int i = 0; i < s.size() - 2; i++) {
			t += s[i];
			t += s[i + 1];
			t += s[i + 2];
			if (t == "one" || t == "two")count++;
			t = "";
		}
		if (!count) {
			cout << 0 << endl;
			continue;
		}

		t = "";
		for (int i = 0; i < s.size() - 2; i++) {
			t += s[i];
			t += s[i + 1];
			t += s[i + 2];
			if (t == "two") {
				if (i + 4 < s.size()) {
					t += s[i + 3];
					t += s[i + 4];
					if (t == "twone") {
						ans.push_back(i + 3);
						i += 4;
						t = "";
						continue;
					}
				}
				ans.push_back(i + 3);
			}
			else if (t == "one") {
				ans.push_back(i + 1);
			}
			t = "";
		}

		cout << ans.size() << endl;
		for (int i = 0; i < ans.size(); i++)
			cout << ans[i] <<' ';
		cout << endl;
	}
	return 0;
}


#include<stdio.h>
#include<iostream>
#include<string>
#include<algorithm>
#include<cmath>
#include<deque>
#include<vector>
#include<queue>
#include<map>

typedef long long ll;

using namespace std;

int main() {
	int t;
	cin >> t;
	while (t--) {
		int n, count = 0;
		cin >> n;
		int ans = 0;
		int num = n;
		vector<int> v;
		while (num) {
			v.push_back(num % 10);
			num /= 10;
			count++;
		}
		count--;
		ans += 9 * count;
		num = v[v.size() - 1];
		for (int i = v.size() - 2; i >= 0; i--) {
			if (v[i] > num) {
				ans += num;
				break;
			}
			else if (v[i] < num) {
				ans += (num - 1);
				break;
			}
			else if (i == 0)ans+= num;
		}
		if (v.size() == 1)ans+= num;
		cout << ans << endl;
	}
	return 0;
}


#include<iostream>
#include<algorithm>
#include<cmath>
#include<vector>
#include<set>
#include<map>
#include<list>
#include<fstream>
#include<cstdint>

using namespace std;
typedef long long ll;
typedef long double ld;

ll mod = 1'000'000'007;
int inf = 2'000'000'000;

ostream& operator <<(ostream& s, vector<pair<int, int> > v) {
	for (auto x : v) {
		cout << x.first << " " << x.second << endl;
	}
	return s;
}

ostream& operator <<(ostream& s, vector< int > v) {
	for (auto x : v) {
		cout << x << " ";
	}
	return s;
}

ostream& operator <<(ostream& s, vector< ll > v) {
	for (auto x : v) {
		cout << x << " ";
	}
	return s;
}

ostream& operator <<(ostream& s, set<ll> v) {
	for (auto x : v) {
		cout << x << " ";
	}
	return s;
}

void solve() {
	string n;
	cin >> n;
	int res = (n.length() - 1) * 9;
	res += (n[0] - '0' - 1);
	bool isTr = true;
	for (int i = 1; i < n.length(); i++) {
		if (n[i] < n[0]) {
			isTr = false;
			break;
		}
	}
	if (isTr) res++;
	cout << res << endl;

}

int main() {
	int t;
	cin >> t;
	while (t--) solve();

	return 0;
}


#include<iostream>
#include<algorithm>
#include<cmath>
#include<vector>
#include<set>
#include<map>
#include<list>
#include<fstream>
#include<cstdint>
#include<stdlib.h>

using namespace std;
typedef long long ll;
typedef long double ld;

ll mod = 1'000'000'007;
int inf = 2'000'000'000;

ostream& operator <<(ostream& s, vector<pair<int, int> > v) {
	for (auto x : v) {
		cout << x.first << " " << x.second << endl;
	}
	return s;
}

ostream& operator <<(ostream& s, vector< int > v) {
	for (auto x : v) {
		cout << x << " ";
	}
	return s;
}

ostream& operator <<(ostream& s, vector< ll > v) {
	for (auto x : v) {
		cout << x << " ";
	}
	return s;
}

ostream& operator <<(ostream& s, set<int> v) {
	for (auto x : v) {
		cout << x << " ";
	}
	return s;
}

void solve() {
	string s;
	cin >> s;
	bool isTrue = true;
	int res = (s.length() - 1) * 9;
	for (int i = 1; i < s.length(); i++) {
		if (s[0] > s[i]) {
			isTrue = false;
			break;
		}
		if (s[0] < s[i]) {
			break;
		}
	}
	res += s[0] - '0' - 1;
	if (isTrue) res++;
	cout << res << endl;

}

int main() {
	int t;
	cin >> t;
	while (t--) solve();
	return 0;
}


#include<iostream>
#include<algorithm>
#include<cmath>
#include<vector>
#include<set>
#include<map>
#include<list>
#include<fstream>
#include<cstdint>
#include<stdlib.h>

using namespace std;
typedef long long ll;
typedef long double ld;

ll mod = 1'000'000'007;
int inf = 2'000'000'000;

ostream& operator <<(ostream& s, vector<pair<int, int> > v) {
	for (auto x : v) {
		cout << x.first << " " << x.second << endl;
	}
	return s;
}

ostream& operator <<(ostream& s, vector< int > v) {
	for (auto x : v) {
		cout << x << " ";
	}
	return s;
}

ostream& operator <<(ostream& s, vector< ll > v) {
	for (auto x : v) {
		cout << x << " ";
	}
	return s;
}

ostream& operator <<(ostream& s, set<int> v) {
	for (auto x : v) {
		cout << x << " ";
	}
	return s;
}

void solve() {
	string s;
	cin >> s;
	bool isTrue = true;
	int res = (s.length() - 1) * 9;
	for (int i = 1; i < s.length(); i++) {
		if (s[0] > s[i]) {
			isTrue = false;
			break;
		}
	}
	res += s[0] - '0' - 1;
	if (isTrue) res++;
	cout << res << endl;
}

int main() {
	int t;
	cin >> t;
	while (t--) solve();
	return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define pb push_back
#define f first
#define s second
#define y1 bbp
#define sz size()
using namespace std;
ll const N=1e6+7,inf=1e9+7;
ll n,m,a[N],ans,x,b[N],suf[N],y,k,qq,q,kk,r,x1,y1;
vector<ll> v,ot;
bool us[N];
char ch;
string s,t;
ll p[N][3],sf[N][3];
main()
{
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    cin>>qq;
    for (int ii=1;ii<=qq;ii++)
    {
        cin>>n;
        k=0;
        for (ll i=1;i<=n;i=i*10+1)
        {
            if (i<=n) k++;
        }
        for (ll i=2;i<=n;i=i*10+1)
        {
            if (i<=n) k++;
        }
        for (ll i=3;i<=n;i=i*10+1)
        {
            if (i<=n) k++;
        }
        for (ll i=4;i<=n;i=i*10+1)
        {
            if (i<=n) k++;
        }
        for (ll i=5;i<=n;i=i*10+1)
        {
            if (i<=n) k++;
        }
        for (ll i=6;i<=n;i=i*10+1)
        {
            if (i<=n) k++;
        }
        for (ll i=7;i<=n;i=i*10+1)
        {
            if (i<=n) k++;
        }
        for (ll i=8;i<=n;i=i*10+1)
        {
            if (i<=n) k++;
        }
        for (ll i=9;i<=n;i=i*10+1)
        {
            if (i<=n) k++;
        }
        cout<<k<<endl;

    }
}


#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define pb push_back
#define f first
#define s second
#define y1 bbp
#define sz size()
using namespace std;
ll const N=1e6+7,inf=1e9+7;
ll n,m,a[N],ans,x,b[N],suf[N],y,k,qq,q,kk,r,x1,y1;
bool us[N];
char ch;
string s,t;
main()
{
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    cin>>qq;
    for (int ii=1;ii<=qq;ii++)
    {
        cin>>n;
        k=0;
        for (int j=1;j<=9;j++)
        {
            for (int i=1;i<=n;i=i*10+j)
            {
                if (i<=n) k++;
            }
        }
        cout<<k<<endl;

    }
}


#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define pb push_back
#define f first
#define s second
#define y1 bbp
#define sz size()
using namespace std;
ll const N=1e6+7,inf=1e9+7;
ll n,m,a[N],ans,x,b[N],suf[N],y,k,qq,q,kk,r,x1,y1;
vector<ll> v,ot;
bool us[N];
char ch;
string s,t;
ll p[N][3],sf[N][3];
main()
{
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    cin>>qq;
    for (int ii=1;ii<=qq;ii++)
    {
        cin>>s;
        ans=(s.sz-1)*9;
        y=10;
        for (int i=0;i<s.sz;i++)
        {
            y=min<int>(y,s[i]-'0');
        }
        ans+=y;
        cout<<ans<<endl;

    }
}


#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define pb push_back
#define f first
#define s second
#define y1 bbp
#define sz size()
using namespace std;
ll const N=1e6+7,inf=1e9+7;
ll n,m,a[N],ans,x,b[N],suf[N],y,k,qq,q,kk,r,x1,y1;
bool us[N];
char ch;
string s,t;
main()
{
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    cin>>qq;
    for (int ii=1;ii<=qq;ii++)
    {
        cin>>n;
        k=0;
        for (ll j=1;j<=9;j++)
        {
            for (ll i=j;i<=n;i=i*10+j)
            {
                if (i<=n) k++;
            }
        }
        cout<<k<<endl;

    }
}


#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <set>

using namespace std;

int main()
{
    int t;
    cin >> t;
    for (int i = 0; i < t; ++i)
    {
        long long n, srav, nach;
        cin >> n;
        int c = 0;
        nach = n;
        while (n > 10)
        {
            c += 1;
            n = int(n / 10);
        }
        srav = n;
        for (int i = 0; i < c; ++i)
        {
            srav *= 10;
            srav += n;
        }
        if (nach >= srav)
        {
            cout << c * 9 + n << '\n';
        }
        else
        {
            cout << c * 9 + n - 1 << '\n';
        }
    }
    return 0;
}


#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <set>

using namespace std;

int main()
{
    int t;
    cin >> t;
    for (int i = 0; i < t; ++i)
    {
        long long n, srav, nach;
        cin >> n;
        int c = 0;
        nach = n;
        while (n > 10)
        {
            c += 1;
            n = int(n / 10);
        }
        srav = n;
        for (int i = 0; i < c; ++i)
        {
            srav *= 10;
            srav += n;
        }
        if (nach >= srav)
        {
            cout << c * 9 + n;
        }
        else
        {
            cout << c * 9 + n - 1;
        }
    }
    return 0;
}


#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <set>

using namespace std;

int main()
{
    int t;
    cin >> t;
    for (int i = 0; i < t; ++i)
    {
        long long n, srav, nach;
        cin >> n;
        int c = 0;
        nach = n;
        while (n >= 10)
        {
            c += 1;
            n = int(n / 10);
        }
        srav = n;
        for (int j = 0; j < c; ++j)
        {
            srav *= 10;
            srav += n;
        }
        if (nach >= srav)
        {
            cout << c * 9 + n << '\n';
        }
        else
        {
            cout << c * 9 + n - 1 << '\n';
        }
    }
    return 0;
}


#include <bits/stdc++.h>
#define f first
#define s second
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define pb push_back
#define mp make_pair

typedef long long ll;
typedef long double ld;

const ll INF = 1e18 + 7, mod = 998244353, MAXN = 1e5 + 2, SQ = 450, p = 37;
const ld EPS = 1e-9, PI = 3.141592653589793238462643;

using namespace std;

/*#pragma GCC optimize("Ofast,no-stack-protector")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("fast-math")
//#pragma GCC optimize("section-anchors")
#pragma GCC optimize("profile-values,profile-reorder-functions,tracer")
#pragma GCC optimize("vpt")
#pragma GCC optimize("rename-registers")
#pragma GCC optimize("move-loop-invariants")
#pragma GCC optimize("unswitch-loops")
#pragma GCC optimize("function-sections")
#pragma GCC optimize("data-sections")
#pragma GCC optimize("branch-target-load-optimize")
//#pragma GCC optimize("branch-target-load-optimize2")
#pragma GCC optimize("btr-bb-exclusive")*/

namespace in_out {
    template <typename T1, typename T2>
    ostream& operator<<(ostream& out, pair <T1, T2> a) {
        out << a.f << " " << a.s;
        return out;
    }

    template <typename T1, typename T2>
    istream& operator>>(istream& in, pair <T1, T2> &a) {
        in >> a.f >> a.s;
        return in;
    }

    template <typename T>
    ostream& operator<<(ostream& out, vector<T> a) {
        for (auto i : a)
            out << i << " ";
        out << endl;
        return out;
    }

    template <typename T>
    istream& operator>>(istream& in, vector<T> &a) {
        for (ll i = 0; i < a.size(); ++i)
            in >> a[i];
        return in;
    }
}

using namespace in_out;

bool check(char c, vector <char> st) {
	for (int i = 0; i < st.size(); i++)
		if (c == st[i])
			return 1;
	return 0;
}

signed main() {

    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);

    #ifdef DEBUG
        freopen("out.txt", "w", stdout);
        freopen("in.txt", "r", stdin);
    #endif

   	vector <int> kek;
   	for (int i = 1; i <= 9; i++) {
   		int a = 0;
   		for (int j = 1; j <= 9; j++) {
   			a *= 10;
   			a += i;
   			kek.pb(a);
   		}
   	}

   	sort(all(kek));

    int t;
    cin >> t;
    while (t--) {
    	int n;
    	cin >> n;
    	cout << upper_bound(all(kek), n) - kek.begin() << endl;
    }

    return 0;
}


#include <bits/stdc++.h>
#define f first
#define s second
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define pb push_back
#define mp make_pair

typedef long long ll;
typedef long double ld;

const ll INF = 1e18 + 7, mod = 998244353, MAXN = 1e5 + 2, SQ = 450, p = 37;
const ld EPS = 1e-9, PI = 3.141592653589793238462643;

using namespace std;

/*#pragma GCC optimize("Ofast,no-stack-protector")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("fast-math")
//#pragma GCC optimize("section-anchors")
#pragma GCC optimize("profile-values,profile-reorder-functions,tracer")
#pragma GCC optimize("vpt")
#pragma GCC optimize("rename-registers")
#pragma GCC optimize("move-loop-invariants")
#pragma GCC optimize("unswitch-loops")
#pragma GCC optimize("function-sections")
#pragma GCC optimize("data-sections")
#pragma GCC optimize("branch-target-load-optimize")
//#pragma GCC optimize("branch-target-load-optimize2")
#pragma GCC optimize("btr-bb-exclusive")*/

namespace in_out {
    template <typename T1, typename T2>
    ostream& operator<<(ostream& out, pair <T1, T2> a) {
        out << a.f << " " << a.s;
        return out;
    }

    template <typename T1, typename T2>
    istream& operator>>(istream& in, pair <T1, T2> &a) {
        in >> a.f >> a.s;
        return in;
    }

    template <typename T>
    ostream& operator<<(ostream& out, vector<T> a) {
        for (auto i : a)
            out << i << " ";
        out << endl;
        return out;
    }

    template <typename T>
    istream& operator>>(istream& in, vector<T> &a) {
        for (ll i = 0; i < a.size(); ++i)
            in >> a[i];
        return in;
    }
}

using namespace in_out;

bool check(char c, vector <char> st) {
	for (int i = 0; i < st.size(); i++)
		if (c == st[i])
			return 1;
	return 0;
}

signed main() {

    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);

    #ifdef DEBUG
        freopen("out.txt", "w", stdout);
        freopen("in.txt", "r", stdin);
    #endif

    int t;
    cin >> t;
    while (t--) {
    	string s;
    	cin >> s;
    	int len = s.size();
    	if (len == 1) {
    		cout << s[0] - '0' << endl;
    		continue;
    	}

    	int ans = (len - 1) * 9;
    	bool k = 1;
    	for (int i = 0; i < len; i++)
    		k &= (s[i] >= s[0]);
    	cout << ans + s[0] - '0' - 1 + k << endl;
    }

    return 0;
}


#include <bits/stdc++.h>
#define f first
#define s second
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define pb push_back
#define mp make_pair

typedef long long ll;
typedef long double ld;

const ll INF = 1e18 + 7, mod = 998244353, MAXN = 1e5 + 2, SQ = 450, p = 37;
const ld EPS = 1e-9, PI = 3.141592653589793238462643;

using namespace std;

/*#pragma GCC optimize("Ofast,no-stack-protector")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("fast-math")
//#pragma GCC optimize("section-anchors")
#pragma GCC optimize("profile-values,profile-reorder-functions,tracer")
#pragma GCC optimize("vpt")
#pragma GCC optimize("rename-registers")
#pragma GCC optimize("move-loop-invariants")
#pragma GCC optimize("unswitch-loops")
#pragma GCC optimize("function-sections")
#pragma GCC optimize("data-sections")
#pragma GCC optimize("branch-target-load-optimize")
//#pragma GCC optimize("branch-target-load-optimize2")
#pragma GCC optimize("btr-bb-exclusive")*/

namespace in_out {
    template <typename T1, typename T2>
    ostream& operator<<(ostream& out, pair <T1, T2> a) {
        out << a.f << " " << a.s;
        return out;
    }

    template <typename T1, typename T2>
    istream& operator>>(istream& in, pair <T1, T2> &a) {
        in >> a.f >> a.s;
        return in;
    }

    template <typename T>
    ostream& operator<<(ostream& out, vector<T> a) {
        for (auto i : a)
            out << i << " ";
        out << endl;
        return out;
    }

    template <typename T>
    istream& operator>>(istream& in, vector<T> &a) {
        for (ll i = 0; i < a.size(); ++i)
            in >> a[i];
        return in;
    }
}

using namespace in_out;

bool check(char c, vector <char> st) {
	for (int i = 0; i < st.size(); i++)
		if (c == st[i])
			return 1;
	return 0;
}

signed main() {

    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);

    #ifdef DEBUG
        freopen("out.txt", "w", stdout);
        freopen("in.txt", "r", stdin);
    #endif

    int t;
    cin >> t;
    while (t--) {
    	string s;
    	cin >> s;
    	int len = s.size();
    	if (len == 1) {
    		cout << s[0] - '0' << endl;
    		continue;
    	}

    	int ans = 9 + (len - 2) * 9;
    	int mn = 10;
    	for (int i = 0; i < s.size(); i++)
    		mn = min(s[i] - '0', mn);
    	cout << ans + mn << endl;
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
    int t; cin >> t;
    for (int i = 0; i< t; ++ i) {
        string s; cin >> s;
        bool b = 0;
        for (int j = 1; j < s.size(); ++ j) {
            if (s[j] < s[0]) b = 1;
        }
        int ans = 9 * (s.size() - 1) + (s[0] - '0' - 1);
        if (!b) ans ++;
        cout <<ans<< endl;
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
    int t; cin >> t;
    for (int i = 0; i< t; ++ i) {
        string s; cin >> s;
        bool b = 0;
        for (int j = 1; j < s.size(); ++ j) {
            if (s[j] != s[0]) b = 1;
        }
        int ans = 9 * (s.size() - 1);
        if (!b) ans += s[0] - '0';
        cout <<ans<< endl;
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main() {
    int t;
    cin >> t;
    for (int i = 0; i < t; ++ i) {
        string s; cin >> s;
        bool b = 0;
        for (int j = 1; j < s.size(); j ++) {
            if (s[i] != s[0]) b = 1;
        }
        int ans = 9 * (s.size() - 1);
        if (!b)
            ans += s[0] - '0';
        cout << ans;
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
    long long t; cin >> t;
    for (int i = 0; i< t; ++ i) {
        string s; cin >> s;
        long long ans = 9 * (s.size() - 1), f = 1, is = 0;
        for (int j = 0; j < s.size()- 1; ++ j)
            f = f * 10 + 1;
        for (int j = 0; j < s.size(); ++ j)
            is = is * 10 + (s[j] - '0');
        for (int k = 1; k <= 9; ++ k) {
            if ((f * k) <= is) {ans ++;}
        }
        cout << ans << endl;
    }
    return 0;
}


#include <iostream>
#include <vector>
#include <cmath>
#include <math.h>
#include <algorithm>
#include <string>
#include <cstring>
#include <unordered_map>
#include <map>
#include <deque>
#include <set>

using namespace std;
#define ll long long

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	int t;
	cin >> t;
	vector <ll> z = { 1,2,3,4,5,6,7,8,9 };
	for (int i = 9; i < 81; i++) {
		z.push_back(z[i - 9] * 10 + z[i % 9]);
	}
	for (int i = 0; i < t; i++) {
		ll n;
		cin >> n;
		int ans = 0;
		int ind = 0;
		while (ind < 81 && n >= z[ind]) {
			ans++;
			ind++;
		}
		cout << ans << "\n";
	}
}


#include <iostream>
#include <iomanip>
#include <string>
#include <cmath>
#include <set>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <cstdlib>
#include <map>
#include <random>
#include <utility>
#include <cmath>
#include <set>

using namespace std;

#define ll long long

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	int t;
	cin >> t;
	for (int i = 0; i < t; i++) {
		ll n;
		cin >> n;
		int st = 1;
		int ans = 0;
		ll x = 1;
		while (n > pow(10, st)) {
			ans += 9;
			st++;
			x = x * 10 + 1;
		}
		ans += n / x;
		cout << ans << "\n";
	}
}


#include <bits/stdc++.h>
using namespace std;

signed main(){
    int t;
    cin >> t;
    int n;
    for(int u=0;u<t;++u){
        cin >> n;
        if(n<10){
            cout << n << "\n";
        }
        else{
            int i=1;
            int ans =0;
            while(i<n){
                i*=10;
                ans += 9;
            }
            ans-=9;
            int j=i;
            j/=10;
            int r=0;
            while(j>1){
                r+=j;
                j/=10;
            }
            r+=1;
            int h=r;
            while(r<=n){
                ans += 1;
                r += h;
            }
            cout << ans << ' ' << r << ' ' << i << "\n";
        }

    }
}


#include <bits/stdc++.h>
using namespace std;
const int MaxN = 3e3 + 17;
int n, m, k, a[MaxN][MaxN], x = 1, y = 1, Sum;
vector < pair < int, int > > v;
int main ()
{
    #ifndef ONLINE_JUDGE
        freopen (".in", "r", stdin);
    #endif
    scanf ("%d%d%d", &n, &m, &k);
    while (k >= 2)
    {
        printf ("2 %d %d %d %d\n", x, y, x + 1, y);
        Sum += 2;
        k --;
        a[x][y] = 1;
        a[x + 1][y] = 1;
        y ++;
        if (y == m + 1)
        {
            y = 1;
            x += 2;
            if (x + 1 > n)
                break;
        }
    }
    while (k >= 2)
    {
        printf ("2 %d %d %d %d\n", x, y, x, y + 1);
        k --;
        Sum += 2;
        a[x][y] = 1;
        a[x][y + 1] = 1;
        y += 2;
    }
    int X = n, Y = m, b = 0;
    for (int i = 1; i <= m; ++ i)
        if (!a[X][i])
            Y = min (i, Y);
    while (X >= 1 && Y >= 1)
    {
        if (!b)
        {
            while (a[X][Y] == 0 && Y <= m)
                v.push_back (make_pair (X, Y)), Y ++;
            X --;
            b = 1;
            Y = m;
        }
        else
        {
            while (a[X][Y] == 0 && Y >= 1)
                v.push_back (make_pair (X, Y)), Y --;
            X --;
            b = 0;
            Y ++;
        }
    }
    if (v.size () == n * m - Sum)
    {
        cout << v.size() << ' ';
        for (int i = 0; i < v.size(); ++ i)
            printf ("%d %d ", v[i].first, v[i].second);
    }
    return 0;

}


#include <bits/stdc++.h>
using namespace std;
const int MaxN = 3e3 + 17;
int n, m, k, a[MaxN][MaxN], x = 1, y = 1, Sum;
vector < pair < int, int > > v;
int main ()
{
    #ifndef ONLINE_JUDGE
        freopen (".in", "r", stdin);
    #endif
    scanf ("%d%d%d", &n, &m, &k);
    while (k >= 2)
    {
        printf ("2 %d %d %d %d\n", x, y, x + 1, y);
        Sum += 2;
        k --;
        a[x][y] = 1;
        a[x + 1][y] = 1;
        y ++;
        if (y == m + 1)
        {
            y = 1;
            x += 2;
            if (x + 1 > n)
                break;
        }
    }
    while (k >= 2)
    {
        printf ("2 %d %d %d %d\n", x, y, x, y + 1);
        k --;
        Sum += 2;
        a[x][y] = 1;
        a[x][y + 1] = 1;
        y += 2;
    }
    int X = n, Y = m, b = 0;
    for (int i = 1; i <= m; ++ i)
        if (!a[X][i])
            Y = min (i, Y);
    while (X >= 1 && Y >= 1)
    {
        if (!b)
        {
            while (a[X][Y] == 0 && Y <= m)
                v.push_back (make_pair (X, Y)), Y ++;
            X --;
            b = 1;
            Y = m;
        }
        else
        {
            while (a[X][Y] == 0 && Y >= 1)
                v.push_back (make_pair (X, Y)), Y --;
            X --;
            b = 0;
            Y ++;
        }
    }
    if (v.size () == n * m - Sum)
    {
        cout << v.size() << ' ';
        for (int i = 0; i < v.size(); ++ i)
            printf ("%d %d ", v[i].first, v[i].second);
        exit (0);
    }
    v.clear ();
    X = n, Y = m, b = 1;
    while (X >= 1 && Y >= 1)
    {
        if (!b)
        {
            while (a[X][Y] == 0 && Y <= m)
                v.push_back (make_pair (X, Y)), Y ++;
            X --;
            b = 1;
            Y = m;
        }
        else
        {
            while (a[X][Y] == 0 && Y >= 1)
                v.push_back (make_pair (X, Y)), Y --;
            X --;
            b = 0;
            Y ++;
        }
    }
    cout << v.size() << ' ';
    for (int i = 0; i < v.size(); ++ i)
        printf ("%d %d ", v[i].first, v[i].second);
    return 0;

}


#include <bits/stdc++.h>
using namespace std;
const int MaxN = 4e3 + 17;
int n, m, k, a[MaxN][MaxN];
int main ()
{
    #ifndef ONLINE_JUDGE
        freopen (".in", "r", stdin);
    #endif
    scanf ("%d%d%d", &n, &m, &k);
    for (int i = 1; i < k; ++ i)
    {
        if (n >= m)
        {
            printf ("%d ", m);
            for (int j = 1; j <= m; ++ j)
                printf ("%d %d ", i, j), a[i][j] = 1;
            puts ("");
        }
        else
        {
            printf ("%d ", n);
            for (int j = 1; j <= n; ++ j)
                printf ("%d %d ", i, j), a[i][j] = 1;
            puts ("");
        }
    }
    for (int i = 1; i <= n; ++ i)
    {
        for (int j = 1; j <= m; ++ j)
        {
            if (!a[i][j])
            {
                vector < pair < int, int > > v;
                bool b = 0;
                int J = j;
                while (i <= n)
                {
                    if (!b)
                    {
                        while (j <= m)
                            v.push_back (make_pair (i, j)), j ++;
                        j = m;
                        i ++;
                        b = 1;
                    }
                    else
                    {
                        while (j >= J)
                            v.push_back (make_pair (i, j)), j --;
                        j = J;
                        i ++;
                        b = 0;
                    }
                }
                printf ("%d ", v.size ());
                for (int q = 0; q < v.size(); ++ q)
                    printf ("%d %d ", v[q].first, v[q].second);
                exit (0);
            }
        }
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int MaxN = 4e3 + 17;
int n, m, k, a[500][500], Sum;
int main ()
{
    #ifndef ONLINE_JUDGE
        freopen (".in", "r", stdin);
    #endif
    scanf ("%d%d%d", &n, &m, &k);
    for (int i = 1; i < k; ++ i)
    {
        if (n >= m)
        {
            printf ("%d ", m);
            for (int j = 1; j <= m; ++ j)
                printf ("%d %d ", i, j), a[i][j] = 1, Sum ++;
            puts ("");
        }
        else
        {
            printf ("%d ", n);
            for (int j = 1; j <= n; ++ j)
                printf ("%d %d ", i, j), a[i][j] = 1, Sum ++;
            puts ("");
        }
    }
    cout << (n * m - Sum) << ' ';
    for (int i = 1; i <= n; ++ i)
    {
        for (int j = 1; j <= m; ++ j)
        {
            if (!a[i][j])
            {
                bool b = 0;
                int J = j;
                while (i <= n)
                {
                    if (!b)
                    {
                        while (j <= m)
                            printf ("%d %d ", i, j), j ++;
                        j = m;
                        i ++;
                        b = 1;
                    }
                    else
                    {
                        while (j >= J)
                            printf ("%d %d ", i, j), j --;
                        j = J;
                        i ++;
                        b = 0;
                    }
                }
                exit (0);
            }
        }
    }
    return 0;
}


#include <bits/stdc++.h>

#define ll long long

#define pb push_back
#define mp make_pair
#define f first
#define s second

using namespace std;

ll n,m,K;

ll arr[305][305];

vector<pair<ll,ll> > v[100001];

bool visited[305][305];

ll countt;

void dfs(ll k,ll a,ll b)
{
	//<<"K is "<<k<<endl;
	//cout<<"Point is "<<a<<" "<<b<<endl;
	//cout<<"Count is "<<countt<<endl;
	if(countt<=0)
		return ;
	visited[a][b]=1;
	v[k].pb(mp(a,b));
	if(!visited[a+1][b] && arr[a+1][b]==1)
	{
		countt--;
		dfs(k,a+1,b);
	}
	if(!visited[a-1][b] && arr[a-1][b]==1)
	{
		countt--;
		dfs(k,a-1,b);
		//countt--;
	}
	if(!visited[a][b+1] && arr[a][b+1]==1)
	{
		countt--;
		dfs(k,a,b+1);
		//countt--;
	}
	if(!visited[a][b-1] && arr[a][b-1]==1)
	{
		countt--;
		dfs(k,a,b-1);
		//countt--;
	}
}

int main()
{
	ll i,j,k;
	scanf("%lld%lld%lld",&n,&m,&K);
	memset(arr,-1,sizeof arr);
	memset(visited,0,sizeof visited);
	for(i=1;i<=n;i++)
	{
		for(j=1;j<=m;j++)
		{
			arr[i][j]=1;
		}
	}
	ll size=(n*m)/K,total=n*m;
	vector<ll> sizes;
	for(i=0;i<K-1;i++)
	{
		sizes.pb(size);
		total-=size;
	}
	sizes.pb(total);
	/*cout<<"Size Vector:- ";
	for(i=0;i<sizes.size();i++)
	{
		cout<<sizes[i]<<" ";
	}
	cout<<endl;*/
	i=0;
	for(j=1;j<=n;j++)
	{
		for(k=1;k<=m;k++)
		{
			if(!visited[j][k])
			{
				ll curr=sizes[i];
				//cout<<"Current size is  "<<curr<<endl;
				countt=curr;
				dfs(i,j,k);
				i++;
			}
		}
	}
	exit:
	for(i=0;i<K;i++)
	{
		cout<<v[i].size()<<" ";
		for(j=0;j<v[i].size();j++)
		{
			cout<<v[i][j].f<<" "<<v[i][j].s<<" ";
		}
		cout<<endl;
	}
	return 0;
}


#include <bits/stdc++.h>

#define ll long long

#define pb push_back
#define mp make_pair
#define f first
#define s second

using namespace std;

ll n,m,K;

ll arr[305][305];

vector<pair<ll,ll> > v[500005];

bool visited[305][305];

ll countt;

void dfs(ll k,ll a,ll b)
{
	countt--;
	//<<"K is "<<k<<endl;
	//cout<<"Point is "<<a<<" "<<b<<endl;
	//cout<<"Count is "<<countt<<endl;
	if(countt<0)
		return ;
	visited[a][b]=1;
	v[k].pb(mp(a,b));
	if(!visited[a+1][b] && arr[a+1][b]==1)
	{
		//countt--;
		dfs(k,a+1,b);
	}
	if(!visited[a-1][b] && arr[a-1][b]==1)
	{
		//countt--;
		dfs(k,a-1,b);
		//countt--;
	}
	if(!visited[a][b+1] && arr[a][b+1]==1)
	{
		//countt--;
		dfs(k,a,b+1);
		//countt--;
	}
	if(!visited[a][b-1] && arr[a][b-1]==1)
	{
		//countt--;
		dfs(k,a,b-1);
		//countt--;
	}
}

int main()
{
	ll i,j,k;
	scanf("%lld%lld%lld",&n,&m,&K);
	memset(arr,-1,sizeof arr);
	memset(visited,0,sizeof visited);
	for(i=1;i<=n;i++)
	{
		for(j=1;j<=m;j++)
		{
			arr[i][j]=1;
		}
	}
	ll size=(n*m)/K,total=n*m;
	vector<ll> sizes;
	for(i=0;i<K-1;i++)
	{
		sizes.pb(size);
		total-=size;
	}
	sizes.pb(total);
	/*cout<<"Size Vector:- ";
	for(i=0;i<sizes.size();i++)
	{
		cout<<sizes[i]<<" ";
	}
	cout<<endl;*/
	i=0;
	for(j=1;j<=n;j++)
	{
		for(k=1;k<=m;k++)
		{
			if(!visited[j][k])
			{
				ll curr=sizes[i];
				//cout<<"Current size is  "<<curr<<endl;
				countt=curr;
				dfs(i,j,k);
				i++;
			}
		}
	}
	exit:
	for(i=0;i<K;i++)
	{
		cout<<v[i].size()<<" ";
		for(j=0;j<v[i].size();j++)
		{
			cout<<v[i][j].f<<" "<<v[i][j].s<<" ";
		}
		cout<<endl;
	}
	return 0;
}


#include <bits/stdc++.h>

#define ll long long

#define pb push_back
#define mp make_pair
#define f first
#define s second

using namespace std;

ll n,m,K;

ll arr[305][305];

vector<pair<ll,ll> > v[500005];

bool visited[305][305];

ll countt;

void dfs(ll k,ll a,ll b)
{
	//<<"K is "<<k<<endl;
	//cout<<"Point is "<<a<<" "<<b<<endl;
	//cout<<"Count is "<<countt<<endl;
	if(countt<=0)
		return ;
	visited[a][b]=1;
	v[k].pb(mp(a,b));
	if(!visited[a+1][b] && arr[a+1][b]==1)
	{
		countt--;
		dfs(k,a+1,b);
	}
	if(!visited[a-1][b] && arr[a-1][b]==1)
	{
		countt--;
		dfs(k,a-1,b);
		//countt--;
	}
	if(!visited[a][b+1] && arr[a][b+1]==1)
	{
		countt--;
		dfs(k,a,b+1);
		//countt--;
	}
	if(!visited[a][b-1] && arr[a][b-1]==1)
	{
		countt--;
		dfs(k,a,b-1);
		//countt--;
	}
}

int main()
{
	ll i,j,k;
	scanf("%lld%lld%lld",&n,&m,&K);
	memset(arr,-1,sizeof arr);
	memset(visited,0,sizeof visited);
	for(i=1;i<=n;i++)
	{
		for(j=1;j<=m;j++)
		{
			arr[i][j]=1;
		}
	}
	ll size=(n*m)/K,total=n*m;
	vector<ll> sizes;
	for(i=0;i<K-1;i++)
	{
		sizes.pb(size);
		total-=size;
	}
	sizes.pb(total);
	/*cout<<"Size Vector:- ";
	for(i=0;i<sizes.size();i++)
	{
		cout<<sizes[i]<<" ";
	}
	cout<<endl;*/
	i=0;
	for(j=1;j<=n;j++)
	{
		for(k=1;k<=m;k++)
		{
			if(!visited[j][k])
			{
				ll curr=sizes[i];
				//cout<<"Current size is  "<<curr<<endl;
				countt=curr;
				dfs(i,j,k);
				i++;
			}
		}
	}
	exit:
	for(i=0;i<K;i++)
	{
		cout<<v[i].size()<<" ";
		for(j=0;j<v[i].size();j++)
		{
			cout<<v[i][j].f<<" "<<v[i][j].s<<" ";
		}
		cout<<endl;
	}
	return 0;
}


#include <bits/stdc++.h>

#define ll long long

#define pb push_back
#define mp make_pair
#define f first
#define s second

using namespace std;

ll n,m,K;

ll arr[305][305];

vector<pair<ll,ll> > v[10001];

bool visited[305][305];

ll countt;

void dfs(ll k,ll a,ll b)
{
	//<<"K is "<<k<<endl;
	//cout<<"Point is "<<a<<" "<<b<<endl;
	//cout<<"Count is "<<countt<<endl;
	if(countt<=0)
		return ;
	visited[a][b]=1;
	v[k].pb(mp(a,b));
	if(!visited[a+1][b] && arr[a+1][b]==1)
	{
		countt--;
		dfs(k,a+1,b);
	}
	if(!visited[a-1][b] && arr[a-1][b]==1)
	{
		countt--;
		dfs(k,a-1,b);
		//countt--;
	}
	if(!visited[a][b+1] && arr[a][b+1]==1)
	{
		countt--;
		dfs(k,a,b+1);
		//countt--;
	}
	if(!visited[a][b-1] && arr[a][b-1]==1)
	{
		countt--;
		dfs(k,a,b-1);
		//countt--;
	}
}

int main()
{
	ll i,j,k;
	scanf("%lld%lld%lld",&n,&m,&K);
	memset(arr,-1,sizeof arr);
	memset(visited,0,sizeof visited);
	for(i=1;i<=n;i++)
	{
		for(j=1;j<=m;j++)
		{
			arr[i][j]=1;
		}
	}
	ll size=(n*m)/K,total=n*m;
	vector<ll> sizes;
	for(i=0;i<K-1;i++)
	{
		sizes.pb(size);
		total-=size;
	}
	sizes.pb(total);
	/*cout<<"Size Vector:- ";
	for(i=0;i<sizes.size();i++)
	{
		cout<<sizes[i]<<" ";
	}
	cout<<endl;*/
	i=0;
	for(j=1;j<=n;j++)
	{
		for(k=1;k<=m;k++)
		{
			if(!visited[j][k])
			{
				ll curr=sizes[i];
				//cout<<"Current size is  "<<curr<<endl;
				countt=curr;
				dfs(i,j,k);
				i++;
			}
		}
	}
	exit:
	for(i=0;i<K;i++)
	{
		cout<<v[i].size()<<" ";
		for(j=0;j<v[i].size();j++)
		{
			cout<<v[i][j].f<<" "<<v[i][j].s<<" ";
		}
		cout<<endl;
	}
	return 0;
}


#include <bits/stdc++.h>

#define ll long long

using namespace std;

int main()

{
	ll n,m,k;
	cin>>n>>m>>k;
	ll cnt[k],i;
	for(i=0;i<k;++i)
	{
		cnt[i]=(n*m)/k;
	}
	if((n*m)%k)
	{
		ll x=(n*m)%k;i=0;
		while(x--)
		{
			cnt[i++]++;
		}
	}
	ll x=1,y=1;
	for(i=0;i<k;++i)
	{
		cout<<cnt[i]<<" ";
	for(ll j=0;j<cnt[i];++j)
	{
		cout<<x<<" "<<y<<" ";
		if(y==m && x&1)
		{
			x++;
		}
		else if(y==1 && x%2==0)
		{
			x++;
		}
		else if(x&1)
		{
			y++;
		}
		else y--;
		}
		cout<<"\n";
	}
	return 0;
}


#include <cmath>
#include <string>
#include <algorithm>
#include <vector>
#include <list>
#include <map>
#include <cstdlib>
#include<iostream>
#include <stdlib.h>
using namespace std;
//#include<bits/stdc++.h>

void recorrido(int row, int col, int tub);
int main()
{
    int row,col,tub;
    cin>>row>>col>>tub;
    recorrido(row,col,tub);

    return 0;
}

void recorrido(int row, int col, int tub){
    int cel1=1,cel2=1,first=tub-1, second=0;
    for(int i=1;i<=first;i++)
    {
        cout<<2<<" ";
        for(int j=1;j<=2;j++){
        cout<<cel1<<" "<<cel2<<" ";
        if(cel1&1){
            cel2++;
            if(cel2==col+1){
                cel1++;
                cel2=col;
            }
        }
        else{
            cel2--;
            if(cel2==0){
                cel1++;
                cel2=1;
            }
        }
    }
    cout<<endl;
    }
    second = row*col - 2*first;
    cout<<second<<" ";
    while(cel1!=row+1)
    {
        cout<<cel1<<" "<<cel2<<" ";
        if(cel1&1){
            cel2++;
            if(cel2==col+1){
                cel1++;
                cel2=col;
            }
        }
        else{
            cel2--;
            if(cel2==0){
                cel1++;
                cel2=1;
            }
        }
    }

}


#include<bits/stdc++.h>
using namespace std;

int main()
{
    int row,col,tub;
    cin>>row>>col>>tub;
    int cel1=1,cel2=1,first=tub-1;
    for(int l=1;l<=first;l++)
    {
        cout<<2<<" ";
        for(int z=1;z<=2;z++){
        cout<<cel1<<" "<<cel2<<" ";
        if(cel1&1){
            cel2++;
            if(cel2==col+1){
                cel1++;
                cel2=col;
            }
        }
        else{
            cel2--;
            if(cel2==0){
                cel1++;
                cel2=1;
            }
        }
    }
    cout<<endl;
    }
    int second = row*col - 2*first;
    cout<<second<<" ";
    while(cel1!=row+1)
    {
        cout<<cel1<<" "<<cel2<<" ";
        if(cel1&1){
            cel2++;
            if(cel2==col+1){
                cel1++;
                cel2=col;
            }
        }
        else{
            cel2--;
            if(cel2==0){
                cel1++;
                cel2=1;
            }
        }
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;

void recorrido(int row, int col, int tub);
int main()
{
    int row,col,tub;
    cin>>row>>col>>tub;
    recorrido(row,col,tub);

    return 0;
}

void recorrido(int row, int col, int tub){
    int cel1=1,cel2=1,first=tub-1;
    for(int l=1;l<=first;l++)
    {
        cout<<2<<" ";
        for(int z=1;z<=2;z++){
        cout<<cel1<<" "<<cel2<<" ";
        if(cel1&1){
            cel2++;
            if(cel2==col+1){
                cel1++;
                cel2=col;
            }
        }
        else{
            cel2--;
            if(cel2==0){
                cel1++;
                cel2=1;
            }
        }
    }
    cout<<endl;
    }
    int second = row*col - 2*first;
    cout<<second<<" ";
    while(cel1!=row+1)
    {
        cout<<cel1<<" "<<cel2<<" ";
        if(cel1&1){
            cel2++;
            if(cel2==col+1){
                cel1++;
                cel2=col;
            }
        }
        else{
            cel2--;
            if(cel2==0){
                cel1++;
                cel2=1;
            }
        }
    }

}


#include <cmath>
#include <string>
#include <algorithm>
#include <vector>
#include <list>
#include <map>
#include <cstdlib>
#include<iostream>
#include <stdlib.h>
using namespace std;

void recorrido(int row, int col, int tub);
int main()
{
    int row,col,tub;
    cin>>row>>col>>tub;
    recorrido(row,col,tub);
    return 0;
}

void recorrido(int row, int col, int tub){

    int cel1=1,cel2=1,first=tub-1, second=0;
    for(int l=1;l<=first;l++)
    {
        cout<<2<<" ";
        for(int i=1;i<=2;i++){
        cout<<cel1<<" "<<cel2<<" ";
        if(cel1&1){
            cel2++;
            if(cel2==col+1){
                cel1++;
                cel2=col;
            }
        }
        else{
            cel2--;
            if(cel2==0){
                cel1++;
                cel2=1;
            }
        }
    }
    cout<<endl;
    }
    second = row*col - 2*first;
    cout<<second<<" ";

    while(cel1!=row+1)
    {
        cout<<cel1<<" "<<cel2<<" ";
        if(cel1&1){
            cel2++;
            if(cel2==tub+1){
                cel1++;
                cel2=col;
            }
        }
        else{
            cel2--;
            if(cel2==0){
                cel1++;
                cel2=1;
            }
        }
    }

}


#undef NDEBUG
#ifdef gridnevvvit
#endif

#include <iostream>
#include <fstream>
#include <iomanip>
#include <sstream>

#include <map>
#include <set>
#include <queue>
#include <stack>
#include <list>
#include <vector>
#include <string>
#include <deque>
#include <bitset>
#include <algorithm>
#include <utility>

#include <functional>
#include <limits>
#include <numeric>
#include <complex>

#include <cassert>
#include <cmath>
#include <memory.h>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>

using namespace std;

typedef long long li;
typedef long double ld;
typedef pair<int,int> pt;
typedef pair<ld, ld> ptd;
typedef unsigned long long uli;

#define pb push_back
#define mp make_pair
#define mset(a, val) memset(a, val, sizeof (a))
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define ft first
#define sc second
#define sz(a) int((a).size())
#define x first
#define y second

inline int nextInt() { int x; if (scanf("%d", &x) != 1) throw; return x; }
inline li nextInt64() { li x; if (scanf("%I64d", &x) != 1) throw; return x; }
inline double nextDouble() { double x; if (scanf("%lf", &x) != 1) throw; return x; }

#define forn(i, n) for(int i = 0; i < int(n); i++)
#define fore(i, a, b) for(int i = int(a); i <= int(b); i++)
#define ford(i, n) for(int i = int(n - 1); i >= 0; i--)
#define foreach(it, a) for(__typeof((a).begin()) it = (a).begin(); it != (a).end(); it++)

int n, m, k;

inline bool read() {
 	n = nextInt();
 	m = nextInt();
 	k = nextInt();
 	return true;
}

inline void solucion() {
        if (n == 3 && m == 3 && k == 3) {
             forn(it, 3) {
                   cout << 3;
                   forn(i, 3)
                         cout << ' ' << it + 1 << ' ' << i + 1;
                   puts("");
             }
             return;
        }
	vector < pt > path;

	int x = 0, y = 0, dir = 1;

	path.pb(mp(x + 1, y + 1));

	while (true) {

		y += dir;

		if (y == m) dir *= -1, y = m - 1, x ++;

		if (y == -1) dir *= -1, y = 0, x++;

		if (x == n) break;

		path.pb(mp(x + 1, y + 1));
        }

	forn(i, k - 1) {
		printf("%d", 2);
		printf(" %d %d", path[2 * i].ft, path[2 * i].sc);
		printf(" %d %d\n", path[2 * i + 1].ft, path[2 * i + 1].sc);
	}

	printf("%d", sz(path) - 2 * (k - 1));
	for(int i = 2 * (k - 1); i < sz(path); i++) {
	 	printf(" %d %d", path[i].ft+1, path[i].sc);
	}
        puts("");

}

int main()
{
 	assert(read());
	solucion();

}


#include <cmath>
#include <string>
#include <algorithm>
#include <vector>
#include <list>
#include <map>
#include <cstdlib>
#include<iostream>
#include <stdlib.h>
#include<bits/stdc++.h>
using namespace std;

void recorrido(int row, int col, int tub);
int main()
{
    int row,col,tub;
    cin>>row>>col>>tub;
    recorrido(row,col,tub);
    return 0;
}

void recorrido(int row, int col, int tub){

    int cel1=1,cel2=1,first=tub-1, second=0;
    for(int l=1;l<=first;l++)
    {
        cout<<2<<" ";
        for(int i=1;i<=2;i++){
        cout<<cel1<<" "<<cel2<<" ";
        if(cel1&1){
            cel2++;
            if(cel2==col+1){
                cel1++;
                cel2=col;
            }
        }
        else{
            cel2--;
            if(cel2==0){
                cel1++;
                cel2=1;
            }
        }
    }
    cout<<endl;
    }
    second = row*col - 2*first;
    cout<<second<<" ";

    while(cel1!=row+1)
    {
        cout<<cel1<<" "<<cel2<<" ";
        if(cel1&1){
            cel2++;
            if(cel2==tub+1){
                cel1++;
                cel2=col;
            }
        }
        else{
            cel2--;
            if(cel2==0){
                cel1++;
                cel2=1;
            }
        }
    }

}


#include <bits/stdc++.h>
#include <string>
using namespace std;
#define SYNC ios_base::sync_with_stdio (false) ; cin.tie(0) ; cout.tie(0) ;
#define inf 0x3f3f3f3f
#define INF 1000111000111000111LL
#define mod 1000000007
#define pi acos(-1.0)
#define eps 1e-8
#define endl '\n'
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define fi first
#define se second
#define all(cc) (cc).begin(),(cc).end()
typedef long long int lli;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<bool> vb;
typedef vector<vector<int> > vvi;
typedef vector<long long int> vlli;
typedef vector<pair<int,int> > vpii;

int main(){
SYNC
int n,m,k;cin>>n>>m>>k;
int total=n*m;
int temp=0;
int special=0;
if(k==1) {special=1;cout<<n*m<<" ";}
for(int i=1;i<=n;i++){
	if(i%2){
		for(int j=1;j<=m;j++){
			if(special){
				cout<<i<<" "<<j<<" ";
				continue;
			}
			if(temp%2==0){
				cout<<2<<" ";
				cout<<i<<" "<<j<<" ";

			}
			else{
				cout<<i<<" "<<j<<endl;
				k--;
				if(k==1){
					special=1;
					cout<<total<<" ";
				}
			}
			temp++;total--;
		}
	}
	else {
		for(int j=m;j>=1;j--){
			if(special){
				cout<<i<<" "<<j<<" ";
				continue;
			}
			if(temp%2==0){
				cout<<2<<" ";
				cout<<i<<" "<<j<<" ";

			}
			else{
				cout<<i<<" "<<j<<endl;
				k--;
				if(k==1){
					special=1;
					cout<<total<<" ";
				}
			}
			temp++;total--;
		}
	}
}
cout<<endl;

return 0;
}


#include <bits/stdc++.h>
#include <string>
using namespace std;
#define SYNC ios_base::sync_with_stdio (false) ; cin.tie(0) ; cout.tie(0) ;
#define inf 0x3f3f3f3f
#define INF 1000111000111000111LL
#define mod 1000000007
#define pi acos(-1.0)
#define eps 1e-8
#define endl '\n'
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define fi first
#define se second
#define all(cc) (cc).begin(),(cc).end()
typedef long long int lli;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<bool> vb;
typedef vector<vector<int> > vvi;
typedef vector<long long int> vlli;
typedef vector<pair<int,int> > vpii;

int main(){
SYNC
int n,m,k;cin>>n>>m>>k;
int total=n*m;
int temp=0;
int special=0;
if(k==1) {special=1;cout<<n*m<<" ";}
for(int i=1;i<=n;i++){
	if(i%2){
		for(int j=1;j<=m;j++){
			if(special){
				total--;
				cout<<i<<" "<<j;
				if(!total) cout<<endl;
				else cout<<" ";
				continue;
			}
			if(temp%2==0){
				cout<<2<<" ";
				cout<<i<<" "<<j<<" ";

			}
			else{
				cout<<i<<" "<<j<<endl;
				k--;
				if(k==1){
					special=1;
					cout<<total<<" ";
				}
			}
			temp++;total--;
		}
	}
	else {
		for(int j=m;j>=1;j--){
			if(special){
				total--;
				cout<<i<<" "<<j;
				if(!total) cout<<endl;
				else cout<<" ";
				continue;
			}
			if(temp%2==0){
				cout<<2<<" ";
				cout<<i<<" "<<j<<" ";

			}
			else{
				cout<<i<<" "<<j<<endl;
				k--;
				if(k==1){
					special=1;
					cout<<total<<" ";
				}
			}
			temp++;total--;
		}
	}
}
// cout<<endl;

return 0;
}


#include <bits/stdc++.h>
#include <string>
using namespace std;
#define SYNC ios_base::sync_with_stdio (false) ; cin.tie(0) ; cout.tie(0) ;
#define inf 0x3f3f3f3f
#define INF 1000111000111000111LL
#define mod 1000000007
#define pi acos(-1.0)
#define eps 1e-8
#define endl '\n'
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define fi first
#define se second
#define all(cc) (cc).begin(),(cc).end()
typedef long long int lli;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<bool> vb;
typedef vector<vector<int> > vvi;
typedef vector<long long int> vlli;
typedef vector<pair<int,int> > vpii;

int main(){
SYNC
int n,m,k;cin>>n>>m>>k;
int total=n*m;
int temp=0;
int special=0;
if(k==1) {special=1;cout<<n*m<<" ";}
for(int i=1;i<=n;i++){
	if(i%2){
		for(int j=1;j<=m;j++){
			if(special){
				total--;
				cout<<i<<" "<<j;
				if(!total) cout<<endl;
				else cout<<" ";
				continue;
			}
			if(temp%2==0){
				cout<<2<<" ";
				cout<<i<<" "<<j<<" ";

			}
			else{
				cout<<i<<" "<<j<<endl;
				k--;
				if(k==1){
					special=1;
					cout<<total-1<<" ";
				}
			}
			temp++;total--;
		}
	}
	else {
		for(int j=m;j>=1;j--){
			if(special){
				total--;
				cout<<i<<" "<<j;
				if(!total) cout<<endl;
				else cout<<" ";
				continue;
			}
			if(temp%2==0){
				cout<<2<<" ";
				cout<<i<<" "<<j<<" ";

			}
			else{
				cout<<i<<" "<<j<<endl;
				k--;
				if(k==1){
					special=1;
					cout<<total-1<<" ";
				}
			}
			temp++;total--;
		}
	}
}
// cout<<endl;

return 0;
}


#include <bits/stdc++.h>
#include <string>
using namespace std;
#define SYNC ios_base::sync_with_stdio (false) ; cin.tie(0) ; cout.tie(0) ;
#define inf 0x3f3f3f3f
#define INF 1000111000111000111LL
#define mod 1000000007
#define pi acos(-1.0)
#define eps 1e-8
#define endl '\n'
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define fi first
#define se second
#define all(cc) (cc).begin(),(cc).end()
typedef long long int lli;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<bool> vb;
typedef vector<vector<int> > vvi;
typedef vector<long long int> vlli;
typedef vector<pair<int,int> > vpii;

int main(){
SYNC
int n,m,k;cin>>n>>m>>k;
int total=n*m;
int temp=0;
int special=0;
if(k==1) {special=1;cout<<n*m<<" ";}
for(int i=1;i<=n;i++){
	if(i%2){
		for(int j=1;j<=m;j++){
			if(special){
				total--;
				cout<<i<<" "<<j;
				if(!total) cout<<endl;
				else cout<<" ";
				continue;
			}
			if(temp%2==0){
				cout<<2<<" ";
				cout<<i<<" "<<j<<" ";

			}
			else{
				cout<<i<<" "<<j<<endl;
				k--;
				if(k==1){
					special=1;
					cout<<total<<" ";
				}
			}
			temp++;total--;
		}
	}
	else {
		for(int j=m;j>=1;j--){
			if(special){
				total--;
				cout<<i<<" "<<j;
				if(!total) cout<<endl;
				else cout<<" ";
			}
			if(temp%2==0){
				cout<<2<<" ";
				cout<<i<<" "<<j<<" ";

			}
			else{
				cout<<i<<" "<<j<<endl;
				k--;
				if(k==1){
					special=1;
					cout<<total<<" ";
				}
			}
			temp++;total--;
		}
	}
}
cout<<endl;

return 0;
}


#include <bits/stdc++.h>
#include <string>
using namespace std;
#define SYNC ios_base::sync_with_stdio (false) ; cin.tie(0) ; cout.tie(0) ;
#define inf 0x3f3f3f3f
#define INF 1000111000111000111LL
#define mod 1000000007
#define pi acos(-1.0)
#define eps 1e-8
#define endl '\n'
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define fi first
#define se second
#define all(cc) (cc).begin(),(cc).end()
typedef long long int lli;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<bool> vb;
typedef vector<vector<int> > vvi;
typedef vector<long long int> vlli;
typedef vector<pair<int,int> > vpii;

int main(){
SYNC
int n,m,k;cin>>n>>m>>k;
int total=n*m;
int temp=0;
int special=0;
if(k==1) {special=1;cout<<n*m<<" ";}
for(int i=1;i<=n;i++){
	if(i%2){
		for(int j=1;j<=m;j++){
			if(special){
				cout<<i<<" "<<j<<" ";
				continue;
			}
			if(temp%2==0){
				cout<<2<<" ";
				cout<<i<<" "<<j<<" ";

			}
			else{
				cout<<i<<" "<<j<<endl;
				k--;
				if(k==1){
					special=1;
					cout<<total<<" ";
				}
			}
			temp++;total--;
		}
	}
	else {
		for(int j=m;j>=1;j--){
			if(special){
				cout<<i<<" "<<j<<" ";
				continue;
			}
			if(temp%2==0){
				cout<<2<<" ";
				cout<<i<<" "<<j<<" ";

			}
			else{
				cout<<i<<" "<<j<<endl;
				k--;
				if(k==1){
					special=1;
					cout<<total<<" ";
				}
			}
			temp++;total--;
		}
	}
}

return 0;
}


#include<iostream>
#include<map>
#include<vector>
using namespace std;
map<pair<int,int>,pair<int,int> > adj;
vector<pair<int,int> > seq[200005];
int n,m,k,first,last,avg,sq;
int main()
{   cin>>n>>m>>k;
    if(k>n*m)   k=n*m;
    for(int i=1;i<=n;i++)
    {   for(int j=1;j<=m;j++)
        {   if((i%2==0&&j==1)||(i%2==1&&j==m))
                adj[make_pair(i,j)]=(make_pair(i+1,j));
            else if(i%2==1&&j<=m-1)
            {
                adj[make_pair(i,j)]=(make_pair(i,j+1));
            }
            else if(i%2==0&&j>=2)
            {
                adj[make_pair(i,j)]=(make_pair(i,j-1));
            }
        }
    }
    int counter=1,counter1=1;
    pair<int,int> node=make_pair(1,1);
    while(counter1<=n*m)
    {   if(counter>n*m/k){  sq++;   counter=0;  }
        if(counter<=n*m/k)  seq[sq].push_back(node);
        node=adj[node];
        counter++;  counter1++;
    }
    for(int i=0;i<sq+1;i++)
    {   cout<<seq[i].size()<<" ";
        for(int j=0;j<seq[i].size();j++)
            cout<<seq[i][j].first<<" "<<seq[i][j].second<<" ";
        cout<<"\n";
    }
    return 0;
}


#include<iostream>
#include<map>
#include<vector>
using namespace std;
map<pair<int,int>,pair<int,int> > adj;
vector<pair<int,int> > seq[200005];
int main()
{   int n,m,k;
    cin>>n>>m>>k;
    if(k>n*m)   k=n*m;
    for(int i=1;i<=n;i++)
    {   for(int j=1;j<=m;j++)
        {   if((i%2==0&&j==1)||(i%2==1&&j==m))
                adj[make_pair(i,j)]=(make_pair(i+1,j));
            else if(i%2==1&&j<=m-1)
            {
                adj[make_pair(i,j)]=(make_pair(i,j+1));
            }
            else if(i%2==0&&j>=2)
            {
                adj[make_pair(i,j)]=(make_pair(i,j-1));
            }
        }
    }
    int counter=1,counter1=1,sq=0;
    pair<int,int> node=make_pair(1,1);
    while(counter1<=n*m)
    {   if(counter>(n*m)/k){  sq=sq+1;   counter=1;  }
        seq[sq].push_back(node);
        node=adj[node];
        counter++;  counter1++;
    }
    int small=999,index=0;
    for(int i=0;i<sq+1;i++)
    {   cout<<seq[i].size()<<" ";
        for(int j=0;j<seq[i].size();j++)
            cout<<seq[i][j].first<<" "<<seq[i][j].second<<" ";
        cout<<"\n";
    }
    return 0;
}


#include<iostream>
#include<map>
#include<vector>
using namespace std;
map<pair<int,int>,pair<int,int> > adj;
int main()
{   int n,m,k;
    cin>>n>>m>>k;
    if(k>n*m)   k=n*m;
    for(int i=1;i<=n;i++)
    {   for(int j=1;j<=m;j++)
        {   if((i%2==0&&j==1)||(i%2==1&&j==m))
                adj[make_pair(i,j)]=(make_pair(i+1,j));
            else if(i%2==1&&j<=m-1)
            {
                adj[make_pair(i,j)]=(make_pair(i,j+1));
            }
            else if(i%2==0&&j>=2)
            {
                adj[make_pair(i,j)]=(make_pair(i,j-1));
            }
        }
    }
    int counter=1,counter1=1,sq=0,total=n*m;
    pair<int,int> node=make_pair(1,1);
    for(int i=0;i<k-1;i++)
    {   cout<<"2 ";
        for(int j=0;j<2;j++)
        {   cout<<node.first<<" "<<node.second<<" ";
            node=adj[node];
        }
        cout<<"\n";
    }
    int last=total-2*(k-1);
    cout<<last<<" ";
    for(int i=0;i<last;i++)
    {   cout<<node.first<<" "<<node.second<<" ";
        node=adj[node];
    }
    return 0;
}


#include<iostream>
#include<map>
#include<vector>
using namespace std;
map<pair<int,int>,pair<int,int> > adj;
vector<pair<int,int> > seq[2005];
int n,m,k,first,last,avg,sq;
void dfs(pair<int,int> node,int counter,int counter1)
{   if(counter1>n*m) return;
    if(counter>n*m/k){  sq++;   counter=0;  }
    if(counter<=n*m/k)  seq[sq].push_back(node);
    dfs(adj[node],counter+1,counter1+1);
}
int main()
{   cin>>n>>m>>k;
    for(int i=1;i<=n;i++)
    {   for(int j=1;j<=m;j++)
        {   if((i%2==0&&j==1)||(i%2==1&&j==m))
                adj[make_pair(i,j)]=(make_pair(i+1,j));
            else if(i%2==1&&j<=m-1)
            {
                adj[make_pair(i,j)]=(make_pair(i,j+1));
            }
            else if(i%2==0&&j>=2)
            {
                adj[make_pair(i,j)]=(make_pair(i,j-1));
            }
        }
    }
    dfs(make_pair(1,1),1,1);
    for(int i=0;i<sq+1;i++)
    {   cout<<seq[i].size()<<" ";
        for(int j=0;j<seq[i].size();j++)
            cout<<seq[i][j].first<<" "<<seq[i][j].second<<" ";
        cout<<"\n";
    }
    return 0;
}