#include <iostream>
#include <vector>

using namespace std;

int main()
{
    int T, i, j, x, s, br;
    cin >> T;
    vector <int>a;

    for(i = 0; i < T; i++){
        cin >> x;
        a.push_back(x);
    }

    for(i = 0; i < T; i++){
        vector <int>a2;
        br = 0;
        for(j = 1; j <= a[i]; j++){
            s = 1;
            while(j != 0){
                s = s * (x % 10);
                j = j / 10;
            }
            if(a2[s] != s){
                a2[s] = s;
                br++;
            }
        }
        cout << br << endl;
    }
}