#include <bits/stdc++.h>
using namespace std;
int main()
{
short t;
cin >> t;
int count=0;
while(t--)
{
int n;
cin >> n;
for (int i=1; i<=n; i++)
{
bool b=1;
string s = to_string(i);
for (int j =0; j<s.length()-1;j++)
{
if (s[j]!=s[j+1])
{
b =0;
}
}
if (b==1)
{
count++;
}
}
cout << count << endl;
}
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CgppbnQgbWFpbigpCnsKICAgIHNob3J0IHQ7CiAgICBjaW4gPj4gdDsKICAgIGludCBjb3VudD0wOwogICAgd2hpbGUodC0tKQogICAgewogICAgICAgIGludCBuOwogICAgICAgIGNpbiA+PiBuOwoKICAgICAgICBmb3IgKGludCBpPTE7IGk8PW47IGkrKykKICAgICAgICB7CiAgICAgICAgICAgIGJvb2wgYj0xOwogICAgICAgICAgICBzdHJpbmcgcyA9IHRvX3N0cmluZyhpKTsKICAgICAgICAgICAgZm9yIChpbnQgaiA9MDsgajxzLmxlbmd0aCgpLTE7aisrKQogICAgICAgICAgICB7CiAgICAgICAgICAgICAgICBpZiAoc1tqXSE9c1tqKzFdKQogICAgICAgICAgICAgICAgewogICAgICAgICAgICAgICAgICAgIGIgPTA7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgIH0KICAgICAgICAgICAgaWYgKGI9PTEpCiAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgIGNvdW50Kys7CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgY291dCA8PCBjb3VudCA8PCBlbmRsOwogICAgfQogICAgCn0=